Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/draw/font/pcf.c

    r5a6cc679 ra35b458  
    4242#include <offset.h>
    4343#include <stdlib.h>
     44#include <str.h>
    4445
    4546#include "pcf.h"
     
    150151{
    151152        pcf_data_t *data = (pcf_data_t *) opaque_data;
    152        
     153
    153154        /* TODO is this correct? */
    154155        uint8_t byte1 = (chr >> 8) & 0xff;
     
    159160            (byte1 - e->min_byte1) * (e->max_byte2 - e->min_byte2 + 1) +
    160161            (byte2 - e->min_byte2);
    161        
     162
    162163        aoff64_t entry_offset = data->encodings_table.offset +
    163164            (sizeof(uint32_t) + 5 * sizeof(uint16_t)) +
    164165            entry_index * sizeof(uint16_t);
    165        
     166
    166167        int rc = fseek(data->file, entry_offset, SEEK_SET);
    167168        if (rc != 0)
    168169                return errno;
    169        
     170
    170171        uint16_t glyph = 0;
    171172        size_t records_read = fread(&glyph, sizeof(uint16_t), 1, data->file);
    172173        if (records_read != 1)
    173174                return EINVAL;
    174        
     175
    175176        glyph = uint16_t_pcf2host(glyph, data->encodings_table.format);
    176        
     177
    177178        if (glyph == 0xffff)
    178179                return ENOENT;
    179        
     180
    180181        *glyph_id = glyph;
    181        
     182
    182183        return EOK;
    183184}
     
    189190        int rc;
    190191        size_t records_read;
    191        
     192
    192193        if (table->format & PCF_FORMAT_COMPRESSED_METRICS) {
    193194                offset = table->offset + sizeof(uint32_t) + sizeof(uint16_t) +
    194195                    glyph_id * sizeof(pcf_compressed_metrics_t);
    195                
     196
    196197                rc = fseek(data->file, offset, SEEK_SET);
    197198                if (rc != 0)
    198199                        return errno;
    199                
     200
    200201                pcf_compressed_metrics_t compressed_metrics;
    201202                records_read = fread(&compressed_metrics,
     
    203204                if (records_read != 1)
    204205                        return EINVAL;
    205                
     206
    206207                metrics->left_side_bearing =
    207208                    compressed2int(compressed_metrics.left_side_bearing);
     
    219220                offset = table->offset + 2 * sizeof(uint32_t) +
    220221                    glyph_id * sizeof(pcf_default_metrics_t);
    221                
     222
    222223                rc = fseek(data->file, offset, SEEK_SET);
    223224                if (rc != 0)
    224225                        return errno;
    225        
     226
    226227                pcf_default_metrics_t uncompressed_metrics;
    227228                records_read = fread(&uncompressed_metrics,
     
    229230                if (records_read != 1)
    230231                        return EINVAL;
    231                
     232
    232233                metrics->left_side_bearing =
    233234                    int16_t_pcf2host(uncompressed_metrics.left_side_bearing,
     
    249250                    table->format);
    250251        }
    251        
     252
    252253        return EOK;
    253254}
     
    257258{
    258259        pcf_data_t *data = (pcf_data_t *) opaque_data;
    259        
     260
    260261        pcf_default_metrics_t pcf_metrics;
    261262        memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
     
    264265        if (rc != EOK)
    265266                return rc;
    266        
     267
    267268        aoff64_t offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
    268269            (glyph_id * sizeof(uint32_t));
    269        
     270
    270271        if (fseek(data->file, offset, SEEK_SET) < 0)
    271272                return errno;
    272        
     273
    273274        uint32_t bitmap_offset = 0;
    274275        size_t records_read = fread(&bitmap_offset, sizeof(uint32_t), 1,
     
    278279        bitmap_offset = uint32_t_pcf2host(bitmap_offset,
    279280            data->bitmap_table.format);
    280        
     281
    281282        offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
    282283            (data->glyph_count * sizeof(uint32_t)) + (4 * sizeof(uint32_t))
    283284            + bitmap_offset;
    284        
     285
    285286        if (fseek(data->file, offset, SEEK_SET) < 0)
    286287                return errno;
    287        
     288
    288289        surface_coord_t width = pcf_metrics.character_width;
    289290        surface_coord_t height = pcf_metrics.character_ascent +
     
    293294        size_t row_bytes = ALIGN_UP(ALIGN_UP(width, 8) / 8, row_padding_bytes);
    294295        size_t bitmap_bytes = height * row_bytes;
    295        
     296
    296297        uint8_t *bitmap = malloc(bitmap_bytes);
    297298        if (bitmap == NULL)
    298299                return ENOMEM;
    299        
     300
    300301        records_read = fread(bitmap, sizeof(uint8_t), bitmap_bytes,
    301302            data->file);
    302        
     303
    303304        surface_t *surface = surface_create(width, height, NULL, 0);
    304305        if (!surface) {
     
    306307                return ENOMEM;
    307308        }
    308        
     309
    309310        for (unsigned int y = 0; y < height; ++y) {
    310311                size_t row_offset = row_bytes * y;
     
    333334                }
    334335        }
    335        
     336
    336337        *out_surface = surface;
    337338        free(bitmap);
     
    343344{
    344345        pcf_data_t *data = (pcf_data_t *) opaque_data;
    345        
     346
    346347        pcf_default_metrics_t pcf_metrics;
    347348        memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
     
    350351        if (rc != EOK)
    351352                return rc;
    352        
     353
    353354        gm->left_side_bearing = pcf_metrics.left_side_bearing;
    354355        gm->width = pcf_metrics.character_width;
     
    358359            pcf_metrics.character_ascent;
    359360        gm->ascender = pcf_metrics.character_ascent;
    360        
     361
    361362        return EOK;
    362363}
     
    365366{
    366367        pcf_data_t *data = (pcf_data_t *) opaque_data;
    367        
     368
    368369        fclose(data->file);
    369370        free(data);
     
    382383        if (rc != 0)
    383384                return errno;
    384        
     385
    385386        aoff64_t file_size = ftell(data->file);
    386        
     387
    387388        rc = fseek(data->file, 0, SEEK_SET);
    388389        if (rc != 0)
    389390                return errno;
    390        
     391
    391392        char header[4];
    392393        size_t records_read = fread(header, sizeof(char), 4, data->file);
    393394        if (records_read != 4)
    394395                return EINVAL;
    395        
     396
    396397        if (header[0] != 1 || header[1] != 'f' || header[2] != 'c' ||
    397398            header[3] != 'p')
    398399                return EINVAL;
    399        
     400
    400401        uint32_t table_count;
    401402        records_read = fread(&table_count, sizeof(uint32_t), 1,
     
    403404        if (records_read != 1)
    404405                return EINVAL;
    405        
     406
    406407        table_count = uint32_t_le2host(table_count);
    407        
     408
    408409        bool found_bitmap_table = false;
    409410        bool found_metrics_table = false;
    410411        bool found_encodings_table = false;
    411412        bool found_accelerators_table = false;
    412        
     413
    413414        for (uint32_t index = 0; index < table_count; index++) {
    414415                pcf_toc_entry_t toc_entry;
     
    419420                toc_entry.size = uint32_t_le2host(toc_entry.size);
    420421                toc_entry.offset = uint32_t_le2host(toc_entry.offset);
    421                
     422
    422423                if (toc_entry.offset >= file_size)
    423424                        continue;
    424                
     425
    425426                aoff64_t end = ((aoff64_t) toc_entry.offset) + ((aoff64_t) toc_entry.size);
    426427                if (end > file_size)
    427428                        continue;
    428                
     429
    429430                if (toc_entry.type == PCF_TABLE_BITMAPS) {
    430431                        if (found_bitmap_table)
     
    452453                }
    453454        }
    454        
     455
    455456        if (!found_bitmap_table || !found_metrics_table ||
    456457            !found_encodings_table || !found_accelerators_table)
    457458                return EINVAL;
    458        
     459
    459460        return EOK;
    460461}
     
    466467        if (rc != 0)
    467468                return errno;
    468        
     469
    469470        size_t records_read = fread(&format, sizeof(uint32_t), 1, data->file);
    470471        if (records_read != 1)
    471472                return EINVAL;
    472        
     473
    473474        format = uint32_t_le2host(format);
    474475        if (format != table->format)
    475476                return EINVAL;
    476        
     477
    477478        return EOK;
    478479}
     
    483484        if (rc != EOK)
    484485                return rc;
    485        
     486
    486487        if ((data->bitmap_table.format & PCF_FORMAT_MASK) != PCF_FORMAT_DEFAULT)
    487488                return EINVAL;
    488        
     489
    489490        uint32_t glyph_count = 0;
    490491        size_t records_read = fread(&glyph_count, sizeof(uint32_t), 1,
     
    503504        if (rc != EOK)
    504505                return rc;
    505        
     506
    506507        size_t records_read;
    507508        uint32_t metrics_count;
     
    524525                    data->metrics_table.format);
    525526        }
    526        
     527
    527528        if (metrics_count != data->glyph_count)
    528529                return EINVAL;
    529        
     530
    530531        return EOK;
    531532}
     
    536537        if (rc != EOK)
    537538                return rc;
    538        
     539
    539540        pcf_encoding_t encoding;
    540541        size_t records_read = fread(&encoding, sizeof(pcf_encoding_t), 1,
     
    542543        if (records_read != 1)
    543544                return EINVAL;
    544        
     545
    545546        encoding.min_byte1 = uint16_t_pcf2host(encoding.min_byte1,
    546547            data->encodings_table.format);
     
    553554        encoding.default_char = uint16_t_pcf2host(encoding.default_char,
    554555            data->encodings_table.format);
    555        
     556
    556557        data->encoding = encoding;
    557558        return EOK;
     
    563564        if (rc != EOK)
    564565                return rc;
    565        
     566
    566567        pcf_accelerators_t accelerators;
    567568        size_t records_read = fread(&accelerators, sizeof(pcf_accelerators_t),
     
    569570        if (records_read != 1)
    570571                return EINVAL;
    571        
     572
    572573        data->font_metrics.ascender = int32_t_pcf2host(accelerators.font_ascent,
    573574            data->accelerators_table.format);
     
    575576            data->accelerators_table.format);
    576577        data->font_metrics.leading = 0;
    577        
     578
    578579        return EOK;
    579580}
     
    585586        if (data == NULL)
    586587                return ENOMEM;
    587        
     588
    588589        data->file = fopen(filename, "rb");
    589590        if (data->file == NULL)
    590591                goto read_error;
    591        
     592
    592593        rc = pcf_read_toc(data);
    593594        if (rc != EOK)
    594595                goto error;
    595        
     596
    596597        rc = pcf_read_bitmap_table_header(data);
    597598        if (rc != EOK)
    598599                goto error;
    599        
     600
    600601        rc = pcf_read_metrics_table_header(data);
    601602        if (rc != EOK)
    602603                goto error;
    603        
     604
    604605        rc = pcf_read_encodings_table_header(data);
    605606        if (rc != EOK)
    606607                goto error;
    607        
     608
    608609        rc = pcf_read_accelerators_table(data);
    609610        if (rc != EOK)
    610611                goto error;
    611        
     612
    612613        rc = bitmap_font_create(&fd_pcf, data, data->glyph_count,
    613614            data->font_metrics, points, font);
    614615        if (rc != EOK)
    615616                goto error;
    616        
     617
    617618        return EOK;
    618619read_error:
Note: See TracChangeset for help on using the changeset viewer.