Skip to content

Commit 0d1c4c6

Browse files
authored
Merge pull request #198 from Jetski5822/mf/textmeasurementnull
Fix null XFont issue
2 parents c54035d + f11d976 commit 0d1c4c6

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

MigraDocCore.DocumentObjectModel/MigraDoc/MigraDoc.DocumentObjectModel/TextMeasurement.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232

3333
using System;
3434
using System.Diagnostics;
35-
using System.ComponentModel;
36-
using MigraDocCore.DocumentObjectModel.Internals;
3735
using PdfSharpCore.Drawing;
3836

3937
namespace MigraDocCore.DocumentObjectModel
@@ -63,7 +61,7 @@ public XSize MeasureString(string text, UnitType unitType)
6361
if (!Enum.IsDefined(typeof(UnitType), unitType))
6462
throw new ArgumentException();
6563

66-
XSize size = graphics.MeasureString(text, this.xFont);
64+
var size = graphics.MeasureString(text, this.XFont);
6765
switch (unitType)
6866
{
6967
case UnitType.Point:
@@ -99,7 +97,7 @@ public XSize MeasureString(string text, UnitType unitType)
9997
/// <summary>
10098
/// Returns the size of the bounding box of the specified text in point.
10199
/// </summary>
102-
public PdfSharpCore.Drawing.XSize MeasureString(string text)
100+
public XSize MeasureString(string text)
103101
{
104102
return MeasureString(text, UnitType.Point);
105103
}
@@ -121,6 +119,32 @@ public Font Font
121119
}
122120
}
123121
}
122+
123+
/// <summary>
124+
/// Gets the xfont used for measurement.
125+
/// </summary>
126+
private XFont XFont
127+
{
128+
get {
129+
if (this.xFont == null)
130+
{
131+
var style = XFontStyle.Regular;
132+
if (this.Font.Bold)
133+
{
134+
style |= XFontStyle.Bold;
135+
}
136+
if (this.Font.Italic)
137+
{
138+
style |= XFontStyle.Italic;
139+
}
140+
141+
this.xFont = new XFont(this.Font.Name, this.Font.Size, style);
142+
}
143+
144+
return this.xFont;
145+
}
146+
}
147+
124148
Font font;
125149
XFont xFont;
126150
XGraphics graphics;

0 commit comments

Comments
 (0)