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