altos/draw: Add ao_text_width
[fw/altos] / src / draw / ao_draw.h
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
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
15 #ifndef _AO_DRAW_H_
16 #define _AO_DRAW_H_
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include "ao_font.h"
21 #include "ao_box.h"
22
23 struct ao_bitmap {
24         uint32_t        *base;
25         int16_t         stride; /* in units */
26         int16_t         width;  /* in pixels */
27         int16_t         height; /* in pixels */
28         struct ao_box   damage;
29 };
30
31 #define AO_BITMAP_STRIDE(width) (((width) + 31) >> 5)
32
33 struct ao_coord {
34         float   x, y;
35 };
36
37 struct ao_transform {
38         float   x_scale, x_off;
39         float   y_scale, y_off;
40 };
41
42 static inline float ao_t_x(float x, float y, const struct ao_transform *t)
43 {
44         (void) y;
45         return x * t->x_scale + t->x_off;
46 }
47
48 static inline int16_t ao_t_xi(float x, float y, const struct ao_transform *t)
49 {
50         return (int16_t) (ao_t_x(x, y, t) + 0.5f);
51 }
52
53 static inline float ao_t_y(float x, float y, const struct ao_transform *t)
54 {
55         (void) x;
56         return y * t->y_scale + t->y_off;
57 }
58
59 static inline int16_t ao_t_yi(float x, float y, const struct ao_transform *t)
60 {
61         return (int16_t) (ao_t_y(x, y, t) + 0.5f);
62 }
63
64 static inline float ao_t_x_c(const struct ao_coord      *c,
65                              const struct ao_transform  *t)
66 {
67         return ao_t_x(c->x, c->y, t);
68 }
69
70 static inline float ao_t_y_c(const struct ao_coord      *c,
71                              const struct ao_transform  *t)
72 {
73         return ao_t_y(c->x, c->y, t);
74 }
75
76 static inline int16_t
77 ao_stride(int16_t width)
78 {
79         return (int16_t) ((width + 31) >> 5);
80 }
81
82 static inline int16_t
83 ao_stride_bytes(int16_t width)
84 {
85         return (int16_t) ((width + 7) >> 3);
86 }
87
88 static inline void
89 ao_damage_set_empty(struct ao_bitmap *dst)
90 {
91         ao_box_set_empty(&dst->damage);
92 }
93
94 struct ao_pattern {
95         uint8_t         pattern[8];
96 };
97
98 struct ao_glyph_metrics {
99         int8_t  width;
100         int8_t  height;
101         int8_t  x_off;
102         int8_t  y_off;
103         int8_t  advance;
104 };
105
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
114 struct ao_font {
115         const uint8_t   *bytes;
116         const uint16_t  *pos;
117         const struct ao_glyph_metrics *metrics;
118         int16_t max_width;
119         int16_t max_height;
120         int16_t ascent;
121 };
122
123 void
124 ao_copy(struct ao_bitmap        *dst,
125         int16_t                 dst_x,
126         int16_t                 dst_y,
127         int16_t                 width,
128         int16_t                 height,
129         const struct ao_bitmap  *src,
130         int16_t                 src_x,
131         int16_t                 src_y,
132         uint8_t                 rop);
133
134 void
135 ao_rect(struct ao_bitmap        *dst,
136         int16_t                 x,
137         int16_t                 y,
138         int16_t                 width,
139         int16_t                 height,
140         uint32_t                fill,
141         uint8_t                 rop);
142
143 void
144 ao_pattern(struct ao_bitmap             *dst,
145            int16_t                      x,
146            int16_t                      y,
147            int16_t                      width,
148            int16_t                      height,
149            const struct ao_pattern      *pattern,
150            int16_t                      pat_x,
151            int16_t                      pat_y,
152            uint8_t                      rop);
153
154 void
155 ao_line(struct ao_bitmap        *dst,
156         int16_t                 x1,
157         int16_t                 y1,
158         int16_t                 x2,
159         int16_t                 y2,
160         uint32_t                fill,
161         uint8_t                 rop);
162
163 void
164 ao_poly(struct ao_bitmap                *dst,
165         const struct ao_coord           *coords,
166         uint16_t                        ncoords,
167         const struct ao_transform       *transform,
168         uint32_t                        fill,
169         uint8_t                         rop);
170
171 int16_t
172 ao_text(struct ao_bitmap        *dst,
173         const struct ao_font    *font,
174         int16_t                 x,
175         int16_t                 y,
176         const char              *string,
177         uint32_t                fill,
178         uint8_t                 rop);
179
180 int16_t
181 ao_text_width(const struct ao_font      *font,
182               const char                *string);
183
184 void
185 ao_logo_poly(struct ao_bitmap           *dst,
186              const struct ao_transform  *transform,
187              uint32_t                   fill,
188              uint8_t                    rop);
189
190 void
191 ao_logo(struct ao_bitmap                *dst,
192         const struct ao_transform       *transform,
193         const struct ao_font            *font,
194         uint32_t                        fill,
195         uint8_t                         rop);
196
197 extern const struct ao_transform ao_identity;
198
199 #define AO_SHIFT        5
200 #define AO_UNIT         (1 << AO_SHIFT)
201 #define AO_MASK         (AO_UNIT - 1)
202 #define AO_ALLONES      ((uint32_t) -1)
203
204 /*
205  *          dst
206  *         0   1
207  *
208  *      0  a   b
209  *  src
210  *      1  c   d
211  *
212  *      ROP = abcd
213  */
214
215 #define AO_CLEAR         0x0    /* 0 */
216 #define AO_AND           0x1    /* src AND dst */
217 #define AO_AND_REVERSE   0x2    /* src AND NOT dst */
218 #define AO_COPY          0x3    /* src */
219 #define AO_AND_INVERTED  0x4    /* NOT src AND dst */
220 #define AO_NOOP          0x5    /* dst */
221 #define AO_XOR           0x6    /* src XOR dst */
222 #define AO_OR            0x7    /* src OR dst */
223 #define AO_NOR           0x8    /* NOT src AND NOT dst */
224 #define AO_EQUIV         0x9    /* NOT src XOR dst */
225 #define AO_INVERT        0xa    /* NOT dst */
226 #define AO_OR_REVERSE    0xb    /* src OR NOT dst */
227 #define AO_COPY_INVERTED 0xc    /* NOT src */
228 #define AO_OR_INVERTED   0xd    /* NOT src OR dst */
229 #define AO_NAND          0xe    /* NOT src OR NOT dst */
230 #define AO_SET           0xf    /* 1 */
231
232 #define AO_WHITE        0xffffffff
233 #define AO_BLACK        0x00000000
234
235 #endif /* _AO_DRAW_H_ */