Skip to content

Commit d1fd905

Browse files
committed
altos/draw: Add ao_text_width
Computes the total advance of the given string, not the width of the resulting ink. Signed-off-by: Keith Packard <[email protected]>
1 parent f9103d1 commit d1fd905

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/draw/ao_draw.h

+12
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ struct ao_glyph_metrics {
103103
int8_t advance;
104104
};
105105

106+
struct ao_text_metrics {
107+
int16_t width;
108+
int16_t height;
109+
int16_t x_off;
110+
int16_t y_off;
111+
int16_t advance;
112+
};
113+
106114
struct ao_font {
107115
const uint8_t *bytes;
108116
const uint16_t *pos;
@@ -169,6 +177,10 @@ ao_text(struct ao_bitmap *dst,
169177
uint32_t fill,
170178
uint8_t rop);
171179

180+
int16_t
181+
ao_text_width(const struct ao_font *font,
182+
const char *string);
183+
172184
void
173185
ao_logo_poly(struct ao_bitmap *dst,
174186
const struct ao_transform *transform,

src/draw/ao_text_width.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright © 2024 Keith Packard <[email protected]>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful, but
10+
* WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along
15+
* with this program; if not, write to the Free Software Foundation, Inc.,
16+
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17+
*/
18+
19+
#include <ao_draw.h>
20+
#include <ao_draw_int.h>
21+
#include "ao_font.h"
22+
#include <string.h>
23+
#include <stdio.h>
24+
25+
int16_t
26+
ao_text_width(const struct ao_font *font,
27+
const char *string)
28+
{
29+
char c;
30+
int16_t x = 0;
31+
32+
while ((c = *string++)) {
33+
if (font->metrics) {
34+
x += font->metrics[(uint8_t) c].advance;
35+
} else {
36+
x += font->max_width;
37+
}
38+
}
39+
return x;
40+
}

0 commit comments

Comments
 (0)