001: /*
002: * Copyright (c) 2007, intarsys consulting GmbH
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * - Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * - Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * - Neither the name of intarsys nor the names of its contributors may be used
015: * to endorse or promote products derived from this software without specific
016: * prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
022: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
024: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
025: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
026: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
027: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
028: * POSSIBILITY OF SUCH DAMAGE.
029: */
030: package de.intarsys.pdf.encoding;
031:
032: import java.io.ByteArrayInputStream;
033: import java.io.ByteArrayOutputStream;
034: import java.io.CharArrayReader;
035: import java.io.IOException;
036: import java.io.InputStream;
037: import java.io.OutputStream;
038: import java.io.Reader;
039: import java.io.StringReader;
040: import java.io.StringWriter;
041: import java.io.Writer;
042: import de.intarsys.pdf.cos.COSName;
043: import de.intarsys.pdf.cos.COSObject;
044: import de.intarsys.tools.stream.StreamTools;
045:
046: /**
047: * An Encoding defines the mapping from a one byte codepoint to a glyph name in
048: * the font.
049: *
050: * <p>
051: * Every font has a built in encoding that can be changed by adding an explicit
052: * encoding to a PDFont. This explicit change may be in the form of a
053: * "differences" encoding in an explicit dictionary or by means of a named
054: * encoding, one of "MacRomanEncoding" or "WinAnsiEncoding".
055: * </p>
056: *
057: * <p>
058: * todo 2 "MacExpertEncoding" is not supported.
059: * </p>
060: *
061: * <p>
062: * If no encoding is found in the font implementation or the font dictionary,
063: * "StandardEncoding" is used.
064: * </p>
065: */
066: public abstract class Encoding {
067: public static final COSName CN_MacRomanEncoding = COSName
068: .constant("MacRomanEncoding"); //$NON-NLS-1$
069:
070: public static final COSName CN_StandardEncoding = COSName
071: .constant("StandardEncoding"); //$NON-NLS-1$
072:
073: public static final COSName CN_WinAnsiEncoding = COSName
074: .constant("WinAnsiEncoding"); //$NON-NLS-1$
075:
076: public static final String NAME_a = "a"; //$NON-NLS-1$
077:
078: public static final String NAME_A = "A"; //$NON-NLS-1$
079:
080: public static final String NAME_aacute = "aacute"; //$NON-NLS-1$
081:
082: public static final String NAME_Aacute = "Aacute"; //$NON-NLS-1$
083:
084: public static final String NAME_acircumflex = "acircumflex"; //$NON-NLS-1$
085:
086: public static final String NAME_Acircumflex = "Acircumflex"; //$NON-NLS-1$
087:
088: public static final String NAME_acute = "acute"; //$NON-NLS-1$
089:
090: public static final String NAME_adieresis = "adieresis"; //$NON-NLS-1$
091:
092: public static final String NAME_Adieresis = "Adieresis"; //$NON-NLS-1$
093:
094: public static final String NAME_ae = "ae"; //$NON-NLS-1$
095:
096: public static final String NAME_AE = "AE"; //$NON-NLS-1$
097:
098: public static final String NAME_agrave = "agrave"; //$NON-NLS-1$
099:
100: public static final String NAME_Agrave = "Agrave"; //$NON-NLS-1$
101:
102: public static final String NAME_ampersand = "ampersand"; //$NON-NLS-1$
103:
104: public static final String NAME_aring = "aring"; //$NON-NLS-1$
105:
106: public static final String NAME_Aring = "Aring"; //$NON-NLS-1$
107:
108: public static final String NAME_asciicircum = "asciicircum"; //$NON-NLS-1$
109:
110: public static final String NAME_asciitilde = "asciitilde"; //$NON-NLS-1$
111:
112: public static final String NAME_asterisk = "asterisk"; //$NON-NLS-1$
113:
114: public static final String NAME_at = "at"; //$NON-NLS-1$
115:
116: public static final String NAME_atilde = "atilde"; //$NON-NLS-1$
117:
118: public static final String NAME_Atilde = "Atilde"; //$NON-NLS-1$
119:
120: public static final String NAME_b = "b"; //$NON-NLS-1$
121:
122: public static final String NAME_B = "B"; //$NON-NLS-1$
123:
124: public static final String NAME_backslash = "backslash"; //$NON-NLS-1$
125:
126: public static final String NAME_bar = "bar"; //$NON-NLS-1$
127:
128: public static final String NAME_braceleft = "braceleft"; //$NON-NLS-1$
129:
130: public static final String NAME_braceright = "braceright"; //$NON-NLS-1$
131:
132: public static final String NAME_bracketleft = "bracketleft"; //$NON-NLS-1$
133:
134: public static final String NAME_bracketright = "bracketright"; //$NON-NLS-1$
135:
136: public static final String NAME_breve = "breve"; //$NON-NLS-1$
137:
138: public static final String NAME_brokenbar = "brokenbar"; //$NON-NLS-1$
139:
140: public static final String NAME_bullet = "bullet"; //$NON-NLS-1$
141:
142: public static final String NAME_c = "c"; //$NON-NLS-1$
143:
144: public static final String NAME_C = "C"; //$NON-NLS-1$
145:
146: public static final String NAME_caron = "caron"; //$NON-NLS-1$
147:
148: public static final String NAME_ccedilla = "ccedilla"; //$NON-NLS-1$
149:
150: public static final String NAME_Ccedilla = "Ccedilla"; //$NON-NLS-1$
151:
152: public static final String NAME_cedilla = "cedilla"; //$NON-NLS-1$
153:
154: public static final String NAME_cent = "cent"; //$NON-NLS-1$
155:
156: public static final String NAME_circumflex = "circumflex"; //$NON-NLS-1$
157:
158: public static final String NAME_colon = "colon"; //$NON-NLS-1$
159:
160: public static final String NAME_comma = "comma"; //$NON-NLS-1$
161:
162: public static final String NAME_copyright = "copyright"; //$NON-NLS-1$
163:
164: public static final String NAME_currency = "currency"; //$NON-NLS-1$
165:
166: public static final String NAME_d = "d"; //$NON-NLS-1$
167:
168: public static final String NAME_D = "D"; //$NON-NLS-1$
169:
170: public static final String NAME_dagger = "dagger"; //$NON-NLS-1$
171:
172: public static final String NAME_daggerdbl = "daggerdbl"; //$NON-NLS-1$
173:
174: public static final String NAME_degree = "degree"; //$NON-NLS-1$
175:
176: public static final String NAME_dieresis = "dieresis"; //$NON-NLS-1$
177:
178: public static final String NAME_divide = "divide"; //$NON-NLS-1$
179:
180: public static final String NAME_dollar = "dollar"; //$NON-NLS-1$
181:
182: public static final String NAME_dotaccent = "dotaccent"; //$NON-NLS-1$
183:
184: public static final String NAME_dotlessi = "dotlessi"; //$NON-NLS-1$
185:
186: public static final String NAME_e = "e"; //$NON-NLS-1$
187:
188: public static final String NAME_E = "E"; //$NON-NLS-1$
189:
190: public static final String NAME_eacute = "eacute"; //$NON-NLS-1$
191:
192: public static final String NAME_Eacute = "Eacute"; //$NON-NLS-1$
193:
194: public static final String NAME_ecircumflex = "ecircumflex"; //$NON-NLS-1$
195:
196: public static final String NAME_Ecircumflex = "Ecircumflex"; //$NON-NLS-1$
197:
198: public static final String NAME_edieresis = "edieresis"; //$NON-NLS-1$
199:
200: public static final String NAME_Edieresis = "Edieresis"; //$NON-NLS-1$
201:
202: public static final String NAME_egrave = "egrave"; //$NON-NLS-1$
203:
204: public static final String NAME_Egrave = "Egrave"; //$NON-NLS-1$
205:
206: public static final String NAME_eight = "eight"; //$NON-NLS-1$
207:
208: public static final String NAME_ellipsis = "ellipsis"; //$NON-NLS-1$
209:
210: public static final String NAME_emdash = "emdash"; //$NON-NLS-1$
211:
212: public static final String NAME_endash = "endash"; //$NON-NLS-1$
213:
214: public static final String NAME_equal = "equal"; //$NON-NLS-1$
215:
216: public static final String NAME_eth = "eth"; //$NON-NLS-1$
217:
218: public static final String NAME_Eth = "Eth"; //$NON-NLS-1$
219:
220: public static final String NAME_Euro = "Euro"; //$NON-NLS-1$
221:
222: public static final String NAME_exclam = "exclam"; //$NON-NLS-1$
223:
224: public static final String NAME_exclamdown = "exclamdown"; //$NON-NLS-1$
225:
226: public static final String NAME_f = "f"; //$NON-NLS-1$
227:
228: public static final String NAME_F = "F"; //$NON-NLS-1$
229:
230: public static final String NAME_fi = "fi"; //$NON-NLS-1$
231:
232: public static final String NAME_five = "five"; //$NON-NLS-1$
233:
234: public static final String NAME_fl = "fl"; //$NON-NLS-1$
235:
236: public static final String NAME_florin = "florin"; //$NON-NLS-1$
237:
238: public static final String NAME_four = "four"; //$NON-NLS-1$
239:
240: public static final String NAME_fraction = "fraction"; //$NON-NLS-1$
241:
242: public static final String NAME_g = "g"; //$NON-NLS-1$
243:
244: public static final String NAME_G = "G"; //$NON-NLS-1$
245:
246: public static final String NAME_germandbls = "germandbls"; //$NON-NLS-1$
247:
248: public static final String NAME_grave = "grave"; //$NON-NLS-1$
249:
250: public static final String NAME_greater = "greater"; //$NON-NLS-1$
251:
252: public static final String NAME_guillemotleft = "guillemotleft"; //$NON-NLS-1$
253:
254: public static final String NAME_guillemotright = "guillemotright"; //$NON-NLS-1$
255:
256: public static final String NAME_guilsinglleft = "guilsinglleft"; //$NON-NLS-1$
257:
258: public static final String NAME_guilsinglright = "guilsinglright"; //$NON-NLS-1$
259:
260: public static final String NAME_h = "h"; //$NON-NLS-1$
261:
262: public static final String NAME_H = "H"; //$NON-NLS-1$
263:
264: public static final String NAME_hungarumlaut = "hungarumlaut"; //$NON-NLS-1$
265:
266: public static final String NAME_hyphen = "hyphen"; //$NON-NLS-1$
267:
268: public static final String NAME_i = "i"; //$NON-NLS-1$
269:
270: public static final String NAME_I = "I"; //$NON-NLS-1$
271:
272: public static final String NAME_iacute = "iacute"; //$NON-NLS-1$
273:
274: public static final String NAME_Iacute = "Iacute"; //$NON-NLS-1$
275:
276: public static final String NAME_icircumflex = "icircumflex"; //$NON-NLS-1$
277:
278: public static final String NAME_Icircumflex = "Icircumflex"; //$NON-NLS-1$
279:
280: public static final String NAME_idieresis = "idieresis"; //$NON-NLS-1$
281:
282: public static final String NAME_Idieresis = "Idieresis"; //$NON-NLS-1$
283:
284: public static final String NAME_igrave = "igrave"; //$NON-NLS-1$
285:
286: public static final String NAME_Igrave = "Igrave"; //$NON-NLS-1$
287:
288: public static final String NAME_j = "j"; //$NON-NLS-1$
289:
290: public static final String NAME_J = "J"; //$NON-NLS-1$
291:
292: public static final String NAME_k = "k"; //$NON-NLS-1$
293:
294: public static final String NAME_K = "K"; //$NON-NLS-1$
295:
296: public static final String NAME_l = "l"; //$NON-NLS-1$
297:
298: public static final String NAME_L = "L"; //$NON-NLS-1$
299:
300: public static final String NAME_less = "less"; //$NON-NLS-1$
301:
302: public static final String NAME_logicalnot = "logicalnot"; //$NON-NLS-1$
303:
304: public static final String NAME_lslash = "lslash"; //$NON-NLS-1$
305:
306: public static final String NAME_Lslash = "Lslash"; //$NON-NLS-1$
307:
308: public static final String NAME_m = "m"; //$NON-NLS-1$
309:
310: public static final String NAME_M = "M"; //$NON-NLS-1$
311:
312: public static final String NAME_macron = "macron"; //$NON-NLS-1$
313:
314: public static final String NAME_minus = "minus"; //$NON-NLS-1$
315:
316: public static final String NAME_mu = "mu"; //$NON-NLS-1$
317:
318: public static final String NAME_multiply = "multiply"; //$NON-NLS-1$
319:
320: public static final String NAME_n = "n"; //$NON-NLS-1$
321:
322: public static final String NAME_N = "N"; //$NON-NLS-1$
323:
324: public static final String NAME_nine = "nine"; //$NON-NLS-1$
325:
326: public static final String NAME_ntilde = "ntilde"; //$NON-NLS-1$
327:
328: public static final String NAME_Ntilde = "Ntilde"; //$NON-NLS-1$
329:
330: public static final String NAME_numbersign = "numbersign"; //$NON-NLS-1$
331:
332: public static final String NAME_o = "o"; //$NON-NLS-1$
333:
334: public static final String NAME_O = "O"; //$NON-NLS-1$
335:
336: public static final String NAME_oacute = "oacute"; //$NON-NLS-1$
337:
338: public static final String NAME_Oacute = "Oacute"; //$NON-NLS-1$
339:
340: public static final String NAME_ocircumflex = "ocircumflex"; //$NON-NLS-1$
341:
342: public static final String NAME_Ocircumflex = "Ocircumflex"; //$NON-NLS-1$
343:
344: public static final String NAME_odieresis = "odieresis"; //$NON-NLS-1$
345:
346: public static final String NAME_Odieresis = "Odieresis"; //$NON-NLS-1$
347:
348: public static final String NAME_oe = "oe"; //$NON-NLS-1$
349:
350: public static final String NAME_OE = "OE"; //$NON-NLS-1$
351:
352: public static final String NAME_ogonek = "ogonek"; //$NON-NLS-1$
353:
354: public static final String NAME_ograve = "ograve"; //$NON-NLS-1$
355:
356: public static final String NAME_Ograve = "Ograve"; //$NON-NLS-1$
357:
358: public static final String NAME_one = "one"; //$NON-NLS-1$
359:
360: public static final String NAME_onehalf = "onehalf"; //$NON-NLS-1$
361:
362: public static final String NAME_onequarter = "onequarter"; //$NON-NLS-1$
363:
364: public static final String NAME_onesuper ior = "onesuperior"; //$NON-NLS-1$
365:
366: public static final String NAME_ordfeminine = "ordfeminine"; //$NON-NLS-1$
367:
368: public static final String NAME_ordmasculine = "ordmasculine"; //$NON-NLS-1$
369:
370: public static final String NAME_oslash = "oslash"; //$NON-NLS-1$
371:
372: public static final String NAME_Oslash = "Oslash"; //$NON-NLS-1$
373:
374: public static final String NAME_otilde = "otilde"; //$NON-NLS-1$
375:
376: public static final String NAME_Otilde = "Otilde"; //$NON-NLS-1$
377:
378: public static final String NAME_p = "p"; //$NON-NLS-1$
379:
380: public static final String NAME_P = "P"; //$NON-NLS-1$
381:
382: public static final String NAME_paragraph = "paragraph"; //$NON-NLS-1$
383:
384: public static final String NAME_parenleft = "parenleft"; //$NON-NLS-1$
385:
386: public static final String NAME_parenright = "parenright"; //$NON-NLS-1$
387:
388: public static final String NAME_percent = "percent"; //$NON-NLS-1$
389:
390: public static final String NAME_period = "period"; //$NON-NLS-1$
391:
392: public static final String NAME_periodcentered = "periodcentered"; //$NON-NLS-1$
393:
394: public static final String NAME_perthousand = "perthousand"; //$NON-NLS-1$
395:
396: public static final String NAME_plus = "plus"; //$NON-NLS-1$
397:
398: public static final String NAME_plusminus = "plusminus"; //$NON-NLS-1$
399:
400: public static final String NAME_q = "q"; //$NON-NLS-1$
401:
402: public static final String NAME_Q = "Q"; //$NON-NLS-1$
403:
404: public static final String NAME_question = "question"; //$NON-NLS-1$
405:
406: public static final String NAME_questiondown = "questiondown"; //$NON-NLS-1$
407:
408: public static final String NAME_quotedbl = "quotedbl"; //$NON-NLS-1$
409:
410: public static final String NAME_quotedblbase = "quotedblbase"; //$NON-NLS-1$
411:
412: public static final String NAME_quotedblleft = "quotedblleft"; //$NON-NLS-1$
413:
414: public static final String NAME_quotedblright = "quotedblright"; //$NON-NLS-1$
415:
416: public static final String NAME_quoteleft = "quoteleft"; //$NON-NLS-1$
417:
418: public static final String NAME_quoteright = "quoteright"; //$NON-NLS-1$
419:
420: public static final String NAME_quotesinglbase = "quotesinglbase"; //$NON-NLS-1$
421:
422: public static final String NAME_quotesingle = "quotesingle"; //$NON-NLS-1$
423:
424: public static final String NAME_r = "r"; //$NON-NLS-1$
425:
426: public static final String NAME_R = "R"; //$NON-NLS-1$
427:
428: public static final String NAME_registered = "registered"; //$NON-NLS-1$
429:
430: public static final String NAME_ring = "ring"; //$NON-NLS-1$
431:
432: public static final String NAME_s = "s"; //$NON-NLS-1$
433:
434: public static final String NAME_S = "S"; //$NON-NLS-1$
435:
436: public static final String NAME_scaron = "scaron"; //$NON-NLS-1$
437:
438: public static final String NAME_Scaron = "Scaron"; //$NON-NLS-1$
439:
440: public static final String NAME_section = "section"; //$NON-NLS-1$
441:
442: public static final String NAME_semicolon = "semicolon"; //$NON-NLS-1$
443:
444: public static final String NAME_seven = "seven"; //$NON-NLS-1$
445:
446: public static final String NAME_six = "six"; //$NON-NLS-1$
447:
448: public static final String NAME_slash = "slash"; //$NON-NLS-1$
449:
450: public static final String NAME_space = "space"; //$NON-NLS-1$
451:
452: public static final String NAME_sterling = "sterling"; //$NON-NLS-1$
453:
454: public static final String NAME_t = "t"; //$NON-NLS-1$
455:
456: public static final String NAME_T = "T"; //$NON-NLS-1$
457:
458: public static final String NAME_thorn = "thorn"; //$NON-NLS-1$
459:
460: public static final String NAME_Thorn = "Thorn"; //$NON-NLS-1$
461:
462: public static final String NAME_three = "three"; //$NON-NLS-1$
463:
464: public static final String NAME_threequarters = "threequarters"; //$NON-NLS-1$
465:
466: public static final String NAME_threesuper ior = "threesuperior"; //$NON-NLS-1$
467:
468: public static final String NAME_tilde = "tilde"; //$NON-NLS-1$
469:
470: public static final String NAME_trademark = "trademark"; //$NON-NLS-1$
471:
472: public static final String NAME_two = "two"; //$NON-NLS-1$
473:
474: public static final String NAME_twosuper ior = "twosuperior"; //$NON-NLS-1$
475:
476: public static final String NAME_u = "u"; //$NON-NLS-1$
477:
478: public static final String NAME_U = "U"; //$NON-NLS-1$
479:
480: public static final String NAME_uacute = "uacute"; //$NON-NLS-1$
481:
482: public static final String NAME_Uacute = "Uacute"; //$NON-NLS-1$
483:
484: public static final String NAME_ucircumflex = "ucircumflex"; //$NON-NLS-1$
485:
486: public static final String NAME_Ucircumflex = "Ucircumflex"; //$NON-NLS-1$
487:
488: public static final String NAME_udieresis = "udieresis"; //$NON-NLS-1$
489:
490: public static final String NAME_Udieresis = "Udieresis"; //$NON-NLS-1$
491:
492: public static final String NAME_ugrave = "ugrave"; //$NON-NLS-1$
493:
494: public static final String NAME_Ugrave = "Ugrave"; //$NON-NLS-1$
495:
496: public static final String NAME_underscore = "underscore"; //$NON-NLS-1$
497:
498: public static final String NAME_v = "v"; //$NON-NLS-1$
499:
500: public static final String NAME_V = "V"; //$NON-NLS-1$
501:
502: public static final String NAME_w = "w"; //$NON-NLS-1$
503:
504: public static final String NAME_W = "W"; //$NON-NLS-1$
505:
506: public static final String NAME_x = "x"; //$NON-NLS-1$
507:
508: public static final String NAME_X = "X"; //$NON-NLS-1$
509:
510: public static final String NAME_y = "y"; //$NON-NLS-1$
511:
512: public static final String NAME_Y = "Y"; //$NON-NLS-1$
513:
514: public static final String NAME_yacute = "yacute"; //$NON-NLS-1$
515:
516: public static final String NAME_Yacute = "Yacute"; //$NON-NLS-1$
517:
518: public static final String NAME_ydieresis = "ydieresis"; //$NON-NLS-1$
519:
520: public static final String NAME_Ydieresis = "Ydieresis"; //$NON-NLS-1$
521:
522: public static final String NAME_yen = "yen"; //$NON-NLS-1$
523:
524: public static final String NAME_z = "z"; //$NON-NLS-1$
525:
526: public static final String NAME_Z = "Z"; //$NON-NLS-1$
527:
528: public static final String NAME_zcaron = "zcaron"; //$NON-NLS-1$
529:
530: public static final String NAME_Zcaron = "Zcaron"; //$NON-NLS-1$
531:
532: public static final String NAME_zero = "zero"; //$NON-NLS-1$
533:
534: /**
535: * "Create" one of the well known encodings.
536: *
537: * @param name
538: * The name of the encoding to create.
539: *
540: * @return The encoding implementation.
541: *
542: * @throws IllegalArgumentException
543: * When the encoding is not supported.
544: */
545: public static Encoding create(COSName name) {
546: if (CN_WinAnsiEncoding.equals(name)) {
547: return WinAnsiEncoding.UNIQUE;
548: }
549: if (CN_MacRomanEncoding.equals(name)) {
550: return MacRomanEncoding.UNIQUE;
551: }
552: if (CN_StandardEncoding.equals(name)) {
553: return StandardEncoding.UNIQUE;
554: }
555:
556: // todo 1 cmap Encoding may contain CMap!!
557: // throw new IllegalArgumentException("encoding not supported");
558: return new CMapEncoding(name);
559: }
560:
561: /**
562: * Return the standard encoding for the PDF specification.
563: *
564: * @return Return the standard encoding for the PDF specification.
565: */
566: public static Encoding getStandard() {
567: return StandardEncoding.UNIQUE;
568: }
569:
570: /**
571: * Create a new Encoding
572: */
573: public Encoding() {
574: super ();
575: }
576:
577: /**
578: * Create a reader on the InputStream <code>is</code> that uses the
579: * encoding defined in the receiver.
580: *
581: * @param is
582: * The input stream to read.
583: *
584: * @return Create a reader on the InputStream <code>is</code> that uses
585: * the encoding defined in the receiver.
586: */
587: public Reader createReader(InputStream is) {
588: return new MappedReader(is, this );
589: }
590:
591: /**
592: * Create a writer on the OutputStream <code>os</code> that uses the
593: * encoding defined in the receiver.
594: *
595: * @param os
596: * The output stream to be written.
597: *
598: * @return Create a writer on the OutputStream <code>os</code> that uses
599: * the encoding defined in the receiver.
600: */
601: public Writer createWriter(OutputStream os) {
602: return new MappedWriter(os, this );
603: }
604:
605: public String decode(byte[] bytes) {
606: InputStream is = new ByteArrayInputStream(bytes);
607: Reader r = createReader(is);
608: StringWriter w = new StringWriter();
609: try {
610: StreamTools.copyEncoded(r, w);
611: } catch (IOException e) {
612: // this can not happen
613: }
614: return w.toString();
615: }
616:
617: public byte[] encode(char[] value) {
618: ByteArrayOutputStream bos = new ByteArrayOutputStream();
619: Writer w = createWriter(bos);
620: Reader r = new CharArrayReader(value);
621: try {
622: StreamTools.copyEncoded(r, w);
623: } catch (IOException e) {
624: // this does not happen
625: }
626: return bos.toByteArray();
627: }
628:
629: public byte[] encode(String value) {
630: ByteArrayOutputStream bos = new ByteArrayOutputStream();
631: Writer w = createWriter(bos);
632: Reader r = new StringReader(value);
633: try {
634: StreamTools.copyEncoded(r, w);
635: } catch (IOException e) {
636: // this does not happen
637: }
638: return bos.toByteArray();
639: }
640:
641: /**
642: * Get the byte code point where the character referenced by the Unicode
643: * code point <code>unicode</code> is mapped in this font encoding or -1
644: * if invalid.
645: *
646: * <p>
647: * <code>unicode</code> must be a valid Unicode code point. The returned
648: * code point is a value from 0...255. If the Unicode code point is not
649: * valid or not mapped, -1 is returned.
650: * </p>
651: *
652: * @param unicode
653: * The Unicode code point to look up.
654: *
655: * @return The index from 0..255 where this character is mapped or -1.
656: */
657: public abstract int getByteCode(int unicode);
658:
659: /**
660: * Get the code point where the character referenced by the Adobe glyph name
661: * <code>name</code> is mapped in this font encoding or -1 if not valid.
662: *
663: * <p>
664: * <code>name</code> must be a valid Adobe glyph name. The code point is a
665: * value from 0...255. If the glyph is not found, -1 is returned.
666: * </p>
667: *
668: * @param name
669: * The glyph name.
670: *
671: * @return The index from 0..255 where this character is mapped or -1.
672: */
673: public abstract int getByteCode(String name);
674:
675: /**
676: * Get an object that can be used as a representation of the receiver
677: * encoding within <code>doc</code>.
678: *
679: * @return Get an object that can be used as a representation of the
680: * receiver encoding within <code>doc</code>.
681: */
682: public abstract COSObject getCosObject();
683:
684: /**
685: * Get the character name for a given encoded byte code point. If no mapping
686: * is defined, return ".notdef".
687: *
688: * @param codePoint
689: * The encoded byte code point.
690: *
691: * @return The glyph name of the character referenced by
692: * <code>codePoint</code> or ".notdef".
693: */
694: public abstract String getGlyphName(int codePoint);
695:
696: public abstract String getName();
697:
698: /**
699: * Get the Unicode code point for the encoded code Point
700: * <code>byteCode</code> or -1 if not available.
701: *
702: * @param byteCode
703: * The encoded code point
704: *
705: * @return The Unicode code point for the encoded code Point
706: * <code>byteCode</code> or -1.
707: */
708: public abstract int getUnicode(int byteCode);
709:
710: /**
711: * Get the byte code point where the character referenced by the Unicode
712: * code point <code>unicode</code> is mapped in this font encoding or a
713: * valid replacement code point if invalid.
714: *
715: * <p>
716: * <code>unicode</code> must be a valid Unicode code point. The returned
717: * code point is a value from 0...255. If the Unicode code point is not
718: * valid or not mapped, a valid code point is returned is returned (for
719: * example the space character).
720: * </p>
721: *
722: * @param unicode
723: * The Unicode code point to look up.
724: *
725: * @return The index from 0..255 where this character is mapped or valid
726: * replacement.
727: */
728: public abstract int getValidByteCode(int unicode);
729:
730: /**
731: * Get the code point where the character referenced by the Adobe glyph name
732: * <code>name</code> is mapped in this font encoding or a valid
733: * replacement.
734: *
735: * <p>
736: * <code>name</code> must be a valid Adobe glyph name. The code point is a
737: * value from 0...255. If the glyph is not found, a valif replacement code
738: * point is returned instead (for example the space character).
739: * </p>
740: *
741: * @param name
742: * The glyph name.
743: *
744: * @return The index from 0..255 where this character is mapped or a valid
745: * replacement.
746: */
747: public abstract int getValidByteCode(String name);
748:
749: /**
750: * Answer true if this encoding is specific to and embedded into the font
751: * program itself so that we can not derive any mapping information from
752: * here.
753: *
754: * @return Answer true if this encoding is specific to and embedded into the
755: * font program itself
756: */
757: public boolean isFontSpecificEncoding() {
758: return false;
759: }
760:
761: public String toString() {
762: return getName();
763: }
764:
765: public int getDifferenceUnicode(int codePoint) {
766: return codePoint;
767: }
768:
769: public String getDifferenceGlyphName(int codePoint) {
770: return null;
771: }
772: }
|