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.

FOPFontFamilyResolverTestCase.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.FontFormatException;
  20. import java.awt.GraphicsEnvironment;
  21. import java.awt.font.FontRenderContext;
  22. import java.awt.font.LineMetrics;
  23. import java.awt.image.BufferedImage;
  24. import java.io.File;
  25. import java.io.IOException;
  26. import java.util.Collections;
  27. import org.junit.Before;
  28. import org.junit.BeforeClass;
  29. import org.junit.Ignore;
  30. import org.junit.Test;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.junit.Assert.assertFalse;
  33. import static org.junit.Assert.assertNotNull;
  34. import static org.junit.Assert.assertNull;
  35. import static org.junit.Assert.assertTrue;
  36. import org.apache.batik.gvt.font.GVTFontFamily;
  37. import org.apache.batik.gvt.font.GVTLineMetrics;
  38. import org.apache.fop.fonts.FontInfo;
  39. public class FOPFontFamilyResolverTestCase {
  40. private static FontInfo fontInfo;
  41. private FOPFontFamilyResolver resolver;
  42. @BeforeClass
  43. public static void setUpFontInfo() {
  44. fontInfo = new FontInfoBuilder()
  45. .useDejaVuLGCSerif()
  46. .useDroidSansMono()
  47. .build();
  48. }
  49. @Before
  50. public void createFontFamilyResolver() {
  51. resolver = new FOPFontFamilyResolverImpl(fontInfo);
  52. }
  53. @Test
  54. public void testResolve() {
  55. assertNull(resolver.resolve("Unavailable"));
  56. assertNotNull(resolver.resolve(FontInfoBuilder.DEJAVU_LGC_SERIF));
  57. }
  58. @Test
  59. public void testGetFamilyThatCanDisplay() {
  60. GVTFontFamily family = resolver.getFamilyThatCanDisplay('\u0180');
  61. assertEquals(FontInfoBuilder.DEJAVU_LGC_SERIF, family.getFamilyName());
  62. family = resolver.getFamilyThatCanDisplay('\u02F3');
  63. assertEquals(FontInfoBuilder.DROID_SANS_MONO, family.getFamilyName());
  64. family = resolver.getFamilyThatCanDisplay('\u02DF');
  65. assertNull(family);
  66. }
  67. @Test
  68. public void testDeriveFont() {
  69. FOPGVTFontFamily family = resolver.resolve(FontInfoBuilder.DEJAVU_LGC_SERIF);
  70. FOPGVTFont font = family.deriveFont(10, Collections.emptyMap());
  71. assertEquals(10, font.getSize(), 0);
  72. assertTrue(font.canDisplay('\u01F6'));
  73. assertFalse(font.canDisplay('\u01F7'));
  74. }
  75. @Test
  76. @Ignore("FOP metrics don't match AWT, but not sure who is right and who is wrong")
  77. public void testLineMetrics() throws FontFormatException, IOException {
  78. FOPGVTFontFamily family = resolver.resolve(FontInfoBuilder.DEJAVU_LGC_SERIF);
  79. FOPGVTFont font = family.deriveFont(10, Collections.emptyMap());
  80. GVTLineMetrics fopMetrics = font.getLineMetrics("", null);
  81. LineMetrics awtMetrics = getAWTLineMetrics();
  82. printDifference("Ascent", awtMetrics.getAscent(), fopMetrics.getAscent());
  83. printDifference("Descent", awtMetrics.getDescent(), fopMetrics.getDescent());
  84. printDifference("Height", awtMetrics.getHeight(), fopMetrics.getHeight());
  85. printDifference("Leading", awtMetrics.getLeading(), fopMetrics.getLeading());
  86. printDifference("StrikethroughOffset", awtMetrics.getStrikethroughOffset(),
  87. fopMetrics.getStrikethroughOffset());
  88. printDifference("StrikethroughThickness", awtMetrics.getStrikethroughThickness(),
  89. fopMetrics.getStrikethroughThickness());
  90. printDifference("UnderlineOffset", awtMetrics.getUnderlineOffset(),
  91. fopMetrics.getUnderlineOffset());
  92. printDifference("UnderlineThickness", awtMetrics.getUnderlineThickness(),
  93. fopMetrics.getUnderlineThickness());
  94. }
  95. private LineMetrics getAWTLineMetrics() throws FontFormatException, IOException {
  96. File fontFile = new File("test/resources/fonts/ttf/DejaVuLGCSerif.ttf");
  97. java.awt.Font awtFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile).deriveFont(10f);
  98. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  99. BufferedImage dummyImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
  100. FontRenderContext frc = ge.createGraphics(dummyImage).getFontRenderContext();
  101. LineMetrics awtMetrics = awtFont.getLineMetrics("ABC", frc);
  102. return awtMetrics;
  103. }
  104. private void printDifference(String value, float awt, float fop) {
  105. System.out.println(String.format("%22s AWT: %10f FOP: %10f Difference: %.2f%%", value, awt, fop,
  106. (fop - awt) / awt * 100));
  107. }
  108. }