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

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