Working with TextFormatter, I came accross this article on MSDN which shows the following sample code :
使用TextFormatter,我在MSDN上发表了这篇文章,其中显示了以下示例代码:
// 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;
}
}
The problem I am having is even after referencing System.Windows.Media.TextFormatting
there is no class/object using CustomTextParagraphProperties
so that line throws an error (of course since it doesn't exist)
我遇到的问题是甚至在引用System.Windows.Media.TextFormatting之后,没有使用CustomTextParagraphProperties的类/对象,因此该行抛出错误(当然因为它不存在)
What else do I need to reference to access this class ?
我还需要参考什么来访问这个类?
This is my using
list :
这是我的使用清单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing.Printing;
using System.ComponentModel;
using System.Windows.Media.TextFormatting;
using System.IO;
And this is the list of my references:
这是我的参考文献清单:
I found TextParagraphProperties
but this is an abstract
class and thus can not be created using new
, meaning it isn't a drop in replacement for CustomTextParagraphProperties
as the example shows, the following line would not work :
我找到了TextParagraphProperties,但这是一个抽象类,因此不能使用new创建,这意味着它不是替换CustomTextParagraphProperties,如示例所示,以下行不起作用:
TextParagraphProperties tp = new TextParagraphProperties();
NOTE: I added the WPF tag as this project inherits WPF functionality, but this is not a WPF project.
注意:我添加了WPF标记,因为此项目继承了WPF功能,但这不是WPF项目。
I started to delve down a rabbit hole, and finding more and more codependant abstract
classes the parent depends upon, and since can't use the new
to inistialize the object, must write a bunch of code, with no end in sight, this is rather frustrating :
我开始深入研究一个兔子洞,并发现越来越多的父依赖的抽象类,并且由于不能使用新的来对象进行反应,所以必须编写一堆代码,没有尽头,这是相当令人沮丧:
#region textformatter
class TextRunProperties : System.Windows.Media.TextFormatting.TextRunProperties {
public override Windows.Media.Brush BackgroundBrush { get; set; }
public override Windows.BaselineAlignment BaselineAlignment { get { return base.BaselineAlignment; } }
public override CultureInfo CultureInfo { get; set; }
public override double FontHintingEmSize { get; set; }
public override double FontRenderingEmSize { get; set; }
public override Windows.Media.Brush ForegroundBrush { get; set; }
public override Windows.Media.NumberSubstitution NumberSubstitution { get { return base.NumberSubstitution; } }
public override TextDecorationCollection TextDecorations { get; set; }
public override Windows.Media.TextEffectCollection TextEffects { get; set; }
public override Windows.Media.Typeface Typeface { get; set; }
public override Windows.Media.TextFormatting.TextRunTypographyProperties TypographyProperties { get { return base.TypographyProperties; } }
}
class TextRun : System.Windows.Media.TextFormatting.TextRun {
public override CharacterBufferReference CharacterBufferReference { get; set; }
public override int Length { get; set; }
public override Windows.Media.TextFormatting.TextRunProperties Properties { get; set; }
}
class TextSource : System.Windows.Media.TextFormatting.TextSource {
public override Windows.Media.TextFormatting.TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText( int textSourceCharacterIndexLimit ) {
return new TextSpan<CultureSpecificCharacterBufferRange>(
textSourceCharacterIndexLimit,
new CultureSpecificCharacterBufferRange(
new CultureInfo(0),
Windows.Media.TextFormatting.CharacterBufferRange.Empty
)
);
}
public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex( int textSourceCharacterIndex ) {
return 0;
}
public override Windows.Media.TextFormatting.TextRun GetTextRun( int textSourceCharacterIndex ) {
return new TextRun();
}
}
class TextFormatter : System.Windows.Media.TextFormatting.TextFormatter {
public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak, TextRunCache textRunCache ) {
throw new NotImplementedException();
}
public override Windows.Media.TextFormatting.TextLine FormatLine( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, double paragraphWidth, TextParagraphProperties paragraphProperties, TextLineBreak previousLineBreak ) {
throw new NotImplementedException();
}
public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties ) {
throw new NotImplementedException();
}
public override Windows.Media.TextFormatting.MinMaxParagraphWidth FormatMinMaxParagraphWidth( Windows.Media.TextFormatting.TextSource textSource, int firstCharIndex, TextParagraphProperties paragraphProperties, TextRunCache textRunCache ) {
throw new NotImplementedException();
}
}
class TextLine : System.Windows.Media.TextFormatting.TextLine {
public override double Baseline {
get { throw new NotImplementedException(); }
}
public override int DependentLength {
get { throw new NotImplementedException(); }
}
public override double Extent {
get { throw new NotImplementedException(); }
}
public override bool HasCollapsed {
get { throw new NotImplementedException(); }
}
public override bool HasOverflowed {
get { throw new NotImplementedException(); }
}
public override double Height {
get { throw new NotImplementedException(); }
}
public override bool IsTruncated {
get {
return base.IsTruncated;
}
}
public override int Length {
get { throw new NotImplementedException(); }
}
public override double MarkerBaseline {
get { throw new NotImplementedException(); }
}
public override double MarkerHeight {
get { throw new NotImplementedException(); }
}
public override int NewlineLength {
get { throw new NotImplementedException(); }
}
public override double OverhangAfter {
get { throw new NotImplementedException(); }
}
public override double OverhangLeading {
get { throw new NotImplementedException(); }
}
public override double OverhangTrailing {
get { throw new NotImplementedException(); }
}
public override double Start {
get { throw new NotImplementedException(); }
}
public override double TextBaseline {
get { throw new NotImplementedException(); }
}
public override double TextHeight {
get { throw new NotImplementedException(); }
}
public override int TrailingWhitespaceLength {
get { throw new NotImplementedException(); }
}
public override double Width {
get { throw new NotImplementedException(); }
}
public override double WidthIncludingTrailingWhitespace {
get { throw new NotImplementedException(); }
}
// functions
public override Windows.Media.TextFormatting.TextLine Collapse( params TextCollapsingProperties[] collapsingPropertiesList ) {
throw new NotImplementedException();
}
public override void Draw( DrawingContext drawingContext, Windows.Point origin, InvertAxes inversion ) {
throw new NotImplementedException();
}
public override CharacterHit GetBackspaceCaretCharacterHit( CharacterHit characterHit ) {
throw new NotImplementedException();
}
public override CharacterHit GetCharacterHitFromDistance( double distance ) {
throw new NotImplementedException();
}
public override double GetDistanceFromCharacterHit( CharacterHit characterHit ) {
throw new NotImplementedException();
}
public override IEnumerable<IndexedGlyphRun> GetIndexedGlyphRuns() {
throw new NotImplementedException();
}
public override CharacterHit GetNextCaretCharacterHit( CharacterHit characterHit ) {
throw new NotImplementedException();
}
public override CharacterHit GetPreviousCaretCharacterHit( CharacterHit characterHit ) {
throw new NotImplementedException();
}
public override IList<TextBounds> GetTextBounds( int firstTextSourceCharacterIndex, int textLength ) {
throw new NotImplementedException();
}
public override IList<TextCollapsedRange> GetTextCollapsedRanges() {
throw new NotImplementedException();
}
public override TextLineBreak GetTextLineBreak() {
throw new NotImplementedException();
}
public override IList<TextSpan<Windows.Media.TextFormatting.TextRun>> GetTextRunSpans() {
throw new NotImplementedException();
}
}
#endregion
I don't even know if that is correct, but from what it seems, none of these must override
methods return anything, so what exactly does this class do ? Does it event process any of the code such as getting a TextEffect, or is this all stuff that has to be hand written -- aka, I must make up my own TextEffects and stuff them in that code as whatever abstract structure with more methods that do nothing ....
我甚至不知道这是否正确,但从它看来,这些都不能覆盖方法返回任何东西,那么这个类到底是做什么的呢?它是否会处理任何代码,例如获取TextEffect,或者这是所有必须手写的东西 - 也就是说,我必须组成我自己的TextEffects并将它们填充到代码中作为任何具有更多方法的抽象结构没做什么 ....
1 个解决方案
#1
0
You will have to derive your own class from TextParagraphProperties
and override the properties as per your requirement
您必须从TextParagraphProperties派生自己的类,并根据您的要求覆盖属性
public class CustomTextParagraphProperties : TextParagraphProperties{...}
you will also have to derive TextRunProperties
and TextMarkerProperties
to return your own values.
您还必须派生TextRunProperties和TextMarkerProperties以返回您自己的值。
public class CustomTextRunProperties : TextRunProperties{...}
public class CustomTextMarkerProperties : TextMarkerProperties{...}
#1
0
You will have to derive your own class from TextParagraphProperties
and override the properties as per your requirement
您必须从TextParagraphProperties派生自己的类,并根据您的要求覆盖属性
public class CustomTextParagraphProperties : TextParagraphProperties{...}
you will also have to derive TextRunProperties
and TextMarkerProperties
to return your own values.
您还必须派生TextRunProperties和TextMarkerProperties以返回您自己的值。
public class CustomTextRunProperties : TextRunProperties{...}
public class CustomTextMarkerProperties : TextMarkerProperties{...}