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.

KerningGlyphLayoutTestCase.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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.geom.Point2D;
  20. import java.text.CharacterIterator;
  21. import java.text.StringCharacterIterator;
  22. import org.junit.Before;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.mockito.ArgumentMatchers.anyInt;
  26. import static org.mockito.ArgumentMatchers.eq;
  27. import static org.mockito.Mockito.mock;
  28. import static org.mockito.Mockito.when;
  29. import org.apache.batik.gvt.font.GVTLineMetrics;
  30. import org.apache.fop.fonts.Font;
  31. import org.apache.fop.fonts.FontMetrics;
  32. public class KerningGlyphLayoutTestCase extends FOPGVTGlyphVectorTest {
  33. private final int fontSize = 37500;
  34. @Before
  35. public void createGlyphVector() {
  36. FontMetrics metrics = mockFontMetrics();
  37. Font font = mockFont(metrics);
  38. FOPGVTFont gvtFont = mockGVTFont(font);
  39. CharacterIterator it = new StringCharacterIterator("AVo", 1, 3, 1);
  40. glyphVector = new FOPGVTGlyphVector(gvtFont, it, null);
  41. glyphVector.performDefaultLayout();
  42. }
  43. private FontMetrics mockFontMetrics() {
  44. FontMetrics metrics = mock(FontMetrics.class);
  45. when(metrics.getWidth(eq(1), eq(fontSize))).thenReturn(25012000);
  46. when(metrics.getWidth(eq(2), eq(fontSize))).thenReturn(22912000);
  47. when(metrics.getWidth(eq(3), eq(fontSize))).thenReturn(20850000);
  48. return metrics;
  49. }
  50. private Font mockFont(FontMetrics metrics) {
  51. Font font = mock(Font.class);
  52. when(font.getFontMetrics()).thenReturn(metrics);
  53. when(font.getFontSize()).thenReturn(fontSize);
  54. when(font.mapChar(eq('A'))).thenReturn((char) 1);
  55. when(font.mapChar(eq('V'))).thenReturn((char) 2);
  56. when(font.mapChar(eq('o'))).thenReturn((char) 3);
  57. when(font.hasKerning()).thenReturn(true);
  58. when(font.getCharWidth('A')).thenReturn(25012);
  59. when(font.getCharWidth('V')).thenReturn(22912);
  60. when(font.getCharWidth('o')).thenReturn(20850);
  61. when(font.getCharWidth(65)).thenReturn(25012);
  62. when(font.getCharWidth(86)).thenReturn(22912);
  63. when(font.getCharWidth(111)).thenReturn(20850);
  64. when(font.getKernValue('V', 'o')).thenReturn(-2812);
  65. return font;
  66. }
  67. private FOPGVTFont mockGVTFont(Font font) {
  68. FOPGVTFont gvtFont = mock(FOPGVTFont.class);
  69. when(gvtFont.getFont()).thenReturn(font);
  70. when(gvtFont.getLineMetrics(anyInt())).thenReturn(
  71. new GVTLineMetrics(8, 0, null, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0));
  72. return gvtFont;
  73. }
  74. @Test
  75. public void testGlyphPositions() {
  76. assertEquals(new Point2D.Float(20.1f, 0), glyphVector.getGlyphPosition(1));
  77. assertEquals(new Point2D.Float(40.95f, 0), glyphVector.getGlyphPosition(2));
  78. }
  79. }