From 09f5af154e3bf8ae5d78eaba845ef94b6801ce4a Mon Sep 17 00:00:00 2001 From: Oleg Agafonov Date: Mon, 20 Aug 2018 17:52:30 +0400 Subject: [PATCH] Fixed too large temp files creation for svg icons on startup --- .../deck/generator/DeckGeneratorDialog.java | 1 + .../client/util/object/SaveObjectUtil.java | 1 + .../org/mage/card/arcane/ManaSymbols.java | 65 ++++++++++++------- .../plugins/counter/CounterPluginImpl.java | 1 + 4 files changed, 46 insertions(+), 22 deletions(-) diff --git a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java index f3a0d367ed..a16607fa29 100644 --- a/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java +++ b/Mage.Client/src/main/java/mage/client/deck/generator/DeckGeneratorDialog.java @@ -325,6 +325,7 @@ public class DeckGeneratorDialog { // Generated deck has a nice unique name which corresponds to the timestamp at which it was created. String deckName = "Generated-Deck-" + dateFormat.format(new Date()); File tmp = new File(tempDir + File.separator + deckName + ".dck"); + tmp.getParentFile().mkdirs(); tmp.createNewFile(); deck.setName(deckName); Sets.saveDeck(tmp.getAbsolutePath(), deck.getDeckCardLists()); diff --git a/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java b/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java index c319f27b5b..b3b8673ebd 100644 --- a/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java +++ b/Mage.Client/src/main/java/mage/client/util/object/SaveObjectUtil.java @@ -57,6 +57,7 @@ public final class SaveObjectUtil { String time = now(DATE_PATTERN); File f = new File("income" + File.separator + name + '_' + time + ".save"); if (!f.exists()) { + f.getParentFile().mkdirs(); f.createNewFile(); } oos = new ObjectOutputStream(new FileOutputStream(f)); diff --git a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java index 7a8695255a..0416999114 100644 --- a/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java +++ b/Mage.Client/src/main/java/org/mage/card/arcane/ManaSymbols.java @@ -56,6 +56,8 @@ import org.apache.batik.util.SVGConstants; import org.apache.log4j.Logger; import org.mage.plugins.card.utils.CardImageUtils; +import static org.mage.plugins.card.utils.CardImageUtils.getImagesDir; + public final class ManaSymbols { private static final Logger LOGGER = Logger.getLogger(ManaSymbols.class); @@ -93,6 +95,41 @@ public final class ManaSymbols { private static final JLabel labelRender = new JLabel(); // render mana text + private static String getSvgPathToCss() { + return getImagesDir() + File.separator + "temp" + File.separator + "batic-svg-settings.css"; + } + private static void prepareSvg(Boolean forceToCreateCss) { + File f = new File(getSvgPathToCss()); + + if (forceToCreateCss || !f.exists()) { + + // Rendering hints can't be set programatically, so + // we override defaults with a temporary stylesheet. + // These defaults emphasize quality and precision, and + // are more similar to the defaults of other SVG viewers. + // SVG documents can still override these defaults. + String css = "svg {" + + "shape-rendering: geometricPrecision;" + + "text-rendering: geometricPrecision;" + + "color-rendering: optimizeQuality;" + + "image-rendering: optimizeQuality;" + + "}"; + + FileWriter w = null; + try { + f.getParentFile().mkdirs(); + f.createNewFile(); + w = new FileWriter(f); + w.write(css); + } catch (Throwable e) { + LOGGER.error("Can't create css file for svg", e); + } + finally { + StreamUtils.closeQuietly(w); + } + } + } + public static void loadImages() { // TODO: delete files rename jpg->gif (it was for backward compatibility for one of the old version?) renameSymbols(getResourceSymbolsPath(ResourceSymbolSize.SMALL)); @@ -101,6 +138,9 @@ public final class ManaSymbols { //renameSymbols(getSymbolsPath(ResourceSymbolSize.SVG)); // not need // TODO: remove medium sets files to "medium" folder like symbols above? + // prepare svg settings + prepareSvg(true); + // preload symbol images loadSymbolImages(15); loadSymbolImages(25); @@ -243,26 +283,9 @@ public final class ManaSymbols { final BufferedImage[] imagePointer = new BufferedImage[1]; - // Rendering hints can't be set programatically, so - // we override defaults with a temporary stylesheet. - // These defaults emphasize quality and precision, and - // are more similar to the defaults of other SVG viewers. - // SVG documents can still override these defaults. - String css = "svg {" - + "shape-rendering: geometricPrecision;" - + "text-rendering: geometricPrecision;" - + "color-rendering: optimizeQuality;" - + "image-rendering: optimizeQuality;" - + "}"; - - File cssFile = File.createTempFile("batik-default-override-", ".css"); - FileWriter w = null; - try { - w = new FileWriter(cssFile); - w.write(css); - } finally { - StreamUtils.closeQuietly(w); - } + // css settings for svg + prepareSvg(false); + File cssFile = new File(getSvgPathToCss()); TranscodingHints transcoderHints = new TranscodingHints(); @@ -311,8 +334,6 @@ public final class ManaSymbols { t.transcode(input, null); } catch (Exception e) { throw new IOException("Couldn't convert svg file: " + svgFile + " , reason: " + e.getMessage()); - } finally { - cssFile.delete(); } BufferedImage originImage = imagePointer[0]; diff --git a/Mage.Plugins/Mage.Counter.Plugin/src/main/java/org/mage/plugins/counter/CounterPluginImpl.java b/Mage.Plugins/Mage.Counter.Plugin/src/main/java/org/mage/plugins/counter/CounterPluginImpl.java index 7c7efdf462..b1d067c328 100644 --- a/Mage.Plugins/Mage.Counter.Plugin/src/main/java/org/mage/plugins/counter/CounterPluginImpl.java +++ b/Mage.Plugins/Mage.Counter.Plugin/src/main/java/org/mage/plugins/counter/CounterPluginImpl.java @@ -41,6 +41,7 @@ public class CounterPluginImpl implements CounterPlugin { File data = new File(PLUGIN_DATA_FOLDER_PATH + File.separator + DATA_STORAGE_FILE); if (!data.exists()) { try { + data.getParentFile().mkdirs(); data.createNewFile(); } catch (IOException e) { log.error(e.getMessage(), e);