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.

FontSelector.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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;
  19. import org.apache.fop.datatypes.PercentBaseContext;
  20. import org.apache.fop.fo.FONode;
  21. import org.apache.fop.fo.FOText;
  22. import org.apache.fop.fo.flow.Character;
  23. import org.apache.fop.fo.properties.CommonFont;
  24. import org.apache.fop.util.CharUtilities;
  25. /**
  26. * Helper class for automatic font selection.
  27. * <p>
  28. * TODO: Check if this could be merged with another font class, such as
  29. * {@link FontManager}.
  30. */
  31. public final class FontSelector {
  32. private FontSelector() {
  33. // Static since this is an utility class.
  34. }
  35. private static Font selectFontForCharacter(char c, FONode fonode,
  36. CommonFont commonFont, PercentBaseContext context) {
  37. FontInfo fi = fonode.getFOEventHandler().getFontInfo();
  38. FontTriplet[] fontkeys = commonFont.getFontState(fi);
  39. for (FontTriplet fontkey : fontkeys) {
  40. Font font = fi.getFontInstance(fontkey, commonFont.fontSize
  41. .getValue(context));
  42. if (font.hasChar(c)) {
  43. return font;
  44. }
  45. }
  46. return fi.getFontInstance(fontkeys[0], commonFont.fontSize
  47. .getValue(context));
  48. }
  49. /**
  50. * Selects a font which is able to display the given character.
  51. *
  52. * @param fobj
  53. * a Character object containing the character and its
  54. * attributes.
  55. * @param context
  56. * the Percent-based context needed for creating the actual font.
  57. * @return a Font object.
  58. */
  59. public static Font selectFontForCharacter(Character fobj,
  60. PercentBaseContext context) {
  61. return FontSelector.selectFontForCharacter(fobj.getCharacter(), fobj,
  62. fobj.getCommonFont(), context);
  63. }
  64. /**
  65. * Selects a font which is able to display the given character.
  66. *
  67. * @param c
  68. * character to find.
  69. * @param text
  70. * the text object which contains the character
  71. * @param context
  72. * the Percent-based context needed for creating the actual font.
  73. * @return a Font object.
  74. */
  75. public static Font selectFontForCharacterInText(char c, FOText text,
  76. PercentBaseContext context) {
  77. return FontSelector.selectFontForCharacter(c, text, text
  78. .getCommonFont(), context);
  79. }
  80. /**
  81. * Selects a font which is able to display the most of the given characters.
  82. *
  83. * @param charSeq
  84. * Text to go through
  85. * @param firstIndex
  86. * first index within text.
  87. * @param breakIndex
  88. * last index +1 within text.
  89. * @param text
  90. * the text object which contains the character
  91. * @param context
  92. * the Percent-based context needed for creating the actual font.
  93. * @return a Font object.
  94. */
  95. public static Font selectFontForCharactersInText(CharSequence charSeq,
  96. int firstIndex, int breakIndex, FOText text,
  97. PercentBaseContext context) {
  98. final FontInfo fi = text.getFOEventHandler().getFontInfo();
  99. final CommonFont commonFont = text.getCommonFont();
  100. final FontTriplet[] fontkeys = commonFont.getFontState(fi);
  101. final int numFonts = fontkeys.length;
  102. final Font[] fonts = new Font[numFonts];
  103. final int[] fontCount = new int[numFonts];
  104. for (int fontnum = 0; fontnum < numFonts; fontnum++) {
  105. final Font font = fi.getFontInstance(fontkeys[fontnum],
  106. commonFont.fontSize.getValue(context));
  107. fonts[fontnum] = font;
  108. int numCodePoints = 0;
  109. for (int cp : CharUtilities.codepointsIter(charSeq, firstIndex, breakIndex)) {
  110. numCodePoints++;
  111. if (font.hasCodePoint(cp)) {
  112. fontCount[fontnum]++;
  113. }
  114. }
  115. // quick fall through if all codepoints can be displayed
  116. if (fontCount[fontnum] == numCodePoints) {
  117. return font;
  118. }
  119. }
  120. Font font = fonts[0];
  121. int max = fontCount[0];
  122. for (int fontnum = 1; fontnum < numFonts; fontnum++) {
  123. final int curCount = fontCount[fontnum];
  124. if (curCount > max) {
  125. font = fonts[fontnum];
  126. max = curCount;
  127. }
  128. }
  129. return font;
  130. }
  131. }