1 | /*
|
---|
2 | * Copyright (c) 2021 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/glyph.h>
|
---|
32 | #include <gfx/glyph_bmp.h>
|
---|
33 | #include <gfx/typeface.h>
|
---|
34 | #include <pcut/pcut.h>
|
---|
35 | #include <str.h>
|
---|
36 | #include "../private/font.h"
|
---|
37 | #include "../private/typeface.h"
|
---|
38 | #include "../private/testgc.h"
|
---|
39 |
|
---|
40 | PCUT_INIT;
|
---|
41 |
|
---|
42 | PCUT_TEST_SUITE(tpf);
|
---|
43 |
|
---|
44 | static const gfx_font_flags_t test_font_flags = gff_bold_italic;
|
---|
45 |
|
---|
46 | enum {
|
---|
47 | test_font_size = 9,
|
---|
48 | test_font_ascent = 4,
|
---|
49 | test_font_descent = 3,
|
---|
50 | test_font_leading = 2,
|
---|
51 | test_glyph_advance = 10
|
---|
52 | };
|
---|
53 |
|
---|
54 | static const char *test_glyph_pattern = "ff";
|
---|
55 |
|
---|
56 | /** Test saving typeface to and loading from TPF file */
|
---|
57 | PCUT_TEST(save_load)
|
---|
58 | {
|
---|
59 | char fname[L_tmpnam];
|
---|
60 | char *p;
|
---|
61 | gfx_font_props_t props;
|
---|
62 | gfx_font_props_t rprops;
|
---|
63 | gfx_font_metrics_t metrics;
|
---|
64 | gfx_font_metrics_t rmetrics;
|
---|
65 | gfx_glyph_metrics_t gmetrics;
|
---|
66 | gfx_glyph_metrics_t rgmetrics;
|
---|
67 | gfx_glyph_pattern_t *pat;
|
---|
68 | const char *str;
|
---|
69 | gfx_typeface_t *tface;
|
---|
70 | gfx_font_t *font;
|
---|
71 | gfx_font_info_t *finfo;
|
---|
72 | gfx_context_t *gc;
|
---|
73 | gfx_glyph_t *glyph;
|
---|
74 | gfx_glyph_bmp_t *bmp;
|
---|
75 | int pix;
|
---|
76 | test_gc_t tgc;
|
---|
77 | errno_t rc;
|
---|
78 |
|
---|
79 | p = tmpnam(fname);
|
---|
80 | PCUT_ASSERT_NOT_NULL(p);
|
---|
81 |
|
---|
82 | rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
|
---|
83 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
84 |
|
---|
85 | rc = gfx_typeface_create(gc, &tface);
|
---|
86 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
87 |
|
---|
88 | gfx_font_props_init(&props);
|
---|
89 | props.size = test_font_size;
|
---|
90 | props.flags = test_font_flags;
|
---|
91 |
|
---|
92 | gfx_font_metrics_init(&metrics);
|
---|
93 | metrics.ascent = test_font_ascent;
|
---|
94 | metrics.descent = test_font_descent;
|
---|
95 | metrics.leading = test_font_leading;
|
---|
96 |
|
---|
97 | rc = gfx_font_create(tface, &props, &metrics, &font);
|
---|
98 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
99 |
|
---|
100 | gfx_glyph_metrics_init(&gmetrics);
|
---|
101 | gmetrics.advance = test_glyph_advance;
|
---|
102 |
|
---|
103 | rc = gfx_glyph_create(font, &gmetrics, &glyph);
|
---|
104 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
105 |
|
---|
106 | rc = gfx_glyph_set_pattern(glyph, test_glyph_pattern);
|
---|
107 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
108 |
|
---|
109 | rc = gfx_glyph_bmp_open(glyph, &bmp);
|
---|
110 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
111 |
|
---|
112 | rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
|
---|
113 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
114 |
|
---|
115 | rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
|
---|
116 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
117 |
|
---|
118 | rc = gfx_glyph_bmp_save(bmp);
|
---|
119 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
120 |
|
---|
121 | gfx_glyph_bmp_close(bmp);
|
---|
122 |
|
---|
123 | rc = gfx_typeface_save(tface, fname);
|
---|
124 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
125 |
|
---|
126 | gfx_typeface_destroy(tface);
|
---|
127 | tface = NULL;
|
---|
128 |
|
---|
129 | rc = gfx_typeface_open(gc, fname, &tface);
|
---|
130 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
131 | PCUT_ASSERT_NOT_NULL(tface);
|
---|
132 |
|
---|
133 | finfo = gfx_typeface_first_font(tface);
|
---|
134 | PCUT_ASSERT_NOT_NULL(finfo);
|
---|
135 |
|
---|
136 | gfx_font_get_props(finfo, &rprops);
|
---|
137 | PCUT_ASSERT_INT_EQUALS(props.size, rprops.size);
|
---|
138 | PCUT_ASSERT_INT_EQUALS(props.flags, rprops.flags);
|
---|
139 |
|
---|
140 | rc = gfx_font_open(finfo, &font);
|
---|
141 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
142 | PCUT_ASSERT_NOT_NULL(font);
|
---|
143 |
|
---|
144 | gfx_font_get_metrics(font, &rmetrics);
|
---|
145 | PCUT_ASSERT_INT_EQUALS(metrics.ascent, rmetrics.ascent);
|
---|
146 | PCUT_ASSERT_INT_EQUALS(metrics.descent, rmetrics.descent);
|
---|
147 | PCUT_ASSERT_INT_EQUALS(metrics.leading, rmetrics.leading);
|
---|
148 |
|
---|
149 | glyph = gfx_font_first_glyph(font);
|
---|
150 | PCUT_ASSERT_NOT_NULL(glyph);
|
---|
151 |
|
---|
152 | gfx_glyph_get_metrics(glyph, &rgmetrics);
|
---|
153 | PCUT_ASSERT_INT_EQUALS(gmetrics.advance, rgmetrics.advance);
|
---|
154 |
|
---|
155 | pat = gfx_glyph_first_pattern(glyph);
|
---|
156 | str = gfx_glyph_pattern_str(pat);
|
---|
157 | PCUT_ASSERT_INT_EQUALS(0, str_cmp(test_glyph_pattern, str));
|
---|
158 |
|
---|
159 | rc = gfx_glyph_bmp_open(glyph, &bmp);
|
---|
160 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
161 |
|
---|
162 | pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
|
---|
163 | PCUT_ASSERT_INT_EQUALS(1, pix);
|
---|
164 | pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
|
---|
165 | PCUT_ASSERT_INT_EQUALS(1, pix);
|
---|
166 | pix = gfx_glyph_bmp_getpix(bmp, 1, 0);
|
---|
167 | PCUT_ASSERT_INT_EQUALS(0, pix);
|
---|
168 | pix = gfx_glyph_bmp_getpix(bmp, 0, 1);
|
---|
169 | PCUT_ASSERT_INT_EQUALS(0, pix);
|
---|
170 |
|
---|
171 | gfx_glyph_bmp_close(bmp);
|
---|
172 |
|
---|
173 | gfx_typeface_destroy(tface);
|
---|
174 | tface = NULL;
|
---|
175 |
|
---|
176 | rc = gfx_context_delete(gc);
|
---|
177 | PCUT_ASSERT_ERRNO_VAL(EOK, rc);
|
---|
178 |
|
---|
179 | (void) remove(p);
|
---|
180 | }
|
---|
181 |
|
---|
182 | PCUT_EXPORT(tpf);
|
---|