Skip to content

Commit 9e503dd

Browse files
authored
Upgrade PDFBox API to v2.0.7 (TomRoush#329)
1 parent 4018549 commit 9e503dd

File tree

77 files changed

+1459
-716
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1459
-716
lines changed

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/font/PDFontTest.java

+93-26
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,25 @@
2525

2626
import java.io.ByteArrayOutputStream;
2727
import java.io.File;
28+
import java.io.FileInputStream;
2829
import java.io.FileOutputStream;
2930
import java.io.IOException;
30-
import java.io.InputStream;
3131
import java.io.OutputStream;
3232
import java.net.URISyntaxException;
3333

3434
import com.tom_roush.fontbox.ttf.TTFParser;
3535
import com.tom_roush.fontbox.ttf.TrueTypeFont;
3636
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader;
37+
import com.tom_roush.pdfbox.cos.COSName;
3738
import com.tom_roush.pdfbox.io.IOUtils;
3839
import com.tom_roush.pdfbox.pdmodel.PDDocument;
3940
import com.tom_roush.pdfbox.pdmodel.PDPage;
4041
import com.tom_roush.pdfbox.pdmodel.PDPageContentStream;
41-
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle;
42+
import com.tom_roush.pdfbox.pdmodel.font.encoding.WinAnsiEncoding;
4243
import com.tom_roush.pdfbox.rendering.PDFRenderer;
44+
import com.tom_roush.pdfbox.text.PDFTextStripper;
4345

46+
import org.junit.Assert;
4447
import org.junit.Before;
4548
import org.junit.Test;
4649

@@ -92,36 +95,100 @@ public void testPDFBox988() throws IOException, URISyntaxException
9295
}
9396

9497
/**
95-
* PDFBOX-3337: Test ability to reuse a TrueTypeFont for several PDFs to avoid parsing it over
96-
* and over again.
98+
* PDFBOX-3826: Test ability to reuse a TrueTypeFont created from a file or a stream for several
99+
* PDFs to avoid parsing it over and over again. Also check that full or partial embedding is
100+
* done, and do render and text extraction.
97101
*
98102
* @throws IOException
103+
* @throws URISyntaxException
99104
*/
100105
@Test
101-
public void testPDFBox3337() throws IOException
106+
public void testPDFBox3826() throws IOException, URISyntaxException
102107
{
103-
InputStream ttfStream = testContext.getAssets().open(
104-
"com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf");
105-
final TrueTypeFont ttf = new TTFParser ().parse (ttfStream);
108+
File fontFile = new File(testContext.getCacheDir(), "F001u_3_7j.pdf");
109+
OutputStream os = new FileOutputStream(fontFile);
110+
IOUtils.copy(testContext.getAssets().open("com/tom_roush/pdfbox/resources/ttf/LiberationSans-Regular.ttf"), os);
111+
os.close();
112+
113+
TrueTypeFont ttf1 = new TTFParser().parse(fontFile);
114+
testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf1), fontFile);
115+
ttf1.close();
116+
117+
TrueTypeFont ttf2 = new TTFParser().parse(new FileInputStream(fontFile));
118+
testPDFBox3826checkFonts(testPDFBox3826createDoc(ttf2), fontFile);
119+
ttf2.close();
120+
}
106121

