1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
//
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using System.IO;
#if GDI
using System.Drawing;
using System.Drawing.Drawing2D;
#endif
#if WPF
using System.Windows.Media;
#endif
using PdfSharp.Internal;
using PdfSharp.Pdf;
using PdfSharp.Pdf.Advanced;
using PdfSharp.Pdf.Internal;
namespace PdfSharp.Drawing.Pdf
{
// TODO: update the following text
//
// In PDF the current transformation matrix (CTM) can only be modified, but not set. The XGraphics
// object allows to set the transformation matrix, which leads to a problem. In PDF the only way
// to reset the CTM to its original value is to save and restore the PDF graphics state. Don't try
// to keep track of every modification and then reset the CTM by multiplying with the inverse matrix
// of the product of all modifications. PDFlib uses this 'trick', but it does not work. Because of
// rounding errors everything on the PDF page looks sloping after some resets. Saving and restoring
// the graphics state is the only possible way to reset the CTM, but because the PDF restore operator
// 'Q' resets not only the CTM but all other graphics state values, we have to implement our own
// graphics state management. This is apparently the only safe way to give the XGrahics users the
// illusion that they can arbitrarily set the transformation matrix.
//
// The current implementation is just a draft. Save/Restore works only once and clipping is not
// correctly restored in some cases.
/// <summary>
/// Represents the current PDF graphics state.
/// </summary>
internal sealed class PdfGraphicsState : ICloneable
{
public PdfGraphicsState(XGraphicsPdfRenderer renderer)
{
this.renderer = renderer;
}
XGraphicsPdfRenderer renderer;
public PdfGraphicsState Clone()
{
PdfGraphicsState state = (PdfGraphicsState)MemberwiseClone();
return state;
}
object ICloneable.Clone()
{
return Clone();
}
internal int Level;
internal InternalGraphicsState InternalState;
public void PushState()
{
//BeginGraphic
this.renderer.Append("q/n");
}
public void PopState()
{
//BeginGraphic
this.renderer.Append("Q/n");
}
#region Stroke
double realizedLineWith = -1;
int realizedLineCap = -1;
int realizedLineJoin = -1;
double realizedMiterLimit = -1;
XDashStyle realizedDashStyle = (XDashStyle)(-1);
string realizedDashPattern;
XColor realizedStrokeColor = XColor.Empty;
public void RealizePen(XPen pen, PdfColorMode colorMode)
{
XColor color = pen.Color;
color = ColorSpaceHelper.EnsureColorMode(colorMode, color);
if (this.realizedLineWith != pen.width)
{
this.renderer.AppendFormat("{0:0.###} w\n", pen.width);
this.realizedLineWith = pen.width;
}
if (this.realizedLineCap != (int)pen.lineCap)
{
this.renderer.AppendFormat("{0} J\n", (int)pen.lineCap);
this.realizedLineCap = (int)pen.lineCap;
}
if (this.realizedLineJoin != (int)pen.lineJoin)
{
this.renderer.AppendFormat("{0} j\n", (int)pen.lineJoin);
this.realizedLineJoin = (int)pen.lineJoin;
}
if (this.realizedLineCap == (int)XLineJoin.Miter)
{
if (this.realizedMiterLimit != (int)pen.miterLimit && (int)pen.miterLimit != 0)
{
this.renderer.AppendFormat("{0} M\n", (int)pen.miterLimit);
this.realizedMiterLimit = (int)pen.miterLimit;
}
}
if (this.realizedDashStyle != pen.dashStyle || pen.dashStyle == XDashStyle.Custom)
{
double dot = pen.Width;
double dash = 3 * dot;
// Line width 0 is not recommended but valid
XDashStyle dashStyle = pen.DashStyle;
if (dot == 0)
dashStyle = XDashStyle.Solid;
switch (dashStyle)
{
case XDashStyle.Solid:
this.renderer.Append("[]0 d\n");
break;
case XDashStyle.Dash:
this.renderer.AppendFormat("[{0:0.##} {1:0.##}]0 d\n", dash, dot);
break;
case XDashStyle.Dot:
this.renderer.AppendFormat("[{0:0.##}]0 d\n", dot);
break;
case XDashStyle.DashDot:
this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
break;
case XDashStyle.DashDotDot:
this.renderer.AppendFormat("[{0:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##} {1:0.##}]0 d\n", dash, dot);
break;
case XDashStyle.Custom:
{
StringBuilder pdf = new StringBuilder("[", 256);
int len = pen.dashPattern == null ? 0 : pen.dashPattern.Length;
for (int idx = 0; idx < len; idx++)
{
if (idx > 0)
pdf.Append(' ');
pdf.Append(PdfEncoders.ToString(pen.dashPattern[idx] * pen.width));
}
// Make an even number of values look like in GDI+
if (len > 0 && len % 2 == 1)
{
pdf.Append(' ');
pdf.Append(PdfEncoders.ToString(0.2 * pen.width));
}
pdf.AppendFormat(CultureInfo.InvariantCulture, "]{0:0.###} d\n", pen.dashOffset * pen.width);
string pattern = pdf.ToString();
// BUG: drice2@ageone.de reported a realizing problem
// HACK: I romove the if clause
//if (this.realizedDashPattern != pattern)
{
this.realizedDashPattern = pattern;
this.renderer.Append(pattern);
}
}
break;
}
this.realizedDashStyle = dashStyle;
}
if (colorMode != PdfColorMode.Cmyk)
{
if (this.realizedStrokeColor.Rgb != color.Rgb)
{
this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
this.renderer.Append(" RG\n");
}
}
else
{
if (!ColorSpaceHelper.IsEqualCmyk(this.realizedStrokeColor, color))
{
this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
this.renderer.Append(" K\n");
}
}
if (this.renderer.Owner.Version >= 14 && this.realizedStrokeColor.A != color.A)
{
PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateStroke(color.A);
string gs = this.renderer.Resources.AddExtGState(extGState);
this.renderer.AppendFormat("{0} gs\n", gs);
// Must create transparany group
if (this.renderer.page != null && color.A < 1)
this.renderer.page.transparencyUsed = true;
}
this.realizedStrokeColor = color;
}
#endregion
#region Fill
XColor realizedFillColor = XColor.Empty;
public void RealizeBrush(XBrush brush, PdfColorMode colorMode)
{
if (brush is XSolidBrush)
{
XColor color = ((XSolidBrush)brush).Color;
color = ColorSpaceHelper.EnsureColorMode(colorMode, color);
if (colorMode != PdfColorMode.Cmyk)
{
if (this.realizedFillColor.Rgb != color.Rgb)
{
this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Rgb));
this.renderer.Append(" rg\n");
}
}
else
{
if (!ColorSpaceHelper.IsEqualCmyk(this.realizedFillColor, color))
{
this.renderer.Append(PdfEncoders.ToString(color, PdfColorMode.Cmyk));
this.renderer.Append(" k\n");
}
}
if (this.renderer.Owner.Version >= 14 && this.realizedFillColor.A != color.A)
{
PdfExtGState extGState = this.renderer.Owner.ExtGStateTable.GetExtGStateNonStroke(color.A);
string gs = this.renderer.Resources.AddExtGState(extGState);
this.renderer.AppendFormat("{0} gs\n", gs);
// Must create transparany group
if (this.renderer.page != null && color.A < 1)
this.renderer.page.transparencyUsed = true;
}
this.realizedFillColor = color;
}
else if (brush is XLinearGradientBrush)
{
XMatrix matrix = this.renderer.defaultViewMatrix;
matrix.Prepend(this.Transform);
PdfShadingPattern pattern = new PdfShadingPattern(this.renderer.Owner);
pattern.SetupFromBrush((XLinearGradientBrush)brush, matrix);
string name = this.renderer.Resources.AddPattern(pattern);
this.renderer.AppendFormat("/Pattern cs\n", name);
this.renderer.AppendFormat("{0} scn\n", name);
// Invalidate fill color
this.realizedFillColor = XColor.Empty;
}
}
#endregion
#region Text
internal PdfFont realizedFont;
string realizedFontName = String.Empty;
double realizedFontSize = 0;
public void RealizeFont(XFont font, XBrush brush, int renderMode)
{
// So far rendering mode 0 only
RealizeBrush(brush, this.renderer.colorMode); // this.renderer.page.document.Options.ColorMode);
this.realizedFont = null;
string fontName = this.renderer.GetFontName(font, out this.realizedFont);
if (fontName != this.realizedFontName || this.realizedFontSize != font.Size)
{
if (this.renderer.Gfx.PageDirection == XPageDirection.Downwards)
this.renderer.AppendFormat("{0} {1:0.###} Tf\n", fontName, -font.Size);
else
this.renderer.AppendFormat("{0} {1:0.###} Tf\n", fontName, font.Size);
this.realizedFontName = fontName;
this.realizedFontSize = font.Size;
}
}
public XPoint realizedTextPosition = new XPoint();
#endregion
#region Transformation
/// <summary>
/// The realized current transformation matrix.
/// </summary>
XMatrix realizedCtm = XMatrix.Identity;
/// <summary>
/// The unrealized current transformation matrix.
/// </summary>
XMatrix unrealizedCtm = XMatrix.Identity;
/// <summary>
/// A flag indicating whether the CTM must be realized.
/// </summary>
public bool MustRealizeCtm;
public XMatrix Transform
{
get
{
if (this.MustRealizeCtm)
{
XMatrix matrix = this.realizedCtm;
matrix.Prepend(this.unrealizedCtm);
return matrix;
}
return this.realizedCtm;
}
set
{
XMatrix matrix = this.realizedCtm;
matrix.Invert();
matrix.Prepend(value);
this.unrealizedCtm = matrix;
this.MustRealizeCtm = !this.unrealizedCtm.IsIdentity;
}
}
/// <summary>
/// Modifies the current transformation matrix.
/// </summary>
public void MultiplyTransform(XMatrix matrix, XMatrixOrder order)
{
if (!matrix.IsIdentity)
{
this.MustRealizeCtm = true;
this.unrealizedCtm.Multiply(matrix, order);
}
}
/// <summary>
/// Realizes the CTM.
/// </summary>
public void RealizeCtm()
{
if (this.MustRealizeCtm)
{
Debug.Assert(!this.unrealizedCtm.IsIdentity, "mrCtm is unnecessarily set.");
double[] matrix = this.unrealizedCtm.GetElements();
// Up to six decimal digits to prevent round up problems
this.renderer.AppendFormat("{0:0.######} {1:0.######} {2:0.######} {3:0.######} {4:0.######} {5:0.######} cm\n",
matrix[0], matrix[1], matrix[2], matrix[3], matrix[4], matrix[5]);
this.realizedCtm.Prepend(this.unrealizedCtm);
this.unrealizedCtm = XMatrix.Identity;
this.MustRealizeCtm = false;
}
}
#endregion
#region Clip Path
public void SetAndRealizeClipRect(XRect clipRect)
{
XGraphicsPath clipPath = new XGraphicsPath();
clipPath.AddRectangle(clipRect);
RealizeClipPath(clipPath);
}
public void SetAndRealizeClipPath(XGraphicsPath clipPath)
{
RealizeClipPath(clipPath);
}
void RealizeClipPath(XGraphicsPath clipPath)
{
this.renderer.BeginGraphic();
RealizeCtm();
#if GDI && !WPF
this.renderer.AppendPath(clipPath.gdipPath);
#endif
#if WPF &&!GDI
this.renderer.AppendPath(clipPath.pathGeometry);
#endif
#if WPF && GDI
if (this.renderer.Gfx.targetContext == XGraphicTargetContext.GDI)
{
this.renderer.AppendPath(clipPath.gdipPath);
}
else
{
this.renderer.AppendPath(clipPath.pathGeometry);
}
#endif
if (clipPath.FillMode == XFillMode.Winding)
this.renderer.Append("W n\n");
else
this.renderer.Append("W* n\n");
}
#endregion
}
}
|