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

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