Skip to content

Commit a321192

Browse files
committed
Added the ability to create an XPen that uses an XBrush (instead of XColor) so XPens can have gradients;
1 parent 4de98f2 commit a321192

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

PdfSharpCore/Drawing.Pdf/PdfGraphicsState.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,11 @@ public void RealizePen(XPen pen, PdfColorMode colorMode)
197197
_realizedDashStyle = dashStyle;
198198
}
199199

200-
if (colorMode != PdfColorMode.Cmyk)
200+
if (pen.Brush != null)
201+
{
202+
RealizeBrush(pen.Brush, colorMode, 0, 0, true);
203+
}
204+
else if (colorMode != PdfColorMode.Cmyk)
201205
{
202206
if (_realizedStrokeColor.Rgb != color.Rgb)
203207
{
@@ -235,7 +239,7 @@ public void RealizePen(XPen pen, PdfColorMode colorMode)
235239
XColor _realizedFillColor = XColor.Empty;
236240
bool _realizedNonStrokeOverPrint;
237241

238-
public void RealizeBrush(XBrush brush, PdfColorMode colorMode, int renderingMode, double fontEmSize)
242+
public void RealizeBrush(XBrush brush, PdfColorMode colorMode, int renderingMode, double fontEmSize, bool isForPen = false)
239243
{
240244
// Rendering mode 2 is used for bold simulation.
241245
// Reference: TABLE 5.3 Text rendering modes / Page 402
@@ -273,9 +277,16 @@ public void RealizeBrush(XBrush brush, PdfColorMode colorMode, int renderingMode
273277
PdfShadingPattern pattern = new PdfShadingPattern(_renderer.Owner);
274278
pattern.SetupFromBrush(gradientBrush, matrix, _renderer);
275279
string name = _renderer.Resources.AddPattern(pattern);
276-
_renderer.AppendFormatString("/Pattern cs\n", name);
277-
_renderer.AppendFormatString("{0} scn\n", name);
278-
280+
if (isForPen)
281+
{
282+
_renderer.AppendFormatString("/Pattern CS\n", name);
283+
_renderer.AppendFormatString("{0} SCN\n", name);
284+
}
285+
else
286+
{
287+
_renderer.AppendFormatString("/Pattern cs\n", name);
288+
_renderer.AppendFormatString("{0} scn\n", name);
289+
}
279290
// Invalidate fill color.
280291
_realizedFillColor = XColor.Empty;
281292
}

PdfSharpCore/Drawing/XPen.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,30 @@ internal XPen(XColor color, double width, bool immutable)
7676
_immutable = immutable;
7777
}
7878

79+
/// <summary>
80+
/// Initializes a new instance of the <see cref="XPen"/> class
81+
/// </summary>
82+
/// <param name="brush"></param>
83+
public XPen(XBrush brush) : this(brush, 1, false) { }
84+
85+
/// <summary>
86+
/// Initializes a new instance of the <see cref="XPen"/> class
87+
/// </summary>
88+
/// <param name="brush"></param>
89+
/// <param name="width"></param>
90+
public XPen(XBrush brush, double width) : this(brush, width, false) { }
91+
92+
internal XPen(XBrush brush, double width, bool immutable)
93+
{
94+
_brush = brush;
95+
_width = width;
96+
_lineJoin = XLineJoin.Miter;
97+
_lineCap = XLineCap.Flat;
98+
_dashStyle = XDashStyle.Solid;
99+
_dashOffset = 0f;
100+
_immutable = immutable;
101+
}
102+
79103
/// <summary>
80104
/// Initializes a new instance of the <see cref="XPen"/> class.
81105
/// </summary>
@@ -100,6 +124,20 @@ public XPen Clone()
100124
return new XPen(this);
101125
}
102126

127+
public XBrush Brush
128+
{
129+
get => _brush;
130+
set
131+
{
132+
if (_immutable)
133+
throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
134+
_dirty = _dirty || _brush != value;
135+
_brush = value;
136+
_color = XColor.Empty;
137+
}
138+
}
139+
internal XBrush _brush;
140+
103141
/// <summary>
104142
/// Gets or sets the color.
105143
/// </summary>
@@ -112,6 +150,7 @@ public XColor Color
112150
throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
113151
_dirty = _dirty || _color != value;
114152
_color = value;
153+
_brush = null;
115154
}
116155
}
117156
internal XColor _color;

PdfSharpCore/Pdf.Internal/PdfEncoders.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public static byte[] FormatStringLiteral(byte[] bytes, bool unicode, bool prefix
426426
}
427427

428428
/// <summary>
429-
/// Converts WinAnsi to DocEncode characters. Incomplete, just maps € and some other characters.
429+
/// Converts WinAnsi to DocEncode characters. Incomplete, just maps ? and some other characters.
430430
/// </summary>
431431
static byte[] docencode_______ = new byte[256]
432432
{
@@ -605,8 +605,8 @@ public static string ToString(XColor color, PdfColorMode colorMode)
605605
color.C, color.M, color.Y, color.K);
606606

607607
default:
608-
return String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "}",
609-
color.R / 255.0, color.G / 255.0, color.B / 255.0);
608+
return String.Format(CultureInfo.InvariantCulture, "{0:" + format + "} {1:" + format + "} {2:" + format + "} {3:" + format + "}",
609+
color.R / 255.0, color.G / 255.0, color.B / 255.0, color.A);
610610
}
611611
}
612612

0 commit comments

Comments
 (0)