Skip to content

Commit

Permalink
Remove duplicate strings
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsteiner1984 committed Aug 14, 2024
1 parent f20a011 commit 26d9935
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 53 deletions.
6 changes: 5 additions & 1 deletion checkstyle-5.5.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,11 @@
<module name="CyclomaticComplexity">
<property name="max" value="22"/>
</module>


<module name="MultipleStringLiterals">
<property name="allowedDuplicates" value="2"/>
<property name="ignoreStringsRegexp" value='^((".{1,4}")|""|" *"|("\\u[0-9A-F]{4}"))$'/>
</module>
</module>
<!-- ===================================================================================================== -->

Expand Down
10 changes: 5 additions & 5 deletions src/java/org/apache/fop/render/pdf/pdfbox/MergeAnnotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void cloneAnnotParent(COSBase annot, PDFDictionary clonedAnnot, Collectio
pdfBoxAdapter.pdfDoc.registerTrailerObject(nextNode);
nextNode.put("Kids", new PDFArray());
nextNode.put("T", name);
nextNode.put("Parent", node);
nextNode.put(COSName.PARENT.getName(), node);
PDFArray kids = (PDFArray) node.get("Kids");
kids.add(nextNode);
}
Expand All @@ -88,7 +88,7 @@ private void insert(PDFDictionary parent, PDFDictionary clonedAnnot) throws IOEx
PDFArray kids = (PDFArray) parent.get("Kids");
kids.add(clonedAnnot);
} else {
PDFDictionary grandParent = (PDFDictionary) parent.get("Parent");
PDFDictionary grandParent = (PDFDictionary) parent.get(COSName.PARENT.getName());
PDFDictionary child = parent;
parent = new PDFDictionary();
if (grandParent != null) {
Expand All @@ -103,14 +103,14 @@ private void insert(PDFDictionary parent, PDFDictionary clonedAnnot) throws IOEx
}
}
child.remove("T");
child.put("Parent", parent);
child.put(COSName.PARENT.getName(), parent);
parent.put("Kids", new PDFArray(clonedAnnot, child));

}
if (getT(clonedAnnot).equals(getT(parent))) {
clonedAnnot.remove("T");
}
clonedAnnot.put("Parent", parent);
clonedAnnot.put(COSName.PARENT.getName(), parent);
}

