[simulator] Kill Cocoa and Console, move Qt4 one level up
[wikipediardware.git] / gui-lib / glyph.c
blob412aebbef2a8ef61b5cad67845718e4fd08c9956
1 /*
2 * guilib - a minimal pixel framework
3 * Copyright (c) 2008, 2009 Daniel Mack <daniel@caiaq.de>
4 * Copyright (c) 2009 Holger Hans Peter Freyther <zecke@openmoko.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 /* wiki-lib includes */
21 #include <wikilib.h>
22 #include <file-io.h>
24 /* gui-lib includes */
25 #include "guilib.h"
26 #include "glyph.h"
27 #include "fontfile.h"
28 #include <regs.h>
29 #include <lcd.h>
30 #include <wikireader.h>
32 void render_glyph(int start_x, int start_y, const struct glyph *glyph)
34 int x, y, w, bit = 0;
35 const char *d = glyph->data;
37 for (y = start_y; y < start_y + glyph->height; ++y) {
38 for (x = start_x, w = glyph->width; w > 0;) {
39 int use;
40 unsigned byte = (x + LCD_VRAM_WIDTH_PIXELS * y) / 8;
42 if (byte >= LCD_VRAM_SIZE)
43 return;
45 use = MIN(8 - (x % 8) , w);
46 use = MIN(8 - bit, use);
47 #ifdef DISPLAY_INVERTED
48 framebuffer[byte] &= ~((*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8));
49 #else
50 framebuffer[byte] |= (*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8);
51 #endif
52 bit += use;
53 x += use;
54 w -= use;
55 if (bit == 8) {
56 bit = 0;
57 d++;
63 #if 0
64 /* we might need that later ... */
65 static int simple_kerning(struct Glyph *a, struct Glyph *b)
67 int y, delta;
69 if (!a || !b)
70 return 0;
72 /* we do some very simple kerning here. The idea is to scan
73 * the left edge of the right glyph and the right edge of the
74 * left glyph and find out how far the two could move towards
75 * each other without colliding. */
76 delta = MIN(a->w, b->w);
77 for (y = 0; y < MIN(a->h, b->h); y++) {
78 int x, d = 0;
80 /* right edge of left glyph */
81 for (x = a->w - 1; x > 0; x--, d++)
82 if (glyph_pixel(a, x, y) != 0)
83 break;
85 /* left edge of right pixel */
86 for (x = 0; x < b->w; x++, d++)
87 if (glyph_pixel(b, x, y) != 0)
88 break;
90 /* find smallest value for all rows */
91 if (d < delta)
92 delta = d;
95 return delta;
97 #endif
100 * static copy a char map... true for some fonts e.g.
101 * the DejaVu family
103 static int char_to_glyph(char c)
105 if (c < 30)
106 return 0;
107 else
108 return c - 29;
112 * Simplistic string drawing
114 * @param font The font index to use
115 * @param string The string to draw. No text wrapping will be done
116 * @param start_x From where to start drawing (upper left)
117 * @param start_y From where to start drawing (upper left)
119 int render_string(const int font, int start_x,
120 int start_y, const char *string, const int text_length)
122 if ((unsigned int) font >= guilib_nr_fonts())
123 return 0;
125 int i;
126 int max_height = 0;
128 int x = start_x;
130 for (i = 0; i < text_length; ++i) {
131 const struct glyph *glyph = get_glyph(font,
132 char_to_glyph(string[i] & 0x7f));
134 /* painting and advance */
135 /* TODO: use the above auto kerning for the advance */
136 render_glyph(x, start_y - glyph->top_bearing, glyph);
137 x += glyph->width + 1;
139 if (glyph->height > max_height)
140 max_height = glyph->height;
143 return max_height;