| 1 | /*
|
|---|
| 2 | * Copyright (c) 2014 Martin Sucha
|
|---|
| 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 draw
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <stddef.h>
|
|---|
| 37 | #include <stdint.h>
|
|---|
| 38 | #include <malloc.h>
|
|---|
| 39 | #include <errno.h>
|
|---|
| 40 | #include <byteorder.h>
|
|---|
| 41 | #include <stdio.h>
|
|---|
| 42 | #include <align.h>
|
|---|
| 43 | #include <offset.h>
|
|---|
| 44 |
|
|---|
| 45 | #include "pcf.h"
|
|---|
| 46 | #include "../drawctx.h"
|
|---|
| 47 | #include "bitmap_backend.h"
|
|---|
| 48 |
|
|---|
| 49 | #define PCF_TABLE_ACCELERATORS 0x02
|
|---|
| 50 | #define PCF_TABLE_METRICS 0x04
|
|---|
| 51 | #define PCF_TABLE_BITMAPS 0x08
|
|---|
| 52 | #define PCF_TABLE_INK_METRICS 0x10
|
|---|
| 53 | #define PCF_TABLE_ENCODINGS 0x20
|
|---|
| 54 |
|
|---|
| 55 | #define PCF_FORMAT_DEFAULT 0x00000000
|
|---|
| 56 | #define PCF_FORMAT_MASK 0xffffff00
|
|---|
| 57 | #define PCF_FORMAT_MSBYTE_FIRST 0x00000004
|
|---|
| 58 | #define PCF_FORMAT_MSBIT_FIRST 0x00000008
|
|---|
| 59 | #define PCF_FORMAT_COMPRESSED_METRICS 0x00000100
|
|---|
| 60 |
|
|---|
| 61 | typedef struct {
|
|---|
| 62 | uint32_t type;
|
|---|
| 63 | uint32_t format;
|
|---|
| 64 | uint32_t size; /* in bytes */
|
|---|
| 65 | uint32_t offset; /* in bytes from beginning of file */
|
|---|
| 66 | } __attribute__((__packed__)) pcf_toc_entry_t;
|
|---|
| 67 |
|
|---|
| 68 | typedef struct {
|
|---|
| 69 | uint16_t min_byte2;
|
|---|
| 70 | uint16_t max_byte2;
|
|---|
| 71 | uint16_t min_byte1;
|
|---|
| 72 | uint16_t max_byte1;
|
|---|
| 73 | uint16_t default_char;
|
|---|
| 74 | } __attribute__((__packed__)) pcf_encoding_t;
|
|---|
| 75 |
|
|---|
| 76 | typedef struct {
|
|---|
| 77 | uint8_t left_side_bearing;
|
|---|
| 78 | uint8_t right_side_bearing;
|
|---|
| 79 | uint8_t character_width;
|
|---|
| 80 | uint8_t character_ascent;
|
|---|
| 81 | uint8_t character_descent;
|
|---|
| 82 | } __attribute__((__packed__)) pcf_compressed_metrics_t;
|
|---|
| 83 |
|
|---|
| 84 | typedef struct {
|
|---|
| 85 | int16_t left_side_bearing;
|
|---|
| 86 | int16_t right_side_bearing;
|
|---|
| 87 | int16_t character_width;
|
|---|
| 88 | int16_t character_ascent;
|
|---|
| 89 | int16_t character_descent;
|
|---|
| 90 | uint16_t character_attributes;
|
|---|
| 91 | } __attribute__((__packed__)) pcf_default_metrics_t;
|
|---|
| 92 |
|
|---|
| 93 | typedef struct {
|
|---|
| 94 | uint8_t unused_font_information[8];
|
|---|
| 95 | int32_t font_ascent;
|
|---|
| 96 | int32_t font_descent;
|
|---|
| 97 | } __attribute__((__packed__)) pcf_accelerators_t;
|
|---|
| 98 |
|
|---|
| 99 | typedef struct {
|
|---|
| 100 | FILE *file;
|
|---|
| 101 | uint32_t glyph_count;
|
|---|
| 102 | pcf_toc_entry_t bitmap_table;
|
|---|
| 103 | pcf_toc_entry_t metrics_table;
|
|---|
| 104 | pcf_toc_entry_t encodings_table;
|
|---|
| 105 | pcf_toc_entry_t accelerators_table;
|
|---|
| 106 | pcf_encoding_t encoding;
|
|---|
| 107 | font_metrics_t font_metrics;
|
|---|
| 108 | } pcf_data_t;
|
|---|
| 109 |
|
|---|
| 110 | static inline uint32_t uint32_t_pcf2host(uint32_t val, uint32_t format)
|
|---|
| 111 | {
|
|---|
| 112 | if (format & PCF_FORMAT_MSBYTE_FIRST) {
|
|---|
| 113 | return uint32_t_be2host(val);
|
|---|
| 114 | }
|
|---|
| 115 | else {
|
|---|
| 116 | return uint32_t_le2host(val);
|
|---|
| 117 | }
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static inline uint16_t uint16_t_pcf2host(uint16_t val, uint32_t format)
|
|---|
| 121 | {
|
|---|
| 122 | if (format & PCF_FORMAT_MSBYTE_FIRST) {
|
|---|
| 123 | return uint16_t_be2host(val);
|
|---|
| 124 | }
|
|---|
| 125 | else {
|
|---|
| 126 | return uint16_t_le2host(val);
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | static inline int16_t int16_t_pcf2host(int16_t val, uint32_t format)
|
|---|
| 131 | {
|
|---|
| 132 | return (int16_t) uint16_t_pcf2host((uint16_t) val, format);
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | static inline int32_t int32_t_pcf2host(int32_t val, uint32_t format)
|
|---|
| 136 | {
|
|---|
| 137 | return (int32_t) uint32_t_pcf2host((uint32_t) val, format);
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 | static int16_t compressed2int(uint8_t compressed)
|
|---|
| 142 | {
|
|---|
| 143 | int16_t ret = compressed;
|
|---|
| 144 | ret -= 0x80;
|
|---|
| 145 | return ret;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | static int pcf_resolve_glyph(void *opaque_data, const wchar_t chr,
|
|---|
| 149 | glyph_id_t *glyph_id)
|
|---|
| 150 | {
|
|---|
| 151 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
|---|
| 152 |
|
|---|
| 153 | /* TODO is this correct? */
|
|---|
| 154 | uint8_t byte1 = (chr >> 8) & 0xff;
|
|---|
| 155 | uint8_t byte2 = chr & 0xff;
|
|---|
| 156 | pcf_encoding_t *e = &data->encoding;
|
|---|
| 157 |
|
|---|
| 158 | aoff64_t entry_index =
|
|---|
| 159 | (byte1 - e->min_byte1) * (e->max_byte2 - e->min_byte2 + 1) +
|
|---|
| 160 | (byte2 - e->min_byte2);
|
|---|
| 161 |
|
|---|
| 162 | aoff64_t entry_offset = data->encodings_table.offset +
|
|---|
| 163 | (sizeof(uint32_t) + 5 * sizeof(uint16_t)) +
|
|---|
| 164 | entry_index * sizeof(uint16_t);
|
|---|
| 165 |
|
|---|
| 166 | int rc = fseek(data->file, entry_offset, SEEK_SET);
|
|---|
| 167 | if (rc != 0)
|
|---|
| 168 | return errno;
|
|---|
| 169 |
|
|---|
| 170 | uint16_t glyph = 0;
|
|---|
| 171 | size_t records_read = fread(&glyph, sizeof(uint16_t), 1, data->file);
|
|---|
| 172 | if (records_read != 1)
|
|---|
| 173 | return EINVAL;
|
|---|
| 174 |
|
|---|
| 175 | glyph = uint16_t_pcf2host(glyph, data->encodings_table.format);
|
|---|
| 176 |
|
|---|
| 177 | if (glyph == 0xffff)
|
|---|
| 178 | return ENOENT;
|
|---|
| 179 |
|
|---|
| 180 | *glyph_id = glyph;
|
|---|
| 181 |
|
|---|
| 182 | return EOK;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | static int load_glyph_metrics(pcf_data_t *data, uint32_t glyph_id,
|
|---|
| 186 | pcf_toc_entry_t *table, pcf_default_metrics_t *metrics)
|
|---|
| 187 | {
|
|---|
| 188 | aoff64_t offset;
|
|---|
| 189 | int rc;
|
|---|
| 190 | size_t records_read;
|
|---|
| 191 |
|
|---|
| 192 | if (table->format & PCF_FORMAT_COMPRESSED_METRICS) {
|
|---|
| 193 | offset = table->offset + sizeof(uint32_t) + sizeof(uint16_t) +
|
|---|
| 194 | glyph_id * sizeof(pcf_compressed_metrics_t);
|
|---|
| 195 |
|
|---|
| 196 | rc = fseek(data->file, offset, SEEK_SET);
|
|---|
| 197 | if (rc != 0)
|
|---|
| 198 | return errno;
|
|---|
| 199 |
|
|---|
| 200 | pcf_compressed_metrics_t compressed_metrics;
|
|---|
| 201 | records_read = fread(&compressed_metrics,
|
|---|
| 202 | sizeof(pcf_compressed_metrics_t), 1,data->file);
|
|---|
| 203 | if (records_read != 1)
|
|---|
| 204 | return EINVAL;
|
|---|
| 205 |
|
|---|
| 206 | metrics->left_side_bearing =
|
|---|
| 207 | compressed2int(compressed_metrics.left_side_bearing);
|
|---|
| 208 | metrics->right_side_bearing =
|
|---|
| 209 | compressed2int(compressed_metrics.right_side_bearing);
|
|---|
| 210 | metrics->character_width =
|
|---|
| 211 | compressed2int(compressed_metrics.character_width);
|
|---|
| 212 | metrics->character_ascent =
|
|---|
| 213 | compressed2int(compressed_metrics.character_ascent);
|
|---|
| 214 | metrics->character_descent =
|
|---|
| 215 | compressed2int(compressed_metrics.character_descent);
|
|---|
| 216 | metrics->character_attributes = 0;
|
|---|
| 217 | }
|
|---|
| 218 | else {
|
|---|
| 219 | offset = table->offset + 2 * sizeof(uint32_t) +
|
|---|
| 220 | glyph_id * sizeof(pcf_default_metrics_t);
|
|---|
| 221 |
|
|---|
| 222 | rc = fseek(data->file, offset, SEEK_SET);
|
|---|
| 223 | if (rc != 0)
|
|---|
| 224 | return errno;
|
|---|
| 225 |
|
|---|
| 226 | pcf_default_metrics_t uncompressed_metrics;
|
|---|
| 227 | records_read = fread(&uncompressed_metrics,
|
|---|
| 228 | sizeof(pcf_default_metrics_t), 1,data->file);
|
|---|
| 229 | if (records_read != 1)
|
|---|
| 230 | return EINVAL;
|
|---|
| 231 |
|
|---|
| 232 | metrics->left_side_bearing =
|
|---|
| 233 | int16_t_pcf2host(uncompressed_metrics.left_side_bearing,
|
|---|
| 234 | table->format);
|
|---|
| 235 | metrics->right_side_bearing =
|
|---|
| 236 | int16_t_pcf2host(uncompressed_metrics.right_side_bearing,
|
|---|
| 237 | table->format);
|
|---|
| 238 | metrics->character_width =
|
|---|
| 239 | int16_t_pcf2host(uncompressed_metrics.character_width,
|
|---|
| 240 | table->format);
|
|---|
| 241 | metrics->character_ascent =
|
|---|
| 242 | int16_t_pcf2host(uncompressed_metrics.character_ascent,
|
|---|
| 243 | table->format);
|
|---|
| 244 | metrics->character_descent =
|
|---|
| 245 | int16_t_pcf2host(uncompressed_metrics.character_descent,
|
|---|
| 246 | table->format);
|
|---|
| 247 | metrics->character_attributes =
|
|---|
| 248 | uint16_t_pcf2host(uncompressed_metrics.character_attributes,
|
|---|
| 249 | table->format);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | return EOK;
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | static int pcf_load_glyph_surface(void *opaque_data, glyph_id_t glyph_id,
|
|---|
| 256 | surface_t **out_surface)
|
|---|
| 257 | {
|
|---|
| 258 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
|---|
| 259 |
|
|---|
| 260 | pcf_default_metrics_t pcf_metrics;
|
|---|
| 261 | memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
|
|---|
| 262 | int rc = load_glyph_metrics(data, glyph_id, &data->metrics_table,
|
|---|
| 263 | &pcf_metrics);
|
|---|
| 264 | if (rc != EOK)
|
|---|
| 265 | return rc;
|
|---|
| 266 |
|
|---|
| 267 | aoff64_t offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
|
|---|
| 268 | (glyph_id * sizeof(uint32_t));
|
|---|
| 269 |
|
|---|
| 270 | rc = fseek(data->file, offset, SEEK_SET);
|
|---|
| 271 | if (rc != 0)
|
|---|
| 272 | return errno;
|
|---|
| 273 |
|
|---|
| 274 | uint32_t bitmap_offset = 0;
|
|---|
| 275 | size_t records_read = fread(&bitmap_offset, sizeof(uint32_t), 1,
|
|---|
| 276 | data->file);
|
|---|
| 277 | if (records_read != 1)
|
|---|
| 278 | return EINVAL;
|
|---|
| 279 | bitmap_offset = uint32_t_pcf2host(bitmap_offset,
|
|---|
| 280 | data->bitmap_table.format);
|
|---|
| 281 |
|
|---|
| 282 | offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
|
|---|
| 283 | (data->glyph_count * sizeof(uint32_t)) + (4 * sizeof(uint32_t))
|
|---|
| 284 | + bitmap_offset;
|
|---|
| 285 |
|
|---|
| 286 | rc = fseek(data->file, offset, SEEK_SET);
|
|---|
| 287 | if (rc != 0)
|
|---|
| 288 | return errno;
|
|---|
| 289 |
|
|---|
| 290 | surface_coord_t width = pcf_metrics.character_width;
|
|---|
| 291 | surface_coord_t height = pcf_metrics.character_ascent +
|
|---|
| 292 | pcf_metrics.character_descent;
|
|---|
| 293 | size_t row_padding_bytes = (1 << (data->bitmap_table.format & 3));
|
|---|
| 294 | size_t word_size_bytes = (1 << ((data->bitmap_table.format >> 4) & 3));
|
|---|
| 295 | size_t row_bytes = ALIGN_UP(ALIGN_UP(width, 8) / 8, row_padding_bytes);
|
|---|
| 296 | size_t bitmap_bytes = height * row_bytes;
|
|---|
| 297 |
|
|---|
| 298 | uint8_t *bitmap = malloc(bitmap_bytes);
|
|---|
| 299 | if (bitmap == NULL)
|
|---|
| 300 | return ENOMEM;
|
|---|
| 301 |
|
|---|
| 302 | records_read = fread(bitmap, sizeof(uint8_t), bitmap_bytes,
|
|---|
| 303 | data->file);
|
|---|
| 304 |
|
|---|
| 305 | surface_t *surface = surface_create(width, height, NULL, 0);
|
|---|
| 306 | if (!surface) {
|
|---|
| 307 | free(bitmap);
|
|---|
| 308 | return ENOMEM;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | for (unsigned int y = 0; y < height; ++y) {
|
|---|
| 312 | size_t row_offset = row_bytes * y;
|
|---|
| 313 | for (unsigned int x = 0; x < width; ++x) {
|
|---|
| 314 | size_t word_index = x / (word_size_bytes * 8);
|
|---|
| 315 | size_t column_offset1 = word_index * word_size_bytes;
|
|---|
| 316 | size_t byte_index_within_word =
|
|---|
| 317 | (x % (word_size_bytes * 8)) / 8;
|
|---|
| 318 | size_t column_offset2;
|
|---|
| 319 | if (data->bitmap_table.format & PCF_FORMAT_MSBYTE_FIRST) {
|
|---|
| 320 | column_offset2 = (word_size_bytes - 1) - byte_index_within_word;
|
|---|
| 321 | }
|
|---|
| 322 | else {
|
|---|
| 323 | column_offset2 = byte_index_within_word;
|
|---|
| 324 | }
|
|---|
| 325 | uint8_t b = bitmap[row_offset + column_offset1 + column_offset2];
|
|---|
| 326 | bool set;
|
|---|
| 327 | if (data->bitmap_table.format & PCF_FORMAT_MSBIT_FIRST) {
|
|---|
| 328 | set = (b >> (7 - (x % 8))) & 1;
|
|---|
| 329 | }
|
|---|
| 330 | else {
|
|---|
| 331 | set = (b >> (x % 8)) & 1;
|
|---|
| 332 | }
|
|---|
| 333 | pixel_t p = set ? PIXEL(255, 0, 0, 0) : PIXEL(0, 0, 0, 0);
|
|---|
| 334 | surface_put_pixel(surface, x, y, p);
|
|---|
| 335 | }
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | *out_surface = surface;
|
|---|
| 339 | free(bitmap);
|
|---|
| 340 | return EOK;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | static int pcf_load_glyph_metrics(void *opaque_data, glyph_id_t glyph_id,
|
|---|
| 344 | glyph_metrics_t *gm)
|
|---|
| 345 | {
|
|---|
| 346 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
|---|
| 347 |
|
|---|
| 348 | pcf_default_metrics_t pcf_metrics;
|
|---|
| 349 | memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
|
|---|
| 350 | int rc = load_glyph_metrics(data, glyph_id, &data->metrics_table,
|
|---|
| 351 | &pcf_metrics);
|
|---|
| 352 | if (rc != EOK)
|
|---|
| 353 | return rc;
|
|---|
| 354 |
|
|---|
| 355 | gm->left_side_bearing = pcf_metrics.left_side_bearing;
|
|---|
| 356 | gm->width = pcf_metrics.character_width;
|
|---|
| 357 | gm->right_side_bearing = pcf_metrics.right_side_bearing -
|
|---|
| 358 | pcf_metrics.character_width;
|
|---|
| 359 | gm->height = pcf_metrics.character_descent +
|
|---|
| 360 | pcf_metrics.character_ascent;
|
|---|
| 361 | gm->ascender = pcf_metrics.character_ascent;
|
|---|
| 362 |
|
|---|
| 363 | return EOK;
|
|---|
| 364 | }
|
|---|
| 365 |
|
|---|
| 366 | static void pcf_release(void *opaque_data)
|
|---|
| 367 | {
|
|---|
| 368 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
|---|
| 369 |
|
|---|
| 370 | fclose(data->file);
|
|---|
| 371 | free(data);
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | bitmap_font_decoder_t fd_pcf = {
|
|---|
| 375 | .resolve_glyph = pcf_resolve_glyph,
|
|---|
| 376 | .load_glyph_surface = pcf_load_glyph_surface,
|
|---|
| 377 | .load_glyph_metrics = pcf_load_glyph_metrics,
|
|---|
| 378 | .release = pcf_release
|
|---|
| 379 | };
|
|---|
| 380 |
|
|---|
| 381 | static int pcf_read_toc(pcf_data_t *data)
|
|---|
| 382 | {
|
|---|
| 383 | int rc = fseek(data->file, 0, SEEK_END);
|
|---|
| 384 | if (rc != 0)
|
|---|
| 385 | return errno;
|
|---|
| 386 |
|
|---|
| 387 | aoff64_t file_size = ftell(data->file);
|
|---|
| 388 |
|
|---|
| 389 | rc = fseek(data->file, 0, SEEK_SET);
|
|---|
| 390 | if (rc != 0)
|
|---|
| 391 | return errno;
|
|---|
| 392 |
|
|---|
| 393 | char header[4];
|
|---|
| 394 | size_t records_read = fread(header, sizeof(char), 4, data->file);
|
|---|
| 395 | if (records_read != 4)
|
|---|
| 396 | return EINVAL;
|
|---|
| 397 |
|
|---|
| 398 | if (header[0] != 1 || header[1] != 'f' || header[2] != 'c' ||
|
|---|
| 399 | header[3] != 'p')
|
|---|
| 400 | return EINVAL;
|
|---|
| 401 |
|
|---|
| 402 | uint32_t table_count;
|
|---|
| 403 | records_read = fread(&table_count, sizeof(uint32_t), 1,
|
|---|
| 404 | data->file);
|
|---|
| 405 | if (records_read != 1)
|
|---|
| 406 | return EINVAL;
|
|---|
| 407 |
|
|---|
| 408 | table_count = uint32_t_le2host(table_count);
|
|---|
| 409 |
|
|---|
| 410 | bool found_bitmap_table = false;
|
|---|
| 411 | bool found_metrics_table = false;
|
|---|
| 412 | bool found_encodings_table = false;
|
|---|
| 413 | bool found_accelerators_table = false;
|
|---|
| 414 |
|
|---|
| 415 | for (uint32_t index = 0; index < table_count; index++) {
|
|---|
| 416 | pcf_toc_entry_t toc_entry;
|
|---|
| 417 | records_read = fread(&toc_entry, sizeof(pcf_toc_entry_t), 1,
|
|---|
| 418 | data->file);
|
|---|
| 419 | toc_entry.type = uint32_t_le2host(toc_entry.type);
|
|---|
| 420 | toc_entry.format = uint32_t_le2host(toc_entry.format);
|
|---|
| 421 | toc_entry.size = uint32_t_le2host(toc_entry.size);
|
|---|
| 422 | toc_entry.offset = uint32_t_le2host(toc_entry.offset);
|
|---|
| 423 |
|
|---|
| 424 | if (toc_entry.offset >= file_size)
|
|---|
| 425 | continue;
|
|---|
| 426 |
|
|---|
| 427 | aoff64_t end = ((aoff64_t) toc_entry.offset) + ((aoff64_t) toc_entry.size);
|
|---|
| 428 | if (end > file_size)
|
|---|
| 429 | continue;
|
|---|
| 430 |
|
|---|
| 431 | if (toc_entry.type == PCF_TABLE_BITMAPS) {
|
|---|
| 432 | if (found_bitmap_table)
|
|---|
| 433 | return EINVAL;
|
|---|
| 434 | found_bitmap_table = true;
|
|---|
| 435 | data->bitmap_table = toc_entry;
|
|---|
| 436 | }
|
|---|
| 437 | else if (toc_entry.type == PCF_TABLE_METRICS) {
|
|---|
| 438 | if (found_metrics_table)
|
|---|
| 439 | return EINVAL;
|
|---|
| 440 | found_metrics_table = true;
|
|---|
| 441 | data->metrics_table = toc_entry;
|
|---|
| 442 | }
|
|---|
| 443 | else if (toc_entry.type == PCF_TABLE_ENCODINGS) {
|
|---|
| 444 | if (found_encodings_table)
|
|---|
| 445 | return EINVAL;
|
|---|
| 446 | found_encodings_table = true;
|
|---|
| 447 | data->encodings_table = toc_entry;
|
|---|
| 448 | }
|
|---|
| 449 | else if (toc_entry.type == PCF_TABLE_ACCELERATORS) {
|
|---|
| 450 | if (found_accelerators_table)
|
|---|
| 451 | return EINVAL;
|
|---|
| 452 | found_accelerators_table = true;
|
|---|
| 453 | data->accelerators_table = toc_entry;
|
|---|
| 454 | }
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | if (!found_bitmap_table || !found_metrics_table ||
|
|---|
| 458 | !found_encodings_table || !found_accelerators_table)
|
|---|
| 459 | return EINVAL;
|
|---|
| 460 |
|
|---|
| 461 | return EOK;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | static int pcf_seek_table_header(pcf_data_t *data, pcf_toc_entry_t *table)
|
|---|
| 465 | {
|
|---|
| 466 | uint32_t format;
|
|---|
| 467 | int rc = fseek(data->file, table->offset, SEEK_SET);
|
|---|
| 468 | if (rc != 0)
|
|---|
| 469 | return errno;
|
|---|
| 470 |
|
|---|
| 471 | size_t records_read = fread(&format, sizeof(uint32_t), 1, data->file);
|
|---|
| 472 | if (records_read != 1)
|
|---|
| 473 | return EINVAL;
|
|---|
| 474 |
|
|---|
| 475 | format = uint32_t_le2host(format);
|
|---|
| 476 | if (format != table->format)
|
|---|
| 477 | return EINVAL;
|
|---|
| 478 |
|
|---|
| 479 | return EOK;
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | static int pcf_read_bitmap_table_header(pcf_data_t *data)
|
|---|
| 483 | {
|
|---|
| 484 | int rc = pcf_seek_table_header(data, &data->bitmap_table);
|
|---|
| 485 | if (rc != EOK)
|
|---|
| 486 | return rc;
|
|---|
| 487 |
|
|---|
| 488 | if ((data->bitmap_table.format & PCF_FORMAT_MASK) != PCF_FORMAT_DEFAULT)
|
|---|
| 489 | return EINVAL;
|
|---|
| 490 |
|
|---|
| 491 | uint32_t glyph_count = 0;
|
|---|
| 492 | size_t records_read = fread(&glyph_count, sizeof(uint32_t), 1,
|
|---|
| 493 | data->file);
|
|---|
| 494 | if (records_read != 1)
|
|---|
| 495 | return EINVAL;
|
|---|
| 496 | glyph_count = uint32_t_pcf2host(glyph_count, data->bitmap_table.format);
|
|---|
| 497 |
|
|---|
| 498 | data->glyph_count = glyph_count;
|
|---|
| 499 | return EOK;
|
|---|
| 500 | }
|
|---|
| 501 |
|
|---|
| 502 | static int pcf_read_metrics_table_header(pcf_data_t *data)
|
|---|
| 503 | {
|
|---|
| 504 | int rc = pcf_seek_table_header(data, &data->metrics_table);
|
|---|
| 505 | if (rc != EOK)
|
|---|
| 506 | return rc;
|
|---|
| 507 |
|
|---|
| 508 | size_t records_read;
|
|---|
| 509 | uint32_t metrics_count;
|
|---|
| 510 | if (data->metrics_table.format & PCF_FORMAT_COMPRESSED_METRICS) {
|
|---|
| 511 | uint16_t metrics_count_16;
|
|---|
| 512 | records_read = fread(&metrics_count_16, sizeof(uint16_t), 1,
|
|---|
| 513 | data->file);
|
|---|
| 514 | if (records_read != 1)
|
|---|
| 515 | return EINVAL;
|
|---|
| 516 | metrics_count_16 = uint16_t_pcf2host(metrics_count_16,
|
|---|
| 517 | data->metrics_table.format);
|
|---|
| 518 | metrics_count = metrics_count_16;
|
|---|
| 519 | }
|
|---|
| 520 | else {
|
|---|
| 521 | records_read = fread(&metrics_count, sizeof(uint32_t), 1,
|
|---|
| 522 | data->file);
|
|---|
| 523 | if (records_read != 1)
|
|---|
| 524 | return EINVAL;
|
|---|
| 525 | metrics_count = uint32_t_pcf2host(metrics_count,
|
|---|
| 526 | data->metrics_table.format);
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | if (metrics_count != data->glyph_count)
|
|---|
| 530 | return EINVAL;
|
|---|
| 531 |
|
|---|
| 532 | return EOK;
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | static int pcf_read_encodings_table_header(pcf_data_t *data)
|
|---|
| 536 | {
|
|---|
| 537 | int rc = pcf_seek_table_header(data, &data->encodings_table);
|
|---|
| 538 | if (rc != EOK)
|
|---|
| 539 | return rc;
|
|---|
| 540 |
|
|---|
| 541 | pcf_encoding_t encoding;
|
|---|
| 542 | size_t records_read = fread(&encoding, sizeof(pcf_encoding_t), 1,
|
|---|
| 543 | data->file);
|
|---|
| 544 | if (records_read != 1)
|
|---|
| 545 | return EINVAL;
|
|---|
| 546 |
|
|---|
| 547 | encoding.min_byte1 = uint16_t_pcf2host(encoding.min_byte1,
|
|---|
| 548 | data->encodings_table.format);
|
|---|
| 549 | encoding.max_byte1 = uint16_t_pcf2host(encoding.max_byte1,
|
|---|
| 550 | data->encodings_table.format);
|
|---|
| 551 | encoding.min_byte2 = uint16_t_pcf2host(encoding.min_byte2,
|
|---|
| 552 | data->encodings_table.format);
|
|---|
| 553 | encoding.max_byte2 = uint16_t_pcf2host(encoding.max_byte2,
|
|---|
| 554 | data->encodings_table.format);
|
|---|
| 555 | encoding.default_char = uint16_t_pcf2host(encoding.default_char,
|
|---|
| 556 | data->encodings_table.format);
|
|---|
| 557 |
|
|---|
| 558 | data->encoding = encoding;
|
|---|
| 559 | return EOK;
|
|---|
| 560 | }
|
|---|
| 561 |
|
|---|
| 562 | static int pcf_read_accelerators_table(pcf_data_t *data)
|
|---|
| 563 | {
|
|---|
| 564 | int rc = pcf_seek_table_header(data, &data->accelerators_table);
|
|---|
| 565 | if (rc != EOK)
|
|---|
| 566 | return rc;
|
|---|
| 567 |
|
|---|
| 568 | pcf_accelerators_t accelerators;
|
|---|
| 569 | size_t records_read = fread(&accelerators, sizeof(pcf_accelerators_t),
|
|---|
| 570 | 1, data->file);
|
|---|
| 571 | if (records_read != 1)
|
|---|
| 572 | return EINVAL;
|
|---|
| 573 |
|
|---|
| 574 | data->font_metrics.ascender = int32_t_pcf2host(accelerators.font_ascent,
|
|---|
| 575 | data->accelerators_table.format);
|
|---|
| 576 | data->font_metrics.descender = int32_t_pcf2host(accelerators.font_descent,
|
|---|
| 577 | data->accelerators_table.format);
|
|---|
| 578 | data->font_metrics.leading = 0;
|
|---|
| 579 |
|
|---|
| 580 | return EOK;
|
|---|
| 581 | }
|
|---|
| 582 |
|
|---|
| 583 | int pcf_font_create(font_t **font, char *filename, uint16_t points)
|
|---|
| 584 | {
|
|---|
| 585 | int rc;
|
|---|
| 586 | pcf_data_t *data = malloc(sizeof(pcf_data_t));
|
|---|
| 587 | if (data == NULL)
|
|---|
| 588 | return ENOMEM;
|
|---|
| 589 |
|
|---|
| 590 | data->file = fopen(filename, "rb");
|
|---|
| 591 | if (data->file == NULL)
|
|---|
| 592 | goto read_error;
|
|---|
| 593 |
|
|---|
| 594 | rc = pcf_read_toc(data);
|
|---|
| 595 | if (rc != EOK)
|
|---|
| 596 | goto error;
|
|---|
| 597 |
|
|---|
| 598 | rc = pcf_read_bitmap_table_header(data);
|
|---|
| 599 | if (rc != EOK)
|
|---|
| 600 | goto error;
|
|---|
| 601 |
|
|---|
| 602 | rc = pcf_read_metrics_table_header(data);
|
|---|
| 603 | if (rc != EOK)
|
|---|
| 604 | goto error;
|
|---|
| 605 |
|
|---|
| 606 | rc = pcf_read_encodings_table_header(data);
|
|---|
| 607 | if (rc != EOK)
|
|---|
| 608 | goto error;
|
|---|
| 609 |
|
|---|
| 610 | rc = pcf_read_accelerators_table(data);
|
|---|
| 611 | if (rc != EOK)
|
|---|
| 612 | goto error;
|
|---|
| 613 |
|
|---|
| 614 | rc = bitmap_font_create(&fd_pcf, data, data->glyph_count,
|
|---|
| 615 | data->font_metrics, points, font);
|
|---|
| 616 | if (rc != EOK)
|
|---|
| 617 | goto error;
|
|---|
| 618 |
|
|---|
| 619 | return EOK;
|
|---|
| 620 | read_error:
|
|---|
| 621 | rc = EINVAL;
|
|---|
| 622 | error:
|
|---|
| 623 | if (data->file)
|
|---|
| 624 | fclose(data->file);
|
|---|
| 625 | free(data);
|
|---|
| 626 | return rc;
|
|---|
| 627 | }
|
|---|
| 628 |
|
|---|
| 629 | /** @}
|
|---|
| 630 | */
|
|---|