-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
ttf.c
2100 lines (1702 loc) · 57.4 KB
/
ttf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Implementation file for TTF library
//
// https://github.com/michaelrsweet/ttf
//
// Copyright © 2018-2024 by Michael R Sweet.
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#ifdef _WIN32
# define _CRT_SECURE_NO_WARNINGS
#endif // _WIN32
#include "ttf.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <fcntl.h>
#ifdef _WIN32
# include <io.h>
# include <direct.h>
//
// Microsoft renames the POSIX functions to _name, and introduces
// a broken compatibility layer using the original names. As a result,
// random crashes can occur when, for example, strdup() allocates memory
// from a different heap than used by malloc() and free().
//
// To avoid moronic problems like this, we #define the POSIX function
// names to the corresponding non-standard Microsoft names.
//
# define access _access
# define close _close
# define fileno _fileno
# define lseek _lseek
# define mkdir(d,p) _mkdir(d)
# define open _open
# define read _read
# define rmdir _rmdir
# define snprintf _snprintf
# define strdup _strdup
# define unlink _unlink
# define vsnprintf _vsnprintf
# define write _write
//
// Map various parameters for POSIX...
//
# define F_OK 00
# define W_OK 02
# define R_OK 04
# define O_BINARY _O_BINARY
# define O_RDONLY _O_RDONLY
# define O_WRONLY _O_WRONLY
# define O_CREAT _O_CREAT
# define O_TRUNC _O_TRUNC
typedef __int64 ssize_t; // POSIX type not present on Windows... @private@
#else
# include <unistd.h>
# define O_BINARY 0
#endif // _WIN32
//
// DEBUG is typically defined for debug builds. TTF_DEBUG maps to fprintf when
// DEBUG is defined and is a no-op otherwise...
//
#ifdef DEBUG
# define TTF_DEBUG(...) fprintf(stderr, __VA_ARGS__)
#else
# define TTF_DEBUG(...)
#endif // DEBUG
//
// TTF_FORMAT_ARGS tells the compiler to validate printf-style format
// arguments, producing warnings when there are issues...
//
#if defined(__has_extension) || defined(__GNUC__)
# define TTF_FORMAT_ARGS(a,b) __attribute__ ((__format__(__printf__, a, b)))
#else
# define TTF_FORMAT_ARGS(a,b)
#endif // __has_extension || __GNUC__
//
// Constants...
//
#define TTF_FONT_MAX_CHAR 262144 // Maximum number of character values
#define TTF_FONT_MAX_GROUPS 65536 // Maximum number of sub-groups
#define TTF_FONT_MAX_NAMES 16777216// Maximum size of names table we support
//
// TTF/OFF tag constants...
//
#define TTF_OFF_cmap 0x636d6170 // Character to glyph mapping
#define TTF_OFF_head 0x68656164 // Font header
#define TTF_OFF_hhea 0x68686561 // Horizontal header
#define TTF_OFF_hmtx 0x686d7478 // Horizontal metrics
#define TTF_OFF_maxp 0x6d617870 // Maximum profile
#define TTF_OFF_name 0x6e616d65 // Naming table
#define TTF_OFF_OS_2 0x4f532f32 // OS/2 and Windows specific metrics
#define TTF_OFF_post 0x706f7374 // PostScript information
#define TTF_OFF_Unicode 0 // Unicode platform ID
#define TTF_OFF_Mac 1 // Macintosh platform ID
#define TTF_OFF_Mac_Roman 0 // Macintosh Roman encoding ID
#define TTF_OFF_Mac_USEnglish 0 // Macintosh US English language ID
#define TTF_OFF_Windows 3 // Windows platform ID
#define TTF_OFF_Windows_English 9 // Windows English language ID base
#define TTF_OFF_Windows_UCS2 1 // Windows UCS-2 encoding
#define TTF_OFF_Windows_UCS4 10 // Windows UCS-4 encoding
#define TTF_OFF_Copyright 0 // Copyright naming string
#define TTF_OFF_FontFamily 1 // Font family naming string ("Arial")
#define TTF_OFF_FontSubfamily 2 // Font sub-family naming string ("Bold")
#define TTF_OFF_FontFullName 4 // Font full name ("Arial Bold")
#define TTF_OFF_FontVersion 5 // Font version number
#define TTF_OFF_PostScriptName 6 // Font PostScript name
//
// Local types...
//
typedef struct _ttf_metric_s //*** Font metric information ****/
{
short width, // Advance width
left_bearing; // Left side bearing
} _ttf_metric_t;
typedef struct _ttf_off_dir_s // OFF/TTF directory entry
{
unsigned tag; // Table identifier
unsigned checksum; // Checksum of table
unsigned offset; // Offset from the beginning of the file
unsigned length; // Length
} _ttf_off_dir_t;
typedef struct _ttf_off_table_s // OFF/TTF offset table
{
int num_entries; // Number of table entries
_ttf_off_dir_t *entries; // Table entries
} _ttf_off_table_t;
typedef struct _ttf_off_name_s // OFF/TTF name string
{
unsigned short platform_id, // Platform identifier
encoding_id, // Encoding identifier
language_id, // Language identifier
name_id, // Name identifier
length, // Length of string
offset; // Offset from start of storage area
} _ttf_off_name_t;
typedef struct _ttf_off_names_s // OFF/TTF naming table
{
int num_names; // Number of names
_ttf_off_name_t *names; // Names
unsigned char *storage; // Storage area
unsigned storage_size; // Size of storage area
} _ttf_off_names_t;
struct _ttf_s
{
int fd; // File descriptor
size_t idx; // Font number in file
ttf_err_cb_t err_cb; // Error callback, if any
void *err_data; // Error callback data
_ttf_off_table_t table; // Offset table
_ttf_off_names_t names; // Names
size_t num_fonts; // Number of fonts in this file
char *copyright; // Copyright string
char *family; // Font family string
char *postscript_name; // PostScript name string
char *version; // Font version string
bool is_fixed; // Is this a fixed-width font?
int max_char, // Last character in font
min_char; // First character in font
size_t num_cmap; // Number of entries in glyph map
int *cmap; // Unicode character to glyph map
_ttf_metric_t *widths[TTF_FONT_MAX_CHAR / 256];
// Character metrics (sparse array)
float units; // Width units
short ascent, // Maximum ascent above baseline
descent, // Maximum descent below baseline
cap_height, // "A" height
x_height, // "x" height
x_max, // Bounding box
x_min,
y_max,
y_min,
weight; // Font weight
float italic_angle; // Angle of italic text
ttf_stretch_t stretch; // Font stretch value
ttf_style_t style; // Font style
};
typedef struct _ttf_off_cmap4_s // Format 4 cmap table
{
unsigned short startCode, // First character
endCode, // Last character
idRangeOffset; // Offset for range (modulo 65536)
short idDelta; // Delta for range (modulo 65536)
} _ttf_off_cmap4_t;
typedef struct _ttf_off_cmap12_s // Format 12 cmap table
{
unsigned startCharCode, // First character
endCharCode, // Last character
startGlyphID; // Glyph index for the first character
} _ttf_off_cmap12_t;
typedef struct _ttf_off_cmap13_s // Format 13 cmap table
{
unsigned startCharCode, // First character
endCharCode, // Last character
glyphID; // Glyph index for all characters
} _ttf_off_cmap13_t;
typedef struct _ttf_off_head_s // Font header
{
unsigned short unitsPerEm; // Units for widths/coordinates
short xMin, // Bounding box of all glyphs
yMin,
xMax,
yMax;
unsigned short macStyle; // Mac style bits
} _ttf_off_head_t;
#define TTF_OFF_macStyle_Bold 0x01
#define TTF_OFF_macStyle_Italic 0x02
#define TTF_OFF_macStyle_Underline 0x04
#define TTF_OFF_macStyle_Outline 0x08
#define TTF_OFF_macStyle_Shadow 0x10
#define TTF_OFF_macStyle_Condensed 0x20
#define TTF_OFF_macStyle_Extended 0x40
typedef struct _ttf_off_hhea_s // Horizontal header
{
short ascender, // Ascender
descender; // Descender
unsigned short numberOfHMetrics; // Number of horizontal metrics
} _ttf_off_hhea_t;
typedef struct _ttf_off_os_2_s // OS/2 information
{
unsigned short usWeightClass, // Font weight
usWidthClass, // Font weight
fsType; // Type bits
short sTypoAscender, // Ascender
sTypoDescender, // Descender
sxHeight, // xHeight
sCapHeight; // CapHeight
} _ttf_off_os_2_t;
typedef struct _ttf_off_post_s // PostScript information
{
float italicAngle; // Italic angle
unsigned isFixedPitch; // Fixed-width font?
} _ttf_off_post_t;
//
// Local functions...
//
static char *copy_name(ttf_t *font, unsigned name_id);
static void errorf(ttf_t *font, const char *message, ...) TTF_FORMAT_ARGS(2,3);
static bool read_cmap(ttf_t *font);
static bool read_head(ttf_t *font, _ttf_off_head_t *head);
static bool read_hhea(ttf_t *font, _ttf_off_hhea_t *hhea);
static _ttf_metric_t *read_hmtx(ttf_t *font, _ttf_off_hhea_t *hhea);
static int read_maxp(ttf_t *font);
static bool read_names(ttf_t *font);
static bool read_os_2(ttf_t *font, _ttf_off_os_2_t *os_2);
static bool read_post(ttf_t *font, _ttf_off_post_t *post);
static int read_short(ttf_t *font);
static bool read_table(ttf_t *font);
static unsigned read_ulong(ttf_t *font);
static int read_ushort(ttf_t *font);
static unsigned seek_table(ttf_t *font, unsigned tag, unsigned offset, bool required);
//
// 'ttfCreate()' - Create a new font object for the named font file.
//
// This function creates a new font object for the named TrueType or OpenType
// font file or collection. The "filename" argument specifies the name of the
// file to read.
//
// The "idx" argument specifies the font to load from a collection - the first
// font is number `0`. Once created, you can call the @link ttfGetNumFonts@
// function to determine whether the loaded font file is a collection with more
// than one font.
//
// The "err_cb" and "err_data" arguments specify a callback function and data
// pointer for receiving error messages. If `NULL`, errors are sent to the
// `stderr` file. The callback function receives the data pointer and a text
// message string, for example:
//
// ```
// void my_err_cb(void *err_data, const char *message)
// {
// fprintf(stderr, "ERROR: %s\n", message);
// }
// ```
//
ttf_t * // O - New font object
ttfCreate(const char *filename, // I - Filename
size_t idx, // I - Font number to create in collection (0-based)
ttf_err_cb_t err_cb, // I - Error callback or `NULL` to log to stderr
void *err_data) // I - Error callback data
{
ttf_t *font = NULL; // New font object
size_t i; // Looping var
_ttf_metric_t *widths = NULL; // Glyph metrics
_ttf_off_head_t head; // head table
_ttf_off_hhea_t hhea; // hhea table
_ttf_off_os_2_t os_2; // OS/2 table
_ttf_off_post_t post; // PostScript table
TTF_DEBUG("ttfCreate(filename=\"%s\", idx=%u, err_cb=%p, err_data=%p)\n", filename, (unsigned)idx, err_cb, err_data);
// Range check input..
if (!filename)
{
errno = EINVAL;
return (NULL);
}
// Allocate memory...
if ((font = (ttf_t *)calloc(1, sizeof(ttf_t))) == NULL)
return (NULL);
font->idx = idx;
font->err_cb = err_cb;
font->err_data = err_data;
// Open the font file...
if ((font->fd = open(filename, O_RDONLY | O_BINARY)) < 0)
{
errorf(font, "Unable to open '%s': %s", filename, strerror(errno));
goto error;
}
TTF_DEBUG("ttfCreate: fd=%d\n", font->fd);
// Read the table of contents and the identifying names...
if (!read_table(font))
goto error;
TTF_DEBUG("ttfCreate: num_entries=%d\n", font->table.num_entries);
if (!read_names(font))
goto error;
TTF_DEBUG("ttfCreate: num_names=%d\n", font->names.num_names);
// Copy key font meta data strings...
font->copyright = copy_name(font, TTF_OFF_Copyright);
font->family = copy_name(font, TTF_OFF_FontFamily);
font->postscript_name = copy_name(font, TTF_OFF_PostScriptName);
font->version = copy_name(font, TTF_OFF_FontVersion);
if (read_post(font, &post))
{
font->italic_angle = post.italicAngle;
font->is_fixed = post.isFixedPitch != 0;
}
TTF_DEBUG("ttfCreate: copyright=\"%s\"\n", font->copyright);
TTF_DEBUG("ttfCreate: family=\"%s\"\n", font->family);
TTF_DEBUG("ttfCreate: postscript_name=\"%s\"\n", font->postscript_name);
TTF_DEBUG("ttfCreate: version=\"%s\"\n", font->version);
TTF_DEBUG("ttfCreate: italic_angle=%g\n", font->italic_angle);
TTF_DEBUG("ttfCreate: is_fixed=%s\n", font->is_fixed ? "true" : "false");
if (!read_cmap(font))
goto error;
if (!read_head(font, &head))
goto error;
font->units = (float)head.unitsPerEm;
font->x_max = head.xMax;
font->x_min = head.xMin;
font->y_max = head.yMax;
font->y_min = head.yMin;
if (head.macStyle & TTF_OFF_macStyle_Italic)
{
if (font->postscript_name && strstr(font->postscript_name, "Oblique"))
font->style = TTF_STYLE_OBLIQUE;
else
font->style = TTF_STYLE_ITALIC;
}
else
font->style = TTF_STYLE_NORMAL;
if (!read_hhea(font, &hhea))
goto error;
font->ascent = hhea.ascender;
font->descent = hhea.descender;
if (read_maxp(font) < 0)
goto error;
if (hhea.numberOfHMetrics > 0)
{
if ((widths = read_hmtx(font, &hhea)) == NULL)
goto error;
}
else
{
errorf(font, "Number of horizontal metrics is 0.");
goto error;
}
if (read_os_2(font, &os_2))
{
// Copy key values from OS/2 table...
static const ttf_stretch_t stretches[] =
{
TTF_STRETCH_ULTRA_CONDENSED, // ultra-condensed
TTF_STRETCH_EXTRA_CONDENSED, // extra-condensed
TTF_STRETCH_CONDENSED, // condensed
TTF_STRETCH_SEMI_CONDENSED, // semi-condensed
TTF_STRETCH_NORMAL, // normal
TTF_STRETCH_SEMI_EXPANDED, // semi-expanded
TTF_STRETCH_EXPANDED, // expanded
TTF_STRETCH_EXTRA_EXPANDED, // extra-expanded
TTF_STRETCH_ULTRA_EXPANDED // ultra-expanded
};
if (os_2.usWidthClass >= 1 && os_2.usWidthClass <= (int)(sizeof(stretches) / sizeof(stretches[0])))
font->stretch = stretches[os_2.usWidthClass - 1];
font->weight = (short)os_2.usWeightClass;
font->cap_height = os_2.sCapHeight;
font->x_height = os_2.sxHeight;
}
else
{
// Default key values since there isn't an OS/2 table...
TTF_DEBUG("ttfCreate: Unable to read OS/2 table.\n");
font->weight = 400;
}
if (font->cap_height == 0)
font->cap_height = font->ascent;
if (font->x_height == 0)
font->x_height = 3 * font->ascent / 5;
// Build a sparse glyph widths table...
font->min_char = -1;
for (i = 0; i < font->num_cmap; i ++)
{
if (font->cmap[i] >= 0)
{
int bin = (int)i / 256, // Sub-array bin
glyph = font->cmap[i]; // Glyph index
// Update min/max...
if (font->min_char < 0)
font->min_char = (int)i;
font->max_char = (int)i;
// Allocate a sub-array as needed...
if (!font->widths[bin])
font->widths[bin] = (_ttf_metric_t *)calloc(256, sizeof(_ttf_metric_t));
// Copy the width of the specified glyph or the last one if we are past
// the end of the table...
if (glyph >= hhea.numberOfHMetrics)
font->widths[bin][i & 255] = widths[hhea.numberOfHMetrics - 1];
else
font->widths[bin][i & 255] = widths[glyph];
}
#ifdef DEBUG
if (i >= ' ' && i < 127 && font->widths[0])
TTF_DEBUG("ttfCreate: width['%c']=%d(%d)\n", (char)i, font->widths[0][i].width, font->widths[0][i].left_bearing);
#endif // DEBUG
}
// Cleanup and return the font...
free(widths);
return (font);
// If we get here something bad happened...
error:
free(widths);
ttfDelete(font);
return (NULL);
}
//
// 'ttfDelete()' - Free all memory used for a font family object.
//
void
ttfDelete(ttf_t *font) // I - Font
{
int i; // Looping var
// Range check input...
if (!font)
return;
// Close the font file...
if (font->fd >= 0)
close(font->fd);
// Free all memory used...
free(font->copyright);
free(font->family);
free(font->postscript_name);
free(font->version);
free(font->table.entries);
free(font->names.names);
free(font->names.storage);
free(font->cmap);
for (i = 0; i < 256; i ++)
free(font->widths[i]);
free(font);
}
//
// 'ttfGetAscent()' - Get the maximum height of non-accented characters.
//
int // O - Ascent in 1000ths
ttfGetAscent(ttf_t *font) // I - Font
{
return (font ? (int)(1000 * font->ascent / font->units) : 0);
}
//
// 'ttfGetBounds()' - Get the bounds of all characters in a font.
//
// This function gets the bounds of all characters in a font. The "bounds"
// argument is a pointer to a `ttf_rect_t` structure that will be filled with
// the limits for characters in the font scaled to a 1000x1000 unit square.
//
ttf_rect_t * // O - Bounds or `NULL` on error
ttfGetBounds(ttf_t *font, // I - Font
ttf_rect_t *bounds) // I - Bounds buffer
{
// Range check input...
if (!font || !bounds)
{
if (bounds)
memset(bounds, 0, sizeof(ttf_rect_t));
return (NULL);
}
bounds->left = 1000.0f * font->x_min / font->units;
bounds->right = 1000.0f * font->x_max / font->units;
bounds->bottom = 1000.0f * font->y_min / font->units;
bounds->top = 1000.0f * font->y_max / font->units;
return (bounds);
}
//
// 'ttfGetCapHeight()' - Get the height of capital letters.
//
int // O - Capital letter height in 1000ths
ttfGetCapHeight(ttf_t *font) // I - Font
{
return (font ? (int)(1000 * font->cap_height / font->units) : 0);
}
//
// 'ttfGetCMap()' - Get the Unicode to glyph mapping table.
//
const int * // O - CMap table
ttfGetCMap(ttf_t *font, // I - Font
size_t *num_cmap) // O - Number of entries in table
{
// Range check input...
if (!font || !num_cmap)
{
if (num_cmap)
*num_cmap = 0;
return (NULL);
}
*num_cmap = font->num_cmap;
return (font->cmap);
}
//
// 'ttfGetCopyright()' - Get the copyright text for a font.
//
const char * // O - Copyright text
ttfGetCopyright(ttf_t *font) // I - Font
{
return (font ? font->copyright : NULL);
}
//
// 'ttfGetDescent()' - Get the maximum depth of non-accented characters.
//
int // O - Descent in 1000ths
ttfGetDescent(ttf_t *font) // I - Font
{
return (font ? (int)(1000 * font->descent / font->units) : 0);
}
//
// 'ttfGetExtents()' - Get the extents of a UTF-8 string.
//
// This function computes the extents of the UTF-8 string "s" when rendered
// using the specified font "font" and size "size". The "extents" argument is
// a pointer to a `ttf_rect_t` structure that is filled with the extents of a
// simple rendering of the string with no kerning or rewriting applied. The
// values are scaled using the specified font size.
//
ttf_rect_t * // O - Pointer to extents or `NULL` on error
ttfGetExtents(
ttf_t *font, // I - Font
float size, // I - Font size
const char *s, // I - String
ttf_rect_t *extents) // O - Extents of the string
{
bool first = true; // First character?
int ch, // Current character
width = 0; // Width
_ttf_metric_t *widths; // Widths
TTF_DEBUG("ttfGetExtents(font=%p, size=%.2f, s=\"%s\", extents=%p)\n", (void *)font, size, s, (void *)extents);
// Make sure extents is zeroed out...
if (extents)
memset(extents, 0, sizeof(ttf_rect_t));
// Range check input...
if (!font || size <= 0.0f || !s || !extents)
return (NULL);
// Loop through the string...
while (*s)
{
// Get the next Unicode character...
if ((*s & 0xe0) == 0xc0 && (s[1] & 0xc0) == 0x80)
{
// Two byte UTF-8
ch = ((*s & 0x1f) << 6) | (s[1] & 0x3f);
s += 2;
}
else if ((*s & 0xf0) == 0xe0 && (s[1] & 0xc0) == 0x80 && (s[2] & 0xc0) == 0x80)
{
// Three byte UTF-8
ch = ((*s & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
s += 3;
}
else if ((*s & 0xf8) == 0xf0 && (s[1] & 0xc0) == 0x80 && (s[2] & 0xc0) == 0x80 && (s[3] & 0xc0) == 0x80)
{
// Four byte UTF-8
ch = ((*s & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
s += 4;
}
else if (*s & 0x80)
{
// Invalid UTF-8
errorf(font, "Invalid UTF-8 sequence starting with 0x%02X.", *s & 255);
return (NULL);
}
else
{
// ASCII...
ch = *s++;
}
// Find its width...
if ((widths = font->widths[ch / 256]) != NULL)
{
if (first)
{
extents->left = -widths[ch & 255].left_bearing / font->units;
first = false;
}
width += widths[ch & 255].width;
}
else if ((widths = font->widths[0]) != NULL)
{
// Use the ".notdef" (0) glyph width...
if (first)
{
extents->left = -widths[0].left_bearing / font->units;
first = false;
}
width += widths[0].width;
}
}
// Calculate the bounding box for the text and return...
TTF_DEBUG("ttfGetExtents: width=%d\n", width);
extents->bottom = size * font->y_min / font->units;
extents->right = size * width / font->units + extents->left;
extents->top = size * font->y_max / font->units;
return (extents);
}
//
// 'ttfGetFamily()' - Get the family name of a font.
//
const char * // O - Family name
ttfGetFamily(ttf_t *font) // I - Font
{
return (font ? font->family : NULL);
}
//
// 'ttfGetItalicAngle()' - Get the italic angle.
//
float // O - Angle in degrees
ttfGetItalicAngle(ttf_t *font) // I - Font
{
return (font ? font->italic_angle : 0.0f);
}
//
// 'ttfGetMaxChar()' - Get the last character in the font.
//
int // O - Last character in font
ttfGetMaxChar(ttf_t *font) // I - Font
{
return (font ? font->max_char : 0);
}
//
// 'ttfGetMinChar()' - Get the first character in the font.
//
int // O - First character in font
ttfGetMinChar(ttf_t *font) // I - Font
{
return (font ? font->min_char : 0);
}
//
// 'ttfGetNumFonts()' - Get the number of fonts in this collection.
//
size_t // O - Number of fonts
ttfGetNumFonts(ttf_t *font) // I - Font
{
return (font ? font->num_fonts : 0);
}
//
// 'ttfGetPostScriptName()' - Get the PostScript name of a font.
//
const char * // O - PostScript name
ttfGetPostScriptName(
ttf_t *font) // I - Font
{
return (font ? font->postscript_name : NULL);
}
//
// 'ttfGetStretch()' - Get the font "stretch" value...
//
ttf_stretch_t // O - Stretch value
ttfGetStretch(ttf_t *font) // I - Font
{
return (font ? font->stretch : TTF_STRETCH_NORMAL);
}
//
// 'ttfGetStyle()' - Get the font style.
//
ttf_style_t // O - Style
ttfGetStyle(ttf_t *font) // I - Font
{
return (font ? font->style : TTF_STYLE_NORMAL);
}
//
// 'ttfGetVersion()' - Get the version number of a font.
//
const char * // O - Version number
ttfGetVersion(ttf_t *font) // I - Font
{
return (font ? font->version : NULL);
}
//
// 'ttfGetWeight()' - Get the weight of a font.
//
ttf_weight_t // O - Weight
ttfGetWeight(ttf_t *font) // I - Font
{
return (font ? (ttf_weight_t)font->weight : TTF_WEIGHT_400);
}
//
// 'ttfGetWidth()' - Get the width of a single character.
//
int // O - Width in 1000ths
ttfGetWidth(ttf_t *font, // I - Font
int ch) // I - Unicode character
{
int bin = ch >> 8; // Bin in widths array
// Range check input...
if (!font || ch < ' ' || ch == 0x7f)
return (0);
if (font->widths[bin])
return ((int)(1000.0f * font->widths[bin][ch & 255].width / font->units));
else if (font->widths[0]) // .notdef
return ((int)(1000.0f * font->widths[0][0].width / font->units));
else
return (0);
}
//
// 'ttfGetXHeight()' - Get the height of lowercase letters.
//
int // O - Lowercase letter height in 1000ths
ttfGetXHeight(ttf_t *font) // I - Font
{
return (font ? (int)(1000 * font->x_height / font->units) : 0);
}
//
// 'ttfIsFixedPitch()' - Determine whether a font is fixedpitch.
//
bool // O - `true` if fixed pitch, `false` otherwise
ttfIsFixedPitch(ttf_t *font) // I - Font
{
return (font ? font->is_fixed : false);
}
//
// 'copy_name()' - Copy a name string from a font.
//
static char * // O - Name string or `NULL`
copy_name(ttf_t *font, // I - Font
unsigned name_id) // I - Name identifier
{
int i; // Looping var
_ttf_off_name_t *name; // Current name
for (i = font->names.num_names, name = font->names.names; i > 0; i --, name ++)
{
if (name->name_id == name_id &&
((name->platform_id == TTF_OFF_Mac && name->language_id == TTF_OFF_Mac_USEnglish) ||
(name->platform_id == TTF_OFF_Windows && (name->language_id & 0xff) == TTF_OFF_Windows_English)))
{
char temp[1024], // Temporary string buffer
*tempptr, // Pointer into temporary string
*storptr; // Pointer into storage
int chars, // Length of string to copy in characters
bpc; // Bytes per character
if ((unsigned)(name->offset + name->length) > font->names.storage_size)
{
TTF_DEBUG("copy_name: offset(%d)+length(%d) > storage_size(%d)\n", name->offset, name->length, font->names.storage_size);
continue;
}
if (name->platform_id == TTF_OFF_Windows && name->encoding_id == TTF_OFF_Windows_UCS2)
{
storptr = (char *)font->names.storage + name->offset;
chars = name->length / 2;
bpc = 2;
}
else if (name->platform_id == TTF_OFF_Windows && name->encoding_id == TTF_OFF_Windows_UCS4)
{
storptr = (char *)font->names.storage + name->offset;
chars = name->length / 4;
bpc = 4;
}
else
{
storptr = (char *)font->names.storage + name->offset;
chars = name->length;
bpc = 1;
}
for (tempptr = temp; chars > 0; storptr += bpc, chars --)
{
int ch; // Current character
// Convert to Unicode...
if (bpc == 1)
ch = *storptr;
else if (bpc == 2)
ch = ((storptr[0] & 255) << 8) | (storptr[1] & 255);
else
ch = ((storptr[0] & 255) << 24) | ((storptr[1] & 255) << 16) | ((storptr[2] & 255) << 8) | (storptr[3] & 255);
// Convert to UTF-8...
if (ch < 0x80)
{
// ASCII...
if (tempptr < (temp + sizeof(temp) - 1))
*tempptr++ = (char)ch;
else
break;
}
else if (ch < 0x400)
{
// Two byte UTF-8
if (tempptr < (temp + sizeof(temp) - 2))
{
*tempptr++ = (char)(0xc0 | (ch >> 6));
*tempptr++ = (char)(0x80 | (ch & 0x3f));
}
else
break;
}
else if (ch < 0x10000)
{
// Three byte UTF-8