Merge FontCacheImpl into FontCache and make it a class
This commit is contained in:
parent
1152e30d55
commit
d35c570ecb
3 changed files with 71 additions and 81 deletions
|
@ -1,9 +1,73 @@
|
|||
package net.kemitix.fontface;
|
||||
|
||||
import net.kemitix.fontface.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;
|
||||
|
||||
public interface FontCache {
|
||||
Font loadFont(FontFace fontFace);
|
||||
@RequiredArgsConstructor
|
||||
public class FontCache {
|
||||
|
||||
private static final Logger LOGGER =
|
||||
Logger.getLogger(
|
||||
FontCache.class.getName());
|
||||
|
||||
private final Map<URI, Font> fileCache = new HashMap<>();
|
||||
|
||||
private final Map<FontAndSize, Font> fontCache = new HashMap<>();
|
||||
|
||||
private final FontLoader fontLoader;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,74 +0,0 @@
|
|||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ import static org.mockito.Mockito.verify;
|
|||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class FontCacheImplTest {
|
||||
public class FontCacheTest {
|
||||
|
||||
private final URI fontLocation = new File("font.otf").toURI();
|
||||
private final String colour = "colour";
|
||||
|
@ -36,7 +36,7 @@ public class FontCacheImplTest {
|
|||
@DisplayName("When cache is empty then load font")
|
||||
public void emptyCache() {
|
||||
//given
|
||||
final FontCacheImpl fontCache = new FontCacheImpl(fontLoader);
|
||||
final FontCache fontCache = new FontCache(fontLoader);
|
||||
final FontFace fontFace = FontFace.of(fontLocation, 26, colour);
|
||||
//when
|
||||
fontCache.loadFont(fontFace);
|
||||
|
@ -48,7 +48,7 @@ public class FontCacheImplTest {
|
|||
@DisplayName("When cache has font, but not size, then don't reload font")
|
||||
public void notInSize() {
|
||||
//given
|
||||
final FontCacheImpl fontCache = new FontCacheImpl(fontLoader);
|
||||
final FontCache fontCache = new FontCache(fontLoader);
|
||||
final FontFace previousFontFace = FontFace.of(fontLocation, 16, colour);
|
||||
fontCache.loadFont(previousFontFace);
|
||||
final FontFace fontFace = FontFace.of(fontLocation, 26, colour);
|
||||
|
@ -63,7 +63,7 @@ public class FontCacheImplTest {
|
|||
@DisplayName("When cache has font in size, then don't load font")
|
||||
public void available() {
|
||||
//given
|
||||
final FontCacheImpl fontCache = new FontCacheImpl(fontLoader);
|
||||
final FontCache fontCache = new FontCache(fontLoader);
|
||||
final FontFace fontFace = FontFace.of(fontLocation, 26, colour);
|
||||
//when
|
||||
fontCache.loadFont(fontFace);
|
Loading…
Reference in a new issue