~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Drawing/InternalGraphicsState.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
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:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
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.
 
28
#endregion
 
29
 
 
30
using System;
 
31
#if GDI
 
32
using System.Drawing;
 
33
using System.Drawing.Drawing2D;
 
34
#endif
 
35
#if WPF
 
36
using System.Windows.Media;
 
37
#endif
 
38
 
 
39
namespace PdfSharp.Drawing
 
40
{
 
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.
 
50
  //
 
51
  // * Before you set a transformation matrix save the graphics state (Save) or begin a new container
 
52
  //   (BeginContainer).
 
53
  // 
 
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).
 
58
  //
 
59
  // * When no clip path is defined, you can set or intersect a new path.
 
60
  //
 
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).
 
63
  //
 
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
 
66
  //   an error occurs.
 
67
  //
 
68
  // Keeping these rules leads to easy to read code and best results in PDF output.
 
69
 
 
70
  /// <summary>
 
71
  /// Represents the internal state of an XGraphics object.
 
72
  /// </summary>
 
73
  internal class InternalGraphicsState
 
74
  {
 
75
    public InternalGraphicsState(XGraphics gfx)
 
76
    {
 
77
      this.gfx = gfx;
 
78
    }
 
79
 
 
80
    public InternalGraphicsState(XGraphics gfx, XGraphicsState state)
 
81
    {
 
82
      this.gfx = gfx;
 
83
      this.state = state;
 
84
      state.InternalState = this;
 
85
      //#if GDI
 
86
      //      //GdiGraphicsState = state.GdiState;
 
87
      //      this.gfx = gfx;
 
88
      //      this.state = state;
 
89
      //      state.InternalState = this;
 
90
      //#endif
 
91
      //#if WPF
 
92
      //      this.gfx = gfx;
 
93
      //      this.state = state;
 
94
      //      state.InternalState = this;
 
95
      //#endif
 
96
    }
 
97
 
 
98
    public InternalGraphicsState(XGraphics gfx, XGraphicsContainer container)
 
99
    {
 
100
      this.gfx = gfx;
 
101
      container.InternalState = this;
 
102
      //#if GDI
 
103
      //      //GdiGraphicsState = container.GdiState;
 
104
      //      this.gfx = gfx;
 
105
      //      container.InternalState = this;
 
106
      //#endif
 
107
      //#if WPF
 
108
      //      this.gfx = gfx;
 
109
      //      container.InternalState = this;
 
110
      //#endif
 
111
    }
 
112
 
 
113
    /// <summary>
 
114
    /// Gets or sets the current transformation matrix.
 
115
    /// </summary>
 
116
    public XMatrix Transform
 
117
    {
 
118
      get { return this.transform; }
 
119
      set { this.transform = value; }
 
120
    }
 
121
    XMatrix transform = XMatrix.Identity;
 
122
 
 
123
    public void Pushed()
 
124
    {
 
125
#if GDI
 
126
#endif
 
127
#if WPF
 
128
#endif
 
129
    }
 
130
 
 
131
    public void Popped()
 
132
    {
 
133
      this.invalid = true;
 
134
#if GDI
 
135
#endif
 
136
#if WPF
 
137
      if (this.gfx.targetContext == XGraphicTargetContext.WPF)
 
138
      {
 
139
        for (int idx = 0; idx < this.transformPushLevel; idx++)
 
140
          this.gfx.dc.Pop();
 
141
        this.transformPushLevel = 0;
 
142
        for (int idx = 0; idx < this.geometryPushLevel; idx++)
 
143
          this.gfx.dc.Pop();
 
144
        this.geometryPushLevel = 0;
 
145
      }
 
146
#endif
 
147
    }
 
148
 
 
149
    internal bool invalid;
 
150
 
 
151
#if GDI_
 
152
    /// <summary>
 
153
    /// The GDI+ GraphicsState if contructed from XGraphicsState.
 
154
    /// </summary>
 
155
    public GraphicsState GdiGraphicsState;
 
156
#endif
 
157
 
 
158
#if WPF
 
159
    public void SetTransform(MatrixTransform transform)
 
160
    {
 
161
      this.gfx.dc.PushTransform(transform);
 
162
      this.transformPushLevel++;
 
163
    }
 
164
    int transformPushLevel;
 
165
 
 
166
    public void SetClip(Geometry geometry)
 
167
    {
 
168
      this.gfx.dc.PushClip(geometry);
 
169
      this.geometryPushLevel++;
 
170
    }
 
171
    int geometryPushLevel;
 
172
 
 
173
#endif
 
174
 
 
175
    internal XGraphics gfx;
 
176
    internal XGraphicsState state;
 
177
    // /// <summary>
 
178
    // /// The GDI+ GraphicsContainer if contructed from XGraphicsContainer.
 
179
    // /// </summary>
 
180
    // public GraphicsContainer GdiGraphicsContainer;
 
181
 
 
182
  }
 
183
}