|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using iText.IO.Font; |
| 4 | +using iText.IO.Font.Constants; |
| 5 | +using iText.IO.Font.Otf; |
| 6 | +using iText.Kernel.Pdf; |
| 7 | +using iText.Test; |
| 8 | + |
| 9 | +namespace iText.Kernel.Font { |
| 10 | + public class PdfSimpleFontTest : ExtendedITextTest { |
| 11 | + private const byte T_CODE = 116; |
| 12 | + |
| 13 | + private const byte E_CODE = 101; |
| 14 | + |
| 15 | + private const byte E_CUSTOM_CODE = 103; |
| 16 | + |
| 17 | + private const byte OPEN_BRACKET_CODE = 40; |
| 18 | + |
| 19 | + private const byte CLOSE_BRACKET_CODE = 41; |
| 20 | + |
| 21 | + private static Glyph E_GLYPH_FONT_SPECIFIC; |
| 22 | + |
| 23 | + private static Glyph T_GLYPH_FONT_SPECIFIC; |
| 24 | + |
| 25 | + private static Glyph E_GLYPH_CUSTOM_MAPPED; |
| 26 | + |
| 27 | + [NUnit.Framework.OneTimeSetUp] |
| 28 | + public static void Init() { |
| 29 | + T_GLYPH_FONT_SPECIFIC = new Glyph(T_CODE, 278, 116, new int[] { 14, -7, 257, 669 }); |
| 30 | + T_GLYPH_FONT_SPECIFIC.SetChars(new char[] { 't' }); |
| 31 | + E_GLYPH_FONT_SPECIFIC = new Glyph(E_CODE, 556, 101, new int[] { 40, -15, 516, 538 }); |
| 32 | + E_GLYPH_FONT_SPECIFIC.SetChars(new char[] { 'e' }); |
| 33 | + E_GLYPH_CUSTOM_MAPPED = new Glyph(E_CUSTOM_CODE, 44, 103); |
| 34 | + E_GLYPH_CUSTOM_MAPPED.SetChars(new char[] { 'e' }); |
| 35 | + } |
| 36 | + |
| 37 | + [NUnit.Framework.Test] |
| 38 | + public virtual void CreateGlyphLineWithSpecificEncodingTest() { |
| 39 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 40 | + ()); |
| 41 | + GlyphLine glyphLine = fontToTest.CreateGlyphLine("te"); |
| 42 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 43 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 44 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 45 | + GlyphLine expected = new GlyphLine(glyphs, 0, 2); |
| 46 | + NUnit.Framework.Assert.AreEqual(expected, glyphLine); |
| 47 | + } |
| 48 | + |
| 49 | + [NUnit.Framework.Test] |
| 50 | + public virtual void CreateGlyphLineWithEmptyEncodingTest() { |
| 51 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 52 | + ()); |
| 53 | + GlyphLine glyphLine = fontToTest.CreateGlyphLine("te"); |
| 54 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 55 | + glyphs.Add(E_GLYPH_CUSTOM_MAPPED); |
| 56 | + GlyphLine expected = new GlyphLine(glyphs, 0, 1); |
| 57 | + NUnit.Framework.Assert.AreEqual(expected, glyphLine); |
| 58 | + } |
| 59 | + |
| 60 | + [NUnit.Framework.Test] |
| 61 | + public virtual void AppendGlyphsWithSpecificEncodingTest() { |
| 62 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 63 | + ()); |
| 64 | + IList<Glyph> toAppend = new List<Glyph>(); |
| 65 | + int processed = fontToTest.AppendGlyphs("te", 0, 1, toAppend); |
| 66 | + NUnit.Framework.Assert.AreEqual(2, processed); |
| 67 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 68 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 69 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 70 | + NUnit.Framework.Assert.AreEqual(glyphs, toAppend); |
| 71 | + } |
| 72 | + |
| 73 | + [NUnit.Framework.Test] |
| 74 | + public virtual void AppendGlyphsWithEmptyEncodingTest() { |
| 75 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 76 | + ()); |
| 77 | + IList<Glyph> toAppend = new List<Glyph>(); |
| 78 | + int processed = fontToTest.AppendGlyphs("e ete", 0, 4, toAppend); |
| 79 | + NUnit.Framework.Assert.AreEqual(3, processed); |
| 80 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 81 | + glyphs.Add(E_GLYPH_CUSTOM_MAPPED); |
| 82 | + glyphs.Add(E_GLYPH_CUSTOM_MAPPED); |
| 83 | + NUnit.Framework.Assert.AreEqual(glyphs, toAppend); |
| 84 | + } |
| 85 | + |
| 86 | + [NUnit.Framework.Test] |
| 87 | + public virtual void AppendAnyGlyphWithSpecificEncodingTest() { |
| 88 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 89 | + ()); |
| 90 | + IList<Glyph> toAppend = new List<Glyph>(); |
| 91 | + int processed = fontToTest.AppendAnyGlyph("te", 0, toAppend); |
| 92 | + NUnit.Framework.Assert.AreEqual(1, processed); |
| 93 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 94 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 95 | + NUnit.Framework.Assert.AreEqual(glyphs, toAppend); |
| 96 | + } |
| 97 | + |
| 98 | + [NUnit.Framework.Test] |
| 99 | + public virtual void AppendAnyGlyphWithEmptyEncodingTest() { |
| 100 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 101 | + ()); |
| 102 | + IList<Glyph> toAppend = new List<Glyph>(); |
| 103 | + int processed = fontToTest.AppendAnyGlyph("e ete", 0, toAppend); |
| 104 | + NUnit.Framework.Assert.AreEqual(1, processed); |
| 105 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 106 | + glyphs.Add(E_GLYPH_CUSTOM_MAPPED); |
| 107 | + NUnit.Framework.Assert.AreEqual(glyphs, toAppend); |
| 108 | + } |
| 109 | + |
| 110 | + [NUnit.Framework.Test] |
| 111 | + public virtual void ConvertGlyphLineToBytesWithSpecificEncodingTest() { |
| 112 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 113 | + ()); |
| 114 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 115 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 116 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 117 | + GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); |
| 118 | + byte[] bytes = fontToTest.ConvertToBytes(glyphLine); |
| 119 | + NUnit.Framework.Assert.AreEqual(new byte[] { T_CODE, E_CODE }, bytes); |
| 120 | + } |
| 121 | + |
| 122 | + [NUnit.Framework.Test] |
| 123 | + public virtual void ConvertGlyphLineToBytesWithEmptyEncodingTest() { |
| 124 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 125 | + ()); |
| 126 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 127 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 128 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 129 | + GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); |
| 130 | + byte[] bytes = fontToTest.ConvertToBytes(glyphLine); |
| 131 | + NUnit.Framework.Assert.AreEqual(new byte[0], bytes); |
| 132 | + } |
| 133 | + |
| 134 | + [NUnit.Framework.Test] |
| 135 | + public virtual void ConvertToBytesWithNullEntry() { |
| 136 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 137 | + ()); |
| 138 | + byte[] bytes = fontToTest.ConvertToBytes((GlyphLine)null); |
| 139 | + NUnit.Framework.Assert.AreEqual(new byte[0], bytes); |
| 140 | + } |
| 141 | + |
| 142 | + [NUnit.Framework.Test] |
| 143 | + public virtual void ConvertGlyphToBytesWithSpecificEncodingTest() { |
| 144 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 145 | + ()); |
| 146 | + byte[] bytes = fontToTest.ConvertToBytes(E_GLYPH_FONT_SPECIFIC); |
| 147 | + NUnit.Framework.Assert.AreEqual(new byte[] { E_CODE }, bytes); |
| 148 | + } |
| 149 | + |
| 150 | + [NUnit.Framework.Test] |
| 151 | + public virtual void ConvertGlyphToBytesWithCustomEncodingTest() { |
| 152 | + FontEncoding emptyFontEncoding = FontEncoding.CreateEmptyFontEncoding(); |
| 153 | + emptyFontEncoding.AddSymbol(E_CUSTOM_CODE, E_CODE); |
| 154 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(emptyFontEncoding); |
| 155 | + byte[] bytes = fontToTest.ConvertToBytes(E_GLYPH_FONT_SPECIFIC); |
| 156 | + NUnit.Framework.Assert.AreEqual(new byte[] { E_CUSTOM_CODE }, bytes); |
| 157 | + } |
| 158 | + |
| 159 | + [NUnit.Framework.Test] |
| 160 | + public virtual void ConvertGlyphToBytesWithEmptyEncodingTest() { |
| 161 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 162 | + ()); |
| 163 | + byte[] bytes = fontToTest.ConvertToBytes(E_GLYPH_FONT_SPECIFIC); |
| 164 | + NUnit.Framework.Assert.AreEqual(new byte[0], bytes); |
| 165 | + } |
| 166 | + |
| 167 | + [NUnit.Framework.Test] |
| 168 | + public virtual void WriteTextGlyphLineWithSpecificEncodingTest() { |
| 169 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateFontSpecificEncoding |
| 170 | + ()); |
| 171 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 172 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 173 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 174 | + GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); |
| 175 | + MemoryStream bos = new MemoryStream(); |
| 176 | + using (PdfOutputStream pos = new PdfOutputStream(bos)) { |
| 177 | + fontToTest.WriteText(glyphLine, 0, 1, pos); |
| 178 | + } |
| 179 | + NUnit.Framework.Assert.AreEqual(new byte[] { OPEN_BRACKET_CODE, T_CODE, E_CODE, CLOSE_BRACKET_CODE }, bos. |
| 180 | + ToArray()); |
| 181 | + } |
| 182 | + |
| 183 | + [NUnit.Framework.Test] |
| 184 | + public virtual void WriteTextGlyphLineWithCustomEncodingTest() { |
| 185 | + FontEncoding fontEncoding = FontEncoding.CreateEmptyFontEncoding(); |
| 186 | + fontEncoding.AddSymbol(E_CUSTOM_CODE, E_CODE); |
| 187 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(fontEncoding); |
| 188 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 189 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 190 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 191 | + GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); |
| 192 | + MemoryStream bos = new MemoryStream(); |
| 193 | + using (PdfOutputStream pos = new PdfOutputStream(bos)) { |
| 194 | + fontToTest.WriteText(glyphLine, 0, 1, pos); |
| 195 | + } |
| 196 | + NUnit.Framework.Assert.AreEqual(new byte[] { OPEN_BRACKET_CODE, E_CUSTOM_CODE, CLOSE_BRACKET_CODE }, bos.ToArray |
| 197 | + ()); |
| 198 | + } |
| 199 | + |
| 200 | + [NUnit.Framework.Test] |
| 201 | + public virtual void WriteTextGlyphLineWithEmptyEncodingTest() { |
| 202 | + PdfSimpleFont<FontProgram> fontToTest = new PdfSimpleFontTest.TestSimpleFont(FontEncoding.CreateEmptyFontEncoding |
| 203 | + ()); |
| 204 | + IList<Glyph> glyphs = new List<Glyph>(); |
| 205 | + glyphs.Add(E_GLYPH_FONT_SPECIFIC); |
| 206 | + glyphs.Add(T_GLYPH_FONT_SPECIFIC); |
| 207 | + GlyphLine glyphLine = new GlyphLine(glyphs, 0, 2); |
| 208 | + MemoryStream bos = new MemoryStream(); |
| 209 | + using (PdfOutputStream pos = new PdfOutputStream(bos)) { |
| 210 | + fontToTest.WriteText(glyphLine, 0, 1, pos); |
| 211 | + } |
| 212 | + NUnit.Framework.Assert.AreEqual(new byte[] { OPEN_BRACKET_CODE, CLOSE_BRACKET_CODE }, bos.ToArray()); |
| 213 | + } |
| 214 | + |
| 215 | + private class TestSimpleFont : PdfSimpleFont<FontProgram> { |
| 216 | + public TestSimpleFont(FontEncoding fontEncoding) { |
| 217 | + this.fontEncoding = fontEncoding; |
| 218 | + SetFontProgram(FontProgramFactory.CreateFont(StandardFonts.HELVETICA)); |
| 219 | + } |
| 220 | + |
| 221 | + public override Glyph GetGlyph(int unicode) { |
| 222 | + if (unicode == E_CODE) { |
| 223 | + return E_GLYPH_CUSTOM_MAPPED; |
| 224 | + } |
| 225 | + return null; |
| 226 | + } |
| 227 | + |
| 228 | + protected internal override void AddFontStream(PdfDictionary fontDescriptor) { |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments