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.

PDFStructureTreeBuilder.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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.LinkedList;
  20. import java.util.Locale;
  21. import org.xml.sax.Attributes;
  22. import org.apache.fop.accessibility.StructureTreeElement;
  23. import org.apache.fop.accessibility.StructureTreeEventHandler;
  24. import org.apache.fop.events.EventBroadcaster;
  25. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  26. import org.apache.fop.pdf.PDFFactory;
  27. import org.apache.fop.pdf.PDFName;
  28. import org.apache.fop.pdf.PDFObject;
  29. import org.apache.fop.pdf.PDFParentTree;
  30. import org.apache.fop.pdf.PDFStructElem;
  31. import org.apache.fop.pdf.PDFStructTreeRoot;
  32. class PDFStructureTreeBuilder implements StructureTreeEventHandler {
  33. private PDFFactory pdfFactory;
  34. private PDFLogicalStructureHandler logicalStructureHandler;
  35. private EventBroadcaster eventBroadcaster;
  36. private LinkedList<PDFStructElem> ancestors = new LinkedList<PDFStructElem>();
  37. private PDFStructElem rootStructureElement;
  38. void setPdfFactory(PDFFactory pdfFactory) {
  39. this.pdfFactory = pdfFactory;
  40. }
  41. void setLogicalStructureHandler(PDFLogicalStructureHandler logicalStructureHandler) {
  42. this.logicalStructureHandler = logicalStructureHandler;
  43. createRootStructureElement();
  44. }
  45. private void createRootStructureElement() {
  46. assert rootStructureElement == null;
  47. PDFParentTree parentTree = logicalStructureHandler.getParentTree();
  48. PDFStructTreeRoot structTreeRoot = pdfFactory.getDocument().makeStructTreeRoot(parentTree);
  49. rootStructureElement = createStructureElement("root", structTreeRoot, null);
  50. structTreeRoot.addKid(rootStructureElement);
  51. }
  52. void setEventBroadcaster(EventBroadcaster eventBroadcaster) {
  53. this.eventBroadcaster = eventBroadcaster;
  54. }
  55. public void startPageSequence(Locale language, String role) {
  56. ancestors = new LinkedList<PDFStructElem>();
  57. PDFStructElem structElem = createStructureElement("page-sequence", rootStructureElement, role);
  58. if (language != null) {
  59. structElem.setLanguage(language);
  60. }
  61. rootStructureElement.addKid(structElem);
  62. ancestors.add(structElem);
  63. }
  64. private PDFStructElem createStructureElement(String name, PDFObject parent, String role) {
  65. PDFName structureType = FOToPDFRoleMap.mapFormattingObject(name, role, parent, eventBroadcaster);
  66. return pdfFactory.getDocument().makeStructureElement(structureType, parent);
  67. }
  68. public void endPageSequence() {
  69. }
  70. public StructureTreeElement startNode(String name, Attributes attributes) {
  71. PDFStructElem parent = ancestors.getFirst();
  72. String role = attributes.getValue("role");
  73. PDFStructElem structElem = createStructureElement(name, parent, role);
  74. setSpanAttributes(structElem, attributes);
  75. parent.addKid(structElem);
  76. ancestors.addFirst(structElem);
  77. return structElem;
  78. }
  79. private void setSpanAttributes(PDFStructElem structElem, Attributes attributes) {
  80. String columnSpan = attributes.getValue("number-columns-spanned");
  81. if (columnSpan != null) {
  82. structElem.setTableAttributeColSpan(Integer.parseInt(columnSpan));
  83. }
  84. String rowSpan = attributes.getValue("number-rows-spanned");
  85. if (rowSpan != null) {
  86. structElem.setTableAttributeRowSpan(Integer.parseInt(rowSpan));
  87. }
  88. }
  89. public void endNode(String name) {
  90. removeFirstAncestor();
  91. }
  92. private void removeFirstAncestor() {
  93. ancestors.removeFirst();
  94. }
  95. public StructureTreeElement startImageNode(String name, Attributes attributes) {
  96. PDFStructElem parent = ancestors.getFirst();
  97. String role = attributes.getValue("role");
  98. PDFStructElem structElem = createStructureElement(name, parent, role);
  99. parent.addKid(structElem);
  100. String altTextNode = attributes.getValue(ExtensionElementMapping.URI, "alt-text");
  101. if (altTextNode != null) {
  102. structElem.put("Alt", altTextNode);
  103. } else {
  104. structElem.put("Alt", "No alternate text specified");
  105. }
  106. ancestors.addFirst(structElem);
  107. return structElem;
  108. }
  109. public StructureTreeElement startReferencedNode(String name, Attributes attributes) {
  110. PDFStructElem parent = ancestors.getFirst();
  111. String role = attributes.getValue("role");
  112. PDFStructElem structElem;
  113. if ("#PCDATA".equals(name)) {
  114. structElem = new PDFStructElem.Placeholder(parent, name);
  115. } else {
  116. structElem = createStructureElement(name, parent, role);
  117. }
  118. parent.addKid(structElem);
  119. ancestors.addFirst(structElem);
  120. return structElem;
  121. }
  122. }