Ignore:
File:
1 edited

Legend:

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

    r1fa6292 rd884672  
    11/*
    2  * Copyright (c) 2021 Jiri Svoboda
     2 * Copyright (c) 2020 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3434#include "../private/font.h"
    3535#include "../private/typeface.h"
    36 #include "../private/testgc.h"
    3736
    3837PCUT_INIT;
    3938
    4039PCUT_TEST_SUITE(font);
     40
     41static errno_t testgc_set_color(void *, gfx_color_t *);
     42static errno_t testgc_fill_rect(void *, gfx_rect_t *);
     43static errno_t testgc_bitmap_create(void *, gfx_bitmap_params_t *,
     44    gfx_bitmap_alloc_t *, void **);
     45static errno_t testgc_bitmap_destroy(void *);
     46static errno_t testgc_bitmap_render(void *, gfx_rect_t *, gfx_coord2_t *);
     47static errno_t testgc_bitmap_get_alloc(void *, gfx_bitmap_alloc_t *);
     48
     49static gfx_context_ops_t test_ops = {
     50        .set_color = testgc_set_color,
     51        .fill_rect = testgc_fill_rect,
     52        .bitmap_create = testgc_bitmap_create,
     53        .bitmap_destroy = testgc_bitmap_destroy,
     54        .bitmap_render = testgc_bitmap_render,
     55        .bitmap_get_alloc = testgc_bitmap_get_alloc
     56};
     57
     58typedef struct {
     59        gfx_bitmap_params_t bm_params;
     60        void *bm_pixels;
     61        gfx_rect_t bm_srect;
     62        gfx_coord2_t bm_offs;
     63} test_gc_t;
     64
     65typedef struct {
     66        test_gc_t *tgc;
     67        gfx_bitmap_alloc_t alloc;
     68        bool myalloc;
     69} testgc_bitmap_t;
    4170
    4271/** Test creating and destroying font */
     
    6089        gfx_font_metrics_init(&metrics);
    6190        rc = gfx_font_create(tface, &props, &metrics, &font);
    62         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    63 
    64         gfx_font_close(font);
    65         gfx_typeface_destroy(tface);
    66 
    67         rc = gfx_context_delete(gc);
    68         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    69 }
    70 
    71 /** Test creating and destroying text-mode font */
    72 PCUT_TEST(create_textmode_destroy)
    73 {
    74         gfx_typeface_t *tface;
    75         gfx_font_t *font;
    76         gfx_context_t *gc;
    77         test_gc_t tgc;
    78         errno_t rc;
    79 
    80         rc = gfx_context_new(&test_ops, (void *)&tgc, &gc);
    81         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    82 
    83         rc = gfx_typeface_create(gc, &tface);
    84         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    85 
    86         rc = gfx_font_create_textmode(tface, &font);
    8791        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    8892
     
    478482        height = 10;
    479483
    480         pixels = calloc(width * height, sizeof(uint32_t));
     484        pixels = calloc(sizeof(uint32_t), width * height);
    481485        PCUT_ASSERT_NOT_NULL(pixels);
    482486
     
    525529        }
    526530
    527         pixels = calloc(width * height, sizeof(uint32_t));
     531        pixels = calloc(sizeof(uint32_t), width * height);
    528532        PCUT_ASSERT_NOT_NULL(pixels);
    529533
     
    541545}
    542546
     547static errno_t testgc_set_color(void *arg, gfx_color_t *color)
     548{
     549        return EOK;
     550}
     551
     552static errno_t testgc_fill_rect(void *arg, gfx_rect_t *rect)
     553{
     554        return EOK;
     555}
     556
     557static errno_t testgc_bitmap_create(void *arg, gfx_bitmap_params_t *params,
     558    gfx_bitmap_alloc_t *alloc, void **rbm)
     559{
     560        test_gc_t *tgc = (test_gc_t *) arg;
     561        testgc_bitmap_t *tbm;
     562
     563        tbm = calloc(1, sizeof(testgc_bitmap_t));
     564        if (tbm == NULL)
     565                return ENOMEM;
     566
     567        if (alloc == NULL) {
     568                tbm->alloc.pitch = (params->rect.p1.x - params->rect.p0.x) *
     569                    sizeof(uint32_t);
     570                tbm->alloc.off0 = 0;
     571                tbm->alloc.pixels = calloc(sizeof(uint32_t),
     572                    tbm->alloc.pitch * (params->rect.p1.y - params->rect.p0.y));
     573                tbm->myalloc = true;
     574                if (tbm->alloc.pixels == NULL) {
     575                        free(tbm);
     576                        return ENOMEM;
     577                }
     578        } else {
     579                tbm->alloc = *alloc;
     580        }
     581
     582        tbm->tgc = tgc;
     583        tgc->bm_params = *params;
     584        tgc->bm_pixels = tbm->alloc.pixels;
     585        *rbm = (void *)tbm;
     586        return EOK;
     587}
     588
     589static errno_t testgc_bitmap_destroy(void *bm)
     590{
     591        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     592        if (tbm->myalloc)
     593                free(tbm->alloc.pixels);
     594        free(tbm);
     595        return EOK;
     596}
     597
     598static errno_t testgc_bitmap_render(void *bm, gfx_rect_t *srect,
     599    gfx_coord2_t *offs)
     600{
     601        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     602        tbm->tgc->bm_srect = *srect;
     603        tbm->tgc->bm_offs = *offs;
     604        return EOK;
     605}
     606
     607static errno_t testgc_bitmap_get_alloc(void *bm, gfx_bitmap_alloc_t *alloc)
     608{
     609        testgc_bitmap_t *tbm = (testgc_bitmap_t *)bm;
     610        *alloc = tbm->alloc;
     611        return EOK;
     612}
     613
    543614PCUT_EXPORT(font);
Note: See TracChangeset for help on using the changeset viewer.