Пример #1
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);
        }
Пример #2
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));
        }
Пример #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));
        }
Пример #4
0
        protected float GetTextStartOffset(int lineCount, int lineIndex,
                                           VerticalTextFormat format,
                                           float height)
        {
            switch (format)
            {
            case VerticalTextFormat.Top:
                return(lineIndex * this.LineSpacing);

            case VerticalTextFormat.Bottom:
                return(height - (lineCount - lineIndex) * this.LineSpacing);

            case VerticalTextFormat.Centered:
                return((height - lineCount * this.LineSpacing) / 2 + lineIndex * this.LineSpacing);

            default:
                throw new NotImplementedException(string.Format("Unknown text format option '{0}'", format));
            }
        }
Пример #5
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);
 }
 protected float GetTextStartOffset(int lineCount, int lineIndex,
                                    VerticalTextFormat format,
                                    float height)
 {
     switch (format) {
         case VerticalTextFormat.Top:
             return lineIndex * this.LineSpacing;
         case VerticalTextFormat.Bottom:
             return height - (lineCount - lineIndex) * this.LineSpacing;
         case VerticalTextFormat.Centered:
             return (height - lineCount * this.LineSpacing) / 2 + lineIndex * this.LineSpacing;
         default:
             throw new NotImplementedException(string.Format("Unknown text format option '{0}'", 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;
 }
 /// <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;
 }
Пример #10
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);
 }