forked from mcneel/opennurbs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopennurbs_freetype.cpp
2242 lines (1926 loc) · 65.9 KB
/
opennurbs_freetype.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
//
// Copyright (c) 1993-2022 Robert McNeel & Associates. All rights reserved.
// OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
// McNeel & Associates.
//
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
// MERCHANTABILITY ARE HEREBY DISCLAIMED.
//
// For complete openNURBS copyright information see <http://www.opennurbs.org>.
//
////////////////////////////////////////////////////////////////
#include "opennurbs.h"
#if !defined(ON_COMPILING_OPENNURBS)
// This check is included in all opennurbs source .c and .cpp files to insure
// ON_COMPILING_OPENNURBS is defined when opennurbs source is compiled.
// When opennurbs source is being compiled, ON_COMPILING_OPENNURBS is defined
// and the opennurbs .h files alter what is declared and how it is declared.
#error ON_COMPILING_OPENNURBS must be defined when compiling opennurbs
#endif
#if defined(OPENNURBS_FREETYPE_SUPPORT)
// Look in opennurbs_system_rumtime.h for the correct place to define OPENNURBS_FREETYPE_SUPPORT.
// Do NOT define OPENNURBS_FREETYPE_SUPPORT here or in your project setting ("makefile").
#include "opennurbs_internal_glyph.h"
// opennurbs uses FreeType to calculate font metric, glyph metric, and glyph outline information.
// FreeType Licensing:
//
//// Retrieved March 22, 2017
//// https://www.freetype.org/freetype2/docs/index.html
////What is FreeType?
////
////FreeType is a software font engine that is designed to be small, efficient,
////highly customizable, and portable while capable of producing high-quality
////output (glyph images). It can be used in graphics libraries, display servers,
////font conversion tools, text image generation tools, and many other products as well.
////
////Note that FreeType is a font service and doesn't provide APIs to perform
////higher-level features like text layout or graphics processing
////(e.g., colored text rendering, ‘hollowing’, etc.). However, it greatly
////simplifies these tasks by providing a simple, easy to use, and uniform
////interface to access the content of font files.
////
////FreeType is released under two open-source licenses: our own BSD-like
////FreeType License and the GNU Public License, Version 2. It can thus
////be used by any kind of projects, be they proprietary or not.
////
////Please note that ‘FreeType’ is also called ‘FreeType 2’, to
////distinguish it from the old, deprecated ‘FreeType 1’ library,
////a predecessor no longer maintained and supported.
////
//// http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT
////
//// The FreeType Project LICENSE
//// ----------------------------
////
//// 2006-Jan-27
////
//// Copyright 1996-2002, 2006 by
//// David Turner, Robert Wilhelm, and Werner Lemberg
////
////
////
////Introduction
////============
////
//// The FreeType Project is distributed in several archive packages;
//// some of them may contain, in addition to the FreeType font engine,
//// various tools and contributions which rely on, or relate to, the
//// FreeType Project.
////
//// This license applies to all files found in such packages, and
//// which do not fall under their own explicit license. The license
//// affects thus the FreeType font engine, the test programs,
//// documentation and makefiles, at the very least.
////
//// This license was inspired by the BSD, Artistic, and IJG
//// (Independent JPEG Group) licenses, which all encourage inclusion
//// and use of free software in commercial and freeware products
//// alike. As a consequence, its main points are that:
////
//// o We don't promise that this software works. However, we will be
//// interested in any kind of bug reports. (`as is' distribution)
////
//// o You can use this software for whatever you want, in parts or
//// full form, without having to pay us. (`royalty-free' usage)
////
//// o You may not pretend that you wrote this software. If you use
//// it, or only parts of it, in a program, you must acknowledge
//// somewhere in your documentation that you have used the
//// FreeType code. (`credits')
////
//// We specifically permit and encourage the inclusion of this
//// software, with or without modifications, in commercial products.
//// We disclaim all warranties covering The FreeType Project and
//// assume no liability related to The FreeType Project.
////
////
//// Finally, many people asked us for a preferred form for a
//// credit/disclaimer to use in compliance with this license. We thus
//// encourage you to use the following text:
////
//// """
//// Portions of this software are copyright © <year> The FreeType
//// Project (www.freetype.org). All rights reserved.
//// """
////
//// Please replace <year> with the value from the FreeType version you
//// actually use.
////
////
////Legal Terms
////===========
////
////0. Definitions
////--------------
////
//// Throughout this license, the terms `package', `FreeType Project',
//// and `FreeType archive' refer to the set of files originally
//// distributed by the authors (David Turner, Robert Wilhelm, and
//// Werner Lemberg) as the `FreeType Project', be they named as alpha,
//// beta or final release.
////
//// `You' refers to the licensee, or person using the project, where
//// `using' is a generic term including compiling the project's source
//// code as well as linking it to form a `program' or `executable'.
//// This program is referred to as `a program using the FreeType
//// engine'.
////
//// This license applies to all files distributed in the original
//// FreeType Project, including all source code, binaries and
//// documentation, unless otherwise stated in the file in its
//// original, unmodified form as distributed in the original archive.
//// If you are unsure whether or not a particular file is covered by
//// this license, you must contact us to verify this.
////
//// The FreeType Project is copyright (C) 1996-2000 by David Turner,
//// Robert Wilhelm, and Werner Lemberg. All rights reserved except as
//// specified below.
////
////1. No Warranty
////--------------
////
//// THE FREETYPE PROJECT IS PROVIDED `AS IS' WITHOUT WARRANTY OF ANY
//// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
//// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//// PURPOSE. IN NO EVENT WILL ANY OF THE AUTHORS OR COPYRIGHT HOLDERS
//// BE LIABLE FOR ANY DAMAGES CAUSED BY THE USE OR THE INABILITY TO
//// USE, OF THE FREETYPE PROJECT.
////
////2. Redistribution
////-----------------
////
//// This license grants a worldwide, royalty-free, perpetual and
//// irrevocable right and license to use, execute, perform, compile,
//// display, copy, create derivative works of, distribute and
//// sublicense the FreeType Project (in both source and object code
//// forms) and derivative works thereof for any purpose; and to
//// authorize others to exercise some or all of the rights granted
//// herein, subject to the following conditions:
////
//// o Redistribution of source code must retain this license file
//// (`FTL.TXT') unaltered; any additions, deletions or changes to
//// the original files must be clearly indicated in accompanying
//// documentation. The copyright notices of the unaltered,
//// original files must be preserved in all copies of source
//// files.
////
//// o Redistribution in binary form must provide a disclaimer that
//// states that the software is based in part of the work of the
//// FreeType Team, in the distribution documentation. We also
//// encourage you to put an URL to the FreeType web page in your
//// documentation, though this isn't mandatory.
////
//// These conditions apply to any software derived from or based on
//// the FreeType Project, not just the unmodified files. If you use
//// our work, you must acknowledge us. However, no fee need be paid
//// to us.
////
////3. Advertising
////--------------
////
//// Neither the FreeType authors and contributors nor you shall use
//// the name of the other for commercial, advertising, or promotional
//// purposes without specific prior written permission.
////
//// We suggest, but do not require, that you use one or more of the
//// following phrases to refer to this software in your documentation
//// or advertising materials: `FreeType Project', `FreeType Engine',
//// `FreeType library', or `FreeType Distribution'.
////
//// As you have not signed this license, you are not required to
//// accept it. However, as the FreeType Project is copyrighted
//// material, only this license, or another one contracted with the
//// authors, grants you the right to use, distribute, and modify it.
//// Therefore, by using, distributing, or modifying the FreeType
//// Project, you indicate that you understand and accept all the terms
//// of this license.
////
////4. Contacts
////-----------
////
//// There are two mailing lists related to FreeType:
////
//// o [email protected]
////
//// Discusses general use and applications of FreeType, as well as
//// future and wanted additions to the library and distribution.
//// If you are looking for support, start in this list if you
//// haven't found anything to help you in the documentation.
////
//// o [email protected]
////
//// Discusses bugs, as well as engine internals, design issues,
//// specific licenses, porting, etc.
////
//// Our home page can be found at
////
//// http://www.freetype.org
////
////--- end of FTL.TXT ---
// There is a compiler option for this file, opennurbs_freetype.cpp, that
// adds ./freetype263/include to the list of "system" include paths.
#include "opennurbs_freetype_include.h"
#if defined(ON_COMPILER_MSC)
#pragma ON_PRAGMA_WARNING_PUSH
/*
Suppress 4263 warnings from dwrite.h
Warning C4263 ..: member function does not override any base class virtual member function ... C:\Program Files (x86)\Windows Kits\8.1\Include\um\DWrite.h ...
Warning C4263 ..: member function does not override any base class virtual member function ... C:\Program Files (x86)\Windows Kits\8.1\Include\um\DWrite_1.h ...
Warning C4263 ..: member function does not override any base class virtual member function ... C:\Program Files (x86)\Windows Kits\8.1\Include\um\dwrite_2.h ...
*/
#pragma ON_PRAGMA_WARNING_DISABLE_MSC( 4263 4264 )
#endif
#include FT_OUTLINE_H
#include FT_GLYPH_H
#include FT_MODULE_H
#include "opennurbs_win_dwrite.h"
#if defined(ON_COMPILER_MSC)
#pragma ON_PRAGMA_WARNING_POP
#endif
#if defined(ON_RUNTIME_ANDROID)
#include <android/font.h>
#include <android/font_matcher.h>
#include <android/system_fonts.h>
#endif
class ON_FontFileBuffer
{
public:
ON_FontFileBuffer() = default;
~ON_FontFileBuffer()
{
Internal_Destroy();
}
ON_FontFileBuffer(const ON_FontFileBuffer& src)
{
Internal_CopyFrom(src);
}
ON_FontFileBuffer& operator=(const ON_FontFileBuffer& src)
{
Internal_CopyFrom(src);
return *this;
}
void* Buffer() const
{
return m_buffer;
}
void* AllocateBuffer(size_t sizeof_buffer)
{
if (sizeof_buffer != m_sizeof_buffer)
{
Internal_Destroy();
if (sizeof_buffer > 0)
{
m_buffer = onmalloc(sizeof_buffer);
if (nullptr != m_buffer)
{
m_sizeof_buffer = sizeof_buffer;
}
}
}
return m_buffer;
}
size_t SizeOfBuffer() const
{
return m_sizeof_buffer;
}
void TransferTo(
ON_FontFileBuffer& dest
)
{
if (this != &dest)
{
dest.Internal_Destroy();
dest.m_buffer = m_buffer;
m_buffer = nullptr;
dest.m_sizeof_buffer = m_sizeof_buffer;
m_sizeof_buffer = 0;
}
}
private:
size_t m_sizeof_buffer = 0;
void* m_buffer = nullptr;
private:
void Internal_CopyFrom(const ON_FontFileBuffer& src)
{
AllocateBuffer(src.m_sizeof_buffer);
if (nullptr != m_buffer)
{
memcpy(m_buffer, src.m_buffer, m_sizeof_buffer);
}
}
void Internal_Destroy()
{
if (nullptr != m_buffer)
{
onfree(m_buffer);
m_buffer = nullptr;
}
m_sizeof_buffer = 0;
}
};
class ON_FreeTypeFace
{
public:
ON_FreeTypeFace() = default;
~ON_FreeTypeFace();
FT_Face m_face = nullptr;
ON_FontFileBuffer m_font_buffer;
private:
ON_FreeTypeFace(const ON_FreeTypeFace&) = delete;
ON_FreeTypeFace& operator=(const ON_FreeTypeFace&) = delete;
};
ON_FreeTypeFace::~ON_FreeTypeFace()
{
// FT_New_Memory_Face documentation states:
// You must not deallocate the memory before calling @FT_Done_Face.
// The memory refered to is m_tt_file_buffer.
if (nullptr != m_face)
{
FT_Done_Face(m_face);
m_face = nullptr;
}
m_font_buffer.AllocateBuffer(0);
}
class ON_FreeType
{
public:
static FT_Library Library();
static ON_FreeTypeFace* CreateFace(
const ON_Font& font
);
/*
Description:
Finds the glyph id for the specified Unicode code point. When a non-zero
glyph is is returned, the face->charmap is set to the charmap that was used
to find the glyph. In some cases this is not a Unicode charmap and unicode_code_point
was internally coverted to a character code appropriate for the returned charmap.
In principle, the glyph id for a Unicode code point is independent of the charmap.
Returns:
0: failure
>0: font glyph id
*/
static unsigned int GlyphId(
FT_Face face,
ON__UINT32 unicode_code_point
);
static const ON_wString FaceFlagsToString(FT_Long face_flags);
static const ON_wString StyleFlagsToString(FT_Long style_flags);
static const ON_wString EncodingTypeToString(
FT_Encoding charmap_encoding
);
static const ON_wString CharmapPlatformEncodingDescription(
const FT_CharMap cmap
);
static bool UseUnicodeAsAppleRomanCharCode(
FT_Face face
);
static bool IsDamagedCharMap(
FT_CharMap cmap
);
private:
static FT_MemoryRec_ m_memory_rec;
static FT_Library m_library;
private:
#if defined(ON_RUNTIME_WIN)
static ON_FreeTypeFace* Internal_CreateFaceFromWindowsFont(
const LOGFONT* logfont
);
#endif
#if defined (ON_RUNTIME_COCOA_AVAILABLE)
static ON_FreeTypeFace* Internal_CreateFaceFromAppleFont(CTFontRef);
#endif
#if defined (ON_RUNTIME_ANDROID)
static ON_FreeTypeFace* Internal_CreateFaceWithAndroidNdk(const ON_Font& font);
#endif
};
FT_MemoryRec_ ON_FreeType::m_memory_rec;
FT_Library ON_FreeType::m_library = nullptr;
static void* ON_Internal_FT_Alloc_Func(
FT_Memory memory,
long size)
{
void* ptr = onmalloc(size);
return ptr;
}
void ON_Internal_FT_Free_Func(
FT_Memory memory,
void* block
)
{
onfree(block);
}
void* ON_InternalFT_Realloc_Func(
FT_Memory memory,
long cur_size,
long new_size,
void* block
)
{
void* ptr = onrealloc(block, new_size);
return ptr;
}
FT_Library ON_FreeType::Library()
{
if (nullptr == ON_FreeType::m_library)
{
#if 1
// The only reason a custom memory allocator is used
// is so memory allocated by freetype is not flagged
// as a leak because it is used in the cached fonts
// ON_Font::ManagedFont(). These are created as needed
// one per face and never deleted.
memset(&ON_FreeType::m_memory_rec, 0, sizeof(ON_FreeType::m_memory_rec));
ON_FreeType::m_memory_rec.user = nullptr;
ON_FreeType::m_memory_rec.alloc = ON_Internal_FT_Alloc_Func;
ON_FreeType::m_memory_rec.free = ON_Internal_FT_Free_Func;
ON_FreeType::m_memory_rec.realloc = ON_InternalFT_Realloc_Func;
FT_Library library = nullptr;
int rc = FT_New_Library(&ON_FreeType::m_memory_rec, &library);
if ( 0 == rc && nullptr != library )
FT_Add_Default_Modules( library );
#else
// Works fin except freetype cached information is flagged as a memory leak
// int rc = FT_Init_FreeType(&library);
#endif
if (0 != rc || nullptr == library)
{
ON_ERROR("FreeType FT_Init_FreeType() failed.");
}
else
{
ON_FreeType::m_library = library;
}
}
return ON_FreeType::m_library;
}
bool ON_FreeType::IsDamagedCharMap(
FT_CharMap cmap
)
{
if (nullptr == cmap || nullptr == cmap->face)
return true;
bool rc = false;
switch (cmap->encoding)
{
case FT_ENCODING_APPLE_ROMAN:
rc = ON_FreeType::UseUnicodeAsAppleRomanCharCode(cmap->face);
break;
default:
break;
}
return rc;
}
#define ON__FLAG_TO_STR(flag,i,v,s) do {if ( flag == (i & flag) ) {if (s.IsNotEmpty()) s += L", "; s += v; }} while(false)
const ON_wString ON_FreeType::FaceFlagsToString(FT_Long face_flags)
{
ON_wString s;
ON__FLAG_TO_STR(FT_FACE_FLAG_SCALABLE, face_flags, L"SCALABLE", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_FIXED_SIZES, face_flags, L"FIXED_SIZES", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_FIXED_WIDTH, face_flags, L"FIXED_WIDTH", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_SFNT, face_flags, L"SFNT", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_HORIZONTAL, face_flags, L"HORIZONTAL", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_VERTICAL, face_flags, L"VERTICAL", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_KERNING, face_flags, L"KERNING", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_FAST_GLYPHS, face_flags, L"FAST_GLYPHS", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_MULTIPLE_MASTERS, face_flags, L"MULTIPLE_MASTERS", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_GLYPH_NAMES, face_flags, L"GLYPH_NAMES", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_EXTERNAL_STREAM, face_flags, L"EXTERNAL_STREAM", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_HINTER, face_flags, L"FLAG_HINTER", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_CID_KEYED, face_flags, L"CID_KEYED", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_TRICKY, face_flags, L"FLAG_TRICKY", s);
ON__FLAG_TO_STR(FT_FACE_FLAG_COLOR, face_flags, L"FLAG_COLOR", s);
return s;
}
const ON_wString ON_FreeType::StyleFlagsToString(FT_Long style_flags)
{
ON_wString s;
ON__FLAG_TO_STR(FT_STYLE_FLAG_BOLD, style_flags, L"BOLD", s);
ON__FLAG_TO_STR(FT_STYLE_FLAG_ITALIC, style_flags, L"ITALIC", s);
return s;
}
bool ON_FreeType::UseUnicodeAsAppleRomanCharCode(FT_Face face)
{
// It's not clear if the bug is in these font files or Windows or Freetype or
// the way we are selecting maps, but the fonts listed below need this hack to
// find the correct glyph and other fonts don't.
//
// The mighty internet contains mentions of other people having trouble with these fonts as well.
//
// It appears that for these fonts, passing a Unicode code point value
// to the cmap[] idenfied as "FT_ENCODING_APPLE_ROMAN" will get the correct
// glyph id.
//
// I have verified that the way opennurbs handles FT_ENCODING_APPLE_ROMAN charmaps
// and the funciton ON_MapUnicodeToAppleRoman() works correctly with all the
// TTF fonts shipped with Windows 10 pro.
//
if (
nullptr != face
&& 1 == face->num_faces
&& 3 == face->num_charmaps
&& nullptr != face->charmaps[0]
&& nullptr != face->charmaps[1]
&& nullptr != face->charmaps[2]
&& 0 == face->charmaps[0]->platform_id && 0 == face->charmaps[0]->encoding_id
&& 1 == face->charmaps[1]->platform_id && 0 == face->charmaps[1]->encoding_id
&& 3 == face->charmaps[2]->platform_id && 0 == face->charmaps[2]->encoding_id
)
{
// May 2017 Dale Lear:
//
// Fonts we've found where FT_ENCODING_APPLE_ROMAN charmap does not work correctly
// with Apple Roman char codes.
//
// Linguist's Software CityBlueprint 2.0 generated with Altsys Fontographer 4.1 9/17/96
// Linguist's Software CountryBlueprint 2.0 generated with Altsys Fontographer 4.1 9/17/96
// Linguist's Software Romantic 2.0 generated with Altsys Fontographer 4.1 9/17/96
// Linguist's Software SansSerif 2.0 generated with Altsys Fontographer 4.1 9/17/96
// Linguist's Software Technic 2.0 generated with Altsys Fontographer 4.1 9/17/96
//
// So far, all styles with these face names have the buggy cmap[].
if (ON_String::EqualOrdinal(face->family_name, "CityBlueprint", false))
return true;
if (ON_String::EqualOrdinal(face->family_name, "CountryBlueprint", false))
return true;
if (ON_String::EqualOrdinal(face->family_name, "Romantic", false))
return true;
if (ON_String::EqualOrdinal(face->family_name, "SansSerif", false))
return true;
if (ON_String::EqualOrdinal(face->family_name, "Technic", false))
return true;
}
return false;
}
const ON_wString ON_FreeType::EncodingTypeToString( FT_Encoding charmap_encoding )
{
ON_wString e;
switch (charmap_encoding)
{
case FT_ENCODING_NONE: e = L"FT_ENCODING_NONE"; break;
case FT_ENCODING_UNICODE: e = L"FT_ENCODING_UNICODE"; break;
case FT_ENCODING_MS_SYMBOL: e = L"FT_ENCODING_MS_SYMBOL"; break;
case FT_ENCODING_ADOBE_LATIN_1: e = L"FT_ENCODING_ADOBE_LATIN_1"; break;
case FT_ENCODING_OLD_LATIN_2: e = L"FT_ENCODING_OLD_LATIN_2"; break;
case FT_ENCODING_SJIS: e = L"FT_ENCODING_SJIS"; break;
case FT_ENCODING_GB2312: e = L"FT_ENCODING_GB2312"; break;
case FT_ENCODING_BIG5: e = L"FT_ENCODING_BIG5"; break;
case FT_ENCODING_WANSUNG: e = L"FT_ENCODING_WANSUNG"; break;
case FT_ENCODING_JOHAB: e = L"FT_ENCODING_JOHAB"; break;
case FT_ENCODING_ADOBE_STANDARD: e = L"FT_ENCODING_ADOBE_STANDARD"; break;
case FT_ENCODING_ADOBE_EXPERT: e = L"FT_ENCODING_ADOBE_EXPERT"; break;
case FT_ENCODING_ADOBE_CUSTOM: e = L"FT_ENCODING_ADOBE_CUSTOM"; break;
case FT_ENCODING_APPLE_ROMAN: e = L"FT_ENCODING_APPLE_ROMAN"; break;
default:
e = ON_wString::FormatToString(
L"((FT_Encoding)%u)",
static_cast<unsigned int>(charmap_encoding)
);
break;
}
return e;
}
const ON_wString ON_FreeType::CharmapPlatformEncodingDescription( const FT_CharMap cmap )
{
// Reference
// https://www.microsoft.com/typography/otspec/name.htm#enc3
ON_wString platform;
ON_wString encoding;
switch (cmap->platform_id)
{
case 0:
platform = L"Unicode";
switch (cmap->encoding_id)
{
case 0: encoding = L"Unicode 1.0 semantics [deprecated]"; break;
case 1: encoding = L"Unicode 1.1 semantics [deprecated]"; break;
case 2: encoding = L"ISO/IEC 10646 semantics [deprecated]"; break;
case 3: encoding = L"Unicode 2.0+ semantics BMP only"; break;
case 4: encoding = L"Unicode 2.0+ semantics"; break;
case 5: encoding = L"Unicode Variation Sequences"; break;
case 6: encoding = L"Unicode full repertoire"; break;
}
break;
case 1:
platform = L"Apple Script Manager";
switch (cmap->encoding_id)
{
case 0: encoding = L"Roman"; break;
case 1: encoding = L"Japanese"; break;
case 2: encoding = L"Chinese (Traditional)"; break;
case 3: encoding = L"Korean"; break;
case 4: encoding = L"Arabic"; break;
case 5: encoding = L"Hebrew"; break;
case 6: encoding = L"Greek"; break;
case 7: encoding = L"Russian"; break;
case 8: encoding = L"RSymbol"; break;
case 9: encoding = L"Devanagari"; break;
case 10: encoding = L"Gurmukhi"; break;
case 11: encoding = L"Gujarati"; break;
case 12: encoding = L"Oriya"; break;
case 13: encoding = L"Bengali"; break;
case 14: encoding = L"Tamil"; break;
case 15: encoding = L"Telugu"; break;
case 16: encoding = L"Kannada"; break;
case 17: encoding = L"Malayalam"; break;
case 18: encoding = L"Sinhalese"; break;
case 19: encoding = L"Burmese"; break;
case 20: encoding = L"Khmer"; break;
case 21: encoding = L"Thai"; break;
case 22: encoding = L"Laotian"; break;
case 23: encoding = L"Georgian"; break;
case 24: encoding = L"Armenian"; break;
case 25: encoding = L"Chinese (Simplified)"; break;
case 26: encoding = L"Tibetan"; break;
case 27: encoding = L"Mongolian"; break;
case 28: encoding = L"Geez"; break;
case 29: encoding = L"Slavic"; break;
case 30: encoding = L"Vietnamese"; break;
case 31: encoding = L"Sindhi"; break;
case 32: encoding = L"Uninterpreted"; break;
}
break;
case 2:
platform = L"ISO [deprecated]";
switch (cmap->encoding_id)
{
case 0: encoding = L"7-bit ASCII"; break;
case 1: encoding = L"ISO 10646"; break;
case 2: encoding = L"ISO 8859-1"; break;
}
break;
case 3:
platform = L"Windows";
switch (cmap->encoding_id)
{
case 0: encoding = L"Symbol"; break;
case 1: encoding = L"Unicode BMP UCS-2"; break;
case 2: encoding = L"ShiftJIS"; break;
case 3: encoding = L"PRC"; break;
case 4: encoding = L"Big5"; break;
case 5: encoding = L"Wansung"; break;
case 6: encoding = L"Johab"; break;
case 10: encoding = L"Unicode UCS-4"; break;
}
break;
}
const ON_wString e = ON_FreeType::EncodingTypeToString(cmap->encoding);
ON_wString s = ON_wString::FormatToString(
L"%ls %d-%d",
static_cast<const wchar_t*>(e),
cmap->platform_id,
cmap->encoding_id
);
if (platform.IsNotEmpty())
{
if (encoding.IsEmpty())
encoding = L"unknown";
s += ON_wString::FormatToString(
L" (%ls %ls)",
static_cast<const wchar_t*>(platform),
static_cast<const wchar_t*>(encoding)
);
}
return s;
}
bool ON_FontGlyph::TestFreeTypeFaceCharMaps(
ON_TextLog* text_log
) const
{
// In order for false to be returned, charmaps[] have to exist and
// an explicit error has to be detected. Otherwise, true is returned.
const ON_Font* font = Font();
if (nullptr == font)
{
if (text_log)
text_log->Print("Font() = nullptr. Nothing to test.\n");
return true; // nothing to test.
}
if (false == font->IsManagedFont())
{
// this "should" never happen.
if (text_log)
text_log->Print("Font().IsManagedFont() = false. Nothing to test.\n");
return true; // nothing to test.
}
const ON__UINT32 unicode_code_point = CodePoint();
if (false == ON_IsValidUnicodeCodePoint(unicode_code_point))
{
if (text_log)
text_log->Print("CodePoint() is not valid. Nothing to test.\n");
return true; // nothing to test.
}
FT_Face face = reinterpret_cast<FT_Face>(ON_Font::FreeTypeFace(font));
if (nullptr == face)
{
if (text_log)
text_log->Print("Face is nullptr. Nothing to test.\n");
return true; // nothing to test.
}
const unsigned int glyph_index = FontGlyphIndex();
if ( 0 == glyph_index )
{
if (text_log)
text_log->Print("FontGlyphIndex is 0. Nothing to test.\n");
return true; // nothing to test.
}
// Save current face charmap state
FT_CharMap charmap0 = face->charmap;
bool rc = true;
for (int charmap_index = 0; charmap_index < face->num_charmaps; charmap_index++)
{
FT_CharMap charmap = face->charmaps[charmap_index];
if (nullptr == charmap)
continue;
bool bHaveCharMap = false;
bool bHaveCharCode = false;
bool bBuggyMap = false;
bool bUnicode = false;
unsigned int char_code = 0xFFFFFFFF;
unsigned int gid = 0;
const ON_wString e = ON_FreeType::CharmapPlatformEncodingDescription(charmap);
switch (charmap->encoding)
{
case FT_ENCODING_UNICODE:
char_code = unicode_code_point;
bHaveCharCode = true;
bUnicode = true;
break;
case FT_ENCODING_APPLE_ROMAN:
bBuggyMap = ON_FreeType::UseUnicodeAsAppleRomanCharCode(face);
if ( bBuggyMap )
{
bHaveCharCode = unicode_code_point <= 0xFF;
if (bHaveCharCode)
{
bUnicode = true;
char_code = unicode_code_point;
}
}
else
{
// Microsoft code page 10000 = Apple Roman encoding
char_code = ON_MapUnicodeToMSSBCP(10000, unicode_code_point);
bHaveCharCode = (char_code <= 0xFF);
}
break;
case FT_ENCODING_MS_SYMBOL:
bHaveCharCode = true;
char_code = unicode_code_point;
break;
default:
break;
}
if (bHaveCharCode)
{
bHaveCharMap
= FT_Err_Ok == FT_Set_Charmap(face, charmap)
&& charmap == face->charmap;
if (bHaveCharMap)
{
gid = FT_Get_Char_Index(face, char_code);
if (glyph_index != gid)
rc = false;
}
else
rc = false;
}
if (text_log)
{
const wchar_t* damaged = ( bBuggyMap || ON_FreeType::IsDamagedCharMap(charmap) )? L"DAMAGED " : L"";
ON_wString s = ON_wString::FormatToString(
L"%lscmap[%d] %ls",
damaged,
charmap_index,
static_cast<const wchar_t*>(e)
);
s += ON_wString::FormatToString(L" U+%04X", unicode_code_point);
if (false == bHaveCharCode)
s += L" (no char code)";
if (false == bUnicode || bBuggyMap || char_code != unicode_code_point)
s += ON_wString::FormatToString(L" -> char code 0x%X (%u)", char_code, char_code);
if (false == bHaveCharMap)
{
s += L" ERROR(cannot use cmap)";
}
else if (0 == gid)
{
s += ON_wString::FormatToString(
L" no glpyh",
char_code
);
}
else
{
s += ON_wString::FormatToString(
L" -> glyph id %u",
gid
);
if (glyph_index != gid)
{
s += ON_wString::FormatToString(L"ERROR(expected glyph index %u)",glyph_index);
}
}
text_log->Print(L"%ls\n", static_cast<const wchar_t*>(s));
}
}
// restore face charmap state
FT_Set_Charmap(face, charmap0);
return rc;
}
unsigned int ON_FreeType::GlyphId(
FT_Face face,
ON__UINT32 unicode_code_point
)
{
if (nullptr == face)
return 0;
const FT_CharMap charmap0 = face->charmap;
if (nullptr == charmap0 || FT_ENCODING_UNICODE != charmap0->encoding)
{
if (FT_Err_Ok != FT_Select_Charmap(face, FT_ENCODING_UNICODE))
FT_Set_Charmap(face, charmap0);
}
FT_CharMap charmap1 = face->charmap;
if (nullptr != charmap1 && FT_ENCODING_UNICODE == charmap1->encoding)
{
unsigned int glyph_id = FT_Get_Char_Index(face, unicode_code_point);
if (0 != glyph_id)
{
// commonly happens for well designed modern fonts.
return glyph_id;
}
}
else
{
charmap1 = nullptr;
}
// May 2017 Dale Lear
//
// In some fonts, there are multiple FT_ENCODING_UNICODE charmaps
// and they can map different unicode code points. These typically are
// Windows UCS-2 and Windows UCS-4 cmaps. UCS-2 and UCS-4 values subsets of Unicode.
//
// In fonts like CityBlueprint and CountryBlueprint (which many customers use), there
// is no FT_ENCODING_UNICODE charmap but there is a viable FT_ENCODING_APPLE_ROMAN charmap.
//
// As we discover fonts that customers use, we will add support for their charmaps.
//
// TrueType platform_id and encoding_id. The encoding id is platform specific.
// platform_id-encoding_id
//
// 0-* "Apple *code" - encoding id varies
//
// 1-0 Apple Roman (256 codes - see ON_MapAppleRomanToUnicode())
// 1-* Apple (encoding id = script manager)
//
// 2-* ISO (encoding id = ISO encoding)
//
// 3-0 Windows Symbol
// 3-1 Wnidows UCS-2 (subset of Unicode)
// 3-2 Windows ShiftJIS
// 3-3 Windows Big5
// 3-4 WIndows PRC
// 3-5 Windows Wansung
// 3-6 Windows Johab
// 3-10 Wnidows UCS-4 (subset of Unicode)
//
// 4-* Custom
//
// http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-chapter08
//
const FT_Encoding encoding_pass[]
{
FT_ENCODING_UNICODE,
FT_ENCODING_APPLE_ROMAN,
FT_ENCODING_NONE // FT_ENCODING_NONE must terminate the array
};
const size_t pass_count = sizeof(encoding_pass) / sizeof(encoding_pass[0]);
for (size_t pass = 0; pass < pass_count; pass++)
{
if (pass+1 >= pass_count && unicode_code_point >= 0x80)
{
// As a last gasp pass, when the unicode_code_point < 0x80, we try every charmap
// because using the "ASCII encoding" for those code points was farily common