Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

FontDetector.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.URL;
  22. import java.util.List;
  23. import org.apache.commons.io.FileUtils;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.util.ClasspathResource;
  27. import org.apache.fop.apps.FOPException;
  28. import org.apache.fop.fonts.autodetect.FontFileFinder;
  29. import org.apache.fop.util.LogUtil;
  30. /**
  31. * Detector of operating system and classpath fonts
  32. */
  33. public class FontDetector {
  34. private static Log log = LogFactory.getLog(FontDetector.class);
  35. private static final String[] FONT_MIMETYPES = {
  36. "application/x-font", "application/x-font-truetype"
  37. };
  38. private final FontManager fontManager;
  39. private final FontAdder fontAdder;
  40. private final boolean strict;
  41. private final FontEventListener eventListener;
  42. /**
  43. * Main constructor
  44. * @param manager the font manager
  45. * @param adder the font adder
  46. * @param strict true if an Exception should be thrown if an error is found.
  47. * @param listener for throwing font related events
  48. */
  49. public FontDetector(FontManager manager, FontAdder adder, boolean strict,
  50. FontEventListener listener) {
  51. this.fontManager = manager;
  52. this.fontAdder = adder;
  53. this.strict = strict;
  54. this.eventListener = listener;
  55. }
  56. /**
  57. * Detect installed fonts on the system
  58. * @param fontInfoList a list of fontinfo to populate
  59. * @throws FOPException thrown if a problem occurred during detection
  60. */
  61. public void detect(List<EmbedFontInfo> fontInfoList) throws FOPException {
  62. // search in font base if it is defined and
  63. // is a directory but don't recurse
  64. FontFileFinder fontFileFinder = new FontFileFinder(eventListener);
  65. String fontBaseURL = fontManager.getFontBaseURL();
  66. if (fontBaseURL != null) {
  67. try {
  68. File fontBase = FileUtils.toFile(new URL(fontBaseURL));
  69. if (fontBase != null) {
  70. List<URL> fontURLList = fontFileFinder.find(fontBase.getAbsolutePath());
  71. fontAdder.add(fontURLList, fontInfoList);
  72. //Can only use the font base URL if it's a file URL
  73. }
  74. } catch (IOException e) {
  75. LogUtil.handleException(log, e, strict);
  76. }
  77. }
  78. // native o/s font directory finding
  79. List<URL> systemFontList;
  80. try {
  81. systemFontList = fontFileFinder.find();
  82. fontAdder.add(systemFontList, fontInfoList);
  83. } catch (IOException e) {
  84. LogUtil.handleException(log, e, strict);
  85. }
  86. // classpath font finding
  87. ClasspathResource resource = ClasspathResource.getInstance();
  88. for (int i = 0; i < FONT_MIMETYPES.length; i++) {
  89. fontAdder.add(resource.listResourcesOfMimeType(FONT_MIMETYPES[i]), fontInfoList);
  90. }
  91. }
  92. }