1 58 package org.krysalis.barcode.output.java2d; 59 60 import java.awt.BasicStroke ; 61 import java.awt.Font ; 62 import java.awt.Graphics2D ; 63 import java.awt.font.FontRenderContext ; 64 import java.awt.font.GlyphVector ; 65 import java.awt.geom.Point2D ; 66 import java.awt.geom.Rectangle2D ; 67 68 import org.krysalis.barcode.output.AbstractCanvasProvider; 69 import org.krysalis.barcode.tools.UnitConv; 70 71 76 public class Java2DCanvasProvider extends AbstractCanvasProvider { 77 78 private static final boolean DEBUG = false; 79 80 private Graphics2D g2d; 81 82 99 public Java2DCanvasProvider(Graphics2D g2d) { 100 this.g2d = g2d; 101 } 102 103 104 public void deviceFillRect(double x, double y, double w, double h) { 105 g2d.fill(new Rectangle2D.Double (x, y, w, h)); 106 } 107 108 109 public void deviceDrawRect(double x, double y, double w, double h) { 110 g2d.draw(new Rectangle2D.Double (x, y, w, h)); 111 } 112 113 114 public void deviceJustifiedText(String text, double x1, double x2, double y1, 115 String fontName, double fontSize) { 116 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, true); 117 } 118 119 120 public void deviceCenteredText(String text, double x1, double x2, double y1, 121 String fontName, double fontSize) { 122 deviceCenteredText(text, x1, x2, y1, fontName, fontSize, false); 123 } 124 125 135 public void deviceCenteredText( 136 String text, 137 double x1, 138 double x2, 139 double y1, 140 String fontName, 141 double fontSize, 142 boolean justify) { 143 if (DEBUG) { 144 System.out.println("deviceText " + x1 + " " + x2 + " " 145 + (x2 - x1) + " " + y1 + " " + text); 146 System.out.println("fontSize: " 147 + fontSize + "pt (" + UnitConv.pt2mm(fontSize) + "mm)"); 148 } 149 Font font = new Font (fontName, Font.PLAIN, 150 (int)Math.round(UnitConv.pt2mm(fontSize))); 151 FontRenderContext frc = g2d.getFontRenderContext(); 152 GlyphVector gv = font.createGlyphVector(frc, text); 153 154 final float textwidth = (float)gv.getLogicalBounds().getWidth(); 155 final float distributableSpace = (float)((x2 - x1) - textwidth); 156 final float intercharSpace; 157 if (gv.getNumGlyphs() > 1) { 158 intercharSpace = distributableSpace / (gv.getNumGlyphs() - 1); 159 } else { 160 intercharSpace = 0.0f; 161 } 162 if (DEBUG) { 163 System.out.println(gv.getLogicalBounds() 164 + " " + gv.getVisualBounds()); 165 System.out.println("textwidth=" + textwidth); 166 System.out.println("distributableSpace=" + distributableSpace); 167 System.out.println("intercharSpace=" + intercharSpace); 168 } 169 final float indent; 170 if (justify && text.length() > 1) { 171 indent = 0.0f; 172 } else { 173 indent = distributableSpace / 2; 174 } 175 Font oldFont = g2d.getFont(); 176 g2d.setFont(font); 177 if (justify) { 178 float rx = 0.0f; 180 for (int i = 0; i < gv.getNumGlyphs(); i++) { 181 Point2D point = gv.getGlyphPosition(i); 182 point.setLocation(point.getX() + i * intercharSpace, point.getY()); 183 gv.setGlyphPosition(i, point); 184 if (DEBUG) { 185 System.out.println(i + " " + point 186 + " " + gv.getGlyphLogicalBounds(i).getBounds2D()); 187 System.out.println(i + " " + text.substring(i, i + 1) 188 + " " + gv.getGlyphMetrics(i).getBounds2D()); 189 } 190 } 191 } 192 g2d.drawGlyphVector(gv, (float)x1 + indent, (float)y1); 193 g2d.setFont(oldFont); 194 if (DEBUG) { 195 g2d.setStroke(new BasicStroke (0.01f)); 196 g2d.draw(new Rectangle2D.Double (x1, y1 - UnitConv.pt2mm(fontSize), 197 x2 - x1, UnitConv.pt2mm(fontSize))); 198 g2d.draw(new Rectangle2D.Double (x1 + indent, 199 y1 - UnitConv.pt2mm(fontSize), 200 textwidth, UnitConv.pt2mm(fontSize))); 201 } 202 } 203 204 } 205 | Popular Tags |