new_editor: unify shared UI text measurement semantics
This commit is contained in:
39
new_editor/include/XCEditor/Foundation/UIEditorTextLayout.h
Normal file
39
new_editor/include/XCEditor/Foundation/UIEditorTextLayout.h
Normal 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
|
||||
Reference in New Issue
Block a user