[ee2f0beb] | 1 | /*
|
---|
[5c27e77] | 2 | * Copyright (c) 2022 Jiri Svoboda
|
---|
[ee2f0beb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup libgfxfont
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Font
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[c78a03d] | 36 | #include <adt/list.h>
|
---|
[ee2f0beb] | 37 | #include <assert.h>
|
---|
[453f203b] | 38 | #include <byteorder.h>
|
---|
[ee2f0beb] | 39 | #include <errno.h>
|
---|
[d2100e2] | 40 | #include <gfx/bitmap.h>
|
---|
[ee2f0beb] | 41 | #include <gfx/font.h>
|
---|
[c78a03d] | 42 | #include <gfx/glyph.h>
|
---|
[ee2f0beb] | 43 | #include <mem.h>
|
---|
| 44 | #include <stdlib.h>
|
---|
| 45 | #include "../private/font.h"
|
---|
[c78a03d] | 46 | #include "../private/glyph.h"
|
---|
[d53af3c8] | 47 | #include "../private/tpf_file.h"
|
---|
[06b8383] | 48 | #include "../private/typeface.h"
|
---|
[ee2f0beb] | 49 |
|
---|
| 50 | /** Initialize font metrics structure.
|
---|
| 51 | *
|
---|
| 52 | * Font metrics structure must always be initialized using this function
|
---|
| 53 | * first.
|
---|
| 54 | *
|
---|
| 55 | * @param metrics Font metrics structure
|
---|
| 56 | */
|
---|
| 57 | void gfx_font_metrics_init(gfx_font_metrics_t *metrics)
|
---|
| 58 | {
|
---|
| 59 | memset(metrics, 0, sizeof(gfx_font_metrics_t));
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[06b8383] | 62 | /** Initialize font properties structure.
|
---|
[ee2f0beb] | 63 | *
|
---|
[06b8383] | 64 | * Font properties structure must always be initialized using this function
|
---|
| 65 | * first.
|
---|
| 66 | *
|
---|
| 67 | * @param props Font properties structure
|
---|
| 68 | */
|
---|
| 69 | void gfx_font_props_init(gfx_font_props_t *props)
|
---|
| 70 | {
|
---|
| 71 | memset(props, 0, sizeof(gfx_font_props_t));
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /** Get font properties.
|
---|
| 75 | *
|
---|
| 76 | * @param finfo Font info
|
---|
| 77 | * @param props Place to store font properties
|
---|
| 78 | */
|
---|
| 79 | void gfx_font_get_props(gfx_font_info_t *finfo, gfx_font_props_t *props)
|
---|
| 80 | {
|
---|
| 81 | *props = finfo->props;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
[ea459d4] | 84 | /** Create font with existing font info structure.
|
---|
[06b8383] | 85 | *
|
---|
| 86 | * @param tface Typeface
|
---|
[ea459d4] | 87 | * @param finfo Font info
|
---|
[ee2f0beb] | 88 | * @param metrics Font metrics
|
---|
| 89 | * @param rfont Place to store pointer to new font
|
---|
| 90 | *
|
---|
| 91 | * @return EOK on success, EINVAL if parameters are invald,
|
---|
| 92 | * ENOMEM if insufficient resources, EIO if graphic device connection
|
---|
| 93 | * was lost
|
---|
| 94 | */
|
---|
[ea459d4] | 95 | static errno_t gfx_font_create_with_info(gfx_typeface_t *tface,
|
---|
| 96 | gfx_font_info_t *finfo, gfx_font_metrics_t *metrics, gfx_font_t **rfont)
|
---|
[ee2f0beb] | 97 | {
|
---|
[06b8383] | 98 | gfx_font_t *font = NULL;
|
---|
[d2100e2] | 99 | gfx_bitmap_params_t params;
|
---|
[ee2f0beb] | 100 | errno_t rc;
|
---|
| 101 |
|
---|
| 102 | font = calloc(1, sizeof(gfx_font_t));
|
---|
[06b8383] | 103 | if (font == NULL) {
|
---|
| 104 | rc = ENOMEM;
|
---|
| 105 | goto error;
|
---|
| 106 | }
|
---|
| 107 | font->typeface = tface;
|
---|
[ea459d4] | 108 | font->finfo = finfo;
|
---|
[ee2f0beb] | 109 |
|
---|
[20d0098] | 110 | font->rect.p0.x = 0;
|
---|
| 111 | font->rect.p0.y = 0;
|
---|
| 112 | font->rect.p1.x = 1;
|
---|
| 113 | font->rect.p1.y = 1;
|
---|
| 114 |
|
---|
[ee2f0beb] | 115 | rc = gfx_font_set_metrics(font, metrics);
|
---|
| 116 | if (rc != EOK) {
|
---|
| 117 | assert(rc == EINVAL);
|
---|
[06b8383] | 118 | goto error;
|
---|
[ee2f0beb] | 119 | }
|
---|
| 120 |
|
---|
[d2100e2] | 121 | /* Create font bitmap */
|
---|
| 122 | gfx_bitmap_params_init(¶ms);
|
---|
| 123 | params.rect = font->rect;
|
---|
[0d62c10] | 124 | params.flags = bmpf_color_key | bmpf_colorize;
|
---|
[74f59b7] | 125 | params.key_color = PIXEL(0, 0, 0, 0);
|
---|
[d2100e2] | 126 |
|
---|
[06b8383] | 127 | rc = gfx_bitmap_create(tface->gc, ¶ms, NULL, &font->bitmap);
|
---|
| 128 | if (rc != EOK)
|
---|
| 129 | goto error;
|
---|
[d2100e2] | 130 |
|
---|
[ee2f0beb] | 131 | font->metrics = *metrics;
|
---|
[c78a03d] | 132 | list_initialize(&font->glyphs);
|
---|
[ea459d4] | 133 | *rfont = font;
|
---|
| 134 | return EOK;
|
---|
| 135 | error:
|
---|
| 136 | if (font != NULL)
|
---|
| 137 | free(font);
|
---|
| 138 | return rc;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** Create font.
|
---|
| 142 | *
|
---|
| 143 | * @param tface Typeface
|
---|
| 144 | * @param props Font properties
|
---|
| 145 | * @param metrics Font metrics
|
---|
| 146 | * @param rfont Place to store pointer to new font
|
---|
| 147 | *
|
---|
| 148 | * @return EOK on success, EINVAL if parameters are invald,
|
---|
| 149 | * ENOMEM if insufficient resources, EIO if graphic device connection
|
---|
| 150 | * was lost
|
---|
| 151 | */
|
---|
| 152 | errno_t gfx_font_create(gfx_typeface_t *tface, gfx_font_props_t *props,
|
---|
| 153 | gfx_font_metrics_t *metrics, gfx_font_t **rfont)
|
---|
| 154 | {
|
---|
| 155 | gfx_font_info_t *finfo = NULL;
|
---|
| 156 | gfx_font_t *font = NULL;
|
---|
| 157 | errno_t rc;
|
---|
| 158 |
|
---|
| 159 | finfo = calloc(1, sizeof(gfx_font_info_t));
|
---|
| 160 | if (finfo == NULL) {
|
---|
| 161 | rc = ENOMEM;
|
---|
| 162 | goto error;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | finfo->typeface = tface;
|
---|
| 166 | finfo->props = *props;
|
---|
| 167 |
|
---|
| 168 | rc = gfx_font_create_with_info(tface, finfo, metrics, &font);
|
---|
| 169 | if (rc != EOK)
|
---|
| 170 | goto error;
|
---|
| 171 |
|
---|
| 172 | finfo->font = font;
|
---|
[06b8383] | 173 | list_append(&finfo->lfonts, &tface->fonts);
|
---|
[ee2f0beb] | 174 | *rfont = font;
|
---|
| 175 | return EOK;
|
---|
[06b8383] | 176 | error:
|
---|
| 177 | if (finfo != NULL)
|
---|
| 178 | free(finfo);
|
---|
| 179 | return rc;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[6a87f28] | 182 | /** Create dummy font for printing text in text mode.
|
---|
| 183 | *
|
---|
| 184 | * @param tface Typeface
|
---|
| 185 | * @param rfont Place to store pointer to new font
|
---|
| 186 | *
|
---|
| 187 | * @return EOK on success, EINVAL if parameters are invald,
|
---|
| 188 | * ENOMEM if insufficient resources, EIO if graphic device connection
|
---|
| 189 | * was lost
|
---|
| 190 | */
|
---|
| 191 | errno_t gfx_font_create_textmode(gfx_typeface_t *tface, gfx_font_t **rfont)
|
---|
| 192 | {
|
---|
| 193 | gfx_font_props_t props;
|
---|
| 194 | gfx_font_metrics_t metrics;
|
---|
| 195 |
|
---|
| 196 | gfx_font_props_init(&props);
|
---|
| 197 | props.size = 1;
|
---|
| 198 | props.flags = gff_text_mode;
|
---|
| 199 |
|
---|
| 200 | gfx_font_metrics_init(&metrics);
|
---|
| 201 | metrics.ascent = 0;
|
---|
| 202 | metrics.descent = 0;
|
---|
| 203 | metrics.leading = 1;
|
---|
| 204 |
|
---|
| 205 | return gfx_font_create(tface, &props, &metrics, rfont);
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[06b8383] | 208 | /** Open font.
|
---|
| 209 | *
|
---|
| 210 | * @param finfo Font info
|
---|
| 211 | * @param rfont Place to store pointer to open font
|
---|
| 212 | * @return EOK on success or an error code
|
---|
| 213 | */
|
---|
| 214 | errno_t gfx_font_open(gfx_font_info_t *finfo, gfx_font_t **rfont)
|
---|
| 215 | {
|
---|
[ea459d4] | 216 | errno_t rc;
|
---|
| 217 |
|
---|
[06b8383] | 218 | if (finfo->font == NULL) {
|
---|
[ea459d4] | 219 | /* Load absent font from TPF file */
|
---|
| 220 | rc = gfx_font_load(finfo);
|
---|
| 221 | if (rc != EOK)
|
---|
| 222 | return rc;
|
---|
| 223 |
|
---|
| 224 | assert(finfo->font != NULL);
|
---|
| 225 | finfo->font->finfo = finfo;
|
---|
[06b8383] | 226 | }
|
---|
| 227 |
|
---|
| 228 | *rfont = finfo->font;
|
---|
| 229 | return EOK;
|
---|
[ee2f0beb] | 230 | }
|
---|
| 231 |
|
---|
[06b8383] | 232 | /** Close font.
|
---|
[ee2f0beb] | 233 | *
|
---|
| 234 | * @param font Font
|
---|
| 235 | */
|
---|
[06b8383] | 236 | void gfx_font_close(gfx_font_t *font)
|
---|
[ee2f0beb] | 237 | {
|
---|
[c78a03d] | 238 | gfx_glyph_t *glyph;
|
---|
| 239 |
|
---|
| 240 | glyph = gfx_font_first_glyph(font);
|
---|
| 241 | while (glyph != NULL) {
|
---|
| 242 | gfx_glyph_destroy(glyph);
|
---|
| 243 | glyph = gfx_font_first_glyph(font);
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[ea459d4] | 246 | font->finfo->font = NULL;
|
---|
[ee2f0beb] | 247 | free(font);
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | /** Get font metrics.
|
---|
| 251 | *
|
---|
| 252 | * @param font Font
|
---|
| 253 | * @param metrics Place to store metrics
|
---|
| 254 | */
|
---|
| 255 | void gfx_font_get_metrics(gfx_font_t *font, gfx_font_metrics_t *metrics)
|
---|
| 256 | {
|
---|
[fe40b67] | 257 | *metrics = font->metrics;
|
---|
[ee2f0beb] | 258 | }
|
---|
| 259 |
|
---|
| 260 | /** Set font metrics.
|
---|
| 261 | *
|
---|
| 262 | * @param font Font
|
---|
| 263 | * @param metrics Place to store metrics
|
---|
| 264 | * @return EOK on success, EINVAL if supplied metrics are invalid
|
---|
| 265 | */
|
---|
| 266 | errno_t gfx_font_set_metrics(gfx_font_t *font, gfx_font_metrics_t *metrics)
|
---|
| 267 | {
|
---|
| 268 | font->metrics = *metrics;
|
---|
| 269 | return EOK;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | /** Get first glyph in font.
|
---|
| 273 | *
|
---|
| 274 | * @param font Font
|
---|
| 275 | * @return First glyph or @c NULL if there are none
|
---|
| 276 | */
|
---|
| 277 | gfx_glyph_t *gfx_font_first_glyph(gfx_font_t *font)
|
---|
| 278 | {
|
---|
[c78a03d] | 279 | link_t *link;
|
---|
| 280 |
|
---|
| 281 | link = list_first(&font->glyphs);
|
---|
| 282 | if (link == NULL)
|
---|
| 283 | return NULL;
|
---|
| 284 |
|
---|
| 285 | return list_get_instance(link, gfx_glyph_t, lglyphs);
|
---|
[ee2f0beb] | 286 | }
|
---|
| 287 |
|
---|
| 288 | /** Get next glyph in font.
|
---|
| 289 | *
|
---|
| 290 | * @param cur Current glyph
|
---|
| 291 | * @return Next glyph in font or @c NULL if @a cur was the last one
|
---|
| 292 | */
|
---|
| 293 | gfx_glyph_t *gfx_font_next_glyph(gfx_glyph_t *cur)
|
---|
| 294 | {
|
---|
[c78a03d] | 295 | link_t *link;
|
---|
| 296 |
|
---|
| 297 | link = list_next(&cur->lglyphs, &cur->font->glyphs);
|
---|
| 298 | if (link == NULL)
|
---|
| 299 | return NULL;
|
---|
| 300 |
|
---|
| 301 | return list_get_instance(link, gfx_glyph_t, lglyphs);
|
---|
[ee2f0beb] | 302 | }
|
---|
| 303 |
|
---|
[d884672] | 304 | /** Get last glyph in font.
|
---|
| 305 | *
|
---|
| 306 | * @param font Font
|
---|
| 307 | * @return Last glyph or @c NULL if there are none
|
---|
| 308 | */
|
---|
| 309 | gfx_glyph_t *gfx_font_last_glyph(gfx_font_t *font)
|
---|
| 310 | {
|
---|
| 311 | link_t *link;
|
---|
| 312 |
|
---|
| 313 | link = list_last(&font->glyphs);
|
---|
| 314 | if (link == NULL)
|
---|
| 315 | return NULL;
|
---|
| 316 |
|
---|
| 317 | return list_get_instance(link, gfx_glyph_t, lglyphs);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /** Get previous glyph in font.
|
---|
| 321 | *
|
---|
| 322 | * @param cur Current glyph
|
---|
| 323 | * @return Previous glyph in font or @c NULL if @a cur was the last one
|
---|
| 324 | */
|
---|
| 325 | gfx_glyph_t *gfx_font_prev_glyph(gfx_glyph_t *cur)
|
---|
| 326 | {
|
---|
| 327 | link_t *link;
|
---|
| 328 |
|
---|
| 329 | link = list_prev(&cur->lglyphs, &cur->font->glyphs);
|
---|
| 330 | if (link == NULL)
|
---|
| 331 | return NULL;
|
---|
| 332 |
|
---|
| 333 | return list_get_instance(link, gfx_glyph_t, lglyphs);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[ee2f0beb] | 336 | /** Search for glyph that should be set for the beginning of a string.
|
---|
| 337 | *
|
---|
| 338 | * @param font Font
|
---|
| 339 | * @param str String whose beginning we would like to set
|
---|
| 340 | * @param rglyph Place to store glyph that should be set
|
---|
| 341 | * @param rsize Place to store number of bytes to advance in the string
|
---|
| 342 | * @return EOK on success, ENOENT if no matching glyph was found
|
---|
| 343 | */
|
---|
[a57c70c] | 344 | errno_t gfx_font_search_glyph(gfx_font_t *font, const char *str,
|
---|
[ee2f0beb] | 345 | gfx_glyph_t **rglyph, size_t *rsize)
|
---|
| 346 | {
|
---|
[c78a03d] | 347 | gfx_glyph_t *glyph;
|
---|
| 348 | size_t msize;
|
---|
| 349 |
|
---|
| 350 | glyph = gfx_font_first_glyph(font);
|
---|
| 351 | while (glyph != NULL) {
|
---|
| 352 | if (gfx_glyph_matches(glyph, str, &msize)) {
|
---|
| 353 | *rglyph = glyph;
|
---|
| 354 | *rsize = msize;
|
---|
| 355 | return EOK;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | glyph = gfx_font_next_glyph(glyph);
|
---|
| 359 | }
|
---|
| 360 |
|
---|
| 361 | return ENOENT;
|
---|
[ee2f0beb] | 362 | }
|
---|
| 363 |
|
---|
[d2100e2] | 364 | /** Replace glyph graphic with empty space of specified width.
|
---|
| 365 | *
|
---|
| 366 | * This is used to resize a glyph in the font bitmap. This changes
|
---|
[313ac8e] | 367 | * the bitmap widht might also make the bitmap taller.
|
---|
| 368 | * Dimensions of the glyph are also adjusted according to @a nrect.
|
---|
[d2100e2] | 369 | *
|
---|
| 370 | * @param font Font
|
---|
| 371 | * @param glyph Glyph to replace
|
---|
[313ac8e] | 372 | * @param nrect Replacement rectangle
|
---|
[d2100e2] | 373 | */
|
---|
| 374 | errno_t gfx_font_splice_at_glyph(gfx_font_t *font, gfx_glyph_t *glyph,
|
---|
[313ac8e] | 375 | gfx_rect_t *nrect)
|
---|
[d2100e2] | 376 | {
|
---|
| 377 | gfx_glyph_t *g;
|
---|
| 378 | gfx_bitmap_t *nbitmap;
|
---|
| 379 | gfx_bitmap_params_t params;
|
---|
| 380 | gfx_coord_t dwidth;
|
---|
[313ac8e] | 381 | gfx_coord_t x0;
|
---|
[d2100e2] | 382 | errno_t rc;
|
---|
| 383 |
|
---|
| 384 | /* Change of width of glyph */
|
---|
[313ac8e] | 385 | dwidth = (nrect->p1.x - nrect->p0.x) -
|
---|
| 386 | (glyph->rect.p1.x - glyph->rect.p0.x);
|
---|
[d2100e2] | 387 |
|
---|
| 388 | /* Create new font bitmap, wider by dwidth pixels */
|
---|
| 389 | gfx_bitmap_params_init(¶ms);
|
---|
| 390 | params.rect = font->rect;
|
---|
| 391 | params.rect.p1.x += dwidth;
|
---|
[313ac8e] | 392 | if (nrect->p1.y - nrect->p0.y > params.rect.p1.y)
|
---|
| 393 | params.rect.p1.y = nrect->p1.y - nrect->p0.y;
|
---|
[0d62c10] | 394 | params.flags = bmpf_color_key | bmpf_colorize;
|
---|
[74f59b7] | 395 | params.key_color = PIXEL(0, 0, 0, 0);
|
---|
[d2100e2] | 396 |
|
---|
[06b8383] | 397 | rc = gfx_bitmap_create(font->typeface->gc, ¶ms, NULL, &nbitmap);
|
---|
[d2100e2] | 398 | if (rc != EOK)
|
---|
| 399 | goto error;
|
---|
| 400 |
|
---|
[313ac8e] | 401 | /*
|
---|
| 402 | * In x0 we compute the left margin of @a glyph. We start with
|
---|
| 403 | * zero and then, if there are any preceding glyphs, we set it
|
---|
| 404 | * to the right margin of the last one.
|
---|
| 405 | */
|
---|
| 406 | x0 = 0;
|
---|
| 407 |
|
---|
[d2100e2] | 408 | /* Transfer glyphs before @a glyph */
|
---|
| 409 | g = gfx_font_first_glyph(font);
|
---|
| 410 | while (g != glyph) {
|
---|
| 411 | assert(g != NULL);
|
---|
| 412 |
|
---|
| 413 | rc = gfx_glyph_transfer(g, 0, nbitmap, ¶ms.rect);
|
---|
| 414 | if (rc != EOK)
|
---|
| 415 | goto error;
|
---|
| 416 |
|
---|
[313ac8e] | 417 | /* Left margin of the next glyph */
|
---|
| 418 | x0 = g->rect.p1.x;
|
---|
| 419 |
|
---|
[d2100e2] | 420 | g = gfx_font_next_glyph(g);
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /* Skip @a glyph */
|
---|
| 424 | g = gfx_font_next_glyph(g);
|
---|
| 425 |
|
---|
| 426 | /* Transfer glyphs after glyph */
|
---|
| 427 | while (g != NULL) {
|
---|
| 428 | rc = gfx_glyph_transfer(g, dwidth, nbitmap, ¶ms.rect);
|
---|
| 429 | if (rc != EOK)
|
---|
| 430 | goto error;
|
---|
| 431 |
|
---|
| 432 | /* Update glyph coordinates */
|
---|
| 433 | g->rect.p0.x += dwidth;
|
---|
| 434 | g->rect.p1.x += dwidth;
|
---|
| 435 | g->origin.x += dwidth;
|
---|
| 436 |
|
---|
| 437 | g = gfx_font_next_glyph(g);
|
---|
| 438 | }
|
---|
| 439 |
|
---|
[313ac8e] | 440 | /* Place glyph rectangle inside the newly created space */
|
---|
| 441 | glyph->origin.x = x0 - nrect->p0.x;
|
---|
| 442 | glyph->origin.y = 0 - nrect->p0.y;
|
---|
| 443 | gfx_rect_translate(&glyph->origin, nrect, &glyph->rect);
|
---|
[d2100e2] | 444 |
|
---|
| 445 | /* Update font bitmap */
|
---|
| 446 | gfx_bitmap_destroy(font->bitmap);
|
---|
| 447 | font->bitmap = nbitmap;
|
---|
| 448 | font->rect = params.rect;
|
---|
| 449 |
|
---|
| 450 | return EOK;
|
---|
| 451 | error:
|
---|
| 452 | if (nbitmap != NULL)
|
---|
| 453 | gfx_bitmap_destroy(nbitmap);
|
---|
| 454 | return rc;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[ea459d4] | 457 | /** Load font properties from RIFF TPF file.
|
---|
| 458 | *
|
---|
| 459 | * @param parent Parent chunk
|
---|
| 460 | * @param props Font properties
|
---|
| 461 | * @return EOK on success or an error code
|
---|
| 462 | */
|
---|
| 463 | static errno_t gfx_font_props_load(riff_rchunk_t *parent,
|
---|
| 464 | gfx_font_props_t *props)
|
---|
| 465 | {
|
---|
| 466 | errno_t rc;
|
---|
| 467 | riff_rchunk_t propsck;
|
---|
[453f203b] | 468 | tpf_font_props_t tprops;
|
---|
[ea459d4] | 469 | size_t nread;
|
---|
| 470 |
|
---|
| 471 | rc = riff_rchunk_match(parent, CKID_fprp, &propsck);
|
---|
| 472 | if (rc != EOK)
|
---|
| 473 | return rc;
|
---|
| 474 |
|
---|
[453f203b] | 475 | rc = riff_read(&propsck, (void *) &tprops, sizeof(tprops), &nread);
|
---|
| 476 | if (rc != EOK || nread != sizeof(tprops))
|
---|
[ea459d4] | 477 | return EIO;
|
---|
| 478 |
|
---|
| 479 | rc = riff_rchunk_end(&propsck);
|
---|
| 480 | if (rc != EOK)
|
---|
| 481 | return rc;
|
---|
| 482 |
|
---|
[453f203b] | 483 | props->size = uint16_t_le2host(tprops.size);
|
---|
| 484 | props->flags = uint16_t_le2host(tprops.flags);
|
---|
| 485 |
|
---|
[ea459d4] | 486 | return EOK;
|
---|
| 487 | }
|
---|
| 488 |
|
---|
[d53af3c8] | 489 | /** Save font properties to RIFF TPF file.
|
---|
| 490 | *
|
---|
| 491 | * @param props Font properties
|
---|
| 492 | * @param riffw RIFF writer
|
---|
| 493 | * @return EOK on success or an error code
|
---|
| 494 | */
|
---|
| 495 | static errno_t gfx_font_props_save(gfx_font_props_t *props, riffw_t *riffw)
|
---|
| 496 | {
|
---|
| 497 | errno_t rc;
|
---|
| 498 | riff_wchunk_t propsck;
|
---|
[453f203b] | 499 | tpf_font_props_t tprops;
|
---|
| 500 |
|
---|
| 501 | tprops.size = host2uint16_t_le(props->size);
|
---|
| 502 | tprops.flags = host2uint16_t_le(props->flags);
|
---|
[d53af3c8] | 503 |
|
---|
| 504 | rc = riff_wchunk_start(riffw, CKID_fprp, &propsck);
|
---|
| 505 | if (rc != EOK)
|
---|
| 506 | return rc;
|
---|
| 507 |
|
---|
[453f203b] | 508 | rc = riff_write(riffw, (void *) &tprops, sizeof(tprops));
|
---|
[d53af3c8] | 509 | if (rc != EOK)
|
---|
| 510 | return rc;
|
---|
| 511 |
|
---|
| 512 | rc = riff_wchunk_end(riffw, &propsck);
|
---|
| 513 | if (rc != EOK)
|
---|
| 514 | return rc;
|
---|
| 515 |
|
---|
| 516 | return EOK;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[ea459d4] | 519 | /** Load font metrics from RIFF TPF file.
|
---|
| 520 | *
|
---|
| 521 | * @param parent Parent chunk
|
---|
| 522 | * @param metrics Font metrics
|
---|
| 523 | * @return EOK on success or an error code
|
---|
| 524 | */
|
---|
| 525 | static errno_t gfx_font_metrics_load(riff_rchunk_t *parent,
|
---|
| 526 | gfx_font_metrics_t *metrics)
|
---|
| 527 | {
|
---|
| 528 | errno_t rc;
|
---|
| 529 | riff_rchunk_t mtrck;
|
---|
[453f203b] | 530 | tpf_font_metrics_t tmetrics;
|
---|
[ea459d4] | 531 | size_t nread;
|
---|
| 532 |
|
---|
| 533 | rc = riff_rchunk_match(parent, CKID_fmtr, &mtrck);
|
---|
| 534 | if (rc != EOK)
|
---|
| 535 | return rc;
|
---|
| 536 |
|
---|
[453f203b] | 537 | rc = riff_read(&mtrck, (void *) &tmetrics, sizeof(tmetrics), &nread);
|
---|
| 538 | if (rc != EOK || nread != sizeof(tmetrics))
|
---|
[ea459d4] | 539 | return EIO;
|
---|
| 540 |
|
---|
| 541 | rc = riff_rchunk_end(&mtrck);
|
---|
| 542 | if (rc != EOK)
|
---|
| 543 | return rc;
|
---|
| 544 |
|
---|
[453f203b] | 545 | metrics->ascent = uint16_t_le2host(tmetrics.ascent);
|
---|
| 546 | metrics->descent = uint16_t_le2host(tmetrics.descent);
|
---|
| 547 | metrics->leading = uint16_t_le2host(tmetrics.leading);
|
---|
[5c27e77] | 548 | metrics->underline_y0 = uint16_t_le2host(tmetrics.underline_y0);
|
---|
| 549 | metrics->underline_y1 = uint16_t_le2host(tmetrics.underline_y1);
|
---|
[ea459d4] | 550 | return EOK;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
[d53af3c8] | 553 | /** Save font metrics to RIFF TPF file.
|
---|
| 554 | *
|
---|
| 555 | * @param metrics Font metrics
|
---|
| 556 | * @param riffw RIFF writer
|
---|
| 557 | * @return EOK on success or an error code
|
---|
| 558 | */
|
---|
| 559 | static errno_t gfx_font_metrics_save(gfx_font_metrics_t *metrics,
|
---|
| 560 | riffw_t *riffw)
|
---|
| 561 | {
|
---|
| 562 | errno_t rc;
|
---|
[453f203b] | 563 | tpf_font_metrics_t tmetrics;
|
---|
[d53af3c8] | 564 | riff_wchunk_t mtrck;
|
---|
| 565 |
|
---|
[453f203b] | 566 | tmetrics.ascent = host2uint16_t_le(metrics->ascent);
|
---|
| 567 | tmetrics.descent = host2uint16_t_le(metrics->descent);
|
---|
| 568 | tmetrics.leading = host2uint16_t_le(metrics->leading);
|
---|
[5c27e77] | 569 | tmetrics.underline_y0 = host2uint16_t_le(metrics->underline_y0);
|
---|
| 570 | tmetrics.underline_y1 = host2uint16_t_le(metrics->underline_y1);
|
---|
[453f203b] | 571 |
|
---|
[d53af3c8] | 572 | rc = riff_wchunk_start(riffw, CKID_fmtr, &mtrck);
|
---|
| 573 | if (rc != EOK)
|
---|
| 574 | return rc;
|
---|
| 575 |
|
---|
[453f203b] | 576 | rc = riff_write(riffw, (void *) &tmetrics, sizeof(tmetrics));
|
---|
[d53af3c8] | 577 | if (rc != EOK)
|
---|
| 578 | return rc;
|
---|
| 579 |
|
---|
| 580 | rc = riff_wchunk_end(riffw, &mtrck);
|
---|
| 581 | if (rc != EOK)
|
---|
| 582 | return rc;
|
---|
| 583 |
|
---|
| 584 | return EOK;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
[f13f1495] | 587 | /** Bit pack font bitmap into 1 bits/pixel format.
|
---|
| 588 | *
|
---|
| 589 | * @param width Bitmap width
|
---|
| 590 | * @param height Bitmap height
|
---|
| 591 | * @param pixels Bitmap pixels
|
---|
| 592 | * @param rdata Place to store pointer to packed data
|
---|
| 593 | * @param rsize Place to store size of packed data
|
---|
| 594 | * @return EOK on sucess, ENOMEM if out of memory
|
---|
| 595 | */
|
---|
| 596 | errno_t gfx_font_bitmap_pack(gfx_coord_t width, gfx_coord_t height,
|
---|
| 597 | uint32_t *pixels, void **rdata, size_t *rsize)
|
---|
| 598 | {
|
---|
| 599 | void *data;
|
---|
| 600 | size_t size;
|
---|
| 601 | size_t bytes_line;
|
---|
| 602 | gfx_coord_t x, y;
|
---|
| 603 | uint8_t b;
|
---|
| 604 | uint8_t *dp;
|
---|
| 605 | uint32_t *sp;
|
---|
| 606 | uint32_t pix;
|
---|
| 607 |
|
---|
| 608 | bytes_line = (width + 7) / 8;
|
---|
| 609 | size = height * bytes_line;
|
---|
| 610 |
|
---|
| 611 | data = malloc(size);
|
---|
| 612 | if (data == NULL)
|
---|
| 613 | return ENOMEM;
|
---|
| 614 |
|
---|
| 615 | sp = pixels;
|
---|
| 616 | dp = data;
|
---|
| 617 | for (y = 0; y < height; y++) {
|
---|
| 618 | b = 0;
|
---|
| 619 | for (x = 0; x < width; x++) {
|
---|
| 620 | pix = *sp++;
|
---|
| 621 | b = (b << 1) | (pix & 1);
|
---|
| 622 | /* Write eight bits */
|
---|
| 623 | if ((x & 7) == 7) {
|
---|
| 624 | *dp++ = b;
|
---|
| 625 | b = 0;
|
---|
| 626 | }
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | /* Write remaining bits */
|
---|
| 630 | if ((x & 7) != 0) {
|
---|
| 631 | b = b << (8 - (x & 7));
|
---|
| 632 | *dp++ = b;
|
---|
| 633 | }
|
---|
| 634 | }
|
---|
| 635 |
|
---|
| 636 | *rdata = data;
|
---|
| 637 | *rsize = size;
|
---|
| 638 | return EOK;
|
---|
| 639 | }
|
---|
| 640 |
|
---|
| 641 | /** Unpack font bitmap from 1 bits/pixel format.
|
---|
| 642 | *
|
---|
| 643 | * @param width Bitmap width
|
---|
| 644 | * @param height Bitmap height
|
---|
| 645 | * @param data Packed data
|
---|
| 646 | * @param dsize Size of packed data
|
---|
| 647 | * @param pixels Bitmap pixels
|
---|
| 648 | * @return EOK on success, EINVAL if data size is invalid
|
---|
| 649 | */
|
---|
| 650 | errno_t gfx_font_bitmap_unpack(gfx_coord_t width, gfx_coord_t height,
|
---|
| 651 | void *data, size_t dsize, uint32_t *pixels)
|
---|
| 652 | {
|
---|
| 653 | size_t size;
|
---|
| 654 | size_t bytes_line;
|
---|
| 655 | gfx_coord_t x, y;
|
---|
| 656 | uint8_t b;
|
---|
| 657 | uint8_t *sp;
|
---|
| 658 | uint32_t *dp;
|
---|
| 659 |
|
---|
| 660 | bytes_line = (width + 7) / 8;
|
---|
| 661 | size = height * bytes_line;
|
---|
| 662 |
|
---|
| 663 | if (dsize != size)
|
---|
| 664 | return EINVAL;
|
---|
| 665 |
|
---|
| 666 | sp = data;
|
---|
| 667 | dp = pixels;
|
---|
| 668 | for (y = 0; y < height; y++) {
|
---|
| 669 | b = 0;
|
---|
| 670 | for (x = 0; x < width; x++) {
|
---|
| 671 | if ((x & 7) == 0)
|
---|
| 672 | b = *sp++;
|
---|
| 673 | *dp++ = (b >> 7) ? PIXEL(255, 255, 255, 255) :
|
---|
| 674 | PIXEL(0, 0, 0, 0);
|
---|
| 675 | b = b << 1;
|
---|
| 676 | }
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | return EOK;
|
---|
| 680 | }
|
---|
| 681 |
|
---|
[ea459d4] | 682 | /** Load font bitmap from RIFF TPF file.
|
---|
| 683 | *
|
---|
| 684 | * @param parent Parent chunk
|
---|
| 685 | * @param font Font
|
---|
| 686 | * @return EOK on success or an error code
|
---|
| 687 | */
|
---|
| 688 | static errno_t gfx_font_bitmap_load(riff_rchunk_t *parent, gfx_font_t *font)
|
---|
| 689 | {
|
---|
| 690 | errno_t rc;
|
---|
| 691 | riff_rchunk_t bmpck;
|
---|
[453f203b] | 692 | tpf_font_bmp_hdr_t thdr;
|
---|
[ea459d4] | 693 | gfx_bitmap_params_t params;
|
---|
| 694 | gfx_bitmap_alloc_t alloc;
|
---|
| 695 | gfx_bitmap_t *bitmap = NULL;
|
---|
| 696 | uint32_t width;
|
---|
| 697 | uint32_t height;
|
---|
[453f203b] | 698 | uint16_t fmt;
|
---|
| 699 | uint16_t depth;
|
---|
[f13f1495] | 700 | size_t bytes_line;
|
---|
| 701 | void *data = NULL;
|
---|
| 702 | size_t size;
|
---|
[ea459d4] | 703 | size_t nread;
|
---|
| 704 |
|
---|
| 705 | rc = riff_rchunk_match(parent, CKID_fbmp, &bmpck);
|
---|
| 706 | if (rc != EOK)
|
---|
| 707 | goto error;
|
---|
| 708 |
|
---|
[453f203b] | 709 | rc = riff_read(&bmpck, &thdr, sizeof(thdr), &nread);
|
---|
| 710 | if (rc != EOK || nread != sizeof(thdr))
|
---|
[ea459d4] | 711 | goto error;
|
---|
| 712 |
|
---|
[453f203b] | 713 | width = uint32_t_le2host(thdr.width);
|
---|
| 714 | height = uint32_t_le2host(thdr.height);
|
---|
| 715 | fmt = uint16_t_le2host(thdr.fmt);
|
---|
| 716 | depth = uint16_t_le2host(thdr.depth);
|
---|
[ea459d4] | 717 |
|
---|
[f13f1495] | 718 | if (fmt != 0 || depth != 1) {
|
---|
[ea459d4] | 719 | rc = ENOTSUP;
|
---|
| 720 | goto error;
|
---|
| 721 | }
|
---|
| 722 |
|
---|
[f13f1495] | 723 | bytes_line = (width + 7) / 8;
|
---|
| 724 | size = height * bytes_line;
|
---|
| 725 |
|
---|
| 726 | data = malloc(size);
|
---|
| 727 | if (data == NULL) {
|
---|
| 728 | rc = ENOMEM;
|
---|
| 729 | goto error;
|
---|
| 730 | }
|
---|
| 731 |
|
---|
[ea459d4] | 732 | gfx_bitmap_params_init(¶ms);
|
---|
| 733 | params.rect.p0.x = 0;
|
---|
| 734 | params.rect.p0.y = 0;
|
---|
| 735 | params.rect.p1.x = width;
|
---|
| 736 | params.rect.p1.y = height;
|
---|
[0d62c10] | 737 | params.flags = bmpf_color_key | bmpf_colorize;
|
---|
[74f59b7] | 738 | params.key_color = PIXEL(0, 0, 0, 0);
|
---|
[ea459d4] | 739 |
|
---|
| 740 | rc = gfx_bitmap_create(font->typeface->gc, ¶ms, NULL, &bitmap);
|
---|
| 741 | if (rc != EOK)
|
---|
| 742 | goto error;
|
---|
| 743 |
|
---|
| 744 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 745 | if (rc != EOK)
|
---|
| 746 | goto error;
|
---|
| 747 |
|
---|
[f13f1495] | 748 | rc = riff_read(&bmpck, data, size, &nread);
|
---|
[ea459d4] | 749 | if (rc != EOK)
|
---|
| 750 | goto error;
|
---|
| 751 |
|
---|
[f13f1495] | 752 | if (nread != size) {
|
---|
[ea459d4] | 753 | rc = EIO;
|
---|
| 754 | goto error;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
| 757 | rc = riff_rchunk_end(&bmpck);
|
---|
| 758 | if (rc != EOK)
|
---|
| 759 | goto error;
|
---|
| 760 |
|
---|
[f13f1495] | 761 | rc = gfx_font_bitmap_unpack(width, height, data, size, alloc.pixels);
|
---|
| 762 | if (rc != EOK)
|
---|
| 763 | goto error;
|
---|
| 764 |
|
---|
| 765 | free(data);
|
---|
[ea459d4] | 766 | gfx_bitmap_destroy(font->bitmap);
|
---|
| 767 | font->bitmap = bitmap;
|
---|
| 768 | font->rect = params.rect;
|
---|
| 769 | return EOK;
|
---|
| 770 | error:
|
---|
[f13f1495] | 771 | if (data != NULL)
|
---|
| 772 | free(data);
|
---|
[ea459d4] | 773 | if (bitmap != NULL)
|
---|
| 774 | gfx_bitmap_destroy(bitmap);
|
---|
| 775 | return rc;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[d53af3c8] | 778 | /** Save font bitmap to RIFF TPF file.
|
---|
| 779 | *
|
---|
| 780 | * @param font Font
|
---|
| 781 | * @param riffw RIFF writer
|
---|
| 782 | * @return EOK on success or an error code
|
---|
| 783 | */
|
---|
| 784 | static errno_t gfx_font_bitmap_save(gfx_font_t *font, riffw_t *riffw)
|
---|
| 785 | {
|
---|
| 786 | errno_t rc;
|
---|
[453f203b] | 787 | tpf_font_bmp_hdr_t thdr;
|
---|
[d53af3c8] | 788 | riff_wchunk_t bmpck;
|
---|
| 789 | gfx_bitmap_alloc_t alloc;
|
---|
[f13f1495] | 790 | void *data = NULL;
|
---|
| 791 | size_t dsize;
|
---|
[453f203b] | 792 |
|
---|
[d53af3c8] | 793 | rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
|
---|
| 794 | if (rc != EOK)
|
---|
| 795 | return rc;
|
---|
| 796 |
|
---|
[f13f1495] | 797 | rc = gfx_font_bitmap_pack(font->rect.p1.x, font->rect.p1.y,
|
---|
| 798 | alloc.pixels, &data, &dsize);
|
---|
[d53af3c8] | 799 | if (rc != EOK)
|
---|
| 800 | return rc;
|
---|
| 801 |
|
---|
[f13f1495] | 802 | thdr.width = host2uint32_t_le(font->rect.p1.x);
|
---|
| 803 | thdr.height = host2uint32_t_le(font->rect.p1.y);
|
---|
| 804 | thdr.fmt = 0;
|
---|
| 805 | thdr.depth = host2uint16_t_le(1);
|
---|
| 806 |
|
---|
| 807 | rc = riff_wchunk_start(riffw, CKID_fbmp, &bmpck);
|
---|
| 808 | if (rc != EOK)
|
---|
| 809 | goto error;
|
---|
| 810 |
|
---|
[453f203b] | 811 | rc = riff_write(riffw, &thdr, sizeof(thdr));
|
---|
[d53af3c8] | 812 | if (rc != EOK)
|
---|
[f13f1495] | 813 | goto error;
|
---|
[d53af3c8] | 814 |
|
---|
[f13f1495] | 815 | rc = riff_write(riffw, data, dsize);
|
---|
[d53af3c8] | 816 | if (rc != EOK)
|
---|
[f13f1495] | 817 | goto error;
|
---|
[d53af3c8] | 818 |
|
---|
| 819 | rc = riff_wchunk_end(riffw, &bmpck);
|
---|
| 820 | if (rc != EOK)
|
---|
[f13f1495] | 821 | goto error;
|
---|
[d53af3c8] | 822 |
|
---|
[f13f1495] | 823 | free(data);
|
---|
[d53af3c8] | 824 | return EOK;
|
---|
[f13f1495] | 825 | error:
|
---|
| 826 | if (data != NULL)
|
---|
| 827 | free(data);
|
---|
| 828 | return rc;
|
---|
[d53af3c8] | 829 | }
|
---|
| 830 |
|
---|
[ea459d4] | 831 | /** Load font info from RIFF TPF file.
|
---|
| 832 | *
|
---|
| 833 | * @param tface Containing typeface
|
---|
| 834 | * @param parent Parent chunk
|
---|
| 835 | * @return EOK on success or an error code
|
---|
| 836 | */
|
---|
| 837 | errno_t gfx_font_info_load(gfx_typeface_t *tface, riff_rchunk_t *parent)
|
---|
| 838 | {
|
---|
| 839 | errno_t rc;
|
---|
| 840 | riff_rchunk_t fontck;
|
---|
| 841 | gfx_font_props_t props;
|
---|
| 842 | gfx_font_info_t *finfo = NULL;
|
---|
| 843 | gfx_font_t *font = NULL;
|
---|
| 844 |
|
---|
| 845 | rc = riff_rchunk_list_match(parent, LTYPE_font, &fontck);
|
---|
| 846 | if (rc != EOK)
|
---|
| 847 | goto error;
|
---|
| 848 |
|
---|
| 849 | finfo = calloc(1, sizeof(gfx_font_info_t));
|
---|
| 850 | if (finfo == NULL) {
|
---|
| 851 | rc = ENOMEM;
|
---|
| 852 | goto error;
|
---|
| 853 | }
|
---|
| 854 |
|
---|
| 855 | finfo->fontck = fontck;
|
---|
| 856 |
|
---|
| 857 | rc = gfx_font_props_load(&fontck, &props);
|
---|
| 858 | if (rc != EOK)
|
---|
| 859 | goto error;
|
---|
| 860 |
|
---|
| 861 | rc = riff_rchunk_end(&fontck);
|
---|
| 862 | if (rc != EOK)
|
---|
| 863 | goto error;
|
---|
| 864 |
|
---|
| 865 | finfo->typeface = tface;
|
---|
| 866 | list_append(&finfo->lfonts, &tface->fonts);
|
---|
| 867 | finfo->props = props;
|
---|
| 868 | finfo->font = NULL;
|
---|
| 869 |
|
---|
| 870 | return EOK;
|
---|
| 871 | error:
|
---|
| 872 | if (finfo != NULL)
|
---|
| 873 | free(finfo);
|
---|
| 874 | if (font != NULL)
|
---|
| 875 | gfx_font_close(font);
|
---|
| 876 | return rc;
|
---|
| 877 | }
|
---|
| 878 |
|
---|
| 879 | /** Load font from RIFF TPF file.
|
---|
| 880 | *
|
---|
| 881 | * @param finfo Font information
|
---|
| 882 | * @return EOK on success or an error code
|
---|
| 883 | */
|
---|
| 884 | errno_t gfx_font_load(gfx_font_info_t *finfo)
|
---|
| 885 | {
|
---|
| 886 | errno_t rc;
|
---|
| 887 | gfx_font_metrics_t metrics;
|
---|
| 888 | gfx_font_t *font = NULL;
|
---|
| 889 |
|
---|
| 890 | /* Seek to beginning of chunk (just after list type) */
|
---|
| 891 | rc = riff_rchunk_seek(&finfo->fontck, sizeof(uint32_t), SEEK_SET);
|
---|
| 892 | if (rc != EOK)
|
---|
| 893 | goto error;
|
---|
| 894 |
|
---|
| 895 | rc = gfx_font_props_load(&finfo->fontck, &finfo->props);
|
---|
| 896 | if (rc != EOK)
|
---|
| 897 | goto error;
|
---|
| 898 |
|
---|
| 899 | rc = gfx_font_metrics_load(&finfo->fontck, &metrics);
|
---|
| 900 | if (rc != EOK)
|
---|
| 901 | goto error;
|
---|
| 902 |
|
---|
| 903 | rc = gfx_font_create_with_info(finfo->typeface, finfo, &metrics, &font);
|
---|
| 904 | if (rc != EOK)
|
---|
| 905 | goto error;
|
---|
| 906 |
|
---|
| 907 | rc = gfx_font_bitmap_load(&finfo->fontck, font);
|
---|
| 908 | if (rc != EOK)
|
---|
| 909 | goto error;
|
---|
| 910 |
|
---|
| 911 | while (true) {
|
---|
| 912 | rc = gfx_glyph_load(font, &finfo->fontck);
|
---|
| 913 | if (rc == ENOENT)
|
---|
| 914 | break;
|
---|
| 915 | if (rc != EOK)
|
---|
| 916 | goto error;
|
---|
| 917 | }
|
---|
| 918 |
|
---|
| 919 | finfo->font = font;
|
---|
| 920 | return EOK;
|
---|
| 921 | error:
|
---|
| 922 | if (font != NULL)
|
---|
| 923 | gfx_font_close(font);
|
---|
| 924 | return rc;
|
---|
| 925 | }
|
---|
| 926 |
|
---|
[d53af3c8] | 927 | /** Save font into RIFF TPF file.
|
---|
| 928 | *
|
---|
| 929 | * @param finfo Font info
|
---|
| 930 | * @param riffw RIFF writer
|
---|
[ea459d4] | 931 | * @return EOK on success or an error code
|
---|
[d53af3c8] | 932 | */
|
---|
| 933 | errno_t gfx_font_save(gfx_font_info_t *finfo, riffw_t *riffw)
|
---|
| 934 | {
|
---|
| 935 | errno_t rc;
|
---|
| 936 | riff_wchunk_t fontck;
|
---|
| 937 | gfx_glyph_t *glyph;
|
---|
| 938 |
|
---|
| 939 | rc = riff_wchunk_start(riffw, CKID_LIST, &fontck);
|
---|
| 940 | if (rc != EOK)
|
---|
| 941 | return rc;
|
---|
| 942 |
|
---|
| 943 | rc = riff_write_uint32(riffw, LTYPE_font);
|
---|
| 944 | if (rc != EOK)
|
---|
| 945 | return rc;
|
---|
| 946 |
|
---|
| 947 | rc = gfx_font_props_save(&finfo->props, riffw);
|
---|
| 948 | if (rc != EOK)
|
---|
| 949 | return rc;
|
---|
| 950 |
|
---|
| 951 | rc = gfx_font_metrics_save(&finfo->font->metrics, riffw);
|
---|
| 952 | if (rc != EOK)
|
---|
| 953 | return rc;
|
---|
| 954 |
|
---|
| 955 | rc = gfx_font_bitmap_save(finfo->font, riffw);
|
---|
| 956 | if (rc != EOK)
|
---|
| 957 | return rc;
|
---|
| 958 |
|
---|
| 959 | glyph = gfx_font_first_glyph(finfo->font);
|
---|
| 960 | while (glyph != NULL) {
|
---|
| 961 | rc = gfx_glyph_save(glyph, riffw);
|
---|
| 962 | if (rc != EOK)
|
---|
| 963 | return rc;
|
---|
| 964 |
|
---|
| 965 | glyph = gfx_font_next_glyph(glyph);
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 | rc = riff_wchunk_end(riffw, &fontck);
|
---|
| 969 | if (rc != EOK)
|
---|
| 970 | return rc;
|
---|
| 971 |
|
---|
| 972 | return EOK;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
[ee2f0beb] | 975 | /** @}
|
---|
| 976 | */
|
---|