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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /**
  20. * Font utilities.
  21. */
  22. public class FontUtil {
  23. /**
  24. * Parses an CSS2 (SVG and XSL-FO) font weight (normal, bold, 100-900) to
  25. * an integer.
  26. * See http://www.w3.org/TR/REC-CSS2/fonts.html#propdef-font-weight
  27. * TODO: Implement "lighter" and "bolder".
  28. * @param text the font weight to parse
  29. * @return an integer between 100 and 900 (100, 200, 300...)
  30. */
  31. public static int parseCSS2FontWeight(String text) {
  32. int weight = 400;
  33. try {
  34. weight = Integer.parseInt(text);
  35. weight = ((int)weight / 100) * 100;
  36. weight = Math.max(weight, 100);
  37. weight = Math.min(weight, 900);
  38. } catch (NumberFormatException nfe) {
  39. //weight is no number, so convert symbolic name to number
  40. if (text.equals("normal")) {
  41. weight = 400;
  42. } else if (text.equals("bold")) {
  43. weight = 700;
  44. } else {
  45. throw new IllegalArgumentException(
  46. "Illegal value for font weight: '"
  47. + text
  48. + "'. Use one of: 100, 200, 300, "
  49. + "400, 500, 600, 700, 800, 900, "
  50. + "normal (=400), bold (=700)");
  51. }
  52. }
  53. return weight;
  54. }
  55. /**
  56. * Removes all white space from a string (used primarily for font names)
  57. * @param str the string
  58. * @return the processed result
  59. */
  60. public static String stripWhiteSpace(String str) {
  61. if (str != null) {
  62. StringBuffer stringBuffer = new StringBuffer(str.length());
  63. for (int i = 0, strLen = str.length(); i < strLen; i++) {
  64. final char ch = str.charAt(i);
  65. if (ch != ' ' && ch != '\r' && ch != '\n' && ch != '\t') {
  66. stringBuffer.append(ch);
  67. }
  68. }
  69. return stringBuffer.toString();
  70. }
  71. return str;
  72. }
  73. /** font constituent names which identify a font as being of "italic" style */
  74. private static final String[] ITALIC_WORDS = {
  75. Font.STYLE_ITALIC, Font.STYLE_OBLIQUE, Font.STYLE_INCLINED
  76. };
  77. /** font constituent names which identify a font as being of "light" weight */
  78. private static final String[] LIGHT_WORDS = {"light"};
  79. /** font constituent names which identify a font as being of "medium" weight */
  80. private static final String[] MEDIUM_WORDS = {"medium"};
  81. /** font constituent names which identify a font as being of "demi/semi" weight */
  82. private static final String[] DEMI_WORDS = {"demi", "semi"};
  83. /** font constituent names which identify a font as being of "bold" weight */
  84. private static final String[] BOLD_WORDS = {"bold"};
  85. /** font constituent names which identify a font as being of "extra bold" weight */
  86. private static final String[] EXTRA_BOLD_WORDS = {"extrabold", "extra bold", "black",
  87. "heavy", "ultra", "super"};
  88. /**
  89. * Guesses the font style of a font using its name.
  90. * @param fontName the font name
  91. * @return "normal" or "italic"
  92. */
  93. public static String guessStyle(String fontName) {
  94. if (fontName != null) {
  95. for (int i = 0; i < ITALIC_WORDS.length; i++) {
  96. if (fontName.indexOf(ITALIC_WORDS[i]) != -1) {
  97. return Font.STYLE_ITALIC;
  98. }
  99. }
  100. }
  101. return Font.STYLE_NORMAL;
  102. }
  103. /**
  104. * Guesses the font weight of a font using its name.
  105. * @param fontName the font name
  106. * @return an integer between 100 and 900
  107. */
  108. public static int guessWeight(String fontName) {
  109. // weight
  110. int weight = Font.WEIGHT_NORMAL;
  111. for (int i = 0; i < BOLD_WORDS.length; i++) {
  112. if (fontName.indexOf(BOLD_WORDS[i]) != -1) {
  113. weight = Font.WEIGHT_BOLD;
  114. break;
  115. }
  116. }
  117. for (int i = 0; i < MEDIUM_WORDS.length; i++) {
  118. if (fontName.indexOf(MEDIUM_WORDS[i]) != -1) {
  119. weight = Font.WEIGHT_NORMAL + 100; //500
  120. break;
  121. }
  122. }
  123. //Search for "semi/demi" before "light", but after "bold"
  124. //(normally semi/demi-bold is meant, but it can also be semi/demi-light)
  125. for (int i = 0; i < DEMI_WORDS.length; i++) {
  126. if (fontName.indexOf(DEMI_WORDS[i]) != -1) {
  127. weight = Font.WEIGHT_BOLD - 100; //600
  128. break;
  129. }
  130. }
  131. for (int i = 0; i < EXTRA_BOLD_WORDS.length; i++) {
  132. if (fontName.indexOf(EXTRA_BOLD_WORDS[i]) != -1) {
  133. weight = Font.WEIGHT_EXTRA_BOLD;
  134. break;
  135. }
  136. }
  137. for (int i = 0; i < LIGHT_WORDS.length; i++) {
  138. if (fontName.indexOf(LIGHT_WORDS[i]) != -1) {
  139. weight = Font.WEIGHT_LIGHT;
  140. break;
  141. }
  142. }
  143. return weight;
  144. }
  145. }