|
21 | 21 |
|
22 | 22 | package processing.app;
|
23 | 23 |
|
24 |
| -import processing.app.helpers.FileUtils; |
25 |
| -import processing.app.helpers.FileUtils.SplitFile; |
26 |
| -import processing.app.helpers.OSUtils; |
27 |
| -import processing.app.helpers.PreferencesHelper; |
28 |
| -import processing.app.helpers.PreferencesMap; |
| 24 | +import static processing.app.I18n.tr; |
29 | 25 |
|
30 |
| -import javax.swing.text.StyleContext; |
31 |
| -import java.awt.*; |
| 26 | +import java.awt.Color; |
| 27 | +import java.awt.Component; |
| 28 | +import java.awt.Font; |
| 29 | +import java.awt.Image; |
| 30 | +import java.awt.MediaTracker; |
| 31 | +import java.awt.RenderingHints; |
| 32 | +import java.awt.SystemColor; |
| 33 | +import java.awt.Toolkit; |
32 | 34 | import java.awt.font.TextAttribute;
|
| 35 | +import java.io.ByteArrayOutputStream; |
33 | 36 | import java.io.File;
|
| 37 | +import java.net.URL; |
34 | 38 | import java.util.HashMap;
|
35 | 39 | import java.util.Hashtable;
|
36 | 40 | import java.util.Map;
|
37 | 41 |
|
38 |
| -import static processing.app.I18n.tr; |
| 42 | +import javax.swing.text.StyleContext; |
| 43 | + |
| 44 | +import org.apache.batik.transcoder.Transcoder; |
| 45 | +import org.apache.batik.transcoder.TranscoderException; |
| 46 | +import org.apache.batik.transcoder.TranscoderInput; |
| 47 | +import org.apache.batik.transcoder.TranscoderOutput; |
| 48 | +import org.apache.batik.transcoder.image.PNGTranscoder; |
| 49 | + |
| 50 | +import processing.app.helpers.OSUtils; |
| 51 | +import processing.app.helpers.PreferencesHelper; |
| 52 | +import processing.app.helpers.PreferencesMap; |
39 | 53 |
|
40 | 54 | /**
|
41 | 55 | * Storage class for theme settings. This was separated from the Preferences
|
@@ -200,51 +214,77 @@ public static Map<String, Object> getStyledFont(String what, Font font) {
|
200 | 214 | /**
|
201 | 215 | * Return an Image object from inside the Processing lib folder.
|
202 | 216 | */
|
203 |
| - static public Image getLibImage(String filename, Component who) { |
204 |
| - Toolkit tk = Toolkit.getDefaultToolkit(); |
205 |
| - |
206 |
| - SplitFile name = FileUtils.splitFilename(filename); |
207 |
| - int scale = getScale(); |
208 |
| - File libFolder = Base.getContentFile("lib"); |
209 |
| - File imageFile1x = new File(libFolder, name.basename + "." + name.extension); |
210 |
| - File imageFile2x = new File(libFolder, name.basename + "@2x." + name.extension); |
211 |
| - |
212 |
| - File imageFile; |
213 |
| - int sourceScale; |
214 |
| - if ((scale > 125 && imageFile2x.exists()) || !imageFile1x.exists()) { |
215 |
| - imageFile = imageFile2x; |
216 |
| - sourceScale = 200; |
217 |
| - } else { |
218 |
| - imageFile = imageFile1x; |
219 |
| - sourceScale = 100; |
| 217 | + static public Image getLibImage(String filename, Component who, int width, |
| 218 | + int height) { |
| 219 | + File libFolder = BaseNoGui.getContentFile("lib"); |
| 220 | + Image image = null; |
| 221 | + |
| 222 | + // Use vector image when available |
| 223 | + File vectorFile = new File(libFolder, filename + ".svg"); |
| 224 | + if (vectorFile.exists()) { |
| 225 | + try { |
| 226 | + image = imageFromSVG(vectorFile.toURI().toURL(), width, height); |
| 227 | + } catch (Exception e) { |
| 228 | + System.err.println("Failed to load " + vectorFile.getAbsolutePath() |
| 229 | + + ": " + e.getMessage()); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + // Otherwise fall-back to PNG bitmaps |
| 234 | + if (image == null) { |
| 235 | + File bitmapFile = new File(libFolder, filename + ".png"); |
| 236 | + File bitmap2xFile = new File(libFolder, filename + "@2x.png"); |
| 237 | + |
| 238 | + File imageFile; |
| 239 | + if ((getScale() > 125 && bitmap2xFile.exists()) || !bitmapFile.exists()) { |
| 240 | + imageFile = bitmap2xFile; |
| 241 | + } else { |
| 242 | + imageFile = bitmapFile; |
| 243 | + } |
| 244 | + Toolkit tk = Toolkit.getDefaultToolkit(); |
| 245 | + image = tk.getImage(imageFile.getAbsolutePath()); |
220 | 246 | }
|
221 | 247 |
|
222 |
| - Image image = tk.getImage(imageFile.getAbsolutePath()); |
223 | 248 | MediaTracker tracker = new MediaTracker(who);
|
224 |
| - tracker.addImage(image, 0); |
225 | 249 | try {
|
| 250 | + tracker.addImage(image, 0); |
226 | 251 | tracker.waitForAll();
|
227 | 252 | } catch (InterruptedException e) {
|
228 | 253 | }
|
229 | 254 |
|
230 |
| - if (scale != sourceScale) { |
231 |
| - int width = image.getWidth(null) * scale / sourceScale; |
232 |
| - int height = image.getHeight(null) * scale / sourceScale; |
| 255 | + if (image.getWidth(null) != width || image.getHeight(null) != height) { |
233 | 256 | image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
|
234 |
| - tracker.addImage(image, 1); |
235 | 257 | try {
|
| 258 | + tracker.addImage(image, 1); |
236 | 259 | tracker.waitForAll();
|
237 | 260 | } catch (InterruptedException e) {
|
238 | 261 | }
|
239 | 262 | }
|
| 263 | + |
240 | 264 | return image;
|
241 | 265 | }
|
242 | 266 |
|
243 | 267 | /**
|
244 | 268 | * Get an image associated with the current color theme.
|
245 | 269 | */
|
246 |
| - static public Image getThemeImage(String name, Component who) { |
247 |
| - return getLibImage("theme/" + name, who); |
| 270 | + static public Image getThemeImage(String name, Component who, int width, |
| 271 | + int height) { |
| 272 | + return getLibImage("theme/" + name, who, width, height); |
| 273 | + } |
| 274 | + |
| 275 | + private static Image imageFromSVG(URL url, int width, int height) |
| 276 | + throws TranscoderException { |
| 277 | + Transcoder t = new PNGTranscoder(); |
| 278 | + t.addTranscodingHint(PNGTranscoder.KEY_WIDTH, new Float(width)); |
| 279 | + t.addTranscodingHint(PNGTranscoder.KEY_HEIGHT, new Float(height)); |
| 280 | + |
| 281 | + TranscoderInput input = new TranscoderInput(url.toString()); |
| 282 | + ByteArrayOutputStream ostream = new ByteArrayOutputStream(); |
| 283 | + TranscoderOutput output = new TranscoderOutput(ostream); |
| 284 | + t.transcode(input, output); |
| 285 | + |
| 286 | + byte[] imgData = ostream.toByteArray(); |
| 287 | + return Toolkit.getDefaultToolkit().createImage(imgData); |
248 | 288 | }
|
249 | 289 |
|
250 | 290 | }
|
0 commit comments