Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register True-Type fonts at workspace startup #1417

Merged
merged 6 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String> 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: {}", e.getMessage());
LOG.trace("Exception", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.deegree.commons.proxy.ProxySettings
org.deegree.commons.proxy.ProxySettings
org.deegree.commons.font.WorkspaceFonts
Original file line number Diff line number Diff line change
@@ -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()));
}

}
Original file line number Diff line number Diff line change
@@ -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
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<anchor-appendix>>
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,
Expand Down