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

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