private PDFDictionary findKid(String name, PDFDictionary node) throws IOException {
Expand All @@ -127,7 +127,7 @@ private PDFDictionary findKid(String name, PDFDictionary node) throws IOExceptio
private void addToTree(PDFDictionary clonedAnnot, List<String> nameList) throws IOException {
String tStr = getT(clonedAnnot);
nameList.add(0, tStr);
Object parent = clonedAnnot.get("Parent");
Object parent = clonedAnnot.get(COSName.PARENT.getName());
if (parent instanceof PDFDictionary) {
addToTree((PDFDictionary) parent, nameList);
} else if (!fields.containsKey(tStr)) {
Expand Down
14 changes: 8 additions & 6 deletions src/java/org/apache/fop/render/pdf/pdfbox/MergeCFFFonts.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
Expand Down Expand Up @@ -234,7 +235,7 @@ protected void createCFF() throws IOException {
writeBytes(cffReader.getHeader());

//Name Index
writeIndex(Arrays.asList(fileFont.getName().getBytes("UTF-8")));
writeIndex(Arrays.asList(fileFont.getName().getBytes(StandardCharsets.UTF_8)));

Offsets offsets = new Offsets();

Expand Down Expand Up @@ -343,7 +344,8 @@ protected void writeStringIndex() throws IOException {
//Write the String Index
boolean stdRange = range.containsKey(NUM_STANDARD_STRINGS);
if (!stringIndexData.isEmpty()) {
if (!strings.isEmpty() && !new String(stringIndexData.get(0), "UTF-8").equals(strings.get(0)) && stdRange) {
if (!strings.isEmpty()
&& !new String(stringIndexData.get(0), StandardCharsets.UTF_8).equals(strings.get(0)) && stdRange) {
//Keep strings in order as they are referenced from the TopDICT
for (int i = 0; i < stringIndexSize; i++) {
stringIndexData.add(stringIndexData.remove(0));
Expand All @@ -355,24 +357,24 @@ protected void writeStringIndex() throws IOException {
}
}
if (stdRange) {
stringIndexData.add(embeddedName.getBytes("UTF-8"));
stringIndexData.add(embeddedName.getBytes(StandardCharsets.UTF_8));
} else {
stringIndexData.add(0, embeddedName.getBytes("UTF-8"));
stringIndexData.add(0, embeddedName.getBytes(StandardCharsets.UTF_8));
}
writeIndex(stringIndexData);
} else {
String notice = (String)fileFont.getTopDict().get("Notice");
if (notice != null) {
writeIndex(Arrays.<byte[]>asList(notice.getBytes(PDFDocument.ENCODING),
embeddedName.getBytes("UTF-8")));
embeddedName.getBytes(StandardCharsets.UTF_8)));
} else {
List<byte[]> sindex = new ArrayList<byte[]>();
sindex.add(cffReader.getStringIndex().getData());
if (sindex.size() > 1) {
fallbackIndex = true;
writeIndex(sindex);
} else if (sindex.size() == 1) {
writeIndex(Arrays.asList(embeddedName.getBytes("UTF-8")));
writeIndex(Arrays.asList(embeddedName.getBytes(StandardCharsets.UTF_8)));
} else {
writeCard16(0);
}
Expand Down
3 changes: 2 additions & 1 deletion src/java/org/apache/fop/render/pdf/pdfbox/OTFSubSetFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -99,7 +100,7 @@ private byte[] writeROSEntry(DICTEntry dictEntry) throws IOException {
int sidAStringIndex = stringIndexData.size() + 390;
int sidB = dictEntry.getOperands().get(1).intValue();
if (sidB > 390) {
stringIndexData.add("Identity".getBytes("UTF-8"));
stringIndexData.add("Identity".getBytes(StandardCharsets.UTF_8));
}
int sidBStringIndex = stringIndexData.size() + 390;
byte[] cidEntryByteData = dictEntry.getByteData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void testStructTreeRootEntriesToCopy() throws IOException {
root.setItem(COSName.ROLE_MAP, mapRole);
modifier.structTreeRootEntriesToCopy(root);
structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
PDFDictionary baseRoot = (PDFDictionary) structTreeRoot.get("RoleMap");
PDFDictionary baseRoot = (PDFDictionary) structTreeRoot.get(COSName.ROLE_MAP.getName());
String test = baseRoot.get("Icon").toString();
String expected = "/Figure";
Assert.assertEquals(test, expected);
Expand All @@ -66,7 +66,7 @@ public void testStructTreeRootEntriesToCopy() throws IOException {
structTreeRoot.put("RoleMap", rootBaseRoleMap);
modifier.structTreeRootEntriesToCopy(root);
structTreeRoot = pdfDoc.getRoot().getStructTreeRoot();
PDFDictionary baseRoot2 = (PDFDictionary) structTreeRoot.get("RoleMap");
PDFDictionary baseRoot2 = (PDFDictionary) structTreeRoot.get(COSName.ROLE_MAP.getName());
PDFName nameIcon = (PDFName) baseRoot2.get("Icon");
PDFName myPara = (PDFName)baseRoot2.get("MyPara");
test = nameIcon.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,15 @@ public void testCFF2() throws Exception {
@Test
public void testTTCID() throws Exception {
PDDocument doc = PDFBoxAdapterTestCase.load(PDFBoxAdapterTestCase.TTCID1);
FOPPDFMultiByteFont mbfont = new FOPPDFMultiByteFont(getFont(doc, "C2_0"),
"ArialMT_Type0");
final String font = "ArialMT_Type0";
FOPPDFMultiByteFont mbfont = new FOPPDFMultiByteFont(getFont(doc, "C2_0"), font);
mbfont.addFont(getFont(doc, "C2_0"));
Assert.assertEquals(mbfont.mapChar('t'), 67);

PDDocument doc2 = PDFBoxAdapterTestCase.load(PDFBoxAdapterTestCase.TTCID2);
String name = mbfont.addFont(getFont(doc2, "C2_0"));
Assert.assertEquals(name, "ArialMT_Type0");
Assert.assertEquals(mbfont.getFontName(), "ArialMT_Type0");
Assert.assertEquals(name, font);
Assert.assertEquals(mbfont.getFontName(), font);
byte[] is = IOUtils.toByteArray(mbfont.getInputStream());
Assert.assertEquals(is.length, 38640);
doc.close();
Expand All @@ -128,16 +128,16 @@ public void testTTCID() throws Exception {

@Test
public void testTTSubset() throws Exception {
final String font = "TimesNewRomanPSMT_TrueType";
PDDocument doc = PDFBoxAdapterTestCase.load(PDFBoxAdapterTestCase.TTSubset1);
FOPPDFSingleByteFont mbfont = new FOPPDFSingleByteFont(getFont(doc, "R9"),
"TimesNewRomanPSMT_TrueType");
FOPPDFSingleByteFont mbfont = new FOPPDFSingleByteFont(getFont(doc, "R9"), font);
mbfont.addFont(getFont(doc, "R9"));
// Assert.assertEquals(mbfont.mapChar('t'), 116);

PDDocument doc2 = PDFBoxAdapterTestCase.load(PDFBoxAdapterTestCase.TTSubset2);
String name = mbfont.addFont(getFont(doc2, "R9"));
Assert.assertEquals(name, "TimesNewRomanPSMT_TrueType");
Assert.assertEquals(mbfont.getFontName(), "TimesNewRomanPSMT_TrueType");
Assert.assertEquals(name, font);
Assert.assertEquals(mbfont.getFontName(), font);
byte[] is = IOUtils.toByteArray(mbfont.getInputStream());
Assert.assertEquals(is.length, 41104);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

import org.junit.Assert;
import org.junit.Test;
Expand Down Expand Up @@ -90,7 +91,7 @@ private String pdfToPS(PDDocument doc, String pdf, String font, LazyFont lazyFon
GraphicContext gc = new GraphicContext();
g2d.setGraphicContext(gc);
g.paint(g2d, rect);
return stream.toString("UTF-8");
return stream.toString(StandardCharsets.UTF_8.name());
} finally {
doc.close();
}
Expand Down Expand Up @@ -137,14 +138,14 @@ public void testCheckImageMask() throws IOException, ImageException {
PDDocument doc = PDFBoxAdapterTestCase.load(pdf);
COSStream cosStream = new COSStream();
OutputStream outputStream = cosStream.createOutputStream();
outputStream.write("/Fm0 Do\n".getBytes("UTF-8"));
outputStream.write("/Fm0 Do\n".getBytes(StandardCharsets.UTF_8));
outputStream.close();
PDStream pdStream = new PDStream(cosStream);
doc.getPage(0).setContents(pdStream);

PDXObject form = doc.getPage(0).getResources().getXObject(COSName.getPDFName("Fm0"));
OutputStream formStream = form.getCOSObject().createOutputStream();
formStream.write("1 g".getBytes("UTF-8"));
formStream.write("1 g".getBytes(StandardCharsets.UTF_8));
formStream.close();

String ps = pdfToPS(doc, pdf, null, null);
Expand Down
Loading

0 comments on commit 26d9935

Please sign in to comment.