示例#1
0
        private void DrawText(string text, int offset, int count, Vector3 drawPos, Rect clipRect, TextStyle style)
        {
            if (log.IsDebugEnabled)
            {
                log.DebugFormat("Drawing '{0}' at [{1},{2}] clipped by {3}", text.Substring(offset, count), drawPos.x, drawPos.y, clipRect);
            }
            float      textZ    = drawPos.z - (int)SubLevel.Normal * GuiZSubLevelStep;
            float      shadowZ  = drawPos.z - (int)SubLevel.Shadow * GuiZSubLevelStep;
            float      bgZ      = drawPos.z - (int)SubLevel.Background * GuiZSubLevelStep;
            SimpleFont textFont = this.Font;
            float      x        = textFont.GetTextExtent(text, offset, count);
            float      y        = textFont.LineSpacing;

            if (clipRect != null)
            {
                if (drawPos.x > clipRect.Right || drawPos.x + x < clipRect.Left ||
                    drawPos.y > clipRect.Bottom || drawPos.y + y < clipRect.Top)
                {
                    // this line is entirely out of bounds - technically, the drop shadow
                    // could extend outside this area (as could the font), but I think
                    // that if we wouldn't have drawn the background for the text, we don't
                    // need to draw the text.
                    log.DebugFormat("text with dimensions ({4},{5}) fully clipped by rect [{0},{1} - {2},{3}]", clipRect.Left, clipRect.Top, clipRect.Right, clipRect.Bottom, x, y);
                    return;
                }
            }
            // Draw these on integer boundaries
            drawPos.x = (int)drawPos.x;
            drawPos.y = (int)drawPos.y;
            drawPos.z = textZ;
            ColorRect colorRect;

            if (style.bgEnabled)
            {
                colorRect = new ColorRect(style.bgColor);
                colorRect.SetAlpha(this.EffectiveAlpha);
                Rect bgRect = new Rect(drawPos.x, drawPos.x + x,
                                       drawPos.y, drawPos.y + y);
                bgImage.Draw(bgRect, bgZ, clipRect, colorRect);
            }
            colorRect = new ColorRect(style.textColor);
            colorRect.SetAlpha(this.EffectiveAlpha);
            textFont.DrawTextLine(text, offset, count, drawPos, clipRect, colorRect);
            if (style.shadowEnabled)
            {
                drawPos.x += shadowOffset.X;
                drawPos.y += shadowOffset.Y;
                drawPos.z  = shadowZ;
                colorRect  = new ColorRect(style.shadowColor);
                colorRect.SetAlpha(this.EffectiveAlpha);
                textFont.DrawTextLine(text, offset, count, drawPos, clipRect, colorRect);
            }
        }
 public static List<TextRange> GetWrappedLines(SimpleFont font, List<TextRange> hardWrappedLines, string text, float width, bool nonSpaceWrap)
 {
     List<TextRange> lines = new List<TextRange>();
     foreach (TextRange range in hardWrappedLines) {
         // Does the whole line fit?
         if (font.GetTextExtent(text, range.start, range.Length) < width) {
             lines.Add(range);
         } else {
             int start = range.start;
             // loop for adding multiple lines for the one hard-break line
             while (start < range.end) {
                 float curWidth = 0;
                 TextRange curLine = new TextRange();
                 curLine.start = start;
                 // loop for adding words to the line
                 while (start < range.end) {
                     int wordEndIndex = 0;
                     int chunkEndIndex = 0;
                     // this method should get words.
                     // bounds.start will be the start of the word
                     // bounds.end will be after the end of the word with whitespace trimmed.
                     // chunkEndIndex is after the end of the word including trailing whitespace.
                     TextUtil.GetWordBounds(text, start, range.end, out wordEndIndex, out chunkEndIndex);
                     float wordWidth = font.GetTextExtent(text, start, wordEndIndex - start);
                     if (curWidth + wordWidth < width) {
                         // include the word
                         curLine.end = wordEndIndex;
                         curWidth += wordWidth;
                         // include the trailing space
                         curWidth += font.GetTextExtent(text, wordEndIndex, chunkEndIndex - wordEndIndex);
                         start = chunkEndIndex;
                     } else if (nonSpaceWrap && start == curLine.start) {
                         // FIXME - the one word didn't fit on this line -- deal with non-space wrap
                         Debug.Assert(false, "Still need to handle lines that don't break");
                     } else if (start == curLine.start) {
                         // We don't do non-space wrap, but the first word doesn't fit..
                         // We will have to pretend it does, so that we can make progress.
                         // include the word
                         curLine.end = wordEndIndex;
                         curWidth += wordWidth;
                         // include the trailing space
                         curWidth += font.GetTextExtent(text, wordEndIndex, chunkEndIndex - wordEndIndex);
                         start = chunkEndIndex;
                     } else {
                         // the word doesn't fit on this line, so I'm through with this line
                         curLine.end = start;
                         break;
                     }
                 }
                 lines.Add(curLine);
             }
         }
     }
     //Trace.TraceInformation("GetWrappedLines():");
     //foreach (TextRange range in lines)
     //    Trace.TraceInformation("  '{0}'", text.Substring(range.start, range.end - range.start));
     return lines;
 }
