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.

FOToPDFRoleMap.java 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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.render.pdf;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.apache.fop.events.EventBroadcaster;
  22. import org.apache.fop.pdf.PDFName;
  23. import org.apache.fop.pdf.PDFObject;
  24. import org.apache.fop.pdf.PDFStructElem;
  25. /**
  26. * This class provides the standard mappings from Formatting Objects to PDF structure types.
  27. */
  28. final class FOToPDFRoleMap {
  29. /**
  30. * Standard structure types defined by the PDF Reference, Fourth Edition (PDF 1.5).
  31. */
  32. private static final Map<String, PDFName> STANDARD_STRUCTURE_TYPES
  33. = new HashMap<String, PDFName>();
  34. private static final Map<String, Mapper> DEFAULT_MAPPINGS
  35. = new java.util.HashMap<String, Mapper>();
  36. private static final PDFName THEAD;
  37. private static final PDFName NON_STRUCT;
  38. static {
  39. // Create PDFNames for the standard structure types
  40. // Table 10.18: Grouping elements
  41. addStructureType("Document");
  42. addStructureType("Part");
  43. addStructureType("Art");
  44. addStructureType("Sect");
  45. addStructureType("Div");
  46. addStructureType("BlockQuote");
  47. addStructureType("Caption");
  48. addStructureType("TOC");
  49. addStructureType("TOCI");
  50. addStructureType("Index");
  51. addStructureType("NonStruct");
  52. addStructureType("Private");
  53. // Table 10.20: Paragraphlike elements
  54. addStructureType("H");
  55. addStructureType("H1");
  56. addStructureType("H2");
  57. addStructureType("H3");
  58. addStructureType("H4");
  59. addStructureType("H5");
  60. addStructureType("H6");
  61. addStructureType("P");
  62. // Table 10.21: List elements
  63. addStructureType("L");
  64. addStructureType("LI");
  65. addStructureType("Lbl");
  66. addStructureType("LBody");
  67. // Table 10.22: Table elements
  68. addStructureType("Table");
  69. addStructureType("TR");
  70. addStructureType("TH");
  71. addStructureType("TD");
  72. addStructureType("THead");
  73. addStructureType("TBody");
  74. addStructureType("TFoot");
  75. // Table 10.23: Inline-level structure elements
  76. addStructureType("Span");
  77. addStructureType("Quote");
  78. addStructureType("Note");
  79. addStructureType("Reference");
  80. addStructureType("BibEntry");
  81. addStructureType("Code");
  82. addStructureType("Link");
  83. addStructureType("Annot");
  84. // Table 10.24: Ruby and Warichu elements
  85. addStructureType("Ruby");
  86. addStructureType("RB");
  87. addStructureType("RT");
  88. addStructureType("RP");
  89. addStructureType("Warichu");
  90. addStructureType("WT");
  91. addStructureType("WP");
  92. // Table 10.25: Illustration elements
  93. addStructureType("Figure");
  94. addStructureType("Formula");
  95. addStructureType("Form");
  96. NON_STRUCT = (PDFName) STANDARD_STRUCTURE_TYPES.get("NonStruct");
  97. assert NON_STRUCT != null;
  98. THEAD = (PDFName) STANDARD_STRUCTURE_TYPES.get("THead");
  99. assert THEAD != null;
  100. // Create the standard mappings
  101. // Declarations and Pagination and Layout Formatting Objects
  102. addMapping("root", "Document");
  103. addMapping("page-sequence", "Part");
  104. addMapping("flow", "Sect");
  105. addMapping("static-content", "Sect");
  106. // Block-level Formatting Objects
  107. addMapping("block", "P");
  108. addMapping("block-container", "Div");
  109. // Inline-level Formatting Objects
  110. addMapping("character", "Span");
  111. addMapping("external-graphic", "Figure");
  112. addMapping("instream-foreign-object", "Figure");
  113. addMapping("inline", "Span");
  114. addMapping("inline-container", "Div");
  115. addMapping("page-number", "Quote");
  116. addMapping("page-number-citation", "Quote");
  117. addMapping("page-number-citation-last", "Quote");
  118. // Formatting Objects for Tables
  119. addMapping("table-and-caption", "Div");
  120. addMapping("table", "Table");
  121. addMapping("table-caption", "Caption");
  122. addMapping("table-header", "THead");
  123. addMapping("table-footer", "TFoot");
  124. addMapping("table-body", "TBody");
  125. addMapping("table-row", "TR");
  126. addMapping("table-cell", new TableCellMapper());
  127. // Formatting Objects for Lists
  128. addMapping("list-block", "L");
  129. addMapping("list-item", "LI");
  130. addMapping("list-item-body", "LBody");
  131. addMapping("list-item-label", "Lbl");
  132. // Dynamic Effects: Link and Multi Formatting Objects
  133. addMapping("basic-link", "Link");
  134. // Out-of-Line Formatting Objects
  135. addMapping("float", "Div");
  136. addMapping("footnote", "Note");
  137. addMapping("footnote-body", "Sect");
  138. addMapping("wrapper", "Span");
  139. addMapping("marker", "Private");
  140. }
  141. private static void addStructureType(String structureType) {
  142. STANDARD_STRUCTURE_TYPES.put(structureType, new PDFName(structureType));
  143. }
  144. private static void addMapping(String fo, String structureType) {
  145. PDFName type = (PDFName) STANDARD_STRUCTURE_TYPES.get(structureType);
  146. assert type != null;
  147. addMapping(fo, new SimpleMapper(type));
  148. }
  149. private static void addMapping(String fo, Mapper mapper) {
  150. DEFAULT_MAPPINGS.put(fo, mapper);
  151. }
  152. /**
  153. * Maps a Formatting Object to a PDFName representing the associated structure type.
  154. * @param fo the formatting object's local name
  155. * @param parent the parent of the structure element to be mapped
  156. * @return the structure type or null if no match could be found
  157. */
  158. public static PDFName mapFormattingObject(String fo, PDFObject parent) {
  159. Mapper mapper = (Mapper) DEFAULT_MAPPINGS.get(fo);
  160. if (mapper != null) {
  161. return mapper.getStructureType(parent);
  162. } else {
  163. return NON_STRUCT;
  164. }
  165. }
  166. /**
  167. * Maps a Formatting Object to a PDFName representing the associated structure type.
  168. * @param fo the formatting object's local name
  169. * @param role the value of the formatting object's role property
  170. * @param parent the parent of the structure element to be mapped
  171. * @param eventBroadcaster the event broadcaster
  172. * @return the structure type or null if no match could be found
  173. */
  174. public static PDFName mapFormattingObject(String fo, String role,
  175. PDFObject parent, EventBroadcaster eventBroadcaster) {
  176. PDFName type = null;
  177. if (role == null) {
  178. type = mapFormattingObject(fo, parent);
  179. } else {
  180. type = (PDFName) STANDARD_STRUCTURE_TYPES.get(role);
  181. if (type == null) {
  182. type = mapFormattingObject(fo, parent);
  183. PDFEventProducer.Provider.get(eventBroadcaster).nonStandardStructureType(fo,
  184. fo, role, type.toString().substring(1));
  185. }
  186. }
  187. assert type != null;
  188. return type;
  189. }
  190. private interface Mapper {
  191. PDFName getStructureType(PDFObject parent);
  192. }
  193. private static class SimpleMapper implements Mapper {
  194. private PDFName structureType;
  195. public SimpleMapper(PDFName structureType) {
  196. this.structureType = structureType;
  197. }
  198. public PDFName getStructureType(PDFObject parent) {
  199. return structureType;
  200. }
  201. }
  202. private static class TableCellMapper implements Mapper {
  203. public PDFName getStructureType(PDFObject parent) {
  204. PDFStructElem grandParent = (PDFStructElem)
  205. ((PDFStructElem) parent).getParentStructElem();
  206. //TODO What to do with cells from table-footer? Currently they are mapped on TD.
  207. PDFName type;
  208. if (THEAD.equals(grandParent.getStructureType())) {
  209. type = (PDFName) STANDARD_STRUCTURE_TYPES.get("TH");
  210. } else {
  211. type = (PDFName) STANDARD_STRUCTURE_TYPES.get("TD");
  212. }
  213. assert type != null;
  214. return type;
  215. }
  216. }
  217. private FOToPDFRoleMap() { }
  218. }