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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.util.Collection;
  23. import java.util.Iterator;
  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. /**
  33. * Helps to autodetect/locate available operating system fonts.
  34. */
  35. public class FontFileFinder extends DirectoryWalker implements FontFinder {
  36. /** logging instance */
  37. private final Log log = LogFactory.getLog(FontFileFinder.class);
  38. /** default depth limit of recursion when searching for font files **/
  39. public static final int DEFAULT_DEPTH_LIMIT = -1;
  40. /**
  41. * Default constructor
  42. */
  43. public FontFileFinder() {
  44. super(getDirectoryFilter(), getFileFilter(), DEFAULT_DEPTH_LIMIT);
  45. }
  46. /**
  47. * Constructor
  48. * @param depthLimit recursion depth limit
  49. */
  50. public FontFileFinder(int depthLimit) {
  51. super(getDirectoryFilter(), getFileFilter(), depthLimit);
  52. }
  53. /**
  54. * Font directory filter. Currently ignores hidden directories.
  55. * @return IOFileFilter font directory filter
  56. */
  57. protected static IOFileFilter getDirectoryFilter() {
  58. return FileFilterUtils.andFileFilter(
  59. FileFilterUtils.directoryFileFilter(),
  60. FileFilterUtils.notFileFilter(FileFilterUtils.prefixFileFilter("."))
  61. );
  62. }
  63. /**
  64. * Font file filter. Currently searches for files with .ttf, .ttc, .otf, and .pfb extensions.
  65. * @return IOFileFilter font file filter
  66. */
  67. protected static IOFileFilter getFileFilter() {
  68. return FileFilterUtils.andFileFilter(
  69. FileFilterUtils.fileFileFilter(),
  70. new WildcardFileFilter(
  71. new String[] {"*.ttf", "*.otf", "*.pfb", "*.ttc"},
  72. IOCase.INSENSITIVE)
  73. );
  74. }
  75. /**
  76. * @param directory directory to handle
  77. * @param depth recursion depth
  78. * @param results collection
  79. * @return whether directory should be handled
  80. * {@inheritDoc}
  81. */
  82. protected boolean handleDirectory(File directory, int depth, Collection results) {
  83. return true;
  84. }
  85. /**
  86. * @param file file to handle
  87. * @param depth recursion depth
  88. * @param results collection
  89. * {@inheritDoc}
  90. */
  91. protected void handleFile(File file, int depth, Collection results) {
  92. try {
  93. // Looks Strange, but is actually recommended over just .URL()
  94. results.add(file.toURI().toURL());
  95. } catch (MalformedURLException e) {
  96. log.debug("MalformedURLException" + e.getMessage());
  97. }
  98. }
  99. /**
  100. * @param directory the directory being processed
  101. * @param depth the current directory level
  102. * @param results the collection of results objects
  103. * {@inheritDoc}
  104. */
  105. protected void handleDirectoryEnd(File directory, int depth, Collection results) {
  106. if (log.isDebugEnabled()) {
  107. log.debug(directory + ": found " + results.size() + " font"
  108. + ((results.size() == 1) ? "" : "s"));
  109. }
  110. }
  111. /**
  112. * Automagically finds a list of font files on local system
  113. *
  114. * @return List<URL> of font files
  115. * @throws IOException io exception
  116. * {@inheritDoc}
  117. */
  118. public List find() throws IOException {
  119. final FontFinder fontDirFinder;
  120. final String osName = System.getProperty("os.name");
  121. if (osName.startsWith("Windows")) {
  122. fontDirFinder = new WindowsFontDirFinder();
  123. } else {
  124. if (osName.startsWith("Mac")) {
  125. fontDirFinder = new MacFontDirFinder();
  126. } else {
  127. fontDirFinder = new UnixFontDirFinder();
  128. }
  129. }
  130. List fontDirs = fontDirFinder.find();
  131. List results = new java.util.ArrayList();
  132. for (Iterator iter = fontDirs.iterator(); iter.hasNext();) {
  133. final File dir = (File)iter.next();
  134. super.walk(dir, results);
  135. }
  136. return results;
  137. }
  138. /**
  139. * Searches a given directory for font files
  140. *
  141. * @param dir directory to search
  142. * @return list of font files
  143. * @throws IOException thrown if an I/O exception of some sort has occurred
  144. */
  145. public List find(String dir) throws IOException {
  146. List results = new java.util.ArrayList();
  147. super.walk(new File(dir), results);
  148. return results;
  149. }
  150. }