Skip to content

Commit

Permalink
Cleanups after PR #1082 - Coverage
Browse files Browse the repository at this point in the history
- rename check-Method in assert
- Codacy issues fixed
  • Loading branch information
asturio committed Mar 2, 2024
1 parent a4f95e8 commit 28cae90
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
11 changes: 6 additions & 5 deletions openpdf/src/main/java/com/lowagie/text/pdf/FontDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ class FontDetails {
*/
protected boolean subset = true;

/**
* Contain glyphs that used but missing in Cmap. the value is int[]{glyph, Unicode code}
*/
private Map<Integer, int[]> fillerCmap;


Map<Integer, int[]> getFillerCmap() {
return fillerCmap;
}
Expand All @@ -119,11 +125,6 @@ void putFillerCmap(Integer key, int[] value) {
fillerCmap.put(key, value);
}

/**
* Contain glyphs that used but missing in Cmap. the value is int[]{glyph, Unicode code}
*/
private Map<Integer, int[]> fillerCmap;

/**
* Each font used in a document has an instance of this class. This class stores the characters used in the document
* and other specifics unique to the current working document.
Expand Down
62 changes: 32 additions & 30 deletions openpdf/src/main/java/com/lowagie/text/pdf/TrueTypeFontUnicode.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
*
* @author Paulo Soares (psoares@consiste.pt)
*/
class TrueTypeFontUnicode extends TrueTypeFont implements Comparator {
class TrueTypeFontUnicode extends TrueTypeFont implements Comparator<int[]> {

private static final byte[] rotbits = {(byte) 0x80, (byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08, (byte) 0x04,
(byte) 0x02, (byte) 0x01};

/**
* <CODE>true</CODE> if the encoding is vertical.
Expand Down Expand Up @@ -115,7 +118,6 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator {
if ((cmap31 == null && !fontSpecific) || (cmap10 == null && fontSpecific)) {
directTextToByte = true;
}
//throw new DocumentException(MessageLocalization.getComposedMessage("1.2.does.not.contain.an.usable.cmap", fileName, style));
if (fontSpecific) {
fontSpecific = false;
String tempEncoding = encoding;
Expand All @@ -131,10 +133,11 @@ class TrueTypeFontUnicode extends TrueTypeFont implements Comparator {
vertical = enc.endsWith("V");
}

@Override
void readCMaps() throws DocumentException, IOException {
super.readCMaps();

Map cmap = null;
Map<Integer, int[]> cmap = null;
if (cmapExt != null) {
cmap = cmapExt;
} else if (cmap31 != null) {
Expand All @@ -143,10 +146,9 @@ void readCMaps() throws DocumentException, IOException {

if (cmap != null) {
inverseCmap = new HashMap<>();
for (Object o : cmap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
Integer code = (Integer) entry.getKey();
int[] metrics = (int[]) entry.getValue();
for (Map.Entry<Integer, int[]> entry : cmap.entrySet()) {
Integer code = entry.getKey();
int[] metrics = entry.getValue();
inverseCmap.put(metrics[0], code);
}
}
Expand All @@ -163,6 +165,7 @@ protected Integer getCharacterCode(int code) {
* @param char1 the unicode <CODE>char</CODE> to get the width of
* @return the width in normalized 1000 units
*/
@Override
public int getWidth(int char1) {
if (vertical) {
return 1000;
Expand All @@ -184,14 +187,14 @@ public int getWidth(int char1) {
* @param text the <CODE>String</CODE> to get the width of
* @return the width in normalized 1000 units
*/
@Override
public int getWidth(String text) {
if (vertical) {
return text.length() * 1000;
}
int total = 0;
if (fontSpecific) {
char[] cc = text.toCharArray();
int len = cc.length;
for (char c : cc) {
if ((c & 0xff00) == 0 || (c & 0xff00) == 0xf000) {
total += getRawWidth(c & 0xff, null);
Expand Down Expand Up @@ -284,8 +287,7 @@ private int[][] filterCmapMetrics(int[][] metrics) {
}

List<int[]> cmapMetrics = new ArrayList<>(metrics.length);
for (int[] metric1 : metrics) {
int[] metric = metric1;
for (int[] metric : metrics) {
// PdfContentByte.showText(GlyphVector) uses glyphs that might not
// map to a character.
// the glyphs are included in the metrics array, but we need to
Expand Down Expand Up @@ -355,8 +357,7 @@ private PdfDictionary getCIDFontType2(PdfIndirectReference fontDescriptor, Strin
StringBuilder buf = new StringBuilder("[");
int lastNumber = -10;
boolean firstTime = true;
for (int[] metric1 : metrics) {
int[] metric = metric1;
for (int[] metric : metrics) {
if (metric[1] == 1000) {
continue;
}
Expand Down Expand Up @@ -396,12 +397,9 @@ private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String su
// The PDF Reference manual advises to add -encoding to CID font names
if (cff) {
dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName + "-" + encoding));
}
//dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix+fontName));
else {
} else {
dic.put(PdfName.BASEFONT, new PdfName(subsetPrefix + fontName));
}
//dic.put(PdfName.BASEFONT, new PdfName(fontName));
dic.put(PdfName.ENCODING, new PdfName(encoding));
dic.put(PdfName.DESCENDANTFONTS, new PdfArray(descendant));
if (toUnicode != null) {
Expand All @@ -417,15 +415,12 @@ private PdfDictionary getFontBaseType(PdfIndirectReference descendant, String su
* @param o2 the second element
* @return the comparison
*/
public int compare(Object o1, Object o2) {
int m1 = ((int[]) o1)[0];
int m2 = ((int[]) o2)[0];
public int compare(int[] o1, int[] o2) {
int m1 = (o1)[0];
int m2 = (o2)[0];
return Integer.compare(m1, m2);
}

private static final byte[] rotbits = {(byte) 0x80, (byte) 0x40, (byte) 0x20, (byte) 0x10, (byte) 0x08, (byte) 0x04,
(byte) 0x02, (byte) 0x01};

/**
* Outputs to the writer the font dictionaries and streams.
*
Expand All @@ -443,7 +438,7 @@ void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) thro
addRangeUni(longTag, true, subset);
int[][] metrics = longTag.values().toArray(new int[0][]);
Arrays.sort(metrics, this);
PdfIndirectReference ind_font;
PdfIndirectReference indFont;
PdfObject pobj;
PdfIndirectObject obj;
PdfIndirectReference cidset = null;
Expand Down Expand Up @@ -473,7 +468,7 @@ void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) thro
}
pobj = new StreamFont(b, "CIDFontType0C", compressionLevel);
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
indFont = obj.getIndirectReference();
} else {
byte[] b;
if (subset || directoryOffset != 0) {
Expand All @@ -486,19 +481,19 @@ void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) thro
int[] lengths = new int[]{b.length};
pobj = new StreamFont(b, lengths, compressionLevel);
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
indFont = obj.getIndirectReference();
}
String subsetPrefix = "";
if (subset) {
subsetPrefix = createSubsetPrefix();
}
PdfDictionary dic = getFontDescriptor(ind_font, subsetPrefix, cidset);
PdfDictionary dic = getFontDescriptor(indFont, subsetPrefix, cidset);
obj = writer.addToBody(dic);
ind_font = obj.getIndirectReference();
indFont = obj.getIndirectReference();

pobj = getCIDFontType2(ind_font, subsetPrefix, metrics);
pobj = getCIDFontType2(indFont, subsetPrefix, metrics);
obj = writer.addToBody(pobj);
ind_font = obj.getIndirectReference();
indFont = obj.getIndirectReference();

pobj = getToUnicode(mergeMetricsAndFillerCmap(metrics, fillerCmap));
PdfIndirectReference toUnicodeRef = null;
Expand All @@ -508,7 +503,7 @@ void writeFont(PdfWriter writer, PdfIndirectReference ref, Object[] params) thro
toUnicodeRef = obj.getIndirectReference();
}

pobj = getFontBaseType(ind_font, subsetPrefix, toUnicodeRef);
pobj = getFontBaseType(indFont, subsetPrefix, toUnicodeRef);
writer.addToBody(pobj, ref);
}

Expand All @@ -532,6 +527,7 @@ public int[][] mergeMetricsAndFillerCmap(int[][] metric, Map<Integer, int[]> fil
* @return a PdfStream with the font program
* @since 2.1.3
*/
@Override
public PdfStream getFullFontStream() throws IOException, DocumentException {
if (cff) {
return new StreamFont(readCffFont(), "CIDFontType0C", compressionLevel);
Expand All @@ -545,10 +541,12 @@ public PdfStream getFullFontStream() throws IOException, DocumentException {
* @param text the text
* @return always <CODE>null</CODE>
*/
@Override
byte[] convertToBytes(String text) {
return null;
}

@Override
byte[] convertToBytes(int char1) {
return null;
}
Expand All @@ -559,6 +557,7 @@ byte[] convertToBytes(int char1) {
* @param c the character
* @return an <CODE>int</CODE> array with {glyph index, width}
*/
@Override
public int[] getMetricsTT(int c) {
if (cmapExt != null) {
return cmapExt.get(c);
Expand Down Expand Up @@ -590,6 +589,7 @@ public int[] getMetricsTT(int c) {
* @return <CODE>true</CODE> if the character has a glyph,
* <CODE>false</CODE> otherwise
*/
@Override
public boolean charExists(int c) {
return getMetricsTT(c) != null;
}
Expand All @@ -602,6 +602,7 @@ public boolean charExists(int c) {
* @return <CODE>true</CODE> if the advance was set,
* <CODE>false</CODE> otherwise
*/
@Override
public boolean setCharAdvance(int c, int advance) {
int[] m = getMetricsTT(c);
if (m == null) {
Expand All @@ -611,6 +612,7 @@ public boolean setCharAdvance(int c, int advance) {
return true;
}

@Override
public int[] getCharBBox(int c) {
if (bboxes == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private byte[] getLiberationFontByte() throws IOException {
*/
@Test
void includeCidSetTest() throws Exception {
checkCidSetPresence(true);
checkCidSetPresence(false);
assertCidSetPresence(true);
assertCidSetPresence(false);
}

private void checkCidSetPresence(boolean includeCidSet) throws Exception {
private void assertCidSetPresence(boolean includeCidSet) throws Exception {
byte[] documentBytes;
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Document document = new Document();
Expand Down

0 comments on commit 28cae90

Please sign in to comment.