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.

AFMParserTestCase.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.fonts.type1;
  19. import java.awt.Rectangle;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.util.List;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import static org.junit.Assert.assertTrue;
  26. /**
  27. * Test case for {@link AFMParser}.
  28. */
  29. public class AFMParserTestCase {
  30. private AFMParser sut = new AFMParser();
  31. /**
  32. * We're testing with two identical files except one has:
  33. * EncodingScheme AdobeStandardEncoding
  34. * the other has:
  35. * EncodingScheme ExpectedEncoding
  36. * Both files have the correct character metrics data, and we're checking that both are handled
  37. * consistently with both encoding settings.
  38. *
  39. * @throws IOException if an I/O error occurs
  40. */
  41. @Test
  42. public void testMappingAgainstAdobeStandardEncoding() throws IOException {
  43. InputStream expectedStream = getClass().getResourceAsStream(
  44. "adobe-charset_unknown-encoding.afm");
  45. InputStream adobeStandardStream = getClass().getResourceAsStream(
  46. "adobe-charset_adobe-encoding.afm");
  47. AFMFile expectedParser = sut.parse(expectedStream, null);
  48. AFMFile adobeStandard = sut.parse(adobeStandardStream, null);
  49. List<AFMCharMetrics> adobeMetrics = adobeStandard.getCharMetrics();
  50. checkCharMtrxList(true, expectedParser.getCharMetrics(), adobeMetrics);
  51. compareMetrics(adobeMetrics);
  52. nonAdobeCharsetUnknownEncoding(adobeMetrics);
  53. nonAdobeCharsetAdobeEncoding(adobeMetrics);
  54. }
  55. private void compareMetrics(List<AFMCharMetrics> charMetrics) {
  56. // in order to ensure that every character is parsed properly, we're going to check them
  57. // against the AFM file (bboxes were created with a counter)
  58. AdobeStandardEncoding[] standardEncoding = AdobeStandardEncoding.values();
  59. for (int i = 0; i < charMetrics.size(); i++) {
  60. Rectangle expectedBbox = new Rectangle(i + 1, i + 1, 0, 0);
  61. AFMCharMetrics thisMetric = charMetrics.get(i);
  62. assertTrue(thisMetric.getBBox().equals(expectedBbox));
  63. assertEquals(thisMetric.getCharName(), standardEncoding[i].getAdobeName());
  64. }
  65. }
  66. /**
  67. * A non-adobe encoded file is tested, all the character codes are not AdobeStandardEncoding and
  68. * the encoding is not AdobeStandardEncoding, we are checking a failure case here. Checking that
  69. * the AdobeStandardEncoding isn't forced on other encodings.
  70. *
  71. * @param expected the AdobeStandardEncoding encoded character metrics list
  72. * @throws IOException if an IO error occurs
  73. */
  74. private void nonAdobeCharsetUnknownEncoding(List<AFMCharMetrics> expected)
  75. throws IOException {
  76. InputStream inStream = getClass().getResourceAsStream(
  77. "notadobe-charset_unknown-encoding.afm");
  78. AFMFile afmFile = sut.parse(inStream, null);
  79. List<AFMCharMetrics> unknownEncodingMetrics = afmFile.getCharMetrics();
  80. checkCharMtrxList(false, expected, unknownEncodingMetrics);
  81. }
  82. /**
  83. * This tests a poorly encoded file, it has AdobeStandardEncoding. We are checking that the
  84. * metrics are correctly analysed against properly encoded char metrics.
  85. *
  86. * @param expected
  87. * @throws IOException
  88. */
  89. private void nonAdobeCharsetAdobeEncoding(List<AFMCharMetrics> expected)
  90. throws IOException {
  91. InputStream inStream = getClass().getResourceAsStream(
  92. "notadobe-charset_adobe-encoding.afm");
  93. AFMFile afmFile = sut.parse(inStream, null);
  94. List<AFMCharMetrics> correctedCharMetrics = afmFile.getCharMetrics();
  95. checkCharMtrxList(true, expected, correctedCharMetrics);
  96. }
  97. private boolean charMetricsEqual(AFMCharMetrics o1, AFMCharMetrics o2) {
  98. return o1.getCharCode() == o2.getCharCode()
  99. && objectEquals(o1.getCharacter(), o2.getCharacter())
  100. && o1.getWidthX() == o2.getWidthX()
  101. && o1.getWidthY() == o2.getWidthY()
  102. && objectEquals(o1.getBBox(), o2.getBBox());
  103. }
  104. private void checkCharMtrxList(boolean expectedResult, List<AFMCharMetrics> expectedList,
  105. List<AFMCharMetrics> actualList) {
  106. assertEquals(expectedList.size(), actualList.size());
  107. for (int i = 0; i < expectedList.size(); i++) {
  108. assertEquals(expectedResult, charMetricsEqual(expectedList.get(i), actualList.get(i)));
  109. }
  110. }
  111. private boolean objectEquals(Object o1, Object o2) {
  112. return o1 == null ? o2 == null : (o1 == o2 || o1.equals(o2));
  113. }
  114. }