Changeset 453f203b in mainline


Ignore:
Timestamp:
2020-09-25T14:31:27Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
16357ec
Parents:
120031a5
git-author:
Jiri Svoboda <jiri@…> (2020-09-24 18:30:07)
git-committer:
Jiri Svoboda <jiri@…> (2020-09-25 14:31:27)
Message:

Cannot just write structures to TPF file

This would compromise portability of TPF files. What was I thinking?
Endianness and type widths must be fixed.

Location:
uspace/lib/gfxfont
Files:
3 edited

Legend:

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

    r120031a5 r453f203b  
    3838#define _GFX_PRIVATE_TPF_FILE_H
    3939
     40#include <stdint.h>
     41
    4042enum {
    4143        /** Typeface RIFF format ID */
     
    6365};
    6466
     67/** TPF font properties */
     68typedef struct {
     69        uint16_t size;
     70        uint16_t flags;
     71} tpf_font_props_t;
     72
     73/** TPF font metrics */
     74typedef struct {
     75        uint16_t ascent;
     76        uint16_t descent;
     77        uint16_t leading;
     78} tpf_font_metrics_t;
     79
     80/** TPF glyph metrics */
     81typedef struct {
     82        uint16_t advance;
     83} tpf_glyph_metrics_t;
     84
     85/** TPF glyph rectangle/origin */
     86typedef struct {
     87        /** Rectangle p0.x */
     88        uint32_t p0x;
     89        /** Rectangle p0.y */
     90        uint32_t p0y;
     91        /** Rectangle p1.x */
     92        uint32_t p1x;
     93        /** Rectangle p1.y */
     94        uint32_t p1y;
     95        /** Origin X */
     96        uint32_t orig_x;
     97        /** Origin Y */
     98        uint32_t orig_y;
     99} tpf_glyph_ror_t;
     100
     101/** TPF font bitmap header */
     102typedef struct {
     103        /** Width in pixels */
     104        uint32_t width;
     105        /** Height in pixels */
     106        uint32_t height;
     107        /** Format (0) */
     108        uint16_t fmt;
     109        /** Depth (bits/pixel) */
     110        uint16_t depth;
     111} tpf_font_bmp_hdr_t;
     112
    65113#endif
    66114
  • uspace/lib/gfxfont/src/font.c

    r120031a5 r453f203b  
    3636#include <adt/list.h>
    3737#include <assert.h>
     38#include <byteorder.h>
    3839#include <errno.h>
    3940#include <gfx/bitmap.h>
     
    403404        errno_t rc;
    404405        riff_rchunk_t propsck;
     406        tpf_font_props_t tprops;
    405407        size_t nread;
    406408
     
    409411                return rc;
    410412
    411         rc = riff_read(&propsck, (void *) props, sizeof(*props), &nread);
    412         if (rc != EOK || nread != sizeof(*props))
     413        rc = riff_read(&propsck, (void *) &tprops, sizeof(tprops), &nread);
     414        if (rc != EOK || nread != sizeof(tprops))
    413415                return EIO;
    414416
     
    416418        if (rc != EOK)
    417419                return rc;
     420
     421        props->size = uint16_t_le2host(tprops.size);
     422        props->flags = uint16_t_le2host(tprops.flags);
    418423
    419424        return EOK;
     
    430435        errno_t rc;
    431436        riff_wchunk_t propsck;
     437        tpf_font_props_t tprops;
     438
     439        tprops.size = host2uint16_t_le(props->size);
     440        tprops.flags = host2uint16_t_le(props->flags);
    432441
    433442        rc = riff_wchunk_start(riffw, CKID_fprp, &propsck);
     
    435444                return rc;
    436445
    437         rc = riff_write(riffw, (void *) props, sizeof(*props));
     446        rc = riff_write(riffw, (void *) &tprops, sizeof(tprops));
    438447        if (rc != EOK)
    439448                return rc;
     
    457466        errno_t rc;
    458467        riff_rchunk_t mtrck;
     468        tpf_font_metrics_t tmetrics;
    459469        size_t nread;
    460470
     
    463473                return rc;
    464474
    465         rc = riff_read(&mtrck, (void *) metrics, sizeof(*metrics), &nread);
    466         if (rc != EOK || nread != sizeof(*metrics))
     475        rc = riff_read(&mtrck, (void *) &tmetrics, sizeof(tmetrics), &nread);
     476        if (rc != EOK || nread != sizeof(tmetrics))
    467477                return EIO;
    468478
     
    471481                return rc;
    472482
     483        metrics->ascent = uint16_t_le2host(tmetrics.ascent);
     484        metrics->descent = uint16_t_le2host(tmetrics.descent);
     485        metrics->leading = uint16_t_le2host(tmetrics.leading);
    473486        return EOK;
    474487}
     
    484497{
    485498        errno_t rc;
     499        tpf_font_metrics_t tmetrics;
    486500        riff_wchunk_t mtrck;
    487501
     502        tmetrics.ascent = host2uint16_t_le(metrics->ascent);
     503        tmetrics.descent = host2uint16_t_le(metrics->descent);
     504        tmetrics.leading = host2uint16_t_le(metrics->leading);
     505
    488506        rc = riff_wchunk_start(riffw, CKID_fmtr, &mtrck);
    489507        if (rc != EOK)
    490508                return rc;
    491509
    492         rc = riff_write(riffw, (void *) metrics, sizeof(*metrics));
     510        rc = riff_write(riffw, (void *) &tmetrics, sizeof(tmetrics));
    493511        if (rc != EOK)
    494512                return rc;
     
    511529        errno_t rc;
    512530        riff_rchunk_t bmpck;
     531        tpf_font_bmp_hdr_t thdr;
    513532        gfx_bitmap_params_t params;
    514533        gfx_bitmap_alloc_t alloc;
     
    516535        uint32_t width;
    517536        uint32_t height;
    518         uint32_t depth;
     537        uint16_t fmt;
     538        uint16_t depth;
    519539        size_t nread;
    520540
     
    523543                goto error;
    524544
    525         rc = riff_read_uint32(&bmpck, &width);
    526         if (rc != EOK)
    527                 goto error;
    528 
    529         rc = riff_read_uint32(&bmpck, &height);
    530         if (rc != EOK)
    531                 goto error;
    532 
    533         rc = riff_read_uint32(&bmpck, &depth);
    534         if (rc != EOK)
    535                 goto error;
    536 
    537         if (depth != 8 * sizeof(uint32_t)) {
     545        rc = riff_read(&bmpck, &thdr, sizeof(thdr), &nread);
     546        if (rc != EOK || nread != sizeof(thdr))
     547                goto error;
     548
     549        width = uint32_t_le2host(thdr.width);
     550        height = uint32_t_le2host(thdr.height);
     551        fmt = uint16_t_le2host(thdr.fmt);
     552        depth = uint16_t_le2host(thdr.depth);
     553
     554        if (fmt != 0 || depth != 8 * sizeof(uint32_t)) {
    538555                rc = ENOTSUP;
    539556                goto error;
     
    587604{
    588605        errno_t rc;
     606        tpf_font_bmp_hdr_t thdr;
    589607        riff_wchunk_t bmpck;
    590608        gfx_bitmap_alloc_t alloc;
    591609
     610        thdr.width = host2uint32_t_le(font->rect.p1.x);
     611        thdr.height = host2uint32_t_le(font->rect.p1.y);
     612        thdr.fmt = 0;
     613        thdr.depth = host2uint16_t_le(8 * sizeof(uint32_t));
     614
    592615        rc = gfx_bitmap_get_alloc(font->bitmap, &alloc);
    593616        if (rc != EOK)
     
    598621                return rc;
    599622
    600         rc = riff_write_uint32(riffw, font->rect.p1.x);
    601         if (rc != EOK)
    602                 return rc;
    603 
    604         rc = riff_write_uint32(riffw, font->rect.p1.y);
    605         if (rc != EOK)
    606                 return rc;
    607 
    608         rc = riff_write_uint32(riffw, 8 * sizeof(uint32_t));
     623        rc = riff_write(riffw, &thdr, sizeof(thdr));
    609624        if (rc != EOK)
    610625                return rc;
  • uspace/lib/gfxfont/src/glyph.c

    r120031a5 r453f203b  
    3535
    3636#include <assert.h>
     37#include <byteorder.h>
    3738#include <errno.h>
    3839#include <gfx/bitmap.h>
     
    326327        errno_t rc;
    327328        riff_rchunk_t mtrck;
     329        tpf_glyph_metrics_t tmetrics;
    328330        size_t nread;
    329331
     
    332334                return rc;
    333335
    334         rc = riff_read(&mtrck, (void *) metrics, sizeof(*metrics), &nread);
    335         if (rc != EOK || nread != sizeof(*metrics))
     336        rc = riff_read(&mtrck, (void *) &tmetrics, sizeof(tmetrics), &nread);
     337        if (rc != EOK || nread != sizeof(tmetrics))
    336338                return EIO;
    337339
     
    340342                return rc;
    341343
     344        metrics->advance = uint16_t_le2host(tmetrics.advance);
    342345        return EOK;
    343346}
     
    353356{
    354357        errno_t rc;
     358        tpf_glyph_metrics_t tmetrics;
    355359        riff_wchunk_t mtrck;
    356360
     361        tmetrics.advance = host2uint16_t_le(metrics->advance);
     362
    357363        rc = riff_wchunk_start(riffw, CKID_gmtr, &mtrck);
    358364        if (rc != EOK)
    359365                return rc;
    360366
    361         rc = riff_write(riffw, (void *) metrics, sizeof(*metrics));
     367        rc = riff_write(riffw, (void *) &tmetrics, sizeof(tmetrics));
    362368        if (rc != EOK)
    363369                return rc;
     
    466472{
    467473        errno_t rc;
     474        tpf_glyph_ror_t tror;
    468475        riff_rchunk_t rorck;
    469476        size_t nread;
     
    473480                return rc;
    474481
    475         rc = riff_read(&rorck, (void *) &glyph->rect, sizeof(glyph->rect),
     482        rc = riff_read(&rorck, (void *) &tror, sizeof(tror),
    476483            &nread);
    477         if (rc != EOK || nread != sizeof(glyph->rect))
     484        if (rc != EOK || nread != sizeof(tror))
    478485                return EIO;
    479486
    480         rc = riff_read(&rorck, (void *) &glyph->origin, sizeof(glyph->origin),
    481             &nread);
    482         if (rc != EOK || nread != sizeof(glyph->origin))
    483                 return EIO;
    484 
    485487        rc = riff_rchunk_end(&rorck);
    486488        if (rc != EOK)
    487489                return rc;
    488490
     491        glyph->rect.p0.x = uint32_t_le2host(tror.p0x);
     492        glyph->rect.p0.y = uint32_t_le2host(tror.p0y);
     493        glyph->rect.p1.x = uint32_t_le2host(tror.p1x);
     494        glyph->rect.p1.y = uint32_t_le2host(tror.p1y);
     495        glyph->origin.x = uint32_t_le2host(tror.orig_x);
     496        glyph->origin.y = uint32_t_le2host(tror.orig_y);
    489497        return EOK;
    490498}
     
    500508{
    501509        errno_t rc;
     510        tpf_glyph_ror_t tror;
    502511        riff_wchunk_t rorck;
    503512
     513        tror.p0x = host2uint32_t_le(glyph->rect.p0.x);
     514        tror.p0y = host2uint32_t_le(glyph->rect.p0.y);
     515        tror.p1x = host2uint32_t_le(glyph->rect.p1.x);
     516        tror.p1y = host2uint32_t_le(glyph->rect.p1.y);
     517        tror.orig_x = host2uint32_t_le(glyph->origin.x);
     518        tror.orig_y = host2uint32_t_le(glyph->origin.y);
     519
    504520        rc = riff_wchunk_start(riffw, CKID_gror, &rorck);
    505521        if (rc != EOK)
    506522                return rc;
    507523
    508         rc = riff_write(riffw, (void *) &glyph->rect, sizeof(glyph->rect));
    509         if (rc != EOK)
    510                 return rc;
    511 
    512         rc = riff_write(riffw, (void *) &glyph->origin, sizeof(glyph->origin));
     524        rc = riff_write(riffw, (void *) &tror, sizeof(tror));
    513525        if (rc != EOK)
    514526                return rc;
Note: See TracChangeset for help on using the changeset viewer.