File: PdfShading.cs

package info (click to toggle)
pdfmod 0.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,196 kB
  • ctags: 9,346
  • sloc: cs: 50,590; xml: 1,177; sh: 709; makefile: 640
file content (262 lines) | stat: -rw-r--r-- 10,486 bytes parent folder | download
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
#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.Collections;
using System.Text;
using System.IO;
#if GDI
using System.Drawing;
using System.Drawing.Imaging;
#endif
#if WPF
using System.Windows.Media;
#endif
using PdfSharp.Drawing;
using PdfSharp.Fonts.TrueType;
using PdfSharp.Internal;
using PdfSharp.Pdf.Internal;

namespace PdfSharp.Pdf.Advanced
{
  /// <summary>
  /// Represents a shading dictionary.
  /// </summary>
  public sealed class PdfShading : PdfDictionary
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="PdfShading"/> class.
    /// </summary>
    public PdfShading(PdfDocument document)
      : base(document)
    { }

    /// <summary>
    /// Setups the shading from the specified brush.
    /// </summary>
    public void SetupFromBrush(XLinearGradientBrush brush)
    {
      if (brush == null)
        throw new ArgumentNullException("brush");

      PdfColorMode colorMode = this.document.Options.ColorMode;
      XColor color1 = ColorSpaceHelper.EnsureColorMode(colorMode, brush.color1);
      XColor color2 = ColorSpaceHelper.EnsureColorMode(colorMode, brush.color2);

      PdfDictionary function = new PdfDictionary();

      Elements[Keys.ShadingType] = new PdfInteger(2);
      if (colorMode != PdfColorMode.Cmyk)
        Elements[Keys.ColorSpace] = new PdfName("/DeviceRGB");
      else
        Elements[Keys.ColorSpace] = new PdfName("/DeviceCMYK");

      double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
      if (brush.useRect)
      {
        switch (brush.linearGradientMode)
        {
          case XLinearGradientMode.Horizontal:
            x1 = brush.rect.x;
            y1 = brush.rect.y;
            x2 = brush.rect.x + brush.rect.width;
            y2 = brush.rect.y;
            break;

          case XLinearGradientMode.Vertical:
            x1 = brush.rect.x;
            y1 = brush.rect.y;
            x2 = brush.rect.x;
            y2 = brush.rect.y + brush.rect.height;
            break;

          case XLinearGradientMode.ForwardDiagonal:
            x1 = brush.rect.x;
            y1 = brush.rect.y;
            x2 = brush.rect.x + brush.rect.width;
            y2 = brush.rect.y + brush.rect.height;
            break;

          case XLinearGradientMode.BackwardDiagonal:
            x1 = brush.rect.x + brush.rect.width;
            y1 = brush.rect.y;
            x2 = brush.rect.x;
            y2 = brush.rect.y + brush.rect.height;
            break;
        }
      }
      else
      {
        x1 = brush.point1.x;
        y1 = brush.point1.y;
        x2 = brush.point2.x;
        y2 = brush.point2.y;
      }
      Elements[Keys.Coords] = new PdfLiteral("[{0:0.###} {1:0.###} {2:0.###} {3:0.###}]", x1, y1, x2, y2);

      //Elements[Keys.Background] = new PdfRawItem("[0 1 1]");
      //Elements[Keys.Domain] = 
      Elements[Keys.Function] = function;
      //Elements[Keys.Extend] = new PdfRawItem("[true true]");

      string clr1 = "[" + PdfEncoders.ToString(color1, colorMode) + "]";
      string clr2 = "[" + PdfEncoders.ToString(color2, colorMode) + "]";

      function.Elements["/FunctionType"] = new PdfInteger(2);
      function.Elements["/C0"] = new PdfLiteral(clr1);
      function.Elements["/C1"] = new PdfLiteral(clr2);
      function.Elements["/Domain"] = new PdfLiteral("[0 1]");
      function.Elements["/N"] = new PdfInteger(1);
    }

    /// <summary>
    /// Common keys for all streams.
    /// </summary>
    internal sealed class Keys : KeysBase
    {
      /// <summary>
      /// (Required) The shading type:
      /// 1 Function-based shading
      /// 2 Axial shading
      /// 3 Radial shading
      /// 4 Free-form Gouraud-shaded triangle mesh
      /// 5 Lattice-form Gouraud-shaded triangle mesh
      /// 6 Coons patch mesh
      /// 7 Tensor-product patch mesh
      /// </summary>
      [KeyInfo(KeyType.Integer | KeyType.Required)]
      public const string ShadingType = "/ShadingType";

