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.

PDFGState.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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.Iterator;
  20. import java.util.Map;
  21. /**
  22. * Class representing a /ExtGState object.
  23. */
  24. public class PDFGState extends PDFObject {
  25. /** Line width (LW) */
  26. public static final String GSTATE_LINE_WIDTH = "LW";
  27. /** Line cap (LC) */
  28. public static final String GSTATE_LINE_CAP = "LC";
  29. /** Line join (LJ) */
  30. public static final String GSTATE_LINE_JOIN = "LJ";
  31. /** Miter limit (ML) */
  32. public static final String GSTATE_MITER_LIMIT = "ML";
  33. /** Dash pattern (D) */
  34. public static final String GSTATE_DASH_PATTERN = "D";
  35. /** Rendering intent (RI) */
  36. public static final String GSTATE_RENDERING_INTENT = "RI";
  37. /** Overprint for stroke (OP) */
  38. public static final String GSTATE_OVERPRINT_STROKE = "OP";
  39. /** Overprint for fill (op) */
  40. public static final String GSTATE_OVERPRINT_FILL = "op";
  41. /** Overprint mode (OPM) */
  42. public static final String GSTATE_OVERPRINT_MODE = "OPM";
  43. /** Font (Font) */
  44. public static final String GSTATE_FONT = "Font";
  45. /** Black generation (BG) */
  46. public static final String GSTATE_BLACK_GENERATION = "BG";
  47. /** Black generation with default (BG2) */
  48. public static final String GSTATE_BLACK_GENERATION2 = "BG2";
  49. /** Undercolor removal function (UCR) */
  50. public static final String GSTATE_UNDERCOLOR_REMOVAL = "UCR";
  51. /** Undercolor removal function with default (UCR2) */
  52. public static final String GSTATE_UNDERCOLOR_REMOVAL2 = "UCR2";
  53. /** Transfer function (TR) */
  54. public static final String GSTATE_TRANSFER_FUNCTION = "TR";
  55. /** Transfer function with default (TR2) */
  56. public static final String GSTATE_TRANSFER_FUNCTION2 = "TR2";
  57. /** Halftone dictionary or stream (HT) */
  58. public static final String GSTATE_HALFTONE_DICT = "HT";
  59. /** Halftone phase (HTP, does not show up anymore in PDF 1.4)*/
  60. public static final String GSTATE_HALFTONE_PHASE = "HTP";
  61. /** Flatness (FL) */
  62. public static final String GSTATE_FLATNESS = "FL";
  63. /** Smoothness (SM) */
  64. public static final String GSTATE_SMOOTHNESS = "SM";
  65. /** Strike adjustment (SA) */
  66. public static final String GSTATE_STRIKE_ADJ = "SA";
  67. /** Blend mode (BM, PDF 1.4) */
  68. public static final String GSTATE_BLEND_MODE = "BM";
  69. /** Soft mask (SMask, PDF 1.4) */
  70. public static final String GSTATE_SOFT_MASK = "SMask";
  71. /** Stroking Alpha (CA, PDF 1.4) */
  72. public static final String GSTATE_ALPHA_STROKE = "CA";
  73. /** Nonstroking Alpha (ca, PDF 1.4) */
  74. public static final String GSTATE_ALPHA_NONSTROKE = "ca";
  75. /** Alpha Source Flag (AIS, PDF 1.4) */
  76. public static final String GSTATE_ALPHA_SOURCE_FLAG = "AIS";
  77. /** Text Knockout Flag (TK, PDF 1.4) */
  78. public static final String GSTATE_TEXT_KNOCKOUT = "TK";
  79. /** Default GState object */
  80. public static final PDFGState DEFAULT;
  81. static {
  82. DEFAULT = new PDFGState();
  83. Map vals = DEFAULT.values;
  84. /*vals.put(LW, new Float(1.0));
  85. vals.put(LC, new Integer(0));
  86. vals.put(LJ, new Integer(0));
  87. vals.put(ML, new Float(10.0));
  88. vals.put(D, "0 []");
  89. vals.put(RI, "RelativeColorimetric");
  90. vals.put(OP, Boolean.FALSE);
  91. vals.put(op, Boolean.FALSE);
  92. vals.put(OPM, new Integer(1));
  93. vals.put(Font, "");*/
  94. vals.put(GSTATE_ALPHA_STROKE, new Float(1.0));
  95. vals.put(GSTATE_ALPHA_NONSTROKE, new Float(1.0));
  96. }
  97. private Map values = new java.util.HashMap();
  98. /**
  99. * Returns the name of this object
  100. * @return the name
  101. */
  102. public String getName() {
  103. return "GS" + getObjectNumber();
  104. }
  105. /**
  106. * Sets the alpha value.
  107. * @param val alpha value (0.0 - 1.0)
  108. * @param fill True if alpha should be set for non-stroking operations,
  109. * False if for stroking operations
  110. */
  111. public void setAlpha(float val, boolean fill) {
  112. if (fill) {
  113. values.put(GSTATE_ALPHA_NONSTROKE, new Float(val));
  114. } else {
  115. values.put(GSTATE_ALPHA_STROKE, new Float(val));
  116. }
  117. }
  118. /**
  119. * Adds all values from another GState object to this one.
  120. * @param state source object to copy from
  121. */
  122. public void addValues(PDFGState state) {
  123. values.putAll(state.values);
  124. }
  125. /**
  126. * Adds all values from a Map to this object.
  127. * @param vals source object to copy from
  128. */
  129. public void addValues(Map vals) {
  130. values.putAll(vals);
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public String toPDFString() {
  136. StringBuffer sb = new StringBuffer(64);
  137. sb.append("<<\n/Type /ExtGState\n");
  138. appendVal(sb, GSTATE_ALPHA_NONSTROKE);
  139. appendVal(sb, GSTATE_ALPHA_STROKE);
  140. sb.append(">>");
  141. return sb.toString();
  142. }
  143. private void appendVal(StringBuffer sb, String name) {
  144. Object val = values.get(name);
  145. if (val != null) {
  146. sb.append("/" + name + " " + val + "\n");
  147. }
  148. }
  149. /*
  150. * example
  151. * 29 0 obj
  152. * <<
  153. * /Type /ExtGState
  154. * /ca 0.5
  155. * >>
  156. * endobj
  157. */
  158. /** {@inheritDoc} */
  159. protected boolean contentEquals(PDFObject obj) {
  160. if (obj == this) {
  161. return true;
  162. }
  163. if (!(obj instanceof PDFGState)) {
  164. return false;
  165. }
  166. Map vals1 = values;
  167. Map vals2 = ((PDFGState)obj).values;
  168. if (vals1.size() != vals2.size()) {
  169. return false;
  170. }
  171. for (Iterator iter = vals1.keySet().iterator(); iter.hasNext();) {
  172. Object str = iter.next();
  173. Object obj1 = vals1.get(str);
  174. if (!obj1.equals(vals2.get(str))) {
  175. return false;
  176. }
  177. }
  178. return true;
  179. }
  180. }