| 1 |
/* |
| 2 |
* This file is part of Sejda. |
| 3 |
* |
| 4 |
* Sejda is free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU Affero General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* Sejda is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU Affero General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Affero General Public License |
| 15 |
* along with Sejda. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
package org.sejda.impl.sambox.component; |
| 18 |
|
| 19 |
import java.awt.Color; |
| 20 |
import java.awt.Point; |
| 21 |
import java.awt.geom.Point2D; |
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
import org.junit.Test; |
| 25 |
import org.sejda.impl.sambox.util.FontUtils; |
| 26 |
import org.sejda.model.exception.TaskException; |
| 27 |
import org.sejda.model.exception.TaskIOException; |
| 28 |
import org.sejda.model.exception.UnsupportedTextException; |
| 29 |
import org.sejda.model.pdf.StandardType1Font; |
| 30 |
import org.sejda.sambox.cos.COSName; |
| 31 |
import org.sejda.sambox.pdmodel.PDDocument; |
| 32 |
import org.sejda.sambox.pdmodel.PDPage; |
| 33 |
import org.sejda.sambox.pdmodel.font.PDFont; |
| 34 |
import org.sejda.sambox.pdmodel.graphics.state.RenderingMode; |
| 35 |
|
| 36 |
import static org.sejda.core.service.TestUtils.assertPageText; |
| 37 |
import static org.sejda.impl.sambox.util.TestUtils.getTestDoc; |
| 38 |
|
| 39 |
|
| 40 |
public class PageTextWriterTest { |
| 41 |
|
| 42 |
private PDFont helvetica = FontUtils.getStandardType1Font(StandardType1Font.HELVETICA); |
| 43 |
|
| 44 |
private void write(String text) throws TaskException { |
| 45 |
PDDocument doc = new PDDocument(); |
| 46 |
PDPage page = new PDPage(); |
| 47 |
doc.addPage(page); |
| 48 |
PageTextWriter writer = new PageTextWriter(doc); |
| 49 |
writer.write(page, new Point(10, 10), text, helvetica, 10d, PageTextWriter.toPDColor(Color.RED), RenderingMode.FILL, true); |
| 50 |
} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void resolveTextAndFontsWhenTextRepeats() throws TaskException { |
| 54 |
write("123α456α789"); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void resolvedSpaceSeparately() throws TaskException { |
| 59 |
write("ab cd"); |
| 60 |
} |
| 61 |
|
| 62 |
@Test(expected = UnsupportedTextException.class) |
| 63 |
public void throwsWhenCharacterUnsupported() throws TaskException { |
| 64 |
write("\uFE0F"); |
| 65 |
} |
| 66 |
|
| 67 |
@Test |
| 68 |
public void brokenFontWithZeroWidthLetters() throws TaskIOException, IOException { |
| 69 |
PDDocument doc = getTestDoc("pdf/font-with-zero-widths.pdf"); |
| 70 |
|
| 71 |
PDFont font = doc.getPage(0).getResources().getFont(COSName.getPDFName("F1")); |
| 72 |
PDPage page = new PDPage(); |
| 73 |
PageTextWriter textWriter = new PageTextWriter(doc); |
| 74 |
textWriter.write(page, new Point2D.Double(10d, 10d), |
| 75 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 1234567890", font, 12d, Color.RED); |
| 76 |
|
| 77 |
assertPageText(page, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"); |
| 78 |
} |
| 79 |
} |