summaryrefslogtreecommitdiff
path: root/libjava/classpath/java/awt/font/NumericShaper.java
blob: add1c6a44ed42d521bb006b23bcf0a13aedefa01 (plain)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* NumericShaper.java
   Copyright (C) 2003 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package java.awt.font;

import java.io.Serializable;
import java.lang.Character.UnicodeBlock;

/**
 * This class handles numeric shaping.  A shaper can either be contextual
 * or not.  A non-contextual shaper will always translate ASCII digits
 * in its input into the target Unicode range.  A contextual shaper will
 * change the target Unicode range depending on the characters it has
 * previously processed.
 *
 * @author Michael Koch
 * @author Tom Tromey
 *
 * @since 1.4
 * @specnote This class does not handle LIMBU or OSMANYA.
 * @specnote The JDK does not seem to properly handle ranges without a
 * digit zero, such as TAMIL.  This implementation does.
 */
public final class NumericShaper implements Serializable
{
  private static final long serialVersionUID = -8022764705923730308L;

  /** Convenience constant representing all the valid Unicode ranges.  */
  public static final int ALL_RANGES  = 524287;

  /**
   * Constant representing the Unicode ARABIC range.  Shaping done
   * using this range will translate to the arabic decimal characters.
   * Use EASTERN_ARABIC if you want to shape to the eastern arabic
   * (also known as the extended arabic) decimal characters.
   */
  public static final int ARABIC  = 2;

  /** Constant representing the Unicode BENGALI range.  */
  public static final int BENGALI  = 16;

  /** Constant representing the Unicode DEVANAGARI range.  */
  public static final int DEVANAGARI  = 8;

  /**
   * Constant representing the Unicode extended arabic range.
   * In Unicode there are two different sets of arabic digits;
   * this selects the extended or eastern set.
   */
  public static final int EASTERN_ARABIC  = 4;

  /**
   * Constant representing the Unicode ETHIOPIC range.  Note that
   * there is no digit zero in this range; an ASCII digit zero
   * is left unchanged when shaping to this range.
   */
  public static final int ETHIOPIC  = 65536;

  /**
   * Constant representing the Unicode EUROPEAN range.  For
   * contextual shaping purposes, characters in the various
   * extended Latin character blocks are recognized as EUROPEAN.
   */
  public static final int EUROPEAN  = 1;

  /** Constant representing the Unicode GUJARATI range.  */
  public static final int GUJARATI  = 64;

  /** Constant representing the Unicode GURMUKHI range.  */
  public static final int GURMUKHI  = 32;

  /** Constant representing the Unicode KANNADA range.  */
  public static final int KANNADA  = 1024;

  /** Constant representing the Unicode KHMER range.  */
  public static final int KHMER  = 131072;

  /** Constant representing the Unicode LAO range.  */
  public static final int LAO  = 8192;

  /** Constant representing the Unicode MALAYALAM range.  */
  public static final int MALAYALAM  = 2048;

  /** Constant representing the Unicode MONGOLIAN range.  */
  public static final int MONGOLIAN  = 262144;

  /** Constant representing the Unicode MYANMAR range.  */
  public static final int MYANMAR  = 32768;

  /** Constant representing the Unicode ORIYA range.  */
  public static final int ORIYA  = 128;

  /**
   * Constant representing the Unicode TAMIL range.  Note that
   * there is no digit zero in this range; an ASCII digit zero
   * is left unchanged when shaping to this range.
   */
  public static final int TAMIL  = 256;

  /** Constant representing the Unicode TELUGU range.  */
  public static final int TELUGU  = 512;

  /** Constant representing the Unicode THAI range.  */
  public static final int THAI  = 4096;

  /** Constant representing the Unicode TIBETAN range.  */
  public static final int TIBETAN  = 16384;

  /**
   * This table holds the zero digits for each language.  This is hard-coded
   * because the values will not change and the table layout is tied to the
   * other constants in this class in any case.  In the two places where a
   * language does not have a zero digit, the character immediately preceeding
   * the one digit is used instead.  These languages are special-cased in
   * the shaping code.
   */
  private static final char[] zeroDigits =
  {
    '0',      // EUROPEAN
    '\u0660', // ARABIC
    '\u06f0', // EASTERN_ARABIC
    '\u0966', // DEVANAGARI
    '\u09e6', // BENGALI
    '\u0a66', // GURMUKHI
    '\u0ae6', // GUJARATI
    '\u0b66', // ORIYA
    '\u0be6', // TAMIL - special case as there is no digit zero
    '\u0c66', // TELUGU
    '\u0ce6', // KANNADA
    '\u0d66', // MALAYALAM
    '\u0e50', // THAI
    '\u0ed0', // LAO
    '\u0f20', // TIBETAN
    '\u1040', // MYANMAR
    '\u1368', // ETHIOPIC - special case as there is no digit zero
    '\u17e0', // KHMER
    '\u1810'  // MONGOLIAN
  };

