Skip to content

Commit

Permalink
Add Monocle support (qzind#632)
Browse files Browse the repository at this point in the history
Add Monocle support, performance improvements
This is a 2.1 refactor of qzind#580

Co-authored-by: Berenz <thebrettberenz@gmail.com>
  • Loading branch information
tresf and Berenz authored May 26, 2020
1 parent 4bf9704 commit 409be26
Show file tree
Hide file tree
Showing 13 changed files with 580 additions and 136 deletions.
10 changes: 5 additions & 5 deletions ant/javafx.xml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
<!-- Downloads and extracts javafx for macOS -->
<target name="download-javafx-mac" if="javafx.mac.needed" unless="javafx.mac.found" depends="check-javafx-needed,check-javafx-found">
<antcall target="download-javafx-platform">
<param name="javafx.platform" value="mac"/>
<param name="javafx.platform" value="osx"/>
</antcall>
</target>

Expand All @@ -116,8 +116,8 @@

<!-- Downloads and extracts javafx for the specified platform -->
<target name="download-javafx-platform" depends="get-javafx-version">
<get src="${javafx.mirror}/javafx-${javafx.version-url}-sdk-${javafx.platform}/" verbose="true" dest="javafx-${javafx.platform}.zip"/>
<unzip src="javafx-${javafx.platform}.zip" dest="lib/javafx/${javafx.platform}" overwrite="true"/>
<get src="${javafx.mirror}/openjfx-${javafx.version-url}-${javafx.platform}-x64_bin-sdk.zip" verbose="true" dest="javafx-${javafx.platform}.zip"/>
<unzip src="javafx-${javafx.platform}.zip" dest="lib/javafx/${javafx.platform}/javafx-${javafx.version}" overwrite="true"/>
<delete file="javafx-${javafx.platform}.zip"/>
</target>

Expand All @@ -126,7 +126,7 @@
<delete>
<fileset dir="lib">
<include name="**/javafx*/**"/>
<exclude name="**/javafx*${javafx.version}*/**"/>
<exclude name="**/javafx-${javafx.version}/**"/>
</fileset>
</delete>
</target>
Expand Down Expand Up @@ -223,4 +223,4 @@
<not><equals arg1="${javafx.linux.files}" arg2=""/></not>
</condition>
</target>
</project>
</project>
4 changes: 2 additions & 2 deletions ant/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ jar.index=true
# See also qz.common.Constants.java
javac.source=1.8
javac.target=1.8
javafx.version=11.0.2
javafx.mirror=https://gluonhq.com/download
javafx.version=15.ea+3_monocle
javafx.mirror=https://download2.gluonhq.com/openjfx/15
java.download=https://adoptopenjdk.net/?variant=openjdk11
2 changes: 1 addition & 1 deletion sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ <h4 class="panel-title">Options</h4>

/// Pixel Printers ///
function printHTML() {
var config = getUpdatedConfig({ 'pxlScale': false });
var config = getUpdatedConfig();
var opts = getUpdatedOptions(true);

var printData = [
Expand Down
1 change: 1 addition & 0 deletions src/qz/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public class Constants {

public static final String PREFS_NOTIFICATIONS = "tray.notifications";
public static final String PREFS_HEADLESS = "tray.headless";
public static final String PREFS_MONOCLE = "tray.monocle";

public static final String WHITE_LIST = "Permanently allowed \"%s\" to access local resources";
public static final String BLACK_LIST = "Permanently blocked \"%s\" from accessing local resources";
Expand Down
7 changes: 4 additions & 3 deletions src/qz/common/PropertyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public PropertyHelper(String file) {
}

public boolean getBoolean(String key, boolean defaultVal) {
try {
return Boolean.parseBoolean(getProperty(key));
} catch (Throwable t) {
String prop = getProperty(key);
if (prop != null) {
return Boolean.parseBoolean(prop);
} else {
return defaultVal;
}
}
Expand Down
31 changes: 30 additions & 1 deletion src/qz/common/TrayManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

package qz.common;

import com.github.zafarkhaja.semver.Version;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
Expand Down Expand Up @@ -242,6 +243,16 @@ private void addMenuItems() {
notificationsItem.addActionListener(notificationsListener);
diagnosticMenu.add(notificationsItem);

JCheckBoxMenuItem monocleItem = new JCheckBoxMenuItem("Use Monocle for HTML");
monocleItem.setToolTipText("Use monocle platform for HTML printing (restart required)");
monocleItem.setMnemonic(KeyEvent.VK_U);
monocleItem.setState(prefs.getBoolean(Constants.PREFS_MONOCLE, true));
monocleItem.addActionListener(monocleListener);

if (Constants.JAVA_VERSION.greaterThanOrEqualTo(Version.valueOf("11.0.0"))) { //only include if it can be used
diagnosticMenu.add(monocleItem);
}

diagnosticMenu.add(new JSeparator());

JMenuItem logItem = new JMenuItem("View logs (live feed)...", iconCache.getIcon(IconCache.Icon.LOG_ICON));
Expand Down Expand Up @@ -326,6 +337,16 @@ public void actionPerformed(ActionEvent e) {
}
};

private final ActionListener monocleListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JCheckBoxMenuItem j = (JCheckBoxMenuItem)e.getSource();
prefs.setProperty(Constants.PREFS_MONOCLE, j.getState());
displayWarningMessage(String.format("A restart of %s is required to ensure this feature is %sabled.",
Constants.ABOUT_TITLE, j.getState()? "en":"dis"));
}
};

private final ActionListener desktopListener() {
return e -> {
shortcutCreator.createDesktopShortcut();
Expand Down Expand Up @@ -577,7 +598,7 @@ private void displayMessage(final String caption, final String text, final TrayI
if (tray != null) {
SwingUtilities.invokeLater(() -> {
boolean showAllNotifications = prefs.getBoolean(Constants.PREFS_NOTIFICATIONS, false);
if (showAllNotifications || level == TrayIcon.MessageType.ERROR) {
if (showAllNotifications || level != TrayIcon.MessageType.INFO) {
tray.displayMessage(caption, text, level);
}
});
Expand All @@ -595,4 +616,12 @@ public void singleInstanceCheck(java.util.List<Integer> insecurePorts, Integer i
}
}

public boolean isMonoclePreferred() {
return prefs.getBoolean(Constants.PREFS_MONOCLE, true);
}

public boolean isHeadless() {
return headless;
}

}
2 changes: 1 addition & 1 deletion src/qz/printer/action/PrintHTML.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce
PrintingUtilities.Flavor flavor = PrintingUtilities.Flavor.valueOf(data.optString("flavor", "FILE").toUpperCase(Locale.ENGLISH));

double pageZoom = (pxlOpts.getDensity() * pxlOpts.getUnits().as1Inch()) / 72.0;
if (pageZoom <= 1 || data.optBoolean("forceOriginal")) { pageZoom = 1; }
if (pageZoom <= 1) { pageZoom = 1; }

double pageWidth = 0;
double pageHeight = 0;
Expand Down
42 changes: 29 additions & 13 deletions src/qz/printer/action/PrintImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,9 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws
double imgH = imgToPrint.getHeight() / dpiScale;

if (scaleImage) {
//scale up to print density (using less of a stretch if image is already larger than page)
double upScale = dpiScale * Math.min((pageFormat.getImageableWidth() / imgToPrint.getWidth()), (pageFormat.getImageableHeight() / imgToPrint.getHeight()));
if (upScale > dpiScale) { upScale = dpiScale; } else if (upScale < 1) { upScale = 1; }
log.debug("Scaling image up by x{}", upScale);

BufferedImage scaled = new BufferedImage((int)(imgToPrint.getWidth() * upScale), (int)(imgToPrint.getHeight() * upScale), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = scaled.createGraphics();
g2d.setRenderingHints(buildRenderingHints(dithering, interpolation));
g2d.drawImage(imgToPrint, 0, 0, (int)(imgToPrint.getWidth() * upScale), (int)(imgToPrint.getHeight() * upScale), null);
g2d.dispose();
imgToPrint = scale(imgToPrint, pageFormat);

imgToPrint = scaled;

// scale image to smallest edge, keeping size ratio
// adjust dimensions to smallest edge, keeping size ratio
if (((float)imgToPrint.getWidth() / (float)imgToPrint.getHeight()) >= (boundW / boundH)) {
imgW = boundW;
imgH = (imgToPrint.getHeight() / (imgToPrint.getWidth() / boundW));
Expand Down Expand Up @@ -243,6 +232,33 @@ public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws
return PAGE_EXISTS;
}

/**
*
* @param image
* @param pageFormat
* @return
*/
private BufferedImage scale(BufferedImage image, PageFormat pageFormat) {
//scale up to print density (using less of a stretch if image is already larger than page)
double upScale = dpiScale * Math.min((pageFormat.getImageableWidth() / image.getWidth()), (pageFormat.getImageableHeight() / image.getHeight()));
if (upScale > dpiScale) { upScale = dpiScale; } else if (upScale < 1) { upScale = 1; }

if (upScale > 1) {
log.debug("Scaling image up by x{}", upScale);

BufferedImage scaled = new BufferedImage((int)(image.getWidth() * upScale), (int)(image.getHeight() * upScale), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = scaled.createGraphics();
g2d.setRenderingHints(buildRenderingHints(dithering, interpolation));
g2d.drawImage(image, 0, 0, (int)(image.getWidth() * upScale), (int)(image.getHeight() * upScale), null);
g2d.dispose();

return scaled;
} else {
log.debug("No need to upscale image");
return image;
}
}

/**
* Rotates {@code image} by the specified {@code angle}.
*
Expand Down
Loading

0 comments on commit 409be26

Please sign in to comment.