private void RebuildVisualOffsets() { // 本来是打算用一个数组存储偏移,但是实际上很多字符之间的偏移是一样的,为了节省内存这里定义一种新的结构。 CharacterVisualOffsets.Clear(); if (Line.Length == 0) { return; } String text = Document.GetLineText(Line); foreach (var elem in Elements) { GlyphTypeface globalGlyphTypeface = TypefaceGenerator.GetInstance().GenerateGlyphTypeface(new FontFamily("Microsoft YaHei"), elem.FontStyle, elem.FontWeight, elem.FontStretch); GlyphTypeface glyphTypeface = TypefaceGenerator.GetInstance().GenerateGlyphTypeface(Owner.GlyphOption.FontFamily, elem.FontStyle, elem.FontWeight, elem.FontStretch); for (int i = 0; i < elem.Length; i++) { Char ch = text[elem.RelativeOffset + i]; UInt16 indice; Double width; if (glyphTypeface.CharacterToGlyphMap.ContainsKey(ch)) { indice = glyphTypeface.CharacterToGlyphMap[ch]; width = glyphTypeface.AdvanceWidths[indice] * Owner.GlyphOption.FontSize; } else { indice = globalGlyphTypeface.CharacterToGlyphMap[ch]; width = globalGlyphTypeface.AdvanceWidths[indice] * Owner.GlyphOption.FontSize; } CharacterVisualOffsets.Add(width); } } }
public static TypefaceGenerator GetInstance() { if (_instance == null) { _instance = new TypefaceGenerator(); } return(_instance); }
public void GenerateTypeface(FontFamily fontFamily, Int32 fontSize, Double lineHeight) { if (fontFamily == null) { throw new ArgumentNullException("fontFamily"); } _glyphTypeface = TypefaceGenerator.GetInstance().GenerateGlyphTypeface(fontFamily, FontStyle, FontWeight, FontStretch); _globalGlyphTypeface = TypefaceGenerator.GetInstance().GenerateGlyphTypeface(new FontFamily("Microsoft YaHei"), FontStyle, FontWeight, FontStretch); _fontSize = fontSize; _lineHeight = lineHeight; }