  /**
   * The default initial context for this shaper, specified as
   * an integer from 0 to 18.
   */
  private int key;

  /**
   * The target ranges handled by this shaper.  If the shaper
   * is not contextual, the high bit of this field will be set.
   * @specnote This was discovered by reading the serialization spec
   */
  private int mask;

  /**
   * Create a new numeric shaper.  The key given is a constant from
   * this class, the constructor turns it into its internal form.
   * @param key the key to use, as one of the manifest constants
   * @param mask a mask of languages to shape for
   */
  private NumericShaper (int key, int mask)
  {
    // This internal form is a bit goofy, but it is specified by
    // the serialization spec.
    this.key = Integer.numberOfTrailingZeros(key);
    this.mask = mask;
  }

  /**
   * Return an integer representing all the languages for which this
   * shaper will shape.  The result is taken by "or"ing together
   * the constants representing the various languages.
   */
  public int getRanges ()
  {
    return mask & ALL_RANGES;
  }

  /**
   * Return true if this shaper is contextual, false if it is not.
   */
  public boolean isContextual ()
  {
    return mask > 0;
  }

  /**
   * Shape the text in the given array.  The starting context will
   * be the context passed to the shaper at creation time.
   * @param text the text to shape
   * @param start the index of the starting character of the array
   * @param count the number of characters in the array
   */
  public void shape (char[] text, int start, int count)
  {
    shape (text, start, count, 1 << key);
  }

  /**
   * Given a unicode block object, return corresponding language constant.
   * If the block is not recognized, returns zero.  Note that as there
   * is no separate ARABIC block in Character, this case must
   * be specially handled by the caller; EASTERN_ARABIC is preferred when
   * both are specified.
   * @param b the unicode block to classify
   * @return the language constant, or zero if not recognized
   */
  private int classify(UnicodeBlock b)
  {
    if (b == null)
      return 0;
    // ARABIC is handled by the caller; from testing we know
    // that EASTERN_ARABIC takes precedence.
    if (b == UnicodeBlock.ARABIC)
      return EASTERN_ARABIC;
    if (b == UnicodeBlock.BENGALI)
      return BENGALI;
    if (b == UnicodeBlock.DEVANAGARI)
      return DEVANAGARI;
    if (b == UnicodeBlock.ETHIOPIC)
      return ETHIOPIC;
    if (b == UnicodeBlock.BASIC_LATIN
        || b == UnicodeBlock.LATIN_1_SUPPLEMENT
        || b == UnicodeBlock.LATIN_EXTENDED_A
        || b == UnicodeBlock.LATIN_EXTENDED_ADDITIONAL
        || b == UnicodeBlock.LATIN_EXTENDED_B)
      return EUROPEAN;
    if (b == UnicodeBlock.GUJARATI)
      return GUJARATI;
    if (b == UnicodeBlock.GURMUKHI)
      return GURMUKHI;
    if (b == UnicodeBlock.KANNADA)
      return KANNADA;
    if (b == UnicodeBlock.KHMER)
      return KHMER;
    if (b == UnicodeBlock.LAO)
      return LAO;
    if (b == UnicodeBlock.MALAYALAM)
      return MALAYALAM;
    if (b == UnicodeBlock.MONGOLIAN)
      return MONGOLIAN;
    if (b == UnicodeBlock.MYANMAR)
      return MYANMAR;
    if (b == UnicodeBlock.ORIYA)
      return ORIYA;
    if (b == UnicodeBlock.TAMIL)
      return TAMIL;
    if (b == UnicodeBlock.TELUGU)
      return TELUGU;
    if (b == UnicodeBlock.THAI)
      return THAI;
    if (b == UnicodeBlock.TIBETAN)
      return TIBETAN;
    return 0;
  }

