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

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