Skip to content

Commit

Permalink
Added multi-resolution SVG image icon transcoder
Browse files Browse the repository at this point in the history
  • Loading branch information
leifeld committed May 4, 2023
1 parent c6511fe commit 554ba8d
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 14 deletions.
8 changes: 4 additions & 4 deletions build/bibliography.md
Expand Up @@ -4,7 +4,7 @@ author:
bibliography:
- bibliography.bib
csl: apa-numeric-superscript-brackets.csl
date: 2023-03-01
date: 2023-05-04
title: "Discourse Network Analysis: Bibliography"
---

Expand Down Expand Up @@ -1988,9 +1988,9 @@ Paris climate change conference in 2015. *Environmental Communication*,

<div id="ref-swedenmark2018vi" class="csl-entry">

215\. Swedenmark, S. Ö. (2018). *“Vi ska sträcka oss lite längre <span
class="nocase">ä</span>n vi behöver.” En fallstudie om diskursen kring
mellanorganisatorisk samverkan inom Stockholmsregionen*
215\. Swedenmark, S. Ö. (2018). *“Vi ska sträcka oss lite längre
<span class="nocase">ä</span>n vi behöver.” En fallstudie om diskursen
kring mellanorganisatorisk samverkan inom Stockholmsregionen*
\[Magisteruppsats i statsvetenskap, Mittuniversitetet\].
<https://www.diva-portal.org/smash/get/diva2:1239608/FULLTEXT01.pdf>

Expand Down
3 changes: 3 additions & 0 deletions dna/build.gradle
Expand Up @@ -77,4 +77,7 @@ dependencies {
implementation group: 'me.tongfei', name: 'progressbar', version: '0.9.4'
// https://mvnrepository.com/artifact/org.ojalgo/ojalgo
implementation group: 'org.ojalgo', name: 'ojalgo', version: '51.4.1'
// https://mvnrepository.com/artifact/org.apache.xmlgraphics/batik-transcoder
implementation group: 'org.apache.xmlgraphics', name: 'batik-transcoder', version: '1.16'

}
2 changes: 1 addition & 1 deletion dna/src/main/java/dna/Dna.java
Expand Up @@ -27,7 +27,7 @@ public class Dna {
public static Dna dna;
public static Logger logger;
public static Sql sql;
public static final String date = "2023-03-05";
public static final String date = "2023-05-04";
public static final String version = "3.1.0";
public static final String operatingSystem = System.getProperty("os.name");
public static File workingDirectory = null;
Expand Down
17 changes: 8 additions & 9 deletions dna/src/main/java/gui/PopupMulti.java
Expand Up @@ -7,9 +7,9 @@
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
Expand Down Expand Up @@ -130,7 +130,7 @@ public class PopupMulti extends JDialog {
colorPanel.setBackground(color);
colorPanel.setPreferredSize(new Dimension(4, 4));

ImageIcon addRoleIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-row-insert-bottom.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon addRoleIcon = new SvgIcon("/icons/tabler_text_plus.svg", 14).getImageIcon();
addRole = new JButton(addRoleIcon);
addRole.setToolTipText("add an additional role to this statement...");
addRole.setMargin(new Insets(0, 0, 0, 0));
Expand Down Expand Up @@ -182,22 +182,22 @@ public void mouseClicked(MouseEvent e) {
});
addRole.setEnabled(this.editable);

ImageIcon duplicateIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-copy.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon duplicateIcon = new SvgIcon("/icons/tabler_copy.svg", 14).getImageIcon();
duplicate = new JButton(duplicateIcon);
duplicate.setToolTipText("create a copy of this statement at the same location");
duplicate.setMargin(new Insets(0, 0, 0, 0));
duplicate.setContentAreaFilled(false);
duplicate.setEnabled(this.coder.isPermissionAddStatements());

ImageIcon removeIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-trash.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon removeIcon = new SvgIcon("/icons/tabler_trash.svg", 14).getImageIcon();
remove = new JButton(removeIcon);
remove.setToolTipText("completely remove the whole statement (but keep the text)");
remove.setMargin(new Insets(0, 0, 0, 0));
remove.setContentAreaFilled(false);
remove.setEnabled((this.tableStatement.getCoderId() == this.coder.getId() && this.coder.isPermissionDeleteStatements()) ||
(this.tableStatement.getCoderId() != this.coder.getId() && this.editable));

ImageIcon revertIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-rotate-clockwise.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon revertIcon = new SvgIcon("/icons/google_device_reset.svg", 14).getImageIcon();
JButton revertButton = new JButton(revertIcon);
revertButton.setToolTipText("revert any changes to the state when the statement was opened");
revertButton.setMargin(new Insets(0, 0, 0, 0));
Expand All @@ -219,8 +219,7 @@ public void mouseClicked(MouseEvent e) {
}
});