  /**
   * Shape the given text, using the indicated initial context.
   * If this shaper is not a contextual shaper, then the given context
   * will be ignored.
   * @param text the text to shape
   * @param start the index of the first character of the text to shape
   * @param count the number of characters to shape in the text
   * @param context the initial context
   * @throws IllegalArgumentException if the initial context is invalid
   */
  public void shape (char[] text, int start, int count, int context)
  {
    int currentContext;
    if (isContextual())
      {
        if (Integer.bitCount(context) != 1 || (context & ~ALL_RANGES) != 0)
          throw new IllegalArgumentException("invalid context argument");
        // If the indicated context is not one we are handling, reset it.
        if ((context & mask) == 0)
          currentContext = -1;
        else
          currentContext = Integer.numberOfTrailingZeros(context);
      }
    else
      currentContext = key;

    for (int i = 0; i < count; ++i)
      {
        char c = text[start + i];
        if (c >= '0' && c <= '9')
          {
            if (currentContext >= 0)
              {
                // Shape into the current context.
                if (c == '0'
                  && ((1 << currentContext) == TAMIL
                      || (1 << currentContext) == ETHIOPIC))
                  {
                    // No digit 0 in this context; do nothing.
                  }
                else
                  text[start + i]
                    = (char) (zeroDigits[currentContext] + c - '0');
              }
          }
        else if (isContextual())
          {
            // if c is in a group, set currentContext; else reset it.
            int group = classify(UnicodeBlock.of(c));
            // Specially handle ARABIC.
            if (group == EASTERN_ARABIC && (mask & EASTERN_ARABIC) == 0
                && (mask & ARABIC) != 0)
              group = ARABIC;
            if ((mask & group) != 0)
              {
                // The character was classified as being in a group
                // we recognize, and it was selected by the shaper.
                // So, change the context.
                currentContext = Integer.numberOfTrailingZeros(group);
              }
          }
      }
  }

  public boolean equals (Object obj)
  {
    if (! (obj instanceof NumericShaper))
      return false;
    NumericShaper tmp = (NumericShaper) obj;
    return key == tmp.key && mask == tmp.mask;
  }

  public int hashCode ()
  {
    return key ^ mask;
  }

  public String toString ()
  {
    // For debugging only.
    return "key=" + key + "; mask=" + mask;
  }

  /**
   * Return a non-contextual shaper which can shape to a single range.
   * All ASCII digits in the input text are translated to this language.
   * @param singleRange the target language
   * @return a non-contextual shaper for this language
   * @throws IllegalArgumentException if the argument does not name a
   * single language, as specified by the constants declared in this class
   */
  public static NumericShaper getShaper (int singleRange)
  {
    if (Integer.bitCount(singleRange) != 1)
      throw new IllegalArgumentException("more than one bit set in argument");
    if ((singleRange & ~ALL_RANGES) != 0)
      throw new IllegalArgumentException("argument out of range");
    return new NumericShaper(singleRange, Integer.MIN_VALUE | singleRange);
  }

  /**
   * Return a contextual shaper which can shape to any of the indicated
   * languages.  The default initial context for this shaper is EUROPEAN.
   * @param ranges the ranges to shape to
   * @return a contextual shaper which will target any of these ranges
   * @throws IllegalArgumentException if the argument specifies an
   * unrecognized range
   */
  public static NumericShaper getContextualShaper (int ranges)
  {
    if ((ranges & ~ALL_RANGES) != 0)
      throw new IllegalArgumentException("argument out of range");
    return new NumericShaper(EUROPEAN, ranges);
  }

  /**
   * Return a contextual shaper which can shape to any of the indicated
   * languages.  The default initial context for this shaper is given as
   * an argument.
   * @param ranges the ranges to shape to
   * @param defaultContext the default initial context
   * @return a contextual shaper which will target any of these ranges
   * @throws IllegalArgumentException if the ranges argument specifies an
   * unrecognized range, or if the defaultContext argument does not specify
   * a single valid range
   */
  public static NumericShaper getContextualShaper (int ranges,
                                                   int defaultContext)
  {
    if (Integer.bitCount(defaultContext) != 1)
      throw new IllegalArgumentException("more than one bit set in context");
    if ((ranges & ~ALL_RANGES) != 0 || (defaultContext & ~ALL_RANGES) != 0)
      throw new IllegalArgumentException("argument out of range");
    return new NumericShaper(defaultContext, ranges);
  }
}