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.

GlyphLayoutTestCase.java 3.1KB

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.util.Collections;
  20. import org.junit.Test;
  21. import static org.junit.Assert.assertEquals;
  22. import org.apache.fop.fonts.FontInfo;
  23. /**
  24. * Specifically tests glyph positioning from a real font.
  25. */
  26. public class GlyphLayoutTestCase extends FOPGVTGlyphVectorTest {
  27. /**
  28. * Glyph positioning using the legacy kern table.
  29. */
  30. @Test
  31. public void testBasicGlyphPositioning() throws Exception {
  32. testGlyphLayout(false);
  33. }
  34. /**
  35. * Glyph positioning using GPOS sub-tables.
  36. */
  37. @Test
  38. public void testAdvancedGlyphPositioning() throws Exception {
  39. testGlyphLayout(true);
  40. }
  41. private void testGlyphLayout(boolean useAdvanced) {
  42. FOPGVTFont font = loadFont(useAdvanced);
  43. glyphVector = (FOPGVTGlyphVector) font.createGlyphVector(null, "L\u201DP,V.F,A\u2019LT.", "DFLT", "dflt");
  44. glyphVector.performDefaultLayout();
  45. // Values in font units (unitsPerEm = 2048), glyph width - kern
  46. int[] widths = {
  47. /* L */ 1360 - 491,
  48. /* " */ 1047,
  49. /* P */ 1378 - 415,
  50. /* , */ 651,
  51. /* V */ 1479 - 358,
  52. /* . */ 651,
  53. /* F */ 1421 - 319,
  54. /* , */ 651,
  55. /* A */ 1479 - 301,
  56. /* ' */ 651,
  57. /* L */ 1360 - 167,
  58. /* T */ 1366 - 301,
  59. /* . */ 651};
  60. checkGlyphPositions(13, widths);
  61. }
  62. private FOPGVTFont loadFont(boolean useAdvanced) {
  63. FontInfo fontInfo = new FontInfoBuilder().useDejaVuLGCSerif(useAdvanced).build();
  64. FOPFontFamilyResolver resolver = new FOPFontFamilyResolverImpl(fontInfo);
  65. FOPGVTFontFamily family = resolver.resolve(FontInfoBuilder.DEJAVU_LGC_SERIF);
  66. return family.deriveFont(1000, Collections.emptyMap());
  67. }
  68. private void checkGlyphPositions(int expectedGlyphCount, int[] widths) {
  69. assertEquals(expectedGlyphCount, glyphVector.getNumGlyphs());
  70. float[] positions = new float[2 * (widths.length + 1)];
  71. for (int i = 0, n = 2; i < widths.length; i++, n += 2) {
  72. positions[n] = positions[n - 2] + widths[i] / 2.048f;
  73. }
  74. for (int i = 0; i <= widths.length; i++) {
  75. assertEquals(positions[2 * i], glyphVector.getGlyphPosition(i).getX(), 3);
  76. }
  77. }
  78. }