/// <summary> /// Load a TextMeshPro 2.0.x asset used for this to use for SDF rendering. /// Mods can use DaggerfallUI.Instance.Font0 through Font4 to access the instances used by other game windows. /// Custom font should be set only during startup. /// </summary> /// <param name="path">Path to a TextMeshPro 1.3.x font asset.</param> public void LoadSDFFontAsset(string path) { // Attempt to load a TextMeshPro font asset TMP_FontAsset tmpFont = Resources.Load <TMP_FontAsset>(path); if (!tmpFont) { return; } // Create font info SDFFontInfo fi = new SDFFontInfo(); fi.pointSize = tmpFont.faceInfo.pointSize; fi.atlasTexture = tmpFont.atlasTexture; fi.baseline = tmpFont.faceInfo.baseline; fi.glyphs = new Dictionary <int, SDFGlyphInfo>(); // Cache glyph info float atlasWidth = tmpFont.atlasTexture.width; float atlasHeight = tmpFont.atlasTexture.height; foreach (var kvp in tmpFont.characterLookupTable) { // Compose glyph rect inside of atlas TMP_Character character = kvp.Value; float atlasGlyphX = character.glyph.glyphRect.x / atlasWidth; float atlasGlyphY = character.glyph.glyphRect.y / atlasHeight; float atlasGlyphWidth = character.glyph.glyphRect.width / atlasWidth; float atlasGlyphHeight = character.glyph.glyphRect.height / atlasHeight; Rect atlasGlyphRect = new Rect(atlasGlyphX, atlasGlyphY, atlasGlyphWidth, atlasGlyphHeight); // Store information about this glyph SDFGlyphInfo glyphInfo = new SDFGlyphInfo() { code = (int)kvp.Key, rect = atlasGlyphRect, offset = new Vector2(character.glyph.metrics.horizontalBearingX, character.glyph.metrics.horizontalBearingY), size = new Vector2(character.glyph.metrics.width, character.glyph.metrics.height), advance = character.glyph.metrics.horizontalAdvance, }; fi.glyphs.Add((int)kvp.Key, glyphInfo); } // Set live font info sdfFontInfo = fi; }
/// <summary> /// Load a TextMeshPro 1.3.x asset used for this to use for SDF rendering. /// Mods can use DaggerfallUI.Instance.Font1 through Font4 to access the instances used by other game windows. /// Custom font should be set only during startup. /// </summary> /// <param name="path">Path to a TextMeshPro 1.3.x font asset.</param> public void LoadSDFFontAsset(string path) { // Attempt to load a TextMeshPro font asset TMP_FontAsset tmpFont = Resources.Load <TMP_FontAsset>(path); if (!tmpFont) { return; } // Create font info SDFFontInfo fi = new SDFFontInfo(); fi.pointSize = tmpFont.fontInfo.PointSize; fi.atlas = tmpFont.atlas; fi.baseline = tmpFont.fontInfo.Baseline; fi.glyphs = new Dictionary <int, SDFGlyphInfo>(); // Cache glyph info float atlasWidth = tmpFont.atlas.width; float atlasHeight = tmpFont.atlas.height; foreach (var kvp in tmpFont.characterDictionary) { // Compose glyph rect inside of atlas TMP_Glyph glyph = kvp.Value; float atlasGlyphX = glyph.x / atlasWidth; float atlasGlyphY = (atlasHeight - glyph.y - glyph.height) / atlasHeight; float atlasGlyphWidth = glyph.width / atlasWidth; float atlasGlyphHeight = glyph.height / atlasHeight; Rect atlasGlyphRect = new Rect(atlasGlyphX, atlasGlyphY, atlasGlyphWidth, atlasGlyphHeight); // Store information about this glyph SDFGlyphInfo glyphInfo = new SDFGlyphInfo() { code = kvp.Key, rect = atlasGlyphRect, offset = new Vector2(glyph.xOffset, glyph.yOffset), size = new Vector2(glyph.width, glyph.height), advance = glyph.xAdvance, }; fi.glyphs.Add(kvp.Key, glyphInfo); } // Set live font info sdfFontInfo = fi; }