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.

FontInfo.java 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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.util.Collection;
  20. import java.util.Collections;
  21. import java.util.Iterator;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. /**
  28. * The FontInfo holds font information for the layout and rendering of a fo document.
  29. * This stores the list of available fonts that are setup by
  30. * the renderer. The font name can be retrieved for the
  31. * family style and weight.
  32. * <br>
  33. * Currently font supported font-variant small-caps is not
  34. * implemented.
  35. */
  36. public class FontInfo {
  37. /** logging instance */
  38. protected static final Log log = LogFactory.getLog(FontInfo.class);
  39. /** Map containing fonts that have been used */
  40. private Map/*<String,FontMetrics>*/ usedFonts = null; //(String = font key)
  41. /** look up a font-triplet to find a font-name */
  42. private Map/*<FontTriplet,String>*/ triplets = null; //(String = font key)
  43. /** look up a font-triplet to find its priority
  44. * (only used inside addFontProperties()) */
  45. private Map/*<FontTriplet,Integer>*/ tripletPriorities = null; //Map<FontTriplet,Integer>
  46. /** look up a font-name to get a font (that implements FontMetrics at least) */
  47. private Map/*<String,FontMetrics>*/ fonts = null; //(String = font key)
  48. /**
  49. * a collection of missing fonts; used to make sure the user gets
  50. * a warning for a missing font only once (not every time the font is used)
  51. */
  52. private Set/*<FontTriplet>*/ loggedFontKeys = null;
  53. /** Cache for Font instances. */
  54. private Map/*<FontTriplet, Map>*/ fontInstanceCache = null;
  55. /** Event listener for font events */
  56. private FontEventListener eventListener = null;
  57. /**
  58. * Main constructor
  59. */
  60. public FontInfo() {
  61. this.triplets = new java.util.HashMap/*<FontTriplet, String>*/();
  62. this.tripletPriorities = new java.util.HashMap/*<FontTriplet, Integer>*/();
  63. this.fonts = new java.util.HashMap/*<String, FontMetrics>*/();
  64. this.usedFonts = new java.util.HashMap/*<String,FontMetrics>*/();
  65. }
  66. /**
  67. * Sets the font event listener that can be used to receive events about particular events
  68. * in this class.
  69. * @param listener the font event listener
  70. */
  71. public void setEventListener(FontEventListener listener) {
  72. this.eventListener = listener;
  73. }
  74. /**
  75. * Checks if the font setup is valid (At least the ultimate fallback font
  76. * must be registered.)
  77. * @return True if valid
  78. */
  79. public boolean isSetupValid() {
  80. //We're only called when font setup is done:
  81. tripletPriorities = null; // candidate for garbage collection
  82. return triplets.containsKey(Font.DEFAULT_FONT);
  83. }
  84. /**
  85. * Adds a new font triplet.
  86. * @param name internal key
  87. * @param family font family name
  88. * @param style font style (normal, italic, oblique...)
  89. * @param weight font weight
  90. */
  91. public void addFontProperties(String name, String family, String style, int weight) {
  92. addFontProperties(name, createFontKey(family, style, weight));
  93. }
  94. /**
  95. * Adds a series of new font triplets given an array of font family names.
  96. * @param name internal key
  97. * @param families an array of font family names
  98. * @param style font style (normal, italic, oblique...)
  99. * @param weight font weight
  100. */
  101. public void addFontProperties(String name, String[] families, String style, int weight) {
  102. for (int i = 0; i < families.length; i++) {
  103. addFontProperties(name, families[i], style, weight);
  104. }
  105. }
  106. /**
  107. * Adds a new font triplet.
  108. * @param internalFontKey internal font key
  109. * @param triplet the font triplet to associate with the internal key
  110. */
  111. public void addFontProperties(String internalFontKey, FontTriplet triplet) {
  112. /*
  113. * add the given family, style and weight as a lookup for the font
  114. * with the given name
  115. */
  116. if (log.isDebugEnabled()) {
  117. log.debug("Registering: " + triplet + " under " + internalFontKey);
  118. }
  119. String oldName = (String)triplets.get(triplet);
  120. int newPriority = triplet.getPriority();
  121. if (oldName != null) {
  122. int oldPriority = ((Integer)tripletPriorities.get(triplet)).intValue();
  123. if (oldPriority < newPriority) {
  124. logDuplicateFont(triplet, false, oldName, oldPriority,
  125. internalFontKey, newPriority);
  126. return;
  127. } else {
  128. logDuplicateFont(triplet, true, oldName, oldPriority,
  129. internalFontKey, newPriority);
  130. }
  131. }
  132. this.triplets.put(triplet, internalFontKey);
  133. this.tripletPriorities.put(triplet, new Integer(newPriority));
  134. }
  135. /**
  136. * Log warning about duplicate font triplets.
  137. *
  138. * @param triplet the duplicate font triplet
  139. * @param replacing true iff the new font will replace the old one
  140. * @param oldKey the old internal font name
  141. * @param oldPriority the priority of the existing font mapping
  142. * @param newKey the new internal font name
  143. * @param newPriority the priority of the duplicate font mapping
  144. */
  145. private void logDuplicateFont(FontTriplet triplet, boolean replacing,
  146. String oldKey, int oldPriority,
  147. String newKey, int newPriority) {
  148. if (log.isDebugEnabled()) {
  149. log.debug(triplet
  150. + (replacing ? ": Replacing " : ": Not replacing ")
  151. + ((FontMetrics)fonts.get(triplets.get(triplet))).getFullName()
  152. + " (priority=" + oldPriority + ") by "
  153. + ((FontMetrics)fonts.get(newKey)).getFullName()
  154. + " (priority=" + newPriority + ")");
  155. }
  156. }
  157. /**
  158. * Adds font metrics for a specific font.
  159. * @param internalFontKey internal key
  160. * @param metrics metrics to register
  161. */
  162. public void addMetrics(String internalFontKey, FontMetrics metrics) {
  163. // add the given metrics as a font with the given name
  164. if (metrics instanceof Typeface) {
  165. ((Typeface)metrics).setEventListener(this.eventListener);
  166. }
  167. this.fonts.put(internalFontKey, metrics);
  168. }
  169. /**
  170. * Lookup a font.
  171. * <br>
  172. * Locate the font name for a given family, style and weight.
  173. * The font name can then be used as a key as it is unique for
  174. * the associated document.
  175. * This also adds the font to the list of used fonts.
  176. * @param family font family
  177. * @param style font style
  178. * @param weight font weight
  179. * @param substitutable true if the font may be substituted with the
  180. * default font if not found
  181. * @return internal font triplet key
  182. */
  183. private FontTriplet fontLookup(String family, String style,
  184. int weight, boolean substitutable) {
  185. if (log.isTraceEnabled()) {
  186. log.trace("Font lookup: " + family + " " + style + " " + weight
  187. + (substitutable ? " substitutable" : ""));
  188. }
  189. FontTriplet startKey = createFontKey(family, style, weight);
  190. FontTriplet fontTriplet = startKey;
  191. // first try given parameters
  192. String internalFontKey = getInternalFontKey(fontTriplet);
  193. if (internalFontKey == null) {
  194. fontTriplet = fuzzyFontLookup(family, style, weight, startKey, substitutable);
  195. }
  196. if (fontTriplet != null) {
  197. if (fontTriplet != startKey) {
  198. notifyFontReplacement(startKey, fontTriplet);
  199. }
  200. return fontTriplet;
  201. } else {
  202. return null;
  203. }
  204. }
  205. private FontTriplet fuzzyFontLookup(String family, String style,
  206. int weight, FontTriplet startKey, boolean substitutable) {
  207. FontTriplet key;
  208. String internalFontKey = null;
  209. if (!family.equals(startKey.getName())) {
  210. key = createFontKey(family, style, weight);
  211. internalFontKey = getInternalFontKey(key);
  212. if (internalFontKey != null) {
  213. return key;
  214. }
  215. }
  216. // adjust weight, favouring normal or bold
  217. key = findAdjustWeight(family, style, weight);
  218. if (key != null) {
  219. internalFontKey = getInternalFontKey(key);
  220. }
  221. // return null if not found and not substitutable
  222. if (!substitutable && internalFontKey == null) {
  223. return null;
  224. }
  225. // only if the font may be substituted
  226. // fallback 1: try the same font-family and weight with default style
  227. if (internalFontKey == null && style != Font.STYLE_NORMAL) {
  228. key = createFontKey(family, Font.STYLE_NORMAL, weight);
  229. internalFontKey = getInternalFontKey(key);
  230. }
  231. // fallback 2: try the same font-family with default style and try to adjust weight
  232. if (internalFontKey == null && style != Font.STYLE_NORMAL) {
  233. key = findAdjustWeight(family, Font.STYLE_NORMAL, weight);
  234. if (key != null) {
  235. internalFontKey = getInternalFontKey(key);
  236. }
  237. }
  238. // fallback 3: try any family with original style/weight
  239. if (internalFontKey == null) {
  240. return fuzzyFontLookup("any", style, weight, startKey, false);
  241. }
  242. // last resort: use default
  243. if (key == null && internalFontKey == null) {
  244. key = Font.DEFAULT_FONT;
  245. internalFontKey = getInternalFontKey(key);
  246. }
  247. if (internalFontKey != null) {
  248. return key;
  249. } else {
  250. return null;
  251. }
  252. }
  253. /**
  254. * Tells this class that the font with the given internal name has been used.
  255. * @param internalName the internal font name (F1, F2 etc.)
  256. */
  257. public void useFont(String internalName) {
  258. usedFonts.put(internalName, fonts.get(internalName));
  259. }
  260. private Map/*<FontTriplet,Map<Integer,Font>>*/ getFontInstanceCache() {
  261. if (fontInstanceCache == null) {
  262. fontInstanceCache = new java.util.HashMap/*<FontTriplet, Map<Integer,Font>>*/();
  263. }
  264. return fontInstanceCache;
  265. }
  266. /**
  267. * Retrieves a (possibly cached) Font instance based on a FontTriplet and a font size.
  268. *
  269. * @param triplet the font triplet designating the requested font
  270. * @param fontSize the font size
  271. * @return the requested Font instance
  272. */
  273. public Font getFontInstance(FontTriplet triplet, int fontSize) {
  274. Map/*<Integer,Font>*/ sizes
  275. = (Map/*<Integer,Font>*/)getFontInstanceCache().get(triplet);
  276. if (sizes == null) {
  277. sizes = new java.util.HashMap/*<Integer,Font>*/();
  278. getFontInstanceCache().put(triplet, sizes);
  279. }
  280. Integer size = new Integer(fontSize);
  281. Font font = (Font)sizes.get(size);
  282. if (font == null) {
  283. String fontKey = getInternalFontKey(triplet);
  284. useFont(fontKey);
  285. FontMetrics metrics = getMetricsFor(fontKey);
  286. font = new Font(fontKey, triplet, metrics, fontSize);
  287. sizes.put(size, font);
  288. }
  289. return font;
  290. }
  291. private List/*<FontTriplet>*/ getTripletsForName(String fontName) {
  292. List/*<FontTriplet>*/ matchedTriplets = new java.util.ArrayList/*<FontTriplet>*/();
  293. Iterator it = triplets.keySet().iterator();
  294. while (it.hasNext()) {
  295. FontTriplet triplet = (FontTriplet)it.next();
  296. String tripletName = triplet.getName();
  297. if (tripletName.toLowerCase().equals(fontName.toLowerCase())) {
  298. matchedTriplets.add(triplet);
  299. }
  300. }
  301. return matchedTriplets;
  302. }
  303. /**
  304. * Returns a suitable internal font given an AWT Font instance.
  305. *
  306. * @param awtFont the AWT font
  307. * @return a best matching internal Font
  308. */
  309. public Font getFontInstanceForAWTFont(java.awt.Font awtFont) {
  310. String awtFontName = awtFont.getName();
  311. String awtFontFamily = awtFont.getFamily();
  312. String awtFontStyle = awtFont.isItalic() ? Font.STYLE_ITALIC : Font.STYLE_NORMAL;
  313. int awtFontWeight = awtFont.isBold() ? Font.WEIGHT_BOLD : Font.WEIGHT_NORMAL;
  314. FontTriplet matchedTriplet = null;
  315. List/*<FontTriplet>*/ triplets = getTripletsForName(awtFontName);
  316. if (!triplets.isEmpty()) {
  317. Iterator it = triplets.iterator();
  318. while (it.hasNext()) {
  319. FontTriplet triplet = (FontTriplet)it.next();
  320. boolean styleMatched = triplet.getStyle().equals(awtFontStyle);
  321. boolean weightMatched = triplet.getWeight() == awtFontWeight;
  322. if (styleMatched && weightMatched) {
  323. matchedTriplet = triplet;
  324. break;
  325. }
  326. }
  327. }
  328. // not matched on font name so do a lookup using family
  329. if (matchedTriplet == null) {
  330. if (awtFontFamily.equals("sanserif")) {
  331. awtFontFamily = "sans-serif";
  332. }
  333. matchedTriplet = fontLookup(awtFontFamily, awtFontStyle, awtFontWeight);
  334. }
  335. int fontSize = Math.round(awtFont.getSize2D() * 1000);
  336. return getFontInstance(matchedTriplet, fontSize);
  337. }
  338. /**
  339. * Lookup a font.
  340. * <br>
  341. * Locate the font name for a given family, style and weight.
  342. * The font name can then be used as a key as it is unique for
  343. * the associated document.
  344. * This also adds the font to the list of used fonts.
  345. * @param family font family
  346. * @param style font style
  347. * @param weight font weight
  348. * @return the font triplet of the font chosen
  349. */
  350. public FontTriplet fontLookup(String family, String style,
  351. int weight) {
  352. return fontLookup(family, style, weight, true);
  353. }
  354. private List/*<FontTriplet>*/ fontLookup(String[] families, String style,
  355. int weight, boolean substitutable) {
  356. List/*<FontTriplet>*/ matchingTriplets = new java.util.ArrayList/*<FontTriplet>*/();
  357. FontTriplet triplet = null;
  358. for (int i = 0; i < families.length; i++) {
  359. triplet = fontLookup(families[i], style, weight, substitutable);
  360. if (triplet != null) {
  361. matchingTriplets.add(triplet);
  362. }
  363. }
  364. return matchingTriplets;
  365. }
  366. /**
  367. * Looks up a set of fonts.
  368. * <br>
  369. * Locate the font name(s) for the given families, style and weight.
  370. * The font name(s) can then be used as a key as they are unique for
  371. * the associated document.
  372. * This also adds the fonts to the list of used fonts.
  373. * @param families font families (priority list)
  374. * @param style font style
  375. * @param weight font weight
  376. * @return the set of font triplets of all supported and chosen font-families
  377. * in the specified style and weight.
  378. */
  379. public FontTriplet[] fontLookup(String[] families, String style,
  380. int weight) {
  381. if (families.length == 0) {
  382. throw new IllegalArgumentException("Specify at least one font family");
  383. }
  384. // try matching without substitutions
  385. List/*<FontTriplet>*/ matchedTriplets = fontLookup(families, style, weight, false);
  386. // if there are no matching font triplets found try with substitutions
  387. if (matchedTriplets.size() == 0) {
  388. matchedTriplets = fontLookup(families, style, weight, true);
  389. }
  390. // no matching font triplets found!
  391. if (matchedTriplets.size() == 0) {
  392. StringBuffer sb = new StringBuffer();
  393. for (int i = 0, c = families.length; i < c; i++) {
  394. if (i > 0) {
  395. sb.append(", ");
  396. }
  397. sb.append(families[i]);
  398. }
  399. throw new IllegalStateException(
  400. "fontLookup must return an array with at least one "
  401. + "FontTriplet on the last call. Lookup: " + sb.toString());
  402. }
  403. FontTriplet[] fontTriplets = new FontTriplet[matchedTriplets.size()];
  404. matchedTriplets.toArray(fontTriplets);
  405. // found some matching fonts so return them
  406. return fontTriplets;
  407. }
  408. private Set/*<FontTriplet>*/ getLoggedFontKeys() {
  409. if (loggedFontKeys == null) {
  410. loggedFontKeys = new java.util.HashSet/*<FontTriplet>*/();
  411. }
  412. return loggedFontKeys;
  413. }
  414. private void notifyFontReplacement(FontTriplet replacedKey, FontTriplet newKey) {
  415. if (!getLoggedFontKeys().contains(replacedKey)) {
  416. getLoggedFontKeys().add(replacedKey);
  417. if (this.eventListener != null) {
  418. this.eventListener.fontSubstituted(this, replacedKey, newKey);
  419. } else {
  420. log.warn("Font '" + replacedKey + "' not found. "
  421. + "Substituting with '" + newKey + "'.");
  422. }
  423. }
  424. }
  425. /**
  426. * Find a font with a given family and style by trying
  427. * different font weights according to the spec.
  428. * @param family font family
  429. * @param style font style
  430. * @param weight font weight
  431. * @return internal key
  432. */
  433. public FontTriplet findAdjustWeight(String family, String style,
  434. int weight) {
  435. FontTriplet key = null;
  436. String f = null;
  437. int newWeight = weight;
  438. if (newWeight <= 400) {
  439. while (f == null && newWeight > 100) {
  440. newWeight -= 100;
  441. key = createFontKey(family, style, newWeight);
  442. f = getInternalFontKey(key);
  443. }
  444. newWeight = weight;
  445. while (f == null && newWeight < 400) {
  446. newWeight += 100;
  447. key = createFontKey(family, style, newWeight);
  448. f = getInternalFontKey(key);
  449. }
  450. } else if (newWeight == 500) {
  451. key = createFontKey(family, style, 400);
  452. f = getInternalFontKey(key);
  453. } else if (newWeight > 500) {
  454. while (f == null && newWeight < 1000) {
  455. newWeight += 100;
  456. key = createFontKey(family, style, newWeight);
  457. f = getInternalFontKey(key);
  458. }
  459. newWeight = weight;
  460. while (f == null && newWeight > 400) {
  461. newWeight -= 100;
  462. key = createFontKey(family, style, newWeight);
  463. f = getInternalFontKey(key);
  464. }
  465. }
  466. if (f == null && weight != 400) {
  467. key = createFontKey(family, style, 400);
  468. f = getInternalFontKey(key);
  469. }
  470. if (f != null) {
  471. return key;
  472. } else {
  473. return null;
  474. }
  475. }
  476. /**
  477. * Determines if a particular font is available.
  478. * @param family font family
  479. * @param style font style
  480. * @param weight font weight
  481. * @return True if available
  482. */
  483. public boolean hasFont(String family, String style, int weight) {
  484. FontTriplet key = createFontKey(family, style, weight);
  485. return this.triplets.containsKey(key);
  486. }
  487. /**
  488. * Returns the internal font key (F1, F2, F3 etc.) for a given triplet.
  489. * @param triplet the font triplet
  490. * @return the associated internal key or null, if not found
  491. */
  492. public String getInternalFontKey(FontTriplet triplet) {
  493. return (String)triplets.get(triplet);
  494. }
  495. /**
  496. * Creates a key from the given strings.
  497. * @param family font family
  498. * @param style font style
  499. * @param weight font weight
  500. * @return internal key
  501. */
  502. public static FontTriplet createFontKey(String family, String style,
  503. int weight) {
  504. return new FontTriplet(family, style, weight);
  505. }
  506. /**
  507. * Gets a Map of all registered fonts.
  508. * @return a read-only Map with font key/FontMetrics pairs
  509. */
  510. public Map/*<String,FontMetrics>*/ getFonts() {
  511. return java.util.Collections.unmodifiableMap(this.fonts);
  512. }
  513. /**
  514. * Gets a Map of all registered font triplets.
  515. * @return a Map with FontTriplet/font key pairs
  516. */
  517. public Map/*<FontTriplet,String>*/ getFontTriplets() {
  518. return this.triplets;
  519. }
  520. /**
  521. * This is used by the renderers to retrieve all the
  522. * fonts used in the document.
  523. * This is for embedded font or creating a list of used fonts.
  524. * @return a read-only Map with font key/FontMetrics pairs
  525. */
  526. public Map/*<String,FontMetrics>*/ getUsedFonts() {
  527. return this.usedFonts;
  528. }
  529. /**
  530. * Returns the FontMetrics for a particular font
  531. * @param fontName internal key
  532. * @return font metrics
  533. */
  534. public FontMetrics getMetricsFor(String fontName) {
  535. FontMetrics metrics = (FontMetrics)fonts.get(fontName);
  536. usedFonts.put(fontName, metrics);
  537. return metrics;
  538. }
  539. /**
  540. * Returns all font triplet matching the given font name.
  541. * @param fontName The font name we are looking for
  542. * @return A list of matching font triplets
  543. */
  544. public List/*<FontTriplet>*/ getTripletsFor(String fontName) {
  545. List/*<FontTriplet>*/ foundTriplets = new java.util.ArrayList();
  546. for (Iterator iter = triplets.entrySet().iterator(); iter.hasNext();) {
  547. Map.Entry tripletEntry = (Map.Entry) iter.next();
  548. if (fontName.equals((tripletEntry.getValue()))) {
  549. foundTriplets.add(tripletEntry.getKey());
  550. }
  551. }
  552. return foundTriplets;
  553. }
  554. /**
  555. * Returns the first triplet matching the given font name.
  556. * As there may be multiple triplets matching the font name
  557. * the result set is sorted first to guarantee consistent results.
  558. * @param fontName The font name we are looking for
  559. * @return The first triplet for the given font name
  560. */
  561. public FontTriplet getTripletFor(String fontName) {
  562. List/*<FontTriplet>*/ foundTriplets = getTripletsFor(fontName);
  563. if (foundTriplets.size() > 0) {
  564. Collections.sort(foundTriplets);
  565. return (FontTriplet)foundTriplets.get(0);
  566. }
  567. return null;
  568. }
  569. /**
  570. * Returns the font style for a particular font.
  571. * There may be multiple font styles matching this font. Only the first
  572. * found is returned. Searching is done on a sorted list to guarantee consistent
  573. * results.
  574. * @param fontName internal key
  575. * @return font style
  576. */
  577. public String getFontStyleFor(String fontName) {
  578. FontTriplet triplet = getTripletFor(fontName);
  579. if (triplet != null) {
  580. return triplet.getStyle();
  581. } else {
  582. return "";
  583. }
  584. }
  585. /**
  586. * Returns the font weight for a particular font.
  587. * There may be multiple font weights matching this font. Only the first
  588. * found is returned. Searching is done on a sorted list to guarantee consistent
  589. * results.
  590. * @param fontName internal key
  591. * @return font weight
  592. */
  593. public int getFontWeightFor(String fontName) {
  594. FontTriplet triplet = getTripletFor(fontName);
  595. if (triplet != null) {
  596. return triplet.getWeight();
  597. } else {
  598. return 0;
  599. }
  600. }
  601. /**
  602. * Diagnostic method for logging all registered fonts to System.out.
  603. */
  604. public void dumpAllTripletsToSystemOut() {
  605. System.out.print(toString());
  606. }
  607. /**
  608. * {@inheritDoc}
  609. */
  610. public String toString() {
  611. Collection entries = new java.util.TreeSet();
  612. Iterator iter = this.triplets.keySet().iterator();
  613. while (iter.hasNext()) {
  614. FontTriplet triplet = (FontTriplet)iter.next();
  615. String key = getInternalFontKey(triplet);
  616. FontMetrics metrics = getMetricsFor(key);
  617. entries.add(triplet.toString() + " -> " + key + " -> " + metrics.getFontName() + "\n");
  618. }
  619. StringBuffer stringBuffer = new StringBuffer();
  620. for (iter = entries.iterator(); iter.hasNext();) {
  621. stringBuffer.append(iter.next());
  622. }
  623. return stringBuffer.toString();
  624. }
  625. }