Changeset 313ac8e in mainline


Ignore:
Timestamp:
2020-09-17T15:28:03Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7bef2d8
Parents:
414020d9
Message:

Make negative quadrants actually work

Location:
uspace/lib/gfxfont
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfxfont/private/font.h

    r414020d9 r313ac8e  
    8484
    8585extern errno_t gfx_font_splice_at_glyph(gfx_font_t *, gfx_glyph_t *,
    86     gfx_coord_t, gfx_coord_t);
     86    gfx_rect_t *);
    8787
    8888#endif
  • uspace/lib/gfxfont/src/font.c

    r414020d9 r313ac8e  
    271271 *
    272272 * This is used to resize a glyph in the font bitmap. This changes
    273  * the bitmap widht and might also make the bitmap taller.
    274  * Width and height of the glyph is also adjusted accordingly.
     273 * the bitmap widht might also make the bitmap taller.
     274 * Dimensions of the glyph are also adjusted according to @a nrect.
    275275 *
    276276 * @param font Font
    277277 * @param glyph Glyph to replace
    278  * @param width Width of replacement space
    279  * @param height Height of replacement space
     278 * @param nrect Replacement rectangle
    280279 */
    281280errno_t gfx_font_splice_at_glyph(gfx_font_t *font, gfx_glyph_t *glyph,
    282     gfx_coord_t width, gfx_coord_t height)
     281    gfx_rect_t *nrect)
    283282{
    284283        gfx_glyph_t *g;
     
    286285        gfx_bitmap_params_t params;
    287286        gfx_coord_t dwidth;
     287        gfx_coord_t x0;
    288288        errno_t rc;
    289289
    290290        /* Change of width of glyph */
    291         dwidth = width - (glyph->rect.p1.x - glyph->rect.p0.x);
     291        dwidth = (nrect->p1.x - nrect->p0.x) -
     292            (glyph->rect.p1.x - glyph->rect.p0.x);
    292293
    293294        /* Create new font bitmap, wider by dwidth pixels */
     
    295296        params.rect = font->rect;
    296297        params.rect.p1.x += dwidth;
    297         if (height > params.rect.p1.y)
    298                 params.rect.p1.y = height;
     298        if (nrect->p1.y - nrect->p0.y > params.rect.p1.y)
     299                params.rect.p1.y = nrect->p1.y - nrect->p0.y;
    299300
    300301        rc = gfx_bitmap_create(font->typeface->gc, &params, NULL, &nbitmap);
    301302        if (rc != EOK)
    302303                goto error;
     304
     305        /*
     306         * In x0 we compute the left margin of @a glyph. We start with
     307         * zero and then, if there are any preceding glyphs, we set it
     308         * to the right margin of the last one.
     309         */
     310        x0 = 0;
    303311
    304312        /* Transfer glyphs before @a glyph */
     
    310318                if (rc != EOK)
    311319                        goto error;
     320
     321                /* Left margin of the next glyph */
     322                x0 = g->rect.p1.x;
    312323
    313324                g = gfx_font_next_glyph(g);
     
    331342        }
    332343
    333         /* Update glyph width and height */
    334         glyph->rect.p1.x = glyph->rect.p0.x + width;
    335         glyph->rect.p1.y = glyph->rect.p0.y + height;
     344        /* Place glyph rectangle inside the newly created space */
     345        glyph->origin.x = x0 - nrect->p0.x;
     346        glyph->origin.y = 0 - nrect->p0.y;
     347        gfx_rect_translate(&glyph->origin, nrect, &glyph->rect);
    336348
    337349        /* Update font bitmap */
  • uspace/lib/gfxfont/src/glyph_bmp.c

    r414020d9 r313ac8e  
    6666                return ENOMEM;
    6767
    68         bmp->rect.p0.x = 0;
    69         bmp->rect.p0.y = 0;
    70         bmp->rect.p1.x = glyph->rect.p1.x - glyph->rect.p0.x;
    71         bmp->rect.p1.y = glyph->rect.p1.y - glyph->rect.p0.y;
    72 
    73         bmp->pixels = calloc(bmp->rect.p1.x * bmp->rect.p1.y, sizeof(int));
     68        /* Bitmap coordinates are relative to glyph origin point */
     69        gfx_rect_rtranslate(&glyph->origin, &glyph->rect, &bmp->rect);
     70
     71        bmp->pixels = calloc((bmp->rect.p1.x - bmp->rect.p0.x) *
     72            (bmp->rect.p1.y - bmp->rect.p0.y), sizeof(int));
    7473        if (bmp->pixels == NULL) {
    7574                free(bmp);
     
    9291        /* Copy pixels from font bitmap */
    9392
    94         for (y = 0; y < bmp->rect.p1.y; y++) {
    95                 for (x = 0; x < bmp->rect.p1.x; x++) {
    96                         pixel = pixelmap_get_pixel(&pmap, glyph->rect.p0.x + x,
    97                             glyph->rect.p0.y + y);
    98                         bmp->pixels[y * bmp->rect.p1.x + x] =
    99                             (pixel != 0) ? 1 : 0;
     93        for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
     94                for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
     95                        pixel = pixelmap_get_pixel(&pmap, glyph->origin.x + x,
     96                            glyph->origin.y + y);
     97                        bmp->pixels[(y - bmp->rect.p0.y) *
     98                            (bmp->rect.p1.x - bmp->rect.p0.x) +
     99                            (x - bmp->rect.p0.x)] = (pixel != 0) ? 1 : 0;
    100100                }
    101101        }
     
    126126         * is adjusted.
    127127         */
    128         rc = gfx_font_splice_at_glyph(font, glyph,
    129             bmp->rect.p1.x - bmp->rect.p0.x, bmp->rect.p1.y - bmp->rect.p0.y);
     128        rc = gfx_font_splice_at_glyph(font, glyph, &bmp->rect);
    130129        if (rc != EOK)
    131130                return rc;
     
    143142        /* Copy pixels to font bitmap */
    144143
    145         for (y = 0; y < bmp->rect.p1.y; y++) {
    146                 for (x = 0; x < bmp->rect.p1.x; x++) {
    147                         pixel = bmp->pixels[y * bmp->rect.p1.x + x] ?
     144        for (y = bmp->rect.p0.y; y < bmp->rect.p1.y; y++) {
     145                for (x = bmp->rect.p0.x; x < bmp->rect.p1.x; x++) {
     146                        pixel = bmp->pixels[(y - bmp->rect.p0.y) *
     147                            (bmp->rect.p1.x - bmp->rect.p0.x) +
     148                            (x - bmp->rect.p0.x)] ?
    148149                            PIXEL(255, 255, 255, 255) : PIXEL(0, 0, 0, 0);
    149                         pixelmap_put_pixel(&pmap, glyph->rect.p0.x + x,
    150                             glyph->rect.p0.y + y, pixel);
     150                        pixelmap_put_pixel(&pmap, glyph->origin.x + x,
     151                            glyph->origin.y + y, pixel);
    151152                }
    152153        }
     
    193194
    194195        pitch = bmp->rect.p1.x - bmp->rect.p0.x;
     196
    195197        return bmp->pixels[(y - bmp->rect.p0.y) * pitch +
    196198            (x - bmp->rect.p0.x)];
     
    214216        pos.x = x;
    215217        pos.y = y;
     218
    216219        if (!gfx_pix_inside_rect(&pos, &bmp->rect)) {
    217220                rc = gfx_glyph_bmp_extend(bmp, &pos);
  • uspace/lib/gfxfont/test/font.c

    r414020d9 r313ac8e  
    332332        gfx_glyph_t *glyph;
    333333        gfx_context_t *gc;
     334        gfx_rect_t nrect;
    334335        test_gc_t tgc;
    335336        errno_t rc;
     
    350351        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    351352
    352         rc = gfx_font_splice_at_glyph(font, glyph, 10, 10);
     353        nrect.p0.x = -5;
     354        nrect.p0.y = -5;
     355        nrect.p1.x = 5;
     356        nrect.p1.y = 5;
     357        rc = gfx_font_splice_at_glyph(font, glyph, &nrect);
    353358        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    354359
  • uspace/lib/gfxfont/test/glyph_bmp.c

    r414020d9 r313ac8e  
    185185        PCUT_ASSERT_INT_EQUALS(0, pix);
    186186
    187         gfx_glyph_bmp_close(bmp);
     187        /* ... */
     188
     189        rc = gfx_glyph_bmp_setpix(bmp, 1, -1, 1);
     190        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     191
     192        rc = gfx_glyph_bmp_save(bmp);
     193        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     194
     195        gfx_glyph_bmp_close(bmp);
     196
     197        /* Once again */
     198
     199        rc = gfx_glyph_bmp_open(glyph, &bmp);
     200        PCUT_ASSERT_ERRNO_VAL(EOK, rc);
     201        PCUT_ASSERT_NOT_NULL(bmp);
     202
     203        pix = gfx_glyph_bmp_getpix(bmp, 0, 0);
     204        PCUT_ASSERT_INT_EQUALS(1, pix);
     205
     206        pix = gfx_glyph_bmp_getpix(bmp, 1, 1);
     207        PCUT_ASSERT_INT_EQUALS(1, pix);
     208
     209        pix = gfx_glyph_bmp_getpix(bmp, 1, 0);
     210        PCUT_ASSERT_INT_EQUALS(0, pix);
     211
     212        pix = gfx_glyph_bmp_getpix(bmp, 0, 1);
     213        PCUT_ASSERT_INT_EQUALS(0, pix);
     214
     215        pix = gfx_glyph_bmp_getpix(bmp, 1, -1);
     216        PCUT_ASSERT_INT_EQUALS(1, pix);
     217
     218        pix = gfx_glyph_bmp_getpix(bmp, 0, -1);
     219        PCUT_ASSERT_INT_EQUALS(0, pix);
     220
    188221        gfx_glyph_destroy(glyph);
    189222
Note: See TracChangeset for help on using the changeset viewer.