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.

CommonFont.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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.fo.properties;
  19. // FOP
  20. import java.util.List;
  21. import org.apache.fop.datatypes.Length;
  22. import org.apache.fop.datatypes.Numeric;
  23. import org.apache.fop.fo.Constants;
  24. import org.apache.fop.fo.PropertyList;
  25. import org.apache.fop.fo.expr.PropertyException;
  26. import org.apache.fop.fonts.FontInfo;
  27. import org.apache.fop.fonts.FontTriplet;
  28. import org.apache.fop.util.CompareUtil;
  29. /**
  30. * Collection of CommonFont properties
  31. */
  32. public final class CommonFont {
  33. /** cache holding canonical CommonFont instances (only those with
  34. * absolute font-size and font-size-adjust) */
  35. private static final PropertyCache<CommonFont> CACHE = new PropertyCache<CommonFont>();
  36. /** hashcode of this instance */
  37. private int hash = -1;
  38. /** The "font-family" property. */
  39. private final FontFamilyProperty fontFamily;
  40. /** The "font-selection-strategy" property. */
  41. private final EnumProperty fontSelectionStrategy;
  42. /** The "font-stretch" property. */
  43. private final EnumProperty fontStretch;
  44. /** The "font-style" property. */
  45. private final EnumProperty fontStyle;
  46. /** The "font-variant" property. */
  47. private final EnumProperty fontVariant;
  48. /** The "font-weight" property. */
  49. private final EnumProperty fontWeight;
  50. /** The "font-size" property. */
  51. public final Length fontSize;
  52. /** The "font-size-adjust" property. */
  53. public final Numeric fontSizeAdjust;
  54. /**
  55. * Construct a CommonFont instance
  56. *
  57. * @param fontFamily the font-family property
  58. * @param fontSelectionStrategy the font-selection-strategy property
  59. * @param fontStretch the font-stretch property
  60. * @param fontStyle the font-style property
  61. * @param fontVariant the font-variant property
  62. * @param fontWeight the font-weight property
  63. * @param fontSize the font-size (possibly non-cached)
  64. * @param fontSizeAdjust the font-size-adjust (possibly non-cached)
  65. */
  66. private CommonFont(FontFamilyProperty fontFamily,
  67. EnumProperty fontSelectionStrategy,
  68. EnumProperty fontStretch,
  69. EnumProperty fontStyle,
  70. EnumProperty fontVariant,
  71. EnumProperty fontWeight,
  72. Length fontSize,
  73. Numeric fontSizeAdjust) {
  74. this.fontFamily = fontFamily;
  75. this.fontSelectionStrategy = fontSelectionStrategy;
  76. this.fontStretch = fontStretch;
  77. this.fontStyle = fontStyle;
  78. this.fontVariant = fontVariant;
  79. this.fontWeight = fontWeight;
  80. this.fontSize = fontSize;
  81. this.fontSizeAdjust = fontSizeAdjust;
  82. }
  83. /**
  84. * Returns a CommonFont instance for the given PropertyList
  85. * If the font-size and font-size-adjust properties are absolute
  86. * the entire instance will be cached.
  87. * If not, then a distinct instance will be returned, with
  88. * as much cached information as possible.
  89. *
  90. * @param pList the PropertyList to get the properties from
  91. * @return a CommonFont instance corresponding to the properties
  92. * @throws PropertyException if there was a problem getting the properties
  93. */
  94. public static CommonFont getInstance(PropertyList pList) throws PropertyException {
  95. FontFamilyProperty fontFamily = (FontFamilyProperty) pList.get(Constants.PR_FONT_FAMILY);
  96. EnumProperty fontSelectionStrategy
  97. = (EnumProperty) pList.get(Constants.PR_FONT_SELECTION_STRATEGY);
  98. EnumProperty fontStretch = (EnumProperty) pList.get(Constants.PR_FONT_STRETCH);
  99. EnumProperty fontStyle = (EnumProperty) pList.get(Constants.PR_FONT_STYLE);
  100. EnumProperty fontVariant = (EnumProperty) pList.get(Constants.PR_FONT_VARIANT);
  101. EnumProperty fontWeight = (EnumProperty) pList.get(Constants.PR_FONT_WEIGHT);
  102. Numeric fontSizeAdjust = pList.get(Constants.PR_FONT_SIZE_ADJUST).getNumeric();
  103. Length fontSize = pList.get(Constants.PR_FONT_SIZE).getLength();
  104. CommonFont commonFont = new CommonFont(fontFamily,
  105. fontSelectionStrategy,
  106. fontStretch,
  107. fontStyle,
  108. fontVariant,
  109. fontWeight,
  110. fontSize,
  111. fontSizeAdjust);
  112. return CACHE.fetch(commonFont);
  113. }
  114. /** @return an array with the font-family names */
  115. private String[] getFontFamily() {
  116. List<Property> lst = fontFamily.getList();
  117. String[] fontFamily = new String[lst.size()];
  118. for (int i = 0, c = lst.size(); i < c; i++) {
  119. fontFamily[i] = lst.get(i).getString();
  120. }
  121. return fontFamily;
  122. }
  123. /** @return the first font-family name in the list */
  124. public String getFirstFontFamily() {
  125. return fontFamily.list.get(0).getString();
  126. }
  127. /** @return the "font-selection-strategy" property */
  128. public int getFontSelectionStrategy() {
  129. return fontSelectionStrategy.getEnum();
  130. }
  131. /** @return the "font-stretch" property */
  132. public int getFontStretch() {
  133. return fontStretch.getEnum();
  134. }
  135. /** @return the "font-style" property */
  136. public int getFontStyle() {
  137. return fontStyle.getEnum();
  138. }
  139. /** @return the "font-variant" property */
  140. public int getFontVariant() {
  141. return fontVariant.getEnum();
  142. }
  143. /** @return the "font-weight" property */
  144. public int getFontWeight() {
  145. return fontWeight.getEnum();
  146. }
  147. /** @return the "font-size" property. */
  148. public Length getFontSize() {
  149. return fontSize;
  150. }
  151. /** @return the "font-size-adjust" property. */
  152. public Numeric getFontSizeAdjust() {
  153. return fontSizeAdjust;
  154. }
  155. /**
  156. * Create and return an array of <code>FontTriplets</code> based on
  157. * the properties stored in the instance variables.
  158. * @param fontInfo a font info object
  159. * @return a font triplet
  160. */
  161. public FontTriplet[] getFontState(FontInfo fontInfo) {
  162. int fw;
  163. switch (fontWeight.getEnum()) {
  164. case Constants.EN_100: fw = 100; break;
  165. case Constants.EN_200: fw = 200; break;
  166. case Constants.EN_300: fw = 300; break;
  167. case Constants.EN_400: fw = 400; break;
  168. case Constants.EN_500: fw = 500; break;
  169. case Constants.EN_600: fw = 600; break;
  170. case Constants.EN_700: fw = 700; break;
  171. case Constants.EN_800: fw = 800; break;
  172. case Constants.EN_900: fw = 900; break;
  173. default: fw = 400;
  174. }
  175. String style;
  176. switch (fontStyle.getEnum()) {
  177. case Constants.EN_ITALIC:
  178. style = "italic";
  179. break;
  180. case Constants.EN_OBLIQUE:
  181. style = "oblique";
  182. break;
  183. case Constants.EN_BACKSLANT:
  184. style = "backslant";
  185. break;
  186. default:
  187. style = "normal";
  188. }
  189. // NOTE: this is incomplete. font-size may be specified with
  190. // various kinds of keywords too
  191. //int fontVariant = propertyList.get("font-variant").getEnum();
  192. FontTriplet[] triplets = fontInfo.fontLookup(
  193. getFontFamily(),
  194. style, fw);
  195. return triplets;
  196. }
  197. /** {@inheritDoc} */
  198. @Override
  199. public boolean equals(Object obj) {
  200. if (this == obj) {
  201. return true;
  202. }
  203. if (!(obj instanceof CommonFont)) {
  204. return false;
  205. }
  206. CommonFont other = (CommonFont) obj;
  207. return CompareUtil.equal(fontFamily, other.fontFamily)
  208. && CompareUtil.equal(fontSelectionStrategy, other.fontSelectionStrategy)
  209. && CompareUtil.equal(fontSize, other.fontSize)
  210. && CompareUtil.equal(fontSizeAdjust, other.fontSizeAdjust)
  211. && CompareUtil.equal(fontStretch, other.fontStretch)
  212. && CompareUtil.equal(fontStyle, other.fontStyle)
  213. && CompareUtil.equal(fontVariant, other.fontVariant)
  214. && CompareUtil.equal(fontWeight, other.fontWeight);
  215. }
  216. /** {@inheritDoc} */
  217. public int hashCode() {
  218. if (this.hash == -1) {
  219. int hash = 17;
  220. hash = 37 * hash + CompareUtil.getHashCode(fontSize);
  221. hash = 37 * hash + CompareUtil.getHashCode(fontSizeAdjust);
  222. hash = 37 * hash + CompareUtil.getHashCode(fontFamily);
  223. hash = 37 * hash + CompareUtil.getHashCode(fontSelectionStrategy);
  224. hash = 37 * hash + CompareUtil.getHashCode(fontStretch);
  225. hash = 37 * hash + CompareUtil.getHashCode(fontStyle);
  226. hash = 37 * hash + CompareUtil.getHashCode(fontVariant);
  227. hash = 37 * hash + CompareUtil.getHashCode(fontWeight);
  228. this.hash = hash;
  229. }
  230. return hash;
  231. }
  232. }