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.

Document.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.apps;
  18. // Java
  19. import java.util.Map;
  20. import java.io.IOException;
  21. import java.util.Set;
  22. import java.util.HashSet;
  23. // FOP
  24. import org.apache.fop.apps.FOUserAgent;
  25. import org.apache.fop.area.AreaTree;
  26. import org.apache.fop.area.AreaTreeControl;
  27. import org.apache.fop.area.AreaTreeModel;
  28. import org.apache.fop.fo.extensions.Bookmarks;
  29. import org.apache.fop.fo.FOInputHandler;
  30. import org.apache.fop.fo.FOTreeControl;
  31. import org.apache.fop.fo.FOTreeEvent;
  32. import org.apache.fop.fo.FOTreeListener;
  33. import org.apache.fop.fo.pagination.PageSequence;
  34. import org.apache.fop.fonts.Font;
  35. import org.apache.fop.fonts.FontMetrics;
  36. import org.apache.fop.layout.LayoutStrategy;
  37. // SAX
  38. import org.xml.sax.SAXException;
  39. // Avalon
  40. import org.apache.avalon.framework.logger.Logger;
  41. /**
  42. * Class storing information for the FOP Document being processed, and managing
  43. * the processing of it.
  44. */
  45. public class Document implements FOTreeControl, FOTreeListener,
  46. AreaTreeControl {
  47. /** The parent Driver object */
  48. private Driver driver;
  49. /** Map containing fonts that have been used */
  50. private Map usedFonts;
  51. /** look up a font-triplet to find a font-name */
  52. private Map triplets;
  53. /** look up a font-name to get a font (that implements FontMetrics at least) */
  54. private Map fonts;
  55. /**
  56. * the LayoutStrategy to be used to process this document
  57. * TODO: this actually belongs in the RenderContext class, when it is
  58. * created
  59. */
  60. private LayoutStrategy layoutStrategy = null;
  61. /** The current AreaTree for the PageSequence being rendered. */
  62. public AreaTree areaTree;
  63. /** The AreaTreeModel for the PageSequence being rendered. */
  64. public AreaTreeModel atModel;
  65. private Bookmarks bookmarks = null;
  66. /**
  67. * The current set of id's in the FO tree.
  68. * This is used so we know if the FO tree contains duplicates.
  69. */
  70. private Set idReferences = new HashSet();
  71. /**
  72. * Structure handler used to notify structure events
  73. * such as start end element.
  74. */
  75. public FOInputHandler foInputHandler;
  76. /**
  77. * Main constructor
  78. * @param driver the Driver object that is the "parent" of this Document
  79. */
  80. public Document(Driver driver) {
  81. this.driver = driver;
  82. this.triplets = new java.util.HashMap();
  83. this.fonts = new java.util.HashMap();
  84. this.usedFonts = new java.util.HashMap();
  85. }
  86. /**
  87. * Checks if the font setup is valid (At least the ultimate fallback font
  88. * must be registered.)
  89. * @return True if valid
  90. */
  91. public boolean isSetupValid() {
  92. return triplets.containsKey(Font.DEFAULT_FONT);
  93. }
  94. /**
  95. * Adds a new font triplet.
  96. * @param name internal key
  97. * @param family font family name
  98. * @param style font style (normal, italic, oblique...)
  99. * @param weight font weight
  100. */
  101. public void addFontProperties(String name, String family, String style,
  102. int weight) {
  103. /*
  104. * add the given family, style and weight as a lookup for the font
  105. * with the given name
  106. */
  107. String key = createFontKey(family, style, weight);
  108. this.triplets.put(key, name);
  109. }
  110. /**
  111. * Adds font metrics for a specific font.
  112. * @param name internal key
  113. * @param metrics metrics to register
  114. */
  115. public void addMetrics(String name, FontMetrics metrics) {
  116. // add the given metrics as a font with the given name
  117. this.fonts.put(name, metrics);
  118. }
  119. /**
  120. * Lookup a font.
  121. * <br>
  122. * Locate the font name for a given family, style and weight.
  123. * The font name can then be used as a key as it is unique for
  124. * the associated document.
  125. * This also adds the font to the list of used fonts.
  126. * @param family font family
  127. * @param style font style
  128. * @param weight font weight
  129. * @return internal key
  130. */
  131. public String fontLookup(String family, String style,
  132. int weight) {
  133. String key;
  134. // first try given parameters
  135. key = createFontKey(family, style, weight);
  136. String f = (String)triplets.get(key);
  137. if (f == null) {
  138. // then adjust weight, favouring normal or bold
  139. f = findAdjustWeight(family, style, weight);
  140. // then try any family with orig weight
  141. if (f == null) {
  142. key = createFontKey("any", style, weight);
  143. f = (String)triplets.get(key);
  144. }
  145. // then try any family with adjusted weight
  146. if (f == null) {
  147. f = findAdjustWeight(family, style, weight);
  148. }
  149. // then use default
  150. if (f == null) {
  151. f = (String)triplets.get(Font.DEFAULT_FONT);
  152. }
  153. }
  154. usedFonts.put(f, fonts.get(f));
  155. return f;
  156. }
  157. /**
  158. * Find a font with a given family and style by trying
  159. * different font weights according to the spec.
  160. * @param family font family
  161. * @param style font style
  162. * @param weight font weight
  163. * @return internal key
  164. */
  165. public String findAdjustWeight(String family, String style,
  166. int weight) {
  167. String key;
  168. String f = null;
  169. int newWeight = weight;
  170. if (newWeight < 400) {
  171. while (f == null && newWeight > 0) {
  172. newWeight -= 100;
  173. key = createFontKey(family, style, newWeight);
  174. f = (String)triplets.get(key);
  175. }
  176. } else if (newWeight == 500) {
  177. key = createFontKey(family, style, 400);
  178. f = (String)triplets.get(key);
  179. } else if (newWeight > 500) {
  180. while (f == null && newWeight < 1000) {
  181. newWeight += 100;
  182. key = createFontKey(family, style, newWeight);
  183. f = (String)triplets.get(key);
  184. }
  185. newWeight = weight;
  186. while (f == null && newWeight > 400) {
  187. newWeight -= 100;
  188. key = createFontKey(family, style, newWeight);
  189. f = (String)triplets.get(key);
  190. }
  191. }
  192. if (f == null) {
  193. key = createFontKey(family, style, 400);
  194. f = (String)triplets.get(key);
  195. }
  196. return f;
  197. }
  198. /**
  199. * Determines if a particular font is available.
  200. * @param family font family
  201. * @param style font style
  202. * @param weight font weight
  203. * @return True if available
  204. */
  205. public boolean hasFont(String family, String style, int weight) {
  206. String key = createFontKey(family, style, weight);
  207. return this.triplets.containsKey(key);
  208. }
  209. /**
  210. * Creates a key from the given strings.
  211. * @param family font family
  212. * @param style font style
  213. * @param weight font weight
  214. * @return internal key
  215. */
  216. public static String createFontKey(String family, String style,
  217. int weight) {
  218. return family + "," + style + "," + weight;
  219. }
  220. /**
  221. * Gets a Map of all registred fonts.
  222. * @return a read-only Map with font key/FontMetrics pairs
  223. */
  224. public Map getFonts() {
  225. return java.util.Collections.unmodifiableMap(this.fonts);
  226. }
  227. /**
  228. * This is used by the renderers to retrieve all the
  229. * fonts used in the document.
  230. * This is for embedded font or creating a list of used fonts.
  231. * @return a read-only Map with font key/FontMetrics pairs
  232. */
  233. public Map getUsedFonts() {
  234. return this.usedFonts;
  235. }
  236. /**
  237. * Returns the FontMetrics for a particular font
  238. * @param fontName internal key
  239. * @return font metrics
  240. */
  241. public FontMetrics getMetricsFor(String fontName) {
  242. usedFonts.put(fontName, fonts.get(fontName));
  243. return (FontMetrics)fonts.get(fontName);
  244. }
  245. /**
  246. * Set the LayoutStrategy to be used to process this Document
  247. * @param ls the LayoutStrategy object to be used to process this Document
  248. */
  249. public void setLayoutStrategy(LayoutStrategy ls) {
  250. this.layoutStrategy = ls;
  251. }
  252. /**
  253. * @return this Document's LayoutStrategy object
  254. */
  255. public LayoutStrategy getLayoutStrategy () {
  256. return layoutStrategy;
  257. }
  258. /**
  259. * Public accessor for the parent Driver of this Document
  260. * @return the parent Driver for this Document
  261. */
  262. public Driver getDriver() {
  263. return driver;
  264. }
  265. /**
  266. * Required by the FOTreeListener interface. It handles an
  267. * FOTreeEvent that is fired when a PageSequence object has been completed.
  268. * @param event the FOTreeEvent that was fired
  269. * @throws FOPException for errors in building the PageSequence
  270. */
  271. public void foPageSequenceComplete (FOTreeEvent event) throws FOPException {
  272. PageSequence pageSeq = event.getPageSequence();
  273. areaTree.addBookmarksToAreaTree();
  274. layoutStrategy.format(pageSeq, areaTree);
  275. }
  276. /**
  277. * Required by the FOTreeListener interface. It handles an FOTreeEvent that
  278. * is fired when the Document has been completely parsed.
  279. * @param event the FOTreeEvent that was fired
  280. * @throws SAXException for parsing errors
  281. */
  282. public void foDocumentComplete (FOTreeEvent event) throws SAXException {
  283. //processAreaTree(atModel);
  284. try {
  285. areaTree.endDocument();
  286. driver.getRenderer().stopRenderer();
  287. } catch (IOException ex) {
  288. throw new SAXException(ex);
  289. }
  290. }
  291. /**
  292. * Get the area tree for this layout handler.
  293. *
  294. * @return the area tree for this document
  295. */
  296. public AreaTree getAreaTree() {
  297. return areaTree;
  298. }
  299. /**
  300. * Set the Bookmarks object for this Document
  301. * @param bookmarks the Bookmarks object containing the bookmarks for this
  302. * Document
  303. */
  304. public void setBookmarks(Bookmarks bookmarks) {
  305. this.bookmarks = bookmarks;
  306. }
  307. /**
  308. * Public accessor for the Bookmarks for this Document
  309. * @return the Bookmarks for this Document
  310. */
  311. public Bookmarks getBookmarks() {
  312. return bookmarks;
  313. }
  314. /**
  315. * Retuns the set of ID references.
  316. * @return the ID references
  317. */
  318. public Set getIDReferences() {
  319. return idReferences;
  320. }
  321. /**
  322. * @return the FOInputHandler for parsing this FO Tree
  323. */
  324. public FOInputHandler getFOInputHandler() {
  325. return foInputHandler;
  326. }
  327. /**
  328. * @return the Logger to be used for processing this Document
  329. */
  330. public Logger getLogger() {
  331. return getDriver().getLogger();
  332. }
  333. /**
  334. * @return the FOUserAgent used for processing this document
  335. */
  336. public FOUserAgent getUserAgent() {
  337. return getDriver().getUserAgent();
  338. }
  339. }