From 2d6961e84893afd86857989e2d6718a1f94efeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Prante?= Date: Wed, 22 Jan 2025 17:52:05 +0100 Subject: [PATCH] fix font embedder and cloass loader issues, add arabic/hebrew fonts --- .../Google-Noto-LICENSE.txt | 0 .../org.xbib.graphics.fonts.FontEmbedder | 1 + graphics-fonts-cjk/Google-Noto-LICENSE.txt | 45 +++++++++++++ .../Google-Noto-LICENSE.txt | 45 +++++++++++++ graphics-fonts-cyrillic/NOTICE.txt | 2 + graphics-fonts-hebrew/Google-Noto-LICENSE.txt | 45 +++++++++++++ .../Adobe-Source-LICENSE.txt | 0 graphics-fonts-latin/Google-Noto-LICENSE.txt | 45 +++++++++++++ .../org/xbib/graphics/fonts/FontEmbedder.java | 3 + .../xbib/graphics/fonts/TrueTypeEmbedder.java | 33 +--------- graphics-pdfbox-layout/build.gradle | 1 + .../pdfbox/layout/element/Document.java | 66 ++++++++++--------- .../test/HelloNotoSansArabicFontTest.java | 52 +++++++++++++++ .../layout/test/HelloNotoSansCJKFontTest.java | 4 +- .../layout/test/HelloNotoSansFontTest.java | 2 - .../test/HelloNotoSansHebrewFontTest.java | 52 +++++++++++++++ .../layout/test/HelloOpenSansFontTest.java | 8 +-- 17 files changed, 336 insertions(+), 68 deletions(-) rename {graphics-pdfbox-layout => graphics-fonts-arabic}/Google-Noto-LICENSE.txt (100%) create mode 100644 graphics-fonts-arabic/src/main/resources/META-INF/services/org.xbib.graphics.fonts.FontEmbedder create mode 100644 graphics-fonts-cjk/Google-Noto-LICENSE.txt create mode 100644 graphics-fonts-cyrillic/Google-Noto-LICENSE.txt create mode 100644 graphics-fonts-cyrillic/NOTICE.txt create mode 100644 graphics-fonts-hebrew/Google-Noto-LICENSE.txt rename {graphics-pdfbox-layout => graphics-fonts-latin}/Adobe-Source-LICENSE.txt (100%) create mode 100644 graphics-fonts-latin/Google-Noto-LICENSE.txt create mode 100644 graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansArabicFontTest.java create mode 100644 graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansHebrewFontTest.java diff --git a/graphics-pdfbox-layout/Google-Noto-LICENSE.txt b/graphics-fonts-arabic/Google-Noto-LICENSE.txt similarity index 100% rename from graphics-pdfbox-layout/Google-Noto-LICENSE.txt rename to graphics-fonts-arabic/Google-Noto-LICENSE.txt diff --git a/graphics-fonts-arabic/src/main/resources/META-INF/services/org.xbib.graphics.fonts.FontEmbedder b/graphics-fonts-arabic/src/main/resources/META-INF/services/org.xbib.graphics.fonts.FontEmbedder new file mode 100644 index 0000000..bccf9f1 --- /dev/null +++ b/graphics-fonts-arabic/src/main/resources/META-INF/services/org.xbib.graphics.fonts.FontEmbedder @@ -0,0 +1 @@ +org.xbib.graphics.fonts.arabic.ArabicFontEmbedder \ No newline at end of file diff --git a/graphics-fonts-cjk/Google-Noto-LICENSE.txt b/graphics-fonts-cjk/Google-Noto-LICENSE.txt new file mode 100644 index 0000000..aac153a --- /dev/null +++ b/graphics-fonts-cjk/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-fonts-cyrillic/Google-Noto-LICENSE.txt b/graphics-fonts-cyrillic/Google-Noto-LICENSE.txt new file mode 100644 index 0000000..aac153a --- /dev/null +++ b/graphics-fonts-cyrillic/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-fonts-cyrillic/NOTICE.txt b/graphics-fonts-cyrillic/NOTICE.txt new file mode 100644 index 0000000..75e8a57 --- /dev/null +++ b/graphics-fonts-cyrillic/NOTICE.txt @@ -0,0 +1,2 @@ + +Identical to Noto Latin fonts. There is no need to add cyrillic Noto fonst if the latin Noto fonts are already added. \ No newline at end of file diff --git a/graphics-fonts-hebrew/Google-Noto-LICENSE.txt b/graphics-fonts-hebrew/Google-Noto-LICENSE.txt new file mode 100644 index 0000000..aac153a --- /dev/null +++ b/graphics-fonts-hebrew/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/Adobe-Source-LICENSE.txt b/graphics-fonts-latin/Adobe-Source-LICENSE.txt similarity index 100% rename from graphics-pdfbox-layout/Adobe-Source-LICENSE.txt rename to graphics-fonts-latin/Adobe-Source-LICENSE.txt diff --git a/graphics-fonts-latin/Google-Noto-LICENSE.txt b/graphics-fonts-latin/Google-Noto-LICENSE.txt new file mode 100644 index 0000000..aac153a --- /dev/null +++ b/graphics-fonts-latin/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-fonts/src/main/java/org/xbib/graphics/fonts/FontEmbedder.java b/graphics-fonts/src/main/java/org/xbib/graphics/fonts/FontEmbedder.java index 575b795..42a07cb 100644 --- a/graphics-fonts/src/main/java/org/xbib/graphics/fonts/FontEmbedder.java +++ b/graphics-fonts/src/main/java/org/xbib/graphics/fonts/FontEmbedder.java @@ -3,10 +3,13 @@ package org.xbib.graphics.fonts; import org.apache.pdfbox.pdmodel.PDDocument; import java.io.IOException; +import java.util.Collection; public interface FontEmbedder { String getAlias(); + Collection getFonts(); + void embed(PDDocument pdDocument) throws IOException; } diff --git a/graphics-fonts/src/main/java/org/xbib/graphics/fonts/TrueTypeEmbedder.java b/graphics-fonts/src/main/java/org/xbib/graphics/fonts/TrueTypeEmbedder.java index 5d03ea0..ea56d29 100644 --- a/graphics-fonts/src/main/java/org/xbib/graphics/fonts/TrueTypeEmbedder.java +++ b/graphics-fonts/src/main/java/org/xbib/graphics/fonts/TrueTypeEmbedder.java @@ -13,30 +13,14 @@ import java.io.InputStream; import java.util.Collection; import java.util.LinkedHashMap; import java.util.Map; -import java.util.Set; -public class TrueTypeEmbedder implements FontEmbedder { +public abstract class TrueTypeEmbedder implements FontEmbedder { - private static final Map fonts = new LinkedHashMap<>(); + private final Map fonts = new LinkedHashMap<>(); public TrueTypeEmbedder() { } - public static void clearFonts() { - fonts.clear(); - } - - @Override - public String getAlias() { - // dummy - return ""; - } - - @Override - public void embed(PDDocument pdDocument) throws IOException { - // nothing to embed er default - } - public void embed(PDDocument pdDocument, final String name, final String regularFontResource, @@ -102,18 +86,7 @@ public class TrueTypeEmbedder implements FontEmbedder { }); } - public void register(Font font) { - fonts.putIfAbsent(font.getAlias(), font); - } - - public Font getFont(String alias) { - return fonts.get(alias); - } - - public Set getFontAliases() { - return fonts.keySet(); - } - + @Override public Collection getFonts() { return fonts.values(); } diff --git a/graphics-pdfbox-layout/build.gradle b/graphics-pdfbox-layout/build.gradle index 7ec6c74..cdf148e 100644 --- a/graphics-pdfbox-layout/build.gradle +++ b/graphics-pdfbox-layout/build.gradle @@ -6,6 +6,7 @@ dependencies { api libs.datastructures.settings runtimeOnly libs.datastructures.settings.json runtimeOnly libs.datastructures.settings.yaml + testRuntimeOnly project(':graphics-fonts-arabic') testRuntimeOnly project(':graphics-fonts-latin') testRuntimeOnly project(':graphics-fonts-hebrew') testRuntimeOnly project(':graphics-fonts-cjk') diff --git a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/Document.java b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/Document.java index daf9fb9..be1a33b 100644 --- a/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/Document.java +++ b/graphics-pdfbox-layout/src/main/java/org/xbib/graphics/pdfbox/layout/element/Document.java @@ -5,7 +5,6 @@ import org.apache.pdfbox.pdmodel.PDDocumentInformation; import org.xbib.graphics.fonts.BaseFont; import org.xbib.graphics.fonts.Font; import org.xbib.graphics.fonts.FontEmbedder; -import org.xbib.graphics.fonts.TrueTypeEmbedder; import org.xbib.graphics.pdfbox.layout.element.render.Layout; import org.xbib.graphics.pdfbox.layout.element.render.LayoutHint; import org.xbib.graphics.pdfbox.layout.element.render.RenderContext; @@ -31,6 +30,7 @@ import java.util.Map.Entry; import java.util.ServiceLoader; import java.util.logging.Level; import java.util.logging.Logger; +import java.util.stream.StreamSupport; /** * The central class for creating a document. @@ -51,7 +51,7 @@ public class Document implements Element, Closeable, RenderListener { private final PDDocumentInformation pdDocumentInformation; - private final TrueTypeEmbedder trueTypeFonts; + private final Collection fonts; /** * Creates a Document. @@ -85,7 +85,7 @@ public class Document implements Element, Closeable, RenderListener { public Document(PageFormat pageFormat) { this.pdDocument = new PDDocument(); this.pdDocumentInformation = new PDDocumentInformation(); - this.trueTypeFonts = new TrueTypeEmbedder(); + this.fonts = new ArrayList<>(); this.elements = new ArrayList<>(); this.customRenderer = new ArrayList<>(); this.renderListener = new ArrayList<>(); @@ -97,51 +97,57 @@ public class Document implements Element, Closeable, RenderListener { } public void registerCoreFonts() { - trueTypeFonts.register(BaseFont.HELVETICA); - trueTypeFonts.register(BaseFont.TIMES); - trueTypeFonts.register(BaseFont.COURIER); + fonts.add(BaseFont.HELVETICA); + fonts.add(BaseFont.TIMES); + fonts.add(BaseFont.COURIER); } public void embedAllFonts() { - embedAllFonts(getClass().getClassLoader()); - } - - public void embedAllFonts(ClassLoader classLoader) { - for (FontEmbedder fontEmbedder : ServiceLoader.load(FontEmbedder.class, classLoader)) { - try { - fontEmbedder.embed(this.pdDocument); - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); - } - } - logger.log(Level.FINER, "fonts: " + trueTypeFonts.getFontAliases()); + embedFonts(List.of("")); } public void embedFonts(Collection aliases) { - embedFonts(aliases, getClass().getClassLoader()); + embedFonts(aliases, List.of(getClass().getClassLoader(), + ClassLoader.getSystemClassLoader(), + Thread.currentThread().getContextClassLoader())); } - public void embedFonts(Collection aliases, ClassLoader classLoader) { - for (String alias : aliases) { - for (FontEmbedder fontEmbedder : ServiceLoader.load(FontEmbedder.class, classLoader)) { - try { - if (fontEmbedder.getAlias().equals(alias)) { - fontEmbedder.embed(this.pdDocument); + private void embedFonts(Collection aliases, Collection classLoaders) { + for (ClassLoader classLoader : classLoaders) { + Iterable fontEmbedders = ServiceLoader.load(FontEmbedder.class, classLoader); + boolean serviceLoaded = false; + for (String alias : aliases) { + boolean found = false; + for (FontEmbedder fontEmbedder : fontEmbedders) { + try { + if (alias == null || alias.isEmpty() || fontEmbedder.getAlias().equals(alias)) { + fontEmbedder.embed(this.pdDocument); + found = true; + serviceLoaded = true; + fonts.addAll(fontEmbedder.getFonts()); + } + } catch (IOException e) { + logger.log(Level.SEVERE, e.getMessage(), e); } - } catch (IOException e) { - logger.log(Level.SEVERE, e.getMessage(), e); + } + if (!found) { + logger.log(Level.WARNING, "font " + alias + " not found using embedders " + + StreamSupport.stream(fontEmbedders.spliterator(), false).toList()); } } + if (serviceLoaded) { + break; + } } - logger.log(Level.FINER, "fonts: " + trueTypeFonts.getFontAliases()); + logger.log(Level.FINER, "fonts: " + fonts); } public Font getFont(String alias) { - return trueTypeFonts.getFont(alias); + return fonts.stream().filter(f -> f.getAlias().equals(alias)).findFirst().orElse(null); } public Collection getFonts() { - return trueTypeFonts.getFonts(); + return fonts; } public void setPageFormat(PageFormat pageFormat) { diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansArabicFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansArabicFontTest.java new file mode 100644 index 0000000..dfc3f04 --- /dev/null +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansArabicFontTest.java @@ -0,0 +1,52 @@ +package org.xbib.graphics.pdfbox.layout.test; + +import org.junit.jupiter.api.Test; +import org.xbib.graphics.fonts.Font; +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.text.Indent; +import org.xbib.graphics.pdfbox.layout.text.SpaceUnit; + +import java.io.FileOutputStream; +import java.util.Collection; +import java.util.List; + +public class HelloNotoSansArabicFontTest { + + @Test + public void testDocumentOne() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + document.embedFonts(List.of("notosans", "notosansarabic")); + Collection font = document.getFonts(); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Noto Sans Arabic Regular\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans Arabic Bold*\n", 12, font); + paragraph.addText("Hello Unicode Arabic Text: فهرس\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Noto Sans Arabic Regular\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellonotofontarabic.pdf")).close(); + } + + @Test + public void testDocumentTwo() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + document.embedFonts(List.of("notosansarabic")); + List font = List.of(document.getFont("notosansarabic")); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Noto Sans Arabic Regular 2\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans Arabic Bold*\n", 12, font); + paragraph.addText("Hello Unicode Arabic Text: فهرس\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Noto Sans Arabic Regular 2\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellonotofontarabic2.pdf")).close(); + } +} 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 7122872..693c590 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 @@ -17,7 +17,7 @@ public class HelloNotoSansCJKFontTest { @Test public void testDocumentOne() throws Exception { Document document = new Document(PageFormats.A4_PORTRAIT); - document.embedFonts(List.of("notosans", "notosanscjksc"), getClass().getClassLoader()); + document.embedFonts(List.of("notosans", "notosanscjksc")); Collection font = document.getFonts(); Paragraph paragraph = new Paragraph(); paragraph.add(new Indent(32, SpaceUnit.pt)); @@ -37,7 +37,7 @@ public class HelloNotoSansCJKFontTest { @Test public void testDocumentTwo() throws Exception { Document document = new Document(PageFormats.A4_PORTRAIT); - document.embedFonts(List.of("notosans", "notosanscjksc"), getClass().getClassLoader()); + document.embedFonts(List.of("notosanscjksc")); List font = List.of(document.getFont("notosanscjksc")); Paragraph paragraph = new Paragraph(); paragraph.add(new Indent(32, SpaceUnit.pt)); diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansFontTest.java index da89415..4a9f538 100644 --- a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansFontTest.java +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansFontTest.java @@ -15,7 +15,6 @@ public class HelloNotoSansFontTest { @Test public void testDocumentOne() throws Exception { - TrueTypeEmbedder.clearFonts(); Document document = new Document(PageFormats.A4_PORTRAIT); document.embedAllFonts(); List font = List.of(document.getFont("notosans")); @@ -37,7 +36,6 @@ public class HelloNotoSansFontTest { @Test public void testDocumentTwo() throws Exception { - TrueTypeEmbedder.clearFonts(); Document document = new Document(PageFormats.A4_PORTRAIT); document.embedFonts(List.of("notosans")); List font = List.of(document.getFont("notosans")); diff --git a/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansHebrewFontTest.java b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansHebrewFontTest.java new file mode 100644 index 0000000..cfc9c09 --- /dev/null +++ b/graphics-pdfbox-layout/src/test/java/org/xbib/graphics/pdfbox/layout/test/HelloNotoSansHebrewFontTest.java @@ -0,0 +1,52 @@ +package org.xbib.graphics.pdfbox.layout.test; + +import org.junit.jupiter.api.Test; +import org.xbib.graphics.fonts.Font; +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.text.Indent; +import org.xbib.graphics.pdfbox.layout.text.SpaceUnit; + +import java.io.FileOutputStream; +import java.util.Collection; +import java.util.List; + +public class HelloNotoSansHebrewFontTest { + + @Test + public void testDocumentOne() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + document.embedFonts(List.of("notosans", "notosanshebrew")); + Collection font = document.getFonts(); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Noto Sans Hebrew Regular\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans Hebrew Bold*\n", 12, font); + paragraph.addText("Hello Unicode Hebrew Text: יְרוּשָׁלַיִם\n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Noto Sans Hebrew Regular\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellonotofonthebrew.pdf")).close(); + } + + @Test + public void testDocumentTwo() throws Exception { + Document document = new Document(PageFormats.A4_PORTRAIT); + document.embedFonts(List.of("notosanshebrew")); + List font = List.of(document.getFont("notosanshebrew")); + Paragraph paragraph = new Paragraph(); + paragraph.add(new Indent(32, SpaceUnit.pt)); + paragraph.addMarkup("Hello Noto Sans Hebrew Regular 2\n", 12, font); + paragraph.addMarkup("*Hello Noto Sans Hebrew Bold*\n", 12, font); + paragraph.addText("Hello Unicode Hebrew Text: יְרוּשָׁלַיִם \n", 12, font); + document.add(paragraph); + Paragraph anotherParagraph = new Paragraph(); + anotherParagraph.add(new Indent(32, SpaceUnit.pt)); + anotherParagraph.addMarkup("Hello Noto Sans Hebrew Regular 2\n", 12, font); + document.add(anotherParagraph); + document.render().save(new FileOutputStream("build/hellonotofonthebrew2.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 index e5bf80e..1b400d6 100644 --- 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 @@ -17,7 +17,7 @@ public class HelloOpenSansFontTest { public void testDocumentOne() throws Exception { Document document = new Document(PageFormats.A4_PORTRAIT); document.embedAllFonts(); - List font = List.of(document.getFont("opensans")); + List font = List.of(document.getFont("opensans"), document.getFont("notosanscjksc")); Paragraph paragraph = new Paragraph(); paragraph.add(new Indent(32, SpaceUnit.pt)); paragraph.addMarkup("Hello Open Sans Regular\n", 12, font); @@ -26,7 +26,7 @@ public class HelloOpenSansFontTest { 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); + paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); document.add(paragraph); Paragraph anotherParagraph = new Paragraph(); anotherParagraph.add(new Indent(32, SpaceUnit.pt)); @@ -39,7 +39,7 @@ public class HelloOpenSansFontTest { public void testDocumentTwo() throws Exception { Document document = new Document(PageFormats.A4_PORTRAIT); document.embedAllFonts(); - List font = List.of(document.getFont("opensans")); + List font = List.of(document.getFont("opensans"), document.getFont("notosanscjksc")); Paragraph paragraph = new Paragraph(); paragraph.add(new Indent(32, SpaceUnit.pt)); paragraph.addMarkup("Hello Open Sans Regular 2\n", 12, font); @@ -48,7 +48,7 @@ public class HelloOpenSansFontTest { 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); + paragraph.addText("Hello CJK Text: 北京是中国古都,是拥有三千余年建城历史、八百六十余年建都史的历史文化名城\n", 12, font); document.add(paragraph); Paragraph anotherParagraph = new Paragraph(); anotherParagraph.add(new Indent(32, SpaceUnit.pt));