protected float CreateTextLine(string line, float y, float sizeScale, bool kerning, ref List <PositionColoredTextured> verts) { lock (_syncObj) { int x = 0; BitmapCharacter lastChar = null; foreach (char character in line) { BitmapCharacter c = Character(character); // Adjust for kerning if (kerning && lastChar != null) { x += GetKerningAmount(lastChar, character); } lastChar = c; if (!char.IsWhiteSpace(character)) { CreateQuad(c, sizeScale, x, y, ref verts); } x += c.XAdvance; } // Make sure there is at least one character if (verts.Count == 0) { BitmapCharacter c = Character(' '); CreateQuad(c, sizeScale, c.XOffset, y, ref verts); } return(x * sizeScale); } }
protected void CreateQuad(BitmapCharacter c, float sizeScale, float x, float y, ref List <PositionColoredTextured> verts) { x += c.XOffset; y += c.YOffset; PositionColoredTextured tl = new PositionColoredTextured( x * sizeScale, y * sizeScale, 1.0f, (c.X + 0.5f) / _charSet.Width, c.Y / (float)_charSet.Height, Color.Transparent ); PositionColoredTextured br = new PositionColoredTextured( (x + c.Width) * sizeScale, (y + c.Height) * sizeScale, 1.0f, (c.X + c.Width) / (float)_charSet.Width, (c.Y + c.Height - 0.5f) / _charSet.Height, Color.Transparent ); PositionColoredTextured bl = new PositionColoredTextured(tl.X, br.Y, 1.0f, tl.Tu1, br.Tv1, Color.Transparent); PositionColoredTextured tr = new PositionColoredTextured(br.X, tl.Y, 1.0f, br.Tu1, tl.Tv1, Color.Transparent); verts.Add(tl); verts.Add(bl); verts.Add(tr); verts.Add(tr); verts.Add(bl); verts.Add(br); }
/// <summary> /// Gets the width of a sub-string if rendered with this <see cref="FontAssetCore"/> as a particular size, excluding the /// special additional width required for the last char. /// </summary> /// <param name="text">The string to measure.</param> /// <param name="fromIndex">The index of the first character of the sub-string.</param> /// <param name="toIndex">The index of the last character of the sub-string to measure.</param> /// <param name="fontSize">The size of font to use for measurement.</param> /// <param name="kerning">Whether kerning is used to improve font spacing.</param> /// <returns>The width of the sub-string text.</returns> public float PartialTextWidth(string text, int fromIndex, int toIndex, float fontSize, bool kerning) { if (!IsAllocated) { Allocate(); } float width = 0; BitmapCharacter lastChar = null; for (int i = fromIndex; i <= toIndex; i++) { BitmapCharacter c = Character(text[i]); // Should never be the case as even characters not in range should be replaced by 0 index-char. // But this check was added for a seldom NullReferenceException. if (c == null) { continue; } width += c.XAdvance; if (kerning && lastChar != null) { width += GetKerningAmount(lastChar, text[i]); } lastChar = c; } return(width * (fontSize / _charSet.RenderedSize)); }
public bool SetCharacter(uint index, BitmapCharacter character) { if (!IsInRange(index)) { return(false); } _characters[index] = character; return(true); }
/// <summary> /// In order to accurately determine the length of a string the final character may need to have a small /// additional width applied to compensate for the amount that it would normally over-hang the following /// character. This function returns the value of that extension for a given character in the passed string /// </summary> /// <param name="text">The string containing the character to measure.</param> /// <param name="charIndex">The index of the character in the string.</param> /// <param name="fontSize">The size of font to use for measurement.</param> /// <returns>The additonal width required for the specified character.</returns> public float CharWidthExtension(string text, int charIndex, float fontSize) { if (charIndex < 0 || charIndex >= text.Length) { return(0.0f); } BitmapCharacter c = Character(text[charIndex]); return(Math.Max(c.Width - c.XAdvance + c.XOffset, 0) * (fontSize / _charSet.RenderedSize)); }
/// <summary> /// Clones the BitmapCharacter. /// </summary> /// <returns>Cloned BitmapCharacter.</returns> public object Clone() { BitmapCharacter result = new BitmapCharacter { X = X, Y = Y, Width = Width, Height = Height, XOffset = XOffset, YOffset = YOffset, XAdvance = XAdvance }; result.KerningList.AddRange(KerningList); result.Page = Page; return(result); }
protected BitmapCharacter Character(char character) { lock (_syncObj) { uint glyphIndex = GlyphIndex(character); BitmapCharacter result = _charSet.GetCharacter(glyphIndex); if (glyphIndex == 0) { result = null; } if (result == null) { if (!AddGlyph(glyphIndex)) { return(_charSet.GetCharacter(0)); } return(_charSet.GetCharacter(glyphIndex)); } return(result); } }
/// <summary> /// Gets the width of a sub-string if rendered with this <see cref="FontAssetCore"/> as a particular size, excluding the /// special additional width required for the last char. /// </summary> /// <param name="text">The string to measure.</param> /// <param name="fromIndex">The index of the first character of the sub-string.</param> /// <param name="toIndex">The index of the last character of the sub-string to measure.</param> /// <param name="fontSize">The size of font to use for measurement.</param> /// <param name="kerning">Whether kerning is used to improve font spacing.</param> /// <returns>The width of the sub-string text.</returns> public float PartialTextWidth(string text, int fromIndex, int toIndex, float fontSize, bool kerning) { if (!IsAllocated) { Allocate(); } float width = 0; BitmapCharacter lastChar = null; for (int i = fromIndex; i <= toIndex; i++) { BitmapCharacter c = Character(text[i]); width += c.XAdvance; if (kerning && lastChar != null) { width += GetKerningAmount(lastChar, text[i]); } lastChar = c; } return(width * (fontSize / _charSet.RenderedSize)); }
protected int GetKerningAmount(BitmapCharacter first, char second) { Kerning result = first.KerningList.FirstOrDefault(node => node.Second == second); return(result == null ? 0 : result.Amount); }
/// <summary> /// Clones the BitmapCharacter. /// </summary> /// <returns>Cloned BitmapCharacter.</returns> public object Clone() { BitmapCharacter result = new BitmapCharacter { X = X, Y = Y, Width = Width, Height = Height, XOffset = XOffset, YOffset = YOffset, XAdvance = XAdvance }; result.KerningList.AddRange(KerningList); result.Page = Page; return result; }
public bool SetCharacter(uint index, BitmapCharacter character) { if (!IsInRange(index)) return false; _characters[index] = character; return true; }
protected int GetKerningAmount(BitmapCharacter first, char second) { Kerning result = first.KerningList.Where(node => node.Second == second).FirstOrDefault(); return result == null ? 0 : result.Amount; }
protected void CreateQuad(BitmapCharacter c, float sizeScale, float x, float y, ref List<PositionColoredTextured> verts) { x += c.XOffset; y += c.YOffset; PositionColoredTextured tl = new PositionColoredTextured( x * sizeScale, y * sizeScale, 1.0f, (c.X + 0.5f) / _charSet.Width, c.Y / (float) _charSet.Height, 0 ); PositionColoredTextured br = new PositionColoredTextured( (x + c.Width) * sizeScale, (y + c.Height) * sizeScale, 1.0f, (c.X + c.Width) / (float) _charSet.Width, (c.Y + c.Height - 0.5f) / _charSet.Height, 0 ); PositionColoredTextured bl = new PositionColoredTextured(tl.X, br.Y, 1.0f, tl.Tu1, br.Tv1, 0); PositionColoredTextured tr = new PositionColoredTextured(br.X, tl.Y, 1.0f, br.Tu1, tl.Tv1, 0); verts.Add(tl); verts.Add(bl); verts.Add(tr); verts.Add(tr); verts.Add(bl); verts.Add(br); }