| 1 |
/* |
| 2 |
* Created on 13/nov/2012 |
| 3 |
* Copyright 2011 by Andrea Vacondio ([email protected]). |
| 4 |
* |
| 5 |
* This file is part of the Sejda source code |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
package org.sejda.impl.sambox.util; |
| 21 |
|
| 22 |
import static org.hamcrest.Matchers.is; |
| 23 |
import static org.hamcrest.Matchers.not; |
| 24 |
import static org.hamcrest.Matchers.notNullValue; |
| 25 |
import static org.hamcrest.Matchers.nullValue; |
| 26 |
import static org.junit.Assert.assertEquals; |
| 27 |
import static org.junit.Assert.assertFalse; |
| 28 |
import static org.junit.Assert.assertNotNull; |
| 29 |
import static org.junit.Assert.assertNull; |
| 30 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 31 |
import static org.junit.Assert.assertTrue; |
| 32 |
import static org.sejda.impl.sambox.util.FontUtils.canDisplay; |
| 33 |
import static org.sejda.impl.sambox.util.FontUtils.fontOrFallback; |
| 34 |
import static org.sejda.impl.sambox.util.FontUtils.getStandardType1Font; |
| 35 |
import static org.sejda.impl.sambox.util.TestUtils.getTestDoc; |
| 36 |
|
| 37 |
import java.awt.Color; |
| 38 |
import java.awt.Point; |
| 39 |
import java.awt.Rectangle; |
| 40 |
import java.io.File; |
| 41 |
import java.io.IOException; |
| 42 |
import java.util.Arrays; |
| 43 |
import java.util.List; |
| 44 |
|
| 45 |
import org.apache.fontbox.ttf.TrueTypeFont; |
| 46 |
import org.junit.Test; |
| 47 |
import org.sejda.core.support.io.IOUtils; |
| 48 |
import org.sejda.impl.sambox.component.PDDocumentHandler; |
| 49 |
import org.sejda.impl.sambox.component.PageTextWriter; |
| 50 |
import org.sejda.impl.sambox.component.PdfTextExtractorByArea; |
| 51 |
import org.sejda.impl.sambox.component.TextWithFont; |
| 52 |
import org.sejda.io.SeekableSources; |
| 53 |
import org.sejda.model.encryption.NoEncryptionAtRest; |
| 54 |
import org.sejda.model.exception.TaskException; |
| 55 |
import org.sejda.model.exception.TaskIOException; |
| 56 |
import org.sejda.model.exception.UnsupportedTextException; |
| 57 |
import org.sejda.model.pdf.StandardType1Font; |
| 58 |
import org.sejda.sambox.cos.COSName; |
| 59 |
import org.sejda.sambox.input.PDFParser; |
| 60 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 61 |
import org.sejda.sambox.pdmodel.PDPage; |
| 62 |
import org.sejda.sambox.pdmodel.PDResources; |
| 63 |
import org.sejda.sambox.pdmodel.font.FontMappers; |
| 64 |
import org.sejda.sambox.pdmodel.font.FontMapping; |
| 65 |
import org.sejda.sambox.pdmodel.font.PDFont; |
| 66 |
import org.sejda.sambox.pdmodel.font.PDType1Font; |
| 67 |
import org.sejda.sambox.pdmodel.graphics.form.PDFormXObject; |
| 68 |
|
| 69 |
/** |
| 70 |
* @author Andrea Vacondio |
| 71 |
*/ |
| 72 |
public class FontUtilsTest { |
| 73 |
|
| 74 |
private static PDFont HELVETICA = FontUtils.HELVETICA; |
| 75 |
|
| 76 |
@Test |
| 77 |
public void testGetStandardType1Fontg() { |
| 78 |
assertEquals(PDType1Font.COURIER, getStandardType1Font(StandardType1Font.CURIER)); |
| 79 |
assertEquals(PDType1Font.COURIER_BOLD, getStandardType1Font(StandardType1Font.CURIER_BOLD)); |
| 80 |
assertEquals(PDType1Font.COURIER_BOLD_OBLIQUE, getStandardType1Font(StandardType1Font.CURIER_BOLD_OBLIQUE)); |
| 81 |
assertEquals(PDType1Font.COURIER_OBLIQUE, getStandardType1Font(StandardType1Font.CURIER_OBLIQUE)); |
| 82 |
assertEquals(PDType1Font.HELVETICA, getStandardType1Font(StandardType1Font.HELVETICA)); |
| 83 |
assertEquals(PDType1Font.HELVETICA_BOLD, getStandardType1Font(StandardType1Font.HELVETICA_BOLD)); |
| 84 |
assertEquals(PDType1Font.HELVETICA_BOLD_OBLIQUE, |
| 85 |
getStandardType1Font(StandardType1Font.HELVETICA_BOLD_OBLIQUE)); |
| 86 |
assertEquals(PDType1Font.HELVETICA_OBLIQUE, getStandardType1Font(StandardType1Font.HELVETICA_OBLIQUE)); |
| 87 |
assertEquals(PDType1Font.TIMES_BOLD, getStandardType1Font(StandardType1Font.TIMES_BOLD)); |
| 88 |
assertEquals(PDType1Font.TIMES_BOLD_ITALIC, getStandardType1Font(StandardType1Font.TIMES_BOLD_ITALIC)); |
| 89 |
assertEquals(PDType1Font.TIMES_ITALIC, getStandardType1Font(StandardType1Font.TIMES_ITALIC)); |
| 90 |
assertEquals(PDType1Font.TIMES_ROMAN, getStandardType1Font(StandardType1Font.TIMES_ROMAN)); |
| 91 |
assertEquals(PDType1Font.SYMBOL, getStandardType1Font(StandardType1Font.SYMBOL)); |
| 92 |
assertEquals(PDType1Font.ZAPF_DINGBATS, getStandardType1Font(StandardType1Font.ZAPFDINGBATS)); |
| 93 |
} |
| 94 |
|
| 95 |
private PDFont findFontFor(String s) { |
| 96 |
try { |
| 97 |
return FontUtils.findFontFor(new PDDocument(), s); |
| 98 |
} finally { |
| 99 |
FontUtils.clearLoadedFontCache(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void testCanDisplay() { |
| 105 |
assertTrue(canDisplay("Chuck", getStandardType1Font(StandardType1Font.HELVETICA))); |
| 106 |
assertFalse(canDisplay("कसौटी", getStandardType1Font(StandardType1Font.HELVETICA))); |
| 107 |
assertFalse(canDisplay("Chuck", null)); |
| 108 |
} |
| 109 |
|
| 110 |
@Test |
| 111 |
public void testFindFontFor() { |
| 112 |
assertNotNull(findFontFor("ทดสอบ")); // thai |
| 113 |
assertNotNull(findFontFor("αυτό είναι ένα τεστ")); // greek |
| 114 |
assertNotNull(findFontFor("വീട്")); // malayalam |
| 115 |
assertNotNull(findFontFor("मानक")); // hindi |
| 116 |
assertNotNull(findFontFor("జ")); // telugu |
| 117 |
assertNotNull(findFontFor("উ")); // bengali |
| 118 |
assertNotNull(findFontFor("עברית")); // hebrew |
| 119 |
assertNotNull(findFontFor("latin ąćęłńóśźż")); // latin |
| 120 |
assertNotNull(findFontFor("\uFFFD \u2997")); // symbols |
| 121 |
assertNotNull(findFontFor("Newlines\nare\r\nignored")); // newlines |
| 122 |
assertNotNull(findFontFor("\u2984 \u2583 \u2738 ☗⦄✸▃ ")); // symbols |
| 123 |
assertNotNull(findFontFor("ភាសាខ្មែរ")); // khmer |
| 124 |
assertNotNull(findFontFor("ጩ")); // ethiopic |
| 125 |
assertNotNull(findFontFor("پنجابی, ਪੰਜਾਬੀ")); // punjabi |
| 126 |
assertNotNull(findFontFor("தமிழ்")); // tamil |
| 127 |
assertNotNull(findFontFor("ગુજરાતી")); // gujarati |
| 128 |
assertNotNull(findFontFor("န\u103Aမာဘာသာ")); // myanmar |
| 129 |
assertNotNull(findFontFor("සිංහල")); // sinhalese |
| 130 |
assertNotNull(findFontFor("ᠮᠣᠩᠭᠣᠯ")); // mongolian |
| 131 |
assertNotNull(findFontFor("ಕನ್ನಡ")); // kannada |
| 132 |
assertNotNull(findFontFor("ଓଡ଼ିଆ ଭାଷା")); // oryia |
| 133 |
assertNotNull(findFontFor("ކުންފުނި")); // thaana |
| 134 |
|
| 135 |
// TODO: find a way to merge the armenian font into the big merged font with all others |
| 136 |
// so forms can be filled with latin/armenian mixed values |
| 137 |
// assertNotNull(findFontFor("Latin mixed with հայերէն")); |
| 138 |
} |
| 139 |
|
| 140 |
@Test |
| 141 |
public void fontForMultipleLanguagesInOneString() { |
| 142 |
assertNotNull(findFontFor( |
| 143 |
"န\u103Aမာဘာသာ සිංහල ગુજરાતી தமிழ் پنجابی, ਪੰਜਾਬੀ ਹਰਜੋਤ ਸਿੰਘ ភាសាខ្មែរጩ latin ąćęłńóśźż ทดสอบ വീട मानक हिन्दी ് జ উ ភាសាខ្មែរ עברית")); // all in one |
| 144 |
} |
| 145 |
|
| 146 |
@Test |
| 147 |
public void roundTripWriteAndRead() throws TaskException, IOException { |
| 148 |
List<String> strings = Arrays.asList("ଓଡ଼ିଆଭାଷା", "ગુજરાતી ਪੰਜਾਬੀ தமிழ்", "සිංහල", "န\u103Aမာဘာသာ", |
| 149 |
"هذا هو كل الحق إلى اليسار", "123 יתימאה ןחבמ", "032 ציר הורמון הגדילה", |
| 150 |
"This is الحق Mixed יתימאה ןחבמ", |
| 151 |
"ਹਰਜੋਤ ਸਿੰਘ ភាសាខ្មែរ latin ąćęłńóśźż ทดสอบ വീട मानक हिन्दी ് జ উ ☗⦄✸▃ ", "ציר הורמון הגדילה"); |
| 152 |
|
| 153 |
for (String str : strings) { |
| 154 |
PDDocument doc = new PDDocument(); |
| 155 |
PDPage page = new PDPage(); |
| 156 |
new PageTextWriter(doc).write(page, new Point(10, 10), str, |
| 157 |
getStandardType1Font(StandardType1Font.HELVETICA), 10.0d, Color.BLACK); |
| 158 |
doc.addPage(page); |
| 159 |
try (PDDocumentHandler handler = new PDDocumentHandler(doc)) { |
| 160 |
File tmp = IOUtils.createTemporaryBuffer(); |
| 161 |
handler.savePDDocument(tmp, NoEncryptionAtRest.INSTANCE); |
| 162 |
|
| 163 |
PDDocument doc2 = PDFParser.parse(SeekableSources.seekableSourceFrom(tmp)); |
| 164 |
String text = new PdfTextExtractorByArea().extractTextFromArea(doc2.getPage(0), |
| 165 |
new Rectangle(0, 0, 1000, 1000)); |
| 166 |
assertEquals(noWhitespace(str), noWhitespace(text)); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private String noWhitespace(String in) { |
| 172 |
return in.replaceAll("\\s", ""); |
| 173 |
} |
| 174 |
|
| 175 |
@Test |
| 176 |
public void testFontOrFallbackPositive() { |
| 177 |
assertEquals(HELVETICA, fontOrFallback("Chuck", HELVETICA, new PDDocument())); |
| 178 |
} |
| 179 |
|
| 180 |
@Test |
| 181 |
public void testFontOrFallbackNegative() { |
| 182 |
assertNotNull(fontOrFallback("कसौटी", HELVETICA, new PDDocument())); |
| 183 |
} |
| 184 |
|
| 185 |
@Test |
| 186 |
public void testFontOrFallbackNotFoundFallback() { |
| 187 |
assertNull(fontOrFallback("\u1B2A\u1B35\u1B31\u1B29\u1B2E\u1B36, \u1B29\u1B32\u1B29\u1B2E\u1B36", HELVETICA, |
| 188 |
new PDDocument())); |
| 189 |
} |
| 190 |
|
| 191 |
@Test |
| 192 |
public void testCaching() { |
| 193 |
PDDocument doc = new PDDocument(); |
| 194 |
PDFont expected = FontUtils.findFontFor(doc, "ทดสอบ"); |
| 195 |
assertNotNull(expected); |
| 196 |
|
| 197 |
PDFont actual = FontUtils.findFontFor(doc, "ทด"); |
| 198 |
assertTrue("Font is cached, same instance is returned", expected == actual); |
| 199 |
} |
| 200 |
|
| 201 |
@Test |
| 202 |
public void testCanDisplayThai() { |
| 203 |
assertThat(findFontFor("นี่คือการทดสอบ"), is(notNullValue())); |
| 204 |
} |
| 205 |
|
| 206 |
@Test |
| 207 |
public void canDisplayGeorgian() { |
| 208 |
assertNotNull(findFontFor("ქართული ენა")); |
| 209 |
} |
| 210 |
|
| 211 |
@Test |
| 212 |
public void testCanDisplayType0FontsThatDontThrow() throws TaskIOException, IOException { |
| 213 |
PDDocument doc = getTestDoc("pdf/2-up-sample.pdf"); |
| 214 |
|
| 215 |
PDResources res = doc.getPage(0).getResources(); |
| 216 |
PDFormXObject form = (PDFormXObject) res.getXObject(COSName.getPDFName("Form2")); |
| 217 |
PDResources formRes = form.getResources(); |
| 218 |
PDFont font = formRes.getFont(COSName.getPDFName("F0")); |
| 219 |
|
| 220 |
assertThat(font.getName(), is("Arial-BoldMT")); |
| 221 |
assertThat(FontUtils.canDisplay("Redacted out :)", font), is(false)); |
| 222 |
} |
| 223 |
|
| 224 |
@Test |
| 225 |
public void testLoadingFullFontFromSystemForSubsetFonts() throws TaskIOException, IOException { |
| 226 |
boolean isVerdanaAvailable = isFontAvailableOnSystem("Verdana"); |
| 227 |
if (!isVerdanaAvailable) { |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
PDDocument doc = getTestDoc("pdf/subset-font.pdf"); |
| 232 |
|
| 233 |
PDResources res = doc.getPage(0).getResources(); |
| 234 |
PDFormXObject form = (PDFormXObject) res.getXObject(COSName.getPDFName("Xf1")); |
| 235 |
PDResources formRes = form.getResources(); |
| 236 |
PDFont font = formRes.getFont(COSName.getPDFName("F1")); |
| 237 |
assertThat(font.getName(), is("PXAAAA+Verdana")); |
| 238 |
|
| 239 |
PDFont original = new FontUtils.FontSubsetting(font).loadOriginal(doc); |
| 240 |
// relies on Verdana font being present on the system |
| 241 |
assertThat(original.getName(), is("Verdana")); |
| 242 |
} |
| 243 |
|
| 244 |
private boolean isFontAvailableOnSystem(String name) { |
| 245 |
FontMapping<TrueTypeFont> result = FontMappers.instance().getTrueTypeFont(name, null); |
| 246 |
return result != null && !result.isFallback(); |
| 247 |
} |
| 248 |
|
| 249 |
@Test |
| 250 |
public void resolveFontsWhenTextRepeats() { |
| 251 |
PDDocument doc = new PDDocument(); |
| 252 |
List<TextWithFont> textAndFonts = FontUtils.resolveFonts("123α456α789", HELVETICA, doc); |
| 253 |
|
| 254 |
assertThat(textAndFonts.get(0).getFont().getName(), is("Helvetica")); |
| 255 |
assertThat(textAndFonts.get(0).getText(), is("123")); |
| 256 |
|
| 257 |
assertThat(textAndFonts.get(1).getFont().getName(), is(not("Helvetica"))); |
| 258 |
assertThat(textAndFonts.get(1).getText(), is("α")); |
| 259 |
|
| 260 |
assertThat(textAndFonts.get(2).getFont().getName(), is("Helvetica")); |
| 261 |
assertThat(textAndFonts.get(2).getText(), is("456")); |
| 262 |
|
| 263 |
assertThat(textAndFonts.get(3).getFont().getName(), is(not("Helvetica"))); |
| 264 |
assertThat(textAndFonts.get(3).getText(), is("α")); |
| 265 |
} |
| 266 |
|
| 267 |
@Test |
| 268 |
public void resolveFontsWhenSpaceIsNotSeparately() { |
| 269 |
PDDocument doc = new PDDocument(); |
| 270 |
List<TextWithFont> textAndFonts = FontUtils.resolveFonts("ab cd", HELVETICA, doc); |
| 271 |
|
| 272 |
assertThat(textAndFonts.get(0).getFont().getName(), is("Helvetica")); |
| 273 |
assertThat(textAndFonts.get(0).getText(), is("ab cd")); |
| 274 |
} |
| 275 |
|
| 276 |
@Test |
| 277 |
public void resolveFontsWhenUnsupportedCharacters() { |
| 278 |
PDDocument doc = new PDDocument(); |
| 279 |
List<TextWithFont> textAndFonts = FontUtils.resolveFonts("ab\uFE0Fcd", HELVETICA, doc); |
| 280 |
|
| 281 |
assertThat(textAndFonts.get(1).getFont(), is(nullValue())); |
| 282 |
assertThat(textAndFonts.get(1).getText(), is("\uFE0F")); |
| 283 |
|
| 284 |
assertThat(FontUtils.removeUnsupportedCharacters("ab \uFE0Fcd", doc), is("ab cd")); |
| 285 |
} |
| 286 |
|
| 287 |
@Test |
| 288 |
public void removeUnsupportedCharsDoesNotChangeStringForRTLLanguages() { |
| 289 |
PDDocument doc = new PDDocument(); |
| 290 |
String text = "עברית"; |
| 291 |
assertEquals(text, FontUtils.removeUnsupportedCharacters(text, doc)); |
| 292 |
} |
| 293 |
|
| 294 |
@Test |
| 295 |
public void wrapping_Lines() throws TaskIOException { |
| 296 |
PDDocument doc = new PDDocument(); |
| 297 |
List<String> lines = FontUtils.wrapLines( |
| 298 |
"This is a long line that cannot fit on a single line and could be wrapped", HELVETICA, 10, 191, doc); |
| 299 |
assertThat(lines, |
| 300 |
is(Arrays.asList("This is a long line that cannot fit on a", "single line and could be wrapped"))); |
| 301 |
} |
| 302 |
|
| 303 |
@Test |
| 304 |
public void removingUnsupportedCharacters() { |
| 305 |
PDDocument doc = new PDDocument(); |
| 306 |
String str = FontUtils.removeUnsupportedCharacters("ͷ͵ͲͲͲ Text here SǦͳͻǦʹͳͲͻ4 and here", doc); |
| 307 |
assertThat(str, is("͵ Text here SǦͻǦʹͻ4 and here")); |
| 308 |
} |
| 309 |
|
| 310 |
@Test |
| 311 |
public void wrapping_Lines_Without_Word_Break() throws TaskIOException { |
| 312 |
PDDocument doc = new PDDocument(); |
| 313 |
List<String> lines = FontUtils.wrapLines( |
| 314 |
"This_is_a_long_line_that_cannot_fit_on_a_single_line_and_could_be_wrapped", HELVETICA, 10, 191, doc); |
| 315 |
assertThat(lines, |
| 316 |
is(Arrays.asList("This_is_a_long_line_that_cannot_fit_on_a-", "_single_line_and_could_be_wrapped"))); |
| 317 |
} |
| 318 |
|
| 319 |
@Test |
| 320 |
public void wrapping_Lines_Without_Word_Break_Or_Other_Delimiters() throws TaskIOException { |
| 321 |
PDDocument doc = new PDDocument(); |
| 322 |
List<String> lines = FontUtils.wrapLines("Thisisalonglinethatcannotfitonasinglelineandcouldbewrapped", |
| 323 |
HELVETICA, 10, 191, doc); |
| 324 |
assertThat(lines, is(Arrays.asList("Thisisalonglinethatcannotfitonasinglelinean-", "dcouldbewrapped"))); |
| 325 |
} |
| 326 |
|
| 327 |
@Test |
| 328 |
public void wrapping_Lines_Words_Mixed_With_Super_Long_Words() throws TaskIOException { |
| 329 |
PDDocument doc = new PDDocument(); |
| 330 |
List<String> lines = FontUtils.wrapLines("This is a long linethatcannotfitonasinglelineandcouldbe wrapped", |
| 331 |
HELVETICA, 10, 191, doc); |
| 332 |
assertThat(lines, is(Arrays.asList("This is a long linethatcannotfitonasingleline-", "andcouldbe wrapped"))); |
| 333 |
} |
| 334 |
|
| 335 |
@Test(expected = UnsupportedTextException.class) |
| 336 |
public void wrappingLinesUnableToFindFont() throws TaskIOException { |
| 337 |
PDDocument doc = new PDDocument(); |
| 338 |
FontUtils.wrapLines("This_is_a_long_line_that_cannot_fit_on_a_single_line_and_could_be_wrapped_հայերէն", |
| 339 |
HELVETICA, 10, 191, doc); |
| 340 |
} |
| 341 |
|
| 342 |
@Test |
| 343 |
public void brokenFontWithZeroWidthLetters() throws TaskIOException, IOException { |
| 344 |
PDDocument doc = getTestDoc("pdf/font-with-zero-widths.pdf"); |
| 345 |
PDFont font = doc.getPage(0).getResources().getFont(COSName.getPDFName("F1")); |
| 346 |
List<String> result = FontUtils.resolveTextFragments("FRIDA", font); |
| 347 |
assertThat(result, is(Arrays.asList("F", "RIDA"))); |
| 348 |
} |
| 349 |
|
| 350 |
@Test |
| 351 |
public void vectorFontWithSpace() throws TaskIOException, IOException { |
| 352 |
PDDocument doc = getTestDoc("pdf/font-with-vector-font.pdf"); |
| 353 |
PDFont font = doc.getPage(0).getResources().getFont(COSName.getPDFName("F1")); |
| 354 |
|
| 355 |
assertThat(FontUtils.canDisplaySpace(font), is(true)); |
| 356 |
|
| 357 |
List<TextWithFont> result = FontUtils.resolveFonts("31 days", font, doc); |
| 358 |
assertThat(result.get(0).getText(), is("31 days")); |
| 359 |
assertThat(result.get(0).getFont(), is(font)); |
| 360 |
} |
| 361 |
} |