Example #1
0
        /// <summary>
        ///   Get the index of the character at position pos
        /// </summary>
        /// <param name="pos">the position of the character within the text draw area</param>
        /// <param name="text"></param>
        /// <param name="lines"></param>
        /// <param name="horzFormat"></param>
        /// <param name="vertFormat"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="leftToRight"></param>
        /// <returns></returns>
        public int GetTextIndexFromPosition(PointF pos, string text,
                                            List <TextRange> lines,
                                            HorizontalTextFormat horzFormat,
                                            VerticalTextFormat vertFormat,
                                            float width, float height,
                                            bool leftToRight)
        {
            float y      = GetTextStartOffset(lines.Count, 0, vertFormat, height);
            float deltaY = pos.Y - y;

            if (deltaY < 0)
            {
                return(0);
            }
            int lineIndex = (int)(deltaY / this.LineSpacing);

            if (lineIndex >= lines.Count)
            {
                return(text.Length);
            }
            float x      = GetTextStartOffset(text, lines[lineIndex], horzFormat, width, leftToRight);
            float deltaX = pos.X - x;

            if (!leftToRight)
            {
                throw new NotImplementedException("rightToLeft layout not fully supported");
            }
            if (deltaX < 0)
            {
                return(lines[lineIndex].start);
            }
            return(GetCharAtPixel(text, lines[lineIndex].start, lines[lineIndex].end - lines[lineIndex].start, deltaX));
        }
Example #2
0
        /// <summary>
        ///  Gets the offset into the rectangle of the top left (or top right
        ///  if right to left) for the character at textIndex.
        /// </summary>
        /// <param name="textIndex"></param>
        /// <returns></returns>
        public PointF GetOffset(string text, List <TextRange> lines,
                                int textIndex,
                                HorizontalTextFormat horzFormat,
                                VerticalTextFormat vertFormat,
                                float width, float height, bool leftToRight)
        {
            int lineIndex = GetLineIndex(lines, textIndex);

            if (lineIndex == -1)
            {
                throw new Exception("Invalid text index");
            }
            TextRange line = lines[lineIndex];
            PointF    rv   = new PointF();

            // Get the top left of the text area
            rv.X = GetTextStartOffset(text, line, horzFormat, width, leftToRight);
            rv.Y = GetTextStartOffset(lines.Count, lineIndex, vertFormat, height);
            // Move to the left past some characters
            if (textIndex > line.end)
            {
                textIndex = line.end;
            }
            rv.X += GetTextExtent(text, line.start, textIndex - line.start);
            return(rv);
        }
Example #3
0
        /// <summary>
        ///		Draw text into a specified area of the display.
        /// </summary>
        /// <param name="text">The text to be drawn.</param>
        /// <param name="area">
        ///		Rect object describing the area of the display where the text is to be rendered.  The text is not clipped to this Rect, but is formatted
        ///		using this Rect depending upon the option specified in <paramref name="format"/>.
        /// </param>
        /// <param name="z">float value specifying the z co-ordinate for the drawn text.</param>
        /// <param name="clip">Rect object describing the clipping area for the drawing.  No drawing will occur outside this Rect.</param>
        /// <param name="format">The text formatting required.</param>
        /// <param name="colors">
        ///		ColorRect object describing the colors to be applied when drawing the text.
        ///		The colors specified in here are applied to each glyph, rather than the text as a whole.
        /// </param>
        /// <returns>The number of lines output.  This does not consider clipping, so if all text was clipped, this would still return >=1.</returns>
        public int DrawText(string text, Rect area, float z, Rect clip,
                            HorizontalTextFormat horzFormat,
                            VerticalTextFormat vertFormat,
                            ColorRect colors)
        {
            List <TextRange> lines = GetLines(text, area.Width, horzFormat, false);

            return(DrawText(text, lines, area, z, clip, horzFormat, vertFormat, colors));
        }
Example #4
0
        public List <TextRange> GetLines(string text, float wrapWidth, HorizontalTextFormat format, bool nonSpaceWrap)
        {
            switch (format)
            {
            case HorizontalTextFormat.Left:
            case HorizontalTextFormat.Right:
            case HorizontalTextFormat.Centered:
                return(TextWrapHelper.GetLines(text));

            case HorizontalTextFormat.WordWrapLeft:
            case HorizontalTextFormat.WordWrapRight:
            case HorizontalTextFormat.WordWrapCentered:
                return(TextWrapHelper.GetWrappedLines(this, text, wrapWidth, nonSpaceWrap));

            default:
                throw new NotImplementedException(string.Format("Unknown text format option '{0}'", format));
            }
        }
