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

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