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.

PDFEncoding.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // Java
  20. import java.util.Collections;
  21. import java.util.Set;
  22. /**
  23. * Class representing an /Encoding object.
  24. *
  25. * A small object expressing the base encoding name and
  26. * the differences from the base encoding.
  27. *
  28. * The three base encodings are given by their name.
  29. *
  30. * Encodings are specified in section 5.5.5 of the PDF 1.4 spec.
  31. */
  32. public class PDFEncoding extends PDFDictionary {
  33. /** the name for the standard encoding scheme */
  34. public static final String STANDARD_ENCODING = "StandardEncoding";
  35. /** the name for the Mac Roman encoding scheme */
  36. public static final String MAC_ROMAN_ENCODING = "MacRomanEncoding";
  37. /** the name for the Mac Export encoding scheme */
  38. public static final String MAC_EXPERT_ENCODING = "MacExpertEncoding";
  39. /** the name for the WinAnsi encoding scheme */
  40. public static final String WIN_ANSI_ENCODING = "WinAnsiEncoding";
  41. /** the name for the PDF document encoding scheme */
  42. public static final String PDF_DOC_ENCODING = "PDFDocEncoding";
  43. /** the set of predefined encodings that can be assumed present in a PDF viewer */
  44. private static final Set PREDEFINED_ENCODINGS;
  45. static {
  46. Set encodings = new java.util.HashSet();
  47. encodings.add(STANDARD_ENCODING);
  48. encodings.add(MAC_ROMAN_ENCODING);
  49. encodings.add(MAC_EXPERT_ENCODING);
  50. encodings.add(WIN_ANSI_ENCODING);
  51. encodings.add(PDF_DOC_ENCODING);
  52. PREDEFINED_ENCODINGS = Collections.unmodifiableSet(encodings);
  53. }
  54. /**
  55. * Create a new /Encoding object.
  56. *
  57. * @param basename the name of the character encoding schema
  58. */
  59. public PDFEncoding(String basename) {
  60. super();
  61. put("Type", new PDFName("Encoding"));
  62. if (basename != null) {
  63. put("BaseEncoding", new PDFName(basename));
  64. }
  65. }
  66. /**
  67. * Indicates whether a given encoding is one of the predefined encodings.
  68. * @param name the encoding name (ex. "StandardEncoding")
  69. * @return true if it is a predefined encoding
  70. */
  71. public static boolean isPredefinedEncoding(String name) {
  72. return PREDEFINED_ENCODINGS.contains(name);
  73. }
  74. /**
  75. * Creates and returns a new DifferencesBuilder instance for constructing the Differences
  76. * array.
  77. * @return the DifferencesBuilder
  78. */
  79. public DifferencesBuilder createDifferencesBuilder() {
  80. return new DifferencesBuilder();
  81. }
  82. /**
  83. * Sets the Differences value.
  84. * @param differences the differences.
  85. */
  86. public void setDifferences(PDFArray differences) {
  87. put("Differences", differences);
  88. }
  89. /**
  90. * Builder class for constructing the Differences array.
  91. */
  92. public class DifferencesBuilder {
  93. private PDFArray differences = new PDFArray();
  94. private int currentCode = -1;
  95. /**
  96. * Start a new difference.
  97. * @param code the starting code index inside the encoding
  98. * @return this builder instance
  99. */
  100. public DifferencesBuilder addDifference(int code) {
  101. this.currentCode = code;
  102. this.differences.add(new Integer(code));
  103. return this;
  104. }
  105. /**
  106. * Adds a character name to the current difference.
  107. * @param name the character name
  108. * @return this builder instance
  109. */
  110. public DifferencesBuilder addName(String name) {
  111. if (this.currentCode < 0) {
  112. throw new IllegalStateException("addDifference(int) must be called first");
  113. }
  114. this.differences.add(new PDFName(name));
  115. return this;
  116. }
  117. /**
  118. * Creates and returns the PDFArray representing the Differences entry.
  119. * @return the Differences entry
  120. */
  121. public PDFArray toPDFArray() {
  122. return this.differences;
  123. }
  124. }
  125. /*
  126. * example (p. 214)
  127. * 25 0 obj
  128. * <<
  129. * /Type /Encoding
  130. * /Differences [39 /quotesingle 96 /grave 128
  131. * /Adieresis /Aring /Ccedilla /Eacute /Ntilde
  132. * /Odieresis /Udieresis /aacute /agrave]
  133. * >>
  134. * endobj
  135. */
  136. }