diff --git a/gradle/quality/checkstyle-suppressions.xml b/gradle/quality/checkstyle-suppressions.xml new file mode 100644 index 0000000..ebee2dd --- /dev/null +++ b/gradle/quality/checkstyle-suppressions.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/gradle/quality/checkstyle.gradle b/gradle/quality/checkstyle.gradle index 85b8bd8..34714af 100644 --- a/gradle/quality/checkstyle.gradle +++ b/gradle/quality/checkstyle.gradle @@ -16,4 +16,5 @@ checkstyle { checkstyleMain { source = sourceSets.main.allSource } + configProperties = [ "suppressionFile" : rootProject.file('gradle/quality/checkstyle-suppressions.xml') ] } diff --git a/gradle/quality/checkstyle.xml b/gradle/quality/checkstyle.xml index 66a9aae..c04a805 100644 --- a/gradle/quality/checkstyle.xml +++ b/gradle/quality/checkstyle.xml @@ -329,5 +329,8 @@ page at http://checkstyle.sourceforge.net/config.html --> value="${com.puppycrawl.tools.checkstyle.checks.sizes.LineLength.ignorePattern}" default="^(package .*;\s*)|(import .*;\s*)|( *(\*|//).*https?://.*)$"/> - + + + + diff --git a/graphics-pdfbox-layout/Adobe-Source-LICENSE.txt b/graphics-pdfbox-layout/Adobe-Source-LICENSE.txt new file mode 100644 index 0000000..ddf7b7e --- /dev/null +++ b/graphics-pdfbox-layout/Adobe-Source-LICENSE.txt @@ -0,0 +1,96 @@ +Copyright 2014-2021 Adobe (http://www.adobe.com/), with Reserved Font +Name 'Source'. Source is a trademark of Adobe in the United States +and/or other countries. + +This Font Software is licensed under the SIL Open Font License, +Version 1.1. + +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font +creation efforts of academic and linguistic communities, and to +provide a free and open framework in which fonts may be shared and +improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply to +any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software +components as distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, +deleting, or substituting -- in part or in whole -- any of the +components of the Original Version, by changing formats or by porting +the Font Software to a new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, +modify, redistribute, and sell modified and unmodified copies of the +Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in +Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the +corresponding Copyright Holder. This restriction only applies to the +primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created using +the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/graphics-pdfbox-layout/Google-Noto-LICENSE.txt b/graphics-pdfbox-layout/Google-Noto-LICENSE.txt new file mode 100644 index 0000000..aac153a --- /dev/null +++ b/graphics-pdfbox-layout/Google-Noto-LICENSE.txt @@ -0,0 +1,45 @@ +SIL Open Font License + +Copyright 2012 Google Inc. All Rights Reserved. + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL + +—————————————————————————————- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +—————————————————————————————- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others. + +The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives. + +DEFINITIONS +“Font Software” refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation. + +“Reserved Font Name” refers to any names specified as such after the copyright statement(s). + +“Original Version” refers to the collection of Font Software components as distributed by the Copyright Holder(s). + +“Modified Version” refers to any derivative made by adding to, deleting, or substituting—in part or in whole—any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment. + +“Author” refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission. + +5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/BaseFont.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/BaseFont.java index 04c7c52..e05775f 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/BaseFont.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/BaseFont.java @@ -4,6 +4,8 @@ import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.font.Standard14Fonts; +import java.io.IOException; + import static org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName.COURIER_BOLD; import static org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName.COURIER_BOLD_OBLIQUE; import static org.apache.pdfbox.pdmodel.font.Standard14Fonts.FontName.COURIER_OBLIQUE; @@ -31,16 +33,16 @@ public enum BaseFont implements Font { HELVETICA(new PDType1Font(Standard14Fonts.FontName.HELVETICA), new PDType1Font(HELVETICA_BOLD), new PDType1Font(HELVETICA_OBLIQUE), new PDType1Font(HELVETICA_BOLD_OBLIQUE)); - private final PDFont plainFont; + private final PDType1Font regularFont; - private final PDFont boldFont; + private final PDType1Font boldFont; - private final PDFont italicFont; + private final PDType1Font italicFont; - private final PDFont boldItalicFont; + private final PDType1Font boldItalicFont; - BaseFont(PDFont plainFont, PDFont boldFont, PDFont italicFont, PDFont boldItalicFont) { - this.plainFont = plainFont; + BaseFont(PDType1Font regularFont, PDType1Font boldFont, PDType1Font italicFont, PDType1Font boldItalicFont) { + this.regularFont = regularFont; this.boldFont = boldFont; this.italicFont = italicFont; this.boldItalicFont = boldItalicFont; @@ -48,7 +50,7 @@ public enum BaseFont implements Font { @Override public PDFont getRegularFont() { - return plainFont; + return regularFont; } @Override @@ -65,4 +67,22 @@ public enum BaseFont implements Font { public PDFont getBoldItalicFont() { return boldItalicFont; } + + @Override + public boolean hasGlyph(int code) { + try { + return regularFont.hasGlyph(code); + } catch (IOException e) { + return false; + } + } + + @Override + public boolean canWrite(String string) { + try { + return regularFont.getStringWidth(string) > 0; + } catch (IOException e) { + return false; + } + } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Font.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Font.java index 3c90d67..3142d69 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Font.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Font.java @@ -11,4 +11,8 @@ public interface Font { PDFont getItalicFont(); PDFont getBoldItalicFont(); + + boolean hasGlyph(int code); + + boolean canWrite(String string); } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/FontDescriptor.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/FontDescriptor.java index a1acf2c..d643bc4 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/FontDescriptor.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/FontDescriptor.java @@ -10,13 +10,13 @@ public class FontDescriptor { private final Font font; - private final float size; + private float size; - private final boolean regular; + private boolean regular; - private final boolean bold; + private boolean bold; - private final boolean italic; + private boolean italic; public FontDescriptor(Font font, float size) { this(font, size, false, false); @@ -25,19 +25,33 @@ public class FontDescriptor { public FontDescriptor(Font font, float size, boolean bold, boolean italic) { this.font = font; this.size = size; - this.regular = !bold && !italic; this.bold = bold; this.italic = italic; + this.regular = !bold && !italic; } public Font getFont() { return font; } + public void setSize(float size) { + this.size = size; + } + public float getSize() { return size; } + public void setBold(boolean bold) { + this.bold = bold; + this.regular = !bold && !italic; + } + + public void setItalic(boolean italic) { + this.italic = italic; + this.regular = !bold && !italic; + } + public PDFont getSelectedFont() { if (regular) { return font.getRegularFont(); diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Fonts.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Fonts.java index f011de7..fd9e822 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Fonts.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/Fonts.java @@ -7,14 +7,22 @@ public enum Fonts { TIMES, COURIER, NOTOSANS, - FREESANS; + NOTOSANSCJK, + OPENSANS, + SOURCESANS; public Font getFont(Document document) { if ("notosans".equalsIgnoreCase(name())) { return new NotoSansFont(document); } - if ("freesans".equalsIgnoreCase(name())) { - return new FreeSansFont(document); + if ("notosanscjk".equalsIgnoreCase(name())) { + return new NotoSansCJKFont(document); + } + if ("opensans".equalsIgnoreCase(name())) { + return new OpenSansFont(document); + } + if ("sourcesans".equalsIgnoreCase(name())) { + return new SourceSansFont(document); } return BaseFont.valueOf(name()); } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansCJKFont.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansCJKFont.java index fe7db6c..b682050 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansCJKFont.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansCJKFont.java @@ -1,25 +1,29 @@ package org.xbib.graphics.pdfbox.layout.font; +import org.apache.fontbox.ttf.TTFParser; +import org.apache.fontbox.ttf.TrueTypeFont; +import org.apache.pdfbox.io.RandomAccessRead; +import org.apache.pdfbox.io.RandomAccessReadBuffer; 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.InputStream; import java.io.UncheckedIOException; -import java.util.Objects; public class NotoSansCJKFont implements Font { private final PDDocument pdDocument; - private PDType0Font regular; + private PDType0Font regularFont; - private PDType0Font bold; + private PDType0Font boldFont; - private PDType0Font italic; + private PDType0Font italicFont; - private PDType0Font bolditalic; + private PDType0Font boldItalicFont; public NotoSansCJKFont(Document document) { this.pdDocument = document.getPdDocument(); @@ -27,39 +31,61 @@ public class NotoSansCJKFont implements Font { @Override public PDFont getRegularFont() { - if (regular == null) { - regular = load("NotoSansCJKsc-Regular.ttf"); + if (regularFont == null) { + regularFont = load("NotoSansCJKsc-Regular.ttf"); } - return regular; + return regularFont; } @Override public PDFont getBoldFont() { - if (bold == null) { - bold = load("NotoSansCJKsc-Bold.ttf"); + if (boldFont == null) { + boldFont = load("NotoSansCJKsc-Regular.ttf"); } - return bold; + return boldFont; } @Override public PDFont getItalicFont() { - if (italic == null) { - italic = load("NotoSansCJKsc-Italic.ttf"); + if (italicFont == null) { + italicFont = load("NotoSansCJKsc-Regular.ttf"); } - return italic; + return italicFont; } @Override public PDFont getBoldItalicFont() { - if (bolditalic == null) { - bolditalic = load("NotoSansCJKsc-BoldItalic.ttf"); + if (boldItalicFont == null) { + boldItalicFont = load("NotoSansCJKsc-Regular.ttf"); + } + return boldItalicFont; + } + + @Override + public boolean hasGlyph(int code) { + try { + return regularFont.hasGlyph(code); + } catch (IOException e) { + return false; + } + } + + @Override + public boolean canWrite(String string) { + try { + return regularFont.getStringWidth(string) > 0; + } catch (IOException e) { + return false; } - return bolditalic; } private PDType0Font load(String resourceName) { try { - return PDType0Font.load(pdDocument, Objects.requireNonNull(NotoSansCJKFont.class.getResourceAsStream(resourceName)), true); + InputStream inputStream = NotoSansCJKFont.class.getResourceAsStream(resourceName); + TTFParser ttfParser = new TTFParser(); + RandomAccessRead randomAccessRead = new RandomAccessReadBuffer(inputStream); + TrueTypeFont openTypeFont = ttfParser.parse(randomAccessRead); + return PDType0Font.load(pdDocument, openTypeFont, true); } catch (IOException e) { throw new UncheckedIOException(e); } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansFont.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansFont.java index 79e46ca..bde422c 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansFont.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/NotoSansFont.java @@ -12,13 +12,13 @@ public class NotoSansFont implements Font { private final PDDocument pdDocument; - private PDType0Font regular; + private PDType0Font regularFont; - private PDType0Font bold; + private PDType0Font boldFont; - private PDType0Font italic; + private PDType0Font italicFont; - private PDType0Font bolditalic; + private PDType0Font boldItalicFont; public NotoSansFont(Document document) { this.pdDocument = document.getPdDocument(); @@ -26,34 +26,52 @@ public class NotoSansFont implements Font { @Override public PDFont getRegularFont() { - if (regular == null) { - regular = load("NotoSans-Regular.ttf"); + if (regularFont == null) { + regularFont = load("NotoSans-Regular.ttf"); } - return regular; + return regularFont; } @Override public PDFont getBoldFont() { - if (bold == null) { - bold = load("NotoSans-Bold.ttf"); + if (boldFont == null) { + boldFont = load("NotoSans-Bold.ttf"); } - return bold; + return boldFont; } @Override public PDFont getItalicFont() { - if (italic == null) { - italic = load("NotoSans-Italic.ttf"); + if (italicFont == null) { + italicFont = load("NotoSans-Italic.ttf"); } - return italic; + return italicFont; } @Override public PDFont getBoldItalicFont() { - if (bolditalic == null) { - bolditalic = load("NotoSans-BoldItalic.ttf"); + if (boldItalicFont == null) { + boldItalicFont = load("NotoSans-BoldItalic.ttf"); + } + return boldItalicFont; + } + + @Override + public boolean hasGlyph(int code) { + try { + return regularFont.hasGlyph(code); + } catch (IOException e) { + return false; + } + } + + @Override + public boolean canWrite(String string) { + try { + return regularFont.getStringWidth(string) > 0; + } catch (IOException e) { + return false; } - return bolditalic; } private PDType0Font load(String resourceName) { diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/OpenSansFont.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/OpenSansFont.java new file mode 100644 index 0000000..bd9effb --- /dev/null +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/OpenSansFont.java @@ -0,0 +1,85 @@ +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 OpenSansFont implements Font { + + private final PDDocument pdDocument; + + private PDType0Font regularFont; + + private PDType0Font boldFont; + + private PDType0Font italicFont; + + private PDType0Font boldItalicFont; + + public OpenSansFont(Document document) { + this.pdDocument = document.getPdDocument(); + } + + @Override + public PDFont getRegularFont() { + if (regularFont == null) { + regularFont = load("OpenSans-Regular.ttf"); + } + return regularFont; + } + + @Override + public PDFont getBoldFont() { + if (boldFont == null) { + boldFont = load("OpenSans-Bold.ttf"); + } + return boldFont; + } + + @Override + public PDFont getItalicFont() { + if (italicFont == null) { + italicFont = load("OpenSans-Italic.ttf"); + } + return italicFont; + } + + @Override + public PDFont getBoldItalicFont() { + if (boldItalicFont == null) { + boldItalicFont = load("OpenSans-BoldItalic.ttf"); + } + return boldItalicFont; + } + + @Override + public boolean hasGlyph(int code) { + try { + return regularFont.hasGlyph(code); + } catch (IOException e) { + return false; + } + } + + @Override + public boolean canWrite(String string) { + try { + return regularFont.getStringWidth(string) > 0; + } catch (IOException e) { + return false; + } + } + + private PDType0Font load(String resourceName) { + try { + return PDType0Font.load(pdDocument, Objects.requireNonNull(OpenSansFont.class.getResourceAsStream(resourceName)), true); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/SourceSansFont.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/SourceSansFont.java new file mode 100644 index 0000000..8ae2585 --- /dev/null +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/font/SourceSansFont.java @@ -0,0 +1,85 @@ +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 SourceSansFont implements Font { + + private final PDDocument pdDocument; + + private PDType0Font regularFont; + + private PDType0Font boldFont; + + private PDType0Font italicFont; + + private PDType0Font boldItalicFont; + + public SourceSansFont(Document document) { + this.pdDocument = document.getPdDocument(); + } + + @Override + public PDFont getRegularFont() { + if (regularFont == null) { + regularFont = load("SourceSans3-Regular.ttf"); + } + return regularFont; + } + + @Override + public PDFont getBoldFont() { + if (boldFont == null) { + boldFont = load("SourceSans3-Bold.ttf"); + } + return boldFont; + } + + @Override + public PDFont getItalicFont() { + if (italicFont == null) { + italicFont = load("SourceSans3-It.ttf"); + } + return italicFont; + } + + @Override + public PDFont getBoldItalicFont() { + if (boldItalicFont == null) { + boldItalicFont = load("SourceSans3-BoldIt.ttf"); + } + return boldItalicFont; + } + + @Override + public boolean hasGlyph(int code) { + try { + return regularFont.hasGlyph(code); + } catch (IOException e) { + return false; + } + } + + @Override + public boolean canWrite(String string) { + try { + return regularFont.getStringWidth(string) > 0; + } catch (IOException e) { + return false; + } + } + + private PDType0Font load(String resourceName) { + try { + return PDType0Font.load(pdDocument, Objects.requireNonNull(SourceSansFont.class.getResourceAsStream(resourceName)), true); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } +} diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java index 2b07b98..2430ae3 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/StyledText.java @@ -3,13 +3,16 @@ package org.xbib.graphics.pdfbox.layout.text; import org.xbib.graphics.pdfbox.layout.font.FontDescriptor; import java.awt.Color; import java.io.IOException; -import java.io.UncheckedIOException; +import java.util.logging.Level; +import java.util.logging.Logger; /** * Base class representing drawable text styled with font, size, color etc. */ public class StyledText implements TextFragment { + private static final Logger logger = Logger.getLogger(StyledText.class.getName()); + private final String text; private final FontDescriptor fontDescriptor; @@ -178,8 +181,9 @@ public class StyledText implements TextFragment { private static float getWidth(FontDescriptor fontDescriptor, String text) { try { return fontDescriptor.getSize() * fontDescriptor.getSelectedFont().getStringWidth(text) / 1000; - } catch (IOException e) { - throw new UncheckedIOException(e); + } catch (Exception e) { + logger.log(Level.WARNING, e.getMessage(), e); + return 0f; } } diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlowUtil.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlowUtil.java index 1c26736..67b1b9e 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlowUtil.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/text/TextFlowUtil.java @@ -65,7 +65,8 @@ public class TextFlowUtil { * @param linespacing the line spacing * @return the created text flow. */ - protected static TextFlow createTextFlow(Iterable parts, FontDescriptor descriptor, + protected static TextFlow createTextFlow(Iterable parts, + FontDescriptor descriptor, float linespacing, float rotation) { final TextFlow textFlow = new TextFlow(); textFlow.setLineSpacing(linespacing); diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Bold.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Bold.ttf new file mode 100644 index 0000000..4a5bc39 Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Bold.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-BoldItalic.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-BoldItalic.ttf new file mode 100644 index 0000000..8878a3e Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-BoldItalic.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Italic.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Italic.ttf new file mode 100644 index 0000000..e84f9ee Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Italic.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Regular.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Regular.ttf new file mode 100644 index 0000000..29e9e60 Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/OpenSans-Regular.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Bold.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Bold.ttf new file mode 100644 index 0000000..85693cc Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Bold.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-BoldIt.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-BoldIt.ttf new file mode 100644 index 0000000..1a83e3f Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-BoldIt.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-It.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-It.ttf new file mode 100644 index 0000000..8e0576f Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-It.ttf differ diff --git a/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Regular.ttf b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Regular.ttf new file mode 100644 index 0000000..c5dd0b8 Binary files /dev/null and b/graphics-pdfbox-layout/src/main/resources/org/xbib/graphics/pdfbox/layout/font/SourceSans3-Regular.ttf differ diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansCJKFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansCJKFontTest.java index b4cb467..90fcb3c 100644 --- a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansCJKFontTest.java +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansCJKFontTest.java @@ -20,16 +20,15 @@ public class HelloNotoSansCJKFontTest { 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); + paragraph.addMarkup("Hello Noto Sans CJK Regular\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans CJK Bold*\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); + paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\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); + anotherParagraph.addMarkup("Hello Noto Sans CJK Regular\n", 12, font); document.add(anotherParagraph); document.render().save(new FileOutputStream("build/hellonotofontcjk.pdf")).close(); } @@ -40,16 +39,15 @@ public class HelloNotoSansCJKFontTest { 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); + paragraph.addMarkup("Hello Noto Sans CJK Regular 2\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans CJK Bold*\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); + paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\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); + anotherParagraph.addMarkup("Hello Noto Sans CJK Regular 2\n", 12, font); document.add(anotherParagraph); document.render().save(new FileOutputStream("build/hellonotofontcjk2.pdf")).close(); } diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloOpenSansFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloOpenSansFontTest.java new file mode 100644 index 0000000..1f87943 --- /dev/null +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloOpenSansFontTest.java @@ -0,0 +1,58 @@ +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.OpenSansFont; +import org.xbib.graphics.pdfbox.layout.text.Indent; +import org.xbib.graphics.pdfbox.layout.text.SpaceUnit; + +import java.io.FileOutputStream; + +public class HelloOpenSansFontTest { + + @Test + public void testDocumentOne() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + Font font = new OpenSansFont(document); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Open Sans Regular\n", 12, font); + paragraph.addMarkup("*Hello Open Sans Bold*\n", 12, font); + paragraph.addMarkup("_Hello Open Sans Italic_\n", 12, font); + paragraph.addMarkup("*_Hello Open Sans 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); + //paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Open Sans Regular\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/helloopensansfont.pdf")).close(); + } + + @Test + public void testDocumentTwo() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + Font font = new OpenSansFont(document); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Open Sans Regular 2\n", 12, font); + paragraph.addMarkup("*Hello Open Sans Bold*\n", 12, font); + paragraph.addMarkup("_Hello Open Sans Italic_\n", 12, font); + paragraph.addMarkup("*_Hello Open Sans 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); + //paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Open Sans Regular 2\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/helloopensansfont2.pdf")).close(); + } + +} diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloSourceSansFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloSourceSansFontTest.java new file mode 100644 index 0000000..7ddce7e --- /dev/null +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloSourceSansFontTest.java @@ -0,0 +1,58 @@ +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.SourceSansFont; +import org.xbib.graphics.pdfbox.layout.text.Indent; +import org.xbib.graphics.pdfbox.layout.text.SpaceUnit; + +import java.io.FileOutputStream; + +public class HelloSourceSansFontTest { + + @Test + public void testDocumentOne() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + Font font = new SourceSansFont(document); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Source Sans Regular\n", 12, font); + paragraph.addMarkup("*Hello Source Sans Bold*\n", 12, font); + paragraph.addMarkup("_Hello Source Sans Italic_\n", 12, font); + paragraph.addMarkup("*_Hello Source Sans 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); + //paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Source Sans Regular\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellosourcesansfont.pdf")).close(); + } + + @Test + public void testDocumentTwo() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + Font font = new SourceSansFont(document); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Source Sans Regular 2\n", 12, font); + paragraph.addMarkup("*Hello Source Sans Bold*\n", 12, font); + paragraph.addMarkup("_Hello Source Sans Italic_\n", 12, font); + paragraph.addMarkup("*_Hello Source Sans 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); + //paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Source Sans Regular 2\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellosourcesansfont2.pdf")).close(); + } + +} diff --git a/graphics-pdfbox/src/main/java/module-info.java b/graphics-pdfbox/src/main/java/module-info.java index 7def632..4354548 100644 --- a/graphics-pdfbox/src/main/java/module-info.java +++ b/graphics-pdfbox/src/main/java/module-info.java @@ -7,6 +7,7 @@ module org.xbib.graphics.pdfbox { exports org.xbib.graphics.pdfbox.paint; requires transitive org.apache.pdfbox; requires transitive org.apache.fontbox; + requires transitive org.apache.pdfbox.io; requires transitive java.desktop; requires java.logging; }