void Init() { Release(); mainTexture = new NTexture(_fontAsset.atlasTexture); mainTexture.destroyMethod = DestroyMethod.None; _manager = mainTexture.GetMaterialManager(this.shader); _manager.onCreateNewMaterial += OnCreateNewMaterial; _material = new Material(_fontAsset.material); //copy _material.SetFloat(ShaderUtilities.ID_TextureWidth, mainTexture.width); _material.SetFloat(ShaderUtilities.ID_TextureHeight, mainTexture.height); _material.SetFloat(ShaderUtilities.ID_GradientScale, fontAsset.atlasPadding + 1); _material.SetFloat(ShaderUtilities.ID_WeightNormal, fontAsset.normalStyle); _material.SetFloat(ShaderUtilities.ID_WeightBold, fontAsset.boldStyle); // _ascent = _fontAsset.faceInfo.ascentLine; // _lineHeight = _fontAsset.faceInfo.lineHeight; _ascent = _fontAsset.faceInfo.pointSize; _lineHeight = _fontAsset.faceInfo.pointSize * 1.25f; bool isAlternativeTypeface; TMP_FontAsset actualAsset; _lineChar = TMP_FontAssetUtilities.GetCharacterFromFontAsset('_', _fontAsset, true, FontStyles.Normal, _fontWeight, out isAlternativeTypeface, out actualAsset); }
override public bool GetGlyph(char ch, out float width, out float height, out float baseline) { _char = GetCharacterFromFontAsset(ch, _style); if (_char != null) { width = _char.glyph.metrics.horizontalAdvance * _boldMultiplier * _scale; height = _lineHeight * _scale; baseline = _ascent * _scale; if (_format.specialStyle == TextFormat.SpecialStyle.Subscript) { height /= SupScale; baseline /= SupScale; } else if (_format.specialStyle == TextFormat.SpecialStyle.Superscript) { height = height / SupScale + baseline * SupOffset; baseline *= (SupOffset + 1 / SupScale); } height = Mathf.RoundToInt(height); baseline = Mathf.RoundToInt(baseline); return(true); } else { width = 0; height = 0; baseline = 0; return(false); } }
/// <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> /// Draws a Text Mesh Pro glyph in the supplied rect. Used for inspectors. /// </summary> private static void EditorDrawTMPGlyph(Rect position, TMP_FontAsset fontAsset, TMP_Character character) { try { float iconSizeMultiplier = 0.625f; // Get a reference to the Glyph Table int glyphIndex = (int)character.glyphIndex; int elementIndex = fontAsset.glyphTable.FindIndex(item => item.index == glyphIndex); // Return if we can't find the glyph if (elementIndex == -1) { return; } Glyph glyph = character.glyph; // Get reference to atlas texture. int atlasIndex = glyph.atlasIndex; Texture2D atlasTexture = fontAsset.atlasTextures.Length > atlasIndex ? fontAsset.atlasTextures[atlasIndex] : null; if (atlasTexture == null) { return; } if (fontRenderMat == null) { fontRenderMat = new Material(Shader.Find("Mixed Reality Toolkit/TextMeshPro")); } Material mat = fontRenderMat; mat.mainTexture = atlasTexture; mat.SetColor("_Color", Color.white); // Draw glyph Rect glyphDrawPosition = new Rect( position.x, position.y, position.width, position.height); int glyphOriginX = glyph.glyphRect.x; int glyphOriginY = glyph.glyphRect.y; int glyphWidth = glyph.glyphRect.width; int glyphHeight = glyph.glyphRect.height; float normalizedHeight = fontAsset.faceInfo.ascentLine - fontAsset.faceInfo.descentLine; float scale = Mathf.Min(glyphDrawPosition.width, glyphDrawPosition.height) / normalizedHeight * iconSizeMultiplier; // Compute the normalized texture coordinates Rect texCoords = new Rect((float)glyphOriginX / atlasTexture.width, (float)glyphOriginY / atlasTexture.height, (float)glyphWidth / atlasTexture.width, (float)glyphHeight / atlasTexture.height); if (Event.current.type == EventType.Repaint) { glyphHeight = (int)Mathf.Min(maxButtonSize, glyphHeight * scale); glyphWidth = (int)Mathf.Min(maxButtonSize, glyphWidth * scale); glyphDrawPosition.x += (glyphDrawPosition.width - glyphWidth) / 2; glyphDrawPosition.y += (glyphDrawPosition.height - glyphHeight) / 2; glyphDrawPosition.width = glyphWidth; glyphDrawPosition.height = glyphHeight; // Could switch to using the default material of the font asset which would require passing scale to the shader. Graphics.DrawTexture(glyphDrawPosition, atlasTexture, texCoords, 0, 0, 0, 0, new Color(1f, 1f, 1f), mat); } } catch (Exception) { EditorGUILayout.LabelField("Couldn't draw character icon. Character may not be available in the font asset."); } }