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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c21d4d6 was 2bb6d04, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Curb the proliferation of libdraw headers

libdraw provides a lot of ambiguously named headers, which makes it
confusing. This change merges the subdirectories into single headers,
and moves all headers into draw subdirectory, so that it's obvious
at a glance what library the header belongs to.

Compare:

#include <path.h>
#include <source.h>
#include <font/bitmap_backend.h>
#include <font/pcf.h>

vs.

#include <draw/path.h>
#include <draw/source.h>
#include <draw/font.h>

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