You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontFileFinder.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.fonts.autodetect;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.MalformedURLException;
  22. import java.net.URL;
  23. import java.util.Collection;
  24. import java.util.List;
  25. import org.apache.commons.io.DirectoryWalker;
  26. import org.apache.commons.io.IOCase;
  27. import org.apache.commons.io.filefilter.FileFilterUtils;
  28. import org.apache.commons.io.filefilter.IOFileFilter;
  29. import org.apache.commons.io.filefilter.WildcardFileFilter;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.fop.fonts.FontEventListener;
  33. /**
  34. * Helps to autodetect/locate available operating system fonts.
  35. */
  36. public class FontFileFinder extends DirectoryWalker implements FontFinder {
  37. /** logging instance */
  38. private final Log log = LogFactory.getLog(FontFileFinder.class);
  39. /** default depth limit of recursion when searching for font files **/
  40. public static final int DEFAULT_DEPTH_LIMIT = -1;
  41. private final FontEventListener eventListener;
  42. /**
  43. * Default constructor
  44. * @param listener for throwing font related events
  45. */
  46. public FontFileFinder(FontEventListener listener) {
  47. this(DEFAULT_DEPTH_LIMIT, listener);
  48. }
  49. /**
  50. * Constructor
  51. * @param depthLimit recursion depth limit
  52. * @param listener for throwing font related events
  53. */
  54. public FontFileFinder(int depthLimit, FontEventListener listener) {
  55. super(getDirectoryFilter(), getFileFilter(), depthLimit);
  56. eventListener = listener;
  57. }
  58. /**
  59. * Font directory filter. Currently ignores hidden directories.
  60. * @return IOFileFilter font directory filter
  61. */
  62. protected static IOFileFilter getDirectoryFilter() {
  63. return FileFilterUtils.andFileFilter(
  64. FileFilterUtils.directoryFileFilter(),
  65. FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("."))
  66. );
  67. }
  68. /**
  69. * Font file filter. Currently searches for files with .ttf, .ttc, .otf, and .pfb extensions.
  70. * @return IOFileFilter font file filter
  71. */
  72. protected static IOFileFilter getFileFilter() {
  73. return FileFilterUtils.andFileFilter(
  74. FileFilterUtils.fileFileFilter(),
  75. new WildcardFileFilter(
  76. new String[] {"*.ttf", "*.otf", "*.pfb", "*.ttc"},
  77. IOCase.INSENSITIVE)
  78. );
  79. }
  80. /**
  81. * @param directory directory to handle
  82. * @param depth recursion depth
  83. * @param results collection
  84. * @return whether directory should be handled
  85. * {@inheritDoc}
  86. */
  87. @Override
  88. protected boolean handleDirectory(File directory, int depth, Collection results) {
  89. return true;
  90. }
  91. /**
  92. * @param file file to handle
  93. * @param depth recursion depth
  94. * @param results collection
  95. * {@inheritDoc}
  96. */
  97. @Override
  98. protected void handleFile(File file, int depth, Collection results) {
  99. try {
  100. // Looks Strange, but is actually recommended over just .URL()
  101. results.add(file.toURI().toURL());
  102. } catch (MalformedURLException e) {
  103. log.debug("MalformedURLException" + e.getMessage());
  104. }
  105. }
  106. /**
  107. * @param directory the directory being processed
  108. * @param depth the current directory level
  109. * @param results the collection of results objects
  110. * {@inheritDoc}
  111. */
  112. @Override
  113. protected void handleDirectoryEnd(File directory, int depth, Collection results) {
  114. if (log.isDebugEnabled()) {
  115. log.debug(directory + ": found " + results.size() + " font"
  116. + ((results.size() == 1) ? "" : "s"));
  117. }
  118. }
  119. /**
  120. * Automagically finds a list of font files on local system
  121. *
  122. * @return List<URL> of font files
  123. * @throws IOException io exception
  124. * {@inheritDoc}
  125. */
  126. public List<URL> find() throws IOException {
  127. final FontDirFinder fontDirFinder;
  128. final String osName = System.getProperty("os.name");
  129. if (osName.startsWith("Windows")) {
  130. fontDirFinder = new WindowsFontDirFinder();
  131. } else {
  132. if (osName.startsWith("Mac")) {
  133. fontDirFinder = new MacFontDirFinder();
  134. } else {
  135. fontDirFinder = new UnixFontDirFinder();
  136. }
  137. }
  138. List<File> fontDirs = fontDirFinder.find();
  139. List<URL> results = new java.util.ArrayList<URL>();
  140. for (File dir : fontDirs) {
  141. super.walk(dir, results);
  142. }
  143. return results;
  144. }
  145. /**
  146. * Searches a given directory for font files
  147. *
  148. * @param dir directory to search
  149. * @return list of font files
  150. * @throws IOException thrown if an I/O exception of some sort has occurred
  151. */
  152. public List<URL> find(String dir) throws IOException {
  153. List<URL> results = new java.util.ArrayList<URL>();
  154. File directory = new File(dir);
  155. if (!directory.isDirectory()) {
  156. eventListener.fontDirectoryNotFound(this, dir);
  157. } else {
  158. super.walk(directory, results);
  159. }
  160. return results;
  161. }
  162. }