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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.util.Locale;
  20. import org.apache.fop.util.LanguageTags;
  21. /**
  22. * Class representing a PDF Structure Element.
  23. */
  24. public class PDFStructElem extends PDFDictionary {
  25. private PDFStructElem parentElement;
  26. /**
  27. * Creates a new structure element.
  28. *
  29. * @param parent parent of this element
  30. * @param structureType the structure type of this element
  31. */
  32. PDFStructElem(PDFObject parent, PDFName structureType) {
  33. if (parent instanceof PDFStructElem) {
  34. parentElement = (PDFStructElem) parent;
  35. }
  36. put("Type", new PDFName("StructElem"));
  37. put("S", structureType);
  38. setParent(parent);
  39. }
  40. /**
  41. * Returns the parent of this structure element.
  42. *
  43. * @return the parent, <code>null</code> if the parent is not a structure
  44. * element (i.e., is the structure tree root)
  45. */
  46. public PDFStructElem getParentStructElem() {
  47. return parentElement;
  48. }
  49. /** {@inheritDoc} */
  50. public void setParent(PDFObject parent) {
  51. if (parent != null) {
  52. put("P", new PDFReference(parent));
  53. }
  54. }
  55. /**
  56. * Returns the kids of this structure element.
  57. *
  58. * @return the value of the K entry
  59. */
  60. private PDFArray getKids() {
  61. return (PDFArray) get("K");
  62. }
  63. /**
  64. * Add a kid to this structure element. This element will then add itself to
  65. * its parent structure element if it has not already, and so will the
  66. * parent, and so on.
  67. *
  68. * @param kid element to be added
  69. */
  70. public void addKid(PDFObject kid) {
  71. PDFArray kids = getKids();
  72. if (kids == null) {
  73. kids = new PDFArray();
  74. put("K", kids);
  75. }
  76. kids.add(kid);
  77. joinHierarchy();
  78. }
  79. private boolean containsKid(PDFObject kid) {
  80. PDFArray kids = getKids();
  81. return kids != null && kids.contains(kid);
  82. }
  83. private void joinHierarchy() {
  84. if (parentElement != null && !parentElement.containsKid(this)) {
  85. parentElement.addKid(this);
  86. }
  87. }
  88. /**
  89. * Sets the given mcid as the kid of this structure element. This element
  90. * will then add itself to its parent structure element if it has not
  91. * already, and so will the parent, and so on.
  92. *
  93. * @param mcid mcid of the marked-content sequence corresponding to this
  94. * structure element's kid
  95. */
  96. public void setMCIDKid(int mcid) {
  97. put("K", mcid);
  98. joinHierarchy();
  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 PDFName getStructureType() {
  114. return (PDFName)get("S");
  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. }