Example #5
0
        /// <summary>
        ///   Return the horizontal pixel extent given text would be formatted to.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="formatArea"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public float GetFormattedTextExtent(string text, Rect area, HorizontalTextFormat format)
        {
            float  lineWidth;
            float  widest = 0;
            int    lineStart = 0, lineEnd = 0;
            string currLine;

            Rect tmpDrawArea = area;

            while (lineEnd < text.Length)
            {
                if ((lineEnd = text.IndexOf('\n', lineStart)) == -1)
                {
                    lineEnd = text.Length;
                }
                currLine  = text.Substring(lineStart, lineEnd - lineStart);
                lineStart = lineEnd + 1;                // +1 to skip \n char

                switch (format)
                {
                case HorizontalTextFormat.Centered:
                case HorizontalTextFormat.Right:
                case HorizontalTextFormat.Left:
                    lineWidth = GetTextExtent(currLine);
                    break;

                case HorizontalTextFormat.WordWrapLeft:
                case HorizontalTextFormat.WordWrapRight:
                case HorizontalTextFormat.WordWrapCentered:
                    lineWidth = GetWrappedTextExtent(currLine, area.Width, false);
                    break;

                default:
                    throw new NotImplementedException("Font.GetFormattedTextExtent - Unknown or unsupported TextFormatting value specified.");
                }

                if (lineWidth > widest)
                {
                    widest = lineWidth;
                }
            }

            return(widest);
        }
Example #6
0
        protected float GetTextStartOffset(string text, TextRange line,
                                           HorizontalTextFormat format,
                                           float width, bool leftToRight)
        {
            float baseX = 0;

            switch (format)
            {
            case HorizontalTextFormat.Left:
            case HorizontalTextFormat.WordWrapLeft:
                if (!leftToRight)
                {
                    baseX += GetTextExtent(text, line.start, line.Length);
                }
                break;

            case HorizontalTextFormat.Right:
            case HorizontalTextFormat.WordWrapRight:
                baseX = width;
                if (leftToRight)
                {
                    baseX -= GetTextExtent(text, line.start, line.Length);
                }
                break;

            case HorizontalTextFormat.Centered:
            case HorizontalTextFormat.WordWrapCentered:
                if (leftToRight)
                {
                    baseX = (float)Math.Floor((width - GetTextExtent(text, line.start, line.Length)) * 0.5f);
                }
                else
                {
                    baseX = width - (float)Math.Floor((width - GetTextExtent(text, line.start, line.Length)) * 0.5f);
                }
                break;

            default:
                throw new NotImplementedException(string.Format("Unknown text format option '{0}'", format));
            }
            return(baseX);
        }
Example #7
0
 /// <summary>
 ///		Draw text into a specified area of the display.
 /// </summary>
 /// <param name="text">The text to be drawn.</param>
 /// <param name="area">
 ///		Rect object describing the area of the display where the text is to be rendered.  The text is not clipped to this Rect, but is formatted
 ///		using this Rect depending upon the option specified in <paramref name="format"/>.
 /// </param>
 /// <param name="z">float value specifying the z co-ordinate for the drawn text.</param>
 /// <param name="clip">Rect object describing the clipping area for the drawing.  No drawing will occur outside this Rect.</param>
 /// <param name="format">The text formatting required.</param>
 /// <param name="colors">
 ///		ColorRect object describing the colors to be applied when drawing the text.
 ///		The colors specified in here are applied to each glyph, rather than the text as a whole.
 /// </param>
 /// <returns>The number of lines output.  This does not consider clipping, so if all text was clipped, this would still return >=1.</returns>
 public int DrawText(string text, List <TextRange> lines, Rect area, float z, Rect clip, HorizontalTextFormat horzFormat, VerticalTextFormat vertFormat, ColorRect colors)
 {
     foreach (TextRange line in lines)
     {
         PointF offset = GetOffset(text, lines, line.start, horzFormat, vertFormat, area.Width, area.Height, leftToRight);
         DrawTextLine(text, line.start, line.Length,
                      new Vector3(area.Left + offset.X, area.Top + offset.Y, z),
                      clip, colors);
     }
     return(lines.Count);
 }