ImageIcon saveIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/google_round_save_black_48dp.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
// ImageIcon saveIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-device-floppy.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon saveIcon = new SvgIcon("/icons/tabler_device_floppy.svg", 14).getImageIcon();
saveButton = new JButton(saveIcon);
saveButton.setToolTipText("save the contents of the statement into the database");
saveButton.setMargin(new Insets(0, 0, 0, 0));
Expand Down Expand Up @@ -616,7 +615,7 @@ private void save() {
// column 3: remove the roleValue row
gbc.insets = new Insets(3, 2, 3, 5);
gbc.gridx++;
ImageIcon addIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-arrows-split-2.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon addIcon = new SvgIcon("/icons/tabler_plus.svg", 14).getImageIcon();
JButton addButton = new JButton(addIcon);
addButton.setMargin(new Insets(0, 0, 0, 0));
addButton.setContentAreaFilled(false);
Expand All @@ -639,7 +638,7 @@ private void save() {
// column 4: add a duplicate
gbc.insets = new Insets(3, 0, 3, 5);
gbc.gridx++;
ImageIcon removeIcon = new ImageIcon(new ImageIcon(Objects.requireNonNull(getClass().getResource("/icons/tabler-icon-trash-x-filled.png"))).getImage().getScaledInstance(14, 14, Image.SCALE_SMOOTH));
ImageIcon removeIcon = new SvgIcon("/icons/tabler_minus.svg", 14).getImageIcon();
JButton removeButton = new JButton(removeIcon);
removeButton.setMargin(new Insets(0, 0, 0, 0));
removeButton.setContentAreaFilled(false);
Expand Down
86 changes: 86 additions & 0 deletions dna/src/main/java/gui/SvgIcon.java
@@ -0,0 +1,86 @@
package gui;

import java.awt.image.BaseMultiResolutionImage;
import java.awt.image.BufferedImage;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import dna.Dna;
import logger.LogEvent;
import logger.Logger;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.ImageTranscoder;

import javax.swing.*;

public class SvgIcon {
private BaseMultiResolutionImage b;

public SvgIcon(String svgPath, int baselineSize) {
this.b = this.createMultiImage(svgPath, baselineSize);
}

public ImageIcon getImageIcon() {
return new ImageIcon(this.b);
}

private BaseMultiResolutionImage createMultiImage(String svgPath, int baselineSize) {
double[] zoomFactors = {1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0};
List<BufferedImage> images = new ArrayList<>();
for (double zoomFactor : zoomFactors) {
int size = (int) (baselineSize * zoomFactor);
BufferedImage image = transcodeSvgToImage(svgPath, size, size);
images.add(image);
}
return new BaseMultiResolutionImage(images.stream().toArray(BufferedImage[]::new));
}

private BufferedImage transcodeSvgToImage(String svgPath, int width, int height) {
String inputString = null;
try {
inputString = getClass().getResource(svgPath).toURI().toString();
} catch (URISyntaxException e) {
LogEvent l = new LogEvent(Logger.ERROR,
"Failed to convert SVG resource to URI.",
"Tried to retrieve the SVG file " + svgPath + " from the JAR container, but failed to convert it into a URI string.",
e);
Dna.logger.log(l);
}
BufferedImageTranscoder transcoder = new BufferedImageTranscoder();
transcoder.addTranscodingHint(ImageTranscoder.KEY_WIDTH, (float) width);
transcoder.addTranscodingHint(ImageTranscoder.KEY_HEIGHT, (float) height);
TranscoderInput input = new TranscoderInput(inputString);
try {
transcoder.transcode(input, null);
} catch (TranscoderException e) {
LogEvent l = new LogEvent(Logger.ERROR,
"Failed to transcode SVG image.",
"Tried to transcode SVG file " + svgPath + " into a rasterized image for use as an icon.",
e);
Dna.logger.log(l);
}
return transcoder.getImage();
}

private static class BufferedImageTranscoder extends ImageTranscoder {
private BufferedImage image = null;

public BufferedImage getImage() {
return image;
}

@Override
public BufferedImage createImage(int w, int h) {
BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
return bi;
}

@Override
public void writeImage(BufferedImage img, TranscoderOutput output) {
image = img;
}
}
}
1 change: 1 addition & 0 deletions dna/src/main/resources/icons/google_device_reset.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
7 changes: 7 additions & 0 deletions dna/src/main/resources/icons/tabler_copy.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions dna/src/main/resources/icons/tabler_device_floppy.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions dna/src/main/resources/icons/tabler_minus.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions dna/src/main/resources/icons/tabler_plus.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions dna/src/main/resources/icons/tabler_text_plus.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions dna/src/main/resources/icons/tabler_trash.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 554ba8d

Please sign in to comment.