Add FontCacheImpl
This commit is contained in:
parent
4fbd648eeb
commit
f214b4f404
1 changed files with 74 additions and 0 deletions
74
src/main/java/net/kemitix/fontface/FontCacheImpl.java
Normal file
74
src/main/java/net/kemitix/fontface/FontCacheImpl.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
package net.kemitix.fontface;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import java.awt.*;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class FontCacheImpl implements FontCache {
|
||||
|
||||
private static final Logger LOGGER =
|
||||
Logger.getLogger(
|
||||
FontCacheImpl.class.getName());
|
||||
|
||||
private final Map<URI, Font> fileCache = new HashMap<>();
|
||||
|
||||
private final Map<FontAndSize, Font> fontCache = new HashMap<>();
|
||||
|
||||
private final FontLoader fontLoader;
|
||||
|
||||
@Override
|
||||
public Font loadFont(final FontFace fontFace) {
|
||||
LOGGER.finest(String.format("Requesting %s %d",
|
||||
fontFace.getFontLocation(), fontFace.getSize()));
|
||||
final Font baseFont =
|
||||
fileCache.computeIfAbsent(
|
||||
fontFace.getFontLocation(),
|
||||
loadNewFontFile(fontFace));
|
||||
return fontCache.computeIfAbsent(
|
||||
FontAndSize.of(baseFont, fontFace.getSize()),
|
||||
resizeFont());
|
||||
}
|
||||
|
||||
private Function<URI, Font> loadNewFontFile(
|
||||
final FontFace fontFace
|
||||
) {
|
||||
return uri -> {
|
||||
LOGGER.fine(String.format("Loading %s", uri));
|
||||
return fontLoader.loadFont(fontFace);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
private Function<FontAndSize, Font> resizeFont() {
|
||||
return fontAndSize -> {
|
||||
LOGGER.finer(String.format("Resizing %s to %d",
|
||||
fontAndSize.getFont().getName(), fontAndSize.getSize()));
|
||||
return fontAndSize.font
|
||||
.deriveFont(Font.PLAIN, fontAndSize.getSize());
|
||||
};
|
||||
}
|
||||
|
||||
@Getter
|
||||
@EqualsAndHashCode
|
||||
private static class FontAndSize {
|
||||
private final Font font;
|
||||
private final int size;
|
||||
|
||||
private FontAndSize(final Font font, final int size) {
|
||||
this.font = font;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
static FontAndSize of(final Font font, final int size) {
|
||||
return new FontAndSize(font, size);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue