public MRATextItemView() { this.textfmtr = TextFormatter.Create(TextFormattingMode.Ideal); this.textbuilder = new StringBuilder(); this.fontindics = new List <ITextFontIndex>(); this.structbarmouseover = false; this.rawtextmouseover = false; this.dvNumberBar = new MRANumberBarDrawingVisual(this); this.dvStructBar = new MRAStructBarDrawingVisual(this); this.dvRawText = new MRARawTextDrawingVisual(this); this.dvSelection = new MRASelectionDrawingVisual(this); this.dvs = new List <MRATextItemDrawingVisual>() { dvNumberBar, dvStructBar, dvSelection, dvRawText }; AddLogicalChild(dvNumberBar); AddLogicalChild(dvStructBar); AddLogicalChild(dvSelection); AddLogicalChild(dvRawText); AddVisualChild(dvNumberBar); AddVisualChild(dvStructBar); AddVisualChild(dvSelection); AddVisualChild(dvRawText); HorizontalAlignment = HorizontalAlignment.Left; VerticalAlignment = VerticalAlignment.Top; }
private void UpdateFormattedText02() { // Create a DrawingGroup object for storing formatted text. myTextDisplay = new DrawingGroup(); DrawingContext drawingContext = myTextDisplay.Open(); // Update the text store. customTextSource.Text = "The quick red fox jumped over the lazy brown dog."; // Create a TextFormatter object. TextFormatter formatter = TextFormatter.Create(); // Create common paragraph property settings. CustomTextParagraphProperties customTextParagraphProperties = new CustomTextParagraphProperties(); // <SnippetTextFormattingSnippet2> // Create a textline from the text store using the TextFormatter object. TextLine myTextLine = formatter.FormatLine( customTextSource, 0, 400, customTextParagraphProperties, null); // Draw the formatted text into the drawing context. myTextLine.Draw(drawingContext, new Point(0, 0), InvertAxes.None); // </SnippetTextFormattingSnippet2> // Persist the drawn text content. drawingContext.Close(); // Display the formatted text in the DrawingGroup object. myDrawingBrush.Drawing = myTextDisplay; }
/// <summary> /// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object. /// </summary> public static TextFormatter Create(DependencyObject owner) { if (owner == null) { throw new ArgumentNullException("owner"); } #if DOTNET4 return(TextFormatter.Create(TextOptions.GetTextFormattingMode(owner))); #else if (TextFormattingModeProperty != null) { object formattingMode = owner.GetValue(TextFormattingModeProperty); return((TextFormatter)typeof(TextFormatter).InvokeMember( "Create", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { formattingMode }, CultureInfo.InvariantCulture)); } else { return(TextFormatter.Create()); } #endif }
static VisualTextLine() { formatter = TextFormatter.Create(); paragraphProperties = new SimpleParagraphProperties { defaultTextRunProperties = TextConfiguration.GetGlobalTextRunProperties() }; }
public void IndentTest() { using (var formatter = TextFormatter.Create()) { List <string> log = new List <string> (); var textSource = new LoggingTextSource(log); textSource.AddContents("test test2"); textSource.AddContents(new TextEndOfLine(1)); var textParagraphProperties = new LoggingTextParagraphProperties(log); textParagraphProperties.alwaysCollapsible = true; textParagraphProperties.indent = 20; log.Clear(); var line = formatter.FormatLine(textSource, 0, 530.0, textParagraphProperties, null); Assert.AreEqual(147.18 + 0.02 / 3.0, line.Height, "line.Height"); Assert.AreEqual(5, line.Length, "line.Length"); Assert.AreEqual(0, line.NewlineLength, "line.NewlineLength"); Assert.AreEqual(117.97, line.Baseline, 0.000000001, "line.Baseline"); Assert.AreEqual(67894 / 300.0, line.Width, 0.000000001, "line.Width"); Assert.IsFalse(line.HasOverflowed, "line.HasOverflowed"); Assert.AreEqual(78563 / 300.0, line.WidthIncludingTrailingWhitespace, 0.000000001, "line.WidthIncludingTrailingWhitespace"); AssertTextRunSpans( new int[] { 5 }, new TextRun[] { textSource.contents[0] }, line); Assert.IsNull(line.GetTextLineBreak(), "GetTextLineBreak"); Assert.AreEqual(0, line.Start, "line.Start"); AssertCharacterHit(line, 25.0, 0, 0); } }
public ChatFormatter(Typeface typeface, double fontSize, Brush foreground, IDictionary <string, Brush> palette) { _runProperties = new CustomTextRunProperties(typeface, fontSize, foreground, Brushes.Transparent, false); _paraProperties = new CustomParagraphProperties(_runProperties); _formatter = TextFormatter.Create(TextFormattingMode.Display); _palette = palette; }
internal static double GetFontHeight(this FrameworkElement source) { if (source == null) { throw new ArgumentNullException("source"); } var textProperties = new TextProperties( TextOptions.GetTextFormattingMode(source), TextElement.GetFontFamily(source), TextElement.GetFontStyle(source), TextElement.GetFontWeight(source), TextElement.GetFontStretch(source), TextElement.GetFontSize(source)); lock ((( ICollection )s_fontHeight).SyncRoot) { double fontHeight; if (!s_fontHeight.TryGetValue(textProperties, out fontHeight)) { var formatter = TextFormatter.Create(textProperties.FormattingMode); var typeface = new Typeface(textProperties.FontFamily, textProperties.FontStyle, textProperties.FontWeight, textProperties.FontStretch); var textSource = new EmptyTextSource(); var textRunProperties = new EmptyTextRunProperties(typeface, textProperties.FontSize); var textParagraphProperties = new EmptyTextParagraphProperties(textRunProperties); var textLine = formatter.FormatLine(textSource, 0, 0d, textParagraphProperties, null); fontHeight = textLine.Height; s_fontHeight.Add(textProperties, fontHeight); } return(fontHeight); } }
void CreateLine() { TextFormatter formatter = TextFormatter.Create(); if (textSource.Text.Length == 0) { TextLine myTextLine = formatter.FormatLine( textSource, 0, this._width, new GenericTextParagraphProperties(fontRender, this.FlowDirection, this.TextWarpping), null); lines.Add(myTextLine); } else { int textStorePosition = 0; this.lines.Clear(); while (textStorePosition < textSource.Text.Length) { TextLine myTextLine = formatter.FormatLine( textSource, textStorePosition, this._width, new GenericTextParagraphProperties(fontRender, this.FlowDirection, this.TextWarpping), null); lines.Add(myTextLine); textStorePosition += myTextLine.Length; } } }
protected override void OnRender(DrawingContext drawingContext) { #if DEBUG var watch = System.Diagnostics.Stopwatch.StartNew(); #endif base.OnRender(drawingContext); if (!string.IsNullOrEmpty(Text)) { int textStorePosition = 0; var position = new Point(0, 0); var length = _TextSource.Length; var maxWidth = this.ActualWidth; var maxHeight = this.ActualHeight; _TextSource.RenderWidth = maxWidth; using (var formatter = TextFormatter.Create()) { while (textStorePosition < length && position.Y < maxHeight) { using (var line = formatter.FormatLine(_TextSource, textStorePosition, maxWidth, _TextParagraph, null)) { line.Draw(drawingContext, position, InvertAxes.None); position.Y += line.Height; textStorePosition += line.Length; } } } } #if DEBUG watch.Stop(); System.Diagnostics.Debug.WriteLine($"OnRender ElapsedTicks:{watch.ElapsedTicks},ElapsedMilliseconds:{watch.ElapsedMilliseconds}"); #endif }
public void OverflowTest() { using (var formatter = TextFormatter.Create()) { List <string> log = new List <string> (); var textSource = new LoggingTextSource(log); textSource.AddContents("test test"); textSource.AddContents(new TextEndOfLine(1)); var textParagraphProperties = new LoggingTextParagraphProperties(log); textParagraphProperties.alwaysCollapsible = true; textParagraphProperties.textWrapping = TextWrapping.NoWrap; log.Clear(); var line = formatter.FormatLine(textSource, 0, 256.0, textParagraphProperties, null); Assert.AreEqual(147.18 + 0.02 / 3.0, line.Height, "line.Height"); Assert.AreEqual(10, line.Length, "line.Length"); Assert.AreEqual(1, line.NewlineLength, "line.NewlineLength"); Assert.AreEqual(117.97, line.Baseline, 0.000000001, "line.Baseline"); Assert.AreEqual(448.19, line.Width, 0.000000001, "line.Width"); Assert.IsTrue(line.HasOverflowed, "line.HasOverflowed"); Assert.AreEqual(448.19, line.WidthIncludingTrailingWhitespace, 0.000000001, "line.WidthIncludingTrailingWhitespace"); AssertTextRunSpans( new int[] { 9, 1 }, new TextRun[] { textSource.contents[0], new TextEndOfLine(1) }, line); Assert.IsNull(line.GetTextLineBreak(), "GetTextLineBreak"); Assert.AreEqual(0, line.Start, "line.Start"); } }
/// <summary> /// Creates a <see cref="TextFormatter"/> using the formatting mode used by the specified owner object. /// </summary> public static TextFormatter Create(DependencyObject owner) { if (owner == null) { throw new ArgumentNullException("owner"); } return(TextFormatter.Create(TextOptions.GetTextFormattingMode(owner))); }
public CustomStringRenderer(IOffsetBasedItem parent, TextBlock block) { _parent = parent; _block = block; _restrictedWidth = 100; CachedLines = new List <TextLineContainer>(); _textSource = new BlockBasedTextSource(_block); _textFormatter = TextFormatter.Create(TextFormattingMode.Display); }
/// <summary> /// Method for starting the text formatting process. Each event handler /// will call this after the current fontrendering is updated. /// </summary> private void UpdateFormattedText(double pixelsPerDip) { // Make sure all UI is loaded if (!_UILoaded) { return; } // Initialize the text store. _textStore = new CustomTextSource(_pixelsPerDip); int textStorePosition = 0; //Index into the text of the textsource System.Windows.Point linePosition = new System.Windows.Point(0, 0); //current line // Create a DrawingGroup object for storing formatted text. textDest = new DrawingGroup(); DrawingContext dc = textDest.Open(); // Update the text store. _textStore.Text = textToFormat.Text; _textStore.FontRendering = _currentRendering; // Create a TextFormatter object. TextFormatter formatter = TextFormatter.Create(); // Format each line of text from the text store and draw it. while (textStorePosition < _textStore.Text.Length) { // Create a textline from the text store using the TextFormatter object. using (TextLine myTextLine = formatter.FormatLine( _textStore, textStorePosition, 96 * 6, new GenericTextParagraphProperties(_currentRendering, _pixelsPerDip), null)) { // Draw the formatted text into the drawing context. myTextLine.Draw(dc, linePosition, InvertAxes.None); // Update the index position in the text store. textStorePosition += myTextLine.Length; // Update the line position coordinate for the displayed line. linePosition.Y += myTextLine.Height; } } // Persist the drawn text content. dc.Close(); // Display the formatted text in the DrawingGroup object. myDrawingBrush.Drawing = textDest; }
protected TextLine GetLine(TextRunProperties runProperties, TextSource textSource, int column = 0) { return(TextFormatter.Create().FormatLine( textSource, column, 96 * 6, new SimpleParagraphProperties { defaultTextRunProperties = runProperties }, null)); }
private void UpdateFormattedText() { // Index into the text of the TextSource object. int textStorePosition = 0; // The position of the current line. Point linePosition = new Point(0, 0); // Create a DrawingGroup object for storing formatted text. myTextDisplay = new DrawingGroup(); DrawingContext drawingContext = myTextDisplay.Open(); // Update the text store. customTextSource.Text = textToFormat.Text; // <SnippetTextFormattingSnippet1> // Create a TextFormatter object. TextFormatter formatter = TextFormatter.Create(); // Create common paragraph property settings. CustomTextParagraphProperties customTextParagraphProperties = new CustomTextParagraphProperties(); // Format each line of text from the text store and draw it. while (textStorePosition < customTextSource.Text.Length) { // Create a textline from the text store using the TextFormatter object. using (TextLine myTextLine = formatter.FormatLine( customTextSource, textStorePosition, 96 * 6, customTextParagraphProperties, null)) { // Draw the formatted text into the drawing context. myTextLine.Draw(drawingContext, linePosition, InvertAxes.None); // Update the index position in the text store. textStorePosition += myTextLine.Length; // Update the line position coordinate for the displayed line. linePosition.Y += myTextLine.Height; } } // </SnippetTextFormattingSnippet1> // Persist the drawn text content. drawingContext.Close(); // Display the formatted text in the DrawingGroup object. myDrawingBrush.Drawing = myTextDisplay; }
public TextView() { InitializeComponent(); Formatter = TextFormatter.Create(TextFormattingMode.Display); this.WhenActivated(d => { this.OneWayBind(ViewModel, vm => vm.Caret.Height, v => v.caret.Height).DisposeWith(d); this.OneWayBind(ViewModel, vm => vm.Caret.Width, v => v.caret.Width).DisposeWith(d); this.WhenAnyValue(v => v.FontFamily, v => v.FontSize, (family, size) => { var typeface = new Typeface(family, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal); return(new Source(typeface, size, Brushes.Black)); }) .ToPropertyEx(ViewModel, v => v.Source) .DisposeWith(d); this.Events().SizeChanged .Throttle(TimeSpan.FromMilliseconds(40), RxApp.MainThreadScheduler) .Where(args => args.HeightChanged) .Do(args => Console.WriteLine($@"size-changed: <{args.PreviousSize}->{args.NewSize}>")) .Subscribe(args => { //-- Render(); InvalidateVisual(); }) .DisposeWith(d); ViewModel.Document.Lines.Connect().Subscribe(changeSet => { //-- Console.WriteLine($@"changeSet: <{changeSet}>"); foreach (var change in changeSet) { Console.WriteLine($@"change: <{change}>"); } Render(); InvalidateVisual(); // ¯\_(ツ)_/¯ }).DisposeWith(d); }); }
public void SingleWordTahomaTest() { using (var formatter = TextFormatter.Create()) { List <string> log = new List <string> (); var textProps = new LoggingTextRunProperties(log, "Tahoma"); textProps.typeface = new Typeface("Tahoma"); var textSource = new LoggingTextSource(log); textSource.AddContents("test", textProps); textSource.AddContents(new TextEndOfLine(1)); var textParagraphProperties = new LoggingTextParagraphProperties(log); log.Clear(); var line = formatter.FormatLine(textSource, 0, 256.0, textParagraphProperties, null); Assert.AreEqual(154.5, line.Height, "line.Height"); Assert.AreEqual(5, line.Length, "line.Length"); Assert.AreEqual(1, line.NewlineLength, "line.NewlineLength"); Assert.AreEqual(128.06 + 0.01 / 3.0, line.Baseline, "line.Baseline"); Assert.AreEqual(210.12 + 0.02 / 3.0, line.Width, 0.000000001, "line.Width"); Assert.IsFalse(line.HasOverflowed, "line.HasOverflowed"); Assert.AreEqual(210.12 + 0.02 / 3.0, line.WidthIncludingTrailingWhitespace, 0.000000001, "line.WidthIncludingTrailingWhitespace"); AssertTextRunSpans( new int[] { 4, 1 }, new TextRun[] { textSource.contents[0], new TextEndOfLine(1) }, line); Assert.AreEqual(new string[] { "DefaultTextRunProperties", "DefaultTextRunProperties.Typeface", "DefaultTextRunProperties.FontRenderingEmSize", "Indent", "LineHeight", "FlowDirection", "AlwaysCollapsible", "TextAlignment", "FirstLineInParagraph", "TextMarkerProperties", "TextWrapping", "GetTextRun(0)", "Tahoma.FontRenderingEmSize", "Tahoma.CultureInfo", "Tahoma.Typeface", "Tahoma.TextEffects", "Tahoma.TextDecorations", "GetTextRun(4)", }, log); Assert.IsNull(line.GetTextLineBreak(), "GetTextLineBreak"); Assert.AreEqual(0, line.Start, "line.Start"); } }
public WpfTextFormatter(object mode) { if (TextFormatterFactory.TextFormattingModeProperty != null) { return((TextFormatter)typeof(TextFormatter).InvokeMember( "Create", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { mode }, CultureInfo.InvariantCulture)); } else { return(TextFormatter.Create()); } }
private void UpdateFormat() { var lineNumberType = _classificationTypeRegistry.GetClassificationType("line number"); _formatting = _classificationFormatMap.GetTextProperties(lineNumberType); Background = _formatting.BackgroundBrush; _textFormatter = _formattedLineSource.UseDisplayMode ? TextFormatter.Create(TextFormattingMode.Display) : TextFormatter.Create(TextFormattingMode.Ideal); NumberWidth = Enumerable.Range(0, 10).Max(x => MakeTextLine(x).Width); _formatChanged = true; }
public IList <TextLineVisual> CreateLineVisual(Int32 lineNumber, IList <SpaceNegotiation> spaceNegotiations) { IList <ClassificationSpan> classificationSpans; Int32 startOfLineFromLineNumber = _textView.TextBuffer.GetStartOfLineFromLineNumber(lineNumber); Int32 startOfNextLineFromPosition = _textView.TextBuffer.GetStartOfNextLineFromPosition(startOfLineFromLineNumber); Int32 lengthOfLineFromLineNumber = _textView.TextBuffer.GetLengthOfLineFromLineNumber(lineNumber); Int32 length = 0; if (startOfNextLineFromPosition == startOfLineFromLineNumber) { length = _textView.TextBuffer.GetEndOfLineFromPosition(startOfLineFromLineNumber) - startOfLineFromLineNumber; } else { length = startOfNextLineFromPosition - startOfLineFromLineNumber; } if (length > 0) { classificationSpans = _classifier.GetClassificationSpans(new VersionedTextSpan(_textView.TextBuffer, startOfLineFromLineNumber, length)); } else { classificationSpans = new List <ClassificationSpan>(0); } TextFormattingSource textSource = new TextFormattingSource(_textView.TextBuffer, _classificationFormatMap, startOfLineFromLineNumber, length, classificationSpans, spaceNegotiations); if (_textFormatter == null) { _textFormatter = TextFormatter.Create(); } List <TextLine> textLines = new List <TextLine>(); Int32 firstCharIndex = 0; TextLineBreak previousLineBreak = null; while (firstCharIndex <= length) { TextLine item = _textFormatter.FormatLine(textSource, firstCharIndex, this.MaxLineWidth, _paragraphProperties, previousLineBreak); firstCharIndex += item.Length; previousLineBreak = item.GetTextLineBreak(); textLines.Add(item); } List <TextLineVisual> list3 = new List <TextLineVisual>(); list3.Add(new TextLineVisual(_textView, textLines, new TextSpan(_textView.TextBuffer, startOfLineFromLineNumber, length), length - lengthOfLineFromLineNumber, textSource.VirtualCharacterPositions)); return(list3); }
/// <summary> /// </summary> public CaretElement(EditorView editorView) { SizeChangedEventHandler handler = null; _editorView = editorView; _caretPlacement = CaretPlacement.LeftOfCharacter; _insertionPoint = new TextPoint(editorView.TextBuffer, 0); _preferredVerticalPosition = _preferredHorizontalPosition = 0; _textViewHelper = new TextViewHelper(_editorView); this.ConstructCaretGeometry(); TextLine line = TextFormatter.Create().FormatLine(new DefaultLineGutterTextSource("W", TextFormattingRunProperties.DefaultProperties), 0, 10, new TextFormattingParagraphProperties(), null); _defaultOverwriteCaretWidth = line.Width; _overwriteCaretBrush = new SolidColorBrush(Colors.Gray); _overwriteCaretBrush.Opacity = 0.5; DoubleAnimationUsingKeyFrames frames = new DoubleAnimationUsingKeyFrames(); frames.BeginTime = new TimeSpan((long)0); frames.RepeatBehavior = RepeatBehavior.Forever; frames.KeyFrames.Add(new DiscreteDoubleKeyFrame(1, KeyTime.FromPercent(0))); Int32 caretBlinkTime = User32.GetCaretBlinkTime(); if (caretBlinkTime > 0) { frames.KeyFrames.Add(new DiscreteDoubleKeyFrame(0, KeyTime.FromPercent(0.5))); } else { caretBlinkTime = 500; } frames.Duration = new Duration(new TimeSpan(0, 0, 0, 0, caretBlinkTime * 2)); _blinkAnimationClock = frames.CreateClock(); base.ApplyAnimationClock(UIElement.OpacityProperty, _blinkAnimationClock); this.PositionChanged = (EventHandler <CaretPositionChangedEventArgs>)Delegate.Combine(this.PositionChanged, new EventHandler <CaretPositionChangedEventArgs>(this.CaretElement_PositionChanged)); if (handler == null) { handler = delegate { this.ConstructCaretGeometry(); }; } base.SizeChanged += handler; base.IsVisibleChanged += new DependencyPropertyChangedEventHandler(this.UIElement_VisibleChanged); this.OverwriteMode = false; }
protected override Size MeasureOverride(Size availableSize) { #if DEBUG var watch = System.Diagnostics.Stopwatch.StartNew(); #endif var size = base.MeasureOverride(availableSize); var length = _TextSource.Length; using (var formatter = TextFormatter.Create()) { if (length > 0) { int textStorePosition = 0; var maxWidth = double.IsInfinity(availableSize.Width) ? 10000 : availableSize.Width; var position = new Point(0, 0); _TextSource.RenderWidth = maxWidth; while (textStorePosition < length && size.Height < availableSize.Height) { using (var line = formatter.FormatLine(_TextSource, textStorePosition, maxWidth, _TextParagraph, null)) { size.Width = Math.Max(size.Width, line.Width); size.Height += line.Height; textStorePosition += line.Length; } } } else { using (var line = formatter.FormatLine(_TextSource, 0, 0, _TextParagraph, null)) { size.Height = line.Height; } } } #if DEBUG watch.Stop(); Debug.WriteLine($"MeasureOverride ElapsedTicks:{watch.ElapsedTicks},ElapsedMilliseconds:{watch.ElapsedMilliseconds}"); #endif return(size); }
public override void Perform() { TextFormatter formatter = TextFormatter.Create(); ListBox listBox = new ListBox(); DoubleAnimation anim = new DoubleAnimation(MinScale, MaxScale, TimeSpan.FromMilliseconds(AnimTime)); anim.AutoReverse = true; ScaleTransform st = new ScaleTransform(MinScale, MinScale); for (int i = 0; i < TextToFormat.Length; i++) { TextSource.Text = TextToFormat[i]; DrawingGroup drawingGroup = new DrawingGroup(); DrawingContext dc = drawingGroup.Open(); dc.PushTransform(st); GenerateFormattedText(TextSource, dc, formatter); dc.Close(); Image image = new Image(); DrawingImage drawingImage = new DrawingImage(drawingGroup); image.Source = drawingImage; image.Stretch = Stretch.None; image.HorizontalAlignment = HorizontalAlignment.Left; image.VerticalAlignment = VerticalAlignment.Top; image.Margin = new Thickness(1, 1, 1, 10); listBox.Items.Add(image); } st.BeginAnimation(ScaleTransform.ScaleXProperty, anim); st.BeginAnimation(ScaleTransform.ScaleYProperty, anim); WindowSource.Width = Microsoft.Test.Display.Monitor.Dpi.x * 9; // 9" WindowSource.Height = Microsoft.Test.Display.Monitor.Dpi.y * 6; // 6" listBox.Height = WindowSource.Height; WindowSource.Content = listBox; DispatcherHelper.DoEvents(AnimTime * 2); }
public void Render(int startPos, int count) { DrawingContext dc = RenderOpen(); if (_textStore != null && count > 0) { _textStore.LastRenderColumn = startPos + count; // Create a TextFormatter object. TextFormatter formatter = TextFormatter.Create(); // Format each line of text from the text store and draw it. // Create a textline from the text store using the TextFormatter object. using (var myTextLine = formatter.FormatLine(_textStore, startPos, 0, _paragraphProps, null)) { // Draw the formatted text into the drawing context. myTextLine.Draw(dc, new Point(), InvertAxes.None); } } dc.Close(); }
public void EmptyLineTest() { using (var formatter = TextFormatter.Create()) { List <string> log = new List <string> (); var textSource = new LoggingTextSource(log); textSource.AddContents(new TextEndOfLine(1)); var textParagraphProperties = new LoggingTextParagraphProperties(log); Assert.AreEqual(2355 / 2048.0, textParagraphProperties.DefaultTextRunProperties.Typeface.FontFamily.LineSpacing); log.Clear(); var line = formatter.FormatLine(textSource, 0, 256.0, textParagraphProperties, null); Assert.AreEqual(147.18 + 0.02 / 3.0, line.Height, "line.Height"); Assert.AreEqual(1, line.Length, "line.Length"); Assert.AreEqual(1, line.NewlineLength, "line.NewlineLength"); Assert.AreEqual(117.97, line.Baseline, 0.000000001, "line.Baseline"); Assert.AreEqual(0, line.Width, "line.Width"); Assert.IsFalse(line.HasOverflowed, "line.HasOverflowed"); Assert.AreEqual(0, line.WidthIncludingTrailingWhitespace, "line.WidthIncludingTrailingWhitespace"); AssertTextRunSpans( new int[] { 1 }, new TextRun[] { new TextEndOfLine(1) }, line); Assert.AreEqual(new string[] { "DefaultTextRunProperties", "DefaultTextRunProperties.Typeface", "DefaultTextRunProperties.FontRenderingEmSize", "Indent", "LineHeight", "FlowDirection", "AlwaysCollapsible", "TextAlignment", "FirstLineInParagraph", "TextMarkerProperties", "TextWrapping", "GetTextRun(0)", }, log); Assert.IsNull(line.GetTextLineBreak(), "GetTextLineBreak"); Assert.AreEqual(0, line.Start, "line.Start"); } }
public void EmptyLineCollapsibleTest() { using (var formatter = TextFormatter.Create()) { List <string> log = new List <string> (); var textSource = new LoggingTextSource(log); textSource.AddContents(new TextEndOfLine(1)); var textParagraphProperties = new LoggingTextParagraphProperties(log); textParagraphProperties.alwaysCollapsible = true; Assert.AreEqual(2355 / 2048.0, textParagraphProperties.DefaultTextRunProperties.Typeface.FontFamily.LineSpacing); log.Clear(); var line = formatter.FormatLine(textSource, 0, 256.0, textParagraphProperties, null); Assert.AreEqual(147.18 + 0.02 / 3.0, line.Height, "line.Height"); Assert.AreEqual(1, line.Length, "line.Length"); Assert.AreEqual(1, line.NewlineLength, "line.NewlineLength"); Assert.AreEqual(117.97, line.Baseline, 0.000000001, "line.Baseline"); Assert.AreEqual(0, line.Width, "line.Width"); Assert.IsFalse(line.HasOverflowed, "line.HasOverflowed"); Assert.AreEqual(0, line.WidthIncludingTrailingWhitespace, "line.WidthIncludingTrailingWhitespace"); AssertTextRunSpans( new int[] { 1 }, new TextRun[] { new TextEndOfLine(1) }, line); Assert.IsNull(line.GetTextLineBreak(), "GetTextLineBreak"); Assert.AreEqual(0, line.Start, "line.Start"); var glyphRuns = new List <IndexedGlyphRun>(line.GetIndexedGlyphRuns()); Assert.AreEqual(0, glyphRuns.Count); AssertCharacterHit(line, -1.0, 0, 0); AssertCharacterHit(line, 0.0, 0, 0); AssertCharacterHit(line, 1.0, 0, 0); var tb = line.GetTextBounds(0, 1); Assert.AreEqual(1, tb.Count, "TextBounds.Count"); Assert.AreEqual(FlowDirection.LeftToRight, tb[0].FlowDirection, "TextBounds.FlowDirection"); Assert.AreEqual(new Rect(0, 0, 0, 44156 / 300.0), tb[0].Rectangle, "TextBounds.Rectangle"); Assert.IsNull(tb[0].TextRunBounds, "TextBounds.TextRunBounds"); AssertCharacterHit(0, 0, line.GetPreviousCaretCharacterHit(new CharacterHit(0, 0)), "previous 0,0"); AssertCharacterHit(0, 0, line.GetNextCaretCharacterHit(new CharacterHit(0, 0)), "next 0,0"); } }
public DrawingVisualText() { this.textFormatter = TextFormatter.Create(TextFormattingMode.Display); this.textSource = new DefaultTextSource(); this.textParagraphProperties = new DefaultTextParagraphProperties(); }
private void Render(Point location, string colour) { int textStorePosition = 0; bool flipped; Position = location; if (ParentAtom == null) { flipped = false; } else { flipped = Flipped; } var textStore = new FunctionalGroupTextSource(ParentGroup, colour, flipped); //main textformatter - this does the writing of the visual using (TextFormatter textFormatter = TextFormatter.Create()) { //set up the default paragraph properties var paraprops = new FunctionalGroupTextSource.GenericTextParagraphProperties( FlowDirection.LeftToRight, TextAlignment.Left, true, false, new LabelTextRunProperties(colour), TextWrapping.NoWrap, GlyphText.SymbolSize, 0d); var anchorRuns = textStore.Runs.Where(f => f.IsAnchor); string anchorString = string.Empty; foreach (var run in anchorRuns) { anchorString += run.Text; } using (TextLine myTextLine = textFormatter.FormatLine(textStore, textStorePosition, 999, paraprops, null)) { IList <TextBounds> textBounds; Rect firstRect = Rect.Empty; if (!Flipped) //isolate them at the beginning { textBounds = myTextLine.GetTextBounds(0, anchorString.Length); } else { //isolate them at the end var start = myTextLine.Length - 1 - anchorString.Length; textBounds = myTextLine.GetTextBounds(start, anchorString.Length); } //add all the bounds together foreach (TextBounds anchorBound in textBounds) { firstRect.Union(anchorBound.Rectangle); } //center will be position close to the origin 0,0 Point center = new Point((firstRect.Left + firstRect.Right) / 2, (firstRect.Top + firstRect.Bottom) / 2); //the displacement vector will be added to each relative coordinate for the glyph run var displacementVector = location - center; //locus is where the text line is drawn var locus = new Point(0, 0) + displacementVector; textBounds = myTextLine.GetTextBounds(0, 999); var obb = textBounds[0].Rectangle; //draw the line of text using (DrawingContext dc = RenderOpen()) { myTextLine.Draw(dc, locus, InvertAxes.None); #if DEBUG #if SHOWBOUNDS obb.Offset(new Vector(locus.X, locus.Y)); dc.DrawRectangle(null, new Pen(new SolidColorBrush(Colors.BlueViolet), 1.0), obb); #endif #endif var glyphRuns = myTextLine.GetIndexedGlyphRuns(); List <Point> outline = new List <Point>(); double advanceWidths = 0d; //build up the convex hull from each glyph //you need to add in the advance widths for each //glyph run as they are traversed, //to the outline foreach (IndexedGlyphRun igr in glyphRuns) { var originalRun = textStore.GetTextRun(igr.TextSourceCharacterIndex); var currentRun = igr.GlyphRun; //need to work out how much the current run has been offset from the baseline var runBounds = myTextLine.GetTextBounds(igr.TextSourceCharacterIndex, igr.TextSourceLength); //get the bounding rect var rect = runBounds[0].TextRunBounds[0].Rectangle; //it's relative to the baseline //adjust it rect.Offset(new Vector(locus.X, locus.Y)); var rectCopy = rect; #if DEBUG #if SHOWBOUNDS dc.DrawRectangle(null, new Pen(new SolidColorBrush(Colors.DarkOrange), 1.0), rect); #endif #endif var runOutline = GlyphUtils.GetOutline(currentRun); //need to see if the run has been super or sub-scripted var variants = originalRun.Properties.TypographyProperties.Variants; if (variants == FontVariants.Subscript || variants == FontVariants.Superscript) { //simply union in the rect -it's easier! outline.AddRange(new[] { rectCopy.BottomLeft, rectCopy.BottomRight, rectCopy.TopLeft, rectCopy.TopRight }); } else { //add in the points from the convex hull for (int i = 0; i < runOutline.Count; i++) { var point = runOutline[i] + displacementVector + new Vector(0.0, myTextLine.Baseline); point.X += advanceWidths; runOutline[i] = point; } outline.AddRange(runOutline); } advanceWidths += currentRun.AdvanceWidths.Sum(); } _sortedOutline = (from Point p in outline orderby p.X ascending, p.Y descending select p).ToList(); Hull = Geometry <Point> .GetHull(_sortedOutline, p => p); // Diag: Show Hulls or Atom centres #if DEBUG #if SHOWHULLS dc.DrawGeometry(null, new Pen(Brushes.GreenYellow, thickness: 1), HullGeometry); #endif #if SHOWATOMCENTRES dc.DrawEllipse(Brushes.Red, null, ParentAtom.Position, 5, 5); #endif #endif // End Diag dc.Close(); } } } }
//public void FormatItemText(ItemText item) //{ // FormatParagraph(item.Paragraph); //} public void FormatNote() { string formattedText = ""; if (_note.Introduction != null && _note.Introduction.Paragraph != null) { _introduction = _note.Introduction.Paragraph; } if (_note.Textblock != null) { _textblocks = _note.Textblock; } if (_introduction.ParagraphText != null) { //string formattedIntroduction += LoopThrough(_introduction.ParagraphText); foreach (var paragraphItem in _introduction.ParagraphText) { if (paragraphItem.GetType() == typeof(string)) { //formattedIntroduction += FormatParagraphText(paragraphItem); } else { } } } DrawingGroup textDest = new DrawingGroup(); DrawingContext dc = textDest.Open(); //_textStore.Text = textToFormat.Text; _textStore.FontRendering = _currentRendering; TextFormatter formatter = TextFormatter.Create(); //while (textStorePosition < _textStore.Text.Length) //{ // using (TextLine myTextLine = formatter.FormatLine( // _textStore, // textStorePosition, // 96*6, // new GenericTextParagraphProperties(_currentRendering), // null)) // { // myTextLine.Draw(dc, linePosition, InvertAxes.None); // textStorePosition += myTextLine.Length; // linePosition.Y += myTextLine.Height; // } //} dc.Close(); //myDrawingBrush.Drawing = textDest; //return formattedNote; }
TextFormatterProvider() { idealModeTextFormatter = TextFormatter.Create(TextFormattingMode.Ideal); displayModeTextFormatter = TextFormatter.Create(TextFormattingMode.Display); }