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.

Typeface.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 java.util.HashSet;
  20. import java.util.Set;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.xmlgraphics.fonts.Glyphs;
  24. /**
  25. * Base class for font classes
  26. */
  27. public abstract class Typeface implements FontMetrics {
  28. /**
  29. * Code point that is used if no code point for a specific character has
  30. * been found.
  31. */
  32. public static final char NOT_FOUND = '#';
  33. /** logger */
  34. private static Log log = LogFactory.getLog(Typeface.class);
  35. /**
  36. * Used to identify whether a font has been used (a character map operation
  37. * is used as the trigger). This could just as well be a boolean but is a
  38. * long out of statistical interest.
  39. */
  40. private long charMapOps;
  41. /** An optional event listener that receives events such as missing glyphs etc. */
  42. protected FontEventListener eventListener;
  43. private Set<Character> warnedChars;
  44. /**
  45. * Get the encoding of the font.
  46. * @return the encoding
  47. */
  48. public abstract String getEncodingName();
  49. /**
  50. * Map a Unicode character to a code point in the font.
  51. * @param c character to map
  52. * @return the mapped character
  53. */
  54. public abstract char mapChar(char c);
  55. /**
  56. * Used for keeping track of character mapping operations in order to determine if a font
  57. * was used at all or not.
  58. */
  59. protected void notifyMapOperation() {
  60. this.charMapOps++;
  61. }
  62. /**
  63. * Indicates whether this font had to do any character mapping operations. If that was
  64. * not the case, it's an indication that the font has never actually been used.
  65. * @return true if the font had to do any character mapping operations
  66. */
  67. public boolean hadMappingOperations() {
  68. return (this.charMapOps > 0);
  69. }
  70. /**
  71. * Determines whether this font contains a particular character/glyph.
  72. * @param c character to check
  73. * @return True if the character is supported, Falso otherwise
  74. */
  75. public abstract boolean hasChar(char c);
  76. /**
  77. * Determines whether the font is a multibyte font.
  78. * @return True if it is multibyte
  79. */
  80. public boolean isMultiByte() {
  81. return false;
  82. }
  83. /** {@inheritDoc} */
  84. public int getMaxAscent(int size) {
  85. return getAscender(size);
  86. }
  87. /** {@inheritDoc} */
  88. public boolean hasFeature(int tableType, String script, String language, String feature) {
  89. return false;
  90. }
  91. /**
  92. * Sets the font event listener that can be used to receive events about particular events
  93. * in this class.
  94. * @param listener the font event listener
  95. */
  96. public void setEventListener(FontEventListener listener) {
  97. this.eventListener = listener;
  98. }
  99. /**
  100. * Provide proper warning if a glyph is not available.
  101. *
  102. * @param c
  103. * the character which is missing.
  104. */
  105. protected void warnMissingGlyph(char c) {
  106. // Give up, character is not available
  107. Character ch = c;
  108. if (warnedChars == null) {
  109. warnedChars = new HashSet<Character>();
  110. }
  111. if (warnedChars.size() < 8 && !warnedChars.contains(ch)) {
  112. warnedChars.add(ch);
  113. if (this.eventListener != null) {
  114. this.eventListener.glyphNotAvailable(this, c, getFontName());
  115. } else {
  116. if (warnedChars.size() == 8) {
  117. log.warn("Many requested glyphs are not available in font "
  118. + getFontName());
  119. } else {
  120. log.warn("Glyph " + (int) c + " (0x"
  121. + Integer.toHexString(c) + ", "
  122. + Glyphs.charToGlyphName(c)
  123. + ") not available in font " + getFontName());
  124. }
  125. }
  126. }
  127. }
  128. /** {@inheritDoc} */
  129. public String toString() {
  130. StringBuffer sbuf = new StringBuffer(super.toString());
  131. sbuf.append('{');
  132. sbuf.append(getFullName());
  133. sbuf.append('}');
  134. return sbuf.toString();
  135. }
  136. }