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.0KB

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