Example #8
0
        public float GetFormattedLineCount(string text, Rect area, HorizontalTextFormat format)
        {
            List <TextRange> lines = GetLines(text, area.Width, format, false);

            return(lines.Count);
        }
 /// <summary>
 ///		Draw text into a specified area of the display.
 /// </summary>
 /// <param name="text">The text to be drawn.</param>
 /// <param name="area">
 ///		Rect object describing the area of the display where the text is to be rendered.  The text is not clipped to this Rect, but is formatted
 ///		using this Rect depending upon the option specified in <paramref name="format"/>.
 /// </param>
 /// <param name="z">float value specifying the z co-ordinate for the drawn text.</param>
 /// <param name="clip">Rect object describing the clipping area for the drawing.  No drawing will occur outside this Rect.</param>
 /// <param name="format">The text formatting required.</param>
 /// <param name="colors">
 ///		ColorRect object describing the colors to be applied when drawing the text.
 ///		The colors specified in here are applied to each glyph, rather than the text as a whole.
 /// </param>
 /// <returns>The number of lines output.  This does not consider clipping, so if all text was clipped, this would still return >=1.</returns>
 public int DrawText(string text, List<TextRange> lines, Rect area, float z, Rect clip, HorizontalTextFormat horzFormat, VerticalTextFormat vertFormat, ColorRect colors)
 {
     foreach (TextRange line in lines) {
         PointF offset = GetOffset(text, lines, line.start, horzFormat, vertFormat, area.Width, area.Height, leftToRight);
         DrawTextLine(text, line.start, line.Length,
                      new Vector3(area.Left + offset.X, area.Top + offset.Y, z),
                      clip, colors);
     }
     return lines.Count;
 }
 protected float GetTextStartOffset(string text, TextRange line, 
                                    HorizontalTextFormat format, 
                                    float width, bool leftToRight)
 {
     float baseX = 0;
     switch (format) {
         case HorizontalTextFormat.Left:
         case HorizontalTextFormat.WordWrapLeft:
             if (!leftToRight)
                 baseX += GetTextExtent(text, line.start, line.Length);
             break;
         case HorizontalTextFormat.Right:
         case HorizontalTextFormat.WordWrapRight:
             baseX = width;
             if (leftToRight)
                 baseX -= GetTextExtent(text, line.start, line.Length);
             break;
         case HorizontalTextFormat.Centered:
         case HorizontalTextFormat.WordWrapCentered:
             if (leftToRight)
                 baseX = (float)Math.Floor((width - GetTextExtent(text, line.start, line.Length)) * 0.5f);
             else
                 baseX = width - (float)Math.Floor((width - GetTextExtent(text, line.start, line.Length)) * 0.5f);
             break;
         default:
             throw new NotImplementedException(string.Format("Unknown text format option '{0}'", format));
     }
     return baseX;
 }
 public List<TextRange> GetLines(string text, float wrapWidth, HorizontalTextFormat format, bool nonSpaceWrap)
 {
 }
 public float GetFormattedLineCount(string text, Rect area, HorizontalTextFormat format)
 {
 }
 /// <summary>
 ///   Get the index of the character at position pos
 /// </summary>
 /// <param name="pos">the position of the character within the text draw area</param>
 /// <param name="text"></param>
 /// <param name="lines"></param>
 /// <param name="horzFormat"></param>
 /// <param name="vertFormat"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="leftToRight"></param>
 /// <returns></returns>
 public int GetTextIndexFromPosition(PointF pos, string text, 
                                     List<TextRange> lines,
                                     HorizontalTextFormat horzFormat,
                                     VerticalTextFormat vertFormat,
                                     float width, float height, 
                                     bool leftToRight)
 {
     float y = GetTextStartOffset(lines.Count, 0, vertFormat, height);
     float deltaY = pos.Y - y;
     if (deltaY < 0)
         return 0;
     int lineIndex = (int)(deltaY / this.LineSpacing);
     if (lineIndex >= lines.Count)
         return text.Length;
     float x = GetTextStartOffset(text, lines[lineIndex], horzFormat, width, leftToRight);
     float deltaX = pos.X - x;
     if (!leftToRight)
         throw new NotImplementedException("rightToLeft layout not fully supported");
     if (deltaX < 0)
         return lines[lineIndex].start;
     return GetCharAtPixel(text, lines[lineIndex].start, lines[lineIndex].end - lines[lineIndex].start, deltaX);
 }
 /// <summary>
 ///  Gets the offset into the rectangle of the top left (or top right 
 ///  if right to left) for the character at textIndex.
 /// </summary>
 /// <param name="textIndex"></param>
 /// <returns></returns>
 public PointF GetOffset(string text, List<TextRange> lines, 
                        int textIndex,
                        HorizontalTextFormat horzFormat, 
                        VerticalTextFormat vertFormat, 
                        float width, float height, bool leftToRight)
 {
     int lineIndex = GetLineIndex(lines, textIndex);
     if (lineIndex == -1)
         throw new Exception("Invalid text index");
     TextRange line = lines[lineIndex];
     PointF rv = new PointF();
     // Get the top left of the text area
     rv.X = GetTextStartOffset(text, line, horzFormat, width, leftToRight);
     rv.Y = GetTextStartOffset(lines.Count, lineIndex, vertFormat, height);
     // Move to the left past some characters
     if (textIndex > line.end)
         textIndex = line.end;
     rv.X += GetTextExtent(text, line.start, textIndex - line.start);
     return rv;
 }
 public List<TextRange> GetLines(string text, float wrapWidth, HorizontalTextFormat format, bool nonSpaceWrap)
 {
     switch (format) {
         case HorizontalTextFormat.Left:
         case HorizontalTextFormat.Right:
         case HorizontalTextFormat.Centered:
             return TextWrapHelper.GetLines(text);
         case HorizontalTextFormat.WordWrapLeft:
         case HorizontalTextFormat.WordWrapRight:
         case HorizontalTextFormat.WordWrapCentered:
             return TextWrapHelper.GetWrappedLines(this, text, wrapWidth, nonSpaceWrap);
         default:
             throw new NotImplementedException(string.Format("Unknown text format option '{0}'", format));
     }
 }
 public float GetFormattedLineCount(string text, Rect area, HorizontalTextFormat format)
 {
     List<TextRange> lines = GetLines(text, area.Width, format, false);
     return lines.Count;
 }
