source: mainline/uspace/lib/gfxfont/src/font.c@ c78a03d

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

Flesh out most of font, glyph and glyph bitmap implementation and tests

  • Property mode set to 100644
File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 2020 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libgfxfont
30 * @{
31 */
32/**
33 * @file Font
34 */
35
36#include <adt/list.h>
37#include <assert.h>
38#include <errno.h>
39#include <gfx/font.h>
40#include <gfx/glyph.h>
41#include <mem.h>
42#include <stdlib.h>
43#include "../private/font.h"
44#include "../private/glyph.h"
45
46/** Initialize font metrics structure.
47 *
48 * Font metrics structure must always be initialized using this function
49 * first.
50 *
51 * @param metrics Font metrics structure
52 */
53void gfx_font_metrics_init(gfx_font_metrics_t *metrics)
54{
55 memset(metrics, 0, sizeof(gfx_font_metrics_t));
56}
57
58/** Create font in graphics context.
59 *
60 * @param gc Graphic context
61 * @param metrics Font metrics
62 * @param rfont Place to store pointer to new font
63 *
64 * @return EOK on success, EINVAL if parameters are invald,
65 * ENOMEM if insufficient resources, EIO if graphic device connection
66 * was lost
67 */
68errno_t gfx_font_create(gfx_context_t *gc, gfx_font_metrics_t *metrics,
69 gfx_font_t **rfont)
70{
71 gfx_font_t *font;
72 errno_t rc;
73
74 font = calloc(1, sizeof(gfx_font_t));
75 if (font == NULL)
76 return ENOMEM;
77
78 font->gc = gc;
79
80 rc = gfx_font_set_metrics(font, metrics);
81 if (rc != EOK) {
82 assert(rc == EINVAL);
83 free(font);
84 return rc;
85 }
86
87 font->metrics = *metrics;
88 list_initialize(&font->glyphs);
89 *rfont = font;
90 return EOK;
91}
92
93/** Destroy font.
94 *
95 * @param font Font
96 */
97void gfx_font_destroy(gfx_font_t *font)
98{
99 gfx_glyph_t *glyph;
100
101 glyph = gfx_font_first_glyph(font);
102 while (glyph != NULL) {
103 gfx_glyph_destroy(glyph);
104 glyph = gfx_font_first_glyph(font);
105 }
106
107 free(font);
108}
109
110/** Get font metrics.
111 *
112 * @param font Font
113 * @param metrics Place to store metrics
114 */
115void gfx_font_get_metrics(gfx_font_t *font, gfx_font_metrics_t *metrics)
116{
117 *metrics = font->metrics;
118}
119
120/** Set font metrics.
121 *
122 * @param font Font
123 * @param metrics Place to store metrics
124 * @return EOK on success, EINVAL if supplied metrics are invalid
125 */
126errno_t gfx_font_set_metrics(gfx_font_t *font, gfx_font_metrics_t *metrics)
127{
128 font->metrics = *metrics;
129 return EOK;
130}
131
132/** Get first glyph in font.
133 *
134 * @param font Font
135 * @return First glyph or @c NULL if there are none
136 */
137gfx_glyph_t *gfx_font_first_glyph(gfx_font_t *font)
138{
139 link_t *link;
140
141 link = list_first(&font->glyphs);
142 if (link == NULL)
143 return NULL;
144
145 return list_get_instance(link, gfx_glyph_t, lglyphs);
146}
147
148/** Get next glyph in font.
149 *
150 * @param cur Current glyph
151 * @return Next glyph in font or @c NULL if @a cur was the last one
152 */
153gfx_glyph_t *gfx_font_next_glyph(gfx_glyph_t *cur)
154{
155 link_t *link;
156
157 link = list_next(&cur->lglyphs, &cur->font->glyphs);
158 if (link == NULL)
159 return NULL;
160
161 return list_get_instance(link, gfx_glyph_t, lglyphs);
162}
163
164/** Search for glyph that should be set for the beginning of a string.
165 *
166 * @param font Font
167 * @param str String whose beginning we would like to set
168 * @param rglyph Place to store glyph that should be set
169 * @param rsize Place to store number of bytes to advance in the string
170 * @return EOK on success, ENOENT if no matching glyph was found
171 */
172int gfx_font_search_glyph(gfx_font_t *font, const char *str,
173 gfx_glyph_t **rglyph, size_t *rsize)
174{
175 gfx_glyph_t *glyph;
176 size_t msize;
177
178 glyph = gfx_font_first_glyph(font);
179 while (glyph != NULL) {
180 if (gfx_glyph_matches(glyph, str, &msize)) {
181 *rglyph = glyph;
182 *rsize = msize;
183 return EOK;
184 }
185
186 glyph = gfx_font_next_glyph(glyph);
187 }
188
189 return ENOENT;
190}
191
192/** @}
193 */
Note: See TracBrowser for help on using the repository browser.