working on fonts in pdfbox layout documents

This commit is contained in:
Jörg Prante 2023-08-02 17:57:52 +02:00
parent b1e6a4dc9b
commit ba2641b774
27 changed files with 680 additions and 68 deletions

View file

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suppressions PUBLIC
"-//Puppy Crawl//DTD Suppressions 1.1//EN"
"http://www.puppycrawl.com/dtds/suppressions_1_1.dtd">
<suppressions>
<!-- For Debug Only -->
<!-- <suppress files=".+\.(?:txt|xml|csv|sh|thrift|html|sql|eot|ttf|woff|css|png|java|jar|zip|war|class|tar|bin|gif|jpg|jpeg|properties)$" checks=".*"/> -->
<!-- Suppressions for Javadoc in Test classes -->
<suppress checks="JavadocMethod" files="src[/\\]test[/\\]java"/>
<suppress checks="JavadocType" files="src[/\\]test[/\\]java"/>
<!-- Suppressions for resources -->
<suppress checks=".*" files="src[/\\]main[/\\]resources" />
<!-- Suppressions for generated source files -->
<suppress checks=".*" files="build[/\\]" />
<!-- Disables Javadoc requirement for all public methods. This suppression
is not recommended, but may be needed (temporarily) to allow checkstyle
to be applied to existing codebases. -->
<suppress checks="JavadocMethod" files="src[/\\]main[/\\]java"/>
<!-- No tab characters ('\t') allowed in the source code -->
<suppress checks="FileTabCharacter" files="src"/>
<!-- No Trailing Whitespace, except on lines that only have an asterisk (e.g. Javadoc comments) -->
<suppress checks="RegexpSingleline" files=".*\.java"/>
<!-- Maximum line length is 120 characters -->
<suppress checks="LineLength" files=".*\.java"/>
<!-- Highlight any TODO or FIXME comments in info messages -->
<suppress checks="TodoComment" files=".*\.java"/>
<!-- ##### Import statement requirements ##### -->
<!-- Star imports (e.g. import java.util.*) are NOT ALLOWED -->
<suppress checks="AvoidStarImport" files=".*\.java"/>
<!-- Redundant import statements are NOT ALLOWED -->
<suppress checks="RedundantImport" files=".*\.java"/>
<!-- Unused import statements are NOT ALLOWED -->
<suppress checks="UnusedImports" files=".*\.java"/>
<!-- Ensure imports appear alphabetically and grouped -->
<suppress checks="CustomImportOrder" files=".*\.java"/>
<!-- ##### Requirements for K&R Style braces ##### -->
<!-- Code blocks MUST HAVE braces, even single line statements (if, while, etc) -->
<suppress checks="NeedBraces" files=".*\.java"/>
<!-- Left braces should be at the end of current line (default value)-->
<suppress checks="LeftCurly" files=".*\.java"/>
<!-- Right braces should be on start of a new line (default value) -->
<suppress checks="RightCurly" files=".*\.java"/>
<!-- ##### Indentation / Whitespace requirements ##### -->
<!-- Require 4-space indentation (default value) -->
<suppress checks="Indentation" files=".*\.java"/>
<!-- Whitespace should exist around all major tokens -->
<suppress checks="WhitespaceAround" files=".*\.java"/>
<!-- Validate whitespace around Generics (angle brackets) per typical conventions
http://checkstyle.sourceforge.net/config_whitespace.html#GenericWhitespace -->
<suppress checks="GenericWhitespace" files=".*\.java"/>
<!-- ##### Blank line requirements ##### -->
<suppress checks="EmptyLineSeparator" files=".*\.java"/>
<!-- ##### Requirements for "switch" statements ##### -->
<!-- "switch" statements MUST have a "default" clause -->
<suppress checks="MissingSwitchDefault" files=".*\.java"/>
<!-- "case" clauses in switch statements MUST include break, return, throw or continue -->
<suppress checks="FallThrough" files=".*\.java"/>
<!-- ##### Other / Miscellaneous requirements ##### -->
<!-- Require utility classes do not have a public constructor -->
<suppress checks="HideUtilityClassConstructor" files=".*\.java"/>
<!-- Require each variable declaration is its own statement on its own line -->
<suppress checks="MultipleVariableDeclarations" files=".*\.java"/>
<!-- Each line of code can only include one statement -->
<suppress checks="OneStatementPerLine" files=".*\.java"/>
<!-- Require that "catch" statements are not empty (must at least contain a comment) -->
<suppress checks="EmptyCatchBlock" files=".*\.java"/>
<!-- Require that 'final' used on variables and parameters -->
<suppress checks="FinalLocalVariable" files=".*.java"/>
<suppress checks="FinalParameters" files=".*.java"/>
</suppressions>

View file

@ -16,4 +16,5 @@ checkstyle {
checkstyleMain {
source = sourceSets.main.allSource
}
configProperties = [ "suppressionFile" : rootProject.file('gradle/quality/checkstyle-suppressions.xml') ]
}

View file

@ -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?://.*)$"/>
</module>
</module>
<module name="SuppressionFilter">
<property name="file" value="${suppressionFile}" default="checkstyle-suppressions.xml"/>
</module>
</module>

View file

@ -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.

View file

@ -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.

View file

@ -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;
}
}
}

View file

@ -11,4 +11,8 @@ public interface Font {
PDFont getItalicFont();
PDFont getBoldItalicFont();
boolean hasGlyph(int code);
boolean canWrite(String string);
}

View file

@ -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();

View file

@ -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());
}

View file

@ -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);
}

View file

@ -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) {

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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;
}
}

View file

@ -65,7 +65,8 @@ public class TextFlowUtil {
* @param linespacing the line spacing
* @return the created text flow.
*/
protected static TextFlow createTextFlow(Iterable<CharSequence> parts, FontDescriptor descriptor,
protected static TextFlow createTextFlow(Iterable<CharSequence> parts,
FontDescriptor descriptor,
float linespacing, float rotation) {
final TextFlow textFlow = new TextFlow();
textFlow.setLineSpacing(linespacing);

View file

@ -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();
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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;
}