new_editor: unify shared UI text measurement semantics

This commit is contained in:
2026-04-22 01:50:00 +08:00
parent 26e75e093e
commit 0ec0eff90c
16 changed files with 284 additions and 86 deletions

View File

@@ -0,0 +1,39 @@
#pragma once
#include <XCEditor/Foundation/UIEditorTextMeasurement.h>
#include <algorithm>
#include <string_view>
namespace XCEngine::UI::Editor {
inline float ClampUIEditorNonNegative(float value) {
return (std::max)(value, 0.0f);
}
inline float EstimateUIEditorSimpleTextWidth(
std::string_view text,
float estimatedGlyphWidth) {
return static_cast<float>(text.size()) * ClampUIEditorNonNegative(estimatedGlyphWidth);
}
inline float ResolveUIEditorMeasuredTextWidth(
std::string_view text,
float measuredWidth,
float fontSize,
float estimatedGlyphWidth,
const UIEditorTextMeasurer* textMeasurer = nullptr) {
if (measuredWidth > 0.0f) {
return measuredWidth;
}
if (textMeasurer != nullptr &&
!text.empty() &&
fontSize > 0.0f) {
return textMeasurer->MeasureTextWidth(UIEditorTextMeasureRequest { text, fontSize });
}
return EstimateUIEditorSimpleTextWidth(text, estimatedGlyphWidth);
}
} // namespace XCEngine::UI::Editor