File: Code2of5Interleaved.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 (201 lines) | stat: -rw-r--r-- 6,812 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
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
//   Klaus Potzesny (mailto:Klaus.Potzesny@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 PdfSharp.Drawing;

namespace PdfSharp.Drawing.BarCodes
{
  /// <summary>
  /// Summary description for Interleave5of9.
  /// </summary>
  public class Code2of5Interleaved : ThickThinBarCode
  {
    /// <summary>
    /// Initializes a new instance of Interleaved2of5.
    /// </summary>
    public Code2of5Interleaved()
      : base("", XSize.Empty, CodeDirection.LeftToRight)
    {
    }

    /// <summary>
    /// Initializes a new instance of Interleaved2of5.
    /// </summary>
    public Code2of5Interleaved(string code)
      : base(code, XSize.Empty, CodeDirection.LeftToRight)
    {
    }

    /// <summary>
    /// Initializes a new instance of Interleaved2of5.
    /// </summary>
    public Code2of5Interleaved(string code, XSize size)
      : base(code, size, CodeDirection.LeftToRight)
    {
    }

    /// <summary>
    /// Initializes a new instance of Interleaved2of5.
    /// </summary>
    public Code2of5Interleaved(string code, XSize size, CodeDirection direction)
      : base(code, size, direction)
    {
    }

    /// <summary>
    /// Returns an array of size 5 that represents the thick (true) and thin (false) lines or spaces
    /// representing the specified digit.
    /// </summary>
    /// <param name="digit">The digit to represent.</param>
    static bool[] ThickAndThinLines(int digit)
    {
      return Lines[digit];
    }
    static bool[][] Lines = new bool[][]
    {
      new bool[] {false, false, true, true, false},
      new bool[] {true, false, false, false, true},
      new bool[] {false, true, false, false, true},
      new bool[] {true, true, false, false, false},
      new bool[] {false, false, true, false, true},
      new bool[] {true, false, true, false, false},
      new bool[] {false, true, true, false, false},
      new bool[] {false, false, false, true, true},
      new bool[] {true, false, false, true, false},
      new bool[] {false, true, false, true, false},
    };

    /// <summary>
    /// Renders the bar code.
    /// </summary>
    protected internal override void Render(XGraphics gfx, XBrush brush, XFont font, XPoint position)
    {
      XGraphicsState state = gfx.Save();

      BarCodeRenderInfo info = new BarCodeRenderInfo(gfx, brush, font, position);
      InitRendering(info);
      info.CurrPosInString = 0;
      //info.CurrPos = info.Center - this.size / 2;
      info.CurrPos = position - CodeBase.CalcDistance(AnchorType.TopLeft, this.anchor, this.size);

      if (TurboBit)
        RenderTurboBit(info, true);
      RenderStart(info);
      while (info.CurrPosInString < this.text.Length)
        RenderNextPair(info);
      RenderStop(info);
      if (TurboBit)
        RenderTurboBit(info, false);
      if (this.TextLocation != TextLocation.None)
        RenderText(info);

      gfx.Restore(state);
    }

    /// <summary>
    /// Calculates the thick and thin line widths,
    /// taking into acount the required rendering size.
    /// </summary>
    internal override void CalcThinBarWidth(BarCodeRenderInfo info)
    {
      /*
       * The total width is the sum of the following parts:
       * Starting lines      = 4 * thin
       *  +
       * Code Representation = (2 * thick + 3 * thin) * code.Length
       *  +
       * Stopping lines      =  1 * thick + 2 * thin
       * 
       * with r = relation ( = thick / thin), this results in
       * 
       * Total width = (6 + r + (2 * r + 3) * text.Length) * thin
       */
      double thinLineAmount = 6 + this.wideNarrowRatio + (2 * this.wideNarrowRatio + 3) * this.text.Length;
      info.ThinBarWidth = this.Size.Width / thinLineAmount;
    }

    private void RenderStart(BarCodeRenderInfo info)
    {
      RenderBar(info, false);
      RenderGap(info, false);
      RenderBar(info, false);
      RenderGap(info, false);
    }

    private void RenderStop(BarCodeRenderInfo info)
    {
      RenderBar(info, true);
      RenderGap(info, false);
      RenderBar(info, false);
    }

    /// <summary>
    /// Renders the next digit pair as bar code element.
    /// </summary>
    private void RenderNextPair(BarCodeRenderInfo info)
    {
      int digitForLines = int.Parse(this.text[info.CurrPosInString].ToString());
      int digitForGaps = int.Parse(this.text[info.CurrPosInString + 1].ToString());
      bool[] linesArray = Lines[digitForLines];
      bool[] gapsArray = Lines[digitForGaps];
      for (int idx = 0; idx < 5; ++idx)
      {
        RenderBar(info, linesArray[idx]);
        RenderGap(info, gapsArray[idx]);
      }
      info.CurrPosInString += 2;
    }

    /// <summary>
    /// Checks the code to be convertible into an interleaved 2 of 5 bar code.
    /// </summary>
    /// <param name="text">The code to be checked.</param>
    protected override void CheckCode(string text)
    {
#if true_
      if (text == null)
        throw new ArgumentNullException("text");

      if (text == "")
        throw new ArgumentException(BcgSR.Invalid2Of5Code(text));

      if (text.Length % 2 != 0)
        throw new ArgumentException(BcgSR.Invalid2Of5Code(text));

      foreach (char ch in text)
      {
        if (!Char.IsDigit(ch))
          throw new ArgumentException(BcgSR.Invalid2Of5Code(text));
      }
#endif
    }
  }
}