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