3 * \brief Font functions
5 * \author Christian Helmuth <ch12@os.inf.tu-dresden.de>
6 * Frank Mehnert <fm3@os.inf.tu-dresden.de>
7 * Adam Lackorzynski <adam@os.inf.tu-dresden.de> */
10 * (c) 2001-2009 Author(s)
11 * economic rights: Technische Universität Dresden (Germany)
12 * This file is part of TUD:OS and distributed under the terms of the
13 * GNU Lesser General Public License 2.1.
14 * Please see the COPYING-LGPL-2.1 file for details.
17 #include <l4/libgfxbitmap/font.h>
18 #include <l4/libgfxbitmap/bitmap.h>
19 #include <l4/sys/l4int.h>
23 extern const char _binary_vgafont_psf_start[];
24 extern const char _binary_lat0_12_psf_start[];
25 extern const char _binary_lat0_14_psf_start[];
26 extern const char _binary_lat0_16_psf_start[];
32 { _binary_lat0_14_psf_start, "lat0-14" }, // first one is the default one
33 { _binary_lat0_12_psf_start, "lat0-12" },
34 { _binary_lat0_16_psf_start, "lat0-16" },
35 { _binary_vgafont_psf_start, "vgafont" },
44 unsigned char magic[2];
47 } __attribute__((packed));
51 struct psf_header header;
55 static struct psf_font *std_font;
58 static int check_magic(struct psf_font *f)
59 { return f->header.magic[0] == 0x36 && f->header.magic[1] == 0x4; }
61 static inline struct psf_font *font_cast(gfxbitmap_font_t font)
63 struct psf_font *f = (struct psf_font *)font;
64 if (!f || !check_magic(f))
69 static unsigned char font_yres(struct psf_font *f)
70 { return f->header.height; }
73 gfxbitmap_font_width(gfxbitmap_font_t font)
80 gfxbitmap_font_height(gfxbitmap_font_t font)
82 struct psf_font *f = font_cast(font);
83 return f ? f->header.height : 0;
88 get_font_char(struct psf_font *f, unsigned c)
90 return &f->data[(FONT_XRES / 8) * font_yres(f) * c];
94 gfxbitmap_font_data(gfxbitmap_font_t font, unsigned c)
96 struct psf_font *f = font_cast(font);
100 return get_font_char(f, c);
104 gfxbitmap_font_text(void *fb, l4re_video_view_info_t *vi,
105 gfxbitmap_font_t font, const char *text, unsigned len,
106 unsigned x, unsigned y,
107 gfxbitmap_color_pix_t fg, gfxbitmap_color_pix_t bg)
110 struct gfxbitmap_offset offset = {0,0,0};
111 struct psf_font *f = font_cast(font);
116 if (len == GFXBITMAP_USE_STRLEN)
119 for (i = 0; i < len; i++, text++)
121 /* optimization: collect spaces */
122 for (j = 0; (i < len) && (*text == ' '); i++, j++, text++)
127 gfxbitmap_fill(fb, vi, x, y, j * FONT_XRES, font_yres(f), bg);
134 gfxbitmap_bmap(fb, vi, x, y, FONT_XRES, font_yres(f),
135 get_font_char(f, *(unsigned char *)text), fg, bg,
136 &offset, pSLIM_BMAP_START_MSB);
143 gfxbitmap_font_text_scale(void *fb, l4re_video_view_info_t *vi,
144 gfxbitmap_font_t font, const char *text, unsigned len,
145 unsigned x, unsigned y,
146 gfxbitmap_color_pix_t fg, gfxbitmap_color_pix_t bg,
147 int scale_x, int scale_y)
152 unsigned rect_w = gfxbitmap_font_width(font) * scale_x;
154 struct psf_font *f = font_cast(font);
161 pix_x = scale_x * 14/15;
164 pix_y = scale_y * 14/15;
166 if (len == GFXBITMAP_USE_STRLEN)
169 for (i=0; i < len; i++, text++)
171 unsigned lrect_x = rect_x;
172 unsigned lrect_y = rect_y;
173 unsigned lrect_w = pix_x;
174 unsigned lrect_h = pix_y;
175 const char *bmap = get_font_char(f, *text);
178 for (j=0; j<font_yres(f); j++)
180 unsigned char mask = 0x80;
183 for (k=0; k<FONT_XRES; k++)
185 unsigned color = (*bmap & mask) ? fg : bg;
186 gfxbitmap_fill(fb, vi, lrect_x, lrect_y, lrect_w, lrect_h, color);
189 mask = (mask >> 1) | (mask << 7);
200 gfxbitmap_font_get(const char *name)
203 for (; i < sizeof(font_list) / sizeof(font_list[0]); ++i)
204 if (!strcmp(font_list[i].name, name))
205 return (gfxbitmap_font_t)font_list[i].fontdata;
211 gfxbitmap_font_init(void)
216 f = font_cast((gfxbitmap_font_t)font_list[0].fontdata);
218 /* check magic number of .psf */
220 return 1; // psf magic number failed
224 /* check file mode */
225 switch (f->header.mode)
236 return 2; // "bad psf font file magic %02x!", f->header.mode
240 printf("Font: Character size is %dx%d, font has %d characters\n",
241 FONT_XRES, font_yres(f), chars);