Example #17
0
 public List <TextRange> GetLines(string text, float wrapWidth, HorizontalTextFormat format, bool nonSpaceWrap)
 {
 }
        /// <summary>
        ///   Return the horizontal pixel extent given text would be formatted to.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="formatArea"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public float GetFormattedTextExtent(string text, Rect area, HorizontalTextFormat format)
        {
            float lineWidth;
            float widest = 0;
            int lineStart = 0, lineEnd = 0;
            string currLine;

            Rect tmpDrawArea = area;

            while (lineEnd < text.Length) {
                if ((lineEnd = text.IndexOf('\n', lineStart)) == -1)
                    lineEnd = text.Length;
                currLine = text.Substring(lineStart, lineEnd - lineStart);
                lineStart = lineEnd + 1;		// +1 to skip \n char

                switch (format) {
                    case HorizontalTextFormat.Centered:
                    case HorizontalTextFormat.Right:
                    case HorizontalTextFormat.Left:
                        lineWidth = GetTextExtent(currLine);
                        break;

                    case HorizontalTextFormat.WordWrapLeft:
                    case HorizontalTextFormat.WordWrapRight:
                    case HorizontalTextFormat.WordWrapCentered:
                        lineWidth = GetWrappedTextExtent(currLine, area.Width, false);
                        break;

                    default:
                        throw new NotImplementedException("Font.GetFormattedTextExtent - Unknown or unsupported TextFormatting value specified.");
                }

                if (lineWidth > widest)
                    widest = lineWidth;
            }

            return widest;
        }
Example #19
0
 public float GetFormattedLineCount(string text, Rect area, HorizontalTextFormat format)
 {
 }
 /// <summary>
 ///		Draw text into a specified area of the display.
 /// </summary>
 /// <param name="text">The text to be drawn.</param>
 /// <param name="area">
 ///		Rect object describing the area of the display where the text is to be rendered.  The text is not clipped to this Rect, but is formatted
 ///		using this Rect depending upon the option specified in <paramref name="format"/>.
 /// </param>
 /// <param name="z">float value specifying the z co-ordinate for the drawn text.</param>
 /// <param name="clip">Rect object describing the clipping area for the drawing.  No drawing will occur outside this Rect.</param>
 /// <param name="format">The text formatting required.</param>
 /// <param name="colors">
 ///		ColorRect object describing the colors to be applied when drawing the text.
 ///		The colors specified in here are applied to each glyph, rather than the text as a whole.
 /// </param>
 /// <returns>The number of lines output.  This does not consider clipping, so if all text was clipped, this would still return >=1.</returns>
 public int DrawText(string text, Rect area, float z, Rect clip, 
                     HorizontalTextFormat horzFormat, 
                     VerticalTextFormat vertFormat, 
                     ColorRect colors)
 {
     List<TextRange> lines = GetLines(text, area.Width, horzFormat, false);
     return DrawText(text, lines, area, z, clip, horzFormat, vertFormat, colors);
 }