| Method from com.lowagie.text.pdf.PdfEncodings Detail: |
public static void addExtraEncoding(String name,
ExtraEncoding enc) {
synchronized (extraEncodings) { // This serializes concurrent updates
HashMap newEncodings = (HashMap)extraEncodings.clone();
newEncodings.put(name.toLowerCase(), enc);
extraEncodings = newEncodings; // This swap does not require synchronization with reader
}
}
|
static void breakLong(long n,
int size,
byte[] seqs) {
for (int k = 0; k < size; ++k) {
seqs[k] = (byte)(n > > ((size - 1 - k) * 8));
}
}
|
public static void clearCmap(String name) {
synchronized (cmaps) {
if (name.length() == 0)
cmaps.clear();
else
cmaps.remove(name);
}
}
Clears the CJK cmaps from the cache. If name is the
empty string then all the cache is cleared. Calling this method
has no consequences other than the need to reload the cmap
if needed. |
public static String convertCmap(String name,
byte[] seq) {
return convertCmap(name, seq, 0, seq.length);
}
Converts a byte array encoded as name
to a CID string. This is needed to reach some CJK characters
that don't exist in 16 bit Unicode.
The font to use this result must use the encoding "Identity-H"
or "Identity-V".
See ftp://ftp.oreilly.com/pub/examples/nutshell/cjkv/adobe/. |
public static String convertCmap(String name,
byte[] seq,
int start,
int length) {
try {
char planes[][] = null;
synchronized (cmaps) {
planes = (char[][])cmaps.get(name);
}
if (planes == null) {
planes = readCmap(name, (byte[][])null);
synchronized (cmaps) {
cmaps.put(name, planes);
}
}
return decodeSequence(seq, start, length, planes);
}
catch (IOException e) {
throw new ExceptionConverter(e);
}
}
Converts a byte array encoded as name
to a CID string. This is needed to reach some CJK characters
that don't exist in 16 bit Unicode.
The font to use this result must use the encoding "Identity-H"
or "Identity-V".
See ftp://ftp.oreilly.com/pub/examples/nutshell/cjkv/adobe/. |
public static final byte[] convertToBytes(String text,
String encoding) {
for (int k = 128; k < 161; ++k) {
char c = winansiByteToChar[k];
if (c != 65533)
winansi.put(c, k);
}
for (int k = 128; k < 161; ++k) {
char c = pdfEncodingByteToChar[k];
if (c != 65533)
pdfEncoding.put(c, k);
}
addExtraEncoding("Wingdings", new WingdingsConversion());
addExtraEncoding("Symbol", new SymbolConversion(true));
addExtraEncoding("ZapfDingbats", new SymbolConversion(false));
addExtraEncoding("SymbolTT", new SymbolTTConversion());
addExtraEncoding("Cp437", new Cp437Conversion());
if (text == null)
return new byte[0];
if (encoding == null || encoding.length() == 0) {
int len = text.length();
byte b[] = new byte[len];
for (int k = 0; k < len; ++k)
b[k] = (byte)text.charAt(k);
return b;
}
ExtraEncoding extra = (ExtraEncoding)extraEncodings.get(encoding.toLowerCase());
if (extra != null) {
byte b[] = extra.charToByte(text, encoding);
if (b != null)
return b;
}
IntHashtable hash = null;
if (encoding.equals(BaseFont.WINANSI))
hash = winansi;
else if (encoding.equals(PdfObject.TEXT_PDFDOCENCODING))
hash = pdfEncoding;
if (hash != null) {
char cc[] = text.toCharArray();
int len = cc.length;
int ptr = 0;
byte b[] = new byte[len];
int c = 0;
for (int k = 0; k < len; ++k) {
char char1 = cc[k];
if (char1 < 128 || (char1 > 160 && char1 < = 255))
c = char1;
else
c = hash.get(char1);
if (c != 0)
b[ptr++] = (byte)c;
}
if (ptr == len)
return b;
byte b2[] = new byte[ptr];
System.arraycopy(b, 0, b2, 0, ptr);
return b2;
}
if (encoding.equals(PdfObject.TEXT_UNICODE)) {
// workaround for jdk 1.2.2 bug
char cc[] = text.toCharArray();
int len = cc.length;
byte b[] = new byte[cc.length * 2 + 2];
b[0] = -2;
b[1] = -1;
int bptr = 2;
for (int k = 0; k < len; ++k) {
char c = cc[k];
b[bptr++] = (byte)(c > > 8);
b[bptr++] = (byte)(c & 0xff);
}
return b;
}
try {
return text.getBytes(encoding);
}
catch (UnsupportedEncodingException e) {
throw new ExceptionConverter(e);
}
}
Converts a String to a byte array according
to the font's encoding. |
public static final byte[] convertToBytes(char char1,
String encoding) {
if (encoding == null || encoding.length() == 0)
return new byte[]{(byte)char1};
ExtraEncoding extra = (ExtraEncoding)extraEncodings.get(encoding.toLowerCase());
if (extra != null) {
byte b[] = extra.charToByte(char1, encoding);
if (b != null)
return b;
}
IntHashtable hash = null;
if (encoding.equals(BaseFont.WINANSI))
hash = winansi;
else if (encoding.equals(PdfObject.TEXT_PDFDOCENCODING))
hash = pdfEncoding;
if (hash != null) {
int c = 0;
if (char1 < 128 || (char1 > 160 && char1 < = 255))
c = char1;
else
c = hash.get(char1);
if (c != 0)
return new byte[]{(byte)c};
else
return new byte[0];
}
if (encoding.equals(PdfObject.TEXT_UNICODE)) {
// workaround for jdk 1.2.2 bug
byte b[] = new byte[4];
b[0] = -2;
b[1] = -1;
b[2] = (byte)(char1 > > 8);
b[3] = (byte)(char1 & 0xff);
return b;
}
try {
return String.valueOf(char1).getBytes(encoding);
}
catch (UnsupportedEncodingException e) {
throw new ExceptionConverter(e);
}
}
Converts a String to a byte array according
to the font's encoding. |
public static final String convertToString(byte[] bytes,
String encoding) {
if (bytes == null)
return PdfObject.NOTHING;
if (encoding == null || encoding.length() == 0) {
char c[] = new char[bytes.length];
for (int k = 0; k < bytes.length; ++k)
c[k] = (char)(bytes[k] & 0xff);
return new String(c);
}
ExtraEncoding extra = (ExtraEncoding)extraEncodings.get(encoding.toLowerCase());
if (extra != null) {
String text = extra.byteToChar(bytes, encoding);
if (text != null)
return text;
}
char ch[] = null;
if (encoding.equals(BaseFont.WINANSI))
ch = winansiByteToChar;
else if (encoding.equals(PdfObject.TEXT_PDFDOCENCODING))
ch = pdfEncodingByteToChar;
if (ch != null) {
int len = bytes.length;
char c[] = new char[len];
for (int k = 0; k < len; ++k) {
c[k] = ch[bytes[k] & 0xff];
}
return new String(c);
}
try {
return new String(bytes, encoding);
}
catch (UnsupportedEncodingException e) {
throw new ExceptionConverter(e);
}
}
Converts a byte array to a String according
to the some encoding. |
static String decodeSequence(byte[] seq,
int start,
int length,
char[][] planes) {
StringBuffer buf = new StringBuffer();
int end = start + length;
int currentPlane = 0;
for (int k = start; k < end; ++k) {
int one = seq[k] & 0xff;
char plane[] = planes[currentPlane];
int cid = plane[one];
if ((cid & 0x8000) == 0) {
buf.append((char)cid);
currentPlane = 0;
}
else
currentPlane = cid & 0x7fff;
}
return buf.toString();
}
|
static void encodeSequence(int size,
byte[] seqs,
char cid,
ArrayList planes) {
--size;
int nextPlane = 0;
for (int idx = 0; idx < size; ++idx) {
char plane[] = (char[])planes.get(nextPlane);
int one = seqs[idx] & 0xff;
char c = plane[one];
if (c != 0 && (c & 0x8000) == 0)
throw new RuntimeException("Inconsistent mapping.");
if (c == 0) {
planes.add(new char[256]);
c = (char)((planes.size() - 1) | 0x8000);
plane[one] = c;
}
nextPlane = c & 0x7fff;
}
char plane[] = (char[])planes.get(nextPlane);
int one = seqs[size] & 0xff;
char c = plane[one];
if ((c & 0x8000) != 0)
throw new RuntimeException("Inconsistent mapping.");
plane[one] = cid;
}
|
static void encodeStream(InputStream in,
ArrayList planes) throws IOException {
BufferedReader rd = new BufferedReader(new InputStreamReader(in, "iso-8859-1"));
String line = null;
int state = CIDNONE;
byte seqs[] = new byte[7];
while ((line = rd.readLine()) != null) {
if (line.length() < 6)
continue;
switch (state) {
case CIDNONE: {
if (line.indexOf("begincidrange") >= 0)
state = CIDRANGE;
else if (line.indexOf("begincidchar") >= 0)
state = CIDCHAR;
else if (line.indexOf("usecmap") >= 0) {
StringTokenizer tk = new StringTokenizer(line);
String t = tk.nextToken();
readCmap(t.substring(1), planes);
}
break;
}
case CIDRANGE: {
if (line.indexOf("endcidrange") >= 0) {
state = CIDNONE;
break;
}
StringTokenizer tk = new StringTokenizer(line);
String t = tk.nextToken();
int size = t.length() / 2 - 1;
long start = Long.parseLong(t.substring(1, t.length() - 1), 16);
t = tk.nextToken();
long end = Long.parseLong(t.substring(1, t.length() - 1), 16);
t = tk.nextToken();
int cid = Integer.parseInt(t);
for (long k = start; k < = end; ++k) {
breakLong(k, size, seqs);
encodeSequence(size, seqs, (char)cid, planes);
++cid;
}
break;
}
case CIDCHAR: {
if (line.indexOf("endcidchar") >= 0) {
state = CIDNONE;
break;
}
StringTokenizer tk = new StringTokenizer(line);
String t = tk.nextToken();
int size = t.length() / 2 - 1;
long start = Long.parseLong(t.substring(1, t.length() - 1), 16);
t = tk.nextToken();
int cid = Integer.parseInt(t);
breakLong(start, size, seqs);
encodeSequence(size, seqs, (char)cid, planes);
break;
}
}
}
}
|
public static boolean isPdfDocEncoding(String text) {
if (text == null)
return true;
int len = text.length();
for (int k = 0; k < len; ++k) {
char char1 = text.charAt(k);
if (char1 < 128 || (char1 > 160 && char1 < = 255))
continue;
if (!pdfEncoding.containsKey(char1))
return false;
}
return true;
}
Checks is text only has PdfDocEncoding characters. |
public static void loadCmap(String name,
byte[][] newline) {
try {
char planes[][] = null;
synchronized (cmaps) {
planes = (char[][])cmaps.get(name);
}
if (planes == null) {
planes = readCmap(name, newline);
synchronized (cmaps) {
cmaps.put(name, planes);
}
}
}
catch (IOException e) {
throw new ExceptionConverter(e);
}
}
Loads a CJK cmap to the cache with the option of associating
sequences to the newline. |
static char[][] readCmap(String name,
byte[][] newline) throws IOException {
ArrayList planes = new ArrayList();
planes.add(new char[256]);
readCmap(name, planes);
if (newline != null) {
for (int k = 0; k < newline.length; ++k)
encodeSequence(newline[k].length, newline[k], BaseFont.CID_NEWLINE, planes);
}
char ret[][] = new char[planes.size()][];
return (char[][])planes.toArray(ret);
}
|
static void readCmap(String name,
ArrayList planes) throws IOException {
String fullName = BaseFont.RESOURCE_PATH + "cmaps/" + name;
InputStream in = BaseFont.getResourceStream(fullName);
if (in == null)
throw new IOException("The Cmap " + name + " was not found.");
encodeStream(in, planes);
in.close();
}
|