/*
 * Copyright (c) OSGi Alliance (2012). All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.osgi.tools.fop;

import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;

/**
 * Front end to FOP.
 * 
 * This class makes sure all the spec fonts are registered with the VM before
 * calling FOP. The directory containing the spec fonts must be specified on the
 * JAVA_FONTS system property.
 * 
 * @version $Id$
 */
public class FOPMain {

	/**
	 * Register any fonts in JAVA_FONTS and call FOP main method with argument.
	 * 
	 * @param args Arguments to pass to FOP main.
	 * @throws Throwable If unable to load or call FOP main or FOP main throws
	 *         an exception.
	 */
	public static void main(String[] args) throws Throwable {
		registerFonts();

		Class<?> fop = Class.forName(System.getProperty("fop.main", "org.apache.fop.cli.Main"));
		Method main = fop.getMethod("main", String[].class);
		try {
			main.invoke(null, (Object) args);
		} catch (InvocationTargetException e) {
			throw e.getCause();
		}
	}

	/**
	 * Register any fonts at JAVA_FONTS which are not currently available.
	 */
	private static void registerFonts() {
		String additionalFontsDir = System.getProperty("JAVA_FONTS");
		if (additionalFontsDir == null) {
			return;
		}
		File fontsDir = new File(additionalFontsDir);
		if (!fontsDir.isDirectory()) {
			return;
		}
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		List<String> availableFonts = Arrays.asList(ge.getAvailableFontFamilyNames());
		File[] fontFiles = fontsDir.listFiles(new FilenameFilter() {
			public boolean accept(File dir, String name) {
				return name.endsWith(".ttf");
			}
		});
		for (File fontFile : fontFiles) {
			Font font;
			try {
				font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
			} catch (FontFormatException e) {
				e.printStackTrace();
				continue;
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
			String name = font.getFamily();
			if (availableFonts.contains(name)) {
				continue;
			}
			boolean registered = ge.registerFont(font);
			if (registered) {
				System.out.println("Registered font \"" + name + "\" from " + fontFile);
			}
		}
	}
}

@@ -1095,17 +1095,19 @@
 director
-y
+ies
  contain
@@ -1149,15 +1149,15 @@
 d on
+%0A *
  the
-%0A *
  JAV
@@ -1248,24 +1248,33 @@
 egister any 
+TrueType 
 fonts in JAV
@@ -1310,16 +1310,37 @@
 hod with
+%0A%09 * the command line
  argumen
@@ -1340,16 +1340,17 @@
 argument
+s
 .%0A%09 * %0A%09
@@ -1364,17 +1364,30 @@
 am args 
-A
+Command line a
 rguments
@@ -1884,16 +1884,29 @@
 any 
-fonts at
+TrueType fonts on the
  JAV
@@ -1913,16 +1913,21 @@
 A_FONTS 
+path 
 which ar
@@ -1931,16 +1931,20 @@
  are not
+%0A%09 *
  current
@@ -2013,27 +2013,23 @@
 ring
- additionalF
+%5B%5D f
 ont
-s
 Dir
+Names
  = S
@@ -2062,43 +2062,397 @@
 NTS%22
-);%0A%09%09if (additionalFontsDir == null
+, %22%22).split(File.pathSeparator);%0A%09%09for (String fontDirName : fontDirNames) %7B%0A%09%09%09registerFonts(fontDirName.trim());%0A%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09 * Register any TrueType fonts in the specified directory which are not%0A%09 * currently available.%0A%09 * %0A%09 * @param fontDirName The name of a directory containing fonts.%0A%09 */%0A%09private static void registerFonts(String fontDirName) %7B%0A%09%09if (fontDirName.length() == 0
 ) %7B%0A
@@ -2477,17 +2477,16 @@
 ile font
-s
 Dir = ne
@@ -2496,26 +2496,19 @@
 ile(
-additionalF
+f
 ont
-s
 Dir
+Name
 );%0A%09
@@ -2513,25 +2513,24 @@
 %0A%09%09if (!font
-s
 Dir.isDirect
@@ -2737,17 +2737,16 @@
 s = font
-s
 Dir.list
@@ -3367,24 +3367,42 @@
 %22 + fontFile
+.getAbsolutePath()
 );%0A%09%09%09%7D%0A%09%09%7D%0A

Allow JAVA_FONTS to be a path