]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/mag-gfx/include/font
update
[l4.git] / l4 / pkg / mag-gfx / include / font
1 // vi:ft=cpp
2 /*
3  * (c) 2010 Adam Lackorzynski <adam@os.inf.tu-dresden.de>,
4  *          Alexander Warg <warg@os.inf.tu-dresden.de>
5  *     economic rights: Technische Universität Dresden (Germany)
6  *
7  * This file is part of TUD:OS and distributed under the terms of the
8  * GNU General Public License 2.
9  * Please see the COPYING-GPL-2 file for details.
10  */
11 #pragma once
12
13 #include <l4/sys/types.h>
14
15 #include <l4/mag-gfx/geometry>
16
17 namespace Mag_gfx {
18
19 struct Font
20 {
21   unsigned char const *img;
22   l4_int32_t const *wtab, *otab;
23   int w, h;
24
25   explicit Font(void const *_ttf)
26   {
27     char const *ttf = (char const *)_ttf;
28
29     otab = (l4_int32_t const *)ttf;
30     wtab = (l4_int32_t const *)(ttf + 1024);
31     w = *(l4_int32_t const *)(ttf + 2048);
32     h = *(l4_int32_t const *)(ttf + 2052);
33     img = (unsigned char const *)(ttf + 2056);
34   }
35
36   /**
37    * Calculate width of string when printed with the font
38    */
39   int str_w(char const *_str) const
40   {
41     if (!_str)
42       return 0;
43
44     unsigned char const *str = (unsigned char const *)_str;
45     int res = 0;
46     for (; *str; ++str)
47       res += wtab[*str];
48
49     return res;
50   }
51
52   int str_w(char const *_str, int len) const
53   {
54     if (!_str)
55       return 0;
56
57     unsigned char const *str = (unsigned char const *)_str;
58     int res = 0;
59     for (; *str && len; ++str, --len)
60       res += wtab[*str];
61
62     return res;
63   }
64
65   /**
66    * Calculate height of string when printed with the font
67    */
68   int str_h(char const *) const { return h; }
69   int str_h(char const *, int) const { return h; }
70
71   Area str_sz(char const *s) const
72   { return Area(str_w(s), str_h(s)); }
73
74   Area str_sz(char const *s, int l) const
75   { return Area(str_w(s, l), str_h(s, l)); }
76
77
78 };
79
80 }