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 5.4KB

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