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

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