107-
for (int i = 0; i < 2; ++i)
108-
{
109-
PDDocument doc = new PDDocument();
110-
111-
final PDPage page = new PDPage(PDRectangle.A4);
112-
doc.addPage(page);
113-
114-
PDPageContentStream cs = new PDPageContentStream(doc, page);
115-
PDFont font = PDType0Font.load(doc, ttf, true);
116-
cs.setFont(font, 10);
117-
cs.beginText();
118-
cs.showText("PDFBOX");
119-
cs.endText();
120-
cs.close();
121-
doc.save(new ByteArrayOutputStream());
122-
doc.close();
123-
}
122+
private void testPDFBox3826checkFonts(byte[] byteArray, File fontFile) throws IOException
123+
{
124+
PDDocument doc = PDDocument.load(byteArray);
125+
126+
PDPage page2 = doc.getPage(0);
127+
128+
// F1 = type0 subset
129+
PDType0Font fontF1 = (PDType0Font) page2.getResources().getFont(COSName.getPDFName("F1"));
130+
Assert.assertTrue(fontF1.getName().contains("+"));
131+
Assert.assertTrue(fontFile.length() > fontF1.getFontDescriptor().getFontFile2().toByteArray().length);
132+
133+
// F2 = type0 full embed
134+
PDType0Font fontF2 = (PDType0Font) page2.getResources().getFont(COSName.getPDFName("F2"));
135+
Assert.assertFalse(fontF2.getName().contains("+"));
136+
Assert.assertEquals(fontFile.length(), fontF2.getFontDescriptor().getFontFile2().toByteArray().length);
137+
138+
// F3 = tt full embed
139+
PDTrueTypeFont fontF3 = (PDTrueTypeFont) page2.getResources().getFont(COSName.getPDFName("F3"));
140+
Assert.assertFalse(fontF2.getName().contains("+"));
141+
Assert.assertEquals(fontFile.length(), fontF3.getFontDescriptor().getFontFile2().toByteArray().length);
142+
143+
new PDFRenderer(doc).renderImage(0);
144+
145+
PDFTextStripper stripper = new PDFTextStripper();
146+
String text = stripper.getText(doc);
147+
//Assert.assertEquals("testMultipleFontFileReuse1\r\ntestMultipleFontFileReuse2\r\ntestMultipleFontFileReuse3\r\n", text);
148+
149+
doc.close();
124150
}
125151

126-
// testPDFBox3747 skipped as its Windows specific
152+
private byte[] testPDFBox3826createDoc(TrueTypeFont ttf) throws IOException
153+
{
154+
PDDocument doc = new PDDocument();
155+
156+
PDPage page = new PDPage();
157+
doc.addPage(page);
158+
159+
// type 0 subset embedding
160+
PDFont font = PDType0Font.load(doc, ttf, true);
161+
PDPageContentStream cs = new PDPageContentStream(doc, page);
162+
163+
cs.beginText();
164+
cs.newLineAtOffset(10, 700);
165+
cs.setFont(font, 10);
166+
cs.showText("testMultipleFontFileReuse1");
167+
cs.endText();
168+
169+
// type 0 full embedding
170+
font = PDType0Font.load(doc, ttf, false);
171+
172+
cs.beginText();
173+
cs.newLineAtOffset(10, 650);
174+
cs.setFont(font, 10);
175+
cs.showText("testMultipleFontFileReuse2");
176+
cs.endText();
177+
178+
// tt full embedding but only WinAnsiEncoding
179+
font = PDTrueTypeFont.load(doc, ttf, WinAnsiEncoding.INSTANCE);
180+
181+
cs.beginText();
182+
cs.newLineAtOffset(10, 600);
183+
cs.setFont(font, 10);
184+
cs.showText("testMultipleFontFileReuse3");
185+
cs.endText();
186+
187+
cs.close();
188+
189+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
190+
doc.save(baos);
191+
doc.close();
192+
return baos.toByteArray();
193+
}
127194
}

library/src/androidTest/java/com/tom_roush/pdfbox/pdmodel/graphics/image/JPEGFactoryTest.java

+15
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ public void testCreateFromStream() throws IOException
8282
"pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpeg.jpg"));
8383
}
8484

85+
/*
86+
* Tests JPEGFactory#createFromStream(PDDocument document, InputStream
87+
* stream) with CMYK color JPEG file
88+
*/
89+
public void testCreateFromStreamCMYK() throws IOException
90+
{
91+
PDDocument document = new PDDocument();
92+
InputStream stream = testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpegcmyk.jpg");
93+
PDImageXObject ximage = JPEGFactory.createFromStream(document, stream);
94+
validate(ximage, 8, 343, 287, "jpg", PDDeviceRGB.INSTANCE.getName()); // TODO: PdfBox-Android
95+
96+
doWritePDF(document, ximage, testResultsDir, "jpegcmykstream.pdf");
97+
checkJpegStream(testResultsDir, "jpegcmykstream.pdf", testContext.getAssets().open("pdfbox/com/tom_roush/pdfbox/pdmodel/graphics/image/jpegcmyk.jpg"));
98+
}
99+
85100
/**
86101
* Tests JPEGFactory#createFromStream(PDDocument document, InputStream
87102
* stream) with gray JPEG file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
%!PS-Adobe-3.0 Resource-CMap
2+
%%DocumentNeededResources: ProcSet (CIDInit)
3+
%%IncludeResource: ProcSet (CIDInit)
4+
%%BeginResource: CMap (H)
5+
%%Title: (H Adobe Japan1 1)
6+
%%Version: 10.005
7+
%%Copyright: -----------------------------------------------------------
8+
%%Copyright: Copyright 1990-2015 Adobe Systems Incorporated.
9+
%%Copyright: All rights reserved.
10+
%%Copyright:
11+
%%Copyright: Redistribution and use in source and binary forms, with or
12+
%%Copyright: without modification, are permitted provided that the
13+
%%Copyright: following conditions are met:
14+
%%Copyright:
15+
%%Copyright: Redistributions of source code must retain the above
16+
%%Copyright: copyright notice, this list of conditions and the following
17+
%%Copyright: disclaimer.
18+
%%Copyright:
19+
%%Copyright: Redistributions in binary form must reproduce the above
20+
%%Copyright: copyright notice, this list of conditions and the following
21+
%%Copyright: disclaimer in the documentation and/or other materials
22+
%%Copyright: provided with the distribution.
23+
%%Copyright:
24+
%%Copyright: Neither the name of Adobe Systems Incorporated nor the names
25+
%%Copyright: of its contributors may be used to endorse or promote
26+
%%Copyright: products derived from this software without specific prior
27+
%%Copyright: written permission.
28+
%%Copyright:
29+
%%Copyright: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30+
%%Copyright: CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31+
%%Copyright: INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32+
%%Copyright: MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33+
%%Copyright: DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
34+
%%Copyright: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35+
%%Copyright: SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36+
%%Copyright: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37+
%%Copyright: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38+
%%Copyright: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39+
%%Copyright: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40+
%%Copyright: OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
41+
%%Copyright: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42+
%%Copyright: -----------------------------------------------------------
43+
%%EndComments
44+
45+
/CIDInit /ProcSet findresource begin
46+
47+
12 dict begin
48+
49+
begincmap
50+
51+
/CIDSystemInfo 3 dict dup begin
52+
/Registry (Adobe) def
53+
/Ordering (Japan1) def
54+
/Supplement 1 def
55+
end def
56+
57+
/CMapName /H def
58+
59+
/CMapVersion 10.005 def
60+
/CMapType 1 def
61+
62+
/UIDOffset 280 def
63+
/XUID [1 10 25335] def
64+
65+
/WMode 0 def
66+
67+
1 begincodespacerange
68+
<2121> <7E7E>
69+
endcodespacerange
70+
71+
100 begincidrange
72+
<2121> <217e> 633
73+
<2221> <222e> 727
74+
<223a> <2241> 741
75+
<224a> <2250> 749
76+
<225c> <226a> 756
77+
<2272> <2279> 771
78+
<227e> <227e> 779
79+
<2330> <2339> 780
80+
<2341> <235a> 790
81+
<2361> <237a> 816
82+
<2421> <2473> 842
83+
<2521> <2576> 925
84+
<2621> <2638> 1011
85+
<2641> <2658> 1035
86+
<2721> <2741> 1059
87+
<2751> <2771> 1092
88+
<2821> <2821> 7479
89+
<2822> <2822> 7481
90+
<2823> <2823> 7491
91+
<2824> <2824> 7495
92+
<2825> <2825> 7503
93+
<2826> <2826> 7499
94+
<2827> <2827> 7507
95+
<2828> <2828> 7523
96+
<2829> <2829> 7515
97+
<282a> <282a> 7531
98+
<282b> <282b> 7539
99+
<282c> <282c> 7480
100+
<282d> <282d> 7482
101+
<282e> <282e> 7494
102+
<282f> <282f> 7498
103+
<2830> <2830> 7506
104+
<2831> <2831> 7502
105+
<2832> <2832> 7514
106+
<2833> <2833> 7530
107+
<2834> <2834> 7522
108+
<2835> <2835> 7538
109+
<2836> <2836> 7554
110+
<2837> <2837> 7511
111+
<2838> <2838> 7526
112+
<2839> <2839> 7519
113+
<283a> <283a> 7534
114+
<283b> <283b> 7542
115+
<283c> <283c> 7508
116+
<283d> <283d> 7527
117+
<283e> <283e> 7516
118+
<283f> <283f> 7535
119+
<2840> <2840> 7545
120+
<3021> <307e> 1125
121+
<3121> <317e> 1219
122+
<3221> <327e> 1313
123+
<3321> <337e> 1407
124+
<3421> <347e> 1501
125+
<3521> <357e> 1595
126+
<3621> <367e> 1689
127+
<3721> <377e> 1783
128+
<3821> <387e> 1877
129+
<3921> <397e> 1971
130+
<3a21> <3a7e> 2065
131+
<3b21> <3b7e> 2159
132+
<3c21> <3c7e> 2253
133+
<3d21> <3d7e> 2347
134+
<3e21> <3e7e> 2441
135+
<3f21> <3f7e> 2535
136+
<4021> <407e> 2629
137+
<4121> <417e> 2723
138+
<4221> <427e> 2817
139+
<4321> <437e> 2911
140+
<4421> <447e> 3005
141+
<4521> <457e> 3099
142+
<4621> <467e> 3193
143+
<4721> <477e> 3287
144+
<4821> <487e> 3381
145+
<4921> <497e> 3475
146+
<4a21> <4a7e> 3569
147+
<4b21> <4b7e> 3663
148+
<4c21> <4c7e> 3757
149+
<4d21> <4d7e> 3851
150+
<4e21> <4e7e> 3945
151+
<4f21> <4f53> 4039
152+
<5021> <507e> 4090
153+
<5121> <517e> 4184
154+
<5221> <527e> 4278
155+
<5321> <537e> 4372
156+
<5421> <547e> 4466
157+
<5521> <557e> 4560
158+
<5621> <567e> 4654
159+
<5721> <577e> 4748
160+
<5821> <587e> 4842
161+
<5921> <597e> 4936
162+
<5a21> <5a7e> 5030
163+
<5b21> <5b7e> 5124
164+
<5c21> <5c7e> 5218
165+
<5d21> <5d7e> 5312
166+
<5e21> <5e7e> 5406
167+
<5f21> <5f7e> 5500
168+
<6021> <607e> 5594
169+
<6121> <617e> 5688
170+
<6221> <627e> 5782
171+
<6321> <637e> 5876
172+
endcidrange
173+
174+
18 begincidrange
175+
<6421> <647e> 5970
176+
<6521> <657e> 6064
177+
<6621> <667e> 6158
178+
<6721> <677e> 6252
179+
<6821> <687e> 6346
180+
<6921> <697e> 6440
181+
<6a21> <6a7e> 6534
182+
<6b21> <6b7e> 6628
183+
<6c21> <6c7e> 6722
184+
<6d21> <6d7e> 6816
185+
<6e21> <6e7e> 6910
186+
<6f21> <6f7e> 7004
187+
<7021> <707e> 7098
188+
<7121> <717e> 7192
189+
<7221> <727e> 7286
190+
<7321> <737e> 7380
191+
<7421> <7424> 7474
192+
<7425> <7426> 8284
193+
endcidrange
194+
endcmap
195+
CMapName currentdict /CMap defineresource pop
196+
end
197+
end
198+
199+
%%EndResource
200+
%%EOF

0 commit comments

Comments
 (0)