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 | } else {
|
---|
116 | return uint32_t_le2host(val);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | static inline uint16_t uint16_t_pcf2host(uint16_t val, uint32_t format)
|
---|
121 | {
|
---|
122 | if (format & PCF_FORMAT_MSBYTE_FIRST) {
|
---|
123 | return uint16_t_be2host(val);
|
---|
124 | } else {
|
---|
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 |
|
---|
147 | static errno_t pcf_resolve_glyph(void *opaque_data, const wchar_t chr,
|
---|
148 | glyph_id_t *glyph_id)
|
---|
149 | {
|
---|
150 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
---|
151 |
|
---|
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);
|
---|
160 |
|
---|
161 | aoff64_t entry_offset = data->encodings_table.offset +
|
---|
162 | (sizeof(uint32_t) + 5 * sizeof(uint16_t)) +
|
---|
163 | entry_index * sizeof(uint16_t);
|
---|
164 |
|
---|
165 | int rc = fseek(data->file, entry_offset, SEEK_SET);
|
---|
166 | if (rc != 0)
|
---|
167 | return errno;
|
---|
168 |
|
---|
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;
|
---|
173 |
|
---|
174 | glyph = uint16_t_pcf2host(glyph, data->encodings_table.format);
|
---|
175 |
|
---|
176 | if (glyph == 0xffff)
|
---|
177 | return ENOENT;
|
---|
178 |
|
---|
179 | *glyph_id = glyph;
|
---|
180 |
|
---|
181 | return EOK;
|
---|
182 | }
|
---|
183 |
|
---|
184 | static errno_t load_glyph_metrics(pcf_data_t *data, uint32_t glyph_id,
|
---|
185 | pcf_toc_entry_t *table, pcf_default_metrics_t *metrics)
|
---|
186 | {
|
---|
187 | aoff64_t offset;
|
---|
188 | int rc;
|
---|
189 | size_t records_read;
|
---|
190 |
|
---|
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);
|
---|
194 |
|
---|
195 | rc = fseek(data->file, offset, SEEK_SET);
|
---|
196 | if (rc != 0)
|
---|
197 | return errno;
|
---|
198 |
|
---|
199 | pcf_compressed_metrics_t compressed_metrics;
|
---|
200 | records_read = fread(&compressed_metrics,
|
---|
201 | sizeof(pcf_compressed_metrics_t), 1, data->file);
|
---|
202 | if (records_read != 1)
|
---|
203 | return EINVAL;
|
---|
204 |
|
---|
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;
|
---|
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 errno_t 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 | errno_t 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 | if (fseek(data->file, offset, SEEK_SET) < 0)
|
---|
269 | return errno;
|
---|
270 |
|
---|
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);
|
---|
278 |
|
---|
279 | offset = data->bitmap_table.offset + (2 * sizeof(uint32_t)) +
|
---|
280 | (data->glyph_count * sizeof(uint32_t)) + (4 * sizeof(uint32_t)) +
|
---|
281 | bitmap_offset;
|
---|
282 |
|
---|
283 | if (fseek(data->file, offset, SEEK_SET) < 0)
|
---|
284 | return errno;
|
---|
285 |
|
---|
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;
|
---|
293 |
|
---|
294 | uint8_t *bitmap = malloc(bitmap_bytes);
|
---|
295 | if (bitmap == NULL)
|
---|
296 | return ENOMEM;
|
---|
297 |
|
---|
298 | records_read = fread(bitmap, sizeof(uint8_t), bitmap_bytes,
|
---|
299 | data->file);
|
---|
300 |
|
---|
301 | surface_t *surface = surface_create(width, height, NULL, 0);
|
---|
302 | if (!surface) {
|
---|
303 | free(bitmap);
|
---|
304 | return ENOMEM;
|
---|
305 | }
|
---|
306 |
|
---|
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 =
|
---|
313 | (x % (word_size_bytes * 8)) / 8;
|
---|
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;
|
---|
317 | } else {
|
---|
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;
|
---|
324 | } else {
|
---|
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 | }
|
---|
331 |
|
---|
332 | *out_surface = surface;
|
---|
333 | free(bitmap);
|
---|
334 | return EOK;
|
---|
335 | }
|
---|
336 |
|
---|
337 | static errno_t pcf_load_glyph_metrics(void *opaque_data, glyph_id_t glyph_id,
|
---|
338 | glyph_metrics_t *gm)
|
---|
339 | {
|
---|
340 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
---|
341 |
|
---|
342 | pcf_default_metrics_t pcf_metrics;
|
---|
343 | memset(&pcf_metrics, 0, sizeof(pcf_default_metrics_t));
|
---|
344 | errno_t rc = load_glyph_metrics(data, glyph_id, &data->metrics_table,
|
---|
345 | &pcf_metrics);
|
---|
346 | if (rc != EOK)
|
---|
347 | return rc;
|
---|
348 |
|
---|
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;
|
---|
356 |
|
---|
357 | return EOK;
|
---|
358 | }
|
---|
359 |
|
---|
360 | static void pcf_release(void *opaque_data)
|
---|
361 | {
|
---|
362 | pcf_data_t *data = (pcf_data_t *) opaque_data;
|
---|
363 |
|
---|
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 |
|
---|
375 | static errno_t pcf_read_toc(pcf_data_t *data)
|
---|
376 | {
|
---|
377 | int rc = fseek(data->file, 0, SEEK_END);
|
---|
378 | if (rc != 0)
|
---|
379 | return errno;
|
---|
380 |
|
---|
381 | aoff64_t file_size = ftell(data->file);
|
---|
382 |
|
---|
383 | rc = fseek(data->file, 0, SEEK_SET);
|
---|
384 | if (rc != 0)
|
---|
385 | return errno;
|
---|
386 |
|
---|
387 | char header[4];
|
---|
388 | size_t records_read = fread(header, sizeof(char), 4, data->file);
|
---|
389 | if (records_read != 4)
|
---|
390 | return EINVAL;
|
---|
391 |
|
---|
392 | if (header[0] != 1 || header[1] != 'f' || header[2] != 'c' ||
|
---|
393 | header[3] != 'p')
|
---|
394 | return EINVAL;
|
---|
395 |
|
---|
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;
|
---|
401 |
|
---|
402 | table_count = uint32_t_le2host(table_count);
|
---|
403 |
|
---|
404 | bool found_bitmap_table = false;
|
---|
405 | bool found_metrics_table = false;
|
---|
406 | bool found_encodings_table = false;
|
---|
407 | bool found_accelerators_table = false;
|
---|
408 |
|
---|
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);
|
---|
417 |
|
---|
418 | if (toc_entry.offset >= file_size)
|
---|
419 | continue;
|
---|
420 |
|
---|
421 | aoff64_t end = ((aoff64_t) toc_entry.offset) + ((aoff64_t) toc_entry.size);
|
---|
422 | if (end > file_size)
|
---|
423 | continue;
|
---|
424 |
|
---|
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;
|
---|
430 | } else if (toc_entry.type == PCF_TABLE_METRICS) {
|
---|
431 | if (found_metrics_table)
|
---|
432 | return EINVAL;
|
---|
433 | found_metrics_table = true;
|
---|
434 | data->metrics_table = toc_entry;
|
---|
435 | } else if (toc_entry.type == PCF_TABLE_ENCODINGS) {
|
---|
436 | if (found_encodings_table)
|
---|
437 | return EINVAL;
|
---|
438 | found_encodings_table = true;
|
---|
439 | data->encodings_table = toc_entry;
|
---|
440 | } else if (toc_entry.type == PCF_TABLE_ACCELERATORS) {
|
---|
441 | if (found_accelerators_table)
|
---|
442 | return EINVAL;
|
---|
443 | found_accelerators_table = true;
|
---|
444 | data->accelerators_table = toc_entry;
|
---|
445 | }
|
---|
446 | }
|
---|
447 |
|
---|
448 | if (!found_bitmap_table || !found_metrics_table ||
|
---|
449 | !found_encodings_table || !found_accelerators_table)
|
---|
450 | return EINVAL;
|
---|
451 |
|
---|
452 | return EOK;
|
---|
453 | }
|
---|
454 |
|
---|
455 | static errno_t pcf_seek_table_header(pcf_data_t *data, pcf_toc_entry_t *table)
|
---|
456 | {
|
---|
457 | uint32_t format;
|
---|
458 | int rc = fseek(data->file, table->offset, SEEK_SET);
|
---|
459 | if (rc != 0)
|
---|
460 | return errno;
|
---|
461 |
|
---|
462 | size_t records_read = fread(&format, sizeof(uint32_t), 1, data->file);
|
---|
463 | if (records_read != 1)
|
---|
464 | return EINVAL;
|
---|
465 |
|
---|
466 | format = uint32_t_le2host(format);
|
---|
467 | if (format != table->format)
|
---|
468 | return EINVAL;
|
---|
469 |
|
---|
470 | return EOK;
|
---|
471 | }
|
---|
472 |
|
---|
473 | static errno_t pcf_read_bitmap_table_header(pcf_data_t *data)
|
---|
474 | {
|
---|
475 | errno_t rc = pcf_seek_table_header(data, &data->bitmap_table);
|
---|
476 | if (rc != EOK)
|
---|
477 | return rc;
|
---|
478 |
|
---|
479 | if ((data->bitmap_table.format & PCF_FORMAT_MASK) != PCF_FORMAT_DEFAULT)
|
---|
480 | return EINVAL;
|
---|
481 |
|
---|
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 |
|
---|
493 | static errno_t pcf_read_metrics_table_header(pcf_data_t *data)
|
---|
494 | {
|
---|
495 | errno_t rc = pcf_seek_table_header(data, &data->metrics_table);
|
---|
496 | if (rc != EOK)
|
---|
497 | return rc;
|
---|
498 |
|
---|
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;
|
---|
510 | } else {
|
---|
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 | }
|
---|
518 |
|
---|
519 | if (metrics_count != data->glyph_count)
|
---|
520 | return EINVAL;
|
---|
521 |
|
---|
522 | return EOK;
|
---|
523 | }
|
---|
524 |
|
---|
525 | static errno_t pcf_read_encodings_table_header(pcf_data_t *data)
|
---|
526 | {
|
---|
527 | errno_t rc = pcf_seek_table_header(data, &data->encodings_table);
|
---|
528 | if (rc != EOK)
|
---|
529 | return rc;
|
---|
530 |
|
---|
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;
|
---|
536 |
|
---|
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);
|
---|
547 |
|
---|
548 | data->encoding = encoding;
|
---|
549 | return EOK;
|
---|
550 | }
|
---|
551 |
|
---|
552 | static errno_t pcf_read_accelerators_table(pcf_data_t *data)
|
---|
553 | {
|
---|
554 | errno_t rc = pcf_seek_table_header(data, &data->accelerators_table);
|
---|
555 | if (rc != EOK)
|
---|
556 | return rc;
|
---|
557 |
|
---|
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;
|
---|
563 |
|
---|
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;
|
---|
569 |
|
---|
570 | return EOK;
|
---|
571 | }
|
---|
572 |
|
---|
573 | errno_t pcf_font_create(font_t **font, char *filename, uint16_t points)
|
---|
574 | {
|
---|
575 | errno_t rc;
|
---|
576 | pcf_data_t *data = malloc(sizeof(pcf_data_t));
|
---|
577 | if (data == NULL)
|
---|
578 | return ENOMEM;
|
---|
579 |
|
---|
580 | data->file = fopen(filename, "rb");
|
---|
581 | if (data->file == NULL)
|
---|
582 | goto read_error;
|
---|
583 |
|
---|
584 | rc = pcf_read_toc(data);
|
---|
585 | if (rc != EOK)
|
---|
586 | goto error;
|
---|
587 |
|
---|
588 | rc = pcf_read_bitmap_table_header(data);
|
---|
589 | if (rc != EOK)
|
---|
590 | goto error;
|
---|
591 |
|
---|
592 | rc = pcf_read_metrics_table_header(data);
|
---|
593 | if (rc != EOK)
|
---|
594 | goto error;
|
---|
595 |
|
---|
596 | rc = pcf_read_encodings_table_header(data);
|
---|
597 | if (rc != EOK)
|
---|
598 | goto error;
|
---|
599 |
|
---|
600 | rc = pcf_read_accelerators_table(data);
|
---|
601 | if (rc != EOK)
|
---|
602 | goto error;
|
---|
603 |
|
---|
604 | rc = bitmap_font_create(&fd_pcf, data, data->glyph_count,
|
---|
605 | data->font_metrics, points, font);
|
---|
606 | if (rc != EOK)
|
---|
607 | goto error;
|
---|
608 |
|
---|
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 | */
|
---|