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.

CachedFontInfo.java 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.net.MalformedURLException;
  21. import java.net.URL;
  22. import java.util.List;
  23. import org.apache.commons.io.FileUtils;
  24. /**
  25. * Font info stored in the cache
  26. */
  27. public class CachedFontInfo extends EmbedFontInfo {
  28. /** Serialization Version UID */
  29. private static final long serialVersionUID = 240028291961081894L;
  30. /** file modify date (if available) */
  31. private long lastModified = -1;
  32. /**
  33. * Returns a file given a list of urls
  34. * @param urls array of possible font urls
  35. * @return file font file
  36. */
  37. public static File getFileFromUrls(String[] urls) {
  38. for (int i = 0; i < urls.length; i++) {
  39. String urlStr = urls[i];
  40. if (urlStr != null) {
  41. File fontFile = null;
  42. if (urlStr.startsWith("file:")) {
  43. try {
  44. URL url = new URL(urlStr);
  45. fontFile = FileUtils.toFile(url);
  46. } catch (MalformedURLException mfue) {
  47. // do nothing
  48. }
  49. }
  50. if (fontFile == null) {
  51. fontFile = new File(urlStr);
  52. }
  53. if (fontFile.exists() && fontFile.canRead()) {
  54. return fontFile;
  55. }
  56. }
  57. }
  58. return null;
  59. }
  60. /**
  61. * Default constructor
  62. * @param metricsFile metrics file
  63. * @param kerning kerning
  64. * @param fontTriplets font triplets
  65. * @param embedFile embed file
  66. * @param lastModified timestamp that this font was last modified
  67. */
  68. public CachedFontInfo(String metricsFile, boolean kerning, List fontTriplets,
  69. String embedFile, long lastModified) {
  70. super(metricsFile, kerning, fontTriplets, embedFile);
  71. this.lastModified = lastModified;
  72. }
  73. /**
  74. * Constructor
  75. * @param fontInfo an existing embed font info
  76. */
  77. public CachedFontInfo(EmbedFontInfo fontInfo) {
  78. super(fontInfo.metricsFile, fontInfo.kerning, fontInfo.fontTriplets, fontInfo.embedFile);
  79. // try and determine modified date
  80. File fontFile = getFileFromUrls(new String[] {embedFile, metricsFile});
  81. if (fontFile != null ) {
  82. this.lastModified = fontFile.lastModified();
  83. }
  84. }
  85. /**
  86. * Gets the modified timestamp for font file (not always available)
  87. * @return modified timestamp
  88. */
  89. public long lastModified() {
  90. return this.lastModified;
  91. }
  92. /**
  93. * Gets the modified timestamp for font file
  94. * (used for the purposes of font info caching)
  95. * @param lastModified modified font file timestamp
  96. */
  97. public void setLastModified(long lastModified) {
  98. this.lastModified = lastModified;
  99. }
  100. /**
  101. * @return string representation of this object
  102. * {@inheritDoc}
  103. */
  104. public String toString() {
  105. return super.toString() + ", lastModified=" + lastModified;
  106. }
  107. }