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.

StaticFontMetrics.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.usermodel;
  16. import java.awt.Font;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Properties;
  24. import org.apache.logging.log4j.LogManager;
  25. import org.apache.logging.log4j.Logger;
  26. /**
  27. * Allows the user to lookup the font metrics for a particular font without
  28. * actually having the font on the system. The font details are loaded as a
  29. * resource from the POI jar file (or classpath) and should be contained in path
  30. * "/font_metrics.properties". The font widths are for a 10 point version of the
  31. * font. Use a multiplier for other sizes.
  32. */
  33. final class StaticFontMetrics {
  34. private static final Logger LOGGER = LogManager.getLogger(StaticFontMetrics.class);
  35. /** The font metrics property file we're using */
  36. private static Properties fontMetricsProps;
  37. /** Our cache of font details we've already looked up */
  38. private static final Map<String, FontDetails> fontDetailsMap = new HashMap<>();
  39. private StaticFontMetrics() {}
  40. /**
  41. * Retrieves the fake font details for a given font.
  42. *
  43. * @param font
  44. * the font to lookup.
  45. * @return the fake font.
  46. */
  47. public static synchronized FontDetails getFontDetails(Font font) {
  48. // If we haven't already identified out font metrics file,
  49. // figure out which one to use and load it
  50. if (fontMetricsProps == null) {
  51. try {
  52. fontMetricsProps = loadMetrics();
  53. } catch (IOException e) {
  54. throw new RuntimeException("Could not load font metrics", e);
  55. }
  56. }
  57. // Grab the base name of the font they've asked about
  58. String fontName = font.getName();
  59. // Some fonts support plain/bold/italic/bolditalic variants
  60. // Others have different font instances for bold etc
  61. // (eg font.dialog.plain.* vs font.Californian FB Bold.*)
  62. String fontStyle = "";
  63. if (font.isPlain()) {
  64. fontStyle += "plain";
  65. }
  66. if (font.isBold()) {
  67. fontStyle += "bold";
  68. }
  69. if (font.isItalic()) {
  70. fontStyle += "italic";
  71. }
  72. // Do we have a definition for this font with just the name?
  73. // If not, check with the font style added
  74. String fontHeight = FontDetails.buildFontHeightProperty(fontName);
  75. String styleHeight = FontDetails.buildFontHeightProperty(fontName + "." + fontStyle);
  76. if (fontMetricsProps.get(fontHeight) == null
  77. && fontMetricsProps.get(styleHeight) != null) {
  78. // Need to add on the style to the font name
  79. fontName += "." + fontStyle;
  80. }
  81. // Get the details on this font
  82. FontDetails fontDetails = fontDetailsMap.get(fontName);
  83. if (fontDetails == null) {
  84. fontDetails = FontDetails.create(fontName, fontMetricsProps);
  85. fontDetailsMap.put(fontName, fontDetails);
  86. }
  87. return fontDetails;
  88. }
  89. private static Properties loadMetrics() throws IOException {
  90. // Check to see if the font metric file was specified
  91. // as a system property
  92. File propFile = null;
  93. try {
  94. String propFileName = System.getProperty("font.metrics.filename");
  95. if (propFileName != null) {
  96. propFile = new File(propFileName);
  97. if (!propFile.exists()) {
  98. LOGGER.atWarn().log("font_metrics.properties not found at path {}", propFile.getAbsolutePath());
  99. propFile = null;
  100. }
  101. }
  102. } catch (SecurityException e) {
  103. LOGGER.atWarn().withThrowable(e).log("Can't access font.metrics.filename system property");
  104. }
  105. try (InputStream metricsIn = (propFile != null)
  106. ? new FileInputStream(propFile)
  107. : FontDetails.class.getResourceAsStream("/font_metrics.properties")
  108. ) {
  109. // Use the built-in font metrics file off the classpath
  110. if (metricsIn == null) {
  111. String err = "font_metrics.properties not found in classpath";
  112. throw new IOException(err);
  113. }
  114. Properties props = new Properties();
  115. props.load(metricsIn);
  116. return props;
  117. }
  118. }
  119. }