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.

DoubleByteFont.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.afp.fonts;
  19. import java.lang.Character.UnicodeBlock;
  20. import java.util.HashSet;
  21. import java.util.Set;
  22. import org.apache.commons.logging.Log;
  23. import org.apache.commons.logging.LogFactory;
  24. import org.apache.fop.afp.AFPEventProducer;
  25. /**
  26. * Implementation of AbstractOutlineFont that supports double-byte fonts (CID Keyed font (Type 0)).
  27. * The width of characters that are not prescribed a width metrics in the font resource use
  28. * a fallback width. The default width is 1 em. A character can be supplied and queried for the
  29. * fallback width of all non-ideograph characters.<p />
  30. */
  31. public class DoubleByteFont extends AbstractOutlineFont {
  32. private static final Log log = LogFactory.getLog(DoubleByteFont.class);
  33. private final Set<Integer> charsProcessed;
  34. //See also http://unicode.org/reports/tr11/ which we've not closely looked at, yet
  35. //TODO the Unicode block listed here is probably not complete (ex. Hiragana, Katakana etc.)
  36. private static final Set<UnicodeBlock> IDEOGRAPHIC = new HashSet<UnicodeBlock>();
  37. static {
  38. IDEOGRAPHIC.add(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS);
  39. //IDEOGRAPHIC.add(Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT);//Java 1.5
  40. IDEOGRAPHIC.add(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS);
  41. IDEOGRAPHIC.add(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A);
  42. //IDEOGRAPHIC.add(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B); //Java 1.1
  43. }
  44. /**
  45. * Constructor for an double-byte outline font.
  46. * @param name the name of the font
  47. * @param embeddable whether or not this font is embeddable
  48. * @param charSet the character set
  49. * @param eventProducer Handles any AFP related events
  50. */
  51. public DoubleByteFont(String name, boolean embeddable, CharacterSet charSet,
  52. AFPEventProducer eventProducer) {
  53. super(name, embeddable, charSet, eventProducer);
  54. charsProcessed = new HashSet<Integer>();
  55. }
  56. /** {@inheritDoc} */
  57. public int getWidth(int character, int size) {
  58. int charWidth;
  59. try {
  60. charWidth = charSet.getWidth(toUnicodeCodepoint(character));
  61. } catch (IllegalArgumentException e) {
  62. if (!charsProcessed.contains(character)) {
  63. charsProcessed.add(character);
  64. getAFPEventProducer().charactersetMissingMetrics(this, (char)character,
  65. charSet.getName().trim());
  66. }
  67. // We shall try and handle characters that have no mapped width metric in font resource
  68. charWidth = -1;
  69. }
  70. if (charWidth == -1) {
  71. charWidth = getDefaultCharacterWidth(character);
  72. }
  73. return charWidth * size;
  74. }
  75. private int getDefaultCharacterWidth(int character) {
  76. int nominalCharIncrement = charSet.getNominalCharIncrement();
  77. if (nominalCharIncrement > 0) {
  78. return nominalCharIncrement;
  79. } else {
  80. return inferCharWidth(character);
  81. }
  82. }
  83. private int inferCharWidth(int character) {
  84. //Is this character an ideograph?
  85. boolean isIdeographic = false;
  86. Character.UnicodeBlock charBlock = Character.UnicodeBlock.of((char)character);
  87. if (charBlock == null) {
  88. isIdeographic = false;
  89. } else if (IDEOGRAPHIC.contains(charBlock)) {
  90. isIdeographic = true;
  91. } else { //default
  92. isIdeographic = false;
  93. }
  94. if (isIdeographic) {
  95. return charSet.getEmSpaceIncrement();
  96. } else {
  97. return charSet.getSpaceIncrement();
  98. }
  99. }
  100. }