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.

FontDetector.java 3.6KB

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