From ed3ea068ac80cc3a9e9a4b07599c82d732320017 Mon Sep 17 00:00:00 2001 From: Jakob Heher Date: Wed, 3 Aug 2022 14:43:06 +0200 Subject: don't pointlessly redraw signature preview unless it's actually needed --- .../SimpleConfigurationComposite.java | 106 ++++++++++----------- .../at/asit/pdfover/gui/utils/ImageConverter.java | 63 ------------ 2 files changed, 49 insertions(+), 120 deletions(-) (limited to 'pdf-over-gui/src') diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java index 34155668..e36164f9 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/composites/configuration/SimpleConfigurationComposite.java @@ -45,7 +45,6 @@ import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Canvas; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; -import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Group; import org.eclipse.swt.widgets.Label; @@ -53,7 +52,6 @@ import org.eclipse.swt.widgets.Text; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import at.asit.pdfover.Util; import at.asit.pdfover.commons.Constants; import at.asit.pdfover.commons.Messages; import at.asit.pdfover.commons.Profile; @@ -98,8 +96,6 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { protected final Combo cmbSignatureLang; protected String logoFile = null; - protected Image sigPreview = null; - protected Image logo = null; protected final Group grpSignatureProfile; protected final Combo cmbSignatureProfiles; @@ -195,7 +191,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { this.cSigPreview = new Canvas(containerComposite, SWT.RESIZE); StateComposite.anchor(cSigPreview).left(0, 5).right(100,-5).top(btnBrowseLogo,5).bottom(100,-5).set(); StateComposite.setFontHeight(cSigPreview, Constants.TEXT_SIZE_NORMAL); - this.cSigPreview.addPaintListener(e -> imagePaintControl(e, SimpleConfigurationComposite.this.sigPreview)); + this.cSigPreview.addPaintListener(e -> SimpleConfigurationComposite.this.paintSignaturePreview(e)); DropTarget dnd_target = new DropTarget(containerComposite, DND.DROP_DEFAULT | DND.DROP_COPY); final FileTransfer fileTransfer = FileTransfer.getInstance(); @@ -345,14 +341,49 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { reloadResources(); } - static void imagePaintControl(PaintEvent e, Image i) { - if (i == null) - return; + boolean needSigPreviewUpdate = true; + private Image sigPreview = null; + void paintSignaturePreview(PaintEvent evt) { + if (needSigPreviewUpdate) + { + log.info("updating signature preview"); + String image = this.configurationContainer.getEmblemPath(); + ImageData img = null; + + try { + PdfAs4SignatureParameter param = new PdfAs4SignatureParameter(); + param.signatureProfileName = this.configurationContainer.getSignatureProfile().name(); + if(this.configurationContainer.signatureNote != null && !this.configurationContainer.signatureNote.isEmpty()) { + param.setProperty("SIG_NOTE", this.configurationContainer.signatureNote); + } + + param.signatureLanguage = this.configurationContainer.signatureLocale.getLanguage(); + param.enablePDFACompat = this.configurationContainer.signaturePDFACompat; + if (image != null && !image.trim().isEmpty()) { + param.emblem = new Emblem(image); + } + // TODO this is super slow, can we get rid of it? + // SWT_AWT, same as PositioningComposite, maybe? + // .... i hate this + img = ImageConverter.convertToSWT((BufferedImage) param.getPlaceholder()); + } catch (Exception e) { + log.error("Failed to load image for display...", e); + } + + if (img != null) { + this.sigPreview = new Image(this.getDisplay(), img); + } else { + this.sigPreview = null; + } + needSigPreviewUpdate = false; + } - Rectangle r = i.getBounds(); + if (this.sigPreview == null) + return; + Rectangle r = this.sigPreview.getBounds(); int srcW = r.width; int srcH = r.height; - Point p = ((Control) e.widget).getSize(); + Point p = ((Canvas)evt.widget).getSize(); float dstW = p.x; float dstH = p.y; @@ -365,7 +396,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { int x = (int) ((dstW / 2) - (w / 2)); int y = (int) ((dstH / 2) - (h / 2)); - e.gc.drawImage(i, 0, 0, srcW, srcH, x, y, (int) w, (int) h); + evt.gc.drawImage(this.sigPreview, 0, 0, srcW, srcH, x, y, (int) w, (int) h); } /** @@ -412,51 +443,12 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { this.configurationContainer.setEmblem(filename); this.btnClearImage.setVisible(filename != null); - this.updateSignatureBlockPreview(); + this.signatureBlockPreviewChanged(); this.doLayout(); } - void updateSignatureBlockPreview() { - String image = this.configurationContainer.getEmblemPath(); - ImageData img = null; - ImageData logo = null; - - try { - PdfAs4SignatureParameter param = new PdfAs4SignatureParameter(); - param.signatureProfileName = this.configurationContainer.getSignatureProfile().name(); - if(this.configurationContainer.signatureNote != null && !this.configurationContainer.signatureNote.isEmpty()) { - param.setProperty("SIG_NOTE", this.configurationContainer.signatureNote); - } - - param.signatureLanguage = this.configurationContainer.signatureLocale.getLanguage(); - param.enablePDFACompat = this.configurationContainer.signaturePDFACompat; - if (image != null && !image.trim().isEmpty()) { - logo = new ImageData(image); - param.emblem = new Emblem(image); - } - img = ImageConverter.convertToSWT((BufferedImage) param.getPlaceholder()); - } catch (Exception e) { - log.error("Failed to load image for display...", e); - } - - if (img != null) { - this.sigPreview = new Image(this.getDisplay(), img); - } else { - this.sigPreview = null; - } - - if (logo != null) { - try { - File imgFile = new File(image); - this.logo = new Image(this.getDisplay(), - ImageConverter.convertToSWT(Util.readImageWithEXIFRotation(imgFile))); - } catch (IOException e) { - log.error("Error reading image", e); - } - } else { - this.logo = null; - } - + void signatureBlockPreviewChanged() { + needSigPreviewUpdate = true; this.cSigPreview.redraw(); } @@ -506,7 +498,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { if ((previous != null) && (txtSignatureNote.getText().equals(getDefaultSignatureBlockNoteTextFor(null, previous)))) txtSignatureNote.setText(getDefaultSignatureBlockNoteTextFor(null, selected)); - updateSignatureBlockPreview(); + signatureBlockPreviewChanged(); } @@ -523,7 +515,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { if (txtSignatureNote.getText().equals(getDefaultSignatureBlockNoteTextFor(oldProfile, null))) txtSignatureNote.setText(getDefaultSignatureBlockNoteTextFor(newProfile, null)); - updateSignatureBlockPreview(); + signatureBlockPreviewChanged(); } String getDefaultSignatureBlockNoteTextFor(Profile profile, Locale locale){ @@ -548,7 +540,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { void processSignatureNoteChanged() { String note = this.txtSignatureNote.getText(); this.configurationContainer.signatureNote = note; - updateSignatureBlockPreview(); + signatureBlockPreviewChanged(); } /* @@ -609,7 +601,7 @@ public class SimpleConfigurationComposite extends ConfigurationCompositeBase { this.txtSignatureNote.setText(note); } - this.updateSignatureBlockPreview(); + this.signatureBlockPreviewChanged(); this.performSignatureLangSelectionChanged(this.configurationContainer.signatureLocale, null); diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/ImageConverter.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/ImageConverter.java index 09582f75..1322be06 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/ImageConverter.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/ImageConverter.java @@ -17,7 +17,6 @@ package at.asit.pdfover.gui.utils; // Imports import java.awt.image.BufferedImage; -import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel; import java.awt.image.DirectColorModel; import java.awt.image.IndexColorModel; @@ -31,68 +30,6 @@ import org.eclipse.swt.graphics.RGB; * */ public class ImageConverter { - - /** - * Convert Image Data to AWT Image - * @param data - * @return AWT Image - */ - public static BufferedImage convertToAWT(ImageData data) { - ColorModel colorModel = null; - PaletteData palette = data.palette; - if (palette.isDirect) { - colorModel = new DirectColorModel(data.depth, palette.redMask, - palette.greenMask, palette.blueMask); - BufferedImage bufferedImage = new BufferedImage(colorModel, - colorModel.createCompatibleWritableRaster(data.width, - data.height), false, null); - WritableRaster raster = bufferedImage.getRaster(); - int[] pixelArray = new int[3]; - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - int pixel = data.getPixel(x, y); - RGB rgb = palette.getRGB(pixel); - pixelArray[0] = rgb.red; - pixelArray[1] = rgb.green; - pixelArray[2] = rgb.blue; - raster.setPixels(x, y, 1, 1, pixelArray); - } - } - return bufferedImage; - } - RGB[] rgbs = palette.getRGBs(); - byte[] red = new byte[rgbs.length]; - byte[] green = new byte[rgbs.length]; - byte[] blue = new byte[rgbs.length]; - for (int i = 0; i < rgbs.length; i++) { - RGB rgb = rgbs[i]; - red[i] = (byte) rgb.red; - green[i] = (byte) rgb.green; - blue[i] = (byte) rgb.blue; - } - if (data.transparentPixel != -1) { - colorModel = new IndexColorModel(data.depth, rgbs.length, red, - green, blue, data.transparentPixel); - } else { - colorModel = new IndexColorModel(data.depth, rgbs.length, red, - green, blue); - } - BufferedImage bufferedImage = new BufferedImage(colorModel, - colorModel.createCompatibleWritableRaster(data.width, - data.height), false, null); - WritableRaster raster = bufferedImage.getRaster(); - int[] pixelArray = new int[1]; - for (int y = 0; y < data.height; y++) { - for (int x = 0; x < data.width; x++) { - int pixel = data.getPixel(x, y); - pixelArray[0] = pixel; - raster.setPixel(x, y, pixelArray); - } - } - return bufferedImage; - - } - /** * Convert AWT Image to SWT Image * -- cgit v1.2.3