void RefreshLookupDictionary(SerializedObject so) { if (m_GlyphLookupDictionary != null) { m_GlyphLookupDictionary.Clear(); FontAssetEditorUtilities.PopulateGlyphProxyLookupDictionary(so, m_GlyphLookupDictionary); } m_ShouldRefreshLookupDictionary = false; }
void DrawGlyph(uint glyphIndex, Rect position, SerializedProperty property) { // Get a reference to the serialized object which can either be a TMP_FontAsset or FontAsset. SerializedObject so = property.serializedObject; if (so == null) { return; } if (m_GlyphLookupDictionary == null) { m_GlyphLookupDictionary = new Dictionary <uint, GlyphProxy>(); FontAssetEditorUtilities.PopulateGlyphProxyLookupDictionary(so, m_GlyphLookupDictionary); } // Try getting a reference to the glyph for the given glyph index. if (!m_GlyphLookupDictionary.TryGetValue(glyphIndex, out GlyphProxy glyph)) { return; } // Get the atlas index of the glyph and lookup its atlas texture SerializedProperty atlasTextureProperty = so.FindProperty("m_AtlasTextures"); Texture2D atlasTexture = atlasTextureProperty.GetArrayElementAtIndex(glyph.atlasIndex).objectReferenceValue as Texture2D; if (atlasTexture == null) { return; } Material mat; GlyphRenderMode atlasRenderMode = (GlyphRenderMode)so.FindProperty("m_AtlasRenderMode").intValue; int padding = so.FindProperty("m_AtlasPadding").intValue; if (((GlyphRasterModes)atlasRenderMode & GlyphRasterModes.RASTER_MODE_BITMAP) == GlyphRasterModes.RASTER_MODE_BITMAP) { mat = FontAssetEditor.internalBitmapMaterial; if (mat == null) { return; } mat.mainTexture = atlasTexture; } else { mat = FontAssetEditor.internalSDFMaterial; if (mat == null) { return; } mat.mainTexture = atlasTexture; mat.SetFloat(TextShaderUtilities.ID_GradientScale, padding + 1); } // Draw glyph from atlas texture. Rect glyphDrawPosition = new Rect(position.x, position.y + 2, position.width, position.height); GlyphRect glyphRect = glyph.glyphRect; int glyphOriginX = glyphRect.x - padding; int glyphOriginY = glyphRect.y - padding; int glyphWidth = glyphRect.width + padding * 2; int glyphHeight = glyphRect.height + padding * 2; SerializedProperty faceInfoProperty = so.FindProperty("m_FaceInfo"); float ascentLine = faceInfoProperty.FindPropertyRelative("m_AscentLine").floatValue; float descentLine = faceInfoProperty.FindPropertyRelative("m_DescentLine").floatValue; float normalizedHeight = ascentLine - descentLine; float scale = glyphDrawPosition.width / normalizedHeight; // 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) { glyphDrawPosition.x += -(glyphWidth * scale / 2 - padding * scale); glyphDrawPosition.y += glyphDrawPosition.height - glyph.metrics.horizontalBearingY * scale; glyphDrawPosition.width = glyphWidth * scale; glyphDrawPosition.height = glyphHeight * scale; // 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); } }