From 30358e6cb39aed6072829b58cbeeff51dd929d9e Mon Sep 17 00:00:00 2001 From: Peter Dell Date: Mon, 9 Jan 2023 01:35:23 +0100 Subject: [PATCH] Correctly reuse hardware font files and fonts --- .../base/hardware/HardwareCharacterSet.java | 39 ++++++++++++------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/com.wudsn.ide.base/src/com/wudsn/ide/base/hardware/HardwareCharacterSet.java b/com.wudsn.ide.base/src/com/wudsn/ide/base/hardware/HardwareCharacterSet.java index 2d113fcd..659b0d47 100644 --- a/com.wudsn.ide.base/src/com/wudsn/ide/base/hardware/HardwareCharacterSet.java +++ b/com.wudsn.ide.base/src/com/wudsn/ide/base/hardware/HardwareCharacterSet.java @@ -21,7 +21,9 @@ package com.wudsn.ide.base.hardware; import java.io.File; import java.util.Map; +import java.util.Set; import java.util.TreeMap; +import java.util.TreeSet; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.swt.SWT; @@ -34,7 +36,7 @@ import com.wudsn.ide.base.common.FileUtility; import com.wudsn.ide.base.common.ResourceUtility; /** - * Logical character set with physical font and character mapping. + * Logical character set with physical font and character mapping. * * @since 1.7.0 */ @@ -55,13 +57,16 @@ public enum HardwareCharacterSet { private final static int CBM_UPPER_FONT_BASE = 0x0100; private final static int CBM_LOWER_FONT_BASE = 0x0200; - private static Map characterMappingMap; private static Map fontNameFileMap; - private static Map fontNameAndSizeFontMap; + private static Set fontPathSet; + private static Map fontNameSizeFontMap; + private static Map characterMappingMap; static { fontNameFileMap = new TreeMap(); - fontNameAndSizeFontMap = new TreeMap(); + fontPathSet = new TreeSet(); + fontNameSizeFontMap = new TreeMap(); + characterMappingMap = new TreeMap(); } @@ -203,7 +208,7 @@ public enum HardwareCharacterSet { // Check if temporary file is already cached? if (!fontNameFileMap.containsKey(fontName)) { try { - file = File.createTempFile("HardwareCharacterSet-"+fontName, null); + file = File.createTempFile("HardwareCharacterSet-" + fontName, null); byte[] content = ResourceUtility.loadResourceAsByteArray(fontPath); FileUtility.writeBytes(file, content); // Make sure the file is kept until the process ends. @@ -219,25 +224,31 @@ public enum HardwareCharacterSet { } // Remember the file, may be null fontNameFileMap.put(fontName, file); + } else { + file = fontNameFileMap.get(fontName); } - } else { - file = fontNameFileMap.get(fontName); } // If temporary file is present,try to load the font. if (file != null) { - var fontSizeKey = fontName + "/" + fontSize; - if (!fontNameAndSizeFontMap.containsKey(fontSizeKey)) { - Device device = Display.getDefault(); + Device device = Display.getDefault(); + if (!fontPathSet.contains(fontPath)) { String absolutePath = file.getAbsolutePath(); if (device.loadFont(absolutePath)) { - font = new Font(device, fontName, fontSize, SWT.NORMAL); - fontNameAndSizeFontMap.put(fontSizeKey, font); + fontPathSet.add(fontPath); + } else { + throw new RuntimeException( + "Cannot load font '" + fontName + "' from file '" + absolutePath + "'"); } - } else { - font = fontNameAndSizeFontMap.get(fontSizeKey); } + var fontNameSizeKey = fontName + "/" + fontSize; + font = fontNameSizeFontMap.get(fontNameSizeKey); + if (font == null) { + font = new Font(device, fontName, fontSize, SWT.NORMAL); + fontNameSizeFontMap.put(fontNameSizeKey, font); + } + } } return font;