Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

FontDetectorFactory.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.net.URI;
  22. import java.net.URISyntaxException;
  23. import java.net.URL;
  24. import java.util.List;
  25. import org.apache.commons.io.FileUtils;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.util.ClasspathResource;
  29. import org.apache.fop.apps.FOPException;
  30. import org.apache.fop.fonts.autodetect.FontFileFinder;
  31. import org.apache.fop.util.LogUtil;
  32. /**
  33. * A factory that provides the font detecting machanism.
  34. */
  35. public final class FontDetectorFactory {
  36. private FontDetectorFactory() {
  37. }
  38. /**
  39. * Creates the default font detector
  40. * @return the default font detector
  41. */
  42. public static FontDetector createDefault() {
  43. return new DefaultFontDetector();
  44. }
  45. /**
  46. * Creates a disabled font detector which, by definition, does nothing to detect fonts.
  47. * @return the completely restricted font detector
  48. */
  49. public static FontDetector createDisabled() {
  50. return new DisabledFontDetector();
  51. }
  52. private static class DisabledFontDetector implements FontDetector {
  53. public void detect(FontManager fontManager, FontAdder fontAdder, boolean strict,
  54. FontEventListener eventListener, List<EmbedFontInfo> fontInfoList)
  55. throws FOPException {
  56. // nop
  57. }
  58. }
  59. /**
  60. * Detector of operating system and classpath fonts
  61. */
  62. private static class DefaultFontDetector implements FontDetector {
  63. private static Log log = LogFactory.getLog(DefaultFontDetector.class);
  64. private static final String[] FONT_MIMETYPES = {
  65. "application/x-font", "application/x-font-truetype"
  66. };
  67. /**
  68. * Detect installed fonts on the system
  69. * @param fontInfoList a list of fontinfo to populate
  70. * @throws FOPException thrown if a problem occurred during detection
  71. */
  72. public void detect(FontManager fontManager, FontAdder fontAdder, boolean strict,
  73. FontEventListener eventListener, List<EmbedFontInfo> fontInfoList)
  74. throws FOPException {
  75. try {
  76. // search in font base if it is defined and
  77. // is a directory but don't recurse
  78. FontFileFinder fontFileFinder = new FontFileFinder(eventListener);
  79. URI fontBaseURI = fontManager.getResourceResolver().getBaseURI();
  80. File fontBase = FileUtils.toFile(fontBaseURI.toURL());
  81. if (fontBase != null) {
  82. List<URL> fontURLList = fontFileFinder.find(fontBase.getAbsolutePath());
  83. fontAdder.add(fontURLList, fontInfoList);
  84. //Can only use the font base URL if it's a file URL
  85. }
  86. // native o/s font directory finding
  87. List<URL> systemFontList;
  88. systemFontList = fontFileFinder.find();
  89. fontAdder.add(systemFontList, fontInfoList);
  90. // classpath font finding
  91. ClasspathResource resource = ClasspathResource.getInstance();
  92. for (String mimeTypes : FONT_MIMETYPES) {
  93. fontAdder.add(resource.listResourcesOfMimeType(mimeTypes), fontInfoList);
  94. }
  95. } catch (IOException e) {
  96. LogUtil.handleException(log, e, strict);
  97. } catch (URISyntaxException use) {
  98. LogUtil.handleException(log, use, strict);
  99. }
  100. }
  101. }
  102. }