|
16 | 16 | import org.apache.logging.log4j.LogManager;
|
17 | 17 | import org.apache.logging.log4j.Logger;
|
18 | 18 | import qz.common.Constants;
|
19 |
| -import qz.common.TrayManager; |
20 | 19 | import qz.printer.PrintOptions;
|
21 | 20 | import qz.printer.PrintOutput;
|
22 | 21 | import qz.utils.ConnectionUtilities;
|
|
26 | 25 | import javax.imageio.IIOException;
|
27 | 26 | import javax.imageio.ImageIO;
|
28 | 27 | import javax.print.attribute.PrintRequestAttributeSet;
|
| 28 | +import javax.print.attribute.ResolutionSyntax; |
29 | 29 | import javax.print.attribute.standard.OrientationRequested;
|
| 30 | +import javax.print.attribute.standard.PrinterResolution; |
30 | 31 | import java.awt.*;
|
31 | 32 | import java.awt.geom.Rectangle2D;
|
32 | 33 | import java.awt.image.BufferedImage;
|
|
39 | 40 | import java.io.IOException;
|
40 | 41 | import java.util.ArrayList;
|
41 | 42 | import java.util.List;
|
42 |
| -import java.util.Locale; |
43 | 43 |
|
44 | 44 |
|
45 | 45 | /**
|
@@ -105,22 +105,40 @@ public void parseData(JSONArray printData, PrintOptions options) throws JSONExce
|
105 | 105 | log.debug("Parsed {} images for printing", images.size());
|
106 | 106 | }
|
107 | 107 |
|
108 |
| - private List<BufferedImage> breakupOverPages(BufferedImage img, PageFormat page) { |
| 108 | + private List<BufferedImage> breakupOverPages(BufferedImage img, PageFormat page, PrintRequestAttributeSet attributes) { |
109 | 109 | List<BufferedImage> splits = new ArrayList<>();
|
110 | 110 |
|
111 | 111 | Rectangle printBounds = new Rectangle(0, 0, (int)page.getImageableWidth(), (int)page.getImageableHeight());
|
| 112 | + PrinterResolution res = (PrinterResolution)attributes.get(PrinterResolution.class); |
| 113 | + float dpi = res.getFeedResolution(1) / (float)ResolutionSyntax.DPI; |
| 114 | + float cdpi = res.getCrossFeedResolution(1) / (float)ResolutionSyntax.DPI; |
112 | 115 |
|
113 |
| - int columnsNeed = (int)Math.ceil(img.getWidth() / page.getImageableWidth()); |
114 |
| - int rowsNeed = (int)Math.ceil(img.getHeight() / page.getImageableHeight()); |
115 |
| - log.trace("Image to be printed across {} pages", columnsNeed * rowsNeed); |
| 116 | + //printing uses 72dpi, convert so we can check split size correctly |
| 117 | + int useWidth = (int)((img.getWidth() / cdpi) * 72); |
| 118 | + int useHeight = (int)((img.getHeight() / dpi) * 72); |
116 | 119 |
|
117 |
| - for(int row = 0; row < rowsNeed; row++) { |
118 |
| - for(int col = 0; col < columnsNeed; col++) { |
119 |
| - Rectangle clip = new Rectangle((col * printBounds.width), (row * printBounds.height), printBounds.width, printBounds.height); |
120 |
| - if (clip.x + clip.width > img.getWidth()) { clip.width = img.getWidth() - clip.x; } |
121 |
| - if (clip.y + clip.height > img.getHeight()) { clip.height = img.getHeight() - clip.y; } |
| 120 | + int columnsNeed = (int)Math.ceil(useWidth / page.getImageableWidth()); |
| 121 | + int rowsNeed = (int)Math.ceil(useHeight / page.getImageableHeight()); |
122 | 122 |
|
123 |
| - splits.add(img.getSubimage(clip.x, clip.y, clip.width, clip.height)); |
| 123 | + if (columnsNeed == 1 && rowsNeed == 1) { |
| 124 | + log.trace("Unscaled image does not need spit"); |
| 125 | + splits.add(img); |
| 126 | + } else { |
| 127 | + log.trace("Image to be printed across {} pages", columnsNeed * rowsNeed); |
| 128 | + //allows us to split the image at the actual dpi instead of 72 |
| 129 | + float upscale = dpi / 72f; |
| 130 | + float c_upscale = cdpi / 72f; |
| 131 | + |
| 132 | + for(int row = 0; row < rowsNeed; row++) { |
| 133 | + for(int col = 0; col < columnsNeed; col++) { |
| 134 | + Rectangle clip = new Rectangle((col * (int)(printBounds.width * c_upscale)), (row * (int)(printBounds.height * upscale)), |
| 135 | + (int)(printBounds.width * c_upscale), (int)(printBounds.height * upscale)); |
| 136 | + |
| 137 | + if (clip.x + clip.width > img.getWidth()) { clip.width = img.getWidth() - clip.x; } |
| 138 | + if (clip.y + clip.height > img.getHeight()) { clip.height = img.getHeight() - clip.y; } |
| 139 | + |
| 140 | + splits.add(img.getSubimage(clip.x, clip.y, clip.width, clip.height)); |
| 141 | + } |
124 | 142 | }
|
125 | 143 | }
|
126 | 144 |
|
@@ -157,7 +175,7 @@ public void print(PrintOutput output, PrintOptions options) throws PrinterExcept
|
157 | 175 | //breakup large images to print across pages as needed
|
158 | 176 | List<BufferedImage> split = new ArrayList<>();
|
159 | 177 | for(BufferedImage bi : images) {
|
160 |
| - split.addAll(breakupOverPages(bi, page)); |
| 178 | + split.addAll(breakupOverPages(bi, page, attributes)); |
161 | 179 | }
|
162 | 180 | images = split;
|
163 | 181 | }
|
|
0 commit comments