Changeset d2100e2 in mainline for uspace/lib/gfxfont/src
- Timestamp:
- 2020-08-09T18:40:28Z (5 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 06b8383
- Parents:
- 5592c56
- Location:
- uspace/lib/gfxfont/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/src/font.c
r5592c56 rd2100e2 37 37 #include <assert.h> 38 38 #include <errno.h> 39 #include <gfx/bitmap.h> 39 40 #include <gfx/font.h> 40 41 #include <gfx/glyph.h> … … 70 71 { 71 72 gfx_font_t *font; 73 gfx_bitmap_params_t params; 72 74 errno_t rc; 73 75 … … 81 83 if (rc != EOK) { 82 84 assert(rc == EINVAL); 85 free(font); 86 return rc; 87 } 88 89 /* Create font bitmap */ 90 gfx_bitmap_params_init(¶ms); 91 params.rect = font->rect; 92 93 rc = gfx_bitmap_create(font->gc, ¶ms, NULL, &font->bitmap); 94 if (rc != EOK) { 83 95 free(font); 84 96 return rc; … … 190 202 } 191 203 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 */ 215 errno_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(¶ms); 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, ¶ms, 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, ¶ms.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, ¶ms.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; 277 error: 278 if (nbitmap != NULL) 279 gfx_bitmap_destroy(nbitmap); 280 return rc; 281 } 282 192 283 /** @} 193 284 */ -
uspace/lib/gfxfont/src/glyph.c
r5592c56 rd2100e2 36 36 #include <assert.h> 37 37 #include <errno.h> 38 #include <gfx/bitmap.h> 38 39 #include <gfx/glyph.h> 40 #include <io/pixelmap.h> 39 41 #include <mem.h> 40 42 #include <stdlib.h> … … 252 254 } 253 255 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 */ 265 errno_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 254 302 /** @} 255 303 */ -
uspace/lib/gfxfont/src/glyph_bmp.c
r5592c56 rd2100e2 35 35 36 36 #include <errno.h> 37 #include <gfx/bitmap.h> 37 38 #include <gfx/coord.h> 39 #include <gfx/font.h> 38 40 #include <gfx/glyph_bmp.h> 41 #include <io/pixelmap.h> 39 42 #include <stdlib.h> 43 #include "../private/font.h" 44 #include "../private/glyph.h" 40 45 #include "../private/glyph_bmp.h" 41 46 … … 50 55 errno_t gfx_glyph_bmp_open(gfx_glyph_t *glyph, gfx_glyph_bmp_t **rbmp) 51 56 { 57 gfx_font_t *font = glyph->font; 52 58 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; 53 64 54 65 bmp = calloc(1, sizeof(gfx_glyph_bmp_t)); … … 58 69 bmp->rect.p0.x = 0; 59 70 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 } 63 103 64 104 bmp->glyph = glyph; … … 74 114 errno_t gfx_glyph_bmp_save(gfx_glyph_bmp_t *bmp) 75 115 { 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 76 155 return EOK; 77 156 }
Note:
See TracChangeset
for help on using the changeset viewer.