示例#3
0
        public static List <TextRange> GetWrappedLines(SimpleFont font, List <TextRange> hardWrappedLines, string text, float width, bool nonSpaceWrap)
        {
            List <TextRange> lines = new List <TextRange>();

            foreach (TextRange range in hardWrappedLines)
            {
                // Does the whole line fit?
                if (font.GetTextExtent(text, range.start, range.Length) < width)
                {
                    lines.Add(range);
                }
                else
                {
                    int start = range.start;
                    // loop for adding multiple lines for the one hard-break line
                    while (start < range.end)
                    {
                        float     curWidth = 0;
                        TextRange curLine  = new TextRange();
                        curLine.start = start;
                        // loop for adding words to the line
                        while (start < range.end)
                        {
                            int wordEndIndex  = 0;
                            int chunkEndIndex = 0;
                            // this method should get words.
                            // bounds.start will be the start of the word
                            // bounds.end will be after the end of the word with whitespace trimmed.
                            // chunkEndIndex is after the end of the word including trailing whitespace.
                            TextUtil.GetWordBounds(text, start, range.end, out wordEndIndex, out chunkEndIndex);
                            float wordWidth = font.GetTextExtent(text, start, wordEndIndex - start);
                            if (curWidth + wordWidth < width)
                            {
                                // include the word
                                curLine.end = wordEndIndex;
                                curWidth   += wordWidth;
                                // include the trailing space
                                curWidth += font.GetTextExtent(text, wordEndIndex, chunkEndIndex - wordEndIndex);
                                start     = chunkEndIndex;
                            }
                            else if (nonSpaceWrap && start == curLine.start)
                            {
                                // FIXME - the one word didn't fit on this line -- deal with non-space wrap
                                Debug.Assert(false, "Still need to handle lines that don't break");
                            }
                            else if (start == curLine.start)
                            {
                                // We don't do non-space wrap, but the first word doesn't fit..
                                // We will have to pretend it does, so that we can make progress.
                                // include the word
                                curLine.end = wordEndIndex;
                                curWidth   += wordWidth;
                                // include the trailing space
                                curWidth += font.GetTextExtent(text, wordEndIndex, chunkEndIndex - wordEndIndex);
                                start     = chunkEndIndex;
                            }
                            else
                            {
                                // the word doesn't fit on this line, so I'm through with this line
                                curLine.end = start;
                                break;
                            }
                        }
                        lines.Add(curLine);
                    }
                }
            }
            //Trace.TraceInformation("GetWrappedLines():");
            //foreach (TextRange range in lines)
            //    Trace.TraceInformation("  '{0}'", text.Substring(range.start, range.end - range.start));
            return(lines);
        }
示例#4
0
 public override int GetStringWidth()
 {
     return((int)Math.Ceiling(font.GetTextExtent(GetAllText())));
 }