-
Notifications
You must be signed in to change notification settings - Fork 28
/
gfxFontUtils.cpp
2250 lines (1891 loc) · 82.8 KB
/
gfxFontUtils.cpp
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
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is thebes gfx code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2007-2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Stuart Parmenter <stuart@mozilla.com>
* John Daggett <jdaggett@mozilla.com>
* Jonathan Kew <jfkthame@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "gfxFontUtils.h"
#include "nsServiceManagerUtils.h"
#include "mozilla/Preferences.h"
#include "nsIStreamBufferAccess.h"
#include "nsIUUIDGenerator.h"
#include "nsMemory.h"
#include "nsICharsetConverterManager.h"
#include "plbase64.h"
#include "woff.h"
#ifdef XP_MACOSX
#include <CoreFoundation/CoreFoundation.h>
#endif
#define NO_RANGE_FOUND 126 // bit 126 in the font unicode ranges is required to be 0
#define UNICODE_BMP_LIMIT 0x10000
using namespace mozilla; // for the AutoSwap_* types
/* Unicode subrange table
* from: http://msdn.microsoft.com/en-us/library/dd374090
*
* Edit the text to extend the initial digit, then use something like:
* perl -pi -e 's/^(\d+)\t([\dA-Fa-f]+)\s+-\s+([\dA-Fa-f]+)\s+\b([a-zA-Z0-9\(\)\- ]+)/ { \1, 0x\2, 0x\3, \"\4\" },/' < unicoderange.txt
* to generate the below list.
*/
struct UnicodeRangeTableEntry
{
PRUint8 bit;
PRUint32 start;
PRUint32 end;
const char *info;
};
static struct UnicodeRangeTableEntry gUnicodeRanges[] = {
{ 0, 0x0000, 0x007F, "Basic Latin" },
{ 1, 0x0080, 0x00FF, "Latin-1 Supplement" },
{ 2, 0x0100, 0x017F, "Latin Extended-A" },
{ 3, 0x0180, 0x024F, "Latin Extended-B" },
{ 4, 0x0250, 0x02AF, "IPA Extensions" },
{ 4, 0x1D00, 0x1D7F, "Phonetic Extensions" },
{ 4, 0x1D80, 0x1DBF, "Phonetic Extensions Supplement" },
{ 5, 0x02B0, 0x02FF, "Spacing Modifier Letters" },
{ 5, 0xA700, 0xA71F, "Modifier Tone Letters" },
{ 6, 0x0300, 0x036F, "Combining Diacritical Marks" },
{ 6, 0x1DC0, 0x1DFF, "Combining Diacritical Marks Supplement" },
{ 7, 0x0370, 0x03FF, "Greek and Coptic" },
{ 8, 0x2C80, 0x2CFF, "Coptic" },
{ 9, 0x0400, 0x04FF, "Cyrillic" },
{ 9, 0x0500, 0x052F, "Cyrillic Supplement" },
{ 9, 0x2DE0, 0x2DFF, "Cyrillic Extended-A" },
{ 9, 0xA640, 0xA69F, "Cyrillic Extended-B" },
{ 10, 0x0530, 0x058F, "Armenian" },
{ 11, 0x0590, 0x05FF, "Hebrew" },
{ 12, 0xA500, 0xA63F, "Vai" },
{ 13, 0x0600, 0x06FF, "Arabic" },
{ 13, 0x0750, 0x077F, "Arabic Supplement" },
{ 14, 0x07C0, 0x07FF, "NKo" },
{ 15, 0x0900, 0x097F, "Devanagari" },
{ 16, 0x0980, 0x09FF, "Bengali" },
{ 17, 0x0A00, 0x0A7F, "Gurmukhi" },
{ 18, 0x0A80, 0x0AFF, "Gujarati" },
{ 19, 0x0B00, 0x0B7F, "Oriya" },
{ 20, 0x0B80, 0x0BFF, "Tamil" },
{ 21, 0x0C00, 0x0C7F, "Telugu" },
{ 22, 0x0C80, 0x0CFF, "Kannada" },
{ 23, 0x0D00, 0x0D7F, "Malayalam" },
{ 24, 0x0E00, 0x0E7F, "Thai" },
{ 25, 0x0E80, 0x0EFF, "Lao" },
{ 26, 0x10A0, 0x10FF, "Georgian" },
{ 26, 0x2D00, 0x2D2F, "Georgian Supplement" },
{ 27, 0x1B00, 0x1B7F, "Balinese" },
{ 28, 0x1100, 0x11FF, "Hangul Jamo" },
{ 29, 0x1E00, 0x1EFF, "Latin Extended Additional" },
{ 29, 0x2C60, 0x2C7F, "Latin Extended-C" },
{ 29, 0xA720, 0xA7FF, "Latin Extended-D" },
{ 30, 0x1F00, 0x1FFF, "Greek Extended" },
{ 31, 0x2000, 0x206F, "General Punctuation" },
{ 31, 0x2E00, 0x2E7F, "Supplemental Punctuation" },
{ 32, 0x2070, 0x209F, "Superscripts And Subscripts" },
{ 33, 0x20A0, 0x20CF, "Currency Symbols" },
{ 34, 0x20D0, 0x20FF, "Combining Diacritical Marks For Symbols" },
{ 35, 0x2100, 0x214F, "Letterlike Symbols" },
{ 36, 0x2150, 0x218F, "Number Forms" },
{ 37, 0x2190, 0x21FF, "Arrows" },
{ 37, 0x27F0, 0x27FF, "Supplemental Arrows-A" },
{ 37, 0x2900, 0x297F, "Supplemental Arrows-B" },
{ 37, 0x2B00, 0x2BFF, "Miscellaneous Symbols and Arrows" },
{ 38, 0x2200, 0x22FF, "Mathematical Operators" },
{ 38, 0x27C0, 0x27EF, "Miscellaneous Mathematical Symbols-A" },
{ 38, 0x2980, 0x29FF, "Miscellaneous Mathematical Symbols-B" },
{ 38, 0x2A00, 0x2AFF, "Supplemental Mathematical Operators" },
{ 39, 0x2300, 0x23FF, "Miscellaneous Technical" },
{ 40, 0x2400, 0x243F, "Control Pictures" },
{ 41, 0x2440, 0x245F, "Optical Character Recognition" },
{ 42, 0x2460, 0x24FF, "Enclosed Alphanumerics" },
{ 43, 0x2500, 0x257F, "Box Drawing" },
{ 44, 0x2580, 0x259F, "Block Elements" },
{ 45, 0x25A0, 0x25FF, "Geometric Shapes" },
{ 46, 0x2600, 0x26FF, "Miscellaneous Symbols" },
{ 47, 0x2700, 0x27BF, "Dingbats" },
{ 48, 0x3000, 0x303F, "CJK Symbols And Punctuation" },
{ 49, 0x3040, 0x309F, "Hiragana" },
{ 50, 0x30A0, 0x30FF, "Katakana" },
{ 50, 0x31F0, 0x31FF, "Katakana Phonetic Extensions" },
{ 51, 0x3100, 0x312F, "Bopomofo" },
{ 50, 0x31A0, 0x31BF, "Bopomofo Extended" },
{ 52, 0x3130, 0x318F, "Hangul Compatibility Jamo" },
{ 53, 0xA840, 0xA87F, "Phags-pa" },
{ 54, 0x3200, 0x32FF, "Enclosed CJK Letters And Months" },
{ 55, 0x3300, 0x33FF, "CJK Compatibility" },
{ 56, 0xAC00, 0xD7AF, "Hangul Syllables" },
{ 57, 0xD800, 0xDFFF, "Non-Plane 0" },
{ 58, 0x10900, 0x1091F, "Phoenician" },
{ 59, 0x2E80, 0x2EFF, "CJK Radicals Supplement" },
{ 59, 0x2F00, 0x2FDF, "Kangxi Radicals" },
{ 59, 0x2FF0, 0x2FFF, "Ideographic Description Characters" },
{ 59, 0x3190, 0x319F, "Kanbun" },
{ 59, 0x3400, 0x4DBF, "CJK Unified Ideographs Extension A" },
{ 59, 0x4E00, 0x9FFF, "CJK Unified Ideographs" },
{ 59, 0x20000, 0x2A6DF, "CJK Unified Ideographs Extension B" },
{ 60, 0xE000, 0xF8FF, "Private Use Area" },
{ 61, 0x31C0, 0x31EF, "CJK Strokes" },
{ 61, 0xF900, 0xFAFF, "CJK Compatibility Ideographs" },
{ 61, 0x2F800, 0x2FA1F, "CJK Compatibility Ideographs Supplement" },
{ 62, 0xFB00, 0xFB4F, "Alphabetic Presentation Forms" },
{ 63, 0xFB50, 0xFDFF, "Arabic Presentation Forms-A" },
{ 64, 0xFE20, 0xFE2F, "Combining Half Marks" },
{ 65, 0xFE10, 0xFE1F, "Vertical Forms" },
{ 65, 0xFE30, 0xFE4F, "CJK Compatibility Forms" },
{ 66, 0xFE50, 0xFE6F, "Small Form Variants" },
{ 67, 0xFE70, 0xFEFF, "Arabic Presentation Forms-B" },
{ 68, 0xFF00, 0xFFEF, "Halfwidth And Fullwidth Forms" },
{ 69, 0xFFF0, 0xFFFF, "Specials" },
{ 70, 0x0F00, 0x0FFF, "Tibetan" },
{ 71, 0x0700, 0x074F, "Syriac" },
{ 72, 0x0780, 0x07BF, "Thaana" },
{ 73, 0x0D80, 0x0DFF, "Sinhala" },
{ 74, 0x1000, 0x109F, "Myanmar" },
{ 75, 0x1200, 0x137F, "Ethiopic" },
{ 75, 0x1380, 0x139F, "Ethiopic Supplement" },
{ 75, 0x2D80, 0x2DDF, "Ethiopic Extended" },
{ 76, 0x13A0, 0x13FF, "Cherokee" },
{ 77, 0x1400, 0x167F, "Unified Canadian Aboriginal Syllabics" },
{ 78, 0x1680, 0x169F, "Ogham" },
{ 79, 0x16A0, 0x16FF, "Runic" },
{ 80, 0x1780, 0x17FF, "Khmer" },
{ 80, 0x19E0, 0x19FF, "Khmer Symbols" },
{ 81, 0x1800, 0x18AF, "Mongolian" },
{ 82, 0x2800, 0x28FF, "Braille Patterns" },
{ 83, 0xA000, 0xA48F, "Yi Syllables" },
{ 83, 0xA490, 0xA4CF, "Yi Radicals" },
{ 84, 0x1700, 0x171F, "Tagalog" },
{ 84, 0x1720, 0x173F, "Hanunoo" },
{ 84, 0x1740, 0x175F, "Buhid" },
{ 84, 0x1760, 0x177F, "Tagbanwa" },
{ 85, 0x10300, 0x1032F, "Old Italic" },
{ 86, 0x10330, 0x1034F, "Gothic" },
{ 87, 0x10400, 0x1044F, "Deseret" },
{ 88, 0x1D000, 0x1D0FF, "Byzantine Musical Symbols" },
{ 88, 0x1D100, 0x1D1FF, "Musical Symbols" },
{ 88, 0x1D200, 0x1D24F, "Ancient Greek Musical Notation" },
{ 89, 0x1D400, 0x1D7FF, "Mathematical Alphanumeric Symbols" },
{ 90, 0xFF000, 0xFFFFD, "Private Use (plane 15)" },
{ 90, 0x100000, 0x10FFFD, "Private Use (plane 16)" },
{ 91, 0xFE00, 0xFE0F, "Variation Selectors" },
{ 91, 0xE0100, 0xE01EF, "Variation Selectors Supplement" },
{ 92, 0xE0000, 0xE007F, "Tags" },
{ 93, 0x1900, 0x194F, "Limbu" },
{ 94, 0x1950, 0x197F, "Tai Le" },
{ 95, 0x1980, 0x19DF, "New Tai Lue" },
{ 96, 0x1A00, 0x1A1F, "Buginese" },
{ 97, 0x2C00, 0x2C5F, "Glagolitic" },
{ 98, 0x2D30, 0x2D7F, "Tifinagh" },
{ 99, 0x4DC0, 0x4DFF, "Yijing Hexagram Symbols" },
{ 100, 0xA800, 0xA82F, "Syloti Nagri" },
{ 101, 0x10000, 0x1007F, "Linear B Syllabary" },
{ 101, 0x10080, 0x100FF, "Linear B Ideograms" },
{ 101, 0x10100, 0x1013F, "Aegean Numbers" },
{ 102, 0x10140, 0x1018F, "Ancient Greek Numbers" },
{ 103, 0x10380, 0x1039F, "Ugaritic" },
{ 104, 0x103A0, 0x103DF, "Old Persian" },
{ 105, 0x10450, 0x1047F, "Shavian" },
{ 106, 0x10480, 0x104AF, "Osmanya" },
{ 107, 0x10800, 0x1083F, "Cypriot Syllabary" },
{ 108, 0x10A00, 0x10A5F, "Kharoshthi" },
{ 109, 0x1D300, 0x1D35F, "Tai Xuan Jing Symbols" },
{ 110, 0x12000, 0x123FF, "Cuneiform" },
{ 110, 0x12400, 0x1247F, "Cuneiform Numbers and Punctuation" },
{ 111, 0x1D360, 0x1D37F, "Counting Rod Numerals" },
{ 112, 0x1B80, 0x1BBF, "Sundanese" },
{ 113, 0x1C00, 0x1C4F, "Lepcha" },
{ 114, 0x1C50, 0x1C7F, "Ol Chiki" },
{ 115, 0xA880, 0xA8DF, "Saurashtra" },
{ 116, 0xA900, 0xA92F, "Kayah Li" },
{ 117, 0xA930, 0xA95F, "Rejang" },
{ 118, 0xAA00, 0xAA5F, "Cham" },
{ 119, 0x10190, 0x101CF, "Ancient Symbols" },
{ 120, 0x101D0, 0x101FF, "Phaistos Disc" },
{ 121, 0x10280, 0x1029F, "Lycian" },
{ 121, 0x102A0, 0x102DF, "Carian" },
{ 121, 0x10920, 0x1093F, "Lydian" },
{ 122, 0x1F000, 0x1F02F, "Mahjong Tiles" },
{ 122, 0x1F030, 0x1F09F, "Domino Tiles" }
};
#pragma pack(1)
typedef struct {
AutoSwap_PRUint16 format;
AutoSwap_PRUint16 reserved;
AutoSwap_PRUint32 length;
AutoSwap_PRUint32 language;
AutoSwap_PRUint32 numGroups;
} Format12CmapHeader;
typedef struct {
AutoSwap_PRUint32 startCharCode;
AutoSwap_PRUint32 endCharCode;
AutoSwap_PRUint32 startGlyphId;
} Format12Group;
#pragma pack()
nsresult
gfxFontUtils::ReadCMAPTableFormat12(const PRUint8 *aBuf, PRUint32 aLength,
gfxSparseBitSet& aCharacterMap)
{
// Ensure table is large enough that we can safely read the header
NS_ENSURE_TRUE(aLength >= sizeof(Format12CmapHeader),
NS_ERROR_GFX_CMAP_MALFORMED);
// Sanity-check header fields
const Format12CmapHeader *cmap12 =
reinterpret_cast<const Format12CmapHeader*>(aBuf);
NS_ENSURE_TRUE(PRUint16(cmap12->format) == 12,
NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(PRUint16(cmap12->reserved) == 0,
NS_ERROR_GFX_CMAP_MALFORMED);
PRUint32 tablelen = cmap12->length;
NS_ENSURE_TRUE(tablelen >= sizeof(Format12CmapHeader) &&
tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(cmap12->language == 0, NS_ERROR_GFX_CMAP_MALFORMED);
// Check that the table is large enough for the group array
const PRUint32 numGroups = cmap12->numGroups;
NS_ENSURE_TRUE((tablelen - sizeof(Format12CmapHeader)) /
sizeof(Format12Group) >= numGroups,
NS_ERROR_GFX_CMAP_MALFORMED);
// The array of groups immediately follows the subtable header.
const Format12Group *group =
reinterpret_cast<const Format12Group*>(aBuf + sizeof(Format12CmapHeader));
// Check that groups are in correct order and do not overlap,
// and record character coverage in aCharacterMap.
PRUint32 prevEndCharCode = 0;
for (PRUint32 i = 0; i < numGroups; i++, group++) {
const PRUint32 startCharCode = group->startCharCode;
const PRUint32 endCharCode = group->endCharCode;
NS_ENSURE_TRUE((prevEndCharCode < startCharCode || i == 0) &&
startCharCode <= endCharCode &&
endCharCode <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
aCharacterMap.SetRange(startCharCode, endCharCode);
prevEndCharCode = endCharCode;
}
aCharacterMap.mBlocks.Compact();
return NS_OK;
}
nsresult
gfxFontUtils::ReadCMAPTableFormat4(const PRUint8 *aBuf, PRUint32 aLength,
gfxSparseBitSet& aCharacterMap)
{
enum {
OffsetFormat = 0,
OffsetLength = 2,
OffsetLanguage = 4,
OffsetSegCountX2 = 6
};
NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 4,
NS_ERROR_GFX_CMAP_MALFORMED);
PRUint16 tablelen = ReadShortAt(aBuf, OffsetLength);
NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(tablelen > 16, NS_ERROR_GFX_CMAP_MALFORMED);
// This field should normally (except for Mac platform subtables) be zero according to
// the OT spec, but some buggy fonts have lang = 1 (which would be English for MacOS).
// E.g. Arial Narrow Bold, v. 1.1 (Tiger), Arial Unicode MS (see bug 530614).
// So accept either zero or one here; the error should be harmless.
NS_ENSURE_TRUE((ReadShortAt(aBuf, OffsetLanguage) & 0xfffe) == 0,
NS_ERROR_GFX_CMAP_MALFORMED);
PRUint16 segCountX2 = ReadShortAt(aBuf, OffsetSegCountX2);
NS_ENSURE_TRUE(tablelen >= 16 + (segCountX2 * 4),
NS_ERROR_GFX_CMAP_MALFORMED);
const PRUint16 segCount = segCountX2 / 2;
const PRUint16 *endCounts = reinterpret_cast<const PRUint16*>(aBuf + 14);
const PRUint16 *startCounts = endCounts + 1 /* skip one uint16 for reservedPad */ + segCount;
const PRUint16 *idDeltas = startCounts + segCount;
const PRUint16 *idRangeOffsets = idDeltas + segCount;
PRUint16 prevEndCount = 0;
for (PRUint16 i = 0; i < segCount; i++) {
const PRUint16 endCount = ReadShortAt16(endCounts, i);
const PRUint16 startCount = ReadShortAt16(startCounts, i);
const PRUint16 idRangeOffset = ReadShortAt16(idRangeOffsets, i);
// sanity-check range
NS_ENSURE_TRUE((startCount > prevEndCount || i == 0 || startCount == 0xFFFF) &&
startCount <= endCount,
NS_ERROR_GFX_CMAP_MALFORMED);
prevEndCount = endCount;
if (idRangeOffset == 0) {
aCharacterMap.SetRange(startCount, endCount);
} else {
// const PRUint16 idDelta = ReadShortAt16(idDeltas, i); // Unused: self-documenting.
for (PRUint32 c = startCount; c <= endCount; ++c) {
if (c == 0xFFFF)
break;
const PRUint16 *gdata = (idRangeOffset/2
+ (c - startCount)
+ &idRangeOffsets[i]);
NS_ENSURE_TRUE((PRUint8*)gdata > aBuf &&
(PRUint8*)gdata < aBuf + aLength,
NS_ERROR_GFX_CMAP_MALFORMED);
// make sure we have a glyph
if (*gdata != 0) {
// The glyph index at this point is:
// glyph = (ReadShortAt16(idDeltas, i) + *gdata) % 65536;
aCharacterMap.set(c);
}
}
}
}
aCharacterMap.mBlocks.Compact();
return NS_OK;
}
nsresult
gfxFontUtils::ReadCMAPTableFormat14(const PRUint8 *aBuf, PRUint32 aLength,
PRUint8*& aTable)
{
enum {
OffsetFormat = 0,
OffsetTableLength = 2,
OffsetNumVarSelectorRecords = 6,
OffsetVarSelectorRecords = 10,
SizeOfVarSelectorRecord = 11,
VSRecOffsetVarSelector = 0,
VSRecOffsetDefUVSOffset = 3,
VSRecOffsetNonDefUVSOffset = 7,
SizeOfDefUVSTable = 4,
DefUVSOffsetStartUnicodeValue = 0,
DefUVSOffsetAdditionalCount = 3,
SizeOfNonDefUVSTable = 5,
NonDefUVSOffsetUnicodeValue = 0,
NonDefUVSOffsetGlyphID = 3
};
NS_ENSURE_TRUE(aLength >= OffsetVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(ReadShortAt(aBuf, OffsetFormat) == 14,
NS_ERROR_GFX_CMAP_MALFORMED);
PRUint32 tablelen = ReadLongAt(aBuf, OffsetTableLength);
NS_ENSURE_TRUE(tablelen <= aLength, NS_ERROR_GFX_CMAP_MALFORMED);
NS_ENSURE_TRUE(tablelen >= OffsetVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
const PRUint32 numVarSelectorRecords = ReadLongAt(aBuf, OffsetNumVarSelectorRecords);
NS_ENSURE_TRUE((tablelen - OffsetVarSelectorRecords) /
SizeOfVarSelectorRecord >= numVarSelectorRecords,
NS_ERROR_GFX_CMAP_MALFORMED);
const PRUint8 *records = aBuf + OffsetVarSelectorRecords;
for (PRUint32 i = 0; i < numVarSelectorRecords;
i++, records += SizeOfVarSelectorRecord) {
const PRUint32 varSelector = ReadUint24At(records, VSRecOffsetVarSelector);
const PRUint32 defUVSOffset = ReadLongAt(records, VSRecOffsetDefUVSOffset);
const PRUint32 nonDefUVSOffset = ReadLongAt(records, VSRecOffsetNonDefUVSOffset);
NS_ENSURE_TRUE(varSelector <= CMAP_MAX_CODEPOINT &&
defUVSOffset <= tablelen - 4 &&
nonDefUVSOffset <= tablelen - 4,
NS_ERROR_GFX_CMAP_MALFORMED);
if (defUVSOffset) {
const PRUint32 numUnicodeValueRanges = ReadLongAt(aBuf, defUVSOffset);
NS_ENSURE_TRUE((tablelen - defUVSOffset) /
SizeOfDefUVSTable >= numUnicodeValueRanges,
NS_ERROR_GFX_CMAP_MALFORMED);
const PRUint8 *tables = aBuf + defUVSOffset + 4;
PRUint32 prevEndUnicode = 0;
for (PRUint32 j = 0; j < numUnicodeValueRanges; j++, tables += SizeOfDefUVSTable) {
const PRUint32 startUnicode = ReadUint24At(tables, DefUVSOffsetStartUnicodeValue);
const PRUint32 endUnicode = startUnicode + tables[DefUVSOffsetAdditionalCount];
NS_ENSURE_TRUE((prevEndUnicode < startUnicode || j == 0) &&
endUnicode <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
prevEndUnicode = endUnicode;
}
}
if (nonDefUVSOffset) {
const PRUint32 numUVSMappings = ReadLongAt(aBuf, nonDefUVSOffset);
NS_ENSURE_TRUE((tablelen - nonDefUVSOffset) /
SizeOfNonDefUVSTable >= numUVSMappings,
NS_ERROR_GFX_CMAP_MALFORMED);
const PRUint8 *tables = aBuf + nonDefUVSOffset + 4;
PRUint32 prevUnicode = 0;
for (PRUint32 j = 0; j < numUVSMappings; j++, tables += SizeOfNonDefUVSTable) {
const PRUint32 unicodeValue = ReadUint24At(tables, NonDefUVSOffsetUnicodeValue);
NS_ENSURE_TRUE((prevUnicode < unicodeValue || j == 0) &&
unicodeValue <= CMAP_MAX_CODEPOINT,
NS_ERROR_GFX_CMAP_MALFORMED);
prevUnicode = unicodeValue;
}
}
}
aTable = new PRUint8[tablelen];
memcpy(aTable, aBuf, tablelen);
return NS_OK;
}
// Windows requires fonts to have a format-4 cmap with a Microsoft ID (3). On the Mac, fonts either have
// a format-4 cmap with Microsoft platform/encoding id or they have one with a platformID == Unicode (0)
// For fonts with two format-4 tables, the first one (Unicode platform) is preferred on the Mac.
#if defined(XP_MACOSX)
#define acceptableFormat4(p,e,k) (((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft && !(k)) || \
((p) == PLATFORM_ID_UNICODE))
#define acceptableUCS4Encoding(p, e, k) \
(((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform) && (k) != 12 || \
((p) == PLATFORM_ID_UNICODE && \
((e) == EncodingIDDefaultForUnicodePlatform || (e) >= EncodingIDUCS4ForUnicodePlatform)))
#else
#define acceptableFormat4(p,e,k) ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDMicrosoft)
#define acceptableUCS4Encoding(p, e, k) \
((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDUCS4ForMicrosoftPlatform)
#endif
#define acceptablePlatform(p) ((p) == PLATFORM_ID_UNICODE || (p) == PLATFORM_ID_MICROSOFT)
#define isSymbol(p,e) ((p) == PLATFORM_ID_MICROSOFT && (e) == EncodingIDSymbol)
#define isUVSEncoding(p, e) ((p) == PLATFORM_ID_UNICODE && (e) == EncodingIDUVSForUnicodePlatform)
PRUint32
gfxFontUtils::FindPreferredSubtable(const PRUint8 *aBuf, PRUint32 aBufLength,
PRUint32 *aTableOffset,
PRUint32 *aUVSTableOffset,
PRBool *aSymbolEncoding)
{
enum {
OffsetVersion = 0,
OffsetNumTables = 2,
SizeOfHeader = 4,
TableOffsetPlatformID = 0,
TableOffsetEncodingID = 2,
TableOffsetOffset = 4,
SizeOfTable = 8,
SubtableOffsetFormat = 0
};
enum {
EncodingIDSymbol = 0,
EncodingIDMicrosoft = 1,
EncodingIDDefaultForUnicodePlatform = 0,
EncodingIDUCS4ForUnicodePlatform = 3,
EncodingIDUVSForUnicodePlatform = 5,
EncodingIDUCS4ForMicrosoftPlatform = 10
};
if (aUVSTableOffset) {
*aUVSTableOffset = nsnull;
}
if (!aBuf || aBufLength < SizeOfHeader) {
// cmap table is missing, or too small to contain header fields!
return 0;
}
// PRUint16 version = ReadShortAt(aBuf, OffsetVersion); // Unused: self-documenting.
PRUint16 numTables = ReadShortAt(aBuf, OffsetNumTables);
if (aBufLength < PRUint32(SizeOfHeader + numTables * SizeOfTable)) {
return 0;
}
// save the format we want here
PRUint32 keepFormat = 0;
const PRUint8 *table = aBuf + SizeOfHeader;
for (PRUint16 i = 0; i < numTables; ++i, table += SizeOfTable) {
const PRUint16 platformID = ReadShortAt(table, TableOffsetPlatformID);
if (!acceptablePlatform(platformID))
continue;
const PRUint16 encodingID = ReadShortAt(table, TableOffsetEncodingID);
const PRUint32 offset = ReadLongAt(table, TableOffsetOffset);
if (aBufLength - 2 < offset) {
// this subtable is not valid - beyond end of buffer
return 0;
}
const PRUint8 *subtable = aBuf + offset;
const PRUint16 format = ReadShortAt(subtable, SubtableOffsetFormat);
if (isSymbol(platformID, encodingID)) {
keepFormat = format;
*aTableOffset = offset;
*aSymbolEncoding = PR_TRUE;
break;
} else if (format == 4 && acceptableFormat4(platformID, encodingID, keepFormat)) {
keepFormat = format;
*aTableOffset = offset;
*aSymbolEncoding = PR_FALSE;
} else if (format == 12 && acceptableUCS4Encoding(platformID, encodingID, keepFormat)) {
keepFormat = format;
*aTableOffset = offset;
*aSymbolEncoding = PR_FALSE;
if (platformID > PLATFORM_ID_UNICODE || !aUVSTableOffset || *aUVSTableOffset) {
break; // we don't want to try anything else when this format is available.
}
} else if (format == 14 && isUVSEncoding(platformID, encodingID) && aUVSTableOffset) {
*aUVSTableOffset = offset;
if (keepFormat == 12) {
break;
}
}
}
return keepFormat;
}
nsresult
gfxFontUtils::ReadCMAP(const PRUint8 *aBuf, PRUint32 aBufLength,
gfxSparseBitSet& aCharacterMap,
PRUint32& aUVSOffset,
PRPackedBool& aUnicodeFont, PRPackedBool& aSymbolFont)
{
PRUint32 offset;
PRBool symbol;
PRUint32 format = FindPreferredSubtable(aBuf, aBufLength,
&offset, &aUVSOffset, &symbol);
if (format == 4) {
if (symbol) {
aUnicodeFont = PR_FALSE;
aSymbolFont = PR_TRUE;
} else {
aUnicodeFont = PR_TRUE;
aSymbolFont = PR_FALSE;
}
return ReadCMAPTableFormat4(aBuf + offset, aBufLength - offset,
aCharacterMap);
}
if (format == 12) {
aUnicodeFont = PR_TRUE;
aSymbolFont = PR_FALSE;
return ReadCMAPTableFormat12(aBuf + offset, aBufLength - offset,
aCharacterMap);
}
return NS_ERROR_FAILURE;
}
#pragma pack(1)
typedef struct {
AutoSwap_PRUint16 format;
AutoSwap_PRUint16 length;
AutoSwap_PRUint16 language;
AutoSwap_PRUint16 segCountX2;
AutoSwap_PRUint16 searchRange;
AutoSwap_PRUint16 entrySelector;
AutoSwap_PRUint16 rangeShift;
AutoSwap_PRUint16 arrays[1];
} Format4Cmap;
typedef struct {
AutoSwap_PRUint16 format;
AutoSwap_PRUint32 length;
AutoSwap_PRUint32 numVarSelectorRecords;
typedef struct {
AutoSwap_PRUint24 varSelector;
AutoSwap_PRUint32 defaultUVSOffset;
AutoSwap_PRUint32 nonDefaultUVSOffset;
} VarSelectorRecord;
VarSelectorRecord varSelectorRecords[1];
} Format14Cmap;
typedef struct {
AutoSwap_PRUint32 numUVSMappings;
typedef struct {
AutoSwap_PRUint24 unicodeValue;
AutoSwap_PRUint16 glyphID;
} UVSMapping;
UVSMapping uvsMappings[1];
} NonDefUVSTable;
#pragma pack()
PRUint32
gfxFontUtils::MapCharToGlyphFormat4(const PRUint8 *aBuf, PRUnichar aCh)
{
const Format4Cmap *cmap4 = reinterpret_cast<const Format4Cmap*>(aBuf);
PRUint16 segCount;
const AutoSwap_PRUint16 *endCodes;
const AutoSwap_PRUint16 *startCodes;
const AutoSwap_PRUint16 *idDelta;
const AutoSwap_PRUint16 *idRangeOffset;
PRUint16 probe;
PRUint16 rangeShiftOver2;
PRUint16 index;
segCount = (PRUint16)(cmap4->segCountX2) / 2;
endCodes = &cmap4->arrays[0];
startCodes = &cmap4->arrays[segCount + 1]; // +1 for reserved word between arrays
idDelta = &startCodes[segCount];
idRangeOffset = &idDelta[segCount];
probe = 1 << (PRUint16)(cmap4->entrySelector);
rangeShiftOver2 = (PRUint16)(cmap4->rangeShift) / 2;
if ((PRUint16)(startCodes[rangeShiftOver2]) <= aCh) {
index = rangeShiftOver2;
} else {
index = 0;
}
while (probe > 1) {
probe >>= 1;
if ((PRUint16)(startCodes[index + probe]) <= aCh) {
index += probe;
}
}
if (aCh >= (PRUint16)(startCodes[index]) && aCh <= (PRUint16)(endCodes[index])) {
PRUint16 result;
if ((PRUint16)(idRangeOffset[index]) == 0) {
result = aCh;
} else {
PRUint16 offset = aCh - (PRUint16)(startCodes[index]);
const AutoSwap_PRUint16 *glyphIndexTable =
(const AutoSwap_PRUint16*)((const char*)&idRangeOffset[index] +
(PRUint16)(idRangeOffset[index]));
result = glyphIndexTable[offset];
}
// note that this is unsigned 16-bit arithmetic, and may wrap around
result += (PRUint16)(idDelta[index]);
return result;
}
return 0;
}
PRUint32
gfxFontUtils::MapCharToGlyphFormat12(const PRUint8 *aBuf, PRUint32 aCh)
{
const Format12CmapHeader *cmap12 =
reinterpret_cast<const Format12CmapHeader*>(aBuf);
// We know that numGroups is within range for the subtable size
// because it was checked by ReadCMAPTableFormat12.
PRUint32 numGroups = cmap12->numGroups;
// The array of groups immediately follows the subtable header.
const Format12Group *groups =
reinterpret_cast<const Format12Group*>(aBuf + sizeof(Format12CmapHeader));
// For most efficient binary search, we want to work on a range that
// is a power of 2 so that we can always halve it by shifting.
// So we find the largest power of 2 that is <= numGroups.
// We will offset this range by rangeOffset so as to reach the end
// of the table, provided that doesn't put us beyond the target
// value from the outset.
PRUint32 powerOf2 = mozilla::FindHighestBit(numGroups);
PRUint32 rangeOffset = numGroups - powerOf2;
PRUint32 range = 0;
PRUint32 startCharCode;
if (groups[rangeOffset].startCharCode <= aCh) {
range = rangeOffset;
}
// Repeatedly halve the size of the range until we find the target group
while (powerOf2 > 1) {
powerOf2 >>= 1;
if (groups[range + powerOf2].startCharCode <= aCh) {
range += powerOf2;
}
}
// Check if the character is actually present in the range and return
// the corresponding glyph ID
startCharCode = groups[range].startCharCode;
if (startCharCode <= aCh && groups[range].endCharCode >= aCh) {
return groups[range].startGlyphId + aCh - startCharCode;
}
// Else it's not present, so return the .notdef glyph
return 0;
}
PRUint16
gfxFontUtils::MapUVSToGlyphFormat14(const PRUint8 *aBuf, PRUint32 aCh, PRUint32 aVS)
{
const Format14Cmap *cmap14 = reinterpret_cast<const Format14Cmap*>(aBuf);
// binary search in varSelectorRecords
PRUint32 min = 0;
PRUint32 max = cmap14->numVarSelectorRecords;
PRUint32 nonDefUVSOffset = 0;
while (min < max) {
PRUint32 index = (min + max) >> 1;
PRUint32 varSelector = cmap14->varSelectorRecords[index].varSelector;
if (aVS == varSelector) {
nonDefUVSOffset = cmap14->varSelectorRecords[index].nonDefaultUVSOffset;
break;
}
if (aVS < varSelector) {
max = index;
} else {
min = index + 1;
}
}
if (!nonDefUVSOffset) {
return 0;
}
const NonDefUVSTable *table = reinterpret_cast<const NonDefUVSTable*>
(aBuf + nonDefUVSOffset);
// binary search in uvsMappings
min = 0;
max = table->numUVSMappings;
while (min < max) {
PRUint32 index = (min + max) >> 1;
PRUint32 unicodeValue = table->uvsMappings[index].unicodeValue;
if (aCh == unicodeValue) {
return table->uvsMappings[index].glyphID;
}
if (aCh < unicodeValue) {
max = index;
} else {
min = index + 1;
}
}
return 0;
}
PRUint32
gfxFontUtils::MapCharToGlyph(const PRUint8 *aBuf, PRUint32 aBufLength,
PRUint32 aCh)
{
PRUint32 offset;
PRBool symbol;
PRUint32 format = FindPreferredSubtable(aBuf, aBufLength, &offset,
nsnull, &symbol);
switch (format) {
case 4:
return aCh < UNICODE_BMP_LIMIT ?
MapCharToGlyphFormat4(aBuf + offset, PRUnichar(aCh)) : 0;
case 12:
return MapCharToGlyphFormat12(aBuf + offset, aCh);
default:
return 0;
}
}
PRUint8 gfxFontUtils::CharRangeBit(PRUint32 ch) {
const PRUint32 n = sizeof(gUnicodeRanges) / sizeof(struct UnicodeRangeTableEntry);
for (PRUint32 i = 0; i < n; ++i)
if (ch >= gUnicodeRanges[i].start && ch <= gUnicodeRanges[i].end)
return gUnicodeRanges[i].bit;
return NO_RANGE_FOUND;
}
void gfxFontUtils::GetPrefsFontList(const char *aPrefName, nsTArray<nsString>& aFontList)
{
const PRUnichar kComma = PRUnichar(',');
aFontList.Clear();
// get the list of single-face font families
nsAdoptingString fontlistValue = Preferences::GetString(aPrefName);
if (!fontlistValue) {
return;
}
// append each font name to the list
nsAutoString fontname;
const PRUnichar *p, *p_end;
fontlistValue.BeginReading(p);
fontlistValue.EndReading(p_end);
while (p < p_end) {
const PRUnichar *nameStart = p;
while (++p != p_end && *p != kComma)
/* nothing */ ;
// pull out a single name and clean out leading/trailing whitespace
fontname = Substring(nameStart, p);
fontname.CompressWhitespace(PR_TRUE, PR_TRUE);
// append it to the list
aFontList.AppendElement(fontname);
++p;
}
}
// produce a unique font name that is (1) a valid Postscript name and (2) less
// than 31 characters in length. Using AddFontMemResourceEx on Windows fails
// for names longer than 30 characters in length.
#define MAX_B64_LEN 32
nsresult gfxFontUtils::MakeUniqueUserFontName(nsAString& aName)
{
nsCOMPtr<nsIUUIDGenerator> uuidgen =
do_GetService("@mozilla.org/uuid-generator;1");
NS_ENSURE_TRUE(uuidgen, NS_ERROR_OUT_OF_MEMORY);
nsID guid;
NS_ASSERTION(sizeof(guid) * 2 <= MAX_B64_LEN, "size of nsID has changed!");
nsresult rv = uuidgen->GenerateUUIDInPlace(&guid);
NS_ENSURE_SUCCESS(rv, rv);
char guidB64[MAX_B64_LEN] = {0};
if (!PL_Base64Encode(reinterpret_cast<char*>(&guid), sizeof(guid), guidB64))
return NS_ERROR_FAILURE;
// all b64 characters except for '/' are allowed in Postscript names, so convert / ==> -
char *p;
for (p = guidB64; *p; p++) {
if (*p == '/')
*p = '-';
}
aName.Assign(NS_LITERAL_STRING("uf"));
aName.AppendASCII(guidB64);
return NS_OK;
}
// TrueType/OpenType table handling code
// need byte aligned structs
#pragma pack(1)
// name table stores set of name record structures, followed by
// large block containing all the strings. name record offset and length
// indicates the offset and length within that block.
// http://www.microsoft.com/typography/otspec/name.htm
struct NameRecordData {
PRUint32 offset;
PRUint32 length;
};
#pragma pack()
static PRBool
IsValidSFNTVersion(PRUint32 version)
{
// normally 0x00010000, CFF-style OT fonts == 'OTTO' and Apple TT fonts = 'true'
// 'typ1' is also possible for old Type 1 fonts in a SFNT container but not supported
return version == 0x10000 ||
version == TRUETYPE_TAG('O','T','T','O') ||
version == TRUETYPE_TAG('t','r','u','e');
}
// copy and swap UTF-16 values, assume no surrogate pairs, can be in place
static void
CopySwapUTF16(const PRUint16 *aInBuf, PRUint16 *aOutBuf, PRUint32 aLen)
{
const PRUint16 *end = aInBuf + aLen;
while (aInBuf < end) {
PRUint16 value = *aInBuf;
*aOutBuf = (value >> 8) | (value & 0xff) << 8;
aOutBuf++;
aInBuf++;
}
}
static PRBool
ValidateKernTable(const PRUint8 *aKernTable, PRUint32 aKernLength)
{
// -- kern table can cause crashes if invalid, so do some basic sanity-checking
const KernTableVersion0 *kernTable0 = reinterpret_cast<const KernTableVersion0*>(aKernTable);
if (aKernLength < sizeof(KernTableVersion0)) {
return PR_FALSE;
}
if (PRUint16(kernTable0->version) == 0) {
if (aKernLength < sizeof(KernTableVersion0) +
PRUint16(kernTable0->nTables) * sizeof(KernTableSubtableHeaderVersion0)) {
return PR_FALSE;
}
// at least the table is big enough to contain the subtable headers;
// we could go further and check the actual subtable sizes....
// for now, assume this is OK
return PR_TRUE;
}
const KernTableVersion1 *kernTable1 = reinterpret_cast<const KernTableVersion1*>(aKernTable);
if (aKernLength < sizeof(KernTableVersion1)) {
return PR_FALSE;
}
if (kernTable1->version == 0x00010000) {
if (aKernLength < sizeof(KernTableVersion1) +
kernTable1->nTables * sizeof(KernTableSubtableHeaderVersion1)) {
return PR_FALSE;
}
// at least the table is big enough to contain the subtable headers;
// we could go further and check the actual subtable sizes....
// for now, assume this is OK
return PR_TRUE;
}
// neither the old Windows version nor the newer Apple one; refuse to use it