private float BlitString(string str, float cursorX, float cursorY, float baseScale) { TFontGlyph prevGlyph = null; foreach (char c in str) { uint charCode = (uint)c; if (!CustomTextConfig.Instance.Glyphs.ContainsKey(charCode)) { continue; } TFontGlyph glyph = CustomTextConfig.Instance.Glyphs[charCode]; float kerning = 0f; if (prevGlyph != null) { kerning = prevGlyph.GetKerning(charCode) * Size; } BlitQuad(cursorX, glyph, baseScale); cursorX += glyph.metrics.xAdvance * baseScale + kerning; prevGlyph = glyph; } if (cursorX > Width) { Width = cursorX; } return(cursorX); }
private void BlitQuad(float cursorX, TFontGlyph glyph, float baseScale) { float atlasWidth = CustomTextConfig.Instance.TFontFace.atlasWidth; float atlasHeight = CustomTextConfig.Instance.TFontFace.atlasHeight; float padding = 1.25f; Vector3 top_left; top_left.x = (glyph.metrics.xOffset - padding) * baseScale + cursorX; top_left.y = (glyph.metrics.yOffset + padding) * baseScale; top_left.z = 0; Vector3 bottom_left; bottom_left.x = top_left.x; bottom_left.y = top_left.y - ((glyph.metrics.height + padding * 2) * baseScale); bottom_left.z = 0; Vector3 top_right; top_right.x = bottom_left.x + ((glyph.metrics.width + padding * 2) * baseScale); top_right.y = top_left.y; top_right.z = 0; Vector3 bottom_right; bottom_right.x = top_right.x; bottom_right.y = bottom_left.y; bottom_right.z = 0; Vector2 uv_bottom_left; uv_bottom_left.x = (glyph.rect.x - padding) / atlasWidth; uv_bottom_left.y = (glyph.rect.y - padding) / atlasHeight; Vector2 uv_top_left; uv_top_left.x = uv_bottom_left.x; uv_top_left.y = (glyph.rect.y + padding + glyph.rect.height) / atlasHeight; Vector2 uv_top_right; uv_top_right.x = (glyph.rect.x + padding + glyph.rect.width) / atlasWidth; uv_top_right.y = uv_top_left.y; Vector2 uv_bottom_right; uv_bottom_right.x = uv_top_right.x; uv_bottom_right.y = uv_bottom_left.y; int index = m_Vertices.Count; m_Vertices.Add(bottom_left); m_Vertices.Add(bottom_right); m_Vertices.Add(top_left); m_Vertices.Add(top_right); m_UVs.Add(uv_bottom_left); m_UVs.Add(uv_bottom_right); m_UVs.Add(uv_top_left); m_UVs.Add(uv_top_right); m_Triangles.Add(index); m_Triangles.Add(index + 2); m_Triangles.Add(index + 1); m_Triangles.Add(index + 1); m_Triangles.Add(index + 2); m_Triangles.Add(index + 3); }
public void Init() { string path = "/Users/denghaiyang/workSpace/TestWork/Assets/TextMesh Pro/Resources/Fonts & Materials/Songti SDF"; m_GlyphDictionary.Clear(); m_KerningDictionary.Clear(); var content = ""; using (var sr = File.OpenText(path)) { content = sr.ReadToEnd(); } var jsonStruct = JsonUtility.FromJson <TMPro_FontJsonStruct>(content); m_TFontFace = new TFontFace( jsonStruct.faceInfo.baseline, jsonStruct.faceInfo.strikethroughOffset, jsonStruct.faceInfo.underlineThickness, jsonStruct.faceInfo.underlineOffset, jsonStruct.faceInfo.subscriptSize, jsonStruct.faceInfo.subscriptOffset, jsonStruct.faceInfo.superscriptSize, jsonStruct.faceInfo.superscriptOffset, jsonStruct.faceInfo.descentLine, jsonStruct.faceInfo.tabWidth, jsonStruct.faceInfo.meanLine, jsonStruct.faceInfo.capLine, jsonStruct.faceInfo.ascentLine, jsonStruct.faceInfo.lineHeight, jsonStruct.faceInfo.scale, jsonStruct.faceInfo.pointSize, jsonStruct.faceInfo.styleName, jsonStruct.faceInfo.familyName, jsonStruct.faceInfo.strikethroughThickness, jsonStruct.faceInfo.atlasWidth, jsonStruct.faceInfo.atlasHeight, jsonStruct.faceInfo.padding ); if (jsonStruct.glyphAdjustmentTableCount > 0) { for (int i = 0; i < jsonStruct.glyphAdjustmentTableCount; i++) { var item = jsonStruct.glyphAdjustmentTable[i]; if (m_KerningDictionary.ContainsKey(item.glyphIndex)) { continue; } m_KerningDictionary.Add(item.glyphIndex, new TFontGlyphAdjustmentRecord[2] { new TFontGlyphAdjustmentRecord() { glyphIndex = item.firstAdjustmentRecord.glyphIndex, xAdvance = item.firstAdjustmentRecord.xAdvance, }, new TFontGlyphAdjustmentRecord() { glyphIndex = item.secondAdjustmentRecord.glyphIndex, xAdvance = item.secondAdjustmentRecord.xAdvance, }, }); } } if (jsonStruct.characterTableCount > 0) { for (int i = 0; i < jsonStruct.characterTableCount; i++) { var item = jsonStruct.characterTable[i]; if (m_GlyphDictionary.ContainsKey(item.unicode)) { continue; } TFontGlyph m_TFontGlyph = new TFontGlyph( new Rect(item.glyphRect.x, item.glyphRect.y, item.glyphRect.width, item.glyphRect.height), item.glyphMetrics.width, item.glyphMetrics.height, item.glyphMetrics.xOffset, item.glyphMetrics.yOffset, item.glyphMetrics.xAdvance ); if (m_KerningDictionary.ContainsKey(item.glyphIndex)) { var kerning = m_KerningDictionary[item.glyphIndex]; m_TFontGlyph.kerning = new Dictionary <uint, float>() { { kerning[1].glyphIndex, kerning[1].xAdvance } }; } m_GlyphDictionary.Add(item.unicode, m_TFontGlyph); } } }