add Noto Sans CJK SC

This commit is contained in:
Jörg Prante 2023-08-01 18:06:59 +02:00
parent 134343ba47
commit b1e6a4dc9b
9 changed files with 131 additions and 3 deletions

View file

@ -6,12 +6,16 @@ public enum Fonts {
HELVETICA,
TIMES,
COURIER,
NOTOSANS;
NOTOSANS,
FREESANS;
public Font getFont(Document document) {
if ("notosans".equalsIgnoreCase(name())) {
return new NotoSansFont(document);
}
if ("freesans".equalsIgnoreCase(name())) {
return new FreeSansFont(document);
}
return BaseFont.valueOf(name());
}
}

View file

@ -0,0 +1,67 @@
package org.xbib.graphics.pdfbox.layout.font;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.xbib.graphics.pdfbox.layout.element.Document;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Objects;
public class NotoSansCJKFont implements Font {
private final PDDocument pdDocument;
private PDType0Font regular;
private PDType0Font bold;
private PDType0Font italic;
private PDType0Font bolditalic;
public NotoSansCJKFont(Document document) {
this.pdDocument = document.getPdDocument();
}
@Override
public PDFont getRegularFont() {
if (regular == null) {
regular = load("NotoSansCJKsc-Regular.ttf");
}
return regular;
}
@Override
public PDFont getBoldFont() {
if (bold == null) {
bold = load("NotoSansCJKsc-Bold.ttf");
}
return bold;
}
@Override
public PDFont getItalicFont() {
if (italic == null) {
italic = load("NotoSansCJKsc-Italic.ttf");
}
return italic;
}
@Override
public PDFont getBoldItalicFont() {
if (bolditalic == null) {
bolditalic = load("NotoSansCJKsc-BoldItalic.ttf");
}
return bolditalic;
}
private PDType0Font load(String resourceName) {
try {
return PDType0Font.load(pdDocument, Objects.requireNonNull(NotoSansCJKFont.class.getResourceAsStream(resourceName)), true);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View file

@ -0,0 +1,57 @@
package org.xbib.graphics.pdfbox.layout.test;
import org.junit.jupiter.api.Test;
import org.xbib.graphics.pdfbox.layout.element.Document;
import org.xbib.graphics.pdfbox.layout.element.PageFormats;
import org.xbib.graphics.pdfbox.layout.element.Paragraph;
import org.xbib.graphics.pdfbox.layout.font.Font;
import org.xbib.graphics.pdfbox.layout.font.NotoSansCJKFont;
import org.xbib.graphics.pdfbox.layout.font.NotoSansFont;
import org.xbib.graphics.pdfbox.layout.text.Indent;
import org.xbib.graphics.pdfbox.layout.text.SpaceUnit;
import java.io.FileOutputStream;
public class HelloNotoSansCJKFontTest {
@Test
public void testDocumentOne() throws Exception {
Document document = new Document(PageFormats.A4_PORTRAIT);
Font font = new NotoSansCJKFont(document);
Paragraph paragraph = new Paragraph();
paragraph.add(new Indent(32, SpaceUnit.pt));
paragraph.addMarkup("Hello Noto Regular\n", 12, font);
paragraph.addMarkup("*Hello Noto Bold*\n", 12, font);
paragraph.addMarkup("_Hello Noto Italic_\n", 12, font);
paragraph.addMarkup("*_Hello Noto Bold Italic_*\n", 12, font);
paragraph.addText("Hello Unicode Text: Zwrotki dla Dorotki : arcyksiążę fiołków\n", 12, font);
paragraph.addMarkup("Hello Unicode Markup: _Zwrotki dla Dorotki : arcyksiążę fiołków_\n", 12, font);
document.add(paragraph);
Paragraph anotherParagraph = new Paragraph();
anotherParagraph.add(new Indent(32, SpaceUnit.pt));
anotherParagraph.addMarkup("Hello Noto Regular\n", 12, font);
document.add(anotherParagraph);
document.render().save(new FileOutputStream("build/hellonotofontcjk.pdf")).close();
}
@Test
public void testDocumentTwo() throws Exception {
Document document = new Document(PageFormats.A4_PORTRAIT);
Font font = new NotoSansCJKFont(document);
Paragraph paragraph = new Paragraph();
paragraph.add(new Indent(32, SpaceUnit.pt));
paragraph.addMarkup("Hello Noto Regular 2\n", 12, font);
paragraph.addMarkup("*Hello Noto Bold*\n", 12, font);
paragraph.addMarkup("_Hello Noto Italic_\n", 12, font);
paragraph.addMarkup("*_Hello Noto Bold Italic_*\n", 12, font);
paragraph.addText("Hello Unicode Text: Zwrotki dla Dorotki : arcyksiążę fiołków\n", 12, font);
paragraph.addMarkup("Hello Unicode Markup: _Zwrotki dla Dorotki : arcyksiążę fiołków_\n", 12, font);
document.add(paragraph);
Paragraph anotherParagraph = new Paragraph();
anotherParagraph.add(new Indent(32, SpaceUnit.pt));
anotherParagraph.addMarkup("Hello Noto Regular 2\n", 12, font);
document.add(anotherParagraph);
document.render().save(new FileOutputStream("build/hellonotofontcjk2.pdf")).close();
}
}

View file

@ -10,7 +10,7 @@ import org.xbib.graphics.pdfbox.layout.text.Indent;
import org.xbib.graphics.pdfbox.layout.text.SpaceUnit;
import java.io.FileOutputStream;
public class HelloNotoFontTest {
public class HelloNotoSansFontTest {
@Test
public void testDocumentOne() throws Exception {

View file

@ -6,7 +6,7 @@ module org.xbib.graphics.pdfbox {
exports org.xbib.graphics.pdfbox.image;
exports org.xbib.graphics.pdfbox.paint;
requires transitive org.apache.pdfbox;
requires org.apache.fontbox;
requires transitive org.apache.fontbox;
requires transitive java.desktop;
requires java.logging;
}