Skip to content

Commit

Permalink
Initial page size support (qzind#1148)
Browse files Browse the repository at this point in the history
* Initial page size support
  • Loading branch information
tresf authored Sep 12, 2023
1 parent e1b82dc commit 4024f22
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 5 deletions.
4 changes: 4 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,10 @@ button > .fa-caret-down {
border-bottom: 1px dashed;
}

summary {
cursor: pointer;
}

code {
color: #31708F;
background-color: #D9EDF7;
Expand Down
40 changes: 40 additions & 0 deletions sample.html
Original file line number Diff line number Diff line change
Expand Up @@ -1613,13 +1613,53 @@ <h4 class="panel-title">Options</h4>
"<li><strong>Density:</strong> " + data[i].density + "dpi</li>" +
"<li><strong>Connection:</strong> " + data[i].connection + "</li>" +
(data[i].trays ? "<li><strong>Trays:</strong> " + data[i].trays + "</li>" : "") +
accumulateSizes(data[i]) +
"</ul></li>";
}

pinMessage("<strong>Printer details:</strong><br/><ul>" + list + "</ul>");
}).catch(displayError);
}

function accumulateSizes(data) {
var html = "";
if(data.sizes) {
var html = "<li><details><summary><strong>Sizes:</strong> (" + data.sizes.length + ")</summary> ";
var sizes = data.sizes;
html += "<ul>";
for(var size in sizes) {
html += "<li><details><summary>" + sizes[size].name + "</summary><ul>";

var inch = sizes[size].in.width + " x " + sizes[size].in.height;
var mill = sizes[size].mm.width + " x " + sizes[size].mm.height;

var inchTrunc = truncate(sizes[size].in.width, 3) + "&nbsp;x&nbsp;" + truncate(sizes[size].in.height, 3);
var millTrunc = truncate(sizes[size].mm.width, 3) + "&nbsp;x&nbsp;" + truncate(sizes[size].mm.height, 3);

html += "<li style='text-overflow: ellipsis;' title='" + inch + "'><strong>in:</strong>&nbsp;" + inchTrunc + "</li>";
html += "<li style='text-overflow: ellipsis;' title='" + mill + "'><strong>mm:</strong>&nbsp;" + millTrunc + "</li>";

html += "</ul></details></li>";
}
html += "</ul></details></li>";
}
return html;
}

function truncate(val, length, ellipsis) {
var truncated;
if(isNaN(val)) {
truncated = val.substring(0, length);
} else {
var mult = Math.pow(10, length);
truncated = Math.floor(val * mult) / mult;
}
if(ellipsis === false) {
return truncated;
}
return val === truncated ? val : truncated + "&hellip;";
}


/// Raw Printers ///
function printCommand() {
Expand Down
34 changes: 29 additions & 5 deletions src/qz/printer/PrintServiceMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.ResolutionSyntax;
import javax.print.attribute.standard.Media;
import javax.print.attribute.standard.MediaTray;
import javax.print.attribute.standard.PrinterName;
import javax.print.attribute.standard.PrinterResolution;
import java.util.Locale;
import javax.print.attribute.standard.*;
import java.util.*;

public class PrintServiceMatcher {
private static final Logger log = LogManager.getLogger(PrintServiceMatcher.class);
Expand Down Expand Up @@ -174,8 +171,35 @@ public static JSONArray getPrintersJSON(boolean includeDetails) throws JSONExcep
log.info("Gathering printer MediaTray information...");
mediaTrayCrawled = true;
}

HashSet<String> uniqueSizes = new HashSet<>();

for(Media m : (Media[])ps.getSupportedAttributeValues(Media.class, null, null)) {
if (m instanceof MediaTray) { jsonService.accumulate("trays", m.toString()); }
if (m instanceof MediaSizeName) {
if(uniqueSizes.add(m.toString())) {
MediaSize mediaSize = MediaSize.getMediaSizeForName((MediaSizeName)m);
if(mediaSize == null) {
continue;
}

JSONObject sizes = new JSONObject();
sizes.put("name", m.toString());

JSONObject in = new JSONObject();
in.put("width", mediaSize.getX(MediaPrintableArea.INCH));
in.put("height", mediaSize.getY(MediaPrintableArea.INCH));
sizes.put("in", in);

JSONObject mm = new JSONObject();
mm.put("width", mediaSize.getX(MediaPrintableArea.MM));
mm.put("height", mediaSize.getY(MediaPrintableArea.MM));
sizes.put("mm", mm);

jsonService.accumulate("sizes", sizes);
}

}
}

PrinterResolution res = printer.getResolution().value();
Expand Down

0 comments on commit 4024f22

Please sign in to comment.