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.

BasicGlyphVectorTestCase.java 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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.svg.font;
  19. import java.awt.Rectangle;
  20. import java.awt.font.GlyphMetrics;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Point2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.text.CharacterIterator;
  25. import java.text.StringCharacterIterator;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import static org.junit.Assert.assertArrayEquals;
  29. import static org.junit.Assert.assertEquals;
  30. import static org.mockito.ArgumentMatchers.anyInt;
  31. import static org.mockito.ArgumentMatchers.eq;
  32. import static org.mockito.Mockito.mock;
  33. import static org.mockito.Mockito.when;
  34. import org.apache.batik.gvt.font.GVTGlyphMetrics;
  35. import org.apache.batik.gvt.font.GVTLineMetrics;
  36. import org.apache.fop.fonts.Font;
  37. import org.apache.fop.fonts.FontMetrics;
  38. /**
  39. * Tests all the methods of {@link FOPGVTGlyphVector} with a mocked font.
  40. */
  41. public class BasicGlyphVectorTestCase extends FOPGVTGlyphVectorTest {
  42. private final int fontSize = 10000;
  43. @Before
  44. public void createGlyphVector() {
  45. FontMetrics metrics = mockFontMetrics();
  46. Font font = mockFont(metrics);
  47. FOPGVTFont gvtFont = mockGVTFont(font);
  48. CharacterIterator it = new StringCharacterIterator("ABC");
  49. glyphVector = new FOPGVTGlyphVector(gvtFont, it, null);
  50. glyphVector.performDefaultLayout();
  51. }
  52. private FontMetrics mockFontMetrics() {
  53. FontMetrics metrics = mock(FontMetrics.class);
  54. when(metrics.getAscender(eq(fontSize))).thenReturn(8000000);
  55. when(metrics.getDescender(eq(fontSize))).thenReturn(-4000000);
  56. when(metrics.getWidth(eq(1), eq(fontSize))).thenReturn(10000000);
  57. when(metrics.getBoundingBox(eq(1), eq(fontSize))).thenReturn(
  58. new Rectangle(-1000000, -2000000, 3000000, 4000000));
  59. when(metrics.getWidth(eq(2), eq(fontSize))).thenReturn(11000000);
  60. when(metrics.getBoundingBox(eq(2), eq(fontSize))).thenReturn(
  61. new Rectangle(-5000000, -6000000, 7000000, 9000000));
  62. when(metrics.getWidth(eq(3), eq(fontSize))).thenReturn(12000000);
  63. when(metrics.getBoundingBox(eq(3), eq(fontSize))).thenReturn(
  64. new Rectangle(-9000000, -10000000, 11000000, 14000000));
  65. return metrics;
  66. }
  67. private Font mockFont(FontMetrics metrics) {
  68. Font font = mock(Font.class);
  69. when(font.getFontMetrics()).thenReturn(metrics);
  70. when(font.getFontSize()).thenReturn(fontSize);
  71. when(font.mapChar(eq('A'))).thenReturn((char) 1);
  72. when(font.mapChar(eq('B'))).thenReturn((char) 2);
  73. when(font.mapChar(eq('C'))).thenReturn((char) 3);
  74. return font;
  75. }
  76. private FOPGVTFont mockGVTFont(Font font) {
  77. FOPGVTFont gvtFont = mock(FOPGVTFont.class);
  78. when(gvtFont.getFont()).thenReturn(font);
  79. when(gvtFont.getLineMetrics(anyInt())).thenReturn(
  80. new GVTLineMetrics(8, 0, null, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  81. return gvtFont;
  82. }
  83. @Test
  84. public void getGlyphCodeReturnsGlyphIndex() {
  85. assertEquals(1, glyphVector.getGlyphCode(0));
  86. assertEquals(2, glyphVector.getGlyphCode(1));
  87. assertEquals(3, glyphVector.getGlyphCode(2));
  88. }
  89. @Test
  90. public void testGetGlyphCodes() {
  91. assertArrayEquals(new int[] {1, 2, 3}, glyphVector.getGlyphCodes(0, 3, null));
  92. assertArrayEquals(new int[] {2, 3}, glyphVector.getGlyphCodes(1, 2, null));
  93. }
  94. @Test
  95. public void testGetGlyphMetrics() {
  96. assertGlyphMetricsEqual(new GVTGlyphMetrics(10, 12, new Rectangle(-1, -2, 3, 4), GlyphMetrics.STANDARD),
  97. glyphVector.getGlyphMetrics(0));
  98. assertGlyphMetricsEqual(new GVTGlyphMetrics(11, 12, new Rectangle(-5, -3, 7, 9), GlyphMetrics.STANDARD),
  99. glyphVector.getGlyphMetrics(1));
  100. assertGlyphMetricsEqual(new GVTGlyphMetrics(12, 12, new Rectangle(-9, -4, 11, 14), GlyphMetrics.STANDARD),
  101. glyphVector.getGlyphMetrics(2));
  102. }
  103. private void assertGlyphMetricsEqual(GVTGlyphMetrics expected, GVTGlyphMetrics actual) {
  104. assertEquals(expected.getHorizontalAdvance(), actual.getHorizontalAdvance(), 0);
  105. assertEquals(expected.getVerticalAdvance(), actual.getVerticalAdvance(), 0);
  106. assertEquals(expected.getBounds2D(), actual.getBounds2D());
  107. assertEquals(expected.getLSB(), actual.getLSB(), 0);
  108. assertEquals(expected.getRSB(), actual.getRSB(), 0);
  109. assertEquals(expected.getType(), actual.getType());
  110. assertEquals(expected.isCombining(), actual.isCombining());
  111. assertEquals(expected.isComponent(), actual.isComponent());
  112. assertEquals(expected.isLigature(), actual.isLigature());
  113. assertEquals(expected.isStandard(), actual.isStandard());
  114. assertEquals(expected.isWhitespace(), actual.isWhitespace());
  115. }
  116. @Test
  117. public void testGetGlyphPosition() {
  118. assertEquals(new Point2D.Float(0, 0), glyphVector.getGlyphPosition(0));
  119. assertEquals(new Point2D.Float(10, 0), glyphVector.getGlyphPosition(1));
  120. assertEquals(new Point2D.Float(21, 0), glyphVector.getGlyphPosition(2));
  121. assertEquals(new Point2D.Float(33, 0), glyphVector.getGlyphPosition(3));
  122. }
  123. @Test
  124. public void testGetGlyphPositions() {
  125. float[] expectedPositions = new float[] {0, 0, 10, 0, 21, 0, 33, 0};
  126. assertArrayEquals(expectedPositions, glyphVector.getGlyphPositions(0, 4, null), 0);
  127. assertArrayEquals(expectedPositions, glyphVector.getGlyphPositions(0, 4, new float[8]), 0);
  128. }
  129. @Test
  130. public void testGetGlyphOutline() {
  131. assertEquals(new Rectangle(-1, -2, 3, 4), glyphVector.getGlyphOutline(0).getBounds());
  132. assertEquals(new Rectangle(5, -3, 7, 9), glyphVector.getGlyphOutline(1).getBounds());
  133. assertEquals(new Rectangle(12, -4, 11, 14), glyphVector.getGlyphOutline(2).getBounds());
  134. }
  135. @Test
  136. public void testGetOutline() {
  137. assertEquals(new Rectangle(-1, -4, 24, 14), glyphVector.getOutline().getBounds());
  138. }
  139. @Test
  140. public void testGetLogicalBounds() {
  141. assertEquals(new Rectangle(0, -8, 33, 12), glyphVector.getLogicalBounds());
  142. }
  143. @Test
  144. public void testGetLogicalBoundsRotated() {
  145. for (int i = 0; i < 3; i++) {
  146. glyphVector.setGlyphTransform(i, new AffineTransform(0.7, 0.7, -0.7, 0.7, 0, 0));
  147. }
  148. Rectangle2D bounds = glyphVector.getLogicalBounds();
  149. assertEquals(new Rectangle2D.Float(-2.8f, -5.6f, 37.8f, 16.8f),
  150. new Rectangle2D.Float((float) bounds.getX(), (float) bounds.getY(),
  151. (float) bounds.getWidth(), (float) bounds.getHeight()));
  152. }
  153. @Test
  154. public void testGetBounds() {
  155. assertEquals(new Rectangle(-1, -4, 24, 14), glyphVector.getBounds2D(null));
  156. }
  157. @Test
  158. public void testGetGlyphVisualBounds() {
  159. assertEquals(new Rectangle(-1, -2, 3, 4), glyphVector.getGlyphVisualBounds(0).getBounds());
  160. assertEquals(new Rectangle(5, -3, 7, 9), glyphVector.getGlyphVisualBounds(1).getBounds());
  161. assertEquals(new Rectangle(12, -4, 11, 14), glyphVector.getGlyphVisualBounds(2).getBounds());
  162. }
  163. @Test
  164. public void testGetGlyphLogicalBounds() {
  165. assertEquals(new Rectangle(0, -8, 10, 12), glyphVector.getGlyphLogicalBounds(0).getBounds());
  166. assertEquals(new Rectangle(10, -8, 11, 12), glyphVector.getGlyphLogicalBounds(1).getBounds());
  167. assertEquals(new Rectangle(21, -8, 12, 12), glyphVector.getGlyphLogicalBounds(2).getBounds());
  168. }
  169. }