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.

CharMetricsHandler.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.io.IOException;
  20. import java.util.Map;
  21. import java.util.Stack;
  22. import java.util.regex.Matcher;
  23. import java.util.regex.Pattern;
  24. import org.apache.commons.logging.Log;
  25. import org.apache.commons.logging.LogFactory;
  26. import org.apache.xmlgraphics.fonts.Glyphs;
  27. import org.apache.fop.fonts.NamedCharacter;
  28. import org.apache.fop.fonts.type1.AFMParser.ValueHandler;
  29. /**
  30. * A handler that parses the various types of character metrics in an AFM file.
  31. */
  32. abstract class CharMetricsHandler {
  33. private static final Log LOG = LogFactory.getLog(CharMetricsHandler.class);
  34. private static final String WHITE_SPACE = "\\s*";
  35. private static final String OPERATOR = "([A-Z0-9]{1,3})";
  36. private static final String OPERANDS = "(.*)";
  37. private static final Pattern METRICS_REGEX = Pattern.compile(
  38. WHITE_SPACE + OPERATOR + WHITE_SPACE + OPERANDS + WHITE_SPACE);
  39. private static final Pattern SPLIT_REGEX = Pattern.compile(WHITE_SPACE + ";" + WHITE_SPACE);
  40. private CharMetricsHandler() {
  41. }
  42. abstract AFMCharMetrics parse(String line, Stack<Object> stack, String afmFileName)
  43. throws IOException;
  44. static CharMetricsHandler getHandler(Map<String, ValueHandler> valueParsers,
  45. String line) {
  46. if (line != null && line.contains(AdobeStandardEncoding.NAME)) {
  47. return new AdobeStandardCharMetricsHandler(valueParsers);
  48. } else {
  49. return new DefaultCharMetricsHandler(valueParsers);
  50. }
  51. }
  52. private static final class DefaultCharMetricsHandler extends CharMetricsHandler {
  53. private final Map<String, ValueHandler> valueParsers;
  54. private DefaultCharMetricsHandler(Map<String, ValueHandler> valueParsers) {
  55. this.valueParsers = valueParsers;
  56. }
  57. AFMCharMetrics parse(String line, Stack<Object> stack, String afmFileName)
  58. throws IOException {
  59. AFMCharMetrics chm = new AFMCharMetrics();
  60. stack.push(chm);
  61. String[] metrics = SPLIT_REGEX.split(line);
  62. for (String metric : metrics) {
  63. Matcher matcher = METRICS_REGEX.matcher(metric);
  64. if (matcher.matches()) {
  65. String operator = matcher.group(1);
  66. String operands = matcher.group(2);
  67. ValueHandler handler = valueParsers.get(operator);
  68. if (handler != null) {
  69. handler.parse(operands, 0, stack);
  70. }
  71. }
  72. }
  73. stack.pop();
  74. return chm;
  75. }
  76. }
  77. private static final class AdobeStandardCharMetricsHandler extends CharMetricsHandler {
  78. private final DefaultCharMetricsHandler defaultHandler;
  79. private AdobeStandardCharMetricsHandler(Map<String, ValueHandler> valueParsers) {
  80. defaultHandler = new DefaultCharMetricsHandler(valueParsers);
  81. }
  82. AFMCharMetrics parse(String line, Stack<Object> stack, String afmFileName)
  83. throws IOException {
  84. AFMCharMetrics chm = defaultHandler.parse(line, stack, afmFileName);
  85. NamedCharacter namedChar = chm.getCharacter();
  86. if (namedChar != null) {
  87. String charName = namedChar.getName();
  88. int codePoint = AdobeStandardEncoding.getAdobeCodePoint(charName);
  89. if (chm.getCharCode() != codePoint && !Glyphs.NOTDEF.equals(charName)) {
  90. LOG.info(afmFileName + ": named character '" + charName + "'"
  91. + " has an incorrect code point: " + chm.getCharCode()
  92. + ". Changed to " + codePoint);
  93. chm.setCharCode(codePoint);
  94. }
  95. }
  96. return chm;
  97. }
  98. }
  99. }