use of com.itextpdf.text.Chunk in project ASCIIGenome by dariober.
the class Pdf method ansiFileToPdfParagraphs.
/**
* Read ansi formatted file line by lone and convert each line to
* a iText paragraph.
* @throws IOException
* @throws InvalidColourException
*/
private List<Paragraph> ansiFileToPdfParagraphs(float fontSize) throws IOException, InvalidColourException {
List<Paragraph> paraList = new ArrayList<Paragraph>();
// Files.readAllBytes(Paths.get(this.ansiInput.toURI()));
byte[] encoded = this.ansiInput.getBytes();
String str = new String(encoded);
// Each string in this list is a sequence of a characters sharing the same formatting.
List<String> ansiList = Splitter.on("\033").splitToList(str);
Paragraph pdfLine = new Paragraph();
int nChars = 0;
int currentMax = 0;
for (String xv : ansiList) {
// if(xv.equals("[48;5;231m")){
// continue;
// }
BaseColor fgBaseCol = new BaseColor(this.xterm256ToColor(xv, false).getRGB());
BaseColor bgBaseCol = new BaseColor(this.xterm256ToColor(xv, true).getRGB());
if (this.extractAnsiCodes(xv).size() != 0) {
// This string begins with ansi sequence and the color has been extracted.
// So remove the ansi sequence at the beginnig and the end, we don't need them anymore
xv = xv.replaceAll("^.+?m", "");
}
for (int i = 0; i < xv.length(); i++) {
char c = xv.charAt(i);
nChars += 1;
Chunk chunk = new Chunk(c, new Font(Font.FontFamily.COURIER, fontSize, Font.NORMAL, fgBaseCol));
chunk.setBackground(bgBaseCol);
pdfLine.add(chunk);
if (c == '\n') {
// Newline found: Start a new parapgrah
paraList.add(pdfLine);
if (nChars > currentMax) {
currentMax = nChars;
}
nChars = 0;
pdfLine = new Paragraph();
}
}
}
Font f = new Font(Font.FontFamily.COURIER, fontSize);
Chunk chunk = new Chunk("X", f);
this.setMaxWidth((int) ((currentMax) * chunk.getWidthPoint()));
for (Paragraph p : paraList) {
p.setSpacingBefore(-3);
p.setSpacingAfter(-3);
}
this.setMaxHeight((int) Math.rint((paraList.size() + 1) * fontSize));
return paraList;
}
Aggregations