source: mainline/uspace/lib/gfxfont/test/text.c@ 8fa65af0

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

Add text rendering routine and demo

  • Property mode set to 100644
File size: 5.5 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#include <gfx/context.h>
30#include <gfx/font.h>
31#include <gfx/text.h>
32#include <gfx/typeface.h>
33#include <pcut/pcut.h>
34#include "../private/font.h"
35#include "../private/typeface.h"
36
37PCUT_INIT;
38
39PCUT_TEST_SUITE(text);
40
41static errno_t testgc_set_color(void *, gfx_color_t *);
42static errno_t testgc_fill_rect(void *, gfx_rect_t *);
43static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
44 gfx_bitmap_alloc_t *, void **);
45static errno_t testgc_bitmap_destroy(void *);
46static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
47static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
48
49static gfx_context_ops_t test_ops = {
50 .set_color = testgc_set_color,
51 .fill_rect = testgc_fill_rect,
52 .bitmap_create = testgc_bitmap_create,
53 .bitmap_destroy = testgc_bitmap_destroy,
54 .bitmap_render = testgc_bitmap_render,
55 .bitmap_get_alloc = testgc_bitmap_get_alloc
56};
57
58typedef struct {
59 gfx_bitmap_params_t bm_params;
60 void *bm_pixels;
61 gfx_rect_t bm_srect;
62 gfx_coord2_t bm_offs;
63} test_gc_t;
64
65typedef struct {
66 test_gc_t *tgc;
67 gfx_bitmap_alloc_t alloc;
68 bool myalloc;
69} testgc_bitmap_t;
70
71/** Test text width computation with a dummy font */
72PCUT_TEST(dummy_text_width)
73{
74 gfx_font_props_t props;
75 gfx_font_metrics_t metrics;
76 gfx_typeface_t *tface;
77 gfx_font_t *font;
78 gfx_context_t *gc;
79 gfx_coord_t width;
80 test_gc_t tgc;
81 errno_t rc;
82
83 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
84 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
85
86 rc = gfx_typeface_create(gc, &tface);
87 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
88
89 gfx_font_props_init(&props);
90 gfx_font_metrics_init(&metrics);
91 rc = gfx_font_create(tface, &props, &metrics, &font);
92 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
93
94 width = gfx_text_width(font, "Hello world!");
95 PCUT_ASSERT_INT_EQUALS(0, width);
96
97 gfx_font_close(font);
98 gfx_typeface_destroy(tface);
99
100 rc = gfx_context_delete(gc);
101 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
102}
103
104/** Test text rendering with a dummy font */
105PCUT_TEST(dummy_puttext)
106{
107 gfx_font_props_t props;
108 gfx_font_metrics_t metrics;
109 gfx_typeface_t *tface;
110 gfx_font_t *font;
111 gfx_context_t *gc;
112 gfx_text_fmt_t fmt;
113 gfx_coord2_t pos;
114 test_gc_t tgc;
115 errno_t rc;
116
117 rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
118 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
119
120 rc = gfx_typeface_create(gc, &tface);
121 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
122
123 gfx_font_props_init(&props);
124 gfx_font_metrics_init(&metrics);
125 rc = gfx_font_create(tface, &props, &metrics, &font);
126 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
127
128 gfx_text_fmt_init(&fmt);
129 pos.x = 0;
130 pos.y = 0;
131
132 rc = gfx_puttext(font, &pos, &fmt, "Hello world!");
133 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
134
135 gfx_font_close(font);
136 gfx_typeface_destroy(tface);
137
138 rc = gfx_context_delete(gc);
139 PCUT_ASSERT_ERRNO_VAL(EOK, rc);
140}
141
142static errno_t testgc_set_color(void *arg, gfx_color_t *color)
143{
144 return EOK;
145}
146
147static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
148{
149 return EOK;
150}
151
152static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
153 gfx_bitmap_alloc_t *alloc, void **rbm)
154{
155 test_gc_t *tgc = (test_gc_t *) arg;
156 testgc_bitmap_t *tbm;
157
158 tbm = calloc(1, sizeof(testgc_bitmap_t));
159 if (tbm == NULL)
160 return ENOMEM;
161
162 if (alloc == NULL) {
163 tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
164 sizeof(uint32_t);
165 tbm->alloc.off0 = 0;
166 tbm->alloc.pixels = calloc(sizeof(uint32_t),
167 tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
168 tbm->myalloc = true;
169 if (tbm->alloc.pixels == NULL) {
170 free(tbm);
171 return ENOMEM;
172 }
173 } else {
174 tbm->alloc = *alloc;
175 }
176
177 tbm->tgc = tgc;
178 tgc->bm_params = *params;
179 tgc->bm_pixels = tbm->alloc.pixels;
180 *rbm = (void *)tbm;
181 return EOK;
182}
183
184static errno_t testgc_bitmap_destroy(void *bm)
185{
186 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
187 if (tbm->myalloc)
188 free(tbm->alloc.pixels);
189 free(tbm);
190 return EOK;
191}
192
193static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
194 gfx_coord2_t *offs)
195{
196 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
197 tbm->tgc->bm_srect = *srect;
198 tbm->tgc->bm_offs = *offs;
199 return EOK;
200}
201
202static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
203{
204 testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
205 *alloc = tbm->alloc;
206 return EOK;
207}
208
209PCUT_EXPORT(text);
Note: See TracBrowser for help on using the repository browser.