void BuildLine(OpenGLFont font, string text, int startIndex, float x, float y, float spaceAdjust) { for (int i = 0; i < text.Length; i++, startIndex++) { char c = text[i]; Glyphs[startIndex] = new GlyphPosition(x, y, font.CharacterWidths[c], font.myHeight); FontCoords[startIndex] = font.TextureCoordinates[c]; x += font.CharacterWidths[c] - font.myLeadingSpace - font.myTrailingSpace; if (c == ' ') { x += spaceAdjust; } short stripindex = (short)(startIndex * 4); short index = (short)(startIndex * 6); Indices[index] = stripindex; Indices[index + 1] = (short)(stripindex + 1); Indices[index + 2] = (short)(stripindex + 2); Indices[index + 3] = (short)(stripindex + 2); Indices[index + 4] = (short)(stripindex + 1); Indices[index + 5] = (short)(stripindex + 3); } }
public GlyphRun(OpenGLFont font, string text, float sizeWidth, float sizeHeight, OpenGLTextAlignment alignment, bool autoEllipsis) { Font = font; List <LineBreak> linebreaks = new List <LineBreak>(); string processingText = text; int totalHeight = 0; int totalWidth = 0; int totalChars = 0; int intWidth; if (sizeWidth != float.PositiveInfinity) { intWidth = (int)sizeWidth; } else { intWidth = int.MaxValue; } int intHeight; if (sizeHeight != float.PositiveInfinity) { intHeight = (int)sizeHeight; } else { intHeight = int.MaxValue; } LineBreak lineBreak = FitString(processingText, 0, intWidth); while (lineBreak.Index != processingText.Length) { LineBreak nextBreak = FitString(processingText, lineBreak.Index, intWidth); // see if this line needs ellipsis if (Font.myHeight + Font.myHeight + totalHeight > intHeight && autoEllipsis) { string lineText = lineBreak.Text; int ellipsisStart = lineText.Length - 3; if (ellipsisStart < 0) { ellipsisStart = 0; } lineText = lineText.Substring(0, ellipsisStart) + "..."; lineBreak.Width = MeasureString(lineText); lineBreak.Text = lineText; break; } linebreaks.Add(lineBreak); totalWidth = Math.Max(totalWidth, lineBreak.Width); totalHeight += Font.myHeight; totalChars += lineBreak.Text.Length; lineBreak = nextBreak; } linebreaks.Add(lineBreak); totalHeight += Font.myHeight; totalWidth = Math.Max(totalWidth, lineBreak.Width); totalChars += lineBreak.Text.Length; myTriangleCount = totalChars * 2; GlyphPosition[] rectangles = new GlyphPosition[totalChars]; GlyphTexCoords[] texCoords = new GlyphTexCoords[totalChars]; short[] indices = new short[totalChars * 6]; Glyphs = rectangles; FontCoords = texCoords; Indices = indices; if (sizeWidth == float.PositiveInfinity) { myWidth = totalWidth; } else { myWidth = sizeWidth; } if (sizeHeight == float.PositiveInfinity) { myHeight = totalHeight; } else { myHeight = sizeHeight; } float y = 0; int curChars = 0; for (int i = 0; i < linebreaks.Count; i++) { LineBreak lbreak = linebreaks[i]; float x; float spaceAdjust = 0; string lbreakText = lbreak.Text; switch (alignment) { case OpenGLTextAlignment.Left: x = 0; break; case OpenGLTextAlignment.Right: x = myWidth - lbreak.Width; break; case OpenGLTextAlignment.Center: x = (myWidth - lbreak.Width) / 2; break; case OpenGLTextAlignment.Justified: x = 0; if (i != linebreaks.Count - 1) { lbreakText = lbreakText.TrimStart(' ').TrimEnd(' '); int spaceCount = 0; foreach (char c in lbreakText) { if (c == ' ') { spaceCount++; } } int newWidth = MeasureString(lbreakText); if (spaceCount != 0) { spaceAdjust = (myWidth - newWidth) / spaceCount; } } break; default: throw new ArgumentException("Unknown alignment type."); } BuildLine(font, lbreakText, curChars, x, y, spaceAdjust); y += Font.myHeight; curChars += lbreakText.Length; } }
public GlyphRun(OpenGLFont font, string text) : this(font, text, float.PositiveInfinity, float.PositiveInfinity, OpenGLTextAlignment.Left, true) { }