diff --git a/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java b/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java new file mode 100644 index 0000000000..81971be61d --- /dev/null +++ b/deegree-core/deegree-core-commons/src/main/java/org/deegree/commons/font/WorkspaceFonts.java @@ -0,0 +1,124 @@ +/*---------------------------------------------------------------------------- + This file is part of deegree, http://deegree.org/ + Copyright (C) 2022 by: + - grit graphische Informationstechnik Beratungsgesellschaft mbH - + + This library is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation; either version 2.1 of the License, or (at your option) + any later version. + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, Inc., + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact information: + + grit graphische Informationstechnik Beratungsgesellschaft mbH + Landwehrstr. 143, 59368 Werne + Germany + http://www.grit.de/ + + lat/lon GmbH + Aennchenstr. 19, 53177 Bonn + Germany + http://lat-lon.de/ + + Department of Geography, University of Bonn + Prof. Dr. Klaus Greve + Postfach 1147, 53001 Bonn + Germany + http://www.geographie.uni-bonn.de/deegree/ + + e-mail: info@deegree.org + ----------------------------------------------------------------------------*/ +package org.deegree.commons.font; + +import static org.deegree.commons.utils.TunableParameter.get; + +import java.awt.Font; +import java.awt.GraphicsEnvironment; +import java.io.File; +import java.util.HashSet; +import java.util.Set; +import org.apache.commons.io.FileUtils; +import org.deegree.workspace.Initializable; +import org.deegree.workspace.Workspace; +import org.deegree.workspace.standard.DefaultWorkspace; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class WorkspaceFonts implements Initializable { + + protected static final Set PROCESSED_FILES = new HashSet<>(); + + private static final boolean ENABLED = get("deegree.workspace.allow-font-loading", false); + + private static final Logger LOG = LoggerFactory.getLogger(WorkspaceFonts.class); + + private static final String FONT_DIR = "fonts"; + + @Override + public void init(Workspace workspace) { + if (!ENABLED) { + LOG.debug( + "Loading fonts from workspace is disabled, set deegree.workspace.allow-font-loading=true to enable it."); + return; + } + LOG.info("--------------------------------------------------------------------------------"); + LOG.info("Fonts in workspace."); + LOG.info("--------------------------------------------------------------------------------"); + if (loadFontsFromWorkspace(workspace)) { + LOG.info("Fonts successfully loaded from workspace."); + return; + } + LOG.info("No Fonts to register"); + } + + private boolean loadFontsFromWorkspace(final Workspace ws) { + File fontDir = new File(((DefaultWorkspace) ws).getLocation(), FONT_DIR); + if (!fontDir.isDirectory()) { + return false; + } + boolean loaded = false; + for (File f : FileUtils.listFiles(fontDir, new String[] { "ttf" }, false)) { + loaded = true; + registerOnce(f); + } + return loaded; + } + + /** + * Load font and register it in the local {@link GraphicsEnvironment} + * + * Note: If a file has already been processed, it wont be loaded or registered again + * @param fontFile font to be processed + */ + static void registerOnce(File fontFile) { + if (fontFile == null) { + return; + } + final String fileKey = fontFile.getAbsolutePath(); + if (PROCESSED_FILES.contains(fileKey)) { + // do not re-register fonts, as fonts can not be deregistered in + // GraphicsEnvironment + LOG.info("Skip file '{}' because it was already processed.", fontFile.getName()); + return; + } + PROCESSED_FILES.add(fileKey); + try { + Font f = Font.createFont(Font.TRUETYPE_FONT, fontFile); + GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(f); + LOG.info("Loaded Font: {} (face: {}, family: {}, file: {})", f.getName(), f.getFontName(), f.getFamily(), + fontFile.getName()); + } + catch (Exception e) { + LOG.warn("Font '{}' could not be loaded: {}", fontFile, e.getMessage()); + LOG.trace("Exception", e); + } + } + +} diff --git a/deegree-core/deegree-core-commons/src/main/resources/META-INF/services/org.deegree.workspace.Initializable b/deegree-core/deegree-core-commons/src/main/resources/META-INF/services/org.deegree.workspace.Initializable index 449a3275a0..00d8ce753f 100644 --- a/deegree-core/deegree-core-commons/src/main/resources/META-INF/services/org.deegree.workspace.Initializable +++ b/deegree-core/deegree-core-commons/src/main/resources/META-INF/services/org.deegree.workspace.Initializable @@ -1 +1,2 @@ -org.deegree.commons.proxy.ProxySettings \ No newline at end of file +org.deegree.commons.proxy.ProxySettings +org.deegree.commons.font.WorkspaceFonts \ No newline at end of file diff --git a/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java b/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java new file mode 100644 index 0000000000..c23b7952bb --- /dev/null +++ b/deegree-core/deegree-core-commons/src/test/java/org/deegree/commons/config/WorkspaceFontTest.java @@ -0,0 +1,40 @@ +package org.deegree.commons.font; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.not; + +import java.io.File; +import org.deegree.commons.utils.TunableParameter; +import org.deegree.workspace.Workspace; +import org.deegree.workspace.standard.DefaultWorkspace; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +public class WorkspaceFontTest { + + private static final File TEST_DIR = new File("src/test/resources/org/deegree/commons/fontworkspace"); + + private static Workspace ws = new DefaultWorkspace(TEST_DIR); + + @BeforeClass + public static void before() { + TunableParameter.resetCache(); + System.setProperty("deegree.workspace.allow-font-loading", "true"); + } + + @AfterClass + public static void after() { + System.setProperty("deegree.workspace.allow-font-loading", ""); + TunableParameter.resetCache(); + } + + @Test + public void testFontLoader() { + WorkspaceFonts wsf = new WorkspaceFonts(); + wsf.init(ws); + assertThat(WorkspaceFonts.PROCESSED_FILES, not(empty())); + } + +} diff --git a/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.LICENSE.txt b/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.LICENSE.txt new file mode 100644 index 0000000000..b83eaee9b3 --- /dev/null +++ b/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.LICENSE.txt @@ -0,0 +1,35 @@ +This file is part of deegree, http://deegree.org/ + Copyright (C) 2022 by: + - grit graphische Informationstechnik Beratungsgesellschaft mbH - + + This library is free software; you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License as published by the Free + Software Foundation; either version 2.1 of the License, or (at your option) + any later version. + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + details. + You should have received a copy of the GNU Lesser General Public License + along with this library; if not, write to the Free Software Foundation, Inc., + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + Contact information: + + grit graphische Informationstechnik Beratungsgesellschaft mbH + Landwehrstr. 143, 59368 Werne + Germany + http://www.grit.de/ + + lat/lon GmbH + Aennchenstr. 19, 53177 Bonn + Germany + http://lat-lon.de/ + + Department of Geography, University of Bonn + Prof. Dr. Klaus Greve + Postfach 1147, 53001 Bonn + Germany + http://www.geographie.uni-bonn.de/deegree/ + + e-mail: info@deegree.org \ No newline at end of file diff --git a/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.ttf b/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.ttf new file mode 100644 index 0000000000..0c993e7c69 Binary files /dev/null and b/deegree-core/deegree-core-commons/src/test/resources/org/deegree/commons/fontworkspace/fonts/DummyFont.ttf differ diff --git a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc index 909a6e1a86..94619cee50 100644 --- a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc +++ b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/appendix.adoc @@ -59,6 +59,8 @@ f |deegree.config.apikey.warn-when-disabled |java.lang.Boolean |true |Log warning if security on REST api is disabled by specifying `*` in _config.apikey_. +|deegree.workspace.allow-font-loading |java.lang.Boolean |false |Allow font registration on workspace startup (disabled by default). + |=== === Interception points diff --git a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc index ff6fc79fe0..077a49717b 100644 --- a/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc +++ b/deegree-services/deegree-webservices-handbook/src/main/asciidoc/basics.adoc @@ -198,8 +198,18 @@ Some common ones are: |appschemas/ |GML application schemas |data/ |Datasets (GML, GeoTIFF, ...) |manager/ |Example requests (for the generic client) +|fonts/ |Fonts |=== +____ +NOTE: Font registration on workspace startup is not allowed by default +and has to be enabled with a parameter, see <> +for details. Font files are processed only once per file and are not +deregistered when a workspace is changed, stopped or reloaded. +To remove a font, remove the font file from the folder and restart +the container. +____ + ==== Workspace files and resources In order to clarify the relation between workspace files and resources,