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.

PDFStructElem.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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.pdf;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Locale;
  24. import org.apache.fop.accessibility.StructureTreeElement;
  25. import org.apache.fop.pdf.StandardStructureAttributes.Table;
  26. import org.apache.fop.util.LanguageTags;
  27. /**
  28. * Class representing a PDF Structure Element.
  29. */
  30. public class PDFStructElem extends PDFDictionary implements StructureTreeElement, CompressedObject {
  31. private StructureType structureType;
  32. private PDFStructElem parentElement;
  33. /**
  34. * Elements to be added to the kids array.
  35. */
  36. protected List<PDFObject> kids;
  37. private List<PDFDictionary> attributes;
  38. /**
  39. * Creates a new structure element.
  40. *
  41. * @param parent parent of this element
  42. * @param structureType the structure type of this element
  43. */
  44. PDFStructElem(PDFObject parent, StructureType structureType) {
  45. this(parent);
  46. this.structureType = structureType;
  47. put("S", structureType.getName());
  48. setParent(parent);
  49. }
  50. private PDFStructElem(PDFObject parent) {
  51. if (parent instanceof PDFStructElem) {
  52. parentElement = (PDFStructElem) parent;
  53. }
  54. }
  55. /**
  56. * Returns the parent of this structure element.
  57. *
  58. * @return the parent, <code>null</code> if the parent is not a structure
  59. * element (i.e., is the structure tree root)
  60. */
  61. public PDFStructElem getParentStructElem() {
  62. return parentElement;
  63. }
  64. /** {@inheritDoc} */
  65. public void setParent(PDFObject parent) {
  66. if (parent != null && parent.hasObjectNumber()) {
  67. put("P", new PDFReference(parent));
  68. }
  69. }
  70. /**
  71. * Adds a kid to this structure element.
  72. *
  73. * @param kid element to be added
  74. */
  75. public void addKid(PDFObject kid) {
  76. if (kids == null) {
  77. kids = new ArrayList<PDFObject>();
  78. }
  79. kids.add(kid);
  80. }
  81. /**
  82. * Sets the given mcid as the kid of this structure element. This element
  83. * will then add itself to its parent structure element if it has not
  84. * already, and so will the parent, and so on.
  85. *
  86. * @param mcid mcid of the marked-content sequence corresponding to this
  87. * structure element's kid
  88. */
  89. public void setMCIDKid(int mcid) {
  90. put("K", mcid);
  91. }
  92. /**
  93. * Sets the page reference of this structure element.
  94. *
  95. * @param page value for the Pg entry
  96. */
  97. public void setPage(PDFPage page) {
  98. put("Pg", page);
  99. }
  100. /**
  101. * Returns the structure type of this structure element.
  102. *
  103. * @return the value of the S entry
  104. */
  105. public StructureType getStructureType() {
  106. return structureType;
  107. }
  108. /**
  109. * Sets the language of this structure element.
  110. * @param language the language (as defined in the section about
  111. * "Natural Language Specification")
  112. */
  113. private void setLanguage(String language) {
  114. put("Lang", language);
  115. }
  116. /**
  117. * Sets the language of this structure element.
  118. *
  119. * @param language a value for the Lang entry
  120. */
  121. public void setLanguage(Locale language) {
  122. setLanguage(LanguageTags.toLanguageTag(language));
  123. }
  124. /**
  125. * Returns the language of this structure element.
  126. *
  127. * @return the value of the Lang entry (<code>null</code> if no language was specified)
  128. */
  129. public String getLanguage() {
  130. return (String) get("Lang");
  131. }
  132. @Override
  133. protected void writeDictionary(OutputStream out, StringBuilder textBuffer) throws IOException {
  134. attachKids();
  135. attachAttributes();
  136. super.writeDictionary(out, textBuffer);
  137. }
  138. private void attachAttributes() {
  139. if (attributes != null) {
  140. if (attributes.size() == 1) {
  141. put("A", attributes.get(0));
  142. } else {
  143. PDFArray array = new PDFArray(attributes);
  144. put("A", array);
  145. }
  146. }
  147. }
  148. /**
  149. * Attaches all valid kids to the kids array.
  150. *
  151. * @return true iff 1+ kids were added to the kids array
  152. */
  153. protected boolean attachKids() {
  154. List<PDFObject> validKids = new ArrayList<PDFObject>();
  155. if (kids != null) {
  156. for (PDFObject kid : kids) {
  157. if (kid instanceof Placeholder) {
  158. if (((Placeholder) kid).attachKids()) {
  159. validKids.add(kid);
  160. }
  161. } else {
  162. validKids.add(kid);
  163. }
  164. }
  165. }
  166. boolean kidsAttached = !validKids.isEmpty();
  167. if (kidsAttached) {
  168. PDFArray array = new PDFArray();
  169. for (PDFObject ob : validKids) {
  170. array.add(ob);
  171. }
  172. put("K", array);
  173. }
  174. return kidsAttached;
  175. }
  176. public void setTableAttributeColSpan(int colSpan) {
  177. setTableAttributeRowColumnSpan("ColSpan", colSpan);
  178. }
  179. public void setTableAttributeRowSpan(int rowSpan) {
  180. setTableAttributeRowColumnSpan("RowSpan", rowSpan);
  181. }
  182. private void setTableAttributeRowColumnSpan(String typeSpan, int span) {
  183. PDFDictionary attribute = new PDFDictionary();
  184. attribute.put("O", Table.NAME);
  185. attribute.put(typeSpan, span);
  186. if (attributes == null) {
  187. attributes = new ArrayList<PDFDictionary>(2);
  188. }
  189. attributes.add(attribute);
  190. }
  191. /**
  192. * Class representing a placeholder for a PDF Structure Element.
  193. */
  194. public static class Placeholder extends PDFStructElem {
  195. @Override
  196. public void outputInline(OutputStream out, StringBuilder textBuffer) throws IOException {
  197. if (kids != null) {
  198. assert kids.size() > 0;
  199. for (int i = 0; i < kids.size(); i++) {
  200. if (i > 0) {
  201. textBuffer.append(' ');
  202. }
  203. Object obj = kids.get(i);
  204. formatObject(obj, out, textBuffer);
  205. }
  206. }
  207. }
  208. public Placeholder(PDFObject parent) {
  209. super(parent);
  210. }
  211. }
  212. }