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