1 /*********************************************************************************************************************** 2 * Copyright (c) 2003, International Barcode Consortium 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * Redistributions of source code must retain the above copyright notice, this list of 9 * conditions and the following disclaimer. 10 * Redistributions in binary form must reproduce the above copyright notice, this list of 11 * conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse 14 * or promote products derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 18 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 ***********************************************************************************************************************/ 26 27 package net.sourceforge.barbecue.output; 28 29 import java.awt.*; 30 import java.awt.font.TextLayout; 31 32 /** 33 * Graphics based outputter to draw barcodes to Graphics objects for printing 34 * and display. 35 * 36 * @author Ryan Martell 37 * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a> 38 */ 39 public class GraphicsOutput extends AbstractOutput { 40 41 private final Graphics2D g; 42 private Color savedColour; 43 44 /** 45 * Creates a Graphics2D AbstractOutput 46 * @param graphics The graphics to output to 47 * @param font The font for text rendering 48 * @param fgColor Foreground Color 49 * @param bgColor Background Color 50 */ 51 public GraphicsOutput(Graphics2D graphics, Font font, Color fgColor, Color bgColor) { 52 super(font, true, 1.0, fgColor, bgColor); 53 this.g = graphics; 54 } 55 56 /** 57 * From AbstractOutput - Saves current colour. 58 */ 59 public void beginDraw() { 60 savedColour = g.getColor(); 61 } 62 63 /** 64 * From AbstractOutput - Restores colour. 65 * @param width The output width (in pixels) of the barcode 66 * @param height The output height (in pixels) of the barcode 67 */ 68 public void endDraw(int width, int height) { 69 g.setColor(savedColour); 70 } 71 72 /** 73 * From AbstractOutput - Draws a bar at the given coordinates onto the output Graphics. 74 * @param x the x coordinate 75 * @param y the y coordinate 76 * @param width the width 77 * @param height the height 78 * @param paintWithForegroundColor if true, use the foreground color, otherwise use the background color 79 */ 80 public int drawBar(int x, int y, int width, int height, boolean paintWithForegroundColor) { 81 if (painting) { 82 if (paintWithForegroundColor) { 83 g.setColor(this.foregroundColour); 84 } else { 85 g.setColor(this.backgroundColour); 86 } 87 88 g.fillRect((int) (scalar * x), (int) (scalar * y), 89 (int) (scalar * width), (int) (scalar * height)); 90 } 91 92 return width; 93 } 94 95 public int drawText(String text, LabelLayout labelLayout) throws OutputException { 96 if(font == null) { 97 return 0; 98 } else { 99 TextLayout layout = new TextLayout(text, font, g.getFontRenderContext()); 100 labelLayout.setTextLayout(layout); 101 102 if (painting) { 103 g.setColor(backgroundColour); 104 g.fillRect(labelLayout.getBackgroundX(), labelLayout.getBackgroundY(), labelLayout.getBackgroundWidth(), labelLayout.getBackgroundHeight()); 105 g.setColor(foregroundColour); 106 layout.draw(g, labelLayout.getTextX(), labelLayout.getTextY()); 107 } 108 109 return labelLayout.getBackgroundHeight(); 110 } 111 } 112 113 /** 114 * Paint the background the background colour, based on the height and the width. 115 * @param x the x coordinate 116 * @param y the y coordinate 117 * @param width the width to be painted 118 * @param height the height to be painted 119 */ 120 public void paintBackground(int x, int y, int width, int height) { 121 if(!painting) { 122 return; 123 } 124 g.setColor(backgroundColour); 125 g.fillRect(x, y, width, height); 126 } 127 128 }