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