uterm: move video API into uterm_video.h
[platform/upstream/kmscon.git] / src / font_unifont.c
1 /*
2  * kmscon - Fixed unifont font
3  *
4  * Copyright (c) 2012 Ted Kotz <ted@kotz.us>
5  * Copyright (c) 2012-2013 David Herrmann <dh.herrmann@googlemail.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files
9  * (the "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26
27 /**
28  * SECTION:font_unifont.c
29  * @short_description: Fixed unifont font
30  * @include: font.h
31  * 
32  * This is a fixed font renderer backend that supports just one font which is
33  * statically compiled into the file. This bitmap font has 8x16 and 16x16 
34  * glyphs. This can statically compile in any font defined as a unifont style
35  * hex format. This font is from the GNU unifont project available at: 
36  * http://unifoundry.com/unifont.html
37  *
38  * This file is heavily based on font_8x16.c
39  * 
40  */
41
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include "font.h"
46 #include "log.h"
47 #include "uterm_video.h"
48
49 #define LOG_SUBSYSTEM "font_unifont"
50
51 /* array is generated and compiled externally */
52 extern const struct kmscon_glyph kmscon_font_unifont_data_hex_glyphs[];
53 extern size_t kmscon_font_unifont_data_hex_len;
54
55 static int kmscon_font_unifont_init(struct kmscon_font *out,
56                                     const struct kmscon_font_attr *attr)
57 {
58         static const char name[] = "static-unifont";
59
60         log_debug("loading static unifont font");
61
62         memset(&out->attr, 0, sizeof(out->attr));
63         memcpy(out->attr.name, name, sizeof(name));
64         out->attr.bold = false;
65         out->attr.italic = false;
66         out->attr.width = 8;
67         out->attr.height = 16;
68         kmscon_font_attr_normalize(&out->attr);
69         out->baseline = 4;
70
71         return 0;
72 }
73
74 static void kmscon_font_unifont_destroy(struct kmscon_font *font)
75 {
76         log_debug("unloading static unifont font");
77 }
78
79 static int kmscon_font_unifont_render(struct kmscon_font *font, uint32_t id,
80                                       const uint32_t *ch, size_t len,
81                                       const struct kmscon_glyph **out)
82 {
83         if (len > 1 || *ch >= kmscon_font_unifont_data_hex_len)
84                 return -ERANGE;
85
86         *out = &kmscon_font_unifont_data_hex_glyphs[*ch];
87         return 0;
88 }
89
90 static int kmscon_font_unifont_render_inval(struct kmscon_font *font,
91                                             const struct kmscon_glyph **out)
92 {
93         if (0xfffd < kmscon_font_unifont_data_hex_len)
94                 *out = &kmscon_font_unifont_data_hex_glyphs[0xfffd];
95         else if ('?' < kmscon_font_unifont_data_hex_len)
96                 *out = &kmscon_font_unifont_data_hex_glyphs['?'];
97         else
98                 *out = &kmscon_font_unifont_data_hex_glyphs[0];
99
100         return 0;
101 }
102
103 static int kmscon_font_unifont_render_empty(struct kmscon_font *font,
104                                             const struct kmscon_glyph **out)
105 {
106         if (' ' < kmscon_font_unifont_data_hex_len) {
107                 *out = &kmscon_font_unifont_data_hex_glyphs[' '];
108                 return 0;
109         } else {
110                 return kmscon_font_unifont_render_inval(font, out);
111         }
112 }
113
114 struct kmscon_font_ops kmscon_font_unifont_ops = {
115         .name = "unifont",
116         .owner = NULL,
117         .init = kmscon_font_unifont_init,
118         .destroy = kmscon_font_unifont_destroy,
119         .render = kmscon_font_unifont_render,
120         .render_empty = kmscon_font_unifont_render_empty,
121         .render_inval = kmscon_font_unifont_render_inval,
122 };