      /// <summary>
      /// (Required) The color space in which color values are expressed. This may be any device, 
      /// CIE-based, or special color space except a Pattern space.
      /// </summary>
      [KeyInfo(KeyType.NameOrArray | KeyType.Required)]
      public const string ColorSpace = "/ColorSpace";

      /// <summary>
      /// (Optional) An array of color components appropriate to the color space, specifying
      /// a single background color value. If present, this color is used, before any painting 
      /// operation involving the shading, to fill those portions of the area to be painted 
      /// that lie outside the bounds of the shading object. In the opaque imaging model, 
      /// the effect is as if the painting operation were performed twice: first with the 
      /// background color and then with the shading.
      /// </summary>
      [KeyInfo(KeyType.Array | KeyType.Optional)]
      public const string Background = "/Background";

      /// <summary>
      /// (Optional) An array of four numbers giving the left, bottom, right, and top coordinates, 
      /// respectively, of the shadings bounding box. The coordinates are interpreted in the 
      /// shadings target coordinate space. If present, this bounding box is applied as a temporary 
      /// clipping boundary when the shading is painted, in addition to the current clipping path
      /// and any other clipping boundaries in effect at that time.
      /// </summary>
      [KeyInfo(KeyType.Rectangle | KeyType.Optional)]
      public const string BBox = "/BBox";

      /// <summary>
      /// (Optional) A flag indicating whether to filter the shading function to prevent aliasing 
      /// artifacts. The shading operators sample shading functions at a rate determined by the 
      /// resolution of the output device. Aliasing can occur if the function is not smooththat
      /// is, if it has a high spatial frequency relative to the sampling rate. Anti-aliasing can
      /// be computationally expensive and is usually unnecessary, since most shading functions
      /// are smooth enough or are sampled at a high enough frequency to avoid aliasing effects.
      /// Anti-aliasing may not be implemented on some output devices, in which case this flag
      /// is ignored.
      /// Default value: false.
      /// </summary>
      [KeyInfo(KeyType.Boolean | KeyType.Optional)]
      public const string AntiAlias = "/AntiAlias";


      // ---- Type 2 ----------------------------------------------------------

      /// <summary>
      /// (Required) An array of four numbers [x0 y0 x1 y1] specifying the starting and
      /// ending coordinates of the axis, expressed in the shadings target coordinate space.
      /// </summary>
      [KeyInfo(KeyType.Array | KeyType.Required)]
      public const string Coords = "/Coords";

      /// <summary>
      /// (Optional) An array of two numbers [t0 t1] specifying the limiting values of a
      /// parametric variable t. The variable is considered to vary linearly between these
      /// two values as the color gradient varies between the starting and ending points of
      /// the axis. The variable t becomes the input argument to the color function(s).
      /// Default value: [0.0 1.0].
      /// </summary>
      [KeyInfo(KeyType.Array | KeyType.Optional)]
      public const string Domain = "/Domain";

      /// <summary>
      /// (Required) A 1-in, n-out function or an array of n 1-in, 1-out functions (where n
      /// is the number of color components in the shading dictionarys color space). The
      /// function(s) are called with values of the parametric variable t in the domain defined
      /// by the Domain entry. Each functions domain must be a superset of that of the shading
      /// dictionary. If the value returned by the function for a given color component is out
      /// of range, it is adjusted to the nearest valid value.
      /// </summary>
      [KeyInfo(KeyType.Function | KeyType.Required)]
      public const string Function = "/Function";

      /// <summary>
      /// (Optional) An array of two boolean values specifying whether to extend the shading
      /// beyond the starting and ending points of the axis, respectively.
      /// Default value: [false false].
      /// </summary>
      [KeyInfo(KeyType.Array | KeyType.Optional)]
      public const string Extend = "/Extend";

      /// <summary>
      /// Gets the KeysMeta for these keys.
      /// </summary>
      internal static DictionaryMeta Meta
      {
        get
        {
          if (Keys.meta == null)
            Keys.meta = CreateMeta(typeof(Keys));
          return Keys.meta;
        }
      }
      static DictionaryMeta meta;
    }

    /// <summary>
    /// Gets the KeysMeta of this dictionary type.
    /// </summary>
    internal override DictionaryMeta Meta
    {
      get { return Keys.Meta; }
    }
  }
}