]> git.karo-electronics.de Git - oswald.git/blob - ui/oswald_graphics.c
Some minor improvements.
[oswald.git] / ui / oswald_graphics.c
1 #include "oswald.h"
2 #include "oswald_strings.h"
3 #include "oswald_fonts.h"
4 #include "oswald_hal.h"
5
6 #include "oswald_graphics.h"
7
8
9 #if 0
10 void oswald_draw_pixel(const unsigned int xstart, const unsigned int ystart, uint8_t color)
11 {
12         hal_lcd_set_pixel(xstart, ystart, TRUE);
13 }
14 #endif
15
16 void oswald_draw_bitmap_opts(const unsigned int xstart, const unsigned int ystart,
17                 const unsigned int xoff, const unsigned int yoff,
18                 const unsigned int width, const unsigned int height,
19                 const boolean invert,
20                 const unsigned int bmp_width, const unsigned int bmp_height,
21                 const void *bmp)
22 {
23         unsigned int x, y;
24         uint8_t *cb;
25
26         if (bmp == NULL)
27                 return;
28
29         //g_printerr("dbmp %d,%d off %d,%d\n", xstart, ystart, width, height);
30         cb = (uint8_t *)bmp;
31         //g_printerr("dat %02x %02x %02x\n", (uint8_t)cb[0], (uint8_t)cb[1], (uint8_t)cb[2]);
32         // we only draw set pixel, unset pixel remain as they are
33         for (y=yoff; y<bmp_height && y<height; y++) {
34                 for (x=xoff; x<bmp_width && x<width; x++) {
35                         cb = (uint8_t *)(bmp + (y * ((bmp_width / 8) + ((bmp_width % 8) ? 1 : 0))) + (x / 8));
36                         // g_printerr("dat %02x %02x %02x\n", (uint8_t)cb[0], (uint8_t)cb[1], (uint8_t)cb[2]);
37                         if (*cb & (1 << (x % 8))) {
38                                 if (!invert)
39                                         hal_lcd_set_pixel((xstart + x) - xoff, (ystart + y) - yoff, TRUE);
40                                 // g_printerr("X");
41                         } else {
42                                 if (invert)
43                                         hal_lcd_set_pixel((xstart + x) - xoff, (ystart + y) - yoff, TRUE);
44                                 // g_printerr(".");
45                         }
46                 }
47                 //g_printerr("\n");
48         }
49 }
50
51 void oswald_draw_line(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend)
52 {
53         int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
54  
55         dx = xend - xstart;
56         dy = yend - ystart;
57  
58         incx = (dx >= 0) ? 1 : -1;
59         incy = (dy >= 0) ? 1 : -1;
60
61         if (dx<0)
62                 dx = -dx;
63         if (dy<0)
64                 dy = -dy;
65  
66         if (dx>dy) {
67                 pdx = incx; pdy = 0;
68                 ddx=incx; ddy=incy;
69                 es =dy;   el =dx;
70         } else {
71                 pdx=0;    pdy=incy;
72                 ddx=incx; ddy=incy;
73                 es =dx;   el =dy;
74         }
75  
76         x = xstart;
77         y = ystart;
78         err = el/2;
79         hal_lcd_set_pixel(x, y, TRUE);
80  
81         for (t = 0; t < el; ++t) {
82                 err -= es; 
83                 if (err < 0) {
84                         err += el;
85                         x += ddx;
86                         y += ddy;
87                 } else {
88                         x += pdx;
89                         y += pdy;
90                 }
91                 hal_lcd_set_pixel(x, y, TRUE);
92         }
93 }
94
95 void oswald_draw_line_ww(const uint8_t xstart, const uint8_t ystart, const uint8_t xend, const uint8_t yend, const uint8_t thickness)
96 {
97         int16_t i, x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
98  
99         dx = xend - xstart;
100         dy = yend - ystart;
101  
102         incx = (dx >= 0) ? 1 : -1;
103         incy = (dy >= 0) ? 1 : -1;
104
105         if (dx<0)
106                 dx = -dx;
107         if (dy<0)
108                 dy = -dy;
109  
110         if (dx>dy) {
111                 pdx = incx;
112                 pdy = 0;
113                 ddx = incx;
114                 ddy = incy;
115                 es = dy;
116                 el = dx;
117         } else {
118                 pdx = 0;
119                 pdy = incy;
120                 ddx = incx;
121                 ddy = incy;
122                 es = dx;
123                 el = dy;
124         }
125  
126         x = xstart;
127         y = ystart;
128         err = el/2;
129         hal_lcd_set_pixel(x, y, TRUE);
130         for (i=1; i<thickness; i++) {
131                 hal_lcd_set_pixel(x-i, y, TRUE);
132                 hal_lcd_set_pixel(x+i, y, TRUE);
133                 hal_lcd_set_pixel(x, y-i, TRUE);
134                 hal_lcd_set_pixel(x, y+i, TRUE);
135         }
136  
137         for (t = 0; t < el; ++t) {
138                 err -= es; 
139                 if (err < 0) {
140                         err += el;
141                         x += ddx;
142                         y += ddy;
143                 } else {
144                         x += pdx;
145                         y += pdy;
146                 }
147                 hal_lcd_set_pixel(x, y, TRUE);
148                 for (i=1; i<thickness; i++) {
149                         hal_lcd_set_pixel(x-i, y, TRUE);
150                         hal_lcd_set_pixel(x+i, y, TRUE);
151                         hal_lcd_set_pixel(x, y-i, TRUE);
152                         hal_lcd_set_pixel(x, y+i, TRUE);
153                 }
154         }
155 }
156
157 uint8_t oswald_write_character(const uint8_t x, const uint8_t y, const oswald_font_face face, const boolean invert, const uint8_t Character)
158 {
159         uint8_t *cdata = oswald_fonts[face].data;
160         uint8_t cwidth;
161         int csize;
162
163 //      if (Character == 32) { // space, blank no need to draw one ;)
164 //              return oswald_fonts[face].width / 2;
165 //      }
166
167         csize = ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * oswald_fonts[face].height;
168         if (oswald_fonts[face].font_type == FONT_TYPE_PROPORTIONAL)
169                 csize += 1; // at least 1px spacing
170
171         //csize += (oswald_fonts[face].height / 8) + ((oswald_fonts[face].height % 8) ? 1 : 0);
172
173         // g_printerr("fp = 0x%08lx cdata = 0x%08lx\n", font_7x12, cdata);
174
175         cdata = (cdata + ((int)csize * (int)Character));
176
177         //g_printerr("%02x\n", oswald_fonts[face].data[0][0]);
178         //g_printerr("char %02x face %d %dx%d csize %d\n", Character, face, oswald_fonts[face].width, oswald_fonts[face].height, csize);
179         //g_printerr("char %02x %02x %02x\n", (uint8_t)cdata[0], (uint8_t)cdata[1], (uint8_t)cdata[2]);
180
181         // oswald_draw_bitmap(x, y, oswald_fonts[face].height, oswald_fonts[face].height, cdata);
182         if (oswald_fonts[face].font_type == FONT_TYPE_MONOSPACE) {
183                 cwidth = oswald_fonts[face].width;
184                 //oswald_draw_bitmap(x, y, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * 8, oswald_fonts[face].height, (uint8_t *)cdata);
185                 oswald_draw_bitmap_opts(x,y,0,0,((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * 8,oswald_fonts[face].height,invert,((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0)) * 8,oswald_fonts[face].height,(uint8_t *)cdata);
186         } else if (oswald_fonts[face].font_type == FONT_TYPE_PROPORTIONAL) {
187                 cwidth = cdata[0];
188                 cdata++;
189                 // oswald_draw_bitmap_size(x, y, (cwidth+1), oswald_fonts[face].height, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, (uint8_t *)cdata);
190                 oswald_draw_bitmap_opts(x,y,0,0,(cwidth+1),oswald_fonts[face].height,invert,((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8,oswald_fonts[face].height,(uint8_t *)cdata);
191         } else {
192                 cwidth = 0;
193         }
194         // oswald_draw_bitmap_offset(x, y, (oswald_fonts[face].width % 8) > 0 ? (8-(oswald_fonts[face].width % 8)) : 0, 0, ((oswald_fonts[face].width / 8) + ((oswald_fonts[face].width % 8) ? 1 : 0))*8, oswald_fonts[face].height, cdata);
195
196         return cwidth;
197 }
198
199 void oswald_write_string(const uint8_t x, const uint8_t y, const oswald_font_face face, const boolean invert, char *str)
200 {
201         uint8_t lx, i, strl;
202
203         strl = oswald_strlen(str);
204         if (strl == 0)
205                 return;
206
207         lx = x;
208         for (i=0; i<strl; i++) {
209                 lx += oswald_write_character(lx, y, face, invert, str[i]);
210         }
211 }
212
213 uint8_t oswald_write_string_length(const uint8_t x, const uint8_t y, const uint8_t len, const oswald_font_face face, const boolean invert, char *str)
214 {
215         uint8_t lx, i, strl;
216
217         strl = oswald_strlen(str);
218         if (strl == 0)
219                 return;
220
221         lx = x;
222         for (i=0; i<strl; i++) {
223                 lx += oswald_write_character(lx, y, face, invert, str[i]);
224                 if (lx > len)
225                         break;
226         }
227
228         return (i+1);
229 }
230
231 void oswald_write_number(const uint8_t x, const uint8_t y, const oswald_font_face face, const boolean invert, const int16_t number)
232 {
233         uint8_t lx, i, strl;
234         char str[8];
235
236         itoa(number, str, 10);
237         strl = oswald_strlen(str);
238         if (strl == 0)
239                 return;
240
241         lx = x;
242         for (i=0; i<strl; i++) {
243                 lx += oswald_write_character(lx, y, face, invert, str[i]);
244         }
245 }
246