Skip to content

Commit 8169010

Browse files
committed
Now the IDE use vectorial images whenever possible
The caller of Theme.getThemeImage(...) now pass only the name of the needed resource and the theme folder is searche in the following order: - name.svg - name.png (if svg is not available) - [email protected] (if none of the above are available or if 1x png is too low resolution for the current scaling factor)
1 parent 6e4b946 commit 8169010

File tree

6 files changed

+87
-40
lines changed

6 files changed

+87
-40
lines changed

app/src/cc/arduino/view/preferences/Preferences.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
197197

198198
additionalBoardsManagerField.setToolTipText(tr("Enter a comma separated list of urls"));
199199

200-
extendedAdditionalUrlFieldWindow.setIcon(new ImageIcon(Theme.getThemeImage("newwindow.png", this)));
200+
extendedAdditionalUrlFieldWindow.setIcon(new ImageIcon(Theme.getThemeImage("newwindow", this, Theme.scale(16), Theme.scale(14))));
201201
extendedAdditionalUrlFieldWindow.setMargin(new java.awt.Insets(1, 1, 1, 1));
202202
extendedAdditionalUrlFieldWindow.addActionListener(new java.awt.event.ActionListener() {
203203
public void actionPerformed(java.awt.event.ActionEvent evt) {

app/src/processing/app/Base.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import processing.app.debug.TargetPackage;
4444
import processing.app.debug.TargetPlatform;
4545
import processing.app.helpers.*;
46-
import processing.app.helpers.FileUtils.SplitFile;
4746
import processing.app.helpers.filefilters.OnlyDirs;
4847
import processing.app.helpers.filefilters.OnlyFilesWithExtension;
4948
import processing.app.javax.swing.filechooser.FileNameExtensionFilter;
@@ -1755,7 +1754,8 @@ static public String[] headerListFromIncludePath(File path) throws IOException {
17551754
*/
17561755
@SuppressWarnings("serial")
17571756
public void handleAbout() {
1758-
final Image image = Theme.getLibImage("about.png", activeEditor);
1757+
final Image image = Theme.getLibImage("about", activeEditor,
1758+
Theme.scale(475), Theme.scale(300));
17591759
final Window window = new Window(activeEditor) {
17601760
public void paint(Graphics g) {
17611761
g.drawImage(image, 0, 0, null);

app/src/processing/app/EditorHeader.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public class EditorHeader extends JComponent {
7373
static final int MENU = 3;
7474

7575
static final int PIECE_WIDTH = scale(4);
76+
static final int PIECE_HEIGHT = scale(33);
7677

7778
// value for the size bars, buttons, etc
7879
static final int GRID_SIZE = scale(33);
@@ -150,8 +151,12 @@ public EditorHeader(Editor eddie) {
150151
pieces = new Image[STATUS.length][WHERE.length];
151152
for (int i = 0; i < STATUS.length; i++) {
152153
for (int j = 0; j < WHERE.length; j++) {
153-
String path = "tab-" + STATUS[i] + "-" + WHERE[j] + ".png";
154-
pieces[i][j] = Theme.getThemeImage(path, this);
154+
String path = "tab-" + STATUS[i] + "-" + WHERE[j];
155+
pieces[i][j] = Theme.getThemeImage(path, this,
156+
// TODO: Refactor this mess...
157+
j == MENU ? PIECE_HEIGHT
158+
: PIECE_WIDTH,
159+
PIECE_HEIGHT);
155160
}
156161
}
157162
}

app/src/processing/app/EditorLineStatus.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public EditorLineStatus() {
6060
high = scale(Theme.getInteger("linestatus.height"));
6161

6262
if (OSUtils.isMacOS()) {
63-
resize = Theme.getThemeImage("resize.png", this);
63+
resize = Theme.getThemeImage("resize", this, RESIZE_IMAGE_SIZE, RESIZE_IMAGE_SIZE);
6464
}
6565
//linestatus.bgcolor = #000000
6666
//linestatus.font = SansSerif,plain,10

app/src/processing/app/EditorToolbar.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ public EditorToolbar(Editor editor, JMenu menu) {
139139
}
140140

141141
private void loadButtons() {
142-
Image allButtons = Theme.getThemeImage("buttons.png", this);
142+
Image allButtons = Theme.getThemeImage("buttons", this,
143+
BUTTON_IMAGE_SIZE * BUTTON_COUNT,
144+
BUTTON_IMAGE_SIZE * 3);
143145
buttonImages = new Image[BUTTON_COUNT][3];
144146

145147
for (int i = 0; i < BUTTON_COUNT; i++) {

app/src/processing/app/Theme.java

+73-33
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,35 @@
2121

2222
package processing.app;
2323

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;
2925

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;
3234
import java.awt.font.TextAttribute;
35+
import java.io.ByteArrayOutputStream;
3336
import java.io.File;
37+
import java.net.URL;
3438
import java.util.HashMap;
3539
import java.util.Hashtable;
3640
import java.util.Map;
3741

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;
3953

4054
/**
4155
* 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) {
200214
/**
201215
* Return an Image object from inside the Processing lib folder.
202216
*/
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());
220246
}
221247

222-
Image image = tk.getImage(imageFile.getAbsolutePath());
223248
MediaTracker tracker = new MediaTracker(who);
224-
tracker.addImage(image, 0);
225249
try {
250+
tracker.addImage(image, 0);
226251
tracker.waitForAll();
227252
} catch (InterruptedException e) {
228253
}
229254

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) {
233256
image = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
234-
tracker.addImage(image, 1);
235257
try {
258+
tracker.addImage(image, 1);
236259
tracker.waitForAll();
237260
} catch (InterruptedException e) {
238261
}
239262
}
263+
240264
return image;
241265
}
242266

243267
/**
244268
* Get an image associated with the current color theme.
245269
*/
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);
248288
}
249289

250290
}

0 commit comments

Comments
 (0)