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: PSFontUtils.java 512838 2007-02-28 16:42:28Z jeremias $ */
019:
020: package org.apache.xmlgraphics.ps;
021:
022: import java.io.BufferedReader;
023: import java.io.IOException;
024: import java.io.InputStream;
025:
026: import org.apache.commons.io.EndianUtils;
027: import org.apache.commons.io.IOUtils;
028: import org.apache.xmlgraphics.fonts.Glyphs;
029: import org.apache.xmlgraphics.util.io.ASCIIHexOutputStream;
030: import org.apache.xmlgraphics.util.io.SubInputStream;
031:
032: /**
033: * Utility code for font handling in PostScript.
034: */
035: public class PSFontUtils {
036:
037: /**
038: * This method reads a Type 1 font from a stream and embeds it into a PostScript stream.
039: * Note: Only the IBM PC Format as described in section 3.3 of the Adobe Technical Note #5040
040: * is supported.
041: * @param gen The PostScript generator
042: * @param in the InputStream from which to read the Type 1 font
043: * @throws IOException in case an I/O problem occurs
044: */
045: public static void embedType1Font(PSGenerator gen, InputStream in)
046: throws IOException {
047: boolean finished = false;
048: while (!finished) {
049: int segIndicator = in.read();
050: if (segIndicator < 0) {
051: throw new IOException(
052: "Unexpected end-of-file while reading segment indicator");
053: } else if (segIndicator != 128) {
054: throw new IOException("Expected ASCII 128, found: "
055: + segIndicator);
056: }
057: int segType = in.read();
058: if (segType < 0) {
059: throw new IOException(
060: "Unexpected end-of-file while reading segment type");
061: }
062: int dataSegLen = 0;
063: switch (segType) {
064: case 1: //ASCII
065: dataSegLen = EndianUtils.readSwappedInteger(in);
066:
067: BufferedReader reader = new BufferedReader(
068: new java.io.InputStreamReader(
069: new SubInputStream(in, dataSegLen),
070: "US-ASCII"));
071: String line;
072: while ((line = reader.readLine()) != null) {
073: gen.writeln(line);
074: }
075: break;
076: case 2: //binary
077: dataSegLen = EndianUtils.readSwappedInteger(in);
078:
079: SubInputStream sin = new SubInputStream(in, dataSegLen);
080: ASCIIHexOutputStream hexOut = new ASCIIHexOutputStream(
081: gen.getOutputStream());
082: IOUtils.copy(sin, hexOut);
083: gen.newLine();
084: break;
085: case 3: //EOF
086: finished = true;
087: break;
088: default:
089: throw new IOException("Unsupported segment type: "
090: + segType);
091: }
092: }
093: }
094:
095: /**
096: * Defines the WinAnsi encoding for use in PostScript files.
097: * @param gen the PostScript generator
098: * @throws IOException In case of an I/O problem
099: */
100: public static void defineWinAnsiEncoding(PSGenerator gen)
101: throws IOException {
102: gen.writeln("/WinAnsiEncoding [");
103: for (int i = 0; i < Glyphs.WINANSI_ENCODING.length; i++) {
104: if (i > 0) {
105: if ((i % 5) == 0) {
106: gen.newLine();
107: } else {
108: gen.write(" ");
109: }
110: }
111: final char ch = Glyphs.WINANSI_ENCODING[i];
112: final String glyphname = Glyphs.charToGlyphName(ch);
113: if ("".equals(glyphname)) {
114: gen.write("/" + Glyphs.NOTDEF);
115: } else {
116: gen.write("/");
117: gen.write(glyphname);
118: }
119: }
120: gen.newLine();
121: gen.writeln("] def");
122: }
123:
124: /**
125: * Redefines the encoding of a font.
126: * @param gen the PostScript generator
127: * @param fontName the font name
128: * @param encoding the new encoding (must be predefined in the PS file)
129: * @throws IOException In case of an I/O problem
130: */
131: public static void redefineFontEncoding(PSGenerator gen,
132: String fontName, String encoding) throws IOException {
133: gen.writeln("/" + fontName + " findfont");
134: gen.writeln("dup length dict begin");
135: gen
136: .writeln(" {1 index /FID ne {def} {pop pop} ifelse} forall");
137: gen.writeln(" /Encoding " + encoding + " def");
138: gen.writeln(" currentdict");
139: gen.writeln("end");
140: gen.writeln("/" + fontName + " exch definefont pop");
141: }
142:
143: }
|