Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 12fb6ae

Browse files
Text formatting
1 parent dfd1c1e commit 12fb6ae

2 files changed

Lines changed: 64 additions & 69 deletions

File tree

src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/BaseClasses/ExtensionMethods.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,23 @@ public static Rect ToWpf(this System.Drawing.Rectangle rect)
6060
return new Rect(rect.Location.ToWpf(), rect.Size.ToWpf());
6161
}
6262

63-
public static System.Windows.Media.Color ToWpf(this System.Drawing.Color c)
63+
public static Color ToWpf(this System.Drawing.Color c)
6464
{
65-
return System.Windows.Media.Color.FromArgb(c.A, c.R, c.G, c.B);
65+
return Color.FromArgb(c.A, c.R, c.G, c.B);
6666
}
6767

68+
69+
public static int FromPoints(double value)
70+
{
71+
return (int)Math.Round(value * 72.0 / 96.0);
72+
}
73+
74+
public static double ToPoints(int value)
75+
{
76+
return Math.Round(value * 96.0 / 72.0);
77+
}
78+
79+
6880
#endregion
6981

7082
#region DPI independence

src/AddIns/Misc/Reporting/ICSharpCode.Reporting/Src/Wpf/Visitor/FixedDocumentCreator.cs

Lines changed: 50 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -63,29 +63,64 @@ public static Canvas CreateContainer(ExportContainer container) {
6363

6464
public static FormattedText CreateFormattedText(ExportText exportText)
6565
{
66+
FlowDirection flowDirection;
67+
68+
var culture = CultureInfo.CurrentCulture;
69+
if (culture.TextInfo.IsRightToLeft) {
70+
flowDirection = FlowDirection.RightToLeft;
71+
} else {
72+
flowDirection = FlowDirection.LeftToRight;
73+
}
74+
75+
var emSize = ExtensionMethodes.ToPoints((int)exportText.Font.SizeInPoints +1);
76+
6677
var formattedText = new FormattedText(exportText.Text,
6778
CultureInfo.CurrentCulture,
68-
FlowDirection.LeftToRight,
79+
flowDirection,
6980
new Typeface(exportText.Font.FontFamily.Name),
70-
exportText.Font.Size,
81+
emSize,
7182
new SolidColorBrush(exportText.ForeColor.ToWpf()), null, TextFormattingMode.Display);
7283

73-
formattedText.MaxTextWidth = exportText.DesiredSize.Width * 96.0 / 72.0;
74-
formattedText.SetFontSize(Math.Floor(exportText.Font.Size * 96.0 / 72.0));
84+
formattedText.MaxTextWidth = ExtensionMethodes.ToPoints(exportText.DesiredSize.Width);
7585

76-
var td = new TextDecorationCollection() ;
77-
CheckUnderline(td,exportText);
78-
formattedText.SetTextDecorations(td);
86+
ApplyPrintStyles(formattedText,exportText);
87+
7988
return formattedText;
8089
}
8190

82-
static void CheckUnderline(TextDecorationCollection td, ExportText exportText)
83-
{
84-
if (exportText.Font.Underline) {
85-
td.Add(new TextDecoration{Location = TextDecorationLocation.Underline});
91+
92+
static void ApplyPrintStyles (FormattedText formattedText,ExportText exportText) {
93+
var font = exportText.Font;
94+
var textDecorations = new TextDecorationCollection();
95+
FontStyle fontStyle;
96+
FontWeight fontWeight;
97+
98+
if ((font.Style & System.Drawing.FontStyle.Italic) != 0) {
99+
fontStyle = FontStyles.Italic;
100+
} else {
101+
fontStyle = FontStyles.Normal;
102+
}
103+
104+
formattedText.SetFontStyle(fontStyle);
105+
106+
if ((font.Style & System.Drawing.FontStyle.Bold) != 0) {
107+
fontWeight = FontWeights.Bold;
108+
} else {
109+
fontWeight = FontWeights.Normal;
86110
}
111+
formattedText.SetFontWeight(fontWeight);
112+
113+
if ((font.Style & System.Drawing.FontStyle.Underline) != 0) {
114+
textDecorations.Add(TextDecorations.Underline);
115+
}
116+
117+
if ((font.Style & System.Drawing.FontStyle.Strikeout) != 0) {
118+
textDecorations.Add(TextDecorations.Strikethrough);
119+
}
120+
121+
formattedText.SetTextDecorations(textDecorations);
87122
}
88-
123+
89124

90125
static Canvas CreateCanvas(ExportContainer container){
91126
var canvas = new Canvas();
@@ -116,33 +151,9 @@ static void SetPosition (UIElement element,IExportColumn exportColumn) {
116151
FixedPage.SetTop(element,exportColumn.Location.Y);
117152
}
118153

119-
/*
120-
static void SetFont(TextBlock textBlock,IExportText exportText){
121-
textBlock.FontFamily = new FontFamily(exportText.Font.FontFamily.Name);
122-
123-
//http://www.codeproject.com/Articles/441009/Drawing-Formatted-Text-in-a-Windows-Forms-Applicat
124-
125-
textBlock.FontSize = Math.Floor(exportText.Font.Size * 96/72);
126-
127-
if (exportText.Font.Bold) {
128-
textBlock.FontWeight = FontWeights.Bold;
129-
}
130-
if (exportText.Font.Underline) {
131-
CreateUnderline(textBlock,exportText);
132-
}
133-
134-
if (exportText.Font.Italic) {
135-
textBlock.FontStyle = FontStyles.Italic ;
136-
}
137-
if (exportText.Font.Strikeout) {
138-
CreateStrikeout(textBlock,exportText);
139-
}
140-
}
141-
*/
142154

143155
static void SetContentAlignment(TextBlock textBlock,ExportText exportText)
144156
{
145-
// http://social.msdn.microsoft.com/Forums/vstudio/en-US/e480abb9-a86c-4f78-8955-dddb866bcfef/vertical-text-alignment-in-textblock?forum=wpf
146157
//Vertical alignment not working
147158

148159
switch (exportText.ContentAlignment) {
@@ -188,34 +199,6 @@ static void SetContentAlignment(TextBlock textBlock,ExportText exportText)
188199
}
189200

190201

191-
static void CreateStrikeout (TextBlock textBlock,IExportText exportColumn ){
192-
if (textBlock == null)
193-
throw new ArgumentNullException("textBlock");
194-
if (exportColumn == null)
195-
throw new ArgumentNullException("exportColumn");
196-
var strikeOut = new TextDecoration();
197-
strikeOut.Location = TextDecorationLocation.Strikethrough;
198-
199-
Pen p = CreateWpfPen(exportColumn);
200-
strikeOut.Pen = p ;
201-
strikeOut.PenThicknessUnit = TextDecorationUnit.FontRecommended;
202-
textBlock.TextDecorations.Add(strikeOut);
203-
}
204-
205-
/*
206-
static void CreateUnderline(TextBlock textBlock,IExportText exportColumn){
207-
if (exportColumn == null)
208-
throw new ArgumentNullException("exportColumn");
209-
if (textBlock == null)
210-
throw new ArgumentNullException("textBlock");
211-
var underLine = new TextDecoration();
212-
Pen p = CreateWpfPen(exportColumn);
213-
underLine.Pen = p ;
214-
underLine.PenThicknessUnit = TextDecorationUnit.FontRecommended;
215-
textBlock.TextDecorations.Add(underLine);
216-
}
217-
*/
218-
219202
public static Pen CreateWpfPen(IReportObject exportColumn){
220203
if (exportColumn == null)
221204
throw new ArgumentNullException("exportColumn");
@@ -226,9 +209,9 @@ public static Pen CreateWpfPen(IReportObject exportColumn){
226209
var exportGraphics = exportColumn as IExportGraphics;
227210
if (exportGraphics != null) {
228211
pen.Thickness = exportGraphics.Thickness;
229-
pen.DashStyle = FixedDocumentCreator.DashStyle(exportGraphics);
230-
pen.StartLineCap = FixedDocumentCreator.LineCap(exportGraphics.StartLineCap);
231-
pen.EndLineCap = FixedDocumentCreator.LineCap(exportGraphics.EndLineCap);
212+
pen.DashStyle = DashStyle(exportGraphics);
213+
pen.StartLineCap = LineCap(exportGraphics.StartLineCap);
214+
pen.EndLineCap = LineCap(exportGraphics.EndLineCap);
232215
}
233216
return pen;
234217
}

0 commit comments

Comments
 (0)