Skip to content

Commit

Permalink
add TrueTypeFontUnicode.includeCidSet flag (LibrePDF#1041)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyro23 committed Feb 12, 2024
1 parent 9251b32 commit 470664f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator{
boolean vertical = false;

Map<Integer, Integer> inverseCmap;

boolean includeCidSet = true;

/**
* Creates a new TrueType font addressed by Unicode characters. The font
Expand Down Expand Up @@ -396,7 +398,7 @@ void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) thro
PdfObject pobj = null;
PdfIndirectObject obj = null;
PdfIndirectReference cidset = null;
if (subset || writer.getPDFXConformance() == PdfWriter.PDFA1A || writer.getPDFXConformance() == PdfWriter.PDFA1B) {
if (includeCidSet || writer.getPDFXConformance() == PdfWriter.PDFA1A || writer.getPDFXConformance() == PdfWriter.PDFA1B) {
PdfStream stream;
if (metrics.length == 0) {
stream = new PdfStream(new byte[]{(byte)0x80});
Expand Down Expand Up @@ -541,4 +543,12 @@ public int[] getCharBBox(int c) {
return null;
return bboxes[m[0]];
}

public boolean isIncludeCidSet() {
return includeCidSet;
}

public void setIncludeCidSet(boolean includeCidSet) {
this.includeCidSet = includeCidSet;
}
}
16 changes: 8 additions & 8 deletions openpdf/src/test/java/com/lowagie/text/pdf/FontSubsetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,24 @@ private byte[] getFontByte(String fileName) throws IOException {
}

/*
* This test is to ensure creation of CIDSet dictionary when using a font subset (required for PDF/A compliance)
* This test is to ensure creation of CIDSet dictionary according to the includeCidSet flag
*/
@Test
public void subsetTest() throws Exception {
checkSubsetPresence(true);
checkSubsetPresence(false);
public void includeCidSetTest() throws Exception {
checkCidSetPresence(true);
checkCidSetPresence(false);
}

private void checkSubsetPresence(boolean subsetIncluded) throws Exception {
private void checkCidSetPresence(boolean includeCidSet) throws Exception {
byte[] documentBytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
Document document = new Document();
PdfWriter.getInstance(document, baos);
document.open();

BaseFont font = BaseFont.createFont("LiberationSerif-Regular.ttf", BaseFont.IDENTITY_H,
TrueTypeFontUnicode font = (TrueTypeFontUnicode) BaseFont.createFont("LiberationSerif-Regular.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED,true, getFontByte("fonts/liberation-serif/LiberationSerif-Regular.ttf"), null);
font.setSubset(subsetIncluded);
font.setIncludeCidSet(includeCidSet);
String text = "This is the test string.";
document.add(new Paragraph(text, new Font(font, 12)));
document.close();
Expand All @@ -91,7 +91,7 @@ private void checkSubsetPresence(boolean subsetIncluded) throws Exception {
PdfDictionary fd = dic.getAsDict(PdfName.FONTDESCRIPTOR);
if (PdfName.FONT.equals(type) && fd != null) {
PdfIndirectReference cidset = fd.getAsIndirectObject(PdfName.CIDSET);
assertEquals(subsetIncluded, cidset != null);
assertEquals(includeCidSet, cidset != null);
fontFound = true;
break;
}
Expand Down

0 comments on commit 470664f

Please sign in to comment.