Skip to content

Commit

Permalink
* disallow by default
Browse files Browse the repository at this point in the history
* change documentation
* formatting
  • Loading branch information
stephanr committed Jun 6, 2024
1 parent 04f21e6 commit c98cf31
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
----------------------------------------------------------------------------*/
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;
Expand All @@ -50,64 +52,73 @@
import org.slf4j.LoggerFactory;

public class WorkspaceFonts implements Initializable {
protected static final Set<String> PROCESSED_FILES = new HashSet<>();

private static final Logger LOG = LoggerFactory.getLogger( WorkspaceFonts.class );

private static final String FONT_DIR = "fonts";

@Override
public void init( Workspace workspace ) {
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 );
}
}

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
Expand Up @@ -11,14 +11,15 @@

public class WorkspaceFontTest {

private static final File TEST_DIR = new File( "src/test/resources/org/deegree/commons/fontworkspace" );
private static final File TEST_DIR = new File("src/test/resources/org/deegree/commons/fontworkspace");

private static Workspace ws = new DefaultWorkspace( TEST_DIR );
private static Workspace ws = new DefaultWorkspace(TEST_DIR);

@Test
public void testFontLoader() {
WorkspaceFonts wsf = new WorkspaceFonts();
wsf.init(ws);
assertThat(WorkspaceFonts.PROCESSED_FILES, not(empty()));
}

@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
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 @@ -202,7 +202,9 @@ Some common ones are:
|===

____
NOTE: Font files are processed only once per file and are not
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.
Expand Down

0 comments on commit c98cf31

Please sign in to comment.