private void CreateCharacterGlyph(char lineCharacter, ref float totalGlyphWidth, float lineStartX) { Glyph characterGlyph = GetGlyphFromDictionary(lineCharacter); totalGlyphWidth += GetKerningFromDictionary(lineCharacter, lastGlyph); var newDrawInfo = PlaceGlyphInLine(characterGlyph, lineStartX, totalGlyphWidth); glyphs.Add(newDrawInfo); lastDrawData = newDrawInfo; totalGlyphWidth += (float)Math.Round(characterGlyph.AdvanceWidth); lastGlyph = characterGlyph; }
private void LoadGlyph(XmlData glyphData, Size fontMapSize) { char character = glyphData.GetAttributeValue("Character", ' '); var glyph = new Glyph { UV = new Rectangle(glyphData.GetAttributeValue("UV")), LeftSideBearing = glyphData.GetAttributeValue("LeftBearing", 0.0f), RightSideBearing = glyphData.GetAttributeValue("RightBearing", 0.0f) }; glyph.AdvanceWidth = glyphData.GetAttributeValue("AdvanceWidth", glyph.UV.Width - 2.0f); glyph.PrecomputedFontMapUV = Rectangle.BuildUVRectangle(glyph.UV, fontMapSize); GlyphDictionary.Add(character, glyph); }
private Glyph ParseCharacter(Size maxTextPixelSize, bool isClipping, Glyph lastGlyph, List<char> currentTextLine) { char character = parsedLine[characterIndex]; glyph = glyphDictionary[character]; float glyphWidth = glyph.AdvanceWidth; glyphWidth += GetGlyphWidth(character, lastGlyph); glyphWidth = GetNextFullPixel(glyphWidth); glyphWidth += GetGlyphWidthOfNextCharacterInLine(); if (isClipping) HandleClipping(maxTextPixelSize, currentTextLine, character, glyphWidth); else HandleNoClipping(currentTextLine, character, glyphWidth); lastGlyph = glyph; return lastGlyph; }
private GlyphDrawData PlaceGlyphInLine(Glyph characterGlyph, float lineStartX, float totalGlyphWidth) { var glyph = new GlyphDrawData(); var position = new Vector2D((lineStartX + totalGlyphWidth + characterGlyph.LeftSideBearing).Round(), lastDrawData.DrawArea.Top); glyph.DrawArea = new Rectangle(position, characterGlyph.UV.Size); glyph.UV = characterGlyph.PrecomputedFontMapUV; return glyph; }
private static int GetKerningFromDictionary(char textChar, Glyph lastGlyph) { int characterKerning; return lastGlyph != null && lastGlyph.Kernings != null && lastGlyph.Kernings.TryGetValue(textChar, out characterKerning) ? characterKerning : 0; }
private static int GetGlyphWidth(char character, Glyph lastGlyph) { int charKerning; if (lastGlyph != null && lastGlyph.Kernings != null && lastGlyph.Kernings.TryGetValue(character, out charKerning)) return charKerning; return 0; }
public List<List<char>> SplitTextIntoLines(string text, Size maxTextPixelSize, bool isClipping) { characterLines = new List<List<char>>(); if (String.IsNullOrEmpty(text)) return characterLines; if (isClipping && IsFontFittingIntoHeight(maxTextPixelSize.Height)) return characterLines; MaxTextLineWidth = 0.0f; TextLineWidths = new List<float>(); parsedLines = parser.GetLines(text); for (lineIndex = 0; lineIndex < parsedLines.Count; lineIndex++) { parsedLine = parsedLines[lineIndex]; var currentTextLine = new List<char>(); currentTextLineWidth = 0.0f; currentWord = new List<char>(); currentWordWidth = 0.0f; wordNumber = 0; Glyph lastGlyph = null; for (characterIndex = 0; characterIndex < parsedLine.Count; characterIndex++) { char character = parsedLine[characterIndex]; glyph = glyphDictionary[character]; float glyphWidth = glyph.AdvanceWidth; glyphWidth += GetGlyphWidth(character, lastGlyph); glyphWidth = GetNextFullPixel(glyphWidth); glyphWidth += GetGlyphWidthOfNextCharacterInLine(); if (isClipping) { currentWord.Add(character); if (!IsSpaceOrTab(character)) currentWordWidth += glyphWidth; if (IsSpaceOrTab(character) || IsLastCharacterInLine()) { if (IsEnoughSpaceForWordInCurrentLineAvailable(maxTextPixelSize) || wordNumber == 0) { currentTextLine.AddRange(currentWord); currentTextLineWidth += currentWordWidth; if (IsSpaceOrTab(character)) currentTextLineWidth += glyphWidth; wordNumber++; } else MoveRestOfLineToTheNextLine(); currentWord = new List<char>(); currentWordWidth = 0.0f; } } else { currentTextLine.Add(character); currentTextLineWidth += glyphWidth; } lastGlyph = glyph; } characterLines.Add(currentTextLine); TextLineWidths.Add(currentTextLineWidth); UpdateMaxTextLineWidth(); if (!IsEnoughSpaceForNextLineAvailable(isClipping, maxTextPixelSize)) break; } return characterLines; }