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

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.util.Map;
  20. import java.util.Set;
  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, Integer.valueOf(0));
  86. vals.put(LJ, Integer.valueOf(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, Integer.valueOf(1));
  93. vals.put(Font, "");*/
  94. vals.put(GSTATE_ALPHA_STROKE, 1.0f);
  95. vals.put(GSTATE_ALPHA_NONSTROKE, 1.0f);
  96. }
  97. private Map values = new java.util.HashMap();
  98. private int objNum;
  99. /**
  100. * Returns the name of this object
  101. * @return the name
  102. */
  103. public String getName() {
  104. if (objNum == 0) {
  105. objNum = ++getDocument().gStateObjectCount;
  106. }
  107. return "GS" + objNum;
  108. }
  109. /**
  110. * Sets the alpha value.
  111. * @param val alpha value (0.0 - 1.0)
  112. * @param fill True if alpha should be set for non-stroking operations,
  113. * False if for stroking operations
  114. */
  115. public void setAlpha(float val, boolean fill) {
  116. if (fill) {
  117. values.put(GSTATE_ALPHA_NONSTROKE, val);
  118. } else {
  119. values.put(GSTATE_ALPHA_STROKE, val);
  120. }
  121. }
  122. /**
  123. * Adds all values from another GState object to this one.
  124. * @param state source object to copy from
  125. */
  126. public void addValues(PDFGState state) {
  127. values.putAll(state.values);
  128. }
  129. /**
  130. * Adds all values from a Map to this object.
  131. * @param vals source object to copy from
  132. */
  133. public void addValues(Map vals) {
  134. values.putAll(vals);
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public String toPDFString() {
  140. StringBuffer sb = new StringBuffer(64);
  141. sb.append("<<\n/Type /ExtGState\n");
  142. appendVal(sb, GSTATE_ALPHA_NONSTROKE);
  143. appendVal(sb, GSTATE_ALPHA_STROKE);
  144. sb.append(">>");
  145. return sb.toString();
  146. }
  147. private void appendVal(StringBuffer sb, String name) {
  148. Object val = values.get(name);
  149. if (val != null) {
  150. sb.append("/" + name + " " + val + "\n");
  151. }
  152. }
  153. /*
  154. * example
  155. * 29 0 obj
  156. * <<
  157. * /Type /ExtGState
  158. * /ca 0.5
  159. * >>
  160. * endobj
  161. */
  162. /** {@inheritDoc} */
  163. protected boolean contentEquals(PDFObject obj) {
  164. if (obj == this) {
  165. return true;
  166. }
  167. if (!(obj instanceof PDFGState)) {
  168. return false;
  169. }
  170. Map vals1 = values;
  171. Map vals2 = ((PDFGState)obj).values;
  172. if (vals1.size() != vals2.size()) {
  173. return false;
  174. }
  175. for (Map.Entry<Object, Object> e : (Set<Map.Entry<Object, Object>>) vals2.entrySet()) {
  176. Object str = e.getKey();
  177. Object obj1 = vals1.get(str);
  178. if (!obj1.equals(e.getValue())) {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. }