Skip to content
Snippets Groups Projects
Commit 6fbf7f32 authored by srosse's avatar srosse
Browse files

OO-5067: try to adapt text to available characters in the PDF fonts

parent 934ab17e
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,7 @@ package org.olat.core.util.pdf;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
......@@ -124,7 +125,7 @@ public class PdfDocument {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(marginLeftRight, currentY);
currentContentStream.showText(text);
showTextToStream(text, currentContentStream);
currentContentStream.endText();
float leading = lineHeightFactory * fontSize;
......@@ -177,7 +178,7 @@ public class PdfDocument {
currentContentStream.setFont(textFont, fontSize);
currentContentStream.newLineAtOffset(marginLeftRight, currentY);
for (String line: lines) {
currentContentStream.showText(line);
showTextToStream(line, currentContentStream);
currentContentStream.newLineAtOffset(0, -leading);
currentY -= leading;
}
......@@ -224,6 +225,16 @@ public class PdfDocument {
.replace('\u2212', '-');
}
public static void showTextToStream(String text, PDPageContentStream stream) throws IOException {
String cleanedText = cleanString(text);
try {
stream.showText(cleanedText);
} catch (IllegalArgumentException e) {
stream.showText(Normalizer.normalize(cleanedText, Normalizer.Form.NFKD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+",""));
}
}
public void drawLine(float xStart, float yStart, float xEnd, float yEnd, float lineWidth)
throws IOException {
currentContentStream.setLineWidth(lineWidth);
......@@ -239,7 +250,7 @@ public class PdfDocument {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, texty);
currentContentStream.showText(text);
showTextToStream(text, currentContentStream);
currentContentStream.endText();
}
......
......@@ -354,7 +354,7 @@ public class CheckboxPDFExport extends PdfDocument implements MediaResource {
currentContentStream.setTextMatrix(Matrix.getRotateInstance(3 * (Math.PI / 2), textx + cellMargin, texty - cellMargin));
textx += colWidth;
}
currentContentStream.showText(cleanString(text));
showTextToStream(text, currentContentStream);
currentContentStream.endText();
}
......@@ -381,7 +381,7 @@ public class CheckboxPDFExport extends PdfDocument implements MediaResource {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, lineTexty);
currentContentStream.showText(textLine);
showTextToStream(textLine, currentContentStream);
currentContentStream.endText();
lineTexty -= (lineHeightFactory * fontSize);
}
......@@ -389,7 +389,7 @@ public class CheckboxPDFExport extends PdfDocument implements MediaResource {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, texty);
currentContentStream.showText(text);
showTextToStream(text, currentContentStream);
currentContentStream.endText();
}
}
......
......@@ -315,7 +315,7 @@ public class CheckedPDFExport extends PdfDocument implements MediaResource {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, texty - headerHeight + cellMargin);
currentContentStream.showText(cleanString(text));
showTextToStream(text, currentContentStream);
currentContentStream.endText();
textx += colWidth;
}
......
......@@ -511,13 +511,13 @@ public class LecturesBlockPDFExport extends PdfDocument implements MediaResource
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize - 2);
currentContentStream.newLineAtOffset(boxx + 2f, texty);
currentContentStream.showText(comment);
showTextToStream(comment, currentContentStream);
currentContentStream.endText();
} else {
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(boxx + 2f, texty);
currentContentStream.showText(comment);
showTextToStream(comment, currentContentStream);
currentContentStream.endText();
}
}
......
......@@ -288,7 +288,7 @@ public class LecturesBlockSignaturePDFExport extends PdfDocument implements Medi
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, lineTexty);
currentContentStream.showText(textLine);
showTextToStream(textLine, currentContentStream);
currentContentStream.endText();
lineTexty -= (lineHeightFactory * fontSize);
}
......@@ -296,7 +296,7 @@ public class LecturesBlockSignaturePDFExport extends PdfDocument implements Medi
currentContentStream.beginText();
currentContentStream.setFont(font, fontSize);
currentContentStream.newLineAtOffset(textx, texty);
currentContentStream.showText(text);
showTextToStream(text, currentContentStream);
currentContentStream.endText();
}
texty -= rowHeights[i];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment