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.

IFGraphicContext.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.render.intermediate;
  19. import java.awt.Dimension;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.util.ArrayList;
  23. import org.apache.xmlgraphics.java2d.GraphicContext;
  24. /**
  25. * Specialized graphic context class for the intermediate format renderer.
  26. */
  27. public class IFGraphicContext extends GraphicContext {
  28. private static final AffineTransform[] EMPTY_TRANSFORM_ARRAY = new AffineTransform[0];
  29. private ArrayList groupList = new ArrayList();
  30. /**
  31. * Default constructor.
  32. */
  33. public IFGraphicContext() {
  34. super();
  35. }
  36. /**
  37. * Copy constructor.
  38. * @param graphicContext the graphic context to make a copy of
  39. */
  40. protected IFGraphicContext(IFGraphicContext graphicContext) {
  41. super(graphicContext);
  42. // N.B. do not perform deep copy on groupList; doing so causes
  43. // a junit regression... have not investigated cause... [GA]
  44. // groupList = (ArrayList) graphicContext.groupList.clone();
  45. }
  46. /**
  47. * {@inheritDoc}
  48. */
  49. public Object clone() {
  50. // FBOFF: CN_IDIOM_NO_SUPER_CALL
  51. return new IFGraphicContext(this);
  52. }
  53. /** @param group a group */
  54. public void pushGroup(Group group) {
  55. this.groupList.add(group);
  56. for (int i = 0, c = group.getTransforms().length; i < c; i++) {
  57. transform(group.getTransforms()[i]);
  58. }
  59. }
  60. /** @return array of groups */
  61. public Group[] getGroups() {
  62. return (Group[])this.groupList.toArray(new Group[getGroupStackSize()]);
  63. }
  64. /** @return array of groups after clearing group list */
  65. public Group[] dropGroups() {
  66. Group[] groups = getGroups();
  67. this.groupList.clear();
  68. return groups;
  69. }
  70. /** @return size of group list */
  71. public int getGroupStackSize() {
  72. return this.groupList.size();
  73. }
  74. /** a group */
  75. public static class Group {
  76. private AffineTransform[] transforms;
  77. private String layer;
  78. /**
  79. * Construct a Group.
  80. * @param transforms an array of transforms
  81. */
  82. public Group(AffineTransform[] transforms) {
  83. this.transforms = transforms;
  84. }
  85. /**
  86. * Construct a Group.
  87. * @param transform a transform
  88. */
  89. public Group(AffineTransform transform) {
  90. this(new AffineTransform[] {transform});
  91. }
  92. /**
  93. * Construct a layer Group, i.e., a Group with no transforms
  94. * but with a optional content group layer label.
  95. * @param layer a layer label
  96. */
  97. public Group(String layer) {
  98. this();
  99. this.layer = layer;
  100. }
  101. /** Default constructor. */
  102. public Group() {
  103. this(EMPTY_TRANSFORM_ARRAY);
  104. }
  105. /** @return array of transforms */
  106. public AffineTransform[] getTransforms() {
  107. return this.transforms;
  108. }
  109. /** @return layer */
  110. public String getLayer() {
  111. return this.layer;
  112. }
  113. /**
  114. * @param painter a painter
  115. * @throws IFException in not caught
  116. */
  117. public void start(IFPainter painter) throws IFException {
  118. painter.startGroup(transforms, layer);
  119. }
  120. /**
  121. * @param painter a painter
  122. * @throws IFException in not caught
  123. */
  124. public void end(IFPainter painter) throws IFException {
  125. painter.endGroup();
  126. }
  127. /** {@inheritDoc} */
  128. public String toString() {
  129. StringBuffer sb = new StringBuffer("group: ");
  130. IFUtil.toString(getTransforms(), sb);
  131. if ((layer != null) && (layer.length() > 0)) {
  132. sb.append(" layer(");
  133. sb.append(layer);
  134. sb.append(')');
  135. }
  136. return sb.toString();
  137. }
  138. }
  139. /** a viewport */
  140. public static class Viewport extends Group {
  141. private Dimension size;
  142. private Rectangle clipRect;
  143. /**
  144. * Construct a viewport.
  145. * @param transforms an array of transforms
  146. * @param size a dimension
  147. * @param clipRect a clip rectangle
  148. */
  149. public Viewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect) {
  150. super(transforms);
  151. this.size = size;
  152. this.clipRect = clipRect;
  153. }
  154. /**
  155. * Construct a viewport.
  156. * @param transform a transform
  157. * @param size a dimension
  158. * @param clipRect a clip rectangle
  159. */
  160. public Viewport(AffineTransform transform, Dimension size, Rectangle clipRect) {
  161. this(new AffineTransform[] {transform}, size, clipRect);
  162. }
  163. /** @return the viewport's size */
  164. public Dimension getSize() {
  165. return this.size;
  166. }
  167. /** @return the clip rectangle */
  168. public Rectangle getClipRect() {
  169. return this.clipRect;
  170. }
  171. /** {@inheritDoc} */
  172. public void start(IFPainter painter) throws IFException {
  173. painter.startViewport(getTransforms(), size, clipRect);
  174. }
  175. /** {@inheritDoc} */
  176. public void end(IFPainter painter) throws IFException {
  177. painter.endViewport();
  178. }
  179. /** {@inheritDoc} */
  180. public String toString() {
  181. StringBuffer sb = new StringBuffer("viewport: ");
  182. IFUtil.toString(getTransforms(), sb);
  183. sb.append(", ").append(getSize());
  184. if (getClipRect() != null) {
  185. sb.append(", ").append(getClipRect());
  186. }
  187. return sb.toString();
  188. }
  189. }
  190. }