Esempio n. 1
0
        public override void OnRender(Media.DrawingContext dc)
        {
            if (_lineCache == null || _lineCache.Count == 0)
            {
                return;
            }

            int nLines = _lineCache.Count;
            int top    = 0;

            int width, height;

            GetRenderSize(out width, out height);

            // Draw each line of Text
            //
            int lineNumber = _currentLine;

            while (lineNumber < nLines)
            {
                TextLine line = (TextLine)_lineCache[lineNumber];
                if (top + line.Height > height)
                {
                    break;
                }

                TextRun[] runs = line.Runs;

                int x;
                switch (_alignment)
                {
                case TextAlignment.Left:
                    x = 0;
                    break;

                case TextAlignment.Center:
                    x = (width - line.Width) >> 1;     // >> 1 is the same as div by 2
                    break;

                case TextAlignment.Right:
                    x = width - line.Width;
                    break;

                default:
                    throw new NotSupportedException();
                }

                for (int i = 0; i < runs.Length; i++)
                {
                    TextRun run = runs[i];
                    int     w, h;
                    run.GetSize(out w, out h);
                    int y = top + line.Baseline - run.Font.Ascent;
                    dc.DrawText(run.Text, run.Font, run.ForeColor, x, y);
                    x += w;
                }

                top += line.Height;
                lineNumber++;
            }
        }
Esempio n. 2
0
        // Given an available width, takes the TextRuns and arranges them into
        // separate lines, breaking where possible at whitespace.
        //
        internal ArrayList SplitLines(int availableWidth)
        {
            Debug.Assert(availableWidth > 0);

            int lineWidth = 0;

            ArrayList remainingRuns = new ArrayList();

            for (int i = 0; i < TextRuns.Count; i++)
            {
                remainingRuns.Add(TextRuns[i]);
            }

            ArrayList lineCache         = new ArrayList();
            ArrayList runsOnCurrentLine = new ArrayList();

            while (remainingRuns.Count > 0)
            {
                bool newLine = false;

                TextRun run = (TextRun)remainingRuns[0];
                remainingRuns.RemoveAt(0);

                if (run.IsEndOfLine)
                {
                    newLine = true;
                }
                else
                {
                    // Add run to end of current line
                    //
                    int runWidth, runHeight;
                    run.GetSize(out runWidth, out runHeight);
                    lineWidth += runWidth;
                    runsOnCurrentLine.Add(run);

                    // If the line length now extends beyond the available width, attempt to break the line
                    //
                    if (lineWidth > availableWidth)
                    {
                        bool onlyRunOnCurrentLine = (runsOnCurrentLine.Count == 1);

                        if (run.Text.Length > 1)
                        {
                            runsOnCurrentLine.Remove(run);

                            TextRun run1, run2;
                            if (run.Break(runWidth - (lineWidth - availableWidth), out run1, out run2, onlyRunOnCurrentLine))
                            {
                                // Break and put overflow on next line
                                //
                                if (run1 != null)
                                {
                                    runsOnCurrentLine.Add(run1);
                                }

                                if (run2 != null)
                                {
                                    remainingRuns.Insert(0, run2);
                                }
                            }
                            else if (!onlyRunOnCurrentLine)
                            {
                                // No break found - put it on its own line
                                //
                                remainingRuns.Insert(0, run);
                            }
                        }
                        else // run.Text.Length == 1
                        {
                            if (!onlyRunOnCurrentLine)
                            {
                                runsOnCurrentLine.Remove(run);
                                remainingRuns.Insert(0, run);
                            }
                        }

                        newLine = true;
                    }

                    if (lineWidth >= availableWidth || remainingRuns.Count == 0)
                    {
                        newLine = true;
                    }
                }

                // If we're done with this line, add it to the list
                //
                if (newLine)
                {
                    int lineHeight = 0;
                    int baseLine   = 0;
                    int nRuns      = runsOnCurrentLine.Count;
                    if (nRuns > 0)
                    {
                        // Compute line height & baseline
                        for (int i = 0; i < nRuns; i++)
                        {
                            Font font = ((TextRun)runsOnCurrentLine[i]).Font;
                            int  h    = font.Height + font.ExternalLeading;
                            if (h > lineHeight)
                            {
                                lineHeight = h;
                                baseLine   = font.Ascent;
                            }
                        }

                        // Add line to cache
                        lineCache.Add(new TextLine(runsOnCurrentLine, lineHeight, baseLine));
                    }
                    else
                    {
                        // Empty line. Just borrow the height from the previous line, if any
                        lineHeight = (lineCache.Count) > 0 ?
                                     ((TextLine)lineCache[lineCache.Count - 1]).Height :
                                     TextLine.DefaultLineHeight;
                        lineCache.Add(new TextLine(lineHeight));
                    }

                    // Move onto next line
                    //
                    runsOnCurrentLine.Clear();
                    lineWidth = 0;
                }
            }

            return(lineCache);
        }