source: mainline/uspace/lib/draw/font/pcf.c@ cd9531d3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cd9531d3 was 09ab0a9a, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix vertical spacing with new Ccheck revision.

  • Property mode set to 100644
File size: 16.8 KB
RevLine 
[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
62typedef 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
69typedef 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
77typedef 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
85typedef 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
94typedef 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
100typedef 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
111static 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
120static 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
129static 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
134static 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
139static int16_t compressed2int(uint8_t compressed)
140{
141 int16_t ret = compressed;
142 ret -= 0x80;
143 return ret;
144}
145
[b7fd2a0]146static errno_t pcf_resolve_glyph(void *opaque_data, const wchar_t chr,
[c6c39d4f]147 glyph_id_t *glyph_id)
148{
149 pcf_data_t *data = (pcf_data_t *) opaque_data;
[a35b458]150
[c6c39d4f]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);
[a35b458]159
[c6c39d4f]160 aoff64_t entry_offset = data->encodings_table.offset +
161 (sizeof(uint32_t) + 5 * sizeof(uint16_t)) +
162 entry_index * sizeof(uint16_t);
[a35b458]163
[c6c39d4f]164 int rc = fseek(data->file, entry_offset, SEEK_SET);
165 if (rc != 0)
166 return errno;
[a35b458]167
[c6c39d4f]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;
[a35b458]172
[c6c39d4f]173 glyph = uint16_t_pcf2host(glyph, data->encodings_table.format);
[a35b458]174
[c6c39d4f]175 if (glyph == 0xffff)
176 return ENOENT;
[a35b458]177
[c6c39d4f]178 *glyph_id = glyph;
[a35b458]179
[c6c39d4f]180 return EOK;
181}
182
[b7fd2a0]183static errno_t load_glyph_metrics(pcf_data_t *data, uint32_t glyph_id,
[c6c39d4f]184 pcf_toc_entry_t *table, pcf_default_metrics_t *metrics)
185{
186 aoff64_t offset;
187 int rc;
188 size_t records_read;
[a35b458]189
[c6c39d4f]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);
[a35b458]193
[c6c39d4f]194 rc = fseek(data->file, offset, SEEK_SET);
195 if (rc != 0)
196 return errno;
[a35b458]197
[c6c39d4f]198 pcf_compressed_metrics_t compressed_metrics;
199 records_read = fread(&compressed_metrics,
[3bacee1]200 sizeof(pcf_compressed_metrics_t), 1, data->file);
[c6c39d4f]201 if (records_read != 1)
202 return EINVAL;
[a35b458]203
[c6c39d4f]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;
[3bacee1]215 } else {
[c6c39d4f]216 offset = table->offset + 2 * sizeof(uint32_t) +
217 glyph_id * sizeof(pcf_default_metrics_t);
[a35b458]218
[c6c39d4f]219 rc = fseek(data->file, offset, SEEK_SET);
220 if (rc != 0)
221 return errno;
[a35b458]222
[c6c39d4f]223 pcf_default_metrics_t uncompressed_metrics;
224 records_read = fread(&uncompressed_metrics,
[3bacee1]225 sizeof(pcf_default_metrics_t), 1, data->file);
[c6c39d4f]226 if (records_read != 1)
227 return EINVAL;
[a35b458]228
[c6c39d4f]229 metrics->left_side_bearing =
230 int16_t_pcf2host(uncompressed_metrics.left_side_bearing,
231 table->format);
232 metrics->right_side_bearing =
233 int16_t_pcf2host(uncompressed_metrics.right_side_bearing,
234 table->format);
235 metrics->character_width =
236 int16_t_pcf2host(uncompressed_metrics.character_width,
237 table->format);
238 metrics->character_ascent =
239 int16_t_pcf2host(uncompressed_metrics.character_ascent,
240 table->format);
241 metrics->character_descent =
242 int16_t_pcf2host(uncompressed_metrics.character_descent,
243 table->format);
244 metrics->character_attributes =
245 uint16_t_pcf2host(uncompressed_metrics.character_attributes,
246 table->format);
247 }
[a35b458]248
[c6c39d4f]249 return EOK;
250}
251
[b7fd2a0]252static errno_t pcf_load_glyph_surface(void *opaque_data, glyph_id_t glyph_id,
[c6c39d4f]253 surface_t **out_surface)
254{
255 pcf_data_t *data = (pcf_data_t *) opaque_data;
[a35b458]256
[c6c39d4f]257 pcf_default_metrics_t pcf_metrics;
258 memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
[b7fd2a0]259 errno_t rc = load_glyph_metrics(data, glyph_id, &data->metrics_table,
[c6c39d4f]260 &pcf_metrics);
261 if (rc != EOK)
262 return rc;
[a35b458]263
[c6c39d4f]264 aoff64_t offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
265 (glyph_id * sizeof(uint32_t));
[a35b458]266
[d5c1051]267 if (fseek(data->file, offset, SEEK_SET) < 0)
[c6c39d4f]268 return errno;
[a35b458]269
[c6c39d4f]270 uint32_t bitmap_offset = 0;
271 size_t records_read = fread(&bitmap_offset, sizeof(uint32_t), 1,
272 data->file);
273 if (records_read != 1)
274 return EINVAL;
275 bitmap_offset = uint32_t_pcf2host(bitmap_offset,
276 data->bitmap_table.format);
[a35b458]277
[c6c39d4f]278 offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
[3bacee1]279 (data->glyph_count * sizeof(uint32_t)) + (4 * sizeof(uint32_t)) +
280 bitmap_offset;
[a35b458]281
[d5c1051]282 if (fseek(data->file, offset, SEEK_SET) < 0)
[c6c39d4f]283 return errno;
[a35b458]284
[c6c39d4f]285 surface_coord_t width = pcf_metrics.character_width;
286 surface_coord_t height = pcf_metrics.character_ascent +
287 pcf_metrics.character_descent;
288 size_t row_padding_bytes = (1 << (data->bitmap_table.format & 3));
289 size_t word_size_bytes = (1 << ((data->bitmap_table.format >> 4) & 3));
290 size_t row_bytes = ALIGN_UP(ALIGN_UP(width, 8) / 8, row_padding_bytes);
291 size_t bitmap_bytes = height * row_bytes;
[a35b458]292
[c6c39d4f]293 uint8_t *bitmap = malloc(bitmap_bytes);
294 if (bitmap == NULL)
295 return ENOMEM;
[a35b458]296
[c6c39d4f]297 records_read = fread(bitmap, sizeof(uint8_t), bitmap_bytes,
298 data->file);
[a35b458]299
[c6c39d4f]300 surface_t *surface = surface_create(width, height, NULL, 0);
301 if (!surface) {
302 free(bitmap);
303 return ENOMEM;
304 }
[a35b458]305
[c6c39d4f]306 for (unsigned int y = 0; y < height; ++y) {
307 size_t row_offset = row_bytes * y;
308 for (unsigned int x = 0; x < width; ++x) {
309 size_t word_index = x / (word_size_bytes * 8);
310 size_t column_offset1 = word_index * word_size_bytes;
311 size_t byte_index_within_word =
[3bacee1]312 (x % (word_size_bytes * 8)) / 8;
[c6c39d4f]313 size_t column_offset2;
314 if (data->bitmap_table.format & PCF_FORMAT_MSBYTE_FIRST) {
315 column_offset2 = (word_size_bytes - 1) - byte_index_within_word;
[3bacee1]316 } else {
[c6c39d4f]317 column_offset2 = byte_index_within_word;
318 }
319 uint8_t b = bitmap[row_offset + column_offset1 + column_offset2];
320 bool set;
321 if (data->bitmap_table.format & PCF_FORMAT_MSBIT_FIRST) {
322 set = (b >> (7 - (x % 8))) & 1;
[3bacee1]323 } else {
[c6c39d4f]324 set = (b >> (x % 8)) & 1;
325 }
326 pixel_t p = set ? PIXEL(255, 0, 0, 0) : PIXEL(0, 0, 0, 0);
327 surface_put_pixel(surface, x, y, p);
328 }
329 }
[a35b458]330
[c6c39d4f]331 *out_surface = surface;
332 free(bitmap);
333 return EOK;
334}
335
[b7fd2a0]336static errno_t pcf_load_glyph_metrics(void *opaque_data, glyph_id_t glyph_id,
[c6c39d4f]337 glyph_metrics_t *gm)
338{
339 pcf_data_t *data = (pcf_data_t *) opaque_data;
[a35b458]340
[c6c39d4f]341 pcf_default_metrics_t pcf_metrics;
342 memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
[b7fd2a0]343 errno_t rc = load_glyph_metrics(data, glyph_id, &data->metrics_table,
[c6c39d4f]344 &pcf_metrics);
345 if (rc != EOK)
346 return rc;
[a35b458]347
[c6c39d4f]348 gm->left_side_bearing = pcf_metrics.left_side_bearing;
349 gm->width = pcf_metrics.character_width;
350 gm->right_side_bearing = pcf_metrics.right_side_bearing -
351 pcf_metrics.character_width;
352 gm->height = pcf_metrics.character_descent +
353 pcf_metrics.character_ascent;
354 gm->ascender = pcf_metrics.character_ascent;
[a35b458]355
[c6c39d4f]356 return EOK;
357}
358
359static void pcf_release(void *opaque_data)
360{
361 pcf_data_t *data = (pcf_data_t *) opaque_data;
[a35b458]362
[c6c39d4f]363 fclose(data->file);
364 free(data);
365}
366
367bitmap_font_decoder_t fd_pcf = {
368 .resolve_glyph = pcf_resolve_glyph,
369 .load_glyph_surface = pcf_load_glyph_surface,
370 .load_glyph_metrics = pcf_load_glyph_metrics,
371 .release = pcf_release
372};
373
[b7fd2a0]374static errno_t pcf_read_toc(pcf_data_t *data)
[c6c39d4f]375{
376 int rc = fseek(data->file, 0, SEEK_END);
377 if (rc != 0)
378 return errno;
[a35b458]379
[c6c39d4f]380 aoff64_t file_size = ftell(data->file);
[a35b458]381
[c6c39d4f]382 rc = fseek(data->file, 0, SEEK_SET);
383 if (rc != 0)
384 return errno;
[a35b458]385
[c6c39d4f]386 char header[4];
387 size_t records_read = fread(header, sizeof(char), 4, data->file);
388 if (records_read != 4)
389 return EINVAL;
[a35b458]390
[c6c39d4f]391 if (header[0] != 1 || header[1] != 'f' || header[2] != 'c' ||
392 header[3] != 'p')
393 return EINVAL;
[a35b458]394
[c6c39d4f]395 uint32_t table_count;
396 records_read = fread(&table_count, sizeof(uint32_t), 1,
397 data->file);
398 if (records_read != 1)
399 return EINVAL;
[a35b458]400
[c6c39d4f]401 table_count = uint32_t_le2host(table_count);
[a35b458]402
[c6c39d4f]403 bool found_bitmap_table = false;
404 bool found_metrics_table = false;
405 bool found_encodings_table = false;
406 bool found_accelerators_table = false;
[a35b458]407
[c6c39d4f]408 for (uint32_t index = 0; index < table_count; index++) {
409 pcf_toc_entry_t toc_entry;
410 records_read = fread(&toc_entry, sizeof(pcf_toc_entry_t), 1,
411 data->file);
412 toc_entry.type = uint32_t_le2host(toc_entry.type);
413 toc_entry.format = uint32_t_le2host(toc_entry.format);
414 toc_entry.size = uint32_t_le2host(toc_entry.size);
415 toc_entry.offset = uint32_t_le2host(toc_entry.offset);
[a35b458]416
[c6c39d4f]417 if (toc_entry.offset >= file_size)
418 continue;
[a35b458]419
[c6c39d4f]420 aoff64_t end = ((aoff64_t) toc_entry.offset) + ((aoff64_t) toc_entry.size);
421 if (end > file_size)
422 continue;
[a35b458]423
[c6c39d4f]424 if (toc_entry.type == PCF_TABLE_BITMAPS) {
425 if (found_bitmap_table)
426 return EINVAL;
427 found_bitmap_table = true;
428 data->bitmap_table = toc_entry;
[3bacee1]429 } else if (toc_entry.type == PCF_TABLE_METRICS) {
[c6c39d4f]430 if (found_metrics_table)
431 return EINVAL;
432 found_metrics_table = true;
433 data->metrics_table = toc_entry;
[3bacee1]434 } else if (toc_entry.type == PCF_TABLE_ENCODINGS) {
[c6c39d4f]435 if (found_encodings_table)
436 return EINVAL;
437 found_encodings_table = true;
438 data->encodings_table = toc_entry;
[3bacee1]439 } else if (toc_entry.type == PCF_TABLE_ACCELERATORS) {
[c6c39d4f]440 if (found_accelerators_table)
441 return EINVAL;
442 found_accelerators_table = true;
443 data->accelerators_table = toc_entry;
444 }
445 }
[a35b458]446
[c6c39d4f]447 if (!found_bitmap_table || !found_metrics_table ||
448 !found_encodings_table || !found_accelerators_table)
449 return EINVAL;
[a35b458]450
[c6c39d4f]451 return EOK;
452}
453
[b7fd2a0]454static errno_t pcf_seek_table_header(pcf_data_t *data, pcf_toc_entry_t *table)
[c6c39d4f]455{
456 uint32_t format;
457 int rc = fseek(data->file, table->offset, SEEK_SET);
458 if (rc != 0)
459 return errno;
[a35b458]460
[c6c39d4f]461 size_t records_read = fread(&format, sizeof(uint32_t), 1, data->file);
462 if (records_read != 1)
463 return EINVAL;
[a35b458]464
[c6c39d4f]465 format = uint32_t_le2host(format);
466 if (format != table->format)
467 return EINVAL;
[a35b458]468
[c6c39d4f]469 return EOK;
470}
471
[b7fd2a0]472static errno_t pcf_read_bitmap_table_header(pcf_data_t *data)
[c6c39d4f]473{
[b7fd2a0]474 errno_t rc = pcf_seek_table_header(data, &data->bitmap_table);
[c6c39d4f]475 if (rc != EOK)
476 return rc;
[a35b458]477
[c6c39d4f]478 if ((data->bitmap_table.format & PCF_FORMAT_MASK) != PCF_FORMAT_DEFAULT)
479 return EINVAL;
[a35b458]480
[c6c39d4f]481 uint32_t glyph_count = 0;
482 size_t records_read = fread(&glyph_count, sizeof(uint32_t), 1,
483 data->file);
484 if (records_read != 1)
485 return EINVAL;
486 glyph_count = uint32_t_pcf2host(glyph_count, data->bitmap_table.format);
487
488 data->glyph_count = glyph_count;
489 return EOK;
490}
491
[b7fd2a0]492static errno_t pcf_read_metrics_table_header(pcf_data_t *data)
[c6c39d4f]493{
[b7fd2a0]494 errno_t rc = pcf_seek_table_header(data, &data->metrics_table);
[c6c39d4f]495 if (rc != EOK)
496 return rc;
[a35b458]497
[c6c39d4f]498 size_t records_read;
499 uint32_t metrics_count;
500 if (data->metrics_table.format & PCF_FORMAT_COMPRESSED_METRICS) {
501 uint16_t metrics_count_16;
502 records_read = fread(&metrics_count_16, sizeof(uint16_t), 1,
503 data->file);
504 if (records_read != 1)
505 return EINVAL;
506 metrics_count_16 = uint16_t_pcf2host(metrics_count_16,
507 data->metrics_table.format);
508 metrics_count = metrics_count_16;
[3bacee1]509 } else {
[c6c39d4f]510 records_read = fread(&metrics_count, sizeof(uint32_t), 1,
511 data->file);
512 if (records_read != 1)
513 return EINVAL;
514 metrics_count = uint32_t_pcf2host(metrics_count,
515 data->metrics_table.format);
516 }
[a35b458]517
[c6c39d4f]518 if (metrics_count != data->glyph_count)
519 return EINVAL;
[a35b458]520
[c6c39d4f]521 return EOK;
522}
523
[b7fd2a0]524static errno_t pcf_read_encodings_table_header(pcf_data_t *data)
[c6c39d4f]525{
[b7fd2a0]526 errno_t rc = pcf_seek_table_header(data, &data->encodings_table);
[c6c39d4f]527 if (rc != EOK)
528 return rc;
[a35b458]529
[c6c39d4f]530 pcf_encoding_t encoding;
531 size_t records_read = fread(&encoding, sizeof(pcf_encoding_t), 1,
532 data->file);
533 if (records_read != 1)
534 return EINVAL;
[a35b458]535
[c6c39d4f]536 encoding.min_byte1 = uint16_t_pcf2host(encoding.min_byte1,
537 data->encodings_table.format);
538 encoding.max_byte1 = uint16_t_pcf2host(encoding.max_byte1,
539 data->encodings_table.format);
540 encoding.min_byte2 = uint16_t_pcf2host(encoding.min_byte2,
541 data->encodings_table.format);
542 encoding.max_byte2 = uint16_t_pcf2host(encoding.max_byte2,
543 data->encodings_table.format);
544 encoding.default_char = uint16_t_pcf2host(encoding.default_char,
545 data->encodings_table.format);
[a35b458]546
[c6c39d4f]547 data->encoding = encoding;
548 return EOK;
549}
550
[b7fd2a0]551static errno_t pcf_read_accelerators_table(pcf_data_t *data)
[c6c39d4f]552{
[b7fd2a0]553 errno_t rc = pcf_seek_table_header(data, &data->accelerators_table);
[c6c39d4f]554 if (rc != EOK)
555 return rc;
[a35b458]556
[c6c39d4f]557 pcf_accelerators_t accelerators;
558 size_t records_read = fread(&accelerators, sizeof(pcf_accelerators_t),
559 1, data->file);
560 if (records_read != 1)
561 return EINVAL;
[a35b458]562
[c6c39d4f]563 data->font_metrics.ascender = int32_t_pcf2host(accelerators.font_ascent,
564 data->accelerators_table.format);
565 data->font_metrics.descender = int32_t_pcf2host(accelerators.font_descent,
566 data->accelerators_table.format);
567 data->font_metrics.leading = 0;
[a35b458]568
[c6c39d4f]569 return EOK;
570}
571
[b7fd2a0]572errno_t pcf_font_create(font_t **font, char *filename, uint16_t points)
[c6c39d4f]573{
[b7fd2a0]574 errno_t rc;
[c6c39d4f]575 pcf_data_t *data = malloc(sizeof(pcf_data_t));
576 if (data == NULL)
577 return ENOMEM;
[a35b458]578
[c6c39d4f]579 data->file = fopen(filename, "rb");
580 if (data->file == NULL)
581 goto read_error;
[a35b458]582
[c6c39d4f]583 rc = pcf_read_toc(data);
584 if (rc != EOK)
585 goto error;
[a35b458]586
[c6c39d4f]587 rc = pcf_read_bitmap_table_header(data);
588 if (rc != EOK)
589 goto error;
[a35b458]590
[c6c39d4f]591 rc = pcf_read_metrics_table_header(data);
592 if (rc != EOK)
593 goto error;
[a35b458]594
[c6c39d4f]595 rc = pcf_read_encodings_table_header(data);
596 if (rc != EOK)
597 goto error;
[a35b458]598
[c6c39d4f]599 rc = pcf_read_accelerators_table(data);
600 if (rc != EOK)
601 goto error;
[a35b458]602
[c6c39d4f]603 rc = bitmap_font_create(&fd_pcf, data, data->glyph_count,
604 data->font_metrics, points, font);
605 if (rc != EOK)
606 goto error;
[a35b458]607
[c6c39d4f]608 return EOK;
609read_error:
610 rc = EINVAL;
611error:
612 if (data->file)
613 fclose(data->file);
614 free(data);
615 return rc;
616}
617
618/** @}
619 */
Note: See TracBrowser for help on using the repository browser.