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

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf.Filters/Filter.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
using System.Diagnostics;
 
32
using PdfSharp.Internal;
 
33
using PdfSharp.Pdf.IO;
 
34
using PdfSharp.Pdf.Internal;
 
35
 
 
36
namespace PdfSharp.Pdf.Filters
 
37
{
 
38
  /// <summary>
 
39
  /// Reserved for future extension.
 
40
  /// </summary>
 
41
  public class FilterParms
 
42
  {
 
43
    // not yet used
 
44
  }
 
45
 
 
46
  /// <summary>
 
47
  /// Base class for all stream filters
 
48
  /// </summary>
 
49
  public abstract class Filter
 
50
  {
 
51
    /// <summary>
 
52
    /// When implemented in a derived class encodes the specified data.
 
53
    /// </summary>
 
54
    public abstract byte[] Encode(byte[] data);
 
55
 
 
56
    /// <summary>
 
57
    /// Encodes a raw string.
 
58
    /// </summary>
 
59
    public virtual byte[] Encode(string rawString)
 
60
    {
 
61
      byte[] bytes = PdfEncoders.RawEncoding.GetBytes(rawString);
 
62
      bytes = Encode(bytes);
 
63
      return bytes;
 
64
    }
 
65
 
 
66
    /// <summary>
 
67
    /// When implemented in a derived class decodes the specified data.
 
68
    /// </summary>
 
69
    public abstract byte[] Decode(byte[] data, FilterParms parms);
 
70
 
 
71
    /// <summary>
 
72
    /// Decodes the specified data.
 
73
    /// </summary>
 
74
    public byte[] Decode(byte[] data)
 
75
    {
 
76
      return Decode(data, null);
 
77
    }
 
78
 
 
79
    /// <summary>
 
80
    /// Decodes to a raw string.
 
81
    /// </summary>
 
82
    public virtual string DecodeToString(byte[] data, FilterParms parms)
 
83
    {
 
84
      byte[] bytes = Decode(data, parms);
 
85
      string text = PdfEncoders.RawEncoding.GetString(bytes);
 
86
      return text;
 
87
    }
 
88
 
 
89
    /// <summary>
 
90
    /// Decodes to a raw string.
 
91
    /// </summary>
 
92
    public string DecodeToString(byte[] data)
 
93
    {
 
94
      return DecodeToString(data, null);
 
95
    }
 
96
 
 
97
    /// <summary>
 
98
    /// Removes all white spaces from the data. The function assumes that the bytes are characters.
 
99
    /// </summary>
 
100
    protected byte[] RemoveWhiteSpace(byte[] data)
 
101
    {
 
102
      int count = data.Length;
 
103
      int j = 0;
 
104
      for (int i = 0; i < count; i++, j++)
 
105
      {
 
106
        switch (data[i])
 
107
        {
 
108
          case (byte)Chars.NUL:  // 0 Null
 
109
          case (byte)Chars.HT:   // 9 Tab
 
110
          case (byte)Chars.LF:   // 10 Line feed
 
111
          case (byte)Chars.FF:   // 12 Form feed
 
112
          case (byte)Chars.CR:   // 13 Carriage return
 
113
          case (byte)Chars.SP:   // 32 Space
 
114
            j--;
 
115
            break;
 
116
 
 
117
          default:
 
118
            if (i != j)
 
119
              data[j] = data[i];
 
120
            break;
 
121
        }
 
122
        if (j < count)
 
123
        {
 
124
          byte[] temp = data;
 
125
          data = new byte[j];
 
126
          for (int idx = 0; idx < j; idx++)
 
127
            data[idx] = temp[idx];
 
128
        }
 
129
      }
 
130
      return data;
 
131
    }
 
132
  }
 
133
}