Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfxfont/test/text.c

    r1fa6292 r7470d97  
    3030#include <gfx/context.h>
    3131#include <gfx/font.h>
    32 #include <gfx/glyph.h>
    3332#include <gfx/text.h>
    3433#include <gfx/typeface.h>
     
    3635#include "../private/font.h"
    3736#include "../private/typeface.h"
    38 #include "../private/testgc.h"
    3937
    4038PCUT_INIT;
    4139
    4240PCUT_TEST_SUITE(text);
     41
     42static errno_t testgc_set_clip_rect(void *, gfx_rect_t *);
     43static errno_t testgc_set_color(void *, gfx_color_t *);
     44static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     45static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     46    gfx_bitmap_alloc_t *, void **);
     47static errno_t testgc_bitmap_destroy(void *);
     48static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     49static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     50
     51static gfx_context_ops_t test_ops = {
     52        .set_clip_rect = testgc_set_clip_rect,
     53        .set_color = testgc_set_color,
     54        .fill_rect = testgc_fill_rect,
     55        .bitmap_create = testgc_bitmap_create,
     56        .bitmap_destroy = testgc_bitmap_destroy,
     57        .bitmap_render = testgc_bitmap_render,
     58        .bitmap_get_alloc = testgc_bitmap_get_alloc
     59};
     60
     61typedef struct {
     62        gfx_bitmap_params_t bm_params;
     63        void *bm_pixels;
     64        gfx_rect_t bm_srect;
     65        gfx_coord2_t bm_offs;
     66} test_gc_t;
     67
     68typedef struct {
     69        test_gc_t *tgc;
     70        gfx_bitmap_alloc_t alloc;
     71        bool myalloc;
     72} testgc_bitmap_t;
    4373
    4474/** Test text width computation with a dummy font */
     
    104134
    105135        gfx_text_fmt_init(&fmt);
    106         fmt.font = font;
    107136        fmt.color = color;
    108137        pos.x = 0;
    109138        pos.y = 0;
    110139
    111         rc = gfx_puttext(&pos, &fmt, "Hello world!");
     140        rc = gfx_puttext(font, &pos, &fmt, "Hello world!");
    112141        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    113142
     
    120149}
    121150
    122 /** gfx_text_start_pos() correctly computes text start position */
    123 PCUT_TEST(text_start_pos)
    124 {
    125         gfx_font_props_t props;
    126         gfx_font_metrics_t metrics;
    127         gfx_typeface_t *tface;
    128         gfx_font_t *font;
    129         gfx_context_t *gc;
    130         gfx_color_t *color;
    131         gfx_text_fmt_t fmt;
    132         gfx_coord2_t pos;
    133         test_gc_t tgc;
    134         errno_t rc;
    135 
    136         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    137         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    138 
    139         rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
    140         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    141 
    142         rc = gfx_typeface_create(gc, &tface);
    143         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    144 
    145         gfx_font_props_init(&props);
    146         gfx_font_metrics_init(&metrics);
    147         metrics.ascent = 10; // XXX
    148         metrics.descent = 10; // XXX
    149         rc = gfx_font_create(tface, &props, &metrics, &font);
    150         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    151 
    152         gfx_text_fmt_init(&fmt);
    153         fmt.font = font;
    154         fmt.color = color;
    155         pos.x = 0;
    156         pos.y = 0;
    157 
    158         rc = gfx_puttext(&pos, &fmt, "Hello world!");
    159         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    160 
    161         gfx_font_close(font);
    162         gfx_typeface_destroy(tface);
    163         gfx_color_delete(color);
    164 
    165         rc = gfx_context_delete(gc);
    166         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    167 }
    168 
    169 /** gfx_text_find_pos() finds position in text */
    170 PCUT_TEST(text_find_pos)
    171 {
    172         gfx_font_props_t props;
    173         gfx_font_metrics_t metrics;
    174         gfx_typeface_t *tface;
    175         gfx_font_t *font;
    176         gfx_glyph_metrics_t gmetrics;
    177         gfx_glyph_t *glyph1;
    178         gfx_glyph_t *glyph2;
    179         gfx_context_t *gc;
    180         gfx_text_fmt_t fmt;
    181         gfx_coord2_t anchor;
    182         gfx_coord2_t fpos;
    183         size_t off;
    184         test_gc_t tgc;
    185         errno_t rc;
    186 
    187         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    188         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    189 
    190         rc = gfx_typeface_create(gc, &tface);
    191         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    192 
    193         gfx_font_props_init(&props);
    194         gfx_font_metrics_init(&metrics);
    195         rc = gfx_font_create(tface, &props, &metrics, &font);
    196         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    197 
    198         /* Need to create some glyphs with metrics */
    199         gfx_glyph_metrics_init(&gmetrics);
    200         gmetrics.advance = 10;
    201 
    202         rc = gfx_glyph_create(font, &gmetrics, &glyph1);
    203         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    204 
    205         rc = gfx_glyph_set_pattern(glyph1, "A");
    206         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    207 
    208         gfx_glyph_metrics_init(&gmetrics);
    209         gmetrics.advance = 1;
    210 
    211         rc = gfx_glyph_create(font, &gmetrics, &glyph2);
    212         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    213 
    214         rc = gfx_glyph_set_pattern(glyph2, "i");
    215         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    216 
    217         gfx_text_fmt_init(&fmt);
    218         fmt.font = font;
    219         anchor.x = 10;
    220         anchor.y = 0;
    221 
    222         fpos.x = 9;
    223         fpos.y = 0;
    224         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    225         PCUT_ASSERT_INT_EQUALS(0, off);
    226 
    227         fpos.x = 10;
    228         fpos.y = 0;
    229         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    230         PCUT_ASSERT_INT_EQUALS(0, off);
    231 
    232         fpos.x = 11;
    233         fpos.y = 0;
    234         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    235         PCUT_ASSERT_INT_EQUALS(0, off);
    236 
    237         fpos.x = 19;
    238         fpos.y = 0;
    239         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    240         PCUT_ASSERT_INT_EQUALS(1, off);
    241 
    242         fpos.x = 20;
    243         fpos.y = 0;
    244         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    245         PCUT_ASSERT_INT_EQUALS(2, off);
    246 
    247         fpos.x = 21;
    248         fpos.y = 0;
    249         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    250         PCUT_ASSERT_INT_EQUALS(3, off);
    251 
    252         fpos.x = 22;
    253         fpos.y = 0;
    254         off = gfx_text_find_pos(&anchor, &fmt, "Aii", &fpos);
    255         PCUT_ASSERT_INT_EQUALS(3, off);
    256 
    257         gfx_glyph_destroy(glyph1);
    258         gfx_glyph_destroy(glyph2);
    259 
    260         gfx_font_close(font);
    261         gfx_typeface_destroy(tface);
    262 
    263         rc = gfx_context_delete(gc);
    264         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    265 }
    266 
    267 /** gfx_text_find_pos() finds position in text in text mode */
    268 PCUT_TEST(text_find_pos_text)
    269 {
    270         gfx_typeface_t *tface;
    271         gfx_font_t *font;
    272         gfx_context_t *gc;
    273         test_gc_t tgc;
    274         size_t off;
    275         gfx_text_fmt_t fmt;
    276         gfx_coord2_t anchor;
    277         gfx_coord2_t fpos;
    278         errno_t rc;
    279 
    280         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    281         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    282 
    283         rc = gfx_typeface_create(gc, &tface);
    284         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    285 
    286         rc = gfx_font_create_textmode(tface, &font);
    287         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    288 
    289         anchor.x = 10;
    290         anchor.y = 0;
    291         gfx_text_fmt_init(&fmt);
    292         fmt.font = font;
    293 
    294         fpos.x = 9;
    295         fpos.y = 0;
    296         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    297         PCUT_ASSERT_INT_EQUALS(0, off);
    298 
    299         fpos.x = 10;
    300         fpos.y = 0;
    301         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    302         PCUT_ASSERT_INT_EQUALS(0, off);
    303 
    304         fpos.x = 11;
    305         fpos.y = 0;
    306         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    307         PCUT_ASSERT_INT_EQUALS(1, off);
    308 
    309         fpos.x = 12;
    310         fpos.y = 0;
    311         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    312         PCUT_ASSERT_INT_EQUALS(2, off);
    313 
    314         fpos.x = 13;
    315         fpos.y = 0;
    316         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    317         PCUT_ASSERT_INT_EQUALS(3, off);
    318 
    319         fpos.x = 14;
    320         fpos.y = 0;
    321         off = gfx_text_find_pos(&anchor, &fmt, "Abc", &fpos);
    322         PCUT_ASSERT_INT_EQUALS(3, off);
    323 
    324         gfx_font_close(font);
    325         gfx_typeface_destroy(tface);
    326 
    327         rc = gfx_context_delete(gc);
    328         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    329 }
    330 
    331 /** gfx_text_cont() produces correct continuation parameters */
    332 PCUT_TEST(text_cont)
    333 {
    334         gfx_typeface_t *tface;
    335         gfx_font_t *font;
    336         gfx_context_t *gc;
    337         gfx_color_t *color;
    338         test_gc_t tgc;
    339         gfx_text_fmt_t fmt;
    340         gfx_coord2_t anchor;
    341         gfx_coord2_t cpos;
    342         gfx_text_fmt_t cfmt;
    343         errno_t rc;
    344 
    345         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    346         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    347 
    348         rc = gfx_typeface_create(gc, &tface);
    349         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    350 
    351         rc = gfx_font_create_textmode(tface, &font);
    352         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    353 
    354         rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
    355         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    356 
    357         anchor.x = 10;
    358         anchor.y = 20;
    359         gfx_text_fmt_init(&fmt);
    360         fmt.font = font;
    361         fmt.color = color;
    362 
    363         gfx_text_cont(&anchor, &fmt, "Abc", &cpos, &cfmt);
    364 
    365         PCUT_ASSERT_INT_EQUALS(13, cpos.x);
    366         PCUT_ASSERT_INT_EQUALS(20, cpos.y);
    367         PCUT_ASSERT_EQUALS(fmt.color, cfmt.color);
    368         PCUT_ASSERT_EQUALS(gfx_halign_left, cfmt.halign);
    369         PCUT_ASSERT_EQUALS(gfx_valign_baseline, cfmt.valign);
    370 
    371         gfx_font_close(font);
    372         gfx_typeface_destroy(tface);
    373         gfx_color_delete(color);
    374 
    375         rc = gfx_context_delete(gc);
    376         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    377 }
    378 
    379 /** gfx_text_rect() computes bounding rectangle */
    380 PCUT_TEST(text_rect)
    381 {
    382         gfx_typeface_t *tface;
    383         gfx_font_t *font;
    384         gfx_context_t *gc;
    385         gfx_color_t *color;
    386         test_gc_t tgc;
    387         gfx_text_fmt_t fmt;
    388         gfx_coord2_t anchor;
    389         gfx_rect_t rect;
    390         errno_t rc;
    391 
    392         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    393         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    394 
    395         rc = gfx_typeface_create(gc, &tface);
    396         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    397 
    398         rc = gfx_font_create_textmode(tface, &font);
    399         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    400 
    401         rc = gfx_color_new_rgb_i16(0, 0, 0, &color);
    402         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    403 
    404         anchor.x = 10;
    405         anchor.y = 20;
    406         gfx_text_fmt_init(&fmt);
    407         fmt.font = font;
    408         fmt.color = color;
    409 
    410         gfx_text_rect(&anchor, &fmt, "Abc", &rect);
    411 
    412         PCUT_ASSERT_INT_EQUALS(10, rect.p0.x);
    413         PCUT_ASSERT_INT_EQUALS(20, rect.p0.y);
    414         PCUT_ASSERT_INT_EQUALS(13, rect.p1.x);
    415         PCUT_ASSERT_INT_EQUALS(21, rect.p1.y);
    416 
    417         gfx_font_close(font);
    418         gfx_typeface_destroy(tface);
    419         gfx_color_delete(color);
    420 
    421         rc = gfx_context_delete(gc);
    422         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     151static errno_t testgc_set_clip_rect(void *arg, gfx_rect_t *rect)
     152{
     153        return EOK;
     154}
     155
     156static errno_t testgc_set_color(void *arg, gfx_color_t *color)
     157{
     158        return EOK;
     159}
     160
     161static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
     162{
     163        return EOK;
     164}
     165
     166static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     167    gfx_bitmap_alloc_t *alloc, void **rbm)
     168{
     169        test_gc_t *tgc = (test_gc_t *) arg;
     170        testgc_bitmap_t *tbm;
     171
     172        tbm = calloc(1, sizeof(testgc_bitmap_t));
     173        if (tbm == NULL)
     174                return ENOMEM;
     175
     176        if (alloc == NULL) {
     177                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     178                    sizeof(uint32_t);
     179                tbm->alloc.off0 = 0;
     180                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     181                    tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
     182                tbm->myalloc = true;
     183                if (tbm->alloc.pixels == NULL) {
     184                        free(tbm);
     185                        return ENOMEM;
     186                }
     187        } else {
     188                tbm->alloc = *alloc;
     189        }
     190
     191        tbm->tgc = tgc;
     192        tgc->bm_params = *params;
     193        tgc->bm_pixels = tbm->alloc.pixels;
     194        *rbm = (void *)tbm;
     195        return EOK;
     196}
     197
     198static errno_t testgc_bitmap_destroy(void *bm)
     199{
     200        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     201        if (tbm->myalloc)
     202                free(tbm->alloc.pixels);
     203        free(tbm);
     204        return EOK;
     205}
     206
     207static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     208    gfx_coord2_t *offs)
     209{
     210        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     211        tbm->tgc->bm_srect = *srect;
     212        tbm->tgc->bm_offs = *offs;
     213        return EOK;
     214}
     215
     216static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     217{
     218        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     219        *alloc = tbm->alloc;
     220        return EOK;
    423221}
    424222
Note: See TracChangeset for help on using the changeset viewer.