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