public static GlyphString Concat(GlyphString g1, GlyphString g2) { if (g1 == null) { return(g2); } if (g2 == null) { return(g1); } int concatLen = g1.Length + g2.Length; GlyphChar[] arr = new GlyphChar[concatLen]; Array.Copy(g1.Glyphs, arr, g1.Length); // Attention: Very confusing parameter order in the // many overloads for the System.Array functions // here we use: SourceArray, SourceIndex, DestArray, DestIndex, DestLength (hopefully) Array.Copy(g2.Glyphs, 0, arr, g1.Length, g2.Length); GlyphString result = new GlyphString(arr); result.Length = concatLen; result.Width = g1.Width + g2.Width; return(result); }
const int minextra = 31; // minimum extra space to reserve /// <summary> /// This will reserve some extra free space for fast subsequent appends /// </summary> /// <returns><c>true</c>, if char was appended, <c>false</c> otherwise.</returns> /// <param name="c">The character to append</param> /// <param name="font">Font.</param> /// <param name="flags">Flags.</param> public int Append(char c, IGUIFont font, SpecialCharacterFlags flags = SpecialCharacterFlags.None) { if (c == '\r' || font == null) { return(0); } GlyphChar g = font.GetGlyph(c, flags); if (g.Glyph > 0) { try { int idx = 0; if (Length == 0) { m_Glyphs = new GlyphChar[++Length]; } else { idx = Length++; if (idx >= m_Glyphs.Length) { Array.Resize(ref m_Glyphs, idx + Math.Max(minextra, Length / 3)); } } Glyphs[idx] = g; Width += g.Width; return(1); } catch (Exception ex) { ex.LogError(); } } return(0); }
public void ParseString(string text, IGUIFont font, SpecialCharacterFlags flags = SpecialCharacterFlags.None) { Length = 0; GlyphChar[] tmp = null; if (!String.IsNullOrEmpty(text) && font != null) { tmp = new GlyphChar[text.Length]; int w = 0; for (int i = 0; i < text.Length; i++) { GlyphChar g = font.GetGlyph(text [i], flags); w += g.Width; tmp [i] = g; } Width = w; Length = text.Length; } if (tmp == null) { tmp = new GlyphChar[0]; } if (m_Glyphs == null) // called from constructor { m_Glyphs = tmp; } else { Concurrency.LockFreeUpdate(ref m_Glyphs, tmp); } }
public int Insert(int index, string text, IGUIFont font, SpecialCharacterFlags flags = SpecialCharacterFlags.None) { if (String.IsNullOrEmpty(text)) { return(0); } // we must handle 3 cases: // (1) insert at index 0, same implementation as (2) would lead to array-copy errors // (2) insert in the middle // (3) append at the end, same as with (1) if (index >= Length) { return(Append(text, font, flags)); } List <GlyphChar> glyphs = new List <GlyphChar> (text.Length); for (int i = 0; i < text.Length; i++) { GlyphChar g = font.GetGlyph(text [i], flags); if (g.Glyph != 0) { glyphs.Add(g); } } if (glyphs.Count == 0) { return(0); } GlyphChar[] source = glyphs.ToArray(); int sourceLen = source.Length; int newLen = Glyphs.Length + sourceLen + 1; GlyphChar[] arr = new GlyphChar[newLen]; if (index <= 0) { Array.Copy(source, arr, sourceLen); Array.Copy(Glyphs, 0, arr, sourceLen, Length); } else { Array.Copy(Glyphs, 0, arr, 0, index); Array.Copy(source, index, arr, 0, sourceLen); Array.Copy(Glyphs, index + sourceLen, arr, index, Length - index); } Length += sourceLen; Width += source.Sum(p => p.Width); return(sourceLen); }
public int Insert(int index, char c, IGUIFont font, SpecialCharacterFlags flags = SpecialCharacterFlags.None) { if (c == '\r' || font == null) { return(0); } // we must handle 3 cases: // (1) insert at index 0, same implementation as (2) would lead to array-copy errors // (2) insert in the middle // (3) append at the end, same as with (1) if (index >= Length) { return(Append(c, font, flags)); } // first see, if we get a valid Glyph GlyphChar glyph = font.GetGlyph(c, flags); if (glyph.Glyph == 0) { return(0); } int newLen = Glyphs.Length; if (Length + 1 >= newLen) { newLen += minextra; } GlyphChar[] arr = new GlyphChar[newLen]; if (index <= 0) { Array.Copy(Glyphs, arr, 1); Glyphs [0] = glyph; } else { Array.Copy(Glyphs, 0, arr, 0, index); Glyphs [index] = glyph; Array.Copy(Glyphs, index + 1, arr, index, Length - index); } Length++; Width += glyph.Width; return(1); }
public static GlyphString SubString(GlyphString g, int start, int length) { if (g == null) { return(null); } length = Math.Min(length, g.Length - (length - start + 1)); if (length <= 0) { return(new GlyphString()); } GlyphChar[] arr = new GlyphChar[length]; Array.Copy(g.Glyphs, arr, length); GlyphString result = new GlyphString(arr); result.Length = length; result.Width = arr.Sum(p => p.Width); return(result); }