001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: PDFCMap.java 454725 2006-10-10 13:00:05Z bdelacretaz $ */
019:
020: package org.apache.fop.pdf;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024:
025: /**
026: * Class representing the CMap encodings.
027: *
028: * CMaps are defined in the "Predefined CJK CMap names" table.
029: * In section 5.6.4 of PDF reference 1.4.
030: */
031: public class PDFCMap extends PDFStream {
032:
033: /*
034: * Chinese (simplified)
035: */
036:
037: /**
038: * GB-EUC-H Microsoft Code Page 936 (lfCharSet 0x86), GB 2312-80
039: * character set, EUC-CN encoding
040: */
041: public static final String ENC_GB_EUC_H = "GB-EUC-H";
042:
043: /**
044: * GB-EUC-V Vertical version of GB-EUC-H
045: */
046: public static final String ENC_GB_EUC_V = "GB_EUC_V";
047:
048: /**
049: * GBpc-EUC-H Mac OS, GB 2312-80 character set, EUC-CN encoding, Script Manager code 19
050: */
051: public static final String ENC_GBPC_EUC_H = "GBpc-EUC-H";
052:
053: /**
054: * GBpc-EUC-V Vertical version of GBpc-EUC-H
055: */
056: public static final String ENC_GBPC_EUC_V = "GBpc-EUC-V";
057:
058: /**
059: * GBK-EUC-H Microsoft Code Page 936 (lfCharSet 0x86), GBK character set, GBK encoding
060: */
061: public static final String ENC_GBK_EUC_H = "GBK-EUC-H";
062:
063: /**
064: * GBK-EUC-V Vertical version of GBK-EUC-H
065: */
066: public static final String ENC_GBK_EUC_V = "GBK-EUC-V";
067:
068: /**
069: * GBKp-EUC-H Same as GBK-EUC-H, but replaces half-width
070: * Latin characters with proportional forms and maps character
071: * code 0x24 to a dollar sign ($) instead of a yuan symbol
072: */
073: public static final String ENC_GBKP_EUC_H = "GBKp-EUC-H";
074:
075: /**
076: * GBKp-EUC-V Vertical version of GBKp-EUC-H
077: */
078: public static final String ENC_GBKP_EUC_V = "GBKp-EUC-V";
079:
080: /**
081: * GBK2K-H GB 18030-2000 character set, mixed 1-, 2-, and 4-byte encoding
082: */
083: public static final String ENC_GBK2K_H = "GBK2K-H";
084:
085: /**
086: * GBK2K-V Vertical version of GBK2K-H
087: */
088: public static final String ENC_GBK2K_V = "GBK2K-V";
089:
090: /**
091: * UniGB-UCS2-H Unicode (UCS-2) encoding for the Adobe-GB1 character collection
092: */
093: public static final String ENC_UNIGB_UCS2_H = "UniGB-UCS2-H";
094:
095: /**
096: * UniGB-UCS2-V Vertical version of UniGB-UCS2-H
097: */
098: public static final String ENC_UNIGB_UCS2_V = "UniGB-UCS2-V";
099:
100: /*
101: * Chinese (Traditional)
102: */
103:
104: /**
105: * B5pc-H Mac OS, Big Five character set, Big Five encoding, Script Manager code 2
106: */
107: public static final String ENC_B5PC_H = "B5pc-H";
108:
109: /**
110: * B5pc-V Vertical version of B5pc-H
111: */
112: public static final String ENC_B5PC_V = "B5pc-V";
113:
114: /**
115: * HKscs-B5-H Hong Kong SCS, an extension to the Big Five
116: * character set and encoding
117: */
118: public static final String ENC_HKSCS_B5_H = "HKscs-B5-H";
119:
120: /**
121: * HKscs-B5-V Vertical version of HKscs-B5-H
122: */
123: public static final String ENC_HKSCS_B5_V = "HKscs-B5-V";
124:
125: /**
126: * ETen-B5-H Microsoft Code Page 950 (lfCharSet 0x88), Big Five
127: * character set with ETen extensions
128: */
129: public static final String ENC_ETEN_B5_H = "ETen-B5-H";
130:
131: /**
132: * ETen-B5-V Vertical version of ETen-B5-H
133: */
134: public static final String ENC_ETEN_B5_V = "ETen-B5-V";
135:
136: /**
137: * ETenms-B5-H Same as ETen-B5-H, but replaces half-width
138: * Latin characters with proportional forms
139: */
140: public static final String ENC_ETENMS_B5_H = "ETenms-B5-H";
141:
142: /**
143: * ETenms-B5-V Vertical version of ETenms-B5-H
144: */
145: public static final String ENC_ETENMS_B5_V = "ETenms-B5-V";
146:
147: /**
148: * CNS-EUC-H CNS 11643-1992 character set, EUC-TW encoding
149: */
150: public static final String ENC_CNS_EUC_H = "CNS-EUC-H";
151:
152: /**
153: * CNS-EUC-V Vertical version of CNS-EUC-H
154: */
155: public static final String ENC_CNS_EUC_V = "CNS-EUC-V";
156:
157: /**
158: * UniCNS-UCS2-H Unicode (UCS-2) encoding for the
159: * Adobe-CNS1 character collection
160: */
161: public static final String ENC_UNICNS_UCS2_H = "UniCNS-UCS2-H";
162:
163: /**
164: * UniCNS-UCS2-V Vertical version of UniCNS-UCS2-H
165: */
166: public static final String ENC_UNICNS_UCS2_V = "UniCNS-UCS2-V";
167:
168: /*
169: * Japanese
170: */
171:
172: /**
173: * 83pv-RKSJ-H Mac OS, JIS X 0208 character set with KanjiTalk6
174: * extensions, Shift-JIS encoding, Script Manager code 1
175: */
176: public static final String ENC_83PV_RKSJ_H = "83pv-RKSJ-H"; // no V version
177:
178: /**
179: * 90ms-RKSJ-H Microsoft Code Page 932 (lfCharSet 0x80), JIS X 0208
180: * character set with NEC and IBM extensions
181: */
182: public static final String ENC_90MS_RKSJ_H = "90ms-RKSJ-H";
183:
184: /**
185: * 90ms-RKSJ-V Vertical version of 90ms-RKSJ-H
186: */
187: public static final String ENC_90MS_RKSJ_V = "90ms-RKSJ-V";
188:
189: /**
190: * 90msp-RKSJ-H Same as 90ms-RKSJ-H, but replaces half-width Latin
191: * characters with proportional forms
192: */
193: public static final String ENC_90MSP_RKSJ_H = "90msp-RKSJ-H";
194:
195: /**
196: * 90msp-RKSJ-V Vertical version of 90msp-RKSJ-H
197: */
198: public static final String ENC_90MSP_RKSJ_V = "90msp-RKSJ-V";
199:
200: /**
201: * 90pv-RKSJ-H Mac OS, JIS X 0208 character set with KanjiTalk7
202: * extensions, Shift-JIS encoding, Script Manager code 1
203: */
204: public static final String ENC_90PV_RKSJ_H = "90pv-RKSJ-H"; // no V version
205:
206: /**
207: * Add-RKSJ-H JIS X 0208 character set with Fujitsu FMR
208: * extensions, Shift-JIS encoding
209: */
210: public static final String ENC_ADD_RKSJ_H = "Add-RKSJ-H";
211:
212: /**
213: * Add-RKSJ-V Vertical version of Add-RKSJ-H
214: */
215: public static final String ENC_ADD_RKSJ_V = "Add-RKSJ-V";
216:
217: /**
218: * EUC-H JIS X 0208 character set, EUC-JP encoding
219: */
220: public static final String ENC_EUC_H = "EUC-H";
221:
222: /**
223: * EUC-V Vertical version of EUC-H
224: */
225: public static final String ENC_EUC_V = "EUC-V";
226:
227: /**
228: * Ext-RKSJ-H JIS C 6226 (JIS78) character set with
229: * NEC extensions, Shift-JIS encoding
230: */
231: public static final String ENC_EXT_RKSJ_H = "Ext-RKSJ-H";
232:
233: /**
234: * Ext-RKSJ-V Vertical version of Ext-RKSJ-H
235: */
236: public static final String ENC_EXT_RKSJ_V = "Ext-RKSJ-V";
237:
238: /**
239: * H JIS X 0208 character set, ISO-2022-JP encoding
240: */
241: public static final String ENC_H = "H";
242:
243: /**
244: * V Vertical version of H
245: */
246: public static final String ENC_V = "V";
247:
248: /**
249: * UniJIS-UCS2-H Unicode (UCS-2) encoding for the
250: * Adobe-Japan1 character collection
251: */
252: public static final String ENC_UNIJIS_UCS2_H = "UniJIS-UCS2-H";
253:
254: /**
255: * UniJIS-UCS2-V Vertical version of UniJIS-UCS2-H
256: */
257: public static final String ENC_UNIJIS_UCS2_V = "UniJIS-UCS2-V";
258:
259: /**
260: * UniJIS-UCS2-HW-H Same as UniJIS-UCS2-H, but replaces proportional
261: * Latin characters with half-width forms
262: */
263: public static final String ENC_UNIJIS_UCS2_HW_H = "UniJIS-UCS2-HW-H";
264:
265: /**
266: * UniJIS-UCS2-HW-V Vertical version of UniJIS-UCS2-HW-H
267: */
268: public static final String ENC_UNIJIS_UCS2_HW_V = "UniJIS-UCS2-HW-V";
269:
270: /*
271: * Korean
272: */
273:
274: /**
275: * KSC-EUC-H KS X 1001:1992 character set, EUC-KR encoding
276: */
277: public static final String ENC_KSC_EUC_H = "KSC-EUC-H";
278:
279: /**
280: * KSC-EUC-V Vertical version of KSC-EUC-H
281: */
282: public static final String ENC_KSC_EUC_V = "KSC-EUC-V";
283:
284: /**
285: * KSCms-UHC-H Microsoft Code Page 949 (lfCharSet 0x81), KS X 1001:1992
286: * character set plus 8822 additional hangul,
287: * Unified Hangul Code (UHC) encoding
288: */
289: public static final String ENC_KSCMS_UHC_H = "KSCms-UHC-H";
290:
291: /**
292: * KSCms-UHC-V Vertical version of KSCms-UHC-H
293: */
294: public static final String ENC_KSCMS_UHC_V = "KSCms-UHC-V";
295:
296: /**
297: * KSCms-UHC-HW-H Same as KSCms-UHC-H, but replaces proportional
298: * Latin characters with half-width forms
299: */
300: public static final String ENC_KSCMS_UHC_HW_H = "KSCms-UHC-HW-H";
301:
302: /**
303: * KSCms-UHC-HW-V Vertical version of KSCms-UHC-HW-H
304: */
305: public static final String ENC_KSCMS_UHC_HW_V = "KSCms-UHC-HW-V";
306:
307: /**
308: * KSCpc-EUC-H Mac OS, KS X 1001:1992 character set with
309: * Mac OS KH extensions, Script Manager Code 3
310: */
311: public static final String ENC_KSCPC_EUC_H = "KSCpc-EUC-H"; // no V version
312:
313: /**
314: * UniKS-UCS2-H Unicode (UCS-2) encoding for the
315: * Adobe-Korea1 character collection
316: */
317: public static final String ENC_UNIKSC_UCS2_H = "UniKSC-UCS2-H";
318:
319: /**
320: * UniKS-UCS2-V Vertical version of UniKS-UCS2-H
321: */
322: public static final String ENC_UNIKSC_UCS2_V = "UniKSC-UCS2-V";
323:
324: /*
325: * Generic
326: */
327:
328: /**
329: * Identity-H The horizontal identity mapping for 2-byte CIDs;
330: * may be used with CIDFonts using any Registry, Ordering, and
331: * Supplement values. It maps 2-byte character codes ranging from
332: * 0 to 65,535 to the same 2-byte CID value, interpreted
333: * high-order byte first.
334: */
335: public static final String ENC_IDENTITY_H = "Identity-H";
336:
337: /**
338: * Identity-V Vertical version of Identity-H. The mapping
339: * is the same as for Identity-H.
340: */
341: public static final String ENC_IDENTTITY_V = "Identity-V";
342:
343: /**
344: * /CMapName attribute, one of the predefined constants
345: */
346: protected String name;
347:
348: /**
349: * /CIDSystemInfo attribute
350: */
351: protected PDFCIDSystemInfo sysInfo;
352:
353: /**
354: * horizontal writing direction
355: */
356: public static final byte WMODE_HORIZONTAL = 0;
357:
358: /**
359: * vertical writing direction
360: */
361: public static final byte WMODE_VERTICAL = 1;
362:
363: /**
364: * font's writing direction
365: */
366: protected byte wMode = WMODE_HORIZONTAL;
367:
368: /**
369: * base CMap (String or PDFStream)
370: */
371: protected Object base;
372:
373: /**
374: * create the /CMap object
375: *
376: * @param name one the registered names (see Table 7.20 on p 215)
377: * @param sysInfo the attributes of the character collection of the CIDFont
378: */
379: public PDFCMap(String name, PDFCIDSystemInfo sysInfo) {
380: super ();
381: this .name = name;
382: this .sysInfo = sysInfo;
383: this .base = null;
384: }
385:
386: /**
387: * set the writing direction
388: *
389: * @param mode is either <code>WMODE_HORIZONTAL</code>
390: * or <code>WMODE_VERTICAL</code>
391: */
392: public void setWMode(byte mode) {
393: this .wMode = mode;
394: }
395:
396: /**
397: * Add the contents of this pdf object to the PDF stream.
398: */
399: public void addContents() {
400: StringBuffer p = new StringBuffer();
401: fillInPDF(p);
402: add(p.toString());
403: }
404:
405: /**
406: * set the base CMap
407: *
408: * @param base the name of the base CMap
409: */
410: public void setUseCMap(String base) {
411: this .base = base;
412: }
413:
414: /**
415: * set the base CMap
416: *
417: * @param base the stream to be used as base CMap
418: */
419: public void setUseCMap(PDFStream base) {
420: this .base = base;
421: }
422:
423: /**
424: * Fill in the pdf string for this CMap.
425: *
426: * @param p the string buffer to add the pdf data to
427: */
428: public void fillInPDF(StringBuffer p) {
429: writePreStream(p);
430: writeStreamComments(p);
431: writeCIDInit(p);
432: writeCIDSystemInfo(p);
433: writeVersionTypeName(p);
434: writeCodeSpaceRange(p);
435: writeCIDRange(p);
436: writeBFEntries(p);
437: writeWrapUp(p);
438: writeStreamAfterComments(p);
439: writeUseCMap(p);
440: add(p.toString());
441: }
442:
443: protected void writePreStream(StringBuffer p) {
444: // p.append("/Type /CMap\n");
445: // p.append(sysInfo.toPDFString());
446: // p.append("/CMapName /" + name + EOL);
447: }
448:
449: protected void writeStreamComments(StringBuffer p) {
450: p.append("%!PS-Adobe-3.0 Resource-CMap\n");
451: p.append("%%DocumentNeededResources: ProcSet (CIDInit)\n");
452: p.append("%%IncludeResource: ProcSet (CIDInit)\n");
453: p.append("%%BeginResource: CMap (" + name + ")\n");
454: p.append("%%EndComments\n");
455: }
456:
457: protected void writeCIDInit(StringBuffer p) {
458: p.append("/CIDInit /ProcSet findresource begin\n");
459: p.append("12 dict begin\n");
460: p.append("begincmap\n");
461: }
462:
463: protected void writeCIDSystemInfo(StringBuffer p) {
464: p.append("/CIDSystemInfo 3 dict dup begin\n");
465: p.append(" /Registry (Adobe) def\n");
466: p.append(" /Ordering (Identity) def\n");
467: p.append(" /Supplement 0 def\n");
468: p.append("end def\n");
469: }
470:
471: protected void writeVersionTypeName(StringBuffer p) {
472: p.append("/CMapVersion 1 def\n");
473: p.append("/CMapType 1 def\n");
474: p.append("/CMapName /" + name + " def\n");
475: }
476:
477: protected void writeCodeSpaceRange(StringBuffer p) {
478: p.append("1 begincodespacerange\n");
479: p.append("<0000> <FFFF>\n");
480: p.append("endcodespacerange\n");
481: }
482:
483: protected void writeCIDRange(StringBuffer p) {
484: p.append("1 begincidrange\n");
485: p.append("<0000> <FFFF> 0\n");
486: p.append("endcidrange\n");
487: }
488:
489: protected void writeBFEntries(StringBuffer p) {
490: // p.append("1 beginbfrange\n");
491: // p.append("<0020> <0100> <0000>\n");
492: // p.append("endbfrange\n");
493: }
494:
495: protected void writeWrapUp(StringBuffer p) {
496: p.append("endcmap\n");
497: p.append("CMapName currentdict /CMap defineresource pop\n");
498: p.append("end\n");
499: p.append("end\n");
500: }
501:
502: protected void writeStreamAfterComments(StringBuffer p) {
503: p.append("%%EndResource\n");
504: p.append("%%EOF\n");
505: }
506:
507: protected void writeUseCMap(StringBuffer p) {
508: /*
509: * p.append(" /Type /CMap");
510: * p.append("/CMapName /" + name + EOL);
511: * p.append("/WMode " + wMode + EOL);
512: * if (base != null) {
513: * p.append("/UseCMap ");
514: * if (base instanceof String) {
515: * p.append("/"+base);
516: * } else {// base instanceof PDFStream
517: * p.append(((PDFStream)base).referencePDF());
518: * }
519: * }
520: */
521: }
522:
523: protected int output(OutputStream stream) throws IOException {
524: fillInPDF(new StringBuffer());
525: return super.output(stream);
526: }
527: }
|