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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.Map;
  20. import org.apache.fop.events.EventBroadcaster;
  21. import org.apache.fop.pdf.PDFObject;
  22. import org.apache.fop.pdf.PDFStructElem;
  23. import org.apache.fop.pdf.StandardStructureTypes;
  24. import org.apache.fop.pdf.StructureType;
  25. /**
  26. * This class provides the standard mappings from Formatting Objects to PDF structure types.
  27. */
  28. final class FOToPDFRoleMap {
  29. private static final Map<String, Mapper> DEFAULT_MAPPINGS = new java.util.HashMap<String, Mapper>();
  30. static {
  31. // Create the standard mappings
  32. // Declarations and Pagination and Layout Formatting Objects
  33. addMapping("root", StandardStructureTypes.Grouping.DOCUMENT);
  34. addMapping("page-sequence", StandardStructureTypes.Grouping.PART);
  35. addMapping("flow", StandardStructureTypes.Grouping.SECT);
  36. addMapping("static-content", StandardStructureTypes.Grouping.SECT);
  37. // Block-level Formatting Objects
  38. addMapping("block", StandardStructureTypes.Paragraphlike.P);
  39. addMapping("block-container", StandardStructureTypes.Grouping.DIV);
  40. // Inline-level Formatting Objects
  41. addMapping("character", StandardStructureTypes.InlineLevelStructure.SPAN);
  42. addMapping("external-graphic", StandardStructureTypes.Illustration.FIGURE);
  43. addMapping("instream-foreign-object", StandardStructureTypes.Illustration.FIGURE);
  44. addMapping("inline", StandardStructureTypes.InlineLevelStructure.SPAN);
  45. addMapping("inline-container", StandardStructureTypes.Grouping.DIV);
  46. addMapping("page-number", StandardStructureTypes.InlineLevelStructure.QUOTE);
  47. addMapping("page-number-citation", StandardStructureTypes.InlineLevelStructure.QUOTE);
  48. addMapping("page-number-citation-last", StandardStructureTypes.InlineLevelStructure.QUOTE);
  49. // Formatting Objects for Tables
  50. addMapping("table-and-caption", StandardStructureTypes.Grouping.DIV);
  51. addMapping("table", StandardStructureTypes.Table.TABLE);
  52. addMapping("table-caption", StandardStructureTypes.Grouping.CAPTION);
  53. addMapping("table-header", StandardStructureTypes.Table.THEAD);
  54. addMapping("table-footer", StandardStructureTypes.Table.TFOOT);
  55. addMapping("table-body", StandardStructureTypes.Table.TBODY);
  56. addMapping("table-row", StandardStructureTypes.Table.TR);
  57. addMapping("table-cell", new TableCellMapper());
  58. // Formatting Objects for Lists
  59. addMapping("list-block", StandardStructureTypes.List.L);
  60. addMapping("list-item", StandardStructureTypes.List.LI);
  61. addMapping("list-item-body", StandardStructureTypes.List.LBODY);
  62. addMapping("list-item-label", StandardStructureTypes.List.LBL);
  63. // Dynamic Effects: Link and Multi Formatting Objects
  64. addMapping("basic-link", StandardStructureTypes.InlineLevelStructure.LINK);
  65. // Out-of-Line Formatting Objects
  66. addMapping("float", StandardStructureTypes.Grouping.DIV);
  67. addMapping("footnote", StandardStructureTypes.InlineLevelStructure.NOTE);
  68. addMapping("footnote-body", StandardStructureTypes.Grouping.SECT);
  69. addMapping("wrapper", StandardStructureTypes.InlineLevelStructure.SPAN);
  70. addMapping("marker", StandardStructureTypes.Grouping.PRIVATE);
  71. }
  72. private static void addMapping(String fo, StructureType structureType) {
  73. addMapping(fo, new SimpleMapper(structureType));
  74. }
  75. private static void addMapping(String fo, Mapper mapper) {
  76. DEFAULT_MAPPINGS.put(fo, mapper);
  77. }
  78. /**
  79. * Maps a Formatting Object to a PDFName representing the associated structure type.
  80. * @param fo the formatting object's local name
  81. * @param role the value of the formatting object's role property
  82. * @param parent the parent of the structure element to be mapped
  83. * @param eventBroadcaster the event broadcaster
  84. * @return the structure type or null if no match could be found
  85. */
  86. public static StructureType mapFormattingObject(String fo, String role,
  87. PDFObject parent, EventBroadcaster eventBroadcaster) {
  88. StructureType type = null;
  89. if (role == null) {
  90. type = getDefaultMappingFor(fo, parent);
  91. } else {
  92. type = StandardStructureTypes.get(role);
  93. if (type == null) {
  94. type = getDefaultMappingFor(fo, parent);
  95. PDFEventProducer.Provider.get(eventBroadcaster).nonStandardStructureType(fo,
  96. fo, role, type.toString().substring(1));
  97. }
  98. }
  99. assert type != null;
  100. return type;
  101. }
  102. /**
  103. * Maps a Formatting Object to a PDFName representing the associated structure type.
  104. * @param fo the formatting object's local name
  105. * @param parent the parent of the structure element to be mapped
  106. * @return the structure type or NonStruct if no match could be found
  107. */
  108. private static StructureType getDefaultMappingFor(String fo, PDFObject parent) {
  109. Mapper mapper = DEFAULT_MAPPINGS.get(fo);
  110. if (mapper != null) {
  111. return mapper.getStructureType(parent);
  112. } else {
  113. return StandardStructureTypes.Grouping.NON_STRUCT;
  114. }
  115. }
  116. private interface Mapper {
  117. StructureType getStructureType(PDFObject parent);
  118. }
  119. private static class SimpleMapper implements Mapper {
  120. private StructureType structureType;
  121. public SimpleMapper(StructureType structureType) {
  122. this.structureType = structureType;
  123. }
  124. public StructureType getStructureType(PDFObject parent) {
  125. return structureType;
  126. }
  127. }
  128. private static class TableCellMapper implements Mapper {
  129. public StructureType getStructureType(PDFObject parent) {
  130. PDFStructElem grandParent = ((PDFStructElem) parent).getParentStructElem();
  131. //TODO What to do with cells from table-footer? Currently they are mapped on TD.
  132. if (grandParent.getStructureType() == StandardStructureTypes.Table.THEAD) {
  133. return StandardStructureTypes.Table.TH;
  134. } else {
  135. return StandardStructureTypes.Table.TD;
  136. }
  137. }
  138. }
  139. private FOToPDFRoleMap() { }
  140. }