001: /*
002: * $Id: SWFFontMetric.java,v 1.3 2002/02/24 02:10:19 skavish Exp $
003: *
004: * ==========================================================================
005: *
006: * The JGenerator Software License, Version 1.0
007: *
008: * Copyright (c) 2000 Dmitry Skavish (skavish@usa.net). All rights reserved.
009: *
010: * Redistribution and use in source and binary forms, with or without
011: * modification, are permitted provided that the following conditions are met:
012: *
013: * 1. Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * 2. Redistributions in binary form must reproduce the above copyright
017: * notice, this list of conditions and the following disclaimer in
018: * the documentation and/or other materials provided with the
019: * distribution.
020: *
021: * 3. The end-user documentation included with the redistribution, if
022: * any, must include the following acknowlegement:
023: * "This product includes software developed by Dmitry Skavish
024: * (skavish@usa.net, http://www.flashgap.com/)."
025: * Alternately, this acknowlegement may appear in the software itself,
026: * if and wherever such third-party acknowlegements normally appear.
027: *
028: * 4. The name "The JGenerator" must not be used to endorse or promote
029: * products derived from this software without prior written permission.
030: * For written permission, please contact skavish@usa.net.
031: *
032: * 5. Products derived from this software may not be called "The JGenerator"
033: * nor may "The JGenerator" appear in their names without prior written
034: * permission of Dmitry Skavish.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL DMITRY SKAVISH OR THE OTHER
040: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: *
049: */
050:
051: package org.openlaszlo.iv.flash.fop;
052:
053: import org.openlaszlo.iv.flash.api.*;
054: import org.openlaszlo.iv.flash.api.shape.*;
055: import org.openlaszlo.iv.flash.api.action.*;
056: import org.openlaszlo.iv.flash.api.button.*;
057: import org.openlaszlo.iv.flash.api.text.*;
058: import org.openlaszlo.iv.flash.parser.*;
059: import org.openlaszlo.iv.flash.util.*;
060:
061: import java.io.*;
062: import java.util.*;
063:
064: /**
065: * Implementation of the FOP FontMetric interface. This class
066: * returns information about Flash fonts to the FOP Framework.
067: *
068: * @author Johan "Spocke" Sörlin
069: * @author James Taylor
070: */
071:
072: public class SWFFontMetric implements org.apache.fop.layout.FontMetric {
073: private String fontName = null;
074: private Font flashFont = null;
075: private int widths[] = null;
076:
077: /**
078: * Create a new SWFFontMetric for the given font filename
079: */
080:
081: public SWFFontMetric(String fontName) {
082: this .fontName = fontName;
083: }
084:
085: public int getAscender(int size) {
086: Font font = getFont();
087: int realAscent;
088:
089: realAscent = font.ascent - font.descent;
090:
091: return convertUnits(size * realAscent);
092: }
093:
094: public int getCapHeight(int size) {
095: return this .getAscender(size);
096: }
097:
098: public int getDescender(int size) {
099: Font font = getFont();
100:
101: return convertUnits(-1 * size * font.descent);
102: }
103:
104: public int getFirstChar() {
105: return 32;
106: }
107:
108: public int getLastChar() {
109: return 255;
110: }
111:
112: public int[] getWidths(int size) {
113: Font font = getFont();
114:
115: if (this .widths == null) {
116: this .widths = new int[255];
117:
118: for (int i = 0; i < this .widths.length; i++) {
119: int code = font.getIndex((char) i);
120: this .widths[i] = convertUnits(size
121: * (font.getAdvanceValue(code) * 2));
122: }
123: }
124:
125: return this .widths;
126: }
127:
128: public int getXHeight(int size) {
129: Font font = getFont();
130:
131: return convertUnits((size + (font.ascent * 1000) + (font.descent * 1000)) * 1000);
132: }
133:
134: public int width(int i, int size) {
135: Font font = getFont();
136:
137: int code = font.getIndex((char) i);
138: int width = font.getAdvanceValue(code);
139:
140: return convertUnits(width * size);
141: }
142:
143: /**
144: * Return the Font object for this metric.
145: */
146:
147: public Font getFont() {
148: if (flashFont == null) {
149: flashFont = FontDef.load(fontName);
150: }
151:
152: return flashFont;
153: }
154:
155: /**
156: * SWF uses a 1024 x 1024 EM square, while fop uses 1000x1000, thus all
157: * metric values must be converted.
158: */
159:
160: private int convertUnits(int n) {
161: return Math.round(n * (1000f / 1024f));
162: }
163: }
|