|
| 1 | +/* |
| 2 | +This file is part of the iText (R) project. |
| 3 | +Copyright (c) 1998-2022 iText Group NV |
| 4 | +Authors: iText Software. |
| 5 | +
|
| 6 | +This program is offered under a commercial and under the AGPL license. |
| 7 | +For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below. |
| 8 | +
|
| 9 | +AGPL licensing: |
| 10 | +This program is free software: you can redistribute it and/or modify |
| 11 | +it under the terms of the GNU Affero General Public License as published by |
| 12 | +the Free Software Foundation, either version 3 of the License, or |
| 13 | +(at your option) any later version. |
| 14 | +
|
| 15 | +This program is distributed in the hope that it will be useful, |
| 16 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | +GNU Affero General Public License for more details. |
| 19 | +
|
| 20 | +You should have received a copy of the GNU Affero General Public License |
| 21 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 22 | +*/ |
| 23 | +using System; |
| 24 | +using System.Collections.Generic; |
| 25 | +using System.IO; |
| 26 | +using iText.Commons.Utils; |
| 27 | +using iText.IO.Source; |
| 28 | +using iText.Test; |
| 29 | + |
| 30 | +namespace iText.IO.Font { |
| 31 | + public class CFFFontSubsetIntegrationTest : ExtendedITextTest { |
| 32 | + private static readonly String SOURCE_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 33 | + .CurrentContext.TestDirectory) + "/resources/itext/io/font/CFFFontSubsetIntegrationTest/"; |
| 34 | + |
| 35 | + private static readonly String FONTS_FOLDER = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext |
| 36 | + .CurrentContext.TestDirectory) + "/resources/itext/io/font/sharedFontsResourceFiles/"; |
| 37 | + |
| 38 | + private static readonly String CJK_JP_BOLD_PATH = FONTS_FOLDER + "NotoSansCJKjp-Bold.otf"; |
| 39 | + |
| 40 | + private const int CJK_JP_BOLD_CFF_OFFSET = 259880; |
| 41 | + |
| 42 | + private const int CJK_JP_BOLD_CFF_LENGTH = 16023217; |
| 43 | + |
| 44 | + private static readonly String JP_REGULAR_PATH = FONTS_FOLDER + "NotoSansJP-Regular_charsetDataFormat0.otf"; |
| 45 | + |
| 46 | + private const int JP_REGULAR_CFF_OFFSET = 337316; |
| 47 | + |
| 48 | + private const int JP_REGULAR_CFF_LENGTH = 4210891; |
| 49 | + |
| 50 | + [NUnit.Framework.Test] |
| 51 | + public virtual void SubsetNotoSansCjkJpBoldNoUsedGlyphsTest() { |
| 52 | + String cmpCff = SOURCE_FOLDER + "subsetNotoSansCJKjpBoldNoUsedGlyphs.cff"; |
| 53 | + ICollection<int> glyphsUsed = JavaCollectionsUtil.EmptySet<int>(); |
| 54 | + byte[] cffSubsetBytes = SubsetNotoSansCjkJpBoldCff(CJK_JP_BOLD_PATH, CJK_JP_BOLD_CFF_OFFSET, CJK_JP_BOLD_CFF_LENGTH |
| 55 | + , glyphsUsed); |
| 56 | + int expectedSubsetLength = 279337; |
| 57 | + NUnit.Framework.Assert.AreEqual(expectedSubsetLength, cffSubsetBytes.Length); |
| 58 | + byte[] cmpBytes = File.ReadAllBytes(System.IO.Path.Combine(cmpCff)); |
| 59 | + NUnit.Framework.Assert.AreEqual(cmpBytes, cffSubsetBytes); |
| 60 | + } |
| 61 | + |
| 62 | + [NUnit.Framework.Test] |
| 63 | + public virtual void SubsetNotoSansCjkJpBoldTwoUsedGlyphsTest() { |
| 64 | + String cmpCff = SOURCE_FOLDER + "subsetNotoSansCJKjpBoldTwoUsedGlyphs.cff"; |
| 65 | + // In this case cid == gid for given characters. |
| 66 | + // \u20eab "𠺫" |
| 67 | + int glyphCid1 = 59715; |
| 68 | + // \uff14 "4" |
| 69 | + int glyphCid2 = 59066; |
| 70 | + HashSet<int> glyphsUsed = new HashSet<int>(JavaUtil.ArraysAsList(glyphCid1, glyphCid2)); |
| 71 | + byte[] cffSubsetBytes = SubsetNotoSansCjkJpBoldCff(CJK_JP_BOLD_PATH, CJK_JP_BOLD_CFF_OFFSET, CJK_JP_BOLD_CFF_LENGTH |
| 72 | + , glyphsUsed); |
| 73 | + int expectedSubsetLength = 365381; |
| 74 | + NUnit.Framework.Assert.AreEqual(expectedSubsetLength, cffSubsetBytes.Length); |
| 75 | + byte[] cmpBytes = File.ReadAllBytes(System.IO.Path.Combine(cmpCff)); |
| 76 | + NUnit.Framework.Assert.AreEqual(cmpBytes, cffSubsetBytes); |
| 77 | + } |
| 78 | + |
| 79 | + [NUnit.Framework.Test] |
| 80 | + public virtual void SubsetNotoSansJpRegularOneUsedGlyphTest() { |
| 81 | + // In this case cid != gid for given characters. |
| 82 | + // \u4FE1 "信"; gid: 0x0a72 / 2674 |
| 83 | + int glyphGid1 = 2674; |
| 84 | + HashSet<int> glyphsUsed = new HashSet<int>(JavaCollectionsUtil.SingletonList(glyphGid1)); |
| 85 | + byte[] cffSubsetBytes = SubsetNotoSansCjkJpBoldCff(JP_REGULAR_PATH, JP_REGULAR_CFF_OFFSET, JP_REGULAR_CFF_LENGTH |
| 86 | + , glyphsUsed); |
| 87 | + int expectedSubsetLength = 121796; |
| 88 | + NUnit.Framework.Assert.AreEqual(expectedSubsetLength, cffSubsetBytes.Length); |
| 89 | + byte[] cmpBytes = File.ReadAllBytes(System.IO.Path.Combine(SOURCE_FOLDER + "subsetNotoSansJPRegularOneUsedGlyph.cff" |
| 90 | + )); |
| 91 | + NUnit.Framework.Assert.AreEqual(cmpBytes, cffSubsetBytes); |
| 92 | + } |
| 93 | + |
| 94 | + private byte[] SubsetNotoSansCjkJpBoldCff(String otfFile, int offsetToCff, int cffLength, ICollection<int> |
| 95 | + glyphsUsed) { |
| 96 | + RandomAccessFileOrArray fontRaf = null; |
| 97 | + try { |
| 98 | + fontRaf = new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateBestSource(otfFile)); |
| 99 | + byte[] cff = new byte[cffLength]; |
| 100 | + try { |
| 101 | + fontRaf.Seek(offsetToCff); |
| 102 | + fontRaf.ReadFully(cff); |
| 103 | + } |
| 104 | + finally { |
| 105 | + fontRaf.Close(); |
| 106 | + } |
| 107 | + CFFFontSubset cffFontSubset = new CFFFontSubset(cff, glyphsUsed); |
| 108 | + return cffFontSubset.Process(); |
| 109 | + } |
| 110 | + finally { |
| 111 | + if (fontRaf != null) { |
| 112 | + fontRaf.Close(); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments