Merge remote-tracking branch 'origin/master' into claude/issue-57
# Conflicts: # project.cpp
This commit is contained in:
commit
ce1eaf6366
11 changed files with 470 additions and 25 deletions
|
|
@ -87,6 +87,28 @@ std::uint32_t Font::GetLineWidth(const std::string_view text, float size) {
|
|||
return lineWidth;
|
||||
}
|
||||
|
||||
std::size_t Font::NearestCursorByte(std::string_view text, float size, float target) {
|
||||
if (target <= 0.0f) return 0;
|
||||
float scale = stbtt_ScaleForPixelHeight(&font, size);
|
||||
std::size_t best = 0; // byte offset 0 → caret before the first glyph
|
||||
float bestDist = target; // |target - 0|, and target > 0 here
|
||||
float cumWidth = 0.0f;
|
||||
std::size_t i = 0;
|
||||
while (i < text.size()) {
|
||||
std::uint32_t cp = DecodeUtf8(text, i); // i advances to the next boundary
|
||||
if (cp == 0) break;
|
||||
cumWidth += (int)(AdvanceUnits(cp) * scale); // match GetLineWidth's per-glyph rounding
|
||||
float d = std::abs(target - cumWidth);
|
||||
if (d < bestDist) {
|
||||
bestDist = d;
|
||||
best = i; // byte offset just past this codepoint
|
||||
} else {
|
||||
break; // monotonic advance ⇒ already past the closest boundary
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
float Font::LineHeight(float size) {
|
||||
float scale = stbtt_ScaleForPixelHeight(&font, size);
|
||||
return (ascent - descent + lineGap) * scale;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue