Skip to content

Commit

Permalink
Tray list checks by instance (qzind#686)
Browse files Browse the repository at this point in the history
Option matching tries to be less exact

Co-authored-by: Berenz <[email protected]>
  • Loading branch information
tresf and Berenz authored Jul 14, 2020
1 parent 3db7eed commit 5217ef3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/qz/printer/PrintServiceMatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -132,7 +133,7 @@ public static JSONArray getPrintersJSON() throws JSONException {
jsonService.put("default", ps == defaultService);

for(Media m : (Media[])ps.getSupportedAttributeValues(Media.class, null, null)) {
if (m.toString().contains("Tray")) { jsonService.accumulate("trays", m.toString()); }
if (m instanceof MediaTray) { jsonService.accumulate("trays", m.toString()); }
}

PrinterResolution res = printer.getResolution().value();
Expand Down
31 changes: 26 additions & 5 deletions src/qz/printer/action/PrintPixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class PrintPixel {

private static final Logger log = LoggerFactory.getLogger(PrintPixel.class);

private static final List<Integer> MAC_BAD_IMAGE_TYPES = Arrays.asList(BufferedImage.TYPE_BYTE_BINARY, BufferedImage.TYPE_CUSTOM);

private static final List<String> PRINTER_TRAY_ALIASES = Arrays.asList("", "Tray ", "Paper Cassette ");


protected PrintRequestAttributeSet applyDefaultSettings(PrintOptions.Pixel pxlOpts, PageFormat page, Media[] supported) {
PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
Expand All @@ -45,14 +45,35 @@ protected PrintRequestAttributeSet applyDefaultSettings(PrintOptions.Pixel pxlOp
attributes.add(pxlOpts.getOrientation().getAsOrientRequested());
}
if (pxlOpts.getPrinterTray() != null && !pxlOpts.getPrinterTray().isEmpty()) {
Pattern exactPattern = Pattern.compile("\\b" + Pattern.quote(pxlOpts.getPrinterTray()) + "\\b", Pattern.CASE_INSENSITIVE);
Pattern fuzzyPattern = Pattern.compile("\\b.*?[" + Pattern.quote(pxlOpts.getPrinterTray()) +"]+.*?\\b", Pattern.CASE_INSENSITIVE);
Media bestFit = null;
Integer fuzzyFitDelta = null;

for(Media m : supported) {
for(String pta : PRINTER_TRAY_ALIASES) {
if (m.toString().trim().equalsIgnoreCase(pta + pxlOpts.getPrinterTray().trim())) {
attributes.add(m);
if (m instanceof MediaTray) {
Matcher exactly = exactPattern.matcher(m.toString().trim());
Matcher fuzzily = fuzzyPattern.matcher(m.toString().trim());

if (exactly.find()) {
bestFit = m;
break;
}

while(fuzzily.find()) {
//look for as close to exact match as possible
int delta = Math.abs(fuzzily.group().length() - pxlOpts.getPrinterTray().length());
if (fuzzyFitDelta == null || delta < fuzzyFitDelta) {
fuzzyFitDelta = delta;
bestFit = m;
}
}
}
}

if (bestFit != null) {
attributes.add(bestFit);
}
}

//TODO - set paper thickness
Expand Down

0 comments on commit 5217ef3

Please sign in to comment.