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

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Drawing.BarCodes/Code3of9Standard.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
//   Klaus Potzesny (mailto:Klaus.Potzesny@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
using System.Diagnostics;
 
32
 
 
33
namespace PdfSharp.Drawing.BarCodes
 
34
{
 
35
  /// <summary>
 
36
  /// 
 
37
  /// </summary>
 
38
  public class Code3of9Standard : ThickThinBarCode
 
39
  {
 
40
    /// <summary>
 
41
    /// Initializes a new instance of Standard3of9.
 
42
    /// </summary>
 
43
    public Code3of9Standard()
 
44
      : base("", XSize.Empty, CodeDirection.LeftToRight)
 
45
    {
 
46
    }
 
47
 
 
48
    /// <summary>
 
49
    /// Initializes a new instance of Standard3of9.
 
50
    /// </summary>
 
51
    public Code3of9Standard(string code)
 
52
      : base(code, XSize.Empty, CodeDirection.LeftToRight)
 
53
    {
 
54
    }
 
55
 
 
56
    /// <summary>
 
57
    /// Initializes a new instance of Standard3of9.
 
58
    /// </summary>
 
59
    public Code3of9Standard(string code, XSize size)
 
60
      : base(code, size, CodeDirection.LeftToRight)
 
61
    {
 
62
    }
 
63
 
 
64
    /// <summary>
 
65
    /// Initializes a new instance of Standard3of9.
 
66
    /// </summary>
 
67
    public Code3of9Standard(string code, XSize size, CodeDirection direction)
 
68
      : base(code, size, direction)
 
69
    {
 
70
    }
 
71
 
 
72
    /// <summary>
 
73
    /// Returns an array of size 9 that represents the thick (true) and thin (false) lines and spaces
 
74
    /// representing the specified digit.
 
75
    /// </summary>
 
76
    /// <param name="ch">The character to represent.</param>
 
77
    private static bool[] ThickThinLines(char ch)
 
78
    {
 
79
      return Lines["0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. $/+%*".IndexOf(ch)];
 
80
    }
 
81
    static bool[][] Lines = new bool[][]
 
82
    {
 
83
      // '0'
 
84
      new bool[] {false, false, false, true, true, false, true, false, false},
 
85
      // '1'
 
86
      new bool[] {true, false, false, true, false, false, false, false, true},
 
87
      // '2'
 
88
      new bool[] {false, false, true, true, false, false, false, false, true},
 
89
      // '3'
 
90
      new bool[] {true, false, true, true, false, false, false, false, false},
 
91
      // '4'
 
92
      new bool[] {false, false, false, true, true, false, false, false, true},
 
93
      // '5'
 
94
      new bool[] {true, false, false, true, true, false, false, false, false},
 
95
      // '6'
 
96
      new bool[] {false, false, true, true, true, false, false, false, false},
 
97
      // '7'
 
98
      new bool[] {false, false, false, true, false, false, true, false, true},
 
99
      // '8'
 
100
      new bool[] {true, false, false, true, false, false, true, false, false},
 
101
      // '9'
 
102
      new bool[] {false, false, true, true, false, false, true, false, false},
 
103
      // 'A'
 
104
      new bool[] {true, false, false, false, false, true, false, false, true},
 
105
      // 'B'
 
106
      new bool[] {false, false, true, false, false, true, false, false, true},
 
107
      // 'C'
 
108
      new bool[] {true, false, true, false, false, true, false, false, false},
 
109
      // 'D'
 
110
      new bool[] {false, false, false, false, true, true, false, false, true},
 
111
      // 'E'
 
112
      new bool[] {true, false, false, false, true, true, false, false, false},
 
113
      // 'F'
 
114
      new bool[] {false, false, true, false, true, true, false, false, false},
 
115
      // 'G'
 
116
      new bool[] {false, false, false, false, false, true, true, false, true},
 
117
      // 'H'
 
118
      new bool[] {true, false, false, false, false, true, true, false, false},
 
119
      // 'I'
 
120
      new bool[] {false, false, true, false, false, true, true, false, false},
 
121
      // 'J'
 
122
      new bool[] {false, false, false, false, true, true, true, false, false},
 
123
      // 'K'
 
124
      new bool[] {true, false, false, false, false, false, false, true, true},
 
125
      // 'L'
 
126
      new bool[] {false, false, true, false, false, false, false, true, true},
 
127
      // 'M'
 
128
      new bool[] {true, false, true, false, false, false, false, true, false},
 
129
      // 'N'
 
130
      new bool[] {false, false, false, false, true, false, false, true, true},
 
131
      // 'O'
 
132
      new bool[] {true, false, false, false, true, false, false, true, false},
 
133
      // 'P':
 
134
      new bool[] {false, false, true, false, true, false, false, true, false},
 
135
      // 'Q'
 
136
      new bool[] {false, false, false, false, false, false, true, true, true},
 
137
      // 'R'
 
138
      new bool[] {true, false, false, false, false, false, true, true, false},
 
139
      // 'S'
 
140
      new bool[] {false, false, true, false, false, false, true, true, false},
 
141
      // 'T'
 
142
      new bool[] {false, false, false, false, true, false, true, true, false},
 
143
      // 'U'
 
144
      new bool[] {true, true, false, false, false, false, false, false, true},
 
145
      // 'V'
 
146
      new bool[] {false, true, true, false, false, false, false, false, true},
 
147
      // 'W'
 
148
      new bool[] {true, true, true, false, false, false, false, false, false},
 
149
      // 'X'
 
150
      new bool[] {false, true, false, false, true, false, false, false, true},
 
151
      // 'Y'
 
152
      new bool[] {true, true, false, false, true, false, false, false, false},
 
153
      // 'Z'
 
154
      new bool[] {false, true, true, false, true, false, false, false, false},
 
155
      // '-'
 
156
      new bool[] {false, true, false, false, false, false, true, false, true},
 
157
      // '.'
 
158
      new bool[] {true, true, false, false, false, false, true, false, false},
 
159
      // ' '
 
160
      new bool[] {false, true, true, false, false, false, true, false, false},
 
161
      // '$'
 
162
      new bool[] {false, true, false, true, false, true, false, false, false},
 
163
      // '/'
 
164
      new bool[] {false, true, false, true, false, false, false, true, false},
 
165
      // '+'
 
166
      new bool[] {false, true, false, false, false, true, false, true, false},
 
167
      // '%'
 
168
      new bool[] {false, false, false, true, false, true, false, true, false},
 
169
      // '*'
 
170
      new bool[] {false, true, false, false, true, false, true, false, false},
 
171
   };
 
172
 
 
173
 
 
174
    /// <summary>
 
175
    /// Calculates the thick and thin line widths,
 
176
    /// taking into acount the required rendering size.
 
177
    /// </summary>
 
178
    internal override void CalcThinBarWidth(BarCodeRenderInfo info)
 
179
    {
 
180
      /*
 
181
       * The total width is the sum of the following parts:
 
182
       * Starting lines      = 3 * thick + 7 * thin
 
183
       *  +
 
184
       * Code Representation = (3 * thick + 7 * thin) * code.Length
 
185
       *  +
 
186
       * Stopping lines      =  3 * thick + 6 * thin
 
187
       * 
 
188
       * with r = relation ( = thick / thin), this results in
 
189
       * 
 
190
       * Total width = (13 + 6 * r + (3 * r + 7) * code.Length) * thin
 
191
       */
 
192
      double thinLineAmount = 13 + 6 * this.wideNarrowRatio + (3 * this.wideNarrowRatio + 7) * this.text.Length;
 
193
      info.ThinBarWidth = this.Size.Width / thinLineAmount;
 
194
    }
 
195
 
 
196
    /// <summary>
 
197
    /// Checks the code to be convertible into an standard 3 of 9 bar code.
 
198
    /// </summary>
 
199
    /// <param name="text">The code to be checked.</param>
 
200
    protected override void CheckCode(string text)
 
201
    {
 
202
      if (text == null)
 
203
        throw new ArgumentNullException("text");
 
204
 
 
205
      if (text.Length == 0)
 
206
        throw new ArgumentException(BcgSR.Invalid3Of9Code(text));
 
207
 
 
208
      foreach (char ch in text)
 
209
      {
 
210
        if ("0123456789ABCDEFGHIJKLMNOP'QRSTUVWXYZ-. $/+%*".IndexOf(ch) < 0)
 
211
          throw new ArgumentException(BcgSR.Invalid3Of9Code(text));
 
212
      }
 
213
    }
 
214
 
 
215
    /// <summary>
 
216
    /// Renders the bar code.
 
217
    /// </summary>
 
218
    protected internal override void Render(XGraphics gfx, XBrush brush, XFont font, XPoint position)
 
219
    {
 
220
      XGraphicsState state = gfx.Save();
 
221
 
 
222
      BarCodeRenderInfo info = new BarCodeRenderInfo(gfx, brush, font, position);
 
223
      InitRendering(info);
 
224
      info.CurrPosInString = 0;
 
225
      //info.CurrPos = info.Center - this.size / 2;
 
226
      info.CurrPos = position - CodeBase.CalcDistance(AnchorType.TopLeft, this.anchor, this.size);
 
227
 
 
228
      if (TurboBit)
 
229
        RenderTurboBit(info, true);
 
230
      RenderStart(info);
 
231
      while (info.CurrPosInString < this.text.Length)
 
232
      {
 
233
        RenderNextChar(info);
 
234
        RenderGap(info, false);
 
235
      }
 
236
      RenderStop(info);
 
237
      if (TurboBit)
 
238
        RenderTurboBit(info, false);
 
239
      if (TextLocation != TextLocation.None)
 
240
        RenderText(info);
 
241
 
 
242
      gfx.Restore(state);
 
243
    }
 
244
 
 
245
    private void RenderNextChar(BarCodeRenderInfo info)
 
246
    {
 
247
      RenderChar(info, this.text[info.CurrPosInString]);
 
248
      ++info.CurrPosInString;
 
249
    }
 
250
 
 
251
    private void RenderChar(BarCodeRenderInfo info, char ch)
 
252
    {
 
253
      bool[] thickThinLines = ThickThinLines(ch);
 
254
      int idx = 0;
 
255
      while (idx < 9)
 
256
      {
 
257
        RenderBar(info, thickThinLines[idx]);
 
258
        if (idx < 8)
 
259
          RenderGap(info, thickThinLines[idx + 1]);
 
260
        idx += 2;
 
261
      }
 
262
    }
 
263
 
 
264
    private void RenderStart(BarCodeRenderInfo info)
 
265
    {
 
266
      RenderChar(info, '*');
 
267
      RenderGap(info, false);
 
268
    }
 
269
 
 
270
    private void RenderStop(BarCodeRenderInfo info)
 
271
    {
 
272
      RenderChar(info, '*');
 
273
    }
 
274
  }
 
275
}