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

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