/** * Converts a {@code String} to a {@code byte} array according to the encoding. * String could contain a unicode symbols or font specific codes. * * @param text the {@code String} to be converted. * @return an array of {@code byte} representing the conversion according to the encoding */ public byte[] convertToBytes(String text) { if (text == null || text.length() == 0) { return emptyBytes; } int ptr = 0; byte[] bytes = new byte[text.length()]; for (int i = 0; i < text.length(); i++) { if (unicodeToCode.containsKey(text.charAt(i))) { bytes[ptr++] = (byte) convertToByte(text.charAt(i)); } } return ArrayUtil.shortenArray(bytes, ptr); }
/** * Converts a {@code String} to a {@code byte} array according to the encoding. * String could contain a unicode symbols or font specific codes. * * @param text the {@code String} to be converted. * @return an array of {@code byte} representing the conversion according to the encoding */ public byte[] convertToBytes(String text) { if (text == null || text.length() == 0) { return emptyBytes; } int ptr = 0; byte[] bytes = new byte[text.length()]; for (int i = 0; i < text.length(); i++) { if (unicodeToCode.containsKey(text.charAt(i))) { bytes[ptr++] = (byte) convertToByte(text.charAt(i)); } } return ArrayUtil.shortenArray(bytes, ptr); }
@Override public byte[] convertToBytes(Glyph glyph) { byte[] bytes = new byte[1]; if (fontEncoding.isFontSpecific()) { bytes[0] = (byte) glyph.getCode(); } else { if (fontEncoding.canEncode(glyph.getUnicode())) { bytes[0] = (byte) fontEncoding.convertToByte(glyph.getUnicode()); } else { return EMPTY_BYTES; } } shortTag[bytes[0] & 0xff] = 1; return bytes; }
static TrueTypeFont createFontProgram(PdfDictionary fontDictionary, FontEncoding fontEncoding, CMapToUnicode toUnicode) { DocTrueTypeFont fontProgram = new DocTrueTypeFont(fontDictionary); fillFontDescriptor(fontProgram, fontDictionary.getAsDictionary(PdfName.FontDescriptor)); PdfNumber firstCharNumber = fontDictionary.getAsNumber(PdfName.FirstChar); int firstChar = firstCharNumber != null ? Math.max(firstCharNumber.intValue(), 0) : 0; int[] widths = FontUtil.convertSimpleWidthsArray(fontDictionary.getAsArray(PdfName.Widths), firstChar, fontProgram.getMissingWidth()); fontProgram.avgWidth = 0; int glyphsWithWidths = 0; for (int i = 0; i < 256; i++) { Glyph glyph = new Glyph(i, widths[i], fontEncoding.getUnicode(i)); fontProgram.codeToGlyph.put(i, glyph); //FontEncoding.codeToUnicode table has higher priority if (glyph.hasValidUnicode() && fontEncoding.convertToByte(glyph.getUnicode()) == i) { fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph); } else if (toUnicode != null) { glyph.setChars(toUnicode.lookup(i)); } if (widths[i] > 0) { glyphsWithWidths++; fontProgram.avgWidth += widths[i]; } } if (glyphsWithWidths != 0) { fontProgram.avgWidth /= glyphsWithWidths; } return fontProgram; }
@Override public void writeText(GlyphLine text, int from, int to, PdfOutputStream stream) { byte[] bytes = new byte[to - from + 1]; int ptr = 0; if (fontEncoding.isFontSpecific()) { for (int i = from; i <= to; i++) { bytes[ptr++] = (byte) text.get(i).getCode(); } } else { for (int i = from; i <= to; i++) { if (fontEncoding.canEncode(text.get(i).getUnicode())) { bytes[ptr++] = (byte) fontEncoding.convertToByte(text.get(i).getUnicode()); } } } bytes = ArrayUtil.shortenArray(bytes, ptr); for (byte b : bytes) { shortTag[b & 0xff] = 1; } StreamUtil.writeEscapedString(stream, bytes); }
@Override public byte[] convertToBytes(GlyphLine glyphLine) { if (glyphLine != null) { byte[] bytes = new byte[glyphLine.size()]; int ptr = 0; if (fontEncoding.isFontSpecific()) { for (int i = 0; i < glyphLine.size(); i++) { bytes[ptr++] = (byte) glyphLine.get(i).getCode(); } } else { for (int i = 0; i < glyphLine.size(); i++) { if (fontEncoding.canEncode(glyphLine.get(i).getUnicode())) { bytes[ptr++] = (byte) fontEncoding.convertToByte(glyphLine.get(i).getUnicode()); } } } bytes = ArrayUtil.shortenArray(bytes, ptr); for (byte b : bytes) { shortTag[b & 0xff] = 1; } return bytes; } else { return EMPTY_BYTES; } }
if (glyph.hasValidUnicode()) { if (fontEncoding.convertToByte(glyph.getUnicode()) == i) { fontProgram.unicodeToGlyph.put(glyph.getUnicode(), glyph);
int unicode = AdobeGlyphList.nameToUnicode(glyphName.getValue()); if (unicode != -1 && fontEncoding.canEncode(unicode)) { int code = fontEncoding.convertToByte(unicode); ((Type3Font) getFontProgram()).addGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.getAsStream(glyphName), getDocument()));