Font.java 0100664 0000764 0000144 00000005261 07555714160 011354 0 ustar vic users /*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.fonts;
//import org.apache.fop.apps.FOPException;
//import org.apache.fop.layout.FontMetric;
//import org.apache.fop.layout.FontDescriptor;
//import org.apache.fop.render.pdf.CodePointMapping;
//import org.apache.fop.messaging.MessageHandler;
/**
* Encapsulates information for fonts of interest to FOP,
* regardless of their source.
* Within FOP, a Font is a specific instance of the more general TypeFace,
* which is itself a member of a TypeFaceFamily.
* A Font is a TypeFace rendered at a specific size, and perhaps with
* various transformations applied. The XSL-FO 1.0 standard does not support
* any transformations, but they are possible in the future.
* (The "font-stretch" property is actually a Typeface selection property,
* not a transformation on a Font.)
* Please note that the terms "font" and "typeface" are used interchangeably in
* many other contexts. Scaleable font files, for example, usually contain
* typeface data, or data for an entire family of typefaces.
*
* @author {@link Victor Mote}
* refactored existing code into this class.
*/
public class Font {
/** The Typeface from which this font was derived. */
private Typeface typeface;
/** The Font size in points. */
private int size;
/**
* This is the main constructor.
*/
public Font(String family, String foStyle, String foWeight,
int variant, int size) {
}
/** Returns the Typeface object for this font. */
public Typeface getTypeface(){
return typeface;
}
/** Returns the font size for this font (in points). */
public int getSize(){
return size;
}
/** Returns the width of a given character number.
* This value does not consider letter spacing.
* The original doc indicated that the value returned was in millipoints.
* However, the division by 1000 would lead one to believe that the value
* returned is points.
*/
public int charWidth(int charnum) {
return (typeface.getFontMetric().width(charnum, size) / 1000);
}
/** Overloaded to allow passage of letterSpacing. */
public int charWidth(int charnum, int letterSpacing) {
return (letterSpacing + charWidth(charnum));
}
public int getAscender() {
return typeface.getFontMetric().getAscender(size) / 1000;
}
public int getDescender() {
return typeface.getFontMetric().getDescender(size) / 1000;
}
}
/* Last Line of $RCSfile$ */
Typeface.java 0100664 0000764 0000144 00000023357 07555715061 012215 0 ustar vic users /*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.fonts;
import org.apache.fop.apps.FOPException;
import org.apache.fop.layout.FontMetric;
import org.apache.fop.layout.FontDescriptor;
import org.apache.fop.render.pdf.CodePointMapping;
import org.apache.fop.messaging.MessageHandler;
import java.util.HashMap;
import java.util.StringTokenizer;
/**
* Encapsulates information for typefaces of interest to FOP,
* regardless of their source.
* Within FOP, a Font is a specific instance of the more general Typeface,
* which is itself a member of a TypefaceFamily.
* A Font is a Typeface rendered at a specific size, and perhaps with
* various transformations applied.
* Please note that the terms "font" and "typeface" are used interchangeably in
* many other contexts. Scaleable font files, for example, usually contain
* typeface data, or data for an entire family of typefaces.
*
* Typeface data is currently derived from two sources:
*
* -
* Hard-Coded. These are the base-14 fonts. Since they never need
* to be embedded, there is no need for access to the physical font file.
* However, the metrics are hard-coded within FOP.
*
-
* Direct Access (DA). These typefaces are accessed by FOP through its
* configuration, which points to an XML file containing the font
* metrics, and (where needed) the physical font file.
* These font metrics must be parsed by FOP routines before they can be
* used by FOP.
* DA Fonts may or may not be registered at the operating
* system level, but if they are, such registration is ignored.
*
*
*
* Planned enhancements to this class include addition of support for
* Indirect Access (IA) Fonts. These fonts are always registered at the
* operating system level, and are available to FOP through AWT classes.
* Please note that references to IA Fonts may still appear
* in the FOP configuration, but only for purposes such as embedding them
* in rendered output.
*
* Support for Direct Access Fonts is desirable because of the following:
*
* -
* There is at least some doubt about whether we can fully trust AWT for
* layout chores.
*
* -
* There is a desire for backward compatibility.
*
*
*
* Support for Indirect Access Fonts is desirable because of the following:
*
* -
* It immediately gives FOP access to all font types (including OpenType)
* that java supports.
*
* -
* It gives us a way to test AWT without breaking preexisting functionality.
*
* -
* It is a generally cleaner way to handle fonts, both for users and
* developers.
*
*
*
* @author Unascribed
* @author {@link Victor Mote}
* refactored existing code into this class.
*/
public class Typeface {
/**
* HashMap of typefaces used.
* Not sure whether the content is different from tripletsFontsMap and
* fontsMap, or whether only the keys are different, allowing multiple ways to
* find the same font.
*/
public static HashMap usedFontsMap = new HashMap();
/**
* HashMap of fonts keyed by the font-triplet (family, style, and weight).
* Not sure whether the content is different from tripletsFontsMap and
* fontsMap, or whether only the keys are different, allowing multiple ways to
* find the same font.
*/
public static HashMap tripletsFontsMap = new HashMap();
/**
* HashMap of fonts keyed by font name. May only include fonts that implement
* FontMetric.
* Not sure whether the content is different from tripletsFontsMap and
* fontsMap, or whether only the keys are different, allowing multiple ways to
* find the same font.
*/
public static HashMap fontsMap = new HashMap();
/** The font family to which this font belongs. */
private String fontFamily;
/**
* The FO typeface style (italic, etc.). Contents should conform to the
* XSL-FO standard.
*/
private String foStyle;
/**
* The FO typeface weight (bold, semibold, etc.). Contents should
* conform to the XSL-FO standard.
*/
private String foWeight;
/**
* The FO typeface variant (normal, small-caps).
*/
private int foVariant;
/** The Font Metric object for this typeface. */
private FontMetric fontMetric;
/** An empty hashmap used for kerning information */
private static HashMap EMPTY_HASHMAP = new HashMap();
/** @return the name of the Font Family for this typeface. */
public String getFamily(){
return fontFamily;
}
/** @return the font style for this typeface. */
public String getFOStyle(){
return foStyle;
}
/** Returns the font weight for this typeface. */
public String getFOWeight(){
return foWeight;
}
/** Returns the font variant for this typeface. */
public int getFOVariant(){
return foVariant;
}
/** Returns the fontMetric object for this typeface. */
public FontMetric getFontMetric() {
return fontMetric;
}
public HashMap getKerning() {
if (fontMetric instanceof FontDescriptor) {
HashMap ret = ((FontDescriptor)fontMetric).getKerningInfo();
if (ret != null)
return ret;
}
return EMPTY_HASHMAP;
}
/**
* Maps a java character (unicode) to a font character.
* Default uses CodePointMapping.
*/
public char mapChar(char c) {
if (fontMetric instanceof org.apache.fop.render.pdf.Font) {
return ((org.apache.fop.render.pdf.Font)fontMetric).mapChar(c);
} else if (fontMetric instanceof org.apache.fop.render.awt.FontMetricsMapper) {
return c;
}
// Use default CodePointMapping
char d = CodePointMapping.getMapping("WinAnsiEncoding").mapChar(c);
if (d != 0) {
c = d;
} else {
c = '#';
}
return c;
}
/*////////////////// METHODS FOR MANAGING THE FONT MAPS /////////////////*/
/** Adds an entry to the tripletsFontsMap (uses family, style and weight as key. */
public static void addFontProperties(String name, String family,
String style, String weight) {
String key = createFontKey(family, style, weight);
tripletsFontsMap.put(key, name);
}
/** Creates a key for the tripletsFontsMap from the given strings */
public static String createFontKey(String family, String style,
String weight) {
int i;
try {
i = Integer.parseInt(weight);
} catch (NumberFormatException e) {
i = 0;
}
if (i > 600)
weight = "bold";
else if (i > 0)
weight = "normal";
return family + "," + style + "," + weight;
}
/**
* Looks for alternative font family names that might be in the
* tripletsFontsMap.
*
* @return name of font family found in table, or null if not found.
*/
public static String findFontFamilyNameToUse
(String family, String style, String weight) {
String fontKey = createFontKey(family, style, weight);
if (hasFont(fontKey)) {
return family;
}
//Tokenizes font-family list
StringTokenizer st = new StringTokenizer(family, ",");
while (st.hasMoreTokens()) {
String token = st.nextToken().trim();
token = TypefaceFamily.normalizeTypefaceFamilyName(token);
//Checks found font family name for existence
fontKey = createFontKey(token, style, weight);
if (hasFont(fontKey)) {
return family;
}
}
return null;
}
/** Add the given metrics as a font with the given name. */
public static void addMetrics(String name, FontMetric metrics) {
fontsMap.put(name, metrics);
}
public static String fontLookup(String family, String style,
String weight) throws FOPException {
return fontLookup(createFontKey(family, style, weight));
}
public static String fontLookup(String key) throws FOPException {
String f = (String)tripletsFontsMap.get(key);
if (f == null) {
int i = key.indexOf(',');
String s = "any" + key.substring(i);
f = (String)tripletsFontsMap.get(s);
if (f == null) {
f = (String)tripletsFontsMap.get("any,normal,normal");
if (f == null) {
throw new FOPException("no default font defined by OutputConverter");
}
MessageHandler.errorln("defaulted font to any,normal,normal");
}
MessageHandler.errorln("unknown font " + key
+ " so defaulted font to any");
}
usedFontsMap.put(f, fontsMap.get(f));
return f;
}
public static boolean hasFont(String family, String style, String weight) {
String key = createFontKey(family, style, weight);
return tripletsFontsMap.get(key) != null;
}
public static boolean hasFont(String key) {
return tripletsFontsMap.get(key) != null;
}
public static HashMap getFonts() {
return fontsMap;
}
public static HashMap getUsedFonts() {
return usedFontsMap;
}
public static FontMetric getMetricsFor(String fontName) throws FOPException {
getUsedFonts().put(fontName, fontsMap.get(fontName));
return (FontMetric)fontsMap.get(fontName);
}
}
/* Last Line of $RCSfile$ */
TypefaceFamily.java 0100664 0000764 0000144 00000006016 07555556501 013352 0 ustar vic users /*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.fonts;
import org.apache.fop.fonts.Typeface;
import java.util.HashMap;
/**
* This class contains information about typeface families registered to and/or
* used within FOP.
*
* To Do:
*
* - Consider adding fields to this class that keep track of which
* typefaces in this family have certain characteristics (bold, italic,
* small caps, etc., or provide methods that iterate through the
* Typeface objects looking for such information.
*
* - Consider adding a Collection of daughter Typefaces, or a method
* to iterate through Typeface objects looking for them.
*
*
* @see org.apache.fop.fonts.Typeface
* @see org.apache.fop.fonts.Font
*
* @author {@link Victor Mote}
*/
public class TypefaceFamily {
private String name;
/** Source (base14, direct access, indirect access). */
private int source = 0;
/** Source is base-14 (hard-coded). */
public final static int SOURCE_HARD_CODED = 1;
/** Source is indirect access (registered at operating system
level, or created using java.awt.Font.createFont). */
public final static int SOURCE_INDIRECT_ACCESS = 2;
/** Source is from metric information configured by user */
public final static int SOURCE_DIRECT_ACCESS = 3;
/** @return the name of this TypefaceFamily. */
public String getName(){
return name;
}
/** @return The source of this TypefaceFamily. */
public int getSource(){
return source;
}
/**
* Normalizes a TypefaceFamily name by handling quotation marks
* and white space.
*/
public static String normalizeTypefaceFamilyName(String inputFF){
//Checks for quoted TypefaceFamily name
if (inputFF.charAt(0) == '"' || inputFF.charAt(0) == '\''){
return inputFF.substring(1, inputFF.length()-1);
}
else {
//In a nonquoted TypefaceFamily name any sequence of whitespace
//inside should be converted to a single space
StringBuffer sb = new StringBuffer();
boolean spaced = false;
for (int i=0; i> ch) & 1L) != 0);
}
}
/* Last Line of $RCSfile$ */