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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8349334 was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 4.5 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 "font.h"
42#include "font/embedded.h"
43#include "drawctx.h"
44
45font_t *font_create(font_backend_t *backend, void *backend_data)
46{
47 font_t *font = malloc(sizeof(font_t));
48 if (font == NULL)
49 return NULL;
50
51 font->backend = backend;
52 font->backend_data = backend_data;
53
54 return font;
55}
56
57void font_release(font_t *font)
58{
59 font->backend->release(font->backend_data);
60}
61
62errno_t font_get_metrics(font_t *font, font_metrics_t *metrics)
63{
64 return font->backend->get_font_metrics(font->backend_data, metrics);
65}
66
67errno_t font_resolve_glyph(font_t *font, wchar_t c, glyph_id_t *glyph_id)
68{
69 return font->backend->resolve_glyph(font->backend_data, c, glyph_id);
70}
71
72errno_t font_get_glyph_metrics(font_t *font, glyph_id_t glyph_id,
73 glyph_metrics_t *glyph_metrics)
74{
75 return font->backend->get_glyph_metrics(font->backend_data,
76 glyph_id, glyph_metrics);
77}
78
79errno_t font_render_glyph(font_t *font, drawctx_t *context, source_t *source,
80 sysarg_t x, sysarg_t y, glyph_id_t glyph_id)
81{
82 return font->backend->render_glyph(font->backend_data, context, source,
83 x, y, glyph_id);
84}
85
86/* TODO this is bad interface */
87errno_t font_get_box(font_t *font, char *text, sysarg_t *width, sysarg_t *height)
88{
89 font_metrics_t fm;
90 errno_t rc = font_get_metrics(font, &fm);
91 if (rc != EOK)
92 return rc;
93
94 native_t x = 0;
95
96 size_t off = 0;
97 while (true) {
98 wchar_t c = str_decode(text, &off, STR_NO_LIMIT);
99 if (c == 0)
100 break;
101
102 glyph_id_t glyph_id;
103 rc = font_resolve_glyph(font, c, &glyph_id);
104 if (rc != EOK) {
105 errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
106 if (rc2 != EOK) {
107 return rc;
108 }
109 }
110
111 glyph_metrics_t glyph_metrics;
112 rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
113 if (rc != EOK)
114 return rc;
115
116 x += glyph_metrics_get_advancement(&glyph_metrics);
117 }
118
119 *width = x;
120 *height = fm.ascender + fm.descender;
121 return EOK;
122}
123
124/* TODO this is bad interface */
125errno_t font_draw_text(font_t *font, drawctx_t *context, source_t *source,
126 const char *text, sysarg_t sx, sysarg_t sy)
127{
128 drawctx_save(context);
129 drawctx_set_compose(context, compose_over);
130
131 font_metrics_t fm;
132 errno_t rc = font_get_metrics(font, &fm);
133 if (rc != EOK)
134 return rc;
135
136 native_t baseline = sy + fm.ascender;
137 native_t x = sx;
138
139 size_t off = 0;
140 while (true) {
141 wchar_t c = str_decode(text, &off, STR_NO_LIMIT);
142 if (c == 0)
143 break;
144
145 glyph_id_t glyph_id;
146 rc = font_resolve_glyph(font, c, &glyph_id);
147 if (rc != EOK) {
148 errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
149 if (rc2 != EOK) {
150 return rc;
151 }
152 }
153
154 glyph_metrics_t glyph_metrics;
155 rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
156 if (rc != EOK)
157 return rc;
158
159 rc = font_render_glyph(font, context, source, x, baseline,
160 glyph_id);
161 if (rc != EOK)
162 return rc;
163
164 x += glyph_metrics_get_advancement(&glyph_metrics);
165
166 }
167
168 drawctx_restore(context);
169 source_set_mask(source, NULL, false);
170
171 return EOK;
172}
173
174/** @}
175 */
Note: See TracBrowser for help on using the repository browser.