/** * {@inheritDoc} */ @Override public GlyphLine decodeIntoGlyphLine(PdfString content) { byte[] contentBytes = content.getValueBytes(); List<Glyph> glyphs = new ArrayList<>(contentBytes.length); for (byte b : contentBytes) { int code = b & 0xff; Glyph glyph = null; if (toUnicode != null && toUnicode.lookup(code) != null && (glyph = fontProgram.getGlyphByCode(code)) != null) { if (!Arrays.equals(toUnicode.lookup(code), glyph.getChars())) { // Copy the glyph because the original one may be reused (e.g. standard Helvetica font program) glyph = new Glyph(glyph); glyph.setChars(toUnicode.lookup(code)); } } else { int uni = fontEncoding.getUnicode(code); if (uni > -1) { glyph = getGlyph(uni); } else if (fontEncoding.getBaseEncoding() == null) { glyph = fontProgram.getGlyphByCode(code); } } if (glyph != null) { glyphs.add(glyph); } } return new GlyphLine(glyphs); }
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; }
for (int k = 0; k < shortTag.length; k++) { if (shortTag[k] != 0) { int uni = fontEncoding.getUnicode(k); Glyph glyph = uni > -1 ? fontProgram.getGlyph(uni) : fontProgram.getGlyphByCode(k); if (glyph != null) {
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); if (glyph.hasValidUnicode()) {
} else { int uni = fontEncoding.getUnicode(k); Glyph glyph = uni > -1 ? getGlyph(uni) : fontProgram.getGlyphByCode(k); wd.add(new PdfNumber(glyph != null ? glyph.getWidth() : 0));
@Override public void flush() { if (isFlushed()) return; ensureUnderlyingObjectHasIndirectReference(); if (((Type3Font) getFontProgram()).getNumberOfGlyphs() < 1) { throw new PdfException("No glyphs defined for type3 font."); } PdfDictionary charProcs = new PdfDictionary(); for (int i = 0; i < 256; i++) { if (fontEncoding.canDecode(i)) { Type3Glyph glyph = getType3Glyph(fontEncoding.getUnicode(i)); if (glyph != null) { charProcs.put(new PdfName(fontEncoding.getDifference(i)), glyph.getContentStream()); glyph.getContentStream().flush(); } } } getPdfObject().put(PdfName.CharProcs, charProcs); getPdfObject().put(PdfName.FontMatrix, new PdfArray(getFontMatrix())); getPdfObject().put(PdfName.FontBBox, new PdfArray(fontProgram.getFontMetrics().getBbox())); String fontName = fontProgram.getFontNames().getFontName(); super.flushFontData(fontName, PdfName.Type3); //BaseFont is not listed as key in Type 3 font specification. getPdfObject().remove(PdfName.BaseFont); super.flush(); }