1
#region PDFsharp - A .NET library for processing PDF
4
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
8
// http://www.pdfsharp.com
9
// http://sourceforge.net/projects/pdfsharp
11
// Permission is hereby granted, free of charge, to any person obtaining a
12
// copy of this software and associated documentation files (the "Software"),
13
// to deal in the Software without restriction, including without limitation
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
// and/or sell copies of the Software, and to permit persons to whom the
16
// Software is furnished to do so, subject to the following conditions:
18
// The above copyright notice and this permission notice shall be included
19
// in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
// DEALINGS IN THE SOFTWARE.
33
using System.Drawing.Drawing2D;
36
using System.Windows.Media;
39
namespace PdfSharp.Drawing
41
// In GDI+ the functions Save/Restore, BeginContainer/EndContainer, Transform, SetClip and ResetClip
42
// can be combined in any order. E.g. you can set a clip region, save the graphics state, empty the
43
// clip region and draw without clipping. Then you can restore to the previous clip region. With PDF
44
// this behaviour is hard to implement. To solve this problem I first an automaton that keeps track
45
// of all clipping paths and the current transformation when the clip path was set. The automation
46
// manages a PDF graphics state stack to calculate the desired bahaviour. It also takes into consideration
47
// not to multiply with inverse matrixes when the user sets a new transformation matrix.
48
// After the design works on pager I decided not to implement it because it is much to large-scale.
49
// Instead I lay down some rules how to use the XGraphics class.
51
// * Before you set a transformation matrix save the graphics state (Save) or begin a new container
54
// * Instead of resetting the transformation matrix, call Restore or EndContainer. If you reset the
55
// transformation, in PDF must be multiplied with the inverse matrix. That leads to round off errors
56
// because in PDF file only 3 digits are used and Acrobat internally uses fixed point numbers (until
57
// versioin 6 or 7 I think).
59
// * When no clip path is defined, you can set or intersect a new path.
61
// * When a clip path is already defined, you can always intersect with a new one (wich leads in general
62
// to a smaller clip region).
64
// * When a clip path is already defined, you can only reset it to the empty region (ResetClip) when
65
// the graphics state stack is at the same position as it had when the clip path was defined. Otherwise
68
// Keeping these rules leads to easy to read code and best results in PDF output.
71
/// Represents the internal state of an XGraphics object.
73
internal class InternalGraphicsState
75
public InternalGraphicsState(XGraphics gfx)
80
public InternalGraphicsState(XGraphics gfx, XGraphicsState state)
84
state.InternalState = this;
86
// //GdiGraphicsState = state.GdiState;
88
// this.state = state;
89
// state.InternalState = this;
93
// this.state = state;
94
// state.InternalState = this;
98
public InternalGraphicsState(XGraphics gfx, XGraphicsContainer container)
101
container.InternalState = this;
103
// //GdiGraphicsState = container.GdiState;
105
// container.InternalState = this;
109
// container.InternalState = this;
114
/// Gets or sets the current transformation matrix.
116
public XMatrix Transform
118
get { return this.transform; }
119
set { this.transform = value; }
121
XMatrix transform = XMatrix.Identity;
137
if (this.gfx.targetContext == XGraphicTargetContext.WPF)
139
for (int idx = 0; idx < this.transformPushLevel; idx++)
141
this.transformPushLevel = 0;
142
for (int idx = 0; idx < this.geometryPushLevel; idx++)
144
this.geometryPushLevel = 0;
149
internal bool invalid;
153
/// The GDI+ GraphicsState if contructed from XGraphicsState.
155
public GraphicsState GdiGraphicsState;
159
public void SetTransform(MatrixTransform transform)
161
this.gfx.dc.PushTransform(transform);
162
this.transformPushLevel++;
164
int transformPushLevel;
166
public void SetClip(Geometry geometry)
168
this.gfx.dc.PushClip(geometry);
169
this.geometryPushLevel++;
171
int geometryPushLevel;
175
internal XGraphics gfx;
176
internal XGraphicsState state;
178
// /// The GDI+ GraphicsContainer if contructed from XGraphicsContainer.
180
// public GraphicsContainer GdiGraphicsContainer;