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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. try {
  40. URL url = new URL(urls[i]);
  41. url.getFile();
  42. File fontFile = FileUtils.toFile(url);
  43. if (fontFile.exists() && fontFile.canRead()) {
  44. return fontFile;
  45. }
  46. } catch (IllegalArgumentException e) {
  47. // do nothing
  48. } catch (MalformedURLException e) {
  49. // do nothing
  50. }
  51. }
  52. return null;
  53. }
  54. /**
  55. * Default constructor
  56. * @param metricsFile metrics file
  57. * @param kerning kerning
  58. * @param fontTriplets font triplets
  59. * @param embedFile embed file
  60. * @param lastModified timestamp that this font was last modified
  61. */
  62. public CachedFontInfo(String metricsFile, boolean kerning, List fontTriplets,
  63. String embedFile, long lastModified) {
  64. super(metricsFile, kerning, fontTriplets, embedFile);
  65. this.lastModified = lastModified;
  66. }
  67. /**
  68. * Constructor
  69. * @param fontInfo an existing embed font info
  70. */
  71. public CachedFontInfo(EmbedFontInfo fontInfo) {
  72. super(fontInfo.metricsFile, fontInfo.kerning, fontInfo.fontTriplets, fontInfo.embedFile);
  73. // try and determine modified date
  74. File fontFile = getFileFromUrls(new String[] {embedFile, metricsFile});
  75. if (fontFile != null ) {
  76. this.lastModified = fontFile.lastModified();
  77. }
  78. }
  79. /**
  80. * Gets the modified timestamp for font file (not always available)
  81. * @return modified timestamp
  82. */
  83. public long lastModified() {
  84. return this.lastModified;
  85. }
  86. /**
  87. * Gets the modified timestamp for font file
  88. * (used for the purposes of font info caching)
  89. * @param lastModified modified font file timestamp
  90. */
  91. public void setLastModified(long lastModified) {
  92. this.lastModified = lastModified;
  93. }
  94. /**
  95. * @return string representation of this object
  96. * {@inheritDoc}
  97. */
  98. public String toString() {
  99. return super.toString() + ", lastModified=" + lastModified;
  100. }
  101. }