File: XPen.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 (373 lines) | stat: -rw-r--r-- 11,330 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
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
#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.IO;
#if GDI
using System.Drawing;
using System.Drawing.Drawing2D;
#endif
#if WPF
using System.Windows;
using System.Windows.Media;
#endif
using PdfSharp.Internal;

namespace PdfSharp.Drawing
{
  /// <summary>
  /// Defines an object used to draw lines and curves.
  /// </summary>
  public sealed class XPen
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="XPen"/> class.
    /// </summary>
    public XPen(XColor color)
      : this(color, 1, false)
    { }

    /// <summary>
    /// Initializes a new instance of the <see cref="XPen"/> class.
    /// </summary>
    public XPen(XColor color, double width)
      : this(color, width, false)
    { }

    internal XPen(XColor color, double width, bool immutable)
    {
      this.color = color;
      this.width = width;
      this.lineJoin = XLineJoin.Miter;
      this.lineCap = XLineCap.Flat;
      this.dashStyle = XDashStyle.Solid;
      this.dashOffset = 0f;
      this.immutable = immutable;
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="XPen"/> class.
    /// </summary>
    public XPen(XPen pen)
    {
      this.color = pen.color;
      this.width = pen.width;
      this.lineJoin = pen.lineJoin;
      this.lineCap = pen.lineCap;
      this.dashStyle = pen.dashStyle;
      this.dashOffset = pen.dashOffset;
      this.dashPattern = pen.dashPattern;
      if (this.dashPattern != null)
        this.dashPattern = (double[])this.dashPattern.Clone();
    }

    /// <summary>
    /// Clones this instance.
    /// </summary>
    public XPen Clone()
    {
      return new XPen(this);
    }

    /// <summary>
    /// Gets or sets the color.
    /// </summary>
    public XColor Color
    {
      get { return this.color; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.color != value;
        this.color = value;
      }
    }
    internal XColor color;

    /// <summary>
    /// Gets or sets the width.
    /// </summary>
    public double Width
    {
      get { return this.width; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.width != value;
        this.width = value;
      }
    }
    internal double width;

    /// <summary>
    /// Gets or sets the line join.
    /// </summary>
    public XLineJoin LineJoin
    {
      get { return this.lineJoin; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.lineJoin != value;
        this.lineJoin = value;
      }
    }
    internal XLineJoin lineJoin;

    /// <summary>
    /// Gets or sets the line cap.
    /// </summary>
    public XLineCap LineCap
    {
      get { return this.lineCap; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.lineCap != value;
        this.lineCap = value;
      }
    }
    internal XLineCap lineCap;

    /// <summary>
    /// Gets or sets the miter limit.
    /// </summary>
    public double MiterLimit
    {
      get { return this.miterLimit; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.miterLimit != value;
        this.miterLimit = value;
      }
    }
    internal double miterLimit;

    /// <summary>
    /// Gets or sets the dash style.
    /// </summary>
    public XDashStyle DashStyle
    {
      get { return this.dashStyle; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.dashStyle != value;
        this.dashStyle = value;
      }
    }
    internal XDashStyle dashStyle;

    /// <summary>
    /// Gets or sets the dash offset.
    /// </summary>
    public double DashOffset
    {
      get { return this.dashOffset; }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));
        this.dirty = this.dirty || this.dashOffset != value;
        this.dashOffset = value;
      }
    }
    internal double dashOffset;

    /// <summary>
    /// Gets or sets the dash pattern.
    /// </summary>
    public double[] DashPattern
    {
      get
      {
        if (this.dashPattern == null)
          this.dashPattern = new double[0];
        return this.dashPattern;
      }
      set
      {
        if (this.immutable)
          throw new ArgumentException(PSSR.CannotChangeImmutableObject("XPen"));

        int length = value.Length;
        //if (length == 0)
        //  throw new ArgumentException("Dash pattern array must not be empty.");

        for (int idx = 0; idx < length; idx++)
        {
          if (value[idx] <= 0)
            throw new ArgumentException("Dash pattern value must greater than zero.");
        }

        this.dirty = true;
        this.dashStyle = XDashStyle.Custom;
        this.dashPattern = (double[])value.Clone();
      }
    }
    internal double[] dashPattern;

#if GDI
#if UseGdiObjects
    /// <summary>
    /// Implicit convertion from Pen to XPen
    /// </summary>
    public static implicit operator XPen(Pen pen)
    {
      XPen xpen;
      switch (pen.PenType)
      {
        case PenType.SolidColor:
          xpen = new XPen(pen.Color, pen.Width);
          xpen.LineJoin = (XLineJoin)pen.LineJoin;
          xpen.DashStyle = (XDashStyle)pen.DashStyle;
          xpen.miterLimit = pen.MiterLimit;
          break;

        default:
          throw new NotImplementedException("Pen type not supported by PDFsharp.");
      }
      // Bug fixed by drice2@ageone.de
      if (pen.DashStyle == System.Drawing.Drawing2D.DashStyle.Custom)
      {
        int length = pen.DashPattern.Length;
        double[] pattern = new double[length];
        for (int idx = 0; idx < length; idx++)
          pattern[idx] = pen.DashPattern[idx];
        xpen.DashPattern = pattern;
        xpen.dashOffset = pen.DashOffset;
      }
      return xpen;
    }
#endif

    internal System.Drawing.Pen RealizeGdiPen()
    {
      if (this.dirty)
      {
        if (this.gdiPen == null)
          this.gdiPen = new System.Drawing.Pen(this.color.ToGdiColor(), (float)this.width);
        else
        {
          this.gdiPen.Color = this.color.ToGdiColor();
          this.gdiPen.Width = (float)this.width;
        }
        LineCap lineCap = XConvert.ToLineCap(this.lineCap);
        this.gdiPen.StartCap = lineCap;
        this.gdiPen.EndCap = lineCap;
        this.gdiPen.LineJoin = XConvert.ToLineJoin(this.lineJoin);
        this.gdiPen.DashOffset = (float)this.dashOffset;
        if (this.dashStyle == XDashStyle.Custom)
        {
          int len = this.dashPattern == null ? 0 : this.dashPattern.Length;
          float[] pattern = new float[len];
          for (int idx = 0; idx < len; idx++)
            pattern[idx] = (float)this.dashPattern[idx];
          this.gdiPen.DashPattern = pattern;
        }
        else
          this.gdiPen.DashStyle = (System.Drawing.Drawing2D.DashStyle)this.dashStyle;
      }
      return this.gdiPen;
    }
#endif

#if WPF
    internal System.Windows.Media.Pen RealizeWpfPen()
    {
      if (this.dirty || !this.dirty) // TODOWPF
      {
        //if (this.wpfPen == null)
        this.wpfPen = new System.Windows.Media.Pen(new SolidColorBrush(this.color.ToWpfColor()), this.width);
        //else
        //{
        //  this.wpfPen.Brush = new SolidColorBrush(this.color.ToWpfColor());
        //  this.wpfPen.Thickness = this.width;
        //}
        PenLineCap lineCap = XConvert.ToPenLineCap(this.lineCap);
        this.wpfPen.StartLineCap = lineCap;
        this.wpfPen.EndLineCap = lineCap;
        this.wpfPen.LineJoin = XConvert.ToPenLineJoin(this.lineJoin);
        if (this.dashStyle == XDashStyle.Custom)
        {
          // TODOWPF
          this.wpfPen.DashStyle = new System.Windows.Media.DashStyle(this.dashPattern, this.dashOffset);
        }
        else
        {
          switch (this.dashStyle)
          {
            case XDashStyle.Solid:
              this.wpfPen.DashStyle = DashStyles.Solid;
              break;

            case XDashStyle.Dash:
              //this.wpfPen.DashStyle = DashStyles.Dash;
              this.wpfPen.DashStyle = new System.Windows.Media.DashStyle(new double[] { 2, 2 }, 0);
              break;

            case XDashStyle.Dot:
              //this.wpfPen.DashStyle = DashStyles.Dot;
              this.wpfPen.DashStyle = new System.Windows.Media.DashStyle(new double[] { 0, 2 }, 1.5);
              break;

            case XDashStyle.DashDot:
              //this.wpfPen.DashStyle = DashStyles.DashDot;
              this.wpfPen.DashStyle = new System.Windows.Media.DashStyle(new double[] { 2, 2, 0, 2 }, 0);
              break;

            case XDashStyle.DashDotDot:
              //this.wpfPen.DashStyle = DashStyles.DashDotDot;
              this.wpfPen.DashStyle = new System.Windows.Media.DashStyle(new double[] { 2, 2, 0, 2, 0, 2 }, 0);
              break;
          }
        }
      }
      return this.wpfPen;
    }
#endif

    bool dirty = true;
    bool immutable;
#if GDI
    System.Drawing.Pen gdiPen;
#endif
#if WPF
    System.Windows.Media.Pen wpfPen;
#endif
  }
}