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.

PDFGraphicsPainterTestCase.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.pdf;
  19. import java.io.IOException;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. import static org.mockito.Matchers.any;
  23. import static org.mockito.Matchers.endsWith;
  24. import static org.mockito.Mockito.mock;
  25. import static org.mockito.Mockito.never;
  26. import static org.mockito.Mockito.verify;
  27. import org.apache.fop.fo.Constants;
  28. import org.apache.fop.pdf.PDFNumber;
  29. public class PDFGraphicsPainterTestCase {
  30. private PDFGraphicsPainter sut;
  31. private PDFContentGenerator generator;
  32. @Before
  33. public void setup() {
  34. generator = mock(PDFContentGenerator.class);
  35. sut = new PDFGraphicsPainter(generator);
  36. }
  37. @Test
  38. public void moveTo() {
  39. int x = 10;
  40. int y = 20;
  41. sut.moveTo(x, y);
  42. verify(generator).add(op("m", x, y));
  43. }
  44. @Test
  45. public void lineTo() {
  46. int x = 10;
  47. int y = 20;
  48. sut.lineTo(x, y);
  49. verify(generator).add(op("l", x, y));
  50. }
  51. @Test
  52. public void arcTo() throws IOException {
  53. int width = 10;
  54. int height = 10;
  55. int x = 0;
  56. int y = 0;
  57. double startAngle = 0;
  58. double endAngle = Math.PI / 2;
  59. sut.arcTo(startAngle, endAngle, x, y, width, height);
  60. //TODO stricter verification
  61. verify(generator).add(endsWith(" c "));
  62. }
  63. @Test
  64. public void closePath() {
  65. sut.closePath();
  66. verify(generator).add(op("h"));
  67. }
  68. @Test
  69. public void clip() {
  70. sut.clip();
  71. verify(generator).add(opln("W\nn"));
  72. }
  73. @Test
  74. public void saveGraphicsState() {
  75. sut.saveGraphicsState();
  76. verify(generator).add(opln("q"));
  77. }
  78. @Test
  79. public void restoreGraphicsState() {
  80. sut.restoreGraphicsState();
  81. verify(generator).add(opln("Q"));
  82. }
  83. @Test
  84. public void rotateCoordinates() throws IOException {
  85. double angle = 0;
  86. float s = (float) Math.sin(angle);
  87. float c = (float) Math.cos(angle);
  88. sut.rotateCoordinates(angle);
  89. testTransformCoordinatesF(c, s, -s, c, 0, 0);
  90. }
  91. @Test
  92. public void translateCoordinates() throws IOException {
  93. int x = 10;
  94. int y = 20;
  95. sut.translateCoordinates(x, y);
  96. testTransformCoordinates(1000, 0, 0, 1000, x, y);
  97. }
  98. @Test
  99. public void scaleCoordinates() throws IOException {
  100. float xScaleFactor = 10f;
  101. float yScaleFactor = 2f;
  102. sut.scaleCoordinates(xScaleFactor, yScaleFactor);
  103. testTransformCoordinatesF(xScaleFactor, 0f, 0f, yScaleFactor, 0f, 0f);
  104. }
  105. @Test
  106. public void cubicBezierTo() {
  107. int[] args = new int[]{1, 2, 3, 4, 5, 6};
  108. sut.cubicBezierTo(args[0], args[1], args[2], args[3], args[4], args[5]);
  109. verify(generator).add(op("c", args));
  110. }
  111. @Test
  112. public void testDrawBorderLineDashed() {
  113. sut.drawBorderLine(0, 0, 0, 0, true, true, Constants.EN_DASHED, null);
  114. verify(generator, never()).add(any(String.class));
  115. }
  116. private void testTransformCoordinatesF(float... args) {
  117. verify(generator).add(opf("cm", args));
  118. }
  119. private void testTransformCoordinates(int... args) {
  120. verify(generator).add(op("cm", args));
  121. }
  122. private String opf(String op, float... args) {
  123. return opf(op, " ", args);
  124. }
  125. private String op(String op, int... args) {
  126. return op(op, " ", args);
  127. }
  128. private String opln(String op, int... args) {
  129. return op(op, "\n", args);
  130. }
  131. private String opf(String op, String ending, float... args) {
  132. StringBuilder sb = new StringBuilder();
  133. for (float arg : args) {
  134. sb.append("" + PDFNumber.doubleOut(arg) + " ");
  135. }
  136. return sb.append(op.trim()).append(ending).toString();
  137. }
  138. private String op(String op, String ending, int... args) {
  139. float[] formattedArgs = new float[args.length];
  140. for (int i = 0; i < args.length; i++) {
  141. formattedArgs[i] = format(args[i]);
  142. }
  143. return opf(op, ending, formattedArgs);
  144. }
  145. private float format(int i) {
  146. return (float) i / 1000;
  147. }
  148. }