~ubuntu-branches/ubuntu/raring/libfonts-java/raring

« back to all changes in this revision

Viewing changes to source/org/jfree/fonts/encoding/manual/Iso8859_1.java

  • Committer: Package Import Robot
  • Author(s): Rene Engelhard
  • Date: 2011-12-29 23:12:17 UTC
  • mfrom: (1.1.3) (2.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20111229231217-f0tkh1n7f86opmn8
Tags: 1.1.6.dfsg-3
add missing build-dep on ant-optional... (closes: #652799) 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * ===========================================
3
 
 * LibFonts : a free Java font reading library
4
 
 * ===========================================
5
 
 *
6
 
 * Project Info:  http://reporting.pentaho.org/libfonts/
7
 
 *
8
 
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
9
 
 *
10
 
 * This library is free software; you can redistribute it and/or modify it under the terms
11
 
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12
 
 * either version 2.1 of the License, or (at your option) any later version.
13
 
 *
14
 
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15
 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
 
 * See the GNU Lesser General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License along with this
19
 
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
 
 * Boston, MA 02111-1307, USA.
21
 
 *
22
 
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
23
 
 * in the United States and other countries.]
24
 
 *
25
 
 * ------------
26
 
 * $Id: Iso8859_1.java 3523 2007-10-16 11:03:09Z tmorgner $
27
 
 * ------------
28
 
 * (C) Copyright 2006-2007, by Pentaho Corporation.
29
 
 */
30
 
package org.jfree.fonts.encoding.manual;
31
 
 
32
 
import java.util.Locale;
33
 
 
34
 
import org.jfree.fonts.encoding.ByteBuffer;
35
 
import org.jfree.fonts.encoding.CodePointBuffer;
36
 
import org.jfree.fonts.encoding.Encoding;
37
 
import org.jfree.fonts.encoding.EncodingErrorType;
38
 
import org.jfree.fonts.encoding.EncodingException;
39
 
 
40
 
/**
41
 
 * This is a lucky case, as ISO-8859-1 can be transformed directly. There is no
42
 
 * lookup step needed.
43
 
 *
44
 
 * @author Thomas Morgner
45
 
 */
46
 
public final class Iso8859_1 implements Encoding
47
 
{
48
 
  public Iso8859_1()
49
 
  {
50
 
  }
51
 
 
52
 
  public String getName()
53
 
  {
54
 
    return "ISO-8859-1";
55
 
  }
56
 
 
57
 
  public String getName(final Locale locale)
58
 
  {
59
 
    return getName();
60
 
  }
61
 
 
62
 
  public boolean isUnicodeCharacterSupported(final int c)
63
 
  {
64
 
    return (c & 0xFFFFFF00) == 0;
65
 
  }
66
 
 
67
 
  /**
68
 
   * Encode, but ignore errors.
69
 
   *
70
 
   * @param text
71
 
   * @param buffer
72
 
   * @return
73
 
   */
74
 
  public ByteBuffer encode(final CodePointBuffer text, ByteBuffer buffer)
75
 
  {
76
 
    final int textLength = text.getLength();
77
 
    if (buffer == null)
78
 
    {
79
 
      buffer = new ByteBuffer(textLength);
80
 
    }
81
 
    else if (buffer.getLength() < textLength)
82
 
    {
83
 
      buffer.ensureSize(textLength);
84
 
    }
85
 
 
86
 
    final byte[] targetArray = buffer.getData();
87
 
    final int[] sourceArray = text.getData();
88
 
 
89
 
    int targetIdx = buffer.getOffset();
90
 
    final int endPos = text.getCursor();
91
 
    for (int i = text.getOffset(); i < endPos; i++)
92
 
    {
93
 
      final int sourceItem = sourceArray[i];
94
 
      if (isUnicodeCharacterSupported(sourceItem))
95
 
      {
96
 
        targetArray[targetIdx] = (byte) (sourceItem & 0xff);
97
 
        targetIdx += 1;
98
 
      }
99
 
    }
100
 
 
101
 
    buffer.setCursor(targetIdx);
102
 
    return buffer;
103
 
  }
104
 
 
105
 
  public CodePointBuffer decode(final ByteBuffer text, CodePointBuffer buffer)
106
 
  {
107
 
    final int textLength = text.getLength();
108
 
    if (buffer == null)
109
 
    {
110
 
      buffer = new CodePointBuffer(textLength);
111
 
    }
112
 
    else if (buffer.getLength() < textLength)
113
 
    {
114
 
      buffer.ensureSize(textLength);
115
 
    }
116
 
 
117
 
    final int[] targetArray = buffer.getData();
118
 
    final byte[] sourceArray = text.getData();
119
 
 
120
 
    int targetIdx = buffer.getOffset();
121
 
    final int endPos = text.getCursor();
122
 
    for (int i = text.getOffset(); i < endPos; i++)
123
 
    {
124
 
      targetArray[targetIdx] = (sourceArray[i] & 0xff);
125
 
      targetIdx += 1;
126
 
    }
127
 
 
128
 
    buffer.setCursor(targetIdx);
129
 
    return buffer;
130
 
  }
131
 
 
132
 
  public ByteBuffer encode(final CodePointBuffer text,
133
 
                           ByteBuffer buffer,
134
 
                           final EncodingErrorType errorHandling)
135
 
          throws EncodingException
136
 
  {
137
 
    final int textLength = text.getLength();
138
 
    if (buffer == null)
139
 
    {
140
 
      buffer = new ByteBuffer(textLength);
141
 
    }
142
 
    else if (buffer.getLength() < textLength)
143
 
    {
144
 
      buffer.ensureSize(textLength);
145
 
    }
146
 
 
147
 
    final byte[] targetArray = buffer.getData();
148
 
    final int[] sourceArray = text.getData();
149
 
 
150
 
    int targetIdx = buffer.getOffset();
151
 
    final int endPos = text.getCursor();
152
 
    for (int i = text.getOffset(); i < endPos; i++)
153
 
    {
154
 
      final int sourceItem = sourceArray[i];
155
 
      if (isUnicodeCharacterSupported(sourceItem))
156
 
      {
157
 
        targetArray[targetIdx] = (byte) (sourceItem & 0xff);
158
 
        targetIdx += 1;
159
 
      }
160
 
      else
161
 
      {
162
 
        if (errorHandling == EncodingErrorType.REPLACE)
163
 
        {
164
 
          targetArray[targetIdx] = (byte) ('?' & 0xff);
165
 
          targetIdx += 1;
166
 
        }
167
 
        else if (errorHandling == EncodingErrorType.FAIL)
168
 
        {
169
 
          throw new EncodingException();
170
 
        }
171
 
      }
172
 
    }
173
 
 
174
 
    buffer.setCursor(targetIdx);
175
 
    return buffer;
176
 
  }
177
 
 
178
 
  public CodePointBuffer decode(final ByteBuffer text,
179
 
                                CodePointBuffer buffer,
180
 
                                final EncodingErrorType errorHandling)
181
 
          throws EncodingException
182
 
  {
183
 
    final int textLength = text.getLength();
184
 
    if (buffer == null)
185
 
    {
186
 
      buffer = new CodePointBuffer(textLength);
187
 
    }
188
 
    else if (buffer.getLength() < textLength)
189
 
    {
190
 
      buffer.ensureSize(textLength);
191
 
    }
192
 
 
193
 
    final int[] targetArray = buffer.getData();
194
 
    final byte[] sourceArray = text.getData();
195
 
 
196
 
    int targetIdx = buffer.getOffset();
197
 
    final int endPos = text.getCursor();
198
 
    for (int i = text.getOffset(); i < endPos; i++)
199
 
    {
200
 
      targetArray[targetIdx] = (sourceArray[i] & 0xff);
201
 
      targetIdx += 1;
202
 
    }
203
 
 
204
 
    buffer.setCursor(targetIdx);
205
 
    return buffer;
206
 
  }
207
 
 
208
 
}