Changeset d2100e2 in mainline for uspace/lib/gfxfont/src


Ignore:
Timestamp:
2020-08-09T18:40:28Z (5 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.

Location:
uspace/lib/gfxfont/src
Files:
3 edited

Legend:

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

    r5592c56 rd2100e2  
    3737#include <assert.h>
    3838#include <errno.h>
     39#include <gfx/bitmap.h>
    3940#include <gfx/font.h>
    4041#include <gfx/glyph.h>
     
    7071{
    7172        gfx_font_t *font;
     73        gfx_bitmap_params_t params;
    7274        errno_t rc;
    7375
     
    8183        if (rc != EOK) {
    8284                assert(rc == EINVAL);
     85                free(font);
     86                return rc;
     87        }
     88
     89        /* Create font bitmap */
     90        gfx_bitmap_params_init(&params);
     91        params.rect = font->rect;
     92
     93        rc = gfx_bitmap_create(font->gc, &params, NULL, &font->bitmap);
     94        if (rc != EOK) {
    8395                free(font);
    8496                return rc;
     
    190202}
    191203
     204/** Replace glyph graphic with empty space of specified width.
     205 *
     206 * This is used to resize a glyph in the font bitmap. This changes
     207 * the bitmap widht and might also make the bitmap taller.
     208 * Width and height of the glyph is also adjusted accordingly.
     209 *
     210 * @param font Font
     211 * @param glyph Glyph to replace
     212 * @param width Width of replacement space
     213 * @param height Height of replacement space
     214 */
     215errno_t gfx_font_splice_at_glyph(gfx_font_t *font, gfx_glyph_t *glyph,
     216    gfx_coord_t width, gfx_coord_t height)
     217{
     218        gfx_glyph_t *g;
     219        gfx_bitmap_t *nbitmap;
     220        gfx_bitmap_params_t params;
     221        gfx_coord_t dwidth;
     222        errno_t rc;
     223
     224        /* Change of width of glyph */
     225        dwidth = width - (glyph->rect.p1.x - glyph->rect.p0.x);
     226
     227        /* Create new font bitmap, wider by dwidth pixels */
     228        gfx_bitmap_params_init(&params);
     229        params.rect = font->rect;
     230        params.rect.p1.x += dwidth;
     231        if (height > params.rect.p1.y)
     232                params.rect.p1.y = height;
     233
     234        rc = gfx_bitmap_create(font->gc, &params, NULL, &nbitmap);
     235        if (rc != EOK)
     236                goto error;
     237
     238        /* Transfer glyphs before @a glyph */
     239        g = gfx_font_first_glyph(font);
     240        while (g != glyph) {
     241                assert(g != NULL);
     242
     243                rc = gfx_glyph_transfer(g, 0, nbitmap, &params.rect);
     244                if (rc != EOK)
     245                        goto error;
     246
     247                g = gfx_font_next_glyph(g);
     248        }
     249
     250        /* Skip @a glyph */
     251        g = gfx_font_next_glyph(g);
     252
     253        /* Transfer glyphs after glyph */
     254        while (g != NULL) {
     255                rc = gfx_glyph_transfer(g, dwidth, nbitmap, &params.rect);
     256                if (rc != EOK)
     257                        goto error;
     258
     259                /* Update glyph coordinates */
     260                g->rect.p0.x += dwidth;
     261                g->rect.p1.x += dwidth;
     262                g->origin.x += dwidth;
     263
     264                g = gfx_font_next_glyph(g);
     265        }
     266
     267        /* Update glyph width and height */
     268        glyph->rect.p1.x = glyph->rect.p0.x + width;
     269        glyph->rect.p1.y = glyph->rect.p0.y + height;
     270
     271        /* Update font bitmap */
     272        gfx_bitmap_destroy(font->bitmap);
     273        font->bitmap = nbitmap;
     274        font->rect = params.rect;
     275
     276        return EOK;
     277error:
     278        if (nbitmap != NULL)
     279                gfx_bitmap_destroy(nbitmap);
     280        return rc;
     281}
     282
    192283/** @}
    193284 */
  • uspace/lib/gfxfont/src/glyph.c

    r5592c56 rd2100e2  
    3636#include <assert.h>
    3737#include <errno.h>
     38#include <gfx/bitmap.h>
    3839#include <gfx/glyph.h>
     40#include <io/pixelmap.h>
    3941#include <mem.h>
    4042#include <stdlib.h>
     
    252254}
    253255
     256/** Transfer glyph to new font bitmap.
     257 *
     258 * @param glyph Glyph
     259 * @param offs Offset in new font bitmap
     260 * @param dest New font bitmap
     261 * @param drect Bounding rectangle for @a dest
     262 *
     263 * @return EOK on success or an error code
     264 */
     265errno_t gfx_glyph_transfer(gfx_glyph_t *glyph, gfx_coord_t offs,
     266    gfx_bitmap_t *dest, gfx_rect_t *drect)
     267{
     268        pixelmap_t smap;
     269        pixelmap_t dmap;
     270        gfx_bitmap_alloc_t salloc;
     271        gfx_bitmap_alloc_t dalloc;
     272        gfx_coord_t x, y;
     273        pixel_t pixel;
     274        errno_t rc;
     275
     276        rc = gfx_bitmap_get_alloc(glyph->font->bitmap, &salloc);
     277        if (rc != EOK)
     278                return rc;
     279
     280        rc = gfx_bitmap_get_alloc(dest, &dalloc);
     281        if (rc != EOK)
     282                return rc;
     283
     284        smap.width = glyph->font->rect.p1.x;
     285        smap.height = glyph->font->rect.p1.y;
     286        smap.data = salloc.pixels;
     287
     288        dmap.width = drect->p1.x;
     289        dmap.height = drect->p1.y;
     290        dmap.data = dalloc.pixels;
     291
     292        for (y = drect->p0.y; y < drect->p1.y; y++) {
     293                for (x = drect->p0.x; x < drect->p1.x; x++) {
     294                        pixel = pixelmap_get_pixel(&smap, x, y);
     295                        pixelmap_put_pixel(&dmap, x + offs, y, pixel);
     296                }
     297        }
     298
     299        return EOK;
     300}
     301
    254302/** @}
    255303 */
  • uspace/lib/gfxfont/src/glyph_bmp.c

    r5592c56 rd2100e2  
    3535
    3636#include <errno.h>
     37#include <gfx/bitmap.h>
    3738#include <gfx/coord.h>
     39#include <gfx/font.h>
    3840#include <gfx/glyph_bmp.h>
     41#include <io/pixelmap.h>
    3942#include <stdlib.h>
     43#include "../private/font.h"
     44#include "../private/glyph.h"
    4045#include "../private/glyph_bmp.h"
    4146
     
    5055errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp)
    5156{
     57        gfx_font_t *font = glyph->font;
    5258        gfx_glyph_bmp_t *bmp;
     59        pixelmap_t pmap;
     60        gfx_bitmap_alloc_t alloc;
     61        gfx_coord_t x, y;
     62        pixel_t pixel;
     63        errno_t rc;
    5364
    5465        bmp = calloc(1, sizeof(gfx_glyph_bmp_t));
     
    5869        bmp->rect.p0.x = 0;
    5970        bmp->rect.p0.y = 0;
    60         bmp->rect.p1.x = 0;
    61         bmp->rect.p1.y = 0;
    62         bmp->pixels = NULL;
     71        bmp->rect.p1.x = glyph->rect.p1.x - glyph->rect.p0.x;
     72        bmp->rect.p1.y = glyph->rect.p1.y - glyph->rect.p0.y;
     73
     74        bmp->pixels = calloc(bmp->rect.p1.x * bmp->rect.p1.y, sizeof(int));
     75        if (bmp->pixels == NULL) {
     76                free(bmp);
     77                return ENOMEM;
     78        }
     79
     80        rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
     81        if (rc != EOK) {
     82                free(bmp->pixels);
     83                free(bmp);
     84                return rc;
     85        }
     86
     87        assert(font->rect.p0.x == 0);
     88        assert(font->rect.p0.y == 0);
     89        pmap.width = font->rect.p1.x;
     90        pmap.height = font->rect.p1.y;
     91        pmap.data = alloc.pixels;
     92
     93        /* Copy pixels from font bitmap */
     94
     95        for (y = 0; y < bmp->rect.p1.y; y++) {
     96                for (x = 0; x < bmp->rect.p1.x; x++) {
     97                        pixel = pixelmap_get_pixel(&pmap, glyph->rect.p0.x + x,
     98                            glyph->rect.p0.y + y);
     99                        bmp->pixels[y * bmp->rect.p1.x + x] =
     100                            (pixel != 0) ? 1 : 0;
     101                }
     102        }
    63103
    64104        bmp->glyph = glyph;
     
    74114errno_t gfx_glyph_bmp_save(gfx_glyph_bmp_t *bmp)
    75115{
     116        gfx_glyph_t *glyph = bmp->glyph;
     117        gfx_font_t *font = glyph->font;
     118        gfx_coord_t x, y;
     119        pixel_t pixel;
     120        pixelmap_t pmap;
     121        gfx_bitmap_alloc_t alloc;
     122        errno_t rc;
     123
     124        /*
     125         * Replace glyph with empty space in the font bitmap, the width
     126         * of the empty equal to new glyph bitmap width. The glyph width
     127         * is adjusted.
     128         */
     129        rc = gfx_font_splice_at_glyph(font, glyph,
     130            bmp->rect.p1.x - bmp->rect.p0.x, bmp->rect.p1.y - bmp->rect.p0.y);
     131        if (rc != EOK)
     132                return rc;
     133
     134        rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
     135        if (rc != EOK)
     136                return rc;
     137
     138        assert(font->rect.p0.x == 0);
     139        assert(font->rect.p0.y == 0);
     140        pmap.width = font->rect.p1.x;
     141        pmap.height = font->rect.p1.y;
     142        pmap.data = alloc.pixels;
     143
     144        /* Copy pixels to font bitmap */
     145
     146        for (y = 0; y < bmp->rect.p1.y; y++) {
     147                for (x = 0; x < bmp->rect.p1.x; x++) {
     148                        pixel = bmp->pixels[y * bmp->rect.p1.x + x] ?
     149                            PIXEL(255, 255, 255, 255) : PIXEL(0, 0, 0, 0);
     150                        pixelmap_put_pixel(&pmap, glyph->rect.p0.x + x,
     151                            glyph->rect.p0.y + y, pixel);
     152                }
     153        }
     154
    76155        return EOK;
    77156}
Note: See TracChangeset for help on using the changeset viewer.