PdfSimpleFont extends PdfFont { @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); } protected  PdfSimpleFont(PdfDictionary fontDictionary); protected  PdfSimpleFont(); @Override boolean isBuiltWith(String fontProgram, String encoding); @Override GlyphLine createGlyphLine(String content); @Override int appendGlyphs(String text, int from, int to, List<Glyph> glyphs); @Override int appendAnyGlyph(String text, int from, List<Glyph> glyphs); FontEncoding getFontEncoding(); @Override byte[] convertToBytes(String text); @Override byte[] convertToBytes(GlyphLine glyphLine); @Override byte[] convertToBytes(Glyph glyph); @Override void writeText(GlyphLine text, int from, int to, PdfOutputStream stream); @Override void writeText(String text, PdfOutputStream stream); @Override String decode(PdfString content); @Override GlyphLine decodeIntoGlyphLine(PdfString content); @Override float getContentWidth(PdfString content); boolean isForceWidthsOutput(); void setForceWidthsOutput(boolean forceWidthsOutput);  }
@Test public void writeTextGlyphLineWithSpecificEncodingTest() throws IOException { PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createFontSpecificEncoding()); List<Glyph> glyphs = new ArrayList<>(); glyphs.add(T_GLYPH_FONT_SPECIFIC); glyphs.add(E_GLYPH_FONT_SPECIFIC); GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (PdfOutputStream pos = new PdfOutputStream(bos)) { fontToTest.writeText(glyphLine, 0, 1, pos); } Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, T_CODE, E_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray()); }
@Test public void writeTextGlyphLineWithCustomEncodingTest() throws IOException { FontEncoding fontEncoding = FontEncoding.createEmptyFontEncoding(); fontEncoding.addSymbol(E_CUSTOM_CODE, E_CODE); PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(fontEncoding); List<Glyph> glyphs = new ArrayList<>(); glyphs.add(E_GLYPH_FONT_SPECIFIC); glyphs.add(T_GLYPH_FONT_SPECIFIC); GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (PdfOutputStream pos = new PdfOutputStream(bos)) { fontToTest.writeText(glyphLine, 0, 1, pos); } Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, E_CUSTOM_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray()); }
@Test public void writeTextGlyphLineWithEmptyEncodingTest() throws IOException { PdfSimpleFont<FontProgram> fontToTest = new TestSimpleFont(FontEncoding.createEmptyFontEncoding()); List<Glyph> glyphs = new ArrayList<>(); glyphs.add(E_GLYPH_FONT_SPECIFIC); glyphs.add(T_GLYPH_FONT_SPECIFIC); GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (PdfOutputStream pos = new PdfOutputStream(bos)) { fontToTest.writeText(glyphLine, 0, 1, pos); } Assert.assertArrayEquals(new byte[]{OPEN_BRACKET_CODE, CLOSE_BRACKET_CODE}, bos.toByteArray()); }