Changeset d2100e2 in mainline for uspace/lib/gfxfont/test/glyph.c


Ignore:
Timestamp:
2020-08-09T18:40:28Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
06b8383
Parents:
5592c56
Message:

Finish glyph bitmap operations and tests

Setting/getting pixel, opening/saving glyph bitmap.

File:
1 edited

Legend:

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

    r5592c56 rd2100e2  
    2727 */
    2828
     29#include <gfx/bitmap.h>
    2930#include <gfx/context.h>
    3031#include <gfx/font.h>
    3132#include <gfx/glyph.h>
     33#include <gfx/glyph_bmp.h>
     34#include <io/pixelmap.h>
    3235#include <pcut/pcut.h>
    3336#include <stdbool.h>
    3437#include <str.h>
     38#include "../private/glyph.h"
    3539
    3640PCUT_INIT;
     
    4044static errno_t testgc_set_color(void *, gfx_color_t *);
    4145static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     46static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     47    gfx_bitmap_alloc_t *, void **);
     48static errno_t testgc_bitmap_destroy(void *);
     49static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     50static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
    4251
    4352static gfx_context_ops_t test_ops = {
    4453        .set_color = testgc_set_color,
    45         .fill_rect = testgc_fill_rect
     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
    4659};
     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;
    4773
    4874/** Test creating and destroying glyph */
     
    5480        gfx_glyph_t *glyph;
    5581        gfx_context_t *gc;
    56         errno_t rc;
    57 
    58         rc = gfx_context_new(&test_ops, NULL, &gc);
     82        test_gc_t tgc;
     83        errno_t rc;
     84
     85        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    5986        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    6087
     
    83110        gfx_glyph_metrics_t rmetrics;
    84111        gfx_context_t *gc;
    85         errno_t rc;
    86 
    87         rc = gfx_context_new(&test_ops, NULL, &gc);
     112        test_gc_t tgc;
     113        errno_t rc;
     114
     115        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    88116        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    89117
     
    118146        gfx_glyph_metrics_t rmetrics;
    119147        gfx_context_t *gc;
    120         errno_t rc;
    121 
    122         rc = gfx_context_new(&test_ops, NULL, &gc);
     148        test_gc_t tgc;
     149        errno_t rc;
     150
     151        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    123152        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    124153
     
    156185        gfx_glyph_t *glyph;
    157186        gfx_context_t *gc;
    158         errno_t rc;
    159 
    160         rc = gfx_context_new(&test_ops, NULL, &gc);
     187        test_gc_t tgc;
     188        errno_t rc;
     189
     190        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    161191        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    162192
     
    198228        gfx_glyph_t *glyph;
    199229        gfx_context_t *gc;
    200         errno_t rc;
    201 
    202         rc = gfx_context_new(&test_ops, NULL, &gc);
     230        test_gc_t tgc;
     231        errno_t rc;
     232
     233        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    203234        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    204235
     
    243274        gfx_glyph_t *glyph;
    244275        gfx_context_t *gc;
     276        test_gc_t tgc;
    245277        bool match;
    246278        size_t msize;
    247279        errno_t rc;
    248280
    249         rc = gfx_context_new(&test_ops, NULL, &gc);
     281        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    250282        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    251283
     
    296328        gfx_glyph_t *glyph;
    297329        gfx_context_t *gc;
     330        test_gc_t tgc;
    298331        gfx_glyph_pattern_t *pat;
    299332        errno_t rc;
    300333
    301         rc = gfx_context_new(&test_ops, NULL, &gc);
     334        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    302335        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    303336
     
    340373        gfx_glyph_t *glyph;
    341374        gfx_context_t *gc;
     375        test_gc_t tgc;
    342376        gfx_glyph_pattern_t *pat;
    343377        const char *pstr;
    344378        errno_t rc;
    345379
    346         rc = gfx_context_new(&test_ops, NULL, &gc);
     380        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
    347381        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    348382
     
    377411}
    378412
     413/** Test gfx_glyph_transfer() */
     414PCUT_TEST(transfer)
     415{
     416        gfx_font_metrics_t fmetrics;
     417        gfx_font_t *font;
     418        gfx_glyph_metrics_t gmetrics;
     419        gfx_glyph_t *glyph;
     420        gfx_context_t *gc;
     421        gfx_bitmap_t *bitmap;
     422        gfx_bitmap_params_t params;
     423        gfx_bitmap_alloc_t alloc;
     424        gfx_glyph_bmp_t *bmp;
     425        pixelmap_t pmap;
     426        pixel_t pixel;
     427        test_gc_t tgc;
     428        errno_t rc;
     429
     430        rc = gfx_context_new(&test_ops, (void *) &tgc, &gc);
     431        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     432
     433        gfx_font_metrics_init(&fmetrics);
     434        rc = gfx_font_create(gc, &fmetrics, &font);
     435        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     436
     437        gfx_glyph_metrics_init(&gmetrics);
     438        gmetrics.advance = 1;
     439
     440        rc = gfx_glyph_create(font, &gmetrics, &glyph);
     441        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     442
     443        /*
     444         * We need to fill some pixels of the glyph.
     445         * We'll use the glyph bitmap for that.
     446         * That means this test won't pass unless glyph
     447         * bitmap works.
     448         */
     449
     450        rc = gfx_glyph_bmp_open(glyph, &bmp);
     451        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     452        PCUT_ASSERT_NOT_NULL(bmp);
     453
     454        rc = gfx_glyph_bmp_setpix(bmp, 0, 0, 1);
     455        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     456
     457        rc = gfx_glyph_bmp_setpix(bmp, 1, 1, 1);
     458        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     459
     460        rc = gfx_glyph_bmp_save(bmp);
     461        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     462        gfx_glyph_bmp_close(bmp);
     463
     464        /* Now create a new bitmap */
     465
     466        gfx_bitmap_params_init(&params);
     467        params.rect.p0.x = 0;
     468        params.rect.p0.y = 0;
     469        params.rect.p1.x = 10;
     470        params.rect.p1.y = 10;
     471        rc = gfx_bitmap_create(gc, &params, NULL, &bitmap);
     472        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     473
     474        rc = gfx_bitmap_get_alloc(bitmap, &alloc);
     475        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     476
     477        /* Transfer the glyph to new bitmap */
     478        rc = gfx_glyph_transfer(glyph, 0, bitmap, &params.rect);
     479        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     480
     481        /* Now let's read pixels from the new bitmap */
     482        pmap.width = params.rect.p1.x;
     483        pmap.height = params.rect.p1.y;
     484        pmap.data = alloc.pixels;
     485
     486        pixel = pixelmap_get_pixel(&pmap, 0, 0);
     487        PCUT_ASSERT_INT_EQUALS(PIXEL(255, 255, 255, 255), pixel);
     488
     489        pixel = pixelmap_get_pixel(&pmap, 1, 1);
     490        PCUT_ASSERT_INT_EQUALS(PIXEL(255, 255, 255, 255), pixel);
     491
     492        pixel = pixelmap_get_pixel(&pmap, 1, 0);
     493        PCUT_ASSERT_INT_EQUALS(PIXEL(0, 0, 0, 0), pixel);
     494
     495        pixel = pixelmap_get_pixel(&pmap, 0, 1);
     496        PCUT_ASSERT_INT_EQUALS(PIXEL(0, 0, 0, 0), pixel);
     497
     498        gfx_glyph_destroy(glyph);
     499
     500        gfx_font_destroy(font);
     501        rc = gfx_context_delete(gc);
     502        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     503}
     504
    379505static errno_t testgc_set_color(void *arg, gfx_color_t *color)
    380506{
     
    387513}
    388514
     515static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     516    gfx_bitmap_alloc_t *alloc, void **rbm)
     517{
     518        test_gc_t *tgc = (test_gc_t *) arg;
     519        testgc_bitmap_t *tbm;
     520
     521        tbm = calloc(1, sizeof(testgc_bitmap_t));
     522        if (tbm == NULL)
     523                return ENOMEM;
     524
     525        if (alloc == NULL) {
     526                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     527                    sizeof(uint32_t);
     528                tbm->alloc.off0 = 0;
     529                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     530                    tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
     531                tbm->myalloc = true;
     532                if (tbm->alloc.pixels == NULL) {
     533                        free(tbm);
     534                        return ENOMEM;
     535                }
     536        } else {
     537                tbm->alloc = *alloc;
     538        }
     539
     540        tbm->tgc = tgc;
     541        tgc->bm_params = *params;
     542        tgc->bm_pixels = tbm->alloc.pixels;
     543        *rbm = (void *)tbm;
     544        return EOK;
     545}
     546
     547static errno_t testgc_bitmap_destroy(void *bm)
     548{
     549        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     550        if (tbm->myalloc)
     551                free(tbm->alloc.pixels);
     552        free(tbm);
     553        return EOK;
     554}
     555
     556static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     557    gfx_coord2_t *offs)
     558{
     559        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     560        tbm->tgc->bm_srect = *srect;
     561        tbm->tgc->bm_offs = *offs;
     562        return EOK;
     563}
     564
     565static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     566{
     567        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     568        *alloc = tbm->alloc;
     569        return EOK;
     570}
     571
    389572PCUT_EXPORT(glyph);
Note: See TracChangeset for help on using the changeset viewer.