CVS: jfor/src/org/jfor/jfor/rtflib/rtfdoc RtfFontManager.java,1.4,1.5

Phil Chu <[email protected]> Sat, 01 Nov 2003 13:29:00 -0800
Newsgroups gmane.text.xml.jfor.cvs
Message-ID <[email protected]>
Update of /cvsroot/jfor/jfor/src/org/jfor/jfor/rtflib/rtfdoc
In directory sc8-pr-cvs1:/tmp/cvs-serv30322

Modified Files:
	RtfFontManager.java 
Log Message:
handle FO generic font families and lists of families, e.g. Symbol,serif. Predefine some fonts so, if used, the RTF family name is output as per spec

Index: RtfFontManager.java
===================================================================
RCS file: /cvsroot/jfor/jfor/src/org/jfor/jfor/rtflib/rtfdoc/RtfFontManager.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** RtfFontManager.java	12 Jul 2002 08:08:31 -0000	1.4
--- RtfFontManager.java	1 Nov 2003 21:28:57 -0000	1.5
***************
*** 4,7 ****
--- 4,9 ----
  import java.util.Hashtable;
  import java.util.Vector;
+ import java.util.StringTokenizer;
+ import java.util.List;
  
  /*-----------------------------------------------------------------------------
***************
*** 54,57 ****
--- 56,67 ----
  -----------------------------------------------------------------------------*/
  
+ //------------------------------------------------------------------------------
+ // $Id$
+ // $Log$
+ // Revision 1.5  2003/11/01 21:28:57  philipchu
+ // handle FO generic font families and lists of families, e.g. Symbol,serif. Predefine some fonts so, if used, the RTF family name is output as per spec
+ //
+ //------------------------------------------------------------------------------
+ 
  /**
   *   RTF font table
***************
*** 60,63 ****
--- 70,74 ----
  public class RtfFontManager
  {
+ 
  	//////////////////////////////////////////////////
  	// @@ Members
***************
*** 72,75 ****
--- 83,143 ----
  	private Vector fontTable = null;
  
+     // Phil Chu - generic font family names used in XSL-FP
+     private final static String FO_GENERIC_SERIF = "serif";
+     private final static String FO_GENERIC_SANSSERIF = "sans-serif";
+     private final static String FO_GENERIC_CURSIVE = "cursive";
+     private final static String FO_GENERIC_FANTASY = "fantasy";
+     private final static String FO_GENERIC_MONOSPACE = "monospace";
+ 
+     // Phil Chu - font family names used in RTF
+     private final static String RTF_FAMILY_NIL = "nil";
+     private final static String RTF_FAMILY_ROMAN = "roman";
+     private final static String RTF_FAMILY_SWISS = "swiss";
+     private final static String RTF_FAMILY_MODERN = "modern";
+     private final static String RTF_FAMILY_SCRIPT = "script";
+     private final static String RTF_FAMILY_DECOR = "decor";
+     private final static String RTF_FAMILY_TECH = "tech";
+     private final static String RTF_FAMILY_BIDI = "bidi";
+ 
+     /**
+      * table of preset fonts
+      */
+     private Hashtable fontlib;
+ 
+     /**
+      * table of XSL-FO generic font family names
+      */
+     private Hashtable genericNames;
+ 
+     /**
+      * Add generic family font name.
+      * Initializes with empty list of fonts.
+      */
+     private void addGenericName(String name) {
+ 	genericNames.put(name,new Vector());
+     }
+ 
+     /**
+      * @return List of fonts in family
+      */
+     private List getGenericFonts(String family) {
+ 	return (List)genericNames.get(family);
+     }
+ 
+     /**
+      * font information. Includes info required for generating RTF font table
+      */
+     private class RtfFontInfo {
+ 	public String name;
+ 	public String family; // nil,roman,swiss,modern,decor,tech,bidi - use enums?
+ 	//	public int num;
+ 
+ 	public RtfFontInfo(String name,String family) {
+ 	    super();
+ 	    this.name = name;
+ 	    this.family = family;
+ 	    //  this.num = num;
+ 	}
+     }
  
  	//////////////////////////////////////////////////
***************
*** 114,120 ****
  	{
  
  //		getFontNumber ("Helvetica");
  		//Chanded by R.Marra default font Arial
! 		getFontNumber ("Arial");
  		getFontNumber ("Symbol"); // used by RtfListItem.java
  		getFontNumber ("Times New Roman");
--- 182,220 ----
  	{
  
+ 	    // Phil Chu - initialize generic font families
+ 	    genericNames = new Hashtable ();
+ 	    fontlib = new Hashtable ();
+ 	    addGenericName(FO_GENERIC_SERIF);
+ 	    addGenericName(FO_GENERIC_SANSSERIF);
+ 	    addGenericName(FO_GENERIC_CURSIVE);
+ 	    addGenericName(FO_GENERIC_FANTASY);
+ 	    addGenericName(FO_GENERIC_MONOSPACE);
+ 
+ 	    // define at least one font for each generic family
+ 	    // fonts listed here are taken from the RTF 1.5 spec
+ 	    defineFont("Arial",RTF_FAMILY_SWISS,FO_GENERIC_SANSSERIF);
+ 	    defineFont("Symbol",RTF_FAMILY_TECH,FO_GENERIC_FANTASY); // correct generic?
+ 	    defineFont("Times New Roman",RTF_FAMILY_ROMAN,FO_GENERIC_SERIF);
+ 	    defineFont("Cursive",RTF_FAMILY_SCRIPT,FO_GENERIC_CURSIVE);
+ 	    defineFont("Courier New",RTF_FAMILY_MODERN,FO_GENERIC_MONOSPACE);
+ 	    // throw in some more for good measure
+ 	    // better to have them specified here than on-the-fly
+ 	    // so that the proper RTF family is included
+ 	    // they will not be included in font table until referenced
+ 	    // i.e. getFontNumber()
+ 	    defineFont("Old English",RTF_FAMILY_DECOR,FO_GENERIC_FANTASY); // correct generic?
+ 	    defineFont("Zapf Chancery",RTF_FAMILY_DECOR,FO_GENERIC_FANTASY); // correct generic?
+ 	    defineFont("Miriam",RTF_FAMILY_BIDI,FO_GENERIC_FANTASY); // correct generic?
+ 	    defineFont("Pica",RTF_FAMILY_MODERN,FO_GENERIC_MONOSPACE);
+ 	    defineFont("Palatino",RTF_FAMILY_SWISS,FO_GENERIC_SERIF);
+ 	    // we use this a lot in tests
+ 	    defineFont("Helvetica",RTF_FAMILY_MODERN,FO_GENERIC_SANSSERIF);
+ 
+ 	    // always include the following in the font table
+ 	    // really only need to include the first one for a default, I believe
+ 
  //		getFontNumber ("Helvetica");
  		//Chanded by R.Marra default font Arial
! 	        getFontNumber ("Arial");
  		getFontNumber ("Symbol"); // used by RtfListItem.java
  		getFontNumber ("Times New Roman");
***************
*** 146,151 ****
  	public int getFontNumber (String family)
  	{
  		
! 		family = family.toLowerCase ();
  		Object o = fontIndex.get (family);
  		int retVal;
--- 246,268 ----
  	public int getFontNumber (String family)
  	{
+ 	    // Phil Chu - handle list of fonts, e.g. "serif,Symbol"
+ 	    // TODO - should handle lists and generics outside of RTF?
+ 	    StringTokenizer toke = new StringTokenizer(family,",");
+ 	    while (toke.hasMoreTokens()) {
+ 		family = toke.nextToken().trim();
+ 		List fonts = getGenericFonts(family);
+ 		// if name is not a family name then go ahead and use it
+ 		if (fonts == null) {
+ 		    break;
+ 		}
+ 		// if there is a font in this family, use it
+ 		if (fonts.size()>0) {
+ 		    family = (String)fonts.get(0);
+ 		    break;
+ 		}
+ 		// otherwise, go on to next one in the list
+ 	    }
  		
! 	    	family = family.toLowerCase ();
  		Object o = fontIndex.get (family);
  		int retVal;
***************
*** 153,159 ****
  		if (o == null)
  		{
! 			addFont (family);
! 
! 			retVal = fontTable.size () - 1;
  		}
  		else
--- 270,274 ----
  		if (o == null)
  		{
! 			retVal = addFont (family);
  		}
  		else
***************
*** 179,184 ****
  		}
  
  		header.writeGroupMark (true);
! 		header.writeControlWord ("fonttbl;");
  
  		int len = fontTable.size ();
--- 294,300 ----
  		}
  
+ 		// <fonttable> := { <fnttbl> <fontinfo>+ }
  		header.writeGroupMark (true);
! 		header.writeControlWord ("fonttbl");
  
  		int len = fontTable.size ();
***************
*** 186,193 ****
  		for (int i = 0; i < len; i++)
  		{
! 			header.writeGroupMark (true);
! 			header.write ("\\f" + i);
! 			header.write (" " + (String) fontTable.elementAt (i));
! 			header.writeGroupMark (false);
  		}
  
--- 302,313 ----
  		for (int i = 0; i < len; i++)
  		{
! 		    // <fontinfo> := { <fontnum> <fontfamily> <fontname> ; }
!       		    RtfFontInfo info = (RtfFontInfo) fontTable.get (i);
! 		    header.writeGroupMark (true);
! 		    header.write ("\\f"+ i);
! 		    header.write ("\\f"+info.family);
! 		    header.write (" " + info.name);
! 		    header.write (";");
! 		    header.writeGroupMark (false);
  		}
  
***************
*** 203,212 ****
  	 * Adds a font to the table.
  	 *
! 	 * @param i Identifier of font
  	 */
! 	private void addFont (String family)
  	{
! 		fontIndex.put (family, new Integer (fontTable.size ()));
! 		fontTable.addElement (family);
  	}
  }
--- 323,361 ----
  	 * Adds a font to the table.
  	 *
! 	 * @param name Identifier of font
! 	 * @return font table index of font entry
  	 */
! 	private int addFont (String name)
  	{
! 	    int num = fontTable.size();
! 	    RtfFontInfo info;
! 	    // check if font is defined
! 	    info = (RtfFontInfo)fontlib.get(name);
! 	    // if not, just make one
! 	    if (info == null) {
! 		info = new RtfFontInfo(name,RTF_FAMILY_NIL);
! 	    }
! 	    // add to query-by-name table
! 	    fontIndex.put (name, new Integer (num));
! 	    // add to ordered font table
! 	    fontTable.addElement (info);
! 	    return num;
! 	}
! 
! 	/**
! 	 * Adds a font to the table.
! 	 *
! 	 * @param name Identifier of font
! 	 * @param family RTF font family name
! 	 */
! 	private void defineFont (String name,String family,String generic)
! 	{
! 	    RtfFontInfo info = new RtfFontInfo(name,family);
! 	    // to be consistent with fontIndex, key using lowercase
! 	    // though perhaps all the lowercasing is unnecessary?
! 	    fontlib.put(name.toLowerCase(),info);
! 	    // add font name to list for generic family
! 	    getGenericFonts(generic).add(name);
  	}
+ 
  }



-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program.
Does SourceForge.net help you be more productive?  Does it
help you create better code?   SHARE THE LOVE, and help us help
YOU!  Click Here: http://sourceforge.net/donate/