Changeset c78a03d in mainline
- Timestamp:
- 2020-07-21T22:48:59Z (4 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5592c56
- Parents:
- 703c743
- Location:
- uspace/lib/gfxfont
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/include/gfx/glyph.h
r703c743 rc78a03d 40 40 #include <types/gfx/font.h> 41 41 #include <types/gfx/glyph.h> 42 #include <types/gfx/glyph_bmp.h> 42 #include <stdbool.h> 43 #include <stddef.h> 43 44 44 45 extern void gfx_glyph_metrics_init(gfx_glyph_metrics_t *); … … 50 51 extern errno_t gfx_glyph_set_pattern(gfx_glyph_t *, const char *); 51 52 extern void gfx_glyph_clear_pattern(gfx_glyph_t *, const char *); 52 extern errno_t gfx_glyph_open_bmp(gfx_glyph_t *, gfx_glyph_bmp_t **); 53 extern bool gfx_glyph_matches(gfx_glyph_t *, const char *, size_t *); 54 extern gfx_glyph_pattern_t *gfx_glyph_first_pattern(gfx_glyph_t *); 55 extern gfx_glyph_pattern_t *gfx_glyph_next_pattern(gfx_glyph_pattern_t *); 56 extern const char *gfx_glyph_pattern_str(gfx_glyph_pattern_t *); 53 57 54 58 #endif -
uspace/lib/gfxfont/include/gfx/glyph_bmp.h
r703c743 rc78a03d 39 39 #include <errno.h> 40 40 #include <gfx/coord.h> 41 #include <types/gfx/glyph.h> 41 42 #include <types/gfx/glyph_bmp.h> 42 43 44 extern errno_t gfx_glyph_bmp_open(gfx_glyph_t *, gfx_glyph_bmp_t **); 43 45 extern errno_t gfx_glyph_bmp_save(gfx_glyph_bmp_t *); 44 46 extern void gfx_glyph_bmp_close(gfx_glyph_bmp_t *); -
uspace/lib/gfxfont/include/types/gfx/font.h
r703c743 rc78a03d 45 45 typedef struct { 46 46 /** Ascent */ 47 gfx_coord_t a cent;47 gfx_coord_t ascent; 48 48 /** Descent */ 49 49 gfx_coord_t descent; -
uspace/lib/gfxfont/include/types/gfx/glyph.h
r703c743 rc78a03d 42 42 typedef struct gfx_glyph gfx_glyph_t; 43 43 44 struct gfx_glyph_pattern; 45 typedef struct gfx_glyph_pattern gfx_glyph_pattern_t; 46 44 47 /** Glyph metrics */ 45 48 typedef struct { -
uspace/lib/gfxfont/private/font.h
r703c743 rc78a03d 38 38 #define _GFX_PRIVATE_FONT_H 39 39 40 #include <adt/list.h> 40 41 #include <types/gfx/context.h> 41 42 #include <types/gfx/font.h> … … 50 51 /** Font metrics */ 51 52 gfx_font_metrics_t metrics; 53 /** Glyphs */ 54 list_t glyphs; 52 55 }; 53 56 -
uspace/lib/gfxfont/private/glyph.h
r703c743 rc78a03d 49 49 struct gfx_font *font; 50 50 /** Link to list of glyphs in the font */ 51 li st_t lglyphs;51 link_t lglyphs; 52 52 /** Glyph metrics */ 53 53 gfx_glyph_metrics_t metrics; 54 /** Text patterns (of gfx_glyph_pattern_t) */ 55 list_t patterns; 56 }; 57 58 /** Glyph pattern. 59 * 60 * Glyph is set if pattern is found in text. 61 */ 62 struct gfx_glyph_pattern { 63 /** Containing glyph */ 64 struct gfx_glyph *glyph; 65 /** Link to gfx_glyph.patterns */ 66 link_t lpatterns; 67 /** Pattern text */ 68 char *text; 54 69 }; 55 70 -
uspace/lib/gfxfont/private/glyph_bmp.h
r703c743 rc78a03d 38 38 #define _GFX_PRIVATE_GLYPH_BMP_H 39 39 40 #include <gfx/coord.h> 41 40 42 /** Glyph bitmap 41 43 * … … 49 51 /** Containing glyph */ 50 52 struct gfx_glyph *glyph; 53 /** Rectangle covered by bitmap */ 54 gfx_rect_t rect; 55 /** Pixel array */ 56 int *pixels; 51 57 }; 52 58 -
uspace/lib/gfxfont/src/font.c
r703c743 rc78a03d 34 34 */ 35 35 36 #include <adt/list.h> 36 37 #include <assert.h> 37 38 #include <errno.h> 38 39 #include <gfx/font.h> 40 #include <gfx/glyph.h> 39 41 #include <mem.h> 40 42 #include <stdlib.h> 41 43 #include "../private/font.h" 44 #include "../private/glyph.h" 42 45 43 46 /** Initialize font metrics structure. … … 83 86 84 87 font->metrics = *metrics; 88 list_initialize(&font->glyphs); 85 89 *rfont = font; 86 90 return EOK; … … 93 97 void gfx_font_destroy(gfx_font_t *font) 94 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 95 107 free(font); 96 108 } … … 125 137 gfx_glyph_t *gfx_font_first_glyph(gfx_font_t *font) 126 138 { 127 return NULL; 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); 128 146 } 129 147 … … 135 153 gfx_glyph_t *gfx_font_next_glyph(gfx_glyph_t *cur) 136 154 { 137 return NULL; 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); 138 162 } 139 163 … … 149 173 gfx_glyph_t **rglyph, size_t *rsize) 150 174 { 151 return EOK; 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; 152 190 } 153 191 -
uspace/lib/gfxfont/src/glyph.c
r703c743 rc78a03d 39 39 #include <mem.h> 40 40 #include <stdlib.h> 41 #include <str.h> 42 #include "../private/font.h" 41 43 #include "../private/glyph.h" 42 44 … … 83 85 84 86 glyph->metrics = *metrics; 87 list_append(&glyph->lglyphs, &glyph->font->glyphs); 88 list_initialize(&glyph->patterns); 85 89 *rglyph = glyph; 86 90 return EOK; … … 93 97 void gfx_glyph_destroy(gfx_glyph_t *glyph) 94 98 { 99 list_remove(&glyph->lglyphs); 95 100 free(glyph); 96 101 } … … 130 135 errno_t gfx_glyph_set_pattern(gfx_glyph_t *glyph, const char *pattern) 131 136 { 137 gfx_glyph_pattern_t *pat; 138 139 pat = gfx_glyph_first_pattern(glyph); 140 while (pat != NULL) { 141 if (str_cmp(pat->text, pattern) == 0) { 142 /* Already set */ 143 return EOK; 144 } 145 146 pat = gfx_glyph_next_pattern(pat); 147 } 148 149 pat = calloc(1, sizeof(gfx_glyph_pattern_t)); 150 if (pat == NULL) 151 return ENOMEM; 152 153 pat->glyph = glyph; 154 pat->text = str_dup(pattern); 155 if (pat->text == NULL) { 156 free(pat); 157 return ENOMEM; 158 } 159 160 list_append(&pat->lpatterns, &glyph->patterns); 132 161 return EOK; 133 162 } … … 142 171 void gfx_glyph_clear_pattern(gfx_glyph_t *glyph, const char *pattern) 143 172 { 144 } 145 146 /** Open glyph bitmap for editing. 147 * 148 * @param glyph Glyph 149 * @param rbmp Place to store glyph bitmap 150 * @return EOK on success, ENOMEM if out of memory 151 */ 152 errno_t gfx_glyph_open_bmp(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp) 153 { 154 return EOK; 173 gfx_glyph_pattern_t *pat; 174 175 pat = gfx_glyph_first_pattern(glyph); 176 while (pat != NULL) { 177 if (str_cmp(pat->text, pattern) == 0) { 178 list_remove(&pat->lpatterns); 179 free(pat->text); 180 free(pat); 181 return; 182 } 183 184 pat = gfx_glyph_next_pattern(pat); 185 } 186 } 187 188 /** Determine if glyph maches the beginning of a string. 189 * 190 * @param glyph Glyph 191 * @param str String 192 * @param rsize Place to store number of bytes in the matching pattern 193 * @return @c true iff glyph matches the beginning of the string 194 */ 195 bool gfx_glyph_matches(gfx_glyph_t *glyph, const char *str, size_t *rsize) 196 { 197 gfx_glyph_pattern_t *pat; 198 199 pat = gfx_glyph_first_pattern(glyph); 200 while (pat != NULL) { 201 if (str_test_prefix(str, pat->text)) { 202 *rsize = str_size(pat->text); 203 return true; 204 } 205 206 pat = gfx_glyph_next_pattern(pat); 207 } 208 209 return false; 210 } 211 212 /** Get first glyph pattern. 213 * 214 * @param glyph Glyph 215 * @return First pattern or @c NULL if there are none 216 */ 217 gfx_glyph_pattern_t *gfx_glyph_first_pattern(gfx_glyph_t *glyph) 218 { 219 link_t *link; 220 221 link = list_first(&glyph->patterns); 222 if (link == NULL) 223 return NULL; 224 225 return list_get_instance(link, gfx_glyph_pattern_t, lpatterns); 226 } 227 228 /** Get next glyph pattern. 229 * 230 * @param cur Current pattern 231 * @return Next pattern or @c NULL if there are none 232 */ 233 gfx_glyph_pattern_t *gfx_glyph_next_pattern(gfx_glyph_pattern_t *cur) 234 { 235 link_t *link; 236 237 link = list_next(&cur->lpatterns, &cur->glyph->patterns); 238 if (link == NULL) 239 return NULL; 240 241 return list_get_instance(link, gfx_glyph_pattern_t, lpatterns); 242 } 243 244 /** Return pattern string. 245 * 246 * @param pattern Pattern 247 * @return Pattern string (owned by @a pattern) 248 */ 249 const char *gfx_glyph_pattern_str(gfx_glyph_pattern_t *pattern) 250 { 251 return pattern->text; 155 252 } 156 253 -
uspace/lib/gfxfont/src/glyph_bmp.c
r703c743 rc78a03d 35 35 36 36 #include <errno.h> 37 #include <gfx/coord.h> 37 38 #include <gfx/glyph_bmp.h> 38 39 #include <stdlib.h> 39 40 #include "../private/glyph_bmp.h" 41 42 static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *, gfx_coord2_t *); 43 44 /** Open glyph bitmap for editing. 45 * 46 * @param glyph Glyph 47 * @param rbmp Place to store glyph bitmap 48 * @return EOK on success, ENOMEM if out of memory 49 */ 50 errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp) 51 { 52 gfx_glyph_bmp_t *bmp; 53 54 bmp = calloc(1, sizeof(gfx_glyph_bmp_t)); 55 if (bmp == NULL) 56 return ENOMEM; 57 58 bmp->rect.p0.x = 0; 59 bmp->rect.p0.y = 0; 60 bmp->rect.p1.x = 0; 61 bmp->rect.p1.y = 0; 62 bmp->pixels = NULL; 63 64 bmp->glyph = glyph; 65 *rbmp = bmp; 66 return EOK; 67 } 40 68 41 69 /** Save glyph bitmap. … … 55 83 void gfx_glyph_bmp_close(gfx_glyph_bmp_t *bmp) 56 84 { 85 free(bmp->pixels); 86 free(bmp); 57 87 } 58 88 … … 66 96 int gfx_glyph_bmp_getpix(gfx_glyph_bmp_t *bmp, gfx_coord_t x, gfx_coord_t y) 67 97 { 68 return 0; 98 gfx_coord2_t pos; 99 size_t pitch; 100 101 pos.x = x; 102 pos.y = y; 103 if (!gfx_pix_inside_rect(&pos, &bmp->rect)) 104 return 0; 105 106 pitch = bmp->rect.p1.x - bmp->rect.p0.x; 107 return bmp->pixels[(y - bmp->rect.p0.y) * pitch + 108 (x - bmp->rect.p0.x)]; 69 109 } 70 110 … … 80 120 gfx_coord_t y, int value) 81 121 { 122 gfx_coord2_t pos; 123 size_t pitch; 124 errno_t rc; 125 126 pos.x = x; 127 pos.y = y; 128 if (!gfx_pix_inside_rect(&pos, &bmp->rect)) { 129 rc = gfx_glyph_bmp_extend(bmp, &pos); 130 if (rc != EOK) 131 return rc; 132 } 133 134 pitch = bmp->rect.p1.x - bmp->rect.p0.x; 135 bmp->pixels[(y - bmp->rect.p0.y) * pitch + 136 (x - bmp->rect.p0.x)] = value; 137 return EOK; 138 } 139 140 /** Extend glyph bitmap to cover a patricular pixel. 141 * 142 * @param bmp Glyph bitmap 143 * @param pos Pixel position 144 * 145 * @return EOK on sucesss, ENOMEM if out of memory 146 */ 147 static errno_t gfx_glyph_bmp_extend(gfx_glyph_bmp_t *bmp, gfx_coord2_t *pos) 148 { 149 gfx_rect_t prect; 150 gfx_rect_t nrect; 151 int *npixels; 152 size_t npitch; 153 size_t opitch; 154 gfx_coord_t x, y; 155 156 /* Compute new rectangle enveloping current rectangle and new pixel */ 157 prect.p0 = *pos; 158 prect.p1.x = prect.p0.x + 1; 159 prect.p1.y = prect.p0.y + 1; 160 161 gfx_rect_envelope(&bmp->rect, &prect, &nrect); 162 163 /* Allocate new pixel array */ 164 npixels = calloc(sizeof(int), (nrect.p1.x - nrect.p0.x) * 165 (nrect.p1.y - nrect.p0.y)); 166 if (npixels == NULL) 167 return ENOMEM; 168 169 /* Transfer pixel data */ 170 opitch = bmp->rect.p1.x - bmp->rect.p0.x; 171 npitch = nrect.p1.x - nrect.p0.x; 172 173 for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) { 174 for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) { 175 npixels[(y - nrect.p0.y) * npitch + x - nrect.p0.x] = 176 bmp->pixels[(y - bmp->rect.p0.y) * opitch + 177 x - bmp->rect.p0.x]; 178 } 179 } 180 181 /* Switch new and old data */ 182 free(bmp->pixels); 183 bmp->pixels = npixels; 184 bmp->rect = nrect; 185 82 186 return EOK; 83 187 } -
uspace/lib/gfxfont/test/font.c
r703c743 rc78a03d 27 27 */ 28 28 29 #include <gfx/context.h> 29 30 #include <gfx/font.h> 30 31 #include <pcut/pcut.h> … … 34 35 PCUT_TEST_SUITE(font); 35 36 36 PCUT_TEST(dummy) 37 { 37 static errno_t testgc_set_color(void *, gfx_color_t *); 38 static errno_t testgc_fill_rect(void *, gfx_rect_t *); 39 40 static gfx_context_ops_t test_ops = { 41 .set_color = testgc_set_color, 42 .fill_rect = testgc_fill_rect 43 }; 44 45 /** Test creating and destroying font */ 46 PCUT_TEST(create_destroy) 47 { 48 gfx_font_metrics_t metrics; 49 gfx_font_t *font; 50 gfx_context_t *gc; 51 errno_t rc; 52 53 rc = gfx_context_new(&test_ops, NULL, &gc); 54 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 55 56 gfx_font_metrics_init(&metrics); 57 rc = gfx_font_create(gc, &metrics, &font); 58 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 59 60 gfx_font_destroy(font); 61 rc = gfx_context_delete(gc); 62 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 63 } 64 65 /** Test gfx_font_get_metrics() */ 66 PCUT_TEST(get_metrics) 67 { 68 gfx_font_metrics_t metrics; 69 gfx_font_metrics_t gmetrics; 70 gfx_font_t *font; 71 gfx_context_t *gc; 72 errno_t rc; 73 74 rc = gfx_context_new(&test_ops, NULL, &gc); 75 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 76 77 gfx_font_metrics_init(&metrics); 78 metrics.ascent = 1; 79 metrics.descent = 2; 80 metrics.leading = 3; 81 82 rc = gfx_font_create(gc, &metrics, &font); 83 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 84 85 gfx_font_get_metrics(font, &gmetrics); 86 PCUT_ASSERT_INT_EQUALS(metrics.ascent, gmetrics.ascent); 87 PCUT_ASSERT_INT_EQUALS(metrics.descent, gmetrics.descent); 88 PCUT_ASSERT_INT_EQUALS(metrics.leading, gmetrics.leading); 89 90 gfx_font_destroy(font); 91 rc = gfx_context_delete(gc); 92 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 93 } 94 95 /** Test gfx_font_set_metrics() */ 96 PCUT_TEST(set_metrics) 97 { 98 gfx_font_metrics_t metrics1; 99 gfx_font_metrics_t metrics2; 100 gfx_font_metrics_t gmetrics; 101 gfx_font_t *font; 102 gfx_context_t *gc; 103 errno_t rc; 104 105 rc = gfx_context_new(&test_ops, NULL, &gc); 106 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 107 108 gfx_font_metrics_init(&metrics1); 109 metrics1.ascent = 1; 110 metrics1.descent = 2; 111 metrics1.leading = 3; 112 113 rc = gfx_font_create(gc, &metrics1, &font); 114 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 115 116 gfx_font_metrics_init(&metrics2); 117 metrics1.ascent = 4; 118 metrics1.descent = 5; 119 metrics1.leading = 6; 120 121 rc = gfx_font_set_metrics(font, &metrics2); 122 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 123 124 gfx_font_get_metrics(font, &gmetrics); 125 PCUT_ASSERT_INT_EQUALS(metrics2.ascent, gmetrics.ascent); 126 PCUT_ASSERT_INT_EQUALS(metrics2.descent, gmetrics.descent); 127 PCUT_ASSERT_INT_EQUALS(metrics2.leading, gmetrics.leading); 128 129 gfx_font_destroy(font); 130 rc = gfx_context_delete(gc); 131 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 132 } 133 134 /** Test gfx_font_first_glyph() */ 135 PCUT_TEST(first_glyph) 136 { 137 gfx_font_metrics_t metrics; 138 gfx_font_t *font; 139 gfx_context_t *gc; 140 gfx_glyph_t *glyph; 141 errno_t rc; 142 143 rc = gfx_context_new(&test_ops, NULL, &gc); 144 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 145 146 gfx_font_metrics_init(&metrics); 147 148 rc = gfx_font_create(gc, &metrics, &font); 149 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 150 151 glyph = gfx_font_first_glyph(font); 152 PCUT_ASSERT_NULL(glyph); 153 154 gfx_font_destroy(font); 155 rc = gfx_context_delete(gc); 156 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 157 } 158 159 /** Test gfx_font_next_glyph() */ 160 PCUT_TEST(next_glyph) 161 { 162 /* TODO */ 163 } 164 165 /** Test gfx_font_search_glyph() */ 166 PCUT_TEST(search_glyph) 167 { 168 gfx_font_metrics_t metrics; 169 gfx_font_t *font; 170 gfx_context_t *gc; 171 gfx_glyph_t *glyph; 172 size_t bytes; 173 errno_t rc; 174 175 rc = gfx_context_new(&test_ops, NULL, &gc); 176 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 177 178 gfx_font_metrics_init(&metrics); 179 180 rc = gfx_font_create(gc, &metrics, &font); 181 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 182 183 rc = gfx_font_search_glyph(font, "Hello", &glyph, &bytes); 184 PCUT_ASSERT_ERRNO_VAL(ENOENT, rc); 185 186 gfx_font_destroy(font); 187 rc = gfx_context_delete(gc); 188 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 189 } 190 191 static errno_t testgc_set_color(void *arg, gfx_color_t *color) 192 { 193 return EOK; 194 } 195 196 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect) 197 { 198 return EOK; 38 199 } 39 200 -
uspace/lib/gfxfont/test/glyph.c
r703c743 rc78a03d 27 27 */ 28 28 29 #include <gfx/context.h> 30 #include <gfx/font.h> 29 31 #include <gfx/glyph.h> 30 32 #include <pcut/pcut.h> 33 #include <stdbool.h> 34 #include <str.h> 31 35 32 36 PCUT_INIT; … … 34 38 PCUT_TEST_SUITE(glyph); 35 39 36 PCUT_TEST(dummy) 37 { 40 static errno_t testgc_set_color(void *, gfx_color_t *); 41 static errno_t testgc_fill_rect(void *, gfx_rect_t *); 42 43 static gfx_context_ops_t test_ops = { 44 .set_color = testgc_set_color, 45 .fill_rect = testgc_fill_rect 46 }; 47 48 /** Test creating and destroying glyph */ 49 PCUT_TEST(create_destroy) 50 { 51 gfx_font_metrics_t fmetrics; 52 gfx_font_t *font; 53 gfx_glyph_metrics_t gmetrics; 54 gfx_glyph_t *glyph; 55 gfx_context_t *gc; 56 errno_t rc; 57 58 rc = gfx_context_new(&test_ops, NULL, &gc); 59 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 60 61 gfx_font_metrics_init(&fmetrics); 62 rc = gfx_font_create(gc, &fmetrics, &font); 63 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 64 65 gfx_glyph_metrics_init(&gmetrics); 66 rc = gfx_glyph_create(font, &gmetrics, &glyph); 67 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 68 69 gfx_glyph_destroy(glyph); 70 71 gfx_font_destroy(font); 72 rc = gfx_context_delete(gc); 73 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 74 } 75 76 /** Test gfx_glyph_get_metrics() */ 77 PCUT_TEST(get_metrics) 78 { 79 gfx_font_metrics_t fmetrics; 80 gfx_font_t *font; 81 gfx_glyph_metrics_t gmetrics; 82 gfx_glyph_t *glyph; 83 gfx_glyph_metrics_t rmetrics; 84 gfx_context_t *gc; 85 errno_t rc; 86 87 rc = gfx_context_new(&test_ops, NULL, &gc); 88 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 89 90 gfx_font_metrics_init(&fmetrics); 91 rc = gfx_font_create(gc, &fmetrics, &font); 92 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 93 94 gfx_glyph_metrics_init(&gmetrics); 95 gmetrics.advance = 42; 96 97 rc = gfx_glyph_create(font, &gmetrics, &glyph); 98 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 99 100 gfx_glyph_get_metrics(glyph, &rmetrics); 101 PCUT_ASSERT_INT_EQUALS(gmetrics.advance, rmetrics.advance); 102 103 gfx_glyph_destroy(glyph); 104 105 gfx_font_destroy(font); 106 rc = gfx_context_delete(gc); 107 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 108 } 109 110 /** Test gfx_glyph_set_metrics() */ 111 PCUT_TEST(set_metrics) 112 { 113 gfx_font_metrics_t fmetrics; 114 gfx_font_t *font; 115 gfx_glyph_metrics_t gmetrics1; 116 gfx_glyph_metrics_t gmetrics2; 117 gfx_glyph_t *glyph; 118 gfx_glyph_metrics_t rmetrics; 119 gfx_context_t *gc; 120 errno_t rc; 121 122 rc = gfx_context_new(&test_ops, NULL, &gc); 123 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 124 125 gfx_font_metrics_init(&fmetrics); 126 rc = gfx_font_create(gc, &fmetrics, &font); 127 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 128 129 gfx_glyph_metrics_init(&gmetrics1); 130 gmetrics1.advance = 1; 131 132 rc = gfx_glyph_create(font, &gmetrics1, &glyph); 133 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 134 135 gfx_glyph_metrics_init(&gmetrics2); 136 gmetrics2.advance = 2; 137 138 gfx_glyph_set_metrics(glyph, &gmetrics2); 139 140 gfx_glyph_get_metrics(glyph, &rmetrics); 141 PCUT_ASSERT_INT_EQUALS(gmetrics2.advance, rmetrics.advance); 142 143 gfx_glyph_destroy(glyph); 144 145 gfx_font_destroy(font); 146 rc = gfx_context_delete(gc); 147 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 148 } 149 150 /** Test gfx_glyph_set_pattern() */ 151 PCUT_TEST(set_pattern) 152 { 153 gfx_font_metrics_t fmetrics; 154 gfx_font_t *font; 155 gfx_glyph_metrics_t gmetrics; 156 gfx_glyph_t *glyph; 157 gfx_context_t *gc; 158 errno_t rc; 159 160 rc = gfx_context_new(&test_ops, NULL, &gc); 161 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 162 163 gfx_font_metrics_init(&fmetrics); 164 rc = gfx_font_create(gc, &fmetrics, &font); 165 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 166 167 gfx_glyph_metrics_init(&gmetrics); 168 gmetrics.advance = 1; 169 170 rc = gfx_glyph_create(font, &gmetrics, &glyph); 171 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 172 173 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 174 175 /* Set a pattern */ 176 rc = gfx_glyph_set_pattern(glyph, "A"); 177 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 178 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 179 180 /* Setting the same pattern again should be OK */ 181 rc = gfx_glyph_set_pattern(glyph, "A"); 182 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 183 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 184 185 gfx_glyph_destroy(glyph); 186 187 gfx_font_destroy(font); 188 rc = gfx_context_delete(gc); 189 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 190 } 191 192 /** Test gfx_glyph_clear_pattern() */ 193 PCUT_TEST(clear_pattern) 194 { 195 gfx_font_metrics_t fmetrics; 196 gfx_font_t *font; 197 gfx_glyph_metrics_t gmetrics; 198 gfx_glyph_t *glyph; 199 gfx_context_t *gc; 200 errno_t rc; 201 202 rc = gfx_context_new(&test_ops, NULL, &gc); 203 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 204 205 gfx_font_metrics_init(&fmetrics); 206 rc = gfx_font_create(gc, &fmetrics, &font); 207 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 208 209 gfx_glyph_metrics_init(&gmetrics); 210 gmetrics.advance = 1; 211 212 rc = gfx_glyph_create(font, &gmetrics, &glyph); 213 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 214 215 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 216 217 /* Set a pattern */ 218 rc = gfx_glyph_set_pattern(glyph, "A"); 219 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 220 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 221 222 /* Now clear a different pattern - should be OK */ 223 gfx_glyph_clear_pattern(glyph, "AA"); 224 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 225 226 /* Now clear the pattern which has been set */ 227 gfx_glyph_clear_pattern(glyph, "A"); 228 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 229 230 gfx_glyph_destroy(glyph); 231 232 gfx_font_destroy(font); 233 rc = gfx_context_delete(gc); 234 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 235 } 236 237 /** Test gfx_glyph_matches() */ 238 PCUT_TEST(matches) 239 { 240 gfx_font_metrics_t fmetrics; 241 gfx_font_t *font; 242 gfx_glyph_metrics_t gmetrics; 243 gfx_glyph_t *glyph; 244 gfx_context_t *gc; 245 bool match; 246 size_t msize; 247 errno_t rc; 248 249 rc = gfx_context_new(&test_ops, NULL, &gc); 250 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 251 252 gfx_font_metrics_init(&fmetrics); 253 rc = gfx_font_create(gc, &fmetrics, &font); 254 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 255 256 gfx_glyph_metrics_init(&gmetrics); 257 gmetrics.advance = 1; 258 259 rc = gfx_glyph_create(font, &gmetrics, &glyph); 260 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 261 262 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 263 264 /* Set a pattern */ 265 rc = gfx_glyph_set_pattern(glyph, "AB"); 266 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 267 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 268 269 match = gfx_glyph_matches(glyph, "A", &msize); 270 PCUT_ASSERT_FALSE(match); 271 272 match = gfx_glyph_matches(glyph, "AB", &msize); 273 PCUT_ASSERT_TRUE(match); 274 PCUT_ASSERT_INT_EQUALS(2, msize); 275 276 match = gfx_glyph_matches(glyph, "ABC", &msize); 277 PCUT_ASSERT_TRUE(match); 278 PCUT_ASSERT_INT_EQUALS(2, msize); 279 280 match = gfx_glyph_matches(glyph, "BAB", &msize); 281 PCUT_ASSERT_FALSE(match); 282 283 gfx_glyph_destroy(glyph); 284 285 gfx_font_destroy(font); 286 rc = gfx_context_delete(gc); 287 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 288 } 289 290 /** Test gfx_glyph_first_pattern(), gfx_glyph_next_pattern() */ 291 PCUT_TEST(first_next_pattern) 292 { 293 gfx_font_metrics_t fmetrics; 294 gfx_font_t *font; 295 gfx_glyph_metrics_t gmetrics; 296 gfx_glyph_t *glyph; 297 gfx_context_t *gc; 298 gfx_glyph_pattern_t *pat; 299 errno_t rc; 300 301 rc = gfx_context_new(&test_ops, NULL, &gc); 302 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 303 304 gfx_font_metrics_init(&fmetrics); 305 rc = gfx_font_create(gc, &fmetrics, &font); 306 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 307 308 gfx_glyph_metrics_init(&gmetrics); 309 gmetrics.advance = 1; 310 311 rc = gfx_glyph_create(font, &gmetrics, &glyph); 312 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 313 314 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 315 316 /* Set a pattern */ 317 rc = gfx_glyph_set_pattern(glyph, "A"); 318 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 319 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 320 321 pat = gfx_glyph_first_pattern(glyph); 322 PCUT_ASSERT_NOT_NULL(pat); 323 324 pat = gfx_glyph_next_pattern(pat); 325 PCUT_ASSERT_NULL(pat); 326 327 gfx_glyph_destroy(glyph); 328 329 gfx_font_destroy(font); 330 rc = gfx_context_delete(gc); 331 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 332 } 333 334 /** Test gfx_glyph_pattern_str() */ 335 PCUT_TEST(pattern_str) 336 { 337 gfx_font_metrics_t fmetrics; 338 gfx_font_t *font; 339 gfx_glyph_metrics_t gmetrics; 340 gfx_glyph_t *glyph; 341 gfx_context_t *gc; 342 gfx_glyph_pattern_t *pat; 343 const char *pstr; 344 errno_t rc; 345 346 rc = gfx_context_new(&test_ops, NULL, &gc); 347 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 348 349 gfx_font_metrics_init(&fmetrics); 350 rc = gfx_font_create(gc, &fmetrics, &font); 351 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 352 353 gfx_glyph_metrics_init(&gmetrics); 354 gmetrics.advance = 1; 355 356 rc = gfx_glyph_create(font, &gmetrics, &glyph); 357 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 358 359 PCUT_ASSERT_NULL(gfx_glyph_first_pattern(glyph)); 360 361 /* Set a pattern */ 362 rc = gfx_glyph_set_pattern(glyph, "A"); 363 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 364 PCUT_ASSERT_NOT_NULL(gfx_glyph_first_pattern(glyph)); 365 366 pat = gfx_glyph_first_pattern(glyph); 367 PCUT_ASSERT_NOT_NULL(pat); 368 369 pstr = gfx_glyph_pattern_str(pat); 370 PCUT_ASSERT_INT_EQUALS(0, str_cmp("A", pstr)); 371 372 gfx_glyph_destroy(glyph); 373 374 gfx_font_destroy(font); 375 rc = gfx_context_delete(gc); 376 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 377 } 378 379 static errno_t testgc_set_color(void *arg, gfx_color_t *color) 380 { 381 return EOK; 382 } 383 384 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect) 385 { 386 return EOK; 38 387 } 39 388 -
uspace/lib/gfxfont/test/glyph_bmp.c
r703c743 rc78a03d 27 27 */ 28 28 29 #include <gfx/context.h> 30 #include <gfx/font.h> 29 31 #include <gfx/glyph.h> 32 #include <gfx/glyph_bmp.h> 30 33 #include <pcut/pcut.h> 31 34 … … 34 37 PCUT_TEST_SUITE(glyph_bmp); 35 38 36 PCUT_TEST(dummy) 37 { 39 static errno_t testgc_set_color(void *, gfx_color_t *); 40 static errno_t testgc_fill_rect(void *, gfx_rect_t *); 41 42 static gfx_context_ops_t test_ops = { 43 .set_color = testgc_set_color, 44 .fill_rect = testgc_fill_rect 45 }; 46 47 /** Test opening and closing glyph bitmap */ 48 PCUT_TEST(open_close) 49 { 50 gfx_font_metrics_t fmetrics; 51 gfx_font_t *font; 52 gfx_glyph_metrics_t gmetrics; 53 gfx_glyph_t *glyph; 54 gfx_context_t *gc; 55 gfx_glyph_bmp_t *bmp; 56 errno_t rc; 57 58 rc = gfx_context_new(&test_ops, NULL, &gc); 59 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 60 61 gfx_font_metrics_init(&fmetrics); 62 rc = gfx_font_create(gc, &fmetrics, &font); 63 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 64 65 gfx_glyph_metrics_init(&gmetrics); 66 gmetrics.advance = 1; 67 68 rc = gfx_glyph_create(font, &gmetrics, &glyph); 69 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 70 71 bmp = NULL; 72 73 rc = gfx_glyph_bmp_open(glyph, &bmp); 74 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 75 PCUT_ASSERT_NOT_NULL(bmp); 76 77 gfx_glyph_bmp_close(bmp); 78 79 gfx_glyph_destroy(glyph); 80 81 gfx_font_destroy(font); 82 rc = gfx_context_delete(gc); 83 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 84 } 85 86 /** Test glyph_bmp_save() */ 87 PCUT_TEST(save) 88 { 89 gfx_font_metrics_t fmetrics; 90 gfx_font_t *font; 91 gfx_glyph_metrics_t gmetrics; 92 gfx_glyph_t *glyph; 93 gfx_context_t *gc; 94 gfx_glyph_bmp_t *bmp; 95 errno_t rc; 96 97 rc = gfx_context_new(&test_ops, NULL, &gc); 98 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 99 100 gfx_font_metrics_init(&fmetrics); 101 rc = gfx_font_create(gc, &fmetrics, &font); 102 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 103 104 gfx_glyph_metrics_init(&gmetrics); 105 gmetrics.advance = 1; 106 107 rc = gfx_glyph_create(font, &gmetrics, &glyph); 108 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 109 110 bmp = NULL; 111 112 rc = gfx_glyph_bmp_open(glyph, &bmp); 113 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 114 PCUT_ASSERT_NOT_NULL(bmp); 115 116 /* TODO: Need a test to verify that save actually worked */ 117 rc = gfx_glyph_bmp_save(bmp); 118 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 119 120 gfx_glyph_bmp_close(bmp); 121 122 gfx_glyph_destroy(glyph); 123 124 gfx_font_destroy(font); 125 rc = gfx_context_delete(gc); 126 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 127 } 128 129 /** Test glyph_bmp_getpix() */ 130 PCUT_TEST(getpix) 131 { 132 gfx_font_metrics_t fmetrics; 133 gfx_font_t *font; 134 gfx_glyph_metrics_t gmetrics; 135 gfx_glyph_t *glyph; 136 gfx_context_t *gc; 137 gfx_glyph_bmp_t *bmp; 138 int pix; 139 errno_t rc; 140 141 rc = gfx_context_new(&test_ops, NULL, &gc); 142 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 143 144 gfx_font_metrics_init(&fmetrics); 145 rc = gfx_font_create(gc, &fmetrics, &font); 146 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 147 148 gfx_glyph_metrics_init(&gmetrics); 149 gmetrics.advance = 1; 150 151 rc = gfx_glyph_create(font, &gmetrics, &glyph); 152 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 153 154 bmp = NULL; 155 156 rc = gfx_glyph_bmp_open(glyph, &bmp); 157 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 158 PCUT_ASSERT_NOT_NULL(bmp); 159 160 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 161 PCUT_ASSERT_INT_EQUALS(0, pix); 162 163 gfx_glyph_bmp_close(bmp); 164 165 gfx_glyph_destroy(glyph); 166 167 gfx_font_destroy(font); 168 rc = gfx_context_delete(gc); 169 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 170 } 171 172 /** Test glyph_bmp_setpix() can flip pixel value both ways */ 173 PCUT_TEST(setpix_flip) 174 { 175 gfx_font_metrics_t fmetrics; 176 gfx_font_t *font; 177 gfx_glyph_metrics_t gmetrics; 178 gfx_glyph_t *glyph; 179 gfx_context_t *gc; 180 gfx_glyph_bmp_t *bmp; 181 int pix; 182 errno_t rc; 183 184 rc = gfx_context_new(&test_ops, NULL, &gc); 185 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 186 187 gfx_font_metrics_init(&fmetrics); 188 rc = gfx_font_create(gc, &fmetrics, &font); 189 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 190 191 gfx_glyph_metrics_init(&gmetrics); 192 gmetrics.advance = 1; 193 194 rc = gfx_glyph_create(font, &gmetrics, &glyph); 195 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 196 197 bmp = NULL; 198 199 rc = gfx_glyph_bmp_open(glyph, &bmp); 200 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 201 PCUT_ASSERT_NOT_NULL(bmp); 202 203 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 204 PCUT_ASSERT_INT_EQUALS(0, pix); 205 206 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1); 207 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 208 209 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 210 PCUT_ASSERT_INT_EQUALS(1, pix); 211 212 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 0); 213 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 214 215 pix = gfx_glyph_bmp_getpix(bmp, 0, 0); 216 PCUT_ASSERT_INT_EQUALS(0, pix); 217 218 gfx_glyph_bmp_close(bmp); 219 220 gfx_glyph_destroy(glyph); 221 222 gfx_font_destroy(font); 223 rc = gfx_context_delete(gc); 224 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 225 } 226 227 /** Test glyph_bmp_setpix() properly extends pixel array */ 228 PCUT_TEST(setpix_externd) 229 { 230 gfx_font_metrics_t fmetrics; 231 gfx_font_t *font; 232 gfx_glyph_metrics_t gmetrics; 233 gfx_glyph_t *glyph; 234 gfx_context_t *gc; 235 gfx_glyph_bmp_t *bmp; 236 gfx_coord_t x, y; 237 int pix; 238 errno_t rc; 239 240 rc = gfx_context_new(&test_ops, NULL, &gc); 241 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 242 243 gfx_font_metrics_init(&fmetrics); 244 rc = gfx_font_create(gc, &fmetrics, &font); 245 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 246 247 gfx_glyph_metrics_init(&gmetrics); 248 gmetrics.advance = 1; 249 250 rc = gfx_glyph_create(font, &gmetrics, &glyph); 251 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 252 253 bmp = NULL; 254 255 rc = gfx_glyph_bmp_open(glyph, &bmp); 256 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 257 PCUT_ASSERT_NOT_NULL(bmp); 258 259 /* 260 * Fill the rectangle [0, 0]..[3, 3] with alternating pixel pattern 261 * and then check it. 262 */ 263 264 rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1); 265 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 266 267 rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1); 268 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 269 270 rc = gfx_glyph_bmp_setpix(bmp, 2, 0, 1); 271 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 272 273 rc = gfx_glyph_bmp_setpix(bmp, 0, 2, 1); 274 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 275 276 rc = gfx_glyph_bmp_setpix(bmp, 2, 2, 1); 277 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 278 279 for (y = 0; y <= 2; y++) { 280 for (x = 0; x <= 2; x++) { 281 pix = gfx_glyph_bmp_getpix(bmp, x, y); 282 PCUT_ASSERT_INT_EQUALS((x & 1) ^ (y & 1) ^ 1, pix); 283 } 284 } 285 286 gfx_glyph_bmp_close(bmp); 287 288 gfx_glyph_destroy(glyph); 289 290 gfx_font_destroy(font); 291 rc = gfx_context_delete(gc); 292 PCUT_ASSERT_ERRNO_VAL(EOK, rc); 293 } 294 295 static errno_t testgc_set_color(void *arg, gfx_color_t *color) 296 { 297 return EOK; 298 } 299 300 static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect) 301 { 302 return EOK; 38 303 } 39 304
Note:
See TracChangeset
for help on using the changeset viewer.