private static void OptionallyParseTables(IReadOnlyDictionary <string, TrueTypeHeaderTable> tables, TrueTypeDataBytes data, TableRegister.Builder tableRegister) { // cmap if (tables.TryGetValue(TrueTypeHeaderTable.Cmap, out var cmap)) { tableRegister.CMapTable = CMapTable.Load(data, cmap, tableRegister); } // hmtx if (tables.TryGetValue(TrueTypeHeaderTable.Hmtx, out var hmtxHeaderTable)) { tableRegister.HorizontalMetricsTable = HorizontalMetricsTable.Load(data, hmtxHeaderTable, tableRegister); } // name if (tables.TryGetValue(TrueTypeHeaderTable.Name, out var nameHeaderTable)) { // TODO: Not important } // os2 // kern if (tables.TryGetValue(TrueTypeHeaderTable.Kern, out var kernHeaderTable)) { tableRegister.KerningTable = KerningTable.Load(data, kernHeaderTable); } }
public void OnEnable() { font_atlas_prop = base.serializedObject.FindProperty("atlas"); font_material_prop = base.serializedObject.FindProperty("material"); fontWeights_prop = base.serializedObject.FindProperty("fontWeights"); m_list = new ReorderableList(base.serializedObject, base.serializedObject.FindProperty("fallbackFontAssets"), true, true, true, true); m_list.drawElementCallback = delegate(Rect rect, int index, bool isActive, bool isFocused) { SerializedProperty arrayElementAtIndex = m_list.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2f; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), arrayElementAtIndex, GUIContent.none); }; m_list.drawHeaderCallback = delegate(Rect rect) { EditorGUI.LabelField(rect, "<b>Fallback Font Asset List</b>", TMP_UIStyleManager.Label); }; font_normalStyle_prop = base.serializedObject.FindProperty("normalStyle"); font_normalSpacing_prop = base.serializedObject.FindProperty("normalSpacingOffset"); font_boldStyle_prop = base.serializedObject.FindProperty("boldStyle"); font_boldSpacing_prop = base.serializedObject.FindProperty("boldSpacing"); font_italicStyle_prop = base.serializedObject.FindProperty("italicStyle"); font_tabSize_prop = base.serializedObject.FindProperty("tabSize"); m_fontInfo_prop = base.serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = base.serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = base.serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = base.serializedObject.FindProperty("m_kerningPair"); m_fontAsset = (base.target as TMP_FontAsset); m_kerningTable = m_fontAsset.kerningInfo; m_materialPresets = TMP_EditorUtility.FindMaterialReferences(m_fontAsset); TMP_UIStyleManager.GetUIStyles(); m_searchList = new List <int>(); }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); font_normalStyle_prop = serializedObject.FindProperty("normalStyle"); font_normalSpacing_prop = serializedObject.FindProperty("normalSpacingOffset"); font_boldStyle_prop = serializedObject.FindProperty("boldStyle"); font_boldSpacing_prop = serializedObject.FindProperty("boldSpacing"); font_italicStyle_prop = serializedObject.FindProperty("italicStyle"); font_tabSize_prop = serializedObject.FindProperty("tabSize"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); //m_isGlyphInfoListExpanded_prop = serializedObject.FindProperty("isGlyphInfoListExpanded"); //m_isKerningTableExpanded_prop = serializedObject.FindProperty("isKerningTableExpanded"); m_fontAsset = target as TMP_FontAsset; m_kerningTable = m_fontAsset.kerningInfo; // Get the UI Skin and Styles for the various Editors TMP_UIStyleManager.GetUIStyles(); }
internal static FontInstance LoadFont(FontReader reader) { // https://www.microsoft.com/typography/otspec/recom.htm#TableOrdering // recomended order HeadTable head = reader.GetTable <HeadTable>(); // head - not saving but loading in suggested order reader.GetTable <HoizontalHeadTable>(); // hhea reader.GetTable <MaximumProfileTable>(); // maxp OS2Table os2 = reader.GetTable <OS2Table>(); // OS/2 HorizontalMetricsTable horizontalMetrics = reader.GetTable <HorizontalMetricsTable>(); // hmtx // LTSH - Linear threshold data // VDMX - Vertical device metrics // hdmx - Horizontal device metrics CMapTable cmap = reader.GetTable <CMapTable>(); // cmap // fpgm - Font Program // prep - Control Value Program // cvt - Control Value Table reader.GetTable <IndexLocationTable>(); // loca GlyphTable glyphs = reader.GetTable <GlyphTable>(); // glyf KerningTable kern = reader.GetTable <KerningTable>(); // kern - Kerning NameTable nameTable = reader.GetTable <NameTable>(); // name // post - PostScript information // gasp - Grid-fitting/Scan-conversion (optional table) // PCLT - PCL 5 data // DSIG - Digital signature return(new FontInstance(nameTable, cmap, glyphs, os2, horizontalMetrics, head, kern)); }
static KerningTable GetKerningTable(string fontFilePath, int pointSize) { KerningTable kerningInfo = new KerningTable(); kerningInfo.kerningPairs = new List <KerningPair>(); // Temporary Array to hold the kerning pairs from the Native Plug-in. FT_KerningPair[] kerningPairs = new FT_KerningPair[7500]; int kpCount = TMPro_FontPlugin.FT_GetKerningPairs(fontFilePath, m_kerningSet, m_kerningSet.Length, kerningPairs); for (int i = 0; i < kpCount; i++) { // Proceed to add each kerning pairs. KerningPair kp = new KerningPair((uint)kerningPairs[i].ascII_Left, (uint)kerningPairs[i].ascII_Right, kerningPairs[i].xAdvanceOffset * pointSize); // Filter kerning pairs to avoid duplicates int index = kerningInfo.kerningPairs.FindIndex(item => item.firstGlyph == kp.firstGlyph && item.secondGlyph == kp.secondGlyph); if (index == -1) { kerningInfo.kerningPairs.Add(kp); } else if (!TMP_Settings.warningsDisabled) { Debug.LogWarning("Kerning Key for [" + kp.firstGlyph + "] and [" + kp.secondGlyph + "] is a duplicate."); } } return(kerningInfo); }
internal FakeFontInstance( NameTable nameTable, MaximumProfileTable maxpTable, CMapTable cmap, GlyphTable glyphs, OS2Table os2, HorizontalHeadTable horizontalHeadTable, HorizontalMetricsTable horizontalMetrics, VerticalHeadTable verticalHeadTable, VerticalMetricsTable verticalMetrics, HeadTable head, KerningTable kern) : base( nameTable, maxpTable, cmap, glyphs, os2, horizontalHeadTable, horizontalMetrics, verticalHeadTable, verticalMetrics, head, kern, null, null, null, null, null, null, null, null) { }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); font_normalStyle_prop = serializedObject.FindProperty("NormalStyle"); font_boldStyle_prop = serializedObject.FindProperty("BoldStyle"); font_italicStyle_prop = serializedObject.FindProperty("ItalicStyle"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); //m_isGlyphInfoListExpanded_prop = serializedObject.FindProperty("isGlyphInfoListExpanded"); //m_isKerningTableExpanded_prop = serializedObject.FindProperty("isKerningTableExpanded"); m_fontAsset = target as TextMeshProFont; m_kerningTable = m_fontAsset.kerningInfo; // Find to location of the TextMesh Pro Asset Folder (as users may have moved it) string tmproAssetFolderPath = TMPro_EditorUtility.GetAssetLocation(); // GUI Skin if (EditorGUIUtility.isProSkin) mySkin = AssetDatabase.LoadAssetAtPath(tmproAssetFolderPath + "/GUISkins/TMPro_DarkSkin.guiskin", typeof(GUISkin)) as GUISkin; else mySkin = AssetDatabase.LoadAssetAtPath(tmproAssetFolderPath + "/GUISkins/TMPro_LightSkin.guiskin", typeof(GUISkin)) as GUISkin; if (mySkin != null) { SectionLabel = mySkin.FindStyle("Section Label"); GroupLabel = mySkin.FindStyle("Group Label"); SquareAreaBox85G = mySkin.FindStyle("Square Area Box (85 Grey)"); } }
public void Write(FileWriter writer) { writer.ByteOrder = Syroot.BinaryData.ByteOrder.BigEndian; BlockCounter = 1; writer.WriteSignature(Signature); writer.Write(BOM); writer.CheckByteOrderMark(BOM); if (Signature == "TNFR") { writer.ReverseMagic = true; } long _ofsFileSize; long _ofsBlockNum; if (Platform == PlatformType.Wii) { writer.Write((ushort)Version); _ofsFileSize = writer.Position; writer.Write(uint.MaxValue); writer.Write(HeaderSize); _ofsBlockNum = writer.Position; writer.Write((ushort)0); //BlockCount } else { writer.Write(HeaderSize); writer.Write(Version); _ofsFileSize = writer.Position; writer.Write(uint.MaxValue); _ofsBlockNum = writer.Position; writer.Write((ushort)0); //BlockCount writer.Write((ushort)0); } writer.SeekBegin(HeaderSize); FontSection.Write(writer, this); if (KerningTable != null) { BlockCounter++; KerningTable.Write(writer, this); } //Save Block Count using (writer.TemporarySeek(_ofsBlockNum, SeekOrigin.Begin)) { writer.Write((ushort)(BlockCounter + 1)); } //Save File size using (writer.TemporarySeek(_ofsFileSize, SeekOrigin.Begin)) { writer.Write((uint)(writer.BaseStream.Length)); } }
public void ShouldReturnDefaultValueWhenTableCouldNotBeFound() { var writer = new BinaryWriter(); writer.WriteTrueTypeFileHeader(); using (var stream = writer.GetStream()) { var table = KerningTable.Load(new FontReader(stream)); Assert.NotNull(table); } }
internal FakeFontInstance( NameTable nameTable, CMapTable cmap, GlyphTable glyphs, OS2Table os2, HorizontalHeadTable horizontalHeadTable, HorizontalMetricsTable horizontalMetrics, HeadTable head, KerningTable kern) : base(nameTable, cmap, glyphs, os2, horizontalHeadTable, horizontalMetrics, head, kern, null, null) { }
private TrueTypeFontTable ReadKerningTable(uint length, TrueTypeTableEntryList list, BigEndianReader reader) { KerningTable kern = new KerningTable(reader.Position); kern.TableVersion = reader.ReadUShortVersion(); ushort tcount = reader.ReadUInt16(); kern.TableCount = tcount; if (tcount > 0) { kern.SubTables = new List <KerningSubTable>(); for (int i = 0; i < tcount; i++) { KerningSubTable sub = new KerningSubTable(); sub.Version = reader.ReadUShortVersion(); sub.Length = reader.ReadUInt16(); sub.Coverage = (KerningCoverage)reader.ReadByte(); sub.Format = (KerningFormat)reader.ReadByte(); if (sub.Format == KerningFormat.Format0) { KerningFormat0 fdata = new KerningFormat0(); fdata.PairCount = reader.ReadUInt16(); fdata.SearchRange = reader.ReadUInt16(); fdata.EntrySelector = reader.ReadUInt16(); fdata.RangeShift = reader.ReadUInt16(); List <Kerning0Pair> pairs = new List <Kerning0Pair>(); for (int j = 0; j < fdata.PairCount; j++) { Kerning0Pair pair = new Kerning0Pair(); pair.LeftGlyphIndex = reader.ReadUInt16(); pair.RightGlyphIndex = reader.ReadUInt16(); pair.Value = reader.ReadInt16(); pairs.Add(pair); } fdata.KerningPairs = pairs; sub.KerningFormatData = fdata; } kern.SubTables.Add(sub); } } return(kern); }
/// <summary> /// Initializes a new instance of the <see cref="FontDescription" /> class. /// </summary> /// <param name="nameTable">The name table.</param> /// <param name="cmap">The cmap.</param> /// <param name="glyphs">The glyphs.</param> /// <param name="os2">The os2.</param> /// <param name="horizontalMetrics">The horizontal metrics.</param> /// <param name="head">The head.</param> /// <param name="kern">The kern.</param> internal FontInstance(NameTable nameTable, CMapTable cmap, GlyphTable glyphs, OS2Table os2, HorizontalMetricsTable horizontalMetrics, HeadTable head, KerningTable kern) { this.cmap = cmap; this.os2 = os2; this.glyphs = glyphs; this.horizontalMetrics = horizontalMetrics; this.head = head; this.glyphCache = new GlyphInstance[this.glyphs.GlyphCount]; // https://www.microsoft.com/typography/otspec/recom.htm#tad this.LineHeight = os2.TypoAscender - os2.TypoDescender + os2.TypoLineGap; this.EmSize = this.head.UnitsPerEm; this.kerning = kern; this.Description = new FontDescription(nameTable, os2, head); }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); fontWeights_prop = serializedObject.FindProperty("fontWeights"); m_list = new ReorderableList(serializedObject, serializedObject.FindProperty("fallbackFontAssets"), true, true, true, true); m_list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { var element = m_list.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none); }; m_list.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, "<b>Fallback Font Asset List</b>", TMP_UIStyleManager.Label); }; font_normalStyle_prop = serializedObject.FindProperty("normalStyle"); font_normalSpacing_prop = serializedObject.FindProperty("normalSpacingOffset"); font_boldStyle_prop = serializedObject.FindProperty("boldStyle"); font_boldSpacing_prop = serializedObject.FindProperty("boldSpacing"); font_italicStyle_prop = serializedObject.FindProperty("italicStyle"); font_tabSize_prop = serializedObject.FindProperty("tabSize"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); m_kerningPairs_prop = m_kerningInfo_prop.FindPropertyRelative("kerningPairs"); m_fontAsset = target as TMP_FontAsset; m_kerningTable = m_fontAsset.kerningInfo; m_materialPresets = TMP_EditorUtility.FindMaterialReferences(m_fontAsset); // Get the UI Skin and Styles for the various Editors TMP_UIStyleManager.GetUIStyles(); m_GlyphSearchList = new List <int>(); m_KerningTableSearchList = new List <int>(); }
/// <summary> /// Create a new <see cref="TableRegister"/>. /// </summary> /// <param name="builder">The builder with necessary tables set.</param> public TableRegister([NotNull] Builder builder) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } HeaderTable = builder.HeaderTable ?? throw new ArgumentException("The builder did not contain the header table"); GlyphTable = builder.GlyphDataTable ?? throw new ArgumentException("The builder did not contain the glyph data table."); HorizontalHeaderTable = builder.HorizontalHeaderTable ?? throw new ArgumentException("The builder did not contain the horizontal header table."); HorizontalMetricsTable = builder.HorizontalMetricsTable; IndexToLocationTable = builder.IndexToLocationTable ?? throw new ArgumentException("The builder did not contain the index to location table."); MaximumProfileTable = builder.MaximumProfileTable ?? throw new ArgumentException("The builder did not contain the maximum profile table."); PostScriptTable = builder.PostScriptTable; CMapTable = builder.CMapTable; KerningTable = builder.KerningTable; }
// Get Kerning Pairs public KerningTable GetKerningTable(string fontFilePath, int pointSize) { KerningTable kerningInfo = new KerningTable(); kerningInfo.kerningPairs = new List <KerningPair>(); // Temporary Array to hold the kerning paris from the Native Plugin. FT_KerningPair[] kerningPairs = new FT_KerningPair[1000]; int kpCount = TMPro_FontPlugin.FT_GetKerningPairs(fontFilePath, m_kerningSet, m_kerningSet.Length, kerningPairs); for (int i = 0; i < kpCount; i++) { // Proceed to add each kerning pairs. KerningPair kp = new KerningPair(kerningPairs[i].ascII_Left, kerningPairs[i].ascII_Right, kerningPairs[i].xAdvanceOffset * pointSize); kerningInfo.kerningPairs.Add(kp); } return(kerningInfo); }
private static void OptionallyParseTables(IReadOnlyDictionary <string, TrueTypeHeaderTable> tables, TrueTypeDataBytes data, TableRegister.Builder tableRegister) { // cmap if (tables.TryGetValue(TrueTypeHeaderTable.Cmap, out var cmap)) { tableRegister.CMapTable = TableParser.Parse <CMapTable>(cmap, data, tableRegister); } // hmtx if (tables.TryGetValue(TrueTypeHeaderTable.Hmtx, out var hmtxHeaderTable)) { tableRegister.HorizontalMetricsTable = TableParser.Parse <HorizontalMetricsTable>(hmtxHeaderTable, data, tableRegister); } // kern if (tables.TryGetValue(TrueTypeHeaderTable.Kern, out var kernHeaderTable)) { tableRegister.KerningTable = KerningTable.Load(data, kernHeaderTable); } }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); fontWeights_prop = serializedObject.FindProperty("fontWeights"); //fontWeight_Thin = serializedObject.FindProperty("thin"); //fontWeight_ExtraLight = serializedObject.FindProperty("extraLight"); //fontWeight_Light = serializedObject.FindProperty("light"); //fontWeight_Normal = serializedObject.FindProperty("normal"); //fontWeight_Medium = serializedObject.FindProperty("medium"); //fontWeight_DemiBold = serializedObject.FindProperty("demiBold"); //fontWeight_Bold = serializedObject.FindProperty("bold"); //fontWeight_Heavy = serializedObject.FindProperty("heavy"); //fontWeight_Black = serializedObject.FindProperty("black"); font_normalStyle_prop = serializedObject.FindProperty("normalStyle"); font_normalSpacing_prop = serializedObject.FindProperty("normalSpacingOffset"); font_boldStyle_prop = serializedObject.FindProperty("boldStyle"); font_boldSpacing_prop = serializedObject.FindProperty("boldSpacing"); font_italicStyle_prop = serializedObject.FindProperty("italicStyle"); font_tabSize_prop = serializedObject.FindProperty("tabSize"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); //m_isGlyphInfoListExpanded_prop = serializedObject.FindProperty("isGlyphInfoListExpanded"); //m_isKerningTableExpanded_prop = serializedObject.FindProperty("isKerningTableExpanded"); m_fontAsset = target as TMP_FontAsset; m_kerningTable = m_fontAsset.kerningInfo; // Get the UI Skin and Styles for the various Editors TMP_UIStyleManager.GetUIStyles(); }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); font_normalStyle_prop = serializedObject.FindProperty("NormalStyle"); font_boldStyle_prop = serializedObject.FindProperty("BoldStyle"); font_italicStyle_prop = serializedObject.FindProperty("ItalicStyle"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); //m_isGlyphInfoListExpanded_prop = serializedObject.FindProperty("isGlyphInfoListExpanded"); //m_isKerningTableExpanded_prop = serializedObject.FindProperty("isKerningTableExpanded"); m_fontAsset = target as TextMeshProFont; m_kerningTable = m_fontAsset.kerningInfo; // Find to location of the TextMesh Pro Asset Folder (as users may have moved it) string tmproAssetFolderPath = TMPro_EditorUtility.GetAssetLocation(); // GUI Skin if (EditorGUIUtility.isProSkin) { mySkin = AssetDatabase.LoadAssetAtPath(tmproAssetFolderPath + "/GUISkins/TMPro_DarkSkin.guiskin", typeof(GUISkin)) as GUISkin; } else { mySkin = AssetDatabase.LoadAssetAtPath(tmproAssetFolderPath + "/GUISkins/TMPro_LightSkin.guiskin", typeof(GUISkin)) as GUISkin; } if (mySkin != null) { SectionLabel = mySkin.FindStyle("Section Label"); GroupLabel = mySkin.FindStyle("Group Label"); SquareAreaBox85G = mySkin.FindStyle("Square Area Box (85 Grey)"); } }
public KerningTable GetKerningTable(string fontFilePath, int pointSize) { KerningTable kerningTable = new KerningTable(); kerningTable.kerningPairs = new List <KerningPair>(); FT_KerningPair[] array = new FT_KerningPair[7500]; int num = TMPro_FontPlugin.FT_GetKerningPairs(fontFilePath, m_kerningSet, m_kerningSet.Length, array); for (int i = 0; i < num; i++) { KerningPair kp = new KerningPair(array[i].ascII_Left, array[i].ascII_Right, array[i].xAdvanceOffset * (float)pointSize); int num2 = kerningTable.kerningPairs.FindIndex((KerningPair item) => item.AscII_Left == kp.AscII_Left && item.AscII_Right == kp.AscII_Right); if (num2 == -1) { kerningTable.kerningPairs.Add(kp); } else if (!TMP_Settings.warningsDisabled) { Debug.LogWarning("Kerning Key for [" + kp.AscII_Left + "] and [" + kp.AscII_Right + "] is a duplicate."); } } return(kerningTable); }
/// <summary> /// Initializes a new instance of the <see cref="FontInstance"/> class. /// </summary> /// <param name="nameTable">The name table.</param> /// <param name="cmap">The cmap.</param> /// <param name="glyphs">The glyphs.</param> /// <param name="os2">The os2.</param> /// <param name="horizontalHeadTable">The horizontal head table.</param> /// <param name="horizontalMetrics">The horizontal metrics.</param> /// <param name="head">The head.</param> /// <param name="kern">The kern.</param> /// <param name="colrTable">The COLR table</param> /// <param name="cpalTable">The CPAL table</param> internal FontInstance( NameTable nameTable, CMapTable cmap, GlyphTable glyphs, OS2Table os2, HorizontalHeadTable horizontalHeadTable, HorizontalMetricsTable horizontalMetrics, HeadTable head, KerningTable kern, ColrTable?colrTable, CpalTable?cpalTable) { this.cmap = cmap; this.os2 = os2; this.glyphs = glyphs; this.horizontalMetrics = horizontalMetrics; this.head = head; this.glyphCache = new GlyphInstance[this.glyphs.GlyphCount]; if (!(colrTable is null)) { this.colorGlyphCache = new GlyphInstance[this.glyphs.GlyphCount][]; } bool useTypoMetrics = os2.FontStyle.HasFlag(OS2Table.FontStyleSelection.USE_TYPO_METRICS); // https://www.microsoft.com/typography/otspec/recom.htm#tad this.Ascender = useTypoMetrics ? os2.TypoAscender : horizontalHeadTable.Ascender; this.Descender = useTypoMetrics ? os2.TypoDescender : horizontalHeadTable.Descender; this.LineGap = useTypoMetrics ? os2.TypoLineGap : horizontalHeadTable.LineGap; this.LineHeight = this.Ascender - this.Descender + this.LineGap; this.EmSize = this.head.UnitsPerEm; this.kerning = kern; this.colrTable = colrTable; this.cpalTable = cpalTable; this.Description = new FontDescription(nameTable, os2, head); }
// Get Kerning Pairs public KerningTable GetKerningTable(string fontFilePath, int pointSize) { KerningTable kerningInfo = new KerningTable(); kerningInfo.kerningPairs = new List<KerningPair>(); // Temporary Array to hold the kerning pairs from the Native Plug-in. FT_KerningPair[] kerningPairs = new FT_KerningPair[1000]; int kpCount = TMPro_FontPlugin.FT_GetKerningPairs(fontFilePath, m_kerningSet, m_kerningSet.Length, kerningPairs); for (int i = 0; i < kpCount; i++) { // Proceed to add each kerning pairs. KerningPair kp = new KerningPair(kerningPairs[i].ascII_Left, kerningPairs[i].ascII_Right, kerningPairs[i].xAdvanceOffset * pointSize); kerningInfo.kerningPairs.Add(kp); } return kerningInfo; }
void Save_Normal_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string relativeAssetPath = filePath.Substring(dataPath.Length - 6); string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont; if (font_asset == null) { //Debug.Log("Creating TextMeshPro font asset!"); font_asset = ScriptableObject.CreateInstance <TextMeshProFont>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(font_asset, tex_Path_NoExt + ".asset"); // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, 1); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TMPro/Bitmap"); Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); font_asset.material = tmp_material; tmp_material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(tmp_material, font_asset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material); // Destroy Assets that will be replaced. DestroyImmediate(font_asset.atlas, true); // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, 1); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, 1); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, font_asset.atlas); } } AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(font_asset)); // Re-import font asset to get the new updated version. //EditorUtility.SetDirty(font_asset); font_asset.ReadFontDefinition(); AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, font_asset); }
void Save_SDF_FontAsset(string filePath) { //filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. //string dataPath = Application.dataPath; //if (filePath.IndexOf(dataPath, System.StringComparison.InvariantCultureIgnoreCase) == -1) //{ // Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); // return; //} string relativeAssetPath = filePath; string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TMP_FontAsset fontAsset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(tex_Path_NoExt + ".asset"); if (fontAsset == null) { //Debug.Log("Creating TextMeshPro font asset!"); fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset"); // Reference to the source font file //font_asset.sourceFontFile = font_TTF as Font; //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; //if (m_destination_Atlas != null) // m_font_Atlas = m_destination_Atlas; // If using the C# SDF creation mode, we need the scale down factor. int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (m_IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Line Breaking Rules //LineBreakingTable lineBreakingTable = new LineBreakingTable(); // // Add Font Atlas as Sub-Asset fontAsset.atlas = m_FontAtlas; if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里 { m_FontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset); } // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height); int spread = m_Padding + 1; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMProFontCustomizedCreater.FindMaterialReferences(fontAsset); if (fontAsset.atlas) // 有可能被其他资产删除了 { // Destroy Assets that will be replaced. DestroyImmediate(fontAsset.atlas, true); } //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_FontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_FontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (m_IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_SourceFontFile); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_FontAtlas; if (!m_FontAtlas.name.EndsWith(" Atlas")) // 因为图集复用,所以只要加到第一个资产里 { m_FontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_FontAtlas, fontAsset); } // Special handling due to a bug in earlier versions of Unity. m_FontAtlas.hideFlags = HideFlags.None; fontAsset.material.hideFlags = HideFlags.None; // Assign new font atlas texture to the existing material. fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_FontAtlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_FontAtlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_FontAtlas.height); int spread = m_Padding + 1; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); } } // Saving File for Debug //var pngData = m_FontAtlas.EncodeToPNG(); //File.WriteAllBytes("Assets/Debug Distance Field.png", pngData); // Save Font Asset creation settings m_SelectedFontAsset = fontAsset; m_LegacyFontAsset = null; // 提到这里才能保存完整 fontAsset.ReadFontDefinition(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); // Re-import font asset to get the new updated version. //fontAsset.ReadFontDefinition(); AssetDatabase.Refresh(); //m_FontAtlas = null; 贴图不要删除 // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }
static void OnFinished(TexFontSigned font) { var sdfPath = TEXPreference.main.MainFolderPath + "/Fonts/TMPro/" + font.id + ".asset"; TMP_FontAsset asset; if (!(asset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(sdfPath))) { // It doesn't exist (or invalid), so create new asset = font.asset = ScriptableObject.CreateInstance <TMP_FontAsset>(); if (!AssetDatabase.IsValidFolder(TEXPreference.main.MainFolderPath + "/Fonts/TMPro")) { AssetDatabase.CreateFolder(TEXPreference.main.MainFolderPath + "/Fonts", "TMPro"); } AssetDatabase.CreateAsset(asset, sdfPath); } asset.fontAssetType = _render >= RenderModes.DistanceField16 ? TMP_FontAsset.FontAssetTypes.SDF : TMP_FontAsset.FontAssetTypes.Bitmap; FaceInfo face = GetFaceInfo(_faceInfo, 1); asset.AddFaceInfo(face); _atlasInfo = new Texture2D(_bufferWidth, _bufferHeight, TextureFormat.Alpha8, false, true); var _buffer32 = Array.ConvertAll(_buffers, x => new Color32(x, x, x, x)); _atlasInfo.SetPixels32(_buffer32); _atlasInfo.Apply(false, true); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(_glyphsInfo, 1); asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset KerningTable kerningTable = GetKerningTable(font.rawpath, (int)face.PointSize); asset.AddKerningInfo(kerningTable); // Add Line Breaking Rules //LineBreakingTable lineBreakingTable = new LineBreakingTable(); // // Add Font Atlas as Sub-Asset asset.atlas = _atlasInfo; _atlasInfo.name = font.id + " Atlas"; _atlasInfo.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(_atlasInfo, asset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = _atlasInfo + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, _atlasInfo); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, _atlasInfo.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, _atlasInfo.height); tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, asset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, asset.boldStyle); int spread = _render >= RenderModes.DistanceField16 ? _padding + 1 : _padding; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. asset.material = tmp_material; tmp_material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(tmp_material, asset); font.asset = asset; font.ImportDictionary(); AssetDatabase.SaveAssets(); }
void Save_SDF_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); // Trim file extension from filePath. string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string relativeAssetPath = filePath.Substring(dataPath.Length - 6); string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. TextMeshProFont font_asset = AssetDatabase.LoadAssetAtPath(tex_Path_NoExt + ".asset", typeof(TextMeshProFont)) as TextMeshProFont; if (font_asset == null) { //Debug.Log("Creating TextMeshPro font asset!"); font_asset = ScriptableObject.CreateInstance <TextMeshProFont>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(font_asset, tex_Path_NoExt + ".asset"); if (m_destination_Atlas != null) { m_font_Atlas = m_destination_Atlas; } // If using the C# SDF creation mode, we need the scaledown factor. int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, scaleDownFactor); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Line Breaking Rules //LineBreakingTable lineBreakingTable = new LineBreakingTable(); // // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TMPro/Distance Field"); Material tmp_material = new Material(default_Shader); //tmp_material.shaderKeywords = new string[] { "BEVEL_OFF", "GLOW_OFF", "UNDERLAY_OFF" }; tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle); int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. font_asset.material = tmp_material; tmp_material.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(tmp_material, font_asset); } else { // Find all Materials referencing this font atlas. Material[] material_references = TMPro_EditorUtility.FindMaterialReferences(font_asset.material); // Destroy Assets that will be replaced. DestroyImmediate(font_asset.atlas, true); int scaleDownFactor = font_renderMode >= RenderModes.DistanceField16 ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_font_faceInfo, scaleDownFactor); font_asset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset GlyphInfo[] glyphs = GetGlyphInfo(m_font_glyphInfo, scaleDownFactor); font_asset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (includeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(fontFilePath, (int)face.PointSize); font_asset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset font_asset.atlas = m_font_Atlas; m_font_Atlas.name = tex_FileName + " Atlas"; m_font_Atlas.hideFlags = HideFlags.HideInHierarchy; AssetDatabase.AddObjectToAsset(m_font_Atlas, font_asset); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, font_asset.atlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, font_asset.NormalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, font_asset.BoldStyle); int spread = font_renderMode >= RenderModes.DistanceField16 ? font_padding + 1 : font_spread; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. } } // Saving File for Debug //var pngData = destination_Atlas.EncodeToPNG(); //File.WriteAllBytes("Assets/Textures/Debug Distance Field.png", pngData); //font_asset.fontCreationSettings = SaveFontCreationSettings(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(font_asset)); // Re-import font asset to get the new updated version. font_asset.ReadFontDefinition(); AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, font_asset); }
void Awake() { if (!Application.isPlaying || Initialization.Environment == Enums.RunningState.Editor) { return; } var type = typeof(global::TMPro.TMP_FontAsset); var fontInfo = new FaceInfo(); fontInfo.Name = FaceInfo_Name; fontInfo.PointSize = FaceInfo_PointSize; fontInfo.Scale = FaceInfo_Scale; fontInfo.CharacterCount = FaceInfo_CharacterCount; fontInfo.LineHeight = FaceInfo_LineHeight; fontInfo.Baseline = FaceInfo_Baseline; fontInfo.Ascender = FaceInfo_Ascender; fontInfo.CapHeight = FaceInfo_CapHeight; fontInfo.Descender = FaceInfo_Descender; fontInfo.CenterLine = FaceInfo_CenterLine; fontInfo.SuperscriptOffset = FaceInfo_SuperscriptOffset; fontInfo.SubscriptOffset = FaceInfo_SubscriptOffset; fontInfo.SubSize = FaceInfo_SubSize; fontInfo.Underline = FaceInfo_Underline; fontInfo.UnderlineThickness = FaceInfo_UnderlineThickness; fontInfo.TabWidth = FaceInfo_TabWidth; fontInfo.Padding = FaceInfo_Padding; fontInfo.AtlasWidth = FaceInfo_AtlasWidth; fontInfo.AtlasHeight = FaceInfo_AtlasHeight; #if UNITY_EDITOR fontInfo.strikethrough = FaceInfo_strikethrough; fontInfo.strikethroughThickness = FaceInfo_strikethroughThickness; #endif type.GetField("m_fontInfo", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, fontInfo); List <TMP_Glyph> glyphs = new List <TMP_Glyph>(); for (int i = 0; i < TMP_Glyph_height.Count; i++) { TMP_Glyph glyph = new TMP_Glyph(); glyph.height = TMP_Glyph_height[i]; glyph.id = TMP_Glyph_id[i]; glyph.scale = TMP_Glyph_scale[i]; glyph.width = TMP_Glyph_width[i]; glyph.x = TMP_Glyph_x[i]; glyph.xAdvance = TMP_Glyph_xAdvance[i]; glyph.xOffset = TMP_Glyph_xOffset[i]; glyph.y = TMP_Glyph_y[i]; glyph.yOffset = TMP_Glyph_yOffset[i]; glyphs.Add(glyph); } type.GetField("m_glyphInfoList", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, glyphs); KerningTable kTable = new KerningTable(); for (int i = 0; i < KerningTable_AscII_Left.Count; i++) { kTable.kerningPairs.Add(new KerningPair(KerningTable_AscII_Left[i], KerningTable_AscII_Right[i], KerningTable_XadvanceOffset[i])); } type.GetField("m_kerningInfo", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, kTable); var newKerningPair = new KerningPair(KerningPair_AscII_Left, KerningPair_AscII_Right, KerningPair_XadvanceOffset); type.GetField("m_kerningPair", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(this, newKerningPair); FontCreationSetting fontSettings = new FontCreationSetting(); fontSettings.fontAtlasHeight = FontCreationSetting_fontAtlasHeight; fontSettings.fontAtlasWidth = FontCreationSetting_fontAtlasWidth; fontSettings.fontCharacterSet = FontCreationSetting_fontCharacterSet; fontSettings.fontKerning = FontCreationSetting_fontKerning; fontSettings.fontPackingMode = FontCreationSetting_fontPackingMode; fontSettings.fontPadding = FontCreationSetting_fontPadding; fontSettings.fontRenderMode = FontCreationSetting_fontRenderMode; fontSettings.fontSize = FontCreationSetting_fontSize; fontSettings.fontSizingMode = FontCreationSetting_fontSizingMode; fontSettings.fontSourcePath = FontCreationSetting_fontSourcePath; fontSettings.fontStlyeModifier = FontCreationSetting_fontStyleModifier; fontSettings.fontStyle = FontCreationSetting_fontStyle; type.GetField("fontCreationSettings", BindingFlags.Public | BindingFlags.Instance).SetValue(this, fontSettings); TMP_FontWeights[] weights = new TMP_FontWeights[TMP_FontWeights_italicTypeface.Count]; for (int i = 0; i < TMP_FontWeights_italicTypeface.Count; i++) { var newWeight = new TMP_FontWeights(); newWeight.italicTypeface = TMP_FontWeights_italicTypeface[i]; newWeight.regularTypeface = TMP_FontWeights_regularTypeface[i]; weights[i] = newWeight; } type.GetField("fontWeights", BindingFlags.Public | BindingFlags.Instance).SetValue(this, weights); }
private void Save_SDF_FontAsset(string filePath) { filePath = filePath.Substring(0, filePath.Length - 6); string dataPath = Application.dataPath; if (filePath.IndexOf(dataPath, StringComparison.InvariantCultureIgnoreCase) == -1) { Debug.LogError("You're saving the font asset in a directory outside of this project folder. This is not supported. Please select a directory under \"" + dataPath + "\""); return; } string path = filePath.Substring(dataPath.Length - 6); string directoryName = Path.GetDirectoryName(path); string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path); string str = directoryName + "/" + fileNameWithoutExtension; TMP_FontAsset tMP_FontAsset = AssetDatabase.LoadAssetAtPath(str + ".asset", typeof(TMP_FontAsset)) as TMP_FontAsset; if (tMP_FontAsset == null) { tMP_FontAsset = ScriptableObject.CreateInstance <WeaverCore.Assets.TMPro.TMP_FontAsset>(); AssetDatabase.CreateAsset(tMP_FontAsset, str + ".asset"); tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleFactor = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor; FaceInfo faceInfo = GetFaceInfo(m_font_faceInfo, scaleFactor); tMP_FontAsset.AddFaceInfo(faceInfo); TMP_Glyph[] glyphInfo = GetGlyphInfo(m_font_glyphInfo, scaleFactor); tMP_FontAsset.AddGlyphInfo(glyphInfo); if (includeKerningPairs) { string assetPath = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable = GetKerningTable(assetPath, (int)faceInfo.PointSize); tMP_FontAsset.AddKerningInfo(kerningTable); } tMP_FontAsset.atlas = m_font_Atlas; m_font_Atlas.name = fileNameWithoutExtension + " Atlas"; AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset); Shader shader = Shader.Find("TextMeshPro/Distance Field"); Material material = new Material(shader); material.name = fileNameWithoutExtension + " Material"; material.SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); material.SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); material.SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); int num = font_padding + 1; material.SetFloat(ShaderUtilities.ID_GradientScale, num); material.SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle); material.SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle); tMP_FontAsset.material = material; AssetDatabase.AddObjectToAsset(material, tMP_FontAsset); } else { Material[] array = TMP_EditorUtility.FindMaterialReferences(tMP_FontAsset); UnityEngine.Object.DestroyImmediate(tMP_FontAsset.atlas, true); tMP_FontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleFactor2 = (font_renderMode >= RenderModes.DistanceField16) ? 1 : font_scaledownFactor; FaceInfo faceInfo2 = GetFaceInfo(m_font_faceInfo, scaleFactor2); tMP_FontAsset.AddFaceInfo(faceInfo2); TMP_Glyph[] glyphInfo2 = GetGlyphInfo(m_font_glyphInfo, scaleFactor2); tMP_FontAsset.AddGlyphInfo(glyphInfo2); if (includeKerningPairs) { string assetPath2 = AssetDatabase.GetAssetPath(font_TTF); KerningTable kerningTable2 = GetKerningTable(assetPath2, (int)faceInfo2.PointSize); tMP_FontAsset.AddKerningInfo(kerningTable2); } tMP_FontAsset.atlas = m_font_Atlas; m_font_Atlas.name = fileNameWithoutExtension + " Atlas"; m_font_Atlas.hideFlags = HideFlags.None; tMP_FontAsset.material.hideFlags = HideFlags.None; AssetDatabase.AddObjectToAsset(m_font_Atlas, tMP_FontAsset); tMP_FontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, tMP_FontAsset.atlas); for (int i = 0; i < array.Length; i++) { array[i].SetTexture(ShaderUtilities.ID_MainTex, m_font_Atlas); array[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_font_Atlas.width); array[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_font_Atlas.height); int num2 = font_padding + 1; array[i].SetFloat(ShaderUtilities.ID_GradientScale, num2); array[i].SetFloat(ShaderUtilities.ID_WeightNormal, tMP_FontAsset.normalStyle); array[i].SetFloat(ShaderUtilities.ID_WeightBold, tMP_FontAsset.boldStyle); } } AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(tMP_FontAsset)); tMP_FontAsset.ReadFontDefinition(); AssetDatabase.Refresh(); m_font_Atlas = null; TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, tMP_FontAsset); }
public void OnEnable() { font_atlas_prop = serializedObject.FindProperty("atlas"); font_material_prop = serializedObject.FindProperty("material"); fontWeights_prop = serializedObject.FindProperty("fontWeights"); m_list = new ReorderableList(serializedObject, serializedObject.FindProperty("fallbackFontAssets"), true, true, true, true); m_list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) => { var element = m_list.serializedProperty.GetArrayElementAtIndex(index); rect.y += 2; EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, GUIContent.none); }; m_list.drawHeaderCallback = rect => { EditorGUI.LabelField(rect, "<b>Fallback Font Asset List</b>", TMP_UIStyleManager.Label); }; font_normalStyle_prop = serializedObject.FindProperty("normalStyle"); font_normalSpacing_prop = serializedObject.FindProperty("normalSpacingOffset"); font_boldStyle_prop = serializedObject.FindProperty("boldStyle"); font_boldSpacing_prop = serializedObject.FindProperty("boldSpacing"); font_italicStyle_prop = serializedObject.FindProperty("italicStyle"); font_tabSize_prop = serializedObject.FindProperty("tabSize"); m_fontInfo_prop = serializedObject.FindProperty("m_fontInfo"); m_glyphInfoList_prop = serializedObject.FindProperty("m_glyphInfoList"); m_kerningInfo_prop = serializedObject.FindProperty("m_kerningInfo"); m_kerningPair_prop = serializedObject.FindProperty("m_kerningPair"); //m_isGlyphInfoListExpanded_prop = serializedObject.FindProperty("isGlyphInfoListExpanded"); //m_isKerningTableExpanded_prop = serializedObject.FindProperty("isKerningTableExpanded"); m_fontAsset = target as TMP_FontAsset; m_kerningTable = m_fontAsset.kerningInfo; // Get the UI Skin and Styles for the various Editors TMP_UIStyleManager.GetUIStyles(); }
// Get Kerning Pairs public KerningTable GetKerningTable(string fontFilePath, int pointSize) { KerningTable kerningInfo = new KerningTable(); kerningInfo.kerningPairs = new List<KerningPair>(); // Temporary Array to hold the kerning pairs from the Native Plug-in. FT_KerningPair[] kerningPairs = new FT_KerningPair[5000]; int kpCount = TMPro_FontPlugin.FT_GetKerningPairs(fontFilePath, m_kerningSet, m_kerningSet.Length, kerningPairs); for (int i = 0; i < kpCount; i++) { // Proceed to add each kerning pairs. KerningPair kp = new KerningPair(kerningPairs[i].ascII_Left, kerningPairs[i].ascII_Right, kerningPairs[i].xAdvanceOffset * pointSize); // Filter kerning pairs to avoid duplicates int index = kerningInfo.kerningPairs.FindIndex(item => item.AscII_Left == kp.AscII_Left && item.AscII_Right == kp.AscII_Right); if (index == -1) kerningInfo.kerningPairs.Add(kp); else if (!TMP_Settings.warningsDisabled) Debug.LogWarning("Kerning Key for [" + kp.AscII_Left + "] and [" + kp.AscII_Right + "] is a duplicate."); } return kerningInfo; }
/// <summary> /// Initializes a new instance of the <see cref="StreamFontMetrics"/> class. /// </summary> /// <param name="nameTable">The name table.</param> /// <param name="maximumProfileTable">The maximum profile table.</param> /// <param name="cmap">The cmap table.</param> /// <param name="glyphs">The glyph table.</param> /// <param name="os2">The os2 table.</param> /// <param name="horizontalHeadTable">The horizontal head table.</param> /// <param name="horizontalMetrics">The horizontal metrics table.</param> /// <param name="verticalHeadTable">The vertical head table.</param> /// <param name="verticalMetrics">The vertical metrics table.</param> /// <param name="head">The head table.</param> /// <param name="kern">The kerning table.</param> /// <param name="gSubTable">The glyph substitution table.</param> /// <param name="gPosTable">The glyph positioning table.</param> /// <param name="colrTable">The COLR table</param> /// <param name="cpalTable">The CPAL table</param> /// <param name="fpgm">The font program table.</param> /// <param name="cvt">The control value table.</param> /// <param name="prep">The control value program table.</param> /// <param name="glyphDefinitionTable">The glyph definition table.</param> internal StreamFontMetrics( NameTable nameTable, MaximumProfileTable maximumProfileTable, CMapTable cmap, GlyphTable glyphs, OS2Table os2, HorizontalHeadTable horizontalHeadTable, HorizontalMetricsTable horizontalMetrics, VerticalHeadTable?verticalHeadTable, VerticalMetricsTable?verticalMetrics, HeadTable head, KerningTable kern, GSubTable?gSubTable, GPosTable?gPosTable, ColrTable?colrTable, CpalTable?cpalTable, FpgmTable?fpgm, CvtTable?cvt, PrepTable?prep, GlyphDefinitionTable?glyphDefinitionTable) { this.maximumProfileTable = maximumProfileTable; this.cmap = cmap; this.os2 = os2; this.glyphs = glyphs; this.horizontalMetrics = horizontalMetrics; this.verticalMetricsTable = verticalMetrics; this.head = head; this.glyphCache = new GlyphMetrics[this.glyphs.GlyphCount][]; if (colrTable is not null) { this.colorGlyphCache = new GlyphMetrics[this.glyphs.GlyphCount][]; } // https://www.microsoft.com/typography/otspec/recom.htm#tad // We use the same approach as FreeType for calculating the the global ascender, descender, and // height of OpenType fonts for consistency. // // 1.If the OS/ 2 table exists and the fsSelection bit 7 is set (USE_TYPO_METRICS), trust the font // and use the Typo* metrics. // 2.Otherwise, use the HorizontalHeadTable "hhea" table's metrics. // 3.If they are zero and the OS/ 2 table exists, // - Use the OS/ 2 table's sTypo* metrics if they are non-zero. // - Otherwise, use the OS / 2 table's usWin* metrics. bool useTypoMetrics = os2.FontStyle.HasFlag(OS2Table.FontStyleSelection.USE_TYPO_METRICS); if (useTypoMetrics) { this.Ascender = os2.TypoAscender; this.Descender = os2.TypoDescender; this.LineGap = os2.TypoLineGap; this.LineHeight = (short)(this.Ascender - this.Descender + this.LineGap); } else { this.Ascender = horizontalHeadTable.Ascender; this.Descender = horizontalHeadTable.Descender; this.LineGap = horizontalHeadTable.LineGap; this.LineHeight = (short)(this.Ascender - this.Descender + this.LineGap); } if (this.Ascender == 0 || this.Descender == 0) { if (os2.TypoAscender != 0 || os2.TypoDescender != 0) { this.Ascender = os2.TypoAscender; this.Descender = os2.TypoDescender; this.LineGap = os2.TypoLineGap; this.LineHeight = (short)(this.Ascender - this.Descender + this.LineGap); } else { this.Ascender = (short)os2.WinAscent; this.Descender = (short)-os2.WinDescent; this.LineHeight = (short)(this.Ascender - this.Descender); } } this.UnitsPerEm = this.head.UnitsPerEm; // 72 * UnitsPerEm means 1pt = 1px this.ScaleFactor = this.UnitsPerEm * 72F; this.AdvanceWidthMax = (short)horizontalHeadTable.AdvanceWidthMax; this.AdvanceHeightMax = verticalHeadTable == null ? this.LineHeight : verticalHeadTable.AdvanceHeightMax; this.kerningTable = kern; this.gSubTable = gSubTable; this.gPosTable = gPosTable; this.colrTable = colrTable; this.cpalTable = cpalTable; this.fpgm = fpgm; this.cvt = cvt; this.prep = prep; this.glyphDefinitionTable = glyphDefinitionTable; this.Description = new FontDescription(nameTable, os2, head); }
private void Save_SDF_FontAsset(string filePath) { string relativeAssetPath = filePath; string tex_DirName = Path.GetDirectoryName(relativeAssetPath); string tex_FileName = Path.GetFileNameWithoutExtension(relativeAssetPath); string tex_Path_NoExt = tex_DirName + "/" + tex_FileName; // Check if TextMeshPro font asset already exists. If not, create a new one. Otherwise update the existing one. var fontAsset = AssetDatabase.LoadAssetAtPath <TMP_FontAsset>(tex_Path_NoExt + ".asset"); if (fontAsset == null) { //Debug.Log("Creating TextMeshPro font asset!"); fontAsset = ScriptableObject.CreateInstance <TMP_FontAsset>(); // Create new TextMeshPro Font Asset. AssetDatabase.CreateAsset(fontAsset, tex_Path_NoExt + ".asset"); //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; // If using the C# SDF creation mode, we need the scale down factor. int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_fontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_fontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_font); KerningTable kerningTable = GetKerningTable(fontFilePath, ( int )face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_fontAtlas; if (!m_fontAtlas.name.EndsWith(" Atlas")) { m_fontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_fontAtlas, fontAsset); } // Create new Material and Add it as Sub-Asset Shader default_Shader = Shader.Find("TextMeshPro/Distance Field"); //m_shaderSelection; Material tmp_material = new Material(default_Shader); tmp_material.name = tex_FileName + " Material"; tmp_material.SetTexture(ShaderUtilities.ID_MainTex, m_fontAtlas); tmp_material.SetFloat(ShaderUtilities.ID_TextureWidth, m_fontAtlas.width); tmp_material.SetFloat(ShaderUtilities.ID_TextureHeight, m_fontAtlas.height); int spread = Padding + 1; tmp_material.SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. tmp_material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); tmp_material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); fontAsset.material = tmp_material; AssetDatabase.AddObjectToAsset(tmp_material, fontAsset); } else { // Find all Materials referencing this font atlas. Material[] material_references = FindMaterialReferences(fontAsset); if (fontAsset.atlas) { // Destroy Assets that will be replaced. GameObject.DestroyImmediate(fontAsset.atlas, true); } //Set Font Asset Type fontAsset.fontAssetType = TMP_FontAsset.FontAssetTypes.SDF; int scaleDownFactor = 1; // ((RasterModes)m_RenderMode & RasterModes.Raster_Mode_SDF) == RasterModes.Raster_Mode_SDF || m_RenderMode == RenderModes.DistanceFieldAA ? 1 : font_scaledownFactor; // Add FaceInfo to Font Asset FaceInfo face = GetFaceInfo(m_fontFaceInfo, scaleDownFactor); fontAsset.AddFaceInfo(face); // Add GlyphInfo[] to Font Asset TMP_Glyph[] glyphs = GetGlyphInfo(m_fontGlyphInfo, scaleDownFactor); fontAsset.AddGlyphInfo(glyphs); // Get and Add Kerning Pairs to Font Asset if (IncludeKerningPairs) { string fontFilePath = AssetDatabase.GetAssetPath(m_font); KerningTable kerningTable = GetKerningTable(fontFilePath, ( int )face.PointSize); fontAsset.AddKerningInfo(kerningTable); } // Add Font Atlas as Sub-Asset fontAsset.atlas = m_fontAtlas; if (!m_fontAtlas.name.EndsWith(" Atlas")) { m_fontAtlas.name = tex_FileName + " Atlas"; AssetDatabase.AddObjectToAsset(m_fontAtlas, fontAsset); } // Special handling due to a bug in earlier versions of Unity. m_fontAtlas.hideFlags = HideFlags.None; fontAsset.material.hideFlags = HideFlags.None; // Assign new font atlas texture to the existing material. fontAsset.material.SetTexture(ShaderUtilities.ID_MainTex, fontAsset.atlas); // Update the Texture reference on the Material for (int i = 0; i < material_references.Length; i++) { material_references[i].SetTexture(ShaderUtilities.ID_MainTex, m_fontAtlas); material_references[i].SetFloat(ShaderUtilities.ID_TextureWidth, m_fontAtlas.width); material_references[i].SetFloat(ShaderUtilities.ID_TextureHeight, m_fontAtlas.height); int spread = Padding + 1; material_references[i].SetFloat(ShaderUtilities.ID_GradientScale, spread); // Spread = Padding for Brute Force SDF. material_references[i].SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); material_references[i].SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); } } fontAsset.ReadFontDefinition(); AssetDatabase.SaveAssets(); AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fontAsset)); // Re-import font asset to get the new updated version. AssetDatabase.Refresh(); // NEED TO GENERATE AN EVENT TO FORCE A REDRAW OF ANY TEXTMESHPRO INSTANCES THAT MIGHT BE USING THIS FONT ASSET TMPro_EventManager.ON_FONT_PROPERTY_CHANGED(true, fontAsset); }