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

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