Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PDFStructElem.java 9.1KB

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