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);
        }
Exemple #4
0
        public void RefreshGlyphs(IGUIFont font, SpecialCharacterFlags flags)
        {
            GlyphList glyphs = new GlyphList();

            foreach (GlyphChar g in Glyphs)
            {
                glyphs.AddLast(font.GetGlyph(g.Char, flags));
            }

            Concurrency.LockFreeUpdate(ref m_Glyphs, glyphs);
            NeedsWordWrap = true;
        }
        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);
        }
Exemple #6
0
        public bool InsertChar(int pos, char c, IGUIFont font, SpecialCharacterFlags flags)
        {
            GlyphChar g = font.GetGlyph(c, flags);

            if (g.Glyph > 0)
            {
                try {
                    Glyphs.InsertAt(pos, g);
                    NeedsWordWrap = true;
                    return(true);
                } catch (Exception ex) {
                    ex.LogError();
                }
            }
            return(false);
        }
Exemple #7
0
        public bool AppendChar(char c, IGUIFont font, SpecialCharacterFlags flags)
        {
            GlyphChar g = font.GetGlyph(c, flags);

            if (g.Glyph > 0)
            {
                try {
                    Glyphs.AddLast(g);
                    NeedsWordWrap = true;
                    return(true);
                } catch (Exception ex) {
                    ex.LogError();
                }
            }
            return(false);
        }