選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DoubleByteFont.java 3.5KB

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