public int Add(TextRun textRun) { if (textRun == null) { throw new ArgumentNullException("textRun"); } int result = _textRuns.Add(textRun); _textFlow.InvalidateMeasure(); return result; }
public int Add(TextRun textRun) { if (textRun == null) { throw new ArgumentNullException("textRun"); } int result = _textRuns.Add(textRun); _textFlow.InvalidateMeasure(); return(result); }
public MFTestResults TextFlow_TextRunTest1() { if (CleaningWindow() != MFTestResults.Pass) { return MFTestResults.Fail; } MFTestResults testResult = MFTestResults.Pass; Log.Comment("Adds a TextRun into a TextFlow, and Verifies the TextRun exists"); _txtRun = true; tr1 = new TextRun("TextRun", _font, _color); UpdateWindow(); if (!trc.Contains(tr1)) { Log.Comment("TextFlow.TextRuns doesn't contain tr1"); testResult = MFTestResults.Fail; } _txtRun = false; return testResult; }
public void Remove(TextRun run) { _textRuns.Remove(run); _textFlow.InvalidateMeasure(); }
public void Insert(int index, TextRun run) { _textRuns.Insert(index, run); _textFlow.InvalidateMeasure(); }
public int IndexOf(TextRun run) { return _textRuns.IndexOf(run); }
public bool Contains(TextRun run) { return _textRuns.Contains(run); }
internal bool Break(int availableWidth, out TextRun run1, out TextRun run2, bool emergencyBreak) { Debug.Assert(availableWidth > 0); Debug.Assert(availableWidth < _width); Debug.Assert(Text.Length > 1); int leftBreak = -1; int rightBreak = -1; int w, h; // Try to find a candidate position for breaking // bool foundBreak = false; while (!foundBreak) { // Try adding a word // int indexOfNextSpace = Text.IndexOf(' ', leftBreak + 1); foundBreak = (indexOfNextSpace == -1); if (!foundBreak) { Font.ComputeExtent(Text.Substring(0, indexOfNextSpace), out w, out h); foundBreak = (w >= availableWidth); if (w == availableWidth) { leftBreak = indexOfNextSpace; } } if (foundBreak) { if (leftBreak >= 0) { rightBreak = leftBreak + 1; } else if (emergencyBreak) { leftBreak = EmergencyBreak(availableWidth); rightBreak = leftBreak; } else { run1 = run2 = null; return(false); } } else { leftBreak = indexOfNextSpace; } } string first = Text.Substring(0, leftBreak).TrimEnd(' '); // Split the text run // run1 = null; if (first.Length > 0) { run1 = new TextRun(first, this.Font, this.ForeColor); } run2 = null; if (rightBreak < Text.Length) { String run2String = Text.Substring(rightBreak).TrimStart(' '); // if run2 is all spaces (length == 0 after trim), we'll leave run2 as null if (run2String.Length > 0) { run2 = new TextRun(run2String, this.Font, this.ForeColor); } } return(true); }
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++; } }
// 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); }
internal bool Break(int availableWidth, out TextRun run1, out TextRun run2, bool emergencyBreak) { Debug.Assert(availableWidth > 0); Debug.Assert(availableWidth < _width); Debug.Assert(Text.Length > 1); int leftBreak = -1; int rightBreak = -1; int w, h; // Try to find a candidate position for breaking // bool foundBreak = false; while (!foundBreak) { // Try adding a word // int indexOfNextSpace = Text.IndexOf(' ', leftBreak + 1); foundBreak = (indexOfNextSpace == -1); if (!foundBreak) { Font.ComputeExtent(Text.Substring(0, indexOfNextSpace), out w, out h); foundBreak = (w >= availableWidth); if (w == availableWidth) { leftBreak = indexOfNextSpace; } } if (foundBreak) { if (leftBreak >= 0) { rightBreak = leftBreak + 1; } else if (emergencyBreak) { leftBreak = EmergencyBreak(availableWidth); rightBreak = leftBreak; } else { run1 = run2 = null; return false; } } else { leftBreak = indexOfNextSpace; } } string first = Text.Substring(0, leftBreak).TrimEnd(' '); // Split the text run // run1 = null; if (first.Length > 0) { run1 = new TextRun(first, this.Font, this.ForeColor); } run2 = null; if (rightBreak < Text.Length) { String run2String = Text.Substring(rightBreak).TrimStart(' '); // if run2 is all spaces (length == 0 after trim), we'll leave run2 as null if (run2String.Length > 0) { run2 = new TextRun(run2String, this.Font, this.ForeColor); } } return true; }
public void Remove(TextRun run) { _textRuns.Remove(run); _textFlow.InvalidateMeasure(); }
public void Insert(int index, TextRun run) { _textRuns.Insert(index, run); _textFlow.InvalidateMeasure(); }
public int IndexOf(TextRun run) { return(_textRuns.IndexOf(run)); }
public bool Contains(TextRun run) { return(_textRuns.Contains(run)); }