Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

CharMetricsHandler.java 4.4KB

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