source: mainline/uspace/lib/draw/font.c@ de9992c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since de9992c was 28a5ebd, checked in by Martin Decky <martin@…>, 5 years ago

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2012 Petr Koupy
3 * Copyright (c) 2014 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup draw
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <errno.h>
38#include <stdlib.h>
39#include <str.h>
40
41#include <draw/font.h>
42#include <draw/drawctx.h>
43
44font_t *font_create(font_backend_t *backend, void *backend_data)
45{
46 font_t *font = malloc(sizeof(font_t));
47 if (font == NULL)
48 return NULL;
49
50 font->backend = backend;
51 font->backend_data = backend_data;
52
53 return font;
54}
55
56void font_release(font_t *font)
57{
58 font->backend->release(font->backend_data);
59}
60
61errno_t font_get_metrics(font_t *font, font_metrics_t *metrics)
62{
63 return font->backend->get_font_metrics(font->backend_data, metrics);
64}
65
66errno_t font_resolve_glyph(font_t *font, char32_t c, glyph_id_t *glyph_id)
67{
68 return font->backend->resolve_glyph(font->backend_data, c, glyph_id);
69}
70
71errno_t font_get_glyph_metrics(font_t *font, glyph_id_t glyph_id,
72 glyph_metrics_t *glyph_metrics)
73{
74 return font->backend->get_glyph_metrics(font->backend_data,
75 glyph_id, glyph_metrics);
76}
77
78errno_t font_render_glyph(font_t *font, drawctx_t *context, source_t *source,
79 sysarg_t x, sysarg_t y, glyph_id_t glyph_id)
80{
81 return font->backend->render_glyph(font->backend_data, context, source,
82 x, y, glyph_id);
83}
84
85/* TODO this is bad interface */
86errno_t font_get_box(font_t *font, char *text, sysarg_t *width, sysarg_t *height)
87{
88 font_metrics_t fm;
89 errno_t rc = font_get_metrics(font, &fm);
90 if (rc != EOK)
91 return rc;
92
93 native_t x = 0;
94
95 size_t off = 0;
96 while (true) {
97 char32_t c = str_decode(text, &off, STR_NO_LIMIT);
98 if (c == 0)
99 break;
100
101 glyph_id_t glyph_id;
102 rc = font_resolve_glyph(font, c, &glyph_id);
103 if (rc != EOK) {
104 errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
105 if (rc2 != EOK) {
106 return rc;
107 }
108 }
109
110 glyph_metrics_t glyph_metrics;
111 rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
112 if (rc != EOK)
113 return rc;
114
115 x += glyph_metrics_get_advancement(&glyph_metrics);
116 }
117
118 *width = x;
119 *height = fm.ascender + fm.descender;
120 return EOK;
121}
122
123/* TODO this is bad interface */
124errno_t font_draw_text(font_t *font, drawctx_t *context, source_t *source,
125 const char *text, sysarg_t sx, sysarg_t sy)
126{
127 drawctx_save(context);
128 drawctx_set_compose(context, compose_over);
129
130 font_metrics_t fm;
131 errno_t rc = font_get_metrics(font, &fm);
132 if (rc != EOK)
133 return rc;
134
135 native_t baseline = sy + fm.ascender;
136 native_t x = sx;
137
138 size_t off = 0;
139 while (true) {
140 char32_t c = str_decode(text, &off, STR_NO_LIMIT);
141 if (c == 0)
142 break;
143
144 glyph_id_t glyph_id;
145 rc = font_resolve_glyph(font, c, &glyph_id);
146 if (rc != EOK) {
147 errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
148 if (rc2 != EOK) {
149 return rc;
150 }
151 }
152
153 glyph_metrics_t glyph_metrics;
154 rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
155 if (rc != EOK)
156 return rc;
157
158 rc = font_render_glyph(font, context, source, x, baseline,
159 glyph_id);
160 if (rc != EOK)
161 return rc;
162
163 x += glyph_metrics_get_advancement(&glyph_metrics);
164
165 }
166
167 drawctx_restore(context);
168 source_set_mask(source, NULL, false);
169
170 return EOK;
171}
172
173/** @}
174 */
Note: See TracBrowser for help on using the repository browser.