Skip to content

Commit

Permalink
itext
Browse files Browse the repository at this point in the history
  • Loading branch information
witek committed Aug 20, 2019
1 parent cc98c56 commit 5a7423d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
6 changes: 3 additions & 3 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
<classpathentry exported="true" kind="lib" path="lib/httpcore-4.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/httpunit-1.6.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itext-asian-5.2.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itext-pdfa-5.5.6-1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itext-xtra-5.5.6.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itextpdf-5.5.6.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jaudiotagger-2.0.3.jar"/>
<classpathentry exported="true" kind="lib" path="lib/javax.mail.jar"/>
<classpathentry exported="true" kind="lib" path="lib/javax.servlet-api-3.1.0.jar"/>
Expand Down Expand Up @@ -75,6 +72,9 @@
<classpathentry exported="true" kind="lib" path="lib/gwt-servlet.jar"/>
<classpathentry exported="true" kind="lib" path="lib/gwt-user.jar"/>
<classpathentry exported="true" kind="lib" path="lib/validation-api-1.0.0.GA.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itext-pdfa-5.5.13.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itext-xtra-5.5.13.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/itextpdf-5.5.13.1.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="build/classes"/>
</classpath>
6 changes: 3 additions & 3 deletions download-dependencies.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ servistodownload httpunit-1.6.2.jar
servistodownload nekohtml-1.9.14.jar
servistodownload tidy.jar

mavendownload com/itextpdf itextpdf 5.5.6
mavendownload com/itextpdf itextpdf 5.5.13.1
mavendownload com/itextpdf itext-asian 5.2.0
mavendownload com/itextpdf itext-pdfa 5.5.6-1
mavendownload com/itextpdf itext-xtra 5.5.6
mavendownload com/itextpdf itext-pdfa 5.5.13.1
mavendownload com/itextpdf itext-xtra 5.5.13.1

mavendownload org jaudiotagger 2.0.3

Expand Down
64 changes: 41 additions & 23 deletions src/main/java/ilarkesto/integration/itext/Paragraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@
*/
package ilarkesto.integration.itext;

import ilarkesto.core.logging.Log;
import ilarkesto.pdf.AImage;
import ilarkesto.pdf.AParagraph;
import ilarkesto.pdf.AParagraphElement;
import ilarkesto.pdf.APdfElement;
import ilarkesto.pdf.FontStyle;
import ilarkesto.pdf.TextChunk;

import java.io.File;
import java.util.Set;

import com.google.gwt.dev.util.collect.HashSet;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
Expand All @@ -34,8 +28,18 @@
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;

import ilarkesto.core.logging.Log;
import ilarkesto.pdf.AImage;
import ilarkesto.pdf.AParagraph;
import ilarkesto.pdf.AParagraphElement;
import ilarkesto.pdf.APdfElement;
import ilarkesto.pdf.FontStyle;
import ilarkesto.pdf.TextChunk;

public class Paragraph extends AParagraph implements ItextElement {

public static boolean embedInternationalFonts = false;

private static Log log = Log.get(Paragraph.class);

Paragraph(APdfElement parent, FontStyle defaultFontStyle) {
Expand All @@ -46,12 +50,19 @@ public class Paragraph extends AParagraph implements ItextElement {
public Element[] createITextElements(Document document) {
com.itextpdf.text.Paragraph p = new com.itextpdf.text.Paragraph();
float maxSize = 0;
FontSelector fontSelector = new FontSelector();
Set<String> fontsAddedToSelector = new HashSet<String>();
for (AParagraphElement element : getElements()) {
if (element instanceof TextChunk) {
TextChunk textChunk = (TextChunk) element;
FontStyle fontStyle = textChunk.getFontStyle();

FontSelector fontSelector = createFontSelector(fontStyle.getFont(), fontStyle);
String fontIdentifier = fontStyle.getFont() + fontStyle.getSize() + fontStyle.getColor()
+ fontStyle.isItalic() + fontStyle.isBold();
if (!fontsAddedToSelector.contains(fontIdentifier)) {
addFont(fontSelector, fontStyle);
fontsAddedToSelector.add(fontIdentifier);
}

String text = textChunk.getText();
Phrase phrase = fontSelector.process(text);
Expand Down Expand Up @@ -112,23 +123,30 @@ private int createStyle(FontStyle fontStyle) {
return style;
}

public FontSelector createFontSelector(String preferredFont, FontStyle fontStyle) {
FontSelector selector = new FontSelector();
selector.addFont(createFont(preferredFont, BaseFont.IDENTITY_H, fontStyle));
public void addFont(FontSelector selector, FontStyle fontStyle) {
log.info("creating new font:", fontStyle);

// fallback from ilarkesto.jar
selector.addFont(createFont("fonts/HDZB_36.ttf", BaseFont.IDENTITY_H, fontStyle)); // embeddable
// chinese
selector.addFont(createFont(fontStyle.getFont(), BaseFont.IDENTITY_H, fontStyle));

// fallback from iTextAsian.jar
selector.addFont(createFont("STSong-Light", "UniGB-UCS2-H", fontStyle)); // simplified chinese
// selector.addFont(createFont("STSong-Light", BaseFont.IDENTITY_H, fontStyle)); // simplified chinese
selector.addFont(createFont("MHei-Medium", BaseFont.IDENTITY_H, fontStyle)); // traditional chinese
selector.addFont(createFont("HeiseiMin-W3", BaseFont.IDENTITY_H, fontStyle)); // japanese
selector.addFont(createFont("KozMinPro-Regular", BaseFont.IDENTITY_H, fontStyle)); // japanese
selector.addFont(createFont("HYGoThic-Medium", BaseFont.IDENTITY_H, fontStyle)); // korean
if (embedInternationalFonts) {
// embeddable chinese
selector.addFont(createFont("fonts/HDZB_36.ttf", BaseFont.IDENTITY_H, fontStyle));

return selector;
// fallback from iTextAsian.jar
selector.addFont(createFont("STSong-Light", "UniGB-UCS2-H", fontStyle)); // simplified chinese

// traditional chinese
selector.addFont(createFont("MHei-Medium", BaseFont.IDENTITY_H, fontStyle));

// japanese
selector.addFont(createFont("HeiseiMin-W3", BaseFont.IDENTITY_H, fontStyle));

// japanese
selector.addFont(createFont("KozMinPro-Regular", BaseFont.IDENTITY_H, fontStyle));

// korean
selector.addFont(createFont("HYGoThic-Medium", BaseFont.IDENTITY_H, fontStyle));
}
}

private Font createFont(String name, String encoding, FontStyle fontStyle) {
Expand Down

0 comments on commit 5a7423d

Please sign in to comment.