001: /**
002: * Copyright (c) 2003-2004, www.pdfbox.org
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * 1. Redistributions of source code must retain the above copyright notice,
009: * this list of conditions and the following disclaimer.
010: * 2. Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: * 3. Neither the name of pdfbox; nor the names of its
014: * contributors may be used to endorse or promote products derived from this
015: * software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
020: * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
021: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
024: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: *
028: * http://www.pdfbox.org
029: *
030: */package org.pdfbox.pdmodel.font;
031:
032: import org.pdfbox.cos.COSDictionary;
033: import org.pdfbox.cos.COSName;
034: import org.pdfbox.cos.COSStream;
035: import org.pdfbox.pdmodel.common.PDMatrix;
036:
037: import java.awt.Graphics;
038: import java.awt.Image;
039:
040: import java.io.IOException;
041:
042: import java.util.HashMap;
043: import java.util.Map;
044:
045: /**
046: * This is implementation of the Type3 Font.
047: *
048: * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
049: * @version $Revision: 1.8 $
050: */
051: public class PDType3Font extends PDSimpleFont {
052: //A map of character code to java.awt.Image for the glyph
053: private Map images = new HashMap();
054:
055: /**
056: * Constructor.
057: */
058: public PDType3Font() {
059: super ();
060: font.setItem(COSName.SUBTYPE, COSName.getPDFName("Type3"));
061: }
062:
063: /**
064: * Constructor.
065: *
066: * @param fontDictionary The font dictionary according to the PDF specification.
067: */
068: public PDType3Font(COSDictionary fontDictionary) {
069: super (fontDictionary);
070: }
071:
072: /**
073: * Type3 fonts have their glyphs defined as a content stream. This
074: * will create the image that represents that character
075: *
076: * @throws IOException If there is an error creating the image.
077: */
078: private Image createImageIfNecessary(char character)
079: throws IOException {
080: Character c = new Character(character);
081: Image retval = (Image) images.get(c);
082: if (retval == null) {
083: COSDictionary charProcs = (COSDictionary) font
084: .getDictionaryObject(COSName
085: .getPDFName("CharProcs"));
086: COSStream stream = (COSStream) charProcs
087: .getDictionaryObject(COSName.getPDFName(""
088: + character));
089: if (stream != null) {
090: Type3StreamParser parser = new Type3StreamParser();
091: retval = parser.createImage(stream);
092: images.put(c, retval);
093: } else {
094: //stream should not be null!!
095: }
096: }
097: return retval;
098:
099: }
100:
101: /**
102: * This will draw a string on a canvas using the font.
103: *
104: * @param string The string to draw.
105: * @param g The graphics to draw onto.
106: * @param fontSize The size of the font to draw.
107: * @param x The x coordinate to draw at.
108: * @param y The y coordinate to draw at.
109: *
110: * @throws IOException If there is an error drawing the image on the screen.
111: */
112: public void drawString(String string, Graphics g, float fontSize,
113: float x, float y) throws IOException {
114: //if( string.equals( "V" )|| string.equals( "o" ) )
115: {
116: for (int i = 0; i < string.length(); i++) {
117: //todo need to use image observers and such
118: char c = string.charAt(i);
119: Image image = createImageIfNecessary(c);
120: if (image != null) {
121: int newWidth = (int) (.12 * image.getWidth(null));
122: int newHeight = (int) (.12 * image.getHeight(null));
123: if (newWidth > 0 && newHeight > 0) {
124: image = image.getScaledInstance(newWidth,
125: newHeight, Image.SCALE_SMOOTH);
126: g.drawImage(image, (int) x, (int) y, null);
127: x += newWidth;
128: }
129: }
130: }
131: }
132: }
133:
134: /**
135: * Set the font matrix for this type3 font.
136: *
137: * @param matrix The font matrix for this type3 font.
138: */
139: public void setFontMatrix(PDMatrix matrix) {
140: font.setItem("FontMatrix", matrix);
141: }
142: }
|