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

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