forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFontAsset.cs
More file actions
1431 lines (1188 loc) · 54.4 KB
/
FontAsset.cs
File metadata and controls
1431 lines (1188 loc) · 54.4 KB
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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.Bindings;
using UnityEngine.TextCore.LowLevel;
namespace UnityEngine.TextCore
{
/// <summary>
/// Contains the font asset for the specified font weight styles.
/// </summary>
[Serializable]
struct FontWeights
{
public FontAsset regularTypeface;
public FontAsset italicTypeface;
}
// Structure which holds the font creation settings
[Serializable]
struct FontAssetCreationSettings
{
public string fontFileGUID;
public int pointSizeSamplingMode;
public int pointSize;
public int padding;
public int packingMode;
public int atlasWidth;
public int atlasHeight;
public int characterSetSelectionMode;
public string characterSequence;
public string referencedFontAssetGUID;
public string referencedTextAssetGUID;
public int fontStyle;
public float fontStyleModifier;
public int renderMode;
public bool includeFontFeatures;
internal FontAssetCreationSettings(string fontFileGUID, int pointSize, int pointSizeSamplingMode, int padding, int packingMode, int atlasWidth, int atlasHeight, int characterSelectionMode, string characterSet, int renderMode)
{
this.fontFileGUID = fontFileGUID;
this.pointSize = pointSize;
this.pointSizeSamplingMode = pointSizeSamplingMode;
this.padding = padding;
this.packingMode = packingMode;
this.atlasWidth = atlasWidth;
this.atlasHeight = atlasHeight;
this.characterSequence = characterSet;
this.characterSetSelectionMode = characterSelectionMode;
this.renderMode = renderMode;
this.referencedFontAssetGUID = string.Empty;
this.referencedTextAssetGUID = string.Empty;
this.fontStyle = 0;
this.fontStyleModifier = 0;
this.includeFontFeatures = false;
}
}
[Serializable]
[VisibleToOtherModules("UnityEngine.UIElementsModule")]
internal class FontAsset : ScriptableObject
{
/// <summary>
/// The version of the font asset class.
/// Version 1.1.0 adds support for the new TextCore.FontEngine and Dynamic SDF system.
/// </summary>
public string version
{
get { return m_Version; }
set { m_Version = value; }
}
[SerializeField]
string m_Version = "1.1.0";
/// <summary>
/// HashCode based on the name of the asset.
/// TODO: Need to handle renaming of font asset.
/// </summary>
public int hashCode
{
get { return m_HashCode; }
set { m_HashCode = value; }
}
[SerializeField]
int m_HashCode;
/// <summary>
/// The general information about the font face.
/// </summary>
public FaceInfo faceInfo
{
get { return m_FaceInfo; }
set { m_FaceInfo = value; }
}
[SerializeField]
FaceInfo m_FaceInfo;
/// <summary>
/// This field is set when the font asset is first created.
/// </summary>
[SerializeField]
internal string m_SourceFontFileGUID;
/// <summary>
/// Editor-Only internal reference to source font file.
/// TODO: This should be enclosed in #if UNITY_EDITOR once Katana tests issue related to serialized properties in the Editor is resolved.
/// </summary>
[SerializeField]
internal Font m_SourceFontFile_EditorRef;
/// <summary>
/// Source font file when atlas population mode is set to dynamic. Null when the atlas population mode is set to static.
/// </summary>
public Font sourceFontFile
{
get { return m_SourceFontFile; }
}
[SerializeField]
internal Font m_SourceFontFile;
internal enum AtlasPopulationMode
{
Static = 0x0,
Dynamic = 0x1,
}
/// <summary>
/// The atlas population mode.
/// When set to Dynamic, the sourceFontFile is set to reference the source font file. Otherwise it is set to null.
/// </summary>
public AtlasPopulationMode atlasPopulationMode
{
get { return m_AtlasPopulationMode; }
set
{
m_AtlasPopulationMode = value;
//if (m_AtlasPopulationMode == AtlasPopulationMode.Static)
// m_SourceFontFile = null;
//else if (m_AtlasPopulationMode == AtlasPopulationMode.Dynamic)
// m_SourceFontFile = m_SourceFontFile_EditorRef;
}
}
[SerializeField]
private AtlasPopulationMode m_AtlasPopulationMode;
/// <summary>
/// List of glyphs contained in the font asset.
/// </summary>
public List<Glyph> glyphTable
{
get { return m_GlyphTable; }
set { m_GlyphTable = value; }
}
[SerializeField]
List<Glyph> m_GlyphTable = new List<Glyph>();
/// <summary>
/// Dictionary used to lookup glyphs contained in the font asset by their index.
/// </summary>
public Dictionary<uint, Glyph> glyphLookupTable
{
get
{
if (m_GlyphLookupDictionary == null)
ReadFontAssetDefinition();
return m_GlyphLookupDictionary;
}
}
Dictionary<uint, Glyph> m_GlyphLookupDictionary;
/// <summary>
/// List containing the characters of the given font asset.
/// </summary>
public List<Character> characterTable
{
get { return m_CharacterTable; }
set { m_CharacterTable = value; }
}
[SerializeField]
List<Character> m_CharacterTable = new List<Character>();
/// <summary>
/// Dictionary used to lookup characters contained in the font asset by their unicode values.
/// </summary>
public Dictionary<uint, Character> characterLookupTable
{
get
{
if (m_CharacterLookupDictionary == null)
ReadFontAssetDefinition();
return m_CharacterLookupDictionary;
}
}
Dictionary<uint, Character> m_CharacterLookupDictionary;
/// <summary>
/// The font atlas used by this font asset.
/// This is always the texture at index [0] of the fontAtlasTextures.
/// </summary>
public Texture2D atlasTexture
{
get
{
if (m_AtlasTexture == null)
{
m_AtlasTexture = atlasTextures[0];
}
return m_AtlasTexture;
}
}
Texture2D m_AtlasTexture;
/// <summary>
/// Array of atlas textures that contain the glyphs used by this font asset.
/// </summary>
public Texture2D[] atlasTextures
{
get
{
if (m_AtlasTextures == null)
{
//
}
return m_AtlasTextures;
}
set
{
m_AtlasTextures = value;
}
}
[SerializeField]
Texture2D[] m_AtlasTextures;
/// <summary>
/// Index of the font atlas texture that still has available space to add new glyphs.
/// Once an atlas texture is full, the index is increased to point to the next element in the m_AtlasTextures array.
/// TODO:
/// </summary>
[SerializeField]
internal int m_AtlasTextureIndex;
/// <summary>
/// The width of the atlas texture(s) used by this font asset.
/// </summary>
public int atlasWidth
{
get { return m_AtlasWidth; }
set { m_AtlasWidth = value; }
}
[SerializeField]
private int m_AtlasWidth;
/// <summary>
/// The height of the atlas texture(s) used by this font asset.
/// </summary>
public int atlasHeight
{
get { return m_AtlasHeight; }
set { m_AtlasHeight = value; }
}
[SerializeField]
private int m_AtlasHeight;
/// <summary>
/// The padding used between glyphs contained in the atlas texture(s) used by this font asset.
/// </summary>
public int atlasPadding
{
get { return m_AtlasPadding; }
set { m_AtlasPadding = value; }
}
[SerializeField]
private int m_AtlasPadding;
public GlyphRenderMode atlasRenderMode
{
get { return m_AtlasRenderMode; }
set { m_AtlasRenderMode = value; }
}
[SerializeField]
private GlyphRenderMode m_AtlasRenderMode;
/// <summary>
/// List of spaces occupied by glyphs in a given texture.
/// </summary>
internal List<GlyphRect> usedGlyphRects
{
get { return m_UsedGlyphRects; }
set { m_UsedGlyphRects = value; }
}
[SerializeField]
List<GlyphRect> m_UsedGlyphRects;
/// <summary>
/// List of spaces available in a given texture to add new glyphs.
/// </summary>
internal List<GlyphRect> freeGlyphRects
{
get { return m_FreeGlyphRects; }
set { m_FreeGlyphRects = value; }
}
[SerializeField]
List<GlyphRect> m_FreeGlyphRects;
/// <summary>
/// Used in the process of adding new glyphs to the atlas texture.
/// </summary>
private List<uint> m_GlyphIndexes = new List<uint>();
private Dictionary<uint, List<uint>> s_GlyphLookupMap = new Dictionary<uint, List<uint>>();
/// <summary>
/// The material used by this asset.
/// </summary>
public Material material
{
get { return m_Material; }
set
{
m_Material = value;
m_MaterialHashCode = TextUtilities.GetHashCodeCaseInSensitive(m_Material.name);
}
}
[SerializeField]
Material m_Material;
/// <summary>
/// HashCode based on the name of the material assigned to this asset.
/// </summary>
public int materialHashCode
{
get { return m_MaterialHashCode; }
set
{
if (m_MaterialHashCode == 0)
m_MaterialHashCode = TextUtilities.GetHashCodeCaseInSensitive(m_Material.name);
m_MaterialHashCode = value;
}
}
[SerializeField]
internal int m_MaterialHashCode;
/// <summary>
///
/// </summary>
public KerningTable kerningTable
{
get { return m_KerningTable; }
set { m_KerningTable = value; }
}
[SerializeField]
internal KerningTable m_KerningTable = new KerningTable();
/// <summary>
/// Dictionary containing the kerning data
/// </summary>
public Dictionary<int, KerningPair> kerningLookupDictionary
{
get { return m_KerningLookupDictionary; }
}
Dictionary<int, KerningPair> m_KerningLookupDictionary;
/// <summary>
/// TODO: This is only used to display an empty kerning pair in the Editor. This should be implemented more cleanly.
/// </summary>
[SerializeField]
internal KerningPair m_EmptyKerningPair;
/// <summary>
/// List containing the Fallback font assets for this font.
/// </summary>
public List<FontAsset> fallbackFontAssetTable
{
get { return m_FallbackFontAssetTable; }
set { m_FallbackFontAssetTable = value; }
}
[SerializeField]
internal List<FontAsset> m_FallbackFontAssetTable;
public FontAssetCreationSettings fontAssetCreationSettings
{
get { return m_FontAssetCreationSettings; }
set { m_FontAssetCreationSettings = value; }
}
[SerializeField]
internal FontAssetCreationSettings m_FontAssetCreationSettings;
/// <summary>
/// Array containing font assets to be used as alternative typefaces for the various potential font weights of this font asset.
/// </summary>
public FontWeights[] fontWeightTable
{
get { return m_FontWeightTable; }
set { m_FontWeightTable = value; }
}
[SerializeField]
internal FontWeights[] m_FontWeightTable = new FontWeights[10];
/// <summary>
/// Defines the dilation of the text when using regular style.
/// </summary>
public float regularStyleWeight { get { return m_RegularStyleWeight; } set { m_RegularStyleWeight = value; } }
[SerializeField]
float m_RegularStyleWeight = 0;
/// <summary>
/// The spacing between characters when using regular style.
/// </summary>
public float regularStyleSpacing { get { return m_RegularStyleSpacing; } set { m_RegularStyleSpacing = value; } }
[SerializeField]
float m_RegularStyleSpacing = 0;
/// <summary>
/// Defines the dilation of the text when using bold style.
/// </summary>
public float boldStyleWeight { get { return m_BoldStyleWeight; } set { m_BoldStyleWeight = value; } }
[SerializeField]
float m_BoldStyleWeight = 0.75f;
/// <summary>
/// The spacing between characters when using regular style.
/// </summary>
public float boldStyleSpacing { get { return m_BoldStyleSpacing; } set { m_BoldStyleSpacing = value; } }
[SerializeField]
float m_BoldStyleSpacing = 7f;
/// <summary>
/// Defines the slant of the text when using italic style.
/// </summary>
public byte italicStyleSlant { get { return m_ItalicStyleSlant; } set { m_ItalicStyleSlant = value; } }
[SerializeField]
byte m_ItalicStyleSlant = 35;
/// <summary>
/// The number of spaces that a tab represents.
/// </summary>
public byte tabMultiple { get { return m_TabMultiple; } set { m_TabMultiple = value; } }
[SerializeField]
byte m_TabMultiple = 10;
internal bool m_IsFontAssetLookupTablesDirty = false;
public static FontAsset CreateFontAsset(Font font)
{
return CreateFontAsset(font, 90, 9, GlyphRenderMode.SDFAA, 1024, 1024, AtlasPopulationMode.Dynamic);
}
/// <summary>
/// Create new instance of a font asset.
/// </summary>
/// <param name="font">The source font file.</param>
/// <param name="samplingPointSize">The sampling point size.</param>
/// <param name="atlasPadding">The padding / spread between individual glyphs in the font asset.</param>
/// <param name="renderMode"></param>
/// <param name="atlasWidth">The atlas texture width.</param>
/// <param name="atlasHeight">The atlas texture height.</param>
/// <param name="atlasPopulationMode"></param>
/// <returns></returns>
public static FontAsset CreateFontAsset(Font font, int samplingPointSize, int atlasPadding, GlyphRenderMode renderMode, int atlasWidth, int atlasHeight, AtlasPopulationMode atlasPopulationMode = AtlasPopulationMode.Dynamic)
{
FontAsset fontAsset = ScriptableObject.CreateInstance<FontAsset>();
// Set face information
FontEngine.InitializeFontEngine();
FontEngine.LoadFontFace(font, samplingPointSize);
fontAsset.faceInfo = FontEngine.GetFaceInfo();
// Set font reference and GUID
if (atlasPopulationMode == AtlasPopulationMode.Dynamic)
fontAsset.m_SourceFontFile = font;
// Set persistent reference to source font file in the Editor only.
//string guid;
//long localID;
//UnityEditor.AssetDatabase.TryGetGUIDAndLocalFileIdentifier(font, out guid, out localID);
//fontAsset.m_SourceFontFileGUID = guid;
fontAsset.m_SourceFontFile_EditorRef = font;
fontAsset.atlasPopulationMode = atlasPopulationMode;
fontAsset.atlasWidth = atlasWidth;
fontAsset.atlasHeight = atlasHeight;
fontAsset.atlasPadding = atlasPadding;
fontAsset.atlasRenderMode = renderMode;
// Initialize array for the font atlas textures.
fontAsset.atlasTextures = new Texture2D[1];
// Create and add font atlas texture.
Texture2D texture = new Texture2D(0, 0, TextureFormat.Alpha8, false);
//texture.name = assetName + " Atlas";
fontAsset.atlasTextures[0] = texture;
// Add free rectangle of the size of the texture.
int packingModifier;
if (((GlyphRasterModes)renderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP)
{
packingModifier = 0;
// Optimize by adding static ref to shader.
Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileBitmap);
//tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
fontAsset.material = tmp_material;
}
else
{
packingModifier = 1;
// Optimize by adding static ref to shader.
Material tmp_material = new Material(ShaderUtilities.ShaderRef_MobileSDF);
//tmp_material.name = texture.name + " Material";
tmp_material.SetTexture(ShaderUtilities.ID_MainTex, texture);
tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, atlasWidth);
tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, atlasHeight);
tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, atlasPadding + packingModifier);
tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.regularStyleWeight);
tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyleWeight);
fontAsset.material = tmp_material;
}
fontAsset.freeGlyphRects = new List<GlyphRect>() { new GlyphRect(0, 0, atlasWidth - packingModifier, atlasHeight - packingModifier) };
fontAsset.usedGlyphRects = new List<GlyphRect>();
fontAsset.InitializeDictionaryLookupTables();
return fontAsset;
}
void Awake()
{
// Update the name and material hash codes.
m_HashCode = TextUtilities.GetHashCodeCaseInSensitive(this.name);
if (m_Material != null)
m_MaterialHashCode = TextUtilities.GetHashCodeCaseInSensitive(m_Material.name);
}
void OnValidate()
{
// Update name hash code.
m_HashCode = TextUtilities.GetHashCodeCaseInSensitive(this.name);
if (m_Material != null)
m_MaterialHashCode = TextUtilities.GetHashCodeCaseInSensitive(m_Material.name);
}
/// <summary>
/// Read the various data tables of the font asset to populate its different dictionaries to allow for faster lookup of related font asset data.
/// </summary>
internal void InitializeDictionaryLookupTables()
{
// Create new instance of the glyph lookup dictionary or clear the existing one.
if (m_GlyphLookupDictionary == null)
m_GlyphLookupDictionary = new Dictionary<uint, Glyph>();
else
m_GlyphLookupDictionary.Clear();
// Add the characters contained in the character table into the dictionary for faster lookup.
for (int i = 0; i < m_GlyphTable.Count; i++)
{
Glyph glyph = m_GlyphTable[i];
uint index = glyph.index;
if (m_GlyphLookupDictionary.ContainsKey(index) == false)
m_GlyphLookupDictionary.Add(index, glyph);
}
// Create new instance of the character lookup dictionary or clear the existing one.
if (m_CharacterLookupDictionary == null)
m_CharacterLookupDictionary = new Dictionary<uint, Character>();
else
m_CharacterLookupDictionary.Clear();
// Add the characters contained in the character table into the dictionary for faster lookup.
for (int i = 0; i < m_CharacterTable.Count; i++)
{
Character character = m_CharacterTable[i];
uint unicode = character.unicode;
if (m_CharacterLookupDictionary.ContainsKey(unicode) == false)
m_CharacterLookupDictionary.Add(unicode, character);
if (m_GlyphLookupDictionary.ContainsKey(character.glyphIndex))
character.glyph = m_GlyphLookupDictionary[character.glyphIndex];
}
// Read Font Features which will include kerning data.
// TODO
// Read Kerning pairs and update Kerning pair dictionary for faster lookup.
if (m_KerningLookupDictionary == null)
m_KerningLookupDictionary = new Dictionary<int, KerningPair>();
else
m_KerningLookupDictionary.Clear();
List<KerningPair> glyphPairAdjustmentRecord = m_KerningTable.kerningPairs;
if (glyphPairAdjustmentRecord != null)
{
for (int i = 0; i < glyphPairAdjustmentRecord.Count; i++)
{
KerningPair pair = glyphPairAdjustmentRecord[i];
KerningPairKey uniqueKey = new KerningPairKey(pair.firstGlyph, pair.secondGlyph);
if (m_KerningLookupDictionary.ContainsKey((int)uniqueKey.key) == false)
{
m_KerningLookupDictionary.Add((int)uniqueKey.key, pair);
}
else
{
if (!TextSettings.warningsDisabled)
Debug.LogWarning("Kerning Key for [" + uniqueKey.ascii_Left + "] and [" + uniqueKey.ascii_Right + "] already exists.");
}
}
}
}
/// <summary>
///
/// </summary>
internal void ReadFontAssetDefinition()
{
// Initialize lookup tables for characters and glyphs.
InitializeDictionaryLookupTables();
// Add Tab char(9) to Dictionary.
if (m_CharacterLookupDictionary.ContainsKey(9) == false)
{
Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, m_FaceInfo.tabWidth * tabMultiple), GlyphRect.zero, 1.0f, 0);
m_CharacterLookupDictionary.Add(9, new Character(9, glyph));
}
// Add Linefeed LF char(10) and Carriage Return CR char(13)
if (m_CharacterLookupDictionary.ContainsKey(10) == false)
{
Glyph glyph = new Glyph(0, new GlyphMetrics(10, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
m_CharacterLookupDictionary.Add(10, new Character(10, glyph));
if (!m_CharacterLookupDictionary.ContainsKey(13))
m_CharacterLookupDictionary.Add(13, new Character(13, glyph));
}
// Add Zero Width Space 8203 (0x200B)
if (m_CharacterLookupDictionary.ContainsKey(8203) == false)
{
Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
m_CharacterLookupDictionary.Add(8203, new Character(8203, glyph));
}
// Add Zero Width Non-Breaking Space 8288 (0x2060)
if (m_CharacterLookupDictionary.ContainsKey(8288) == false)
{
Glyph glyph = new Glyph(0, new GlyphMetrics(0, 0, 0, 0, 0), GlyphRect.zero, 1.0f, 0);
m_CharacterLookupDictionary.Add(8288, new Character(8288, glyph));
}
// Set Cap Height
if (m_FaceInfo.capLine == 0 && m_CharacterLookupDictionary.ContainsKey(72))
m_FaceInfo.capLine = m_CharacterLookupDictionary[72].glyph.metrics.horizontalBearingY;
// Adjust Font Scale for compatibility reasons
if (m_FaceInfo.scale == 0)
m_FaceInfo.scale = 1.0f;
// Set Strikethrough Offset (if needed)
if (m_FaceInfo.strikethroughOffset == 0)
m_FaceInfo.strikethroughOffset = m_FaceInfo.capLine / 2.5f;
// Set Padding value for legacy font assets.
if (m_AtlasPadding == 0)
{
if (material.HasProperty(ShaderUtilities.ID_GradientScale))
m_AtlasPadding = (int)material.GetFloat(ShaderUtilities.ID_GradientScale) - 1;
}
// Compute Hashcode for the font asset name
m_HashCode = TextUtilities.GetHashCodeCaseInSensitive(name);
// Compute Hashcode for the material name
m_MaterialHashCode = TextUtilities.GetHashCodeCaseInSensitive(material.name);
}
/// <summary>
/// Sort the Character table by Unicode values.
/// </summary>
internal void SortCharacterTable()
{
if (m_CharacterTable != null && m_CharacterTable.Count > 0)
m_CharacterTable = m_CharacterTable.OrderBy(c => c.unicode).ToList();
}
/// <summary>
/// Sort the Glyph table by index values.
/// </summary>
internal void SortGlyphTable()
{
if (m_GlyphTable != null && m_GlyphTable.Count > 0)
m_GlyphTable = m_GlyphTable.OrderBy(c => c.index).ToList();
}
/// <summary>
/// Sort both glyph and character tables.
/// </summary>
internal void SortGlyphAndCharacterTables()
{
SortGlyphTable();
SortCharacterTable();
}
/// <summary>
/// Function to check if a certain character exists in the font asset.
/// </summary>
/// <param name="character"></param>
/// <returns></returns>
internal bool HasCharacter(int character)
{
if (m_CharacterLookupDictionary == null)
return false;
if (m_CharacterLookupDictionary.ContainsKey((uint)character))
return true;
return false;
}
/// <summary>
/// Function to check if a certain character exists in the font asset.
/// </summary>
/// <param name="character"></param>
/// <returns></returns>
internal bool HasCharacter(char character)
{
if (m_CharacterLookupDictionary == null)
return false;
if (m_CharacterLookupDictionary.ContainsKey(character))
return true;
return false;
}
/// <summary>
/// Function to check if a character is contained in the font asset with the option to also check through fallback font assets.
/// </summary>
/// <param name="character"></param>
/// <param name="searchFallbacks"></param>
/// <returns></returns>
internal bool HasCharacter(char character, bool searchFallbacks)
{
// Read font asset definition if it hasn't already been done.
if (m_CharacterLookupDictionary == null)
{
ReadFontAssetDefinition();
if (m_CharacterLookupDictionary == null)
return false;
}
// Check font asset
if (m_CharacterLookupDictionary.ContainsKey(character))
return true;
if (searchFallbacks)
{
// Check font asset fallbacks
if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
{
for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
{
if (fallbackFontAssetTable[i].HasCharacter_Internal(character, searchFallbacks))
return true;
}
}
// Check general fallback font assets.
if (TextSettings.fallbackFontAssets != null && TextSettings.fallbackFontAssets.Count > 0)
{
for (int i = 0; i < TextSettings.fallbackFontAssets.Count && TextSettings.fallbackFontAssets[i] != null; i++)
{
if (TextSettings.fallbackFontAssets[i].m_CharacterLookupDictionary == null)
TextSettings.fallbackFontAssets[i].ReadFontAssetDefinition();
if (TextSettings.fallbackFontAssets[i].m_CharacterLookupDictionary != null && TextSettings.fallbackFontAssets[i].HasCharacter_Internal(character, searchFallbacks))
return true;
}
}
// Check Text Settings Default Font Asset
if (TextSettings.defaultFontAsset != null)
{
if (TextSettings.defaultFontAsset.m_CharacterLookupDictionary == null)
TextSettings.defaultFontAsset.ReadFontAssetDefinition();
if (TextSettings.defaultFontAsset.m_CharacterLookupDictionary != null && TextSettings.defaultFontAsset.HasCharacter_Internal(character, searchFallbacks))
return true;
}
}
return false;
}
/// <summary>
/// Function to check if a character is contained in a font asset with the option to also check through fallback font assets.
/// This private implementation does not search the fallback font asset in the Text Settings file.
/// </summary>
/// <param name="character"></param>
/// <param name="searchFallbacks"></param>
/// <returns></returns>
bool HasCharacter_Internal(char character, bool searchFallbacks)
{
// Read font asset definition if it hasn't already been done.
if (m_CharacterLookupDictionary == null)
{
ReadFontAssetDefinition();
if (m_CharacterLookupDictionary == null)
return false;
}
// Check font asset
if (m_CharacterLookupDictionary.ContainsKey(character))
return true;
if (searchFallbacks)
{
// Check Font Asset Fallback fonts.
if (fallbackFontAssetTable != null && fallbackFontAssetTable.Count > 0)
{
for (int i = 0; i < fallbackFontAssetTable.Count && fallbackFontAssetTable[i] != null; i++)
{
if (fallbackFontAssetTable[i].HasCharacter_Internal(character, searchFallbacks))
return true;
}
}
}
return false;
}
/// <summary>
/// Function to check if certain characters exists in the font asset. Function returns a list of missing characters.
/// </summary>
/// <returns></returns>
internal bool HasCharacters(string text, out List<char> missingCharacters)
{
if (m_CharacterLookupDictionary == null)
{
missingCharacters = null;
return false;
}
missingCharacters = new List<char>();
for (int i = 0; i < text.Length; i++)
{
if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
missingCharacters.Add(text[i]);
}
if (missingCharacters.Count == 0)
return true;
return false;
}
/// <summary>
/// Function to check if certain characters exists in the font asset. Function returns false if any characters are missing.
/// </summary>
/// <param name="text">String containing the characters to check</param>
/// <returns></returns>
internal bool HasCharacters(string text)
{
if (m_CharacterLookupDictionary == null)
return false;
for (int i = 0; i < text.Length; i++)
{
if (!m_CharacterLookupDictionary.ContainsKey(text[i]))
return false;
}
return true;
}
/// <summary>
/// Function to extract all the characters from a font asset.
/// </summary>
/// <param name="fontAsset"></param>
/// <returns></returns>
internal static string GetCharacters(FontAsset fontAsset)
{
string characters = string.Empty;
for (int i = 0; i < fontAsset.characterTable.Count; i++)
{
characters += (char)fontAsset.characterTable[i].unicode;
}
return characters;
}
/// <summary>
/// Function which returns an array that contains all the characters from a font asset.
/// </summary>
/// <param name="fontAsset"></param>
/// <returns></returns>
internal static int[] GetCharactersArray(FontAsset fontAsset)
{
int[] characters = new int[fontAsset.characterTable.Count];
for (int i = 0; i < fontAsset.characterTable.Count; i++)
{
characters[i] = (int)fontAsset.characterTable[i].unicode;
}
return characters;
}
// ================================================================================
// Properties and functions related to character and glyph additions as well as
// tacking glyphs that need to be added to various font asset atlas textures.
// ================================================================================
/// <summary>
/// Determines if the font asset is already registered to be updated.
/// </summary>
//private bool m_IsAlreadyRegisteredForUpdate;
/// <summary>
/// List of glyphs that need to be added / packed in atlas texture.
/// </summary>
List<Glyph> m_GlyphsToPack = new List<Glyph>();
/// <summary>
/// List of glyphs that have been packed in the atlas texture and ready to be rendered.
/// </summary>
List<Glyph> m_GlyphsPacked = new List<Glyph>();
/// <summary>
///
/// </summary>
List<Glyph> m_GlyphsToRender = new List<Glyph>();
/// <summary>
///
/// </summary>
/// <param name="unicode"></param>
/// <param name="glyph"></param>
internal Character AddCharacter_Internal(uint unicode, Glyph glyph)
{
// Check if character is already contained in the character table.
if (m_CharacterLookupDictionary.ContainsKey(unicode))
return m_CharacterLookupDictionary[unicode];
uint glyphIndex = glyph.index;
// Check if glyph is already contained in the glyph table.
if (m_GlyphLookupDictionary.ContainsKey(glyphIndex) == false)
{
if (glyph.glyphRect.width == 0 || glyph.glyphRect.width == 0)
{
// Glyphs with zero width and / or height can be automatically added to font asset.
m_GlyphTable.Add(glyph);
}
else
{
// Try packing new glyph
if (FontEngine.TryPackGlyphInAtlas(glyph, m_AtlasPadding, GlyphPackingMode.ContactPointRule, m_AtlasRenderMode, m_AtlasWidth, m_AtlasHeight, m_FreeGlyphRects, m_UsedGlyphRects) == false)
{
// TODO: Add handling to create new atlas texture to fit glyph.