Skip to content

Commit 80e8618

Browse files
committed
Added MeasureText function
1 parent d18a8ce commit 80e8618

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

include/graphics.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,12 @@ int VerticalDrawText(Canvas *c, const Font &font, int x, int y,
137137
const Color &color, const Color *background_color,
138138
const char *utf8_text, int kerning_offset = 0);
139139

140+
// Figure out how many pixels long a bit of text will be when drawn
141+
// Useful for aligning text to the center or right
142+
// Return value will be the length of the text in pixels
143+
// (ie. the width for normal horizontal text)
144+
int MeasureText(const Font &font, const char *utf8_text, int kerning_offset = 0);
145+
140146
// Draw a circle centered at "x", "y", with a radius of "radius" and with "color"
141147
void DrawCircle(Canvas *c, int x, int y, int radius, const Color &color);
142148

lib/graphics.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,19 @@ int VerticalDrawText(Canvas *c, const Font &font, int x, int y,
117117
return y - start_y;
118118
}
119119

120+
int MeasureText(const Font &font, const char *utf8_text, int kerning_offset) {
121+
int result = 0;
122+
while (*utf8_text) {
123+
const uint32_t cp = utf8_next_codepoint(utf8_text);
124+
int width = font.CharacterWidth(cp);
125+
if (width > 0)
126+
result += width;
127+
if (*utf8_text)
128+
result += kerning_offset;
129+
}
130+
return result;
131+
}
132+
120133
void DrawCircle(Canvas *c, int x0, int y0, int radius, const Color &color) {
121134
int x = radius, y = 0;
122135
int radiusError = 1 - x;

0 commit comments

Comments
 (0)