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.

FOPGVTFont.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.svg.font;
  19. import java.awt.font.FontRenderContext;
  20. import java.text.CharacterIterator;
  21. import java.text.StringCharacterIterator;
  22. import org.apache.batik.gvt.font.GVTFont;
  23. import org.apache.batik.gvt.font.GVTFontFamily;
  24. import org.apache.batik.gvt.font.GVTGlyphVector;
  25. import org.apache.batik.gvt.font.GVTLineMetrics;
  26. import org.apache.fop.fonts.Font;
  27. import org.apache.fop.fonts.FontMetrics;
  28. public class FOPGVTFont implements GVTFont {
  29. private final Font font;
  30. private final GVTFontFamily fontFamily;
  31. public FOPGVTFont(Font font, GVTFontFamily fontFamily) {
  32. this.font = font;
  33. this.fontFamily = fontFamily;
  34. }
  35. public Font getFont() {
  36. return font;
  37. }
  38. public boolean canDisplay(char c) {
  39. return font.hasChar(c);
  40. }
  41. public int canDisplayUpTo(char[] text, int start, int limit) {
  42. for (int i = start; i < limit; i++) {
  43. if (!canDisplay(text[i])) {
  44. return i;
  45. }
  46. }
  47. return -1;
  48. }
  49. public int canDisplayUpTo(CharacterIterator iter, int start, int limit) {
  50. for (char c = iter.setIndex(start); iter.getIndex() < limit; c = iter.next()) {
  51. if (!canDisplay(c)) {
  52. return iter.getIndex();
  53. }
  54. }
  55. return -1;
  56. }
  57. public int canDisplayUpTo(String str) {
  58. for (int i = 0; i < str.length(); i++) {
  59. if (!canDisplay(str.charAt(i))) {
  60. return i;
  61. }
  62. }
  63. return -1;
  64. }
  65. public GVTGlyphVector createGlyphVector(FontRenderContext frc, char[] chars) {
  66. return createGlyphVector(frc, new String(chars));
  67. }
  68. public GVTGlyphVector createGlyphVector(FontRenderContext frc, CharacterIterator ci) {
  69. // TODO Batik does manual glyph shaping for Arabic. Replace with complex scripts implementation
  70. return new FOPGVTGlyphVector(this, ci, frc);
  71. }
  72. public GVTGlyphVector createGlyphVector(FontRenderContext frc,
  73. int[] glyphCodes,
  74. CharacterIterator ci) {
  75. throw new UnsupportedOperationException("Not implemented");
  76. }
  77. public GVTGlyphVector createGlyphVector(FontRenderContext frc, String str) {
  78. StringCharacterIterator sci = new StringCharacterIterator(str);
  79. return createGlyphVector(frc, sci);
  80. }
  81. public FOPGVTFont deriveFont(float size) {
  82. throw new UnsupportedOperationException("Not implemented");
  83. }
  84. public String getFamilyName() {
  85. return fontFamily.getFamilyName();
  86. }
  87. public GVTLineMetrics getLineMetrics(char[] chars, int beginIndex, int limit, FontRenderContext frc) {
  88. return getLineMetrics(limit - beginIndex);
  89. }
  90. GVTLineMetrics getLineMetrics(int numChars) {
  91. numChars = numChars < 0 ? 0 : numChars;
  92. FontMetrics metrics = font.getFontMetrics();
  93. int size = font.getFontSize();
  94. return new GVTLineMetrics(
  95. metrics.getCapHeight(size) / 1000000f,
  96. java.awt.Font.ROMAN_BASELINE, // Not actually used by Batik
  97. null, // Not actually used by Batik
  98. -metrics.getDescender(size) / 1000000f,
  99. 0, // Not actually used by Batik
  100. 0, // Not actually used by Batik
  101. numChars,
  102. -metrics.getStrikeoutPosition(size) / 1000000f,
  103. metrics.getStrikeoutThickness(size) / 1000000f,
  104. -metrics.getUnderlinePosition(size) / 1000000f,
  105. metrics.getUnderlineThickness(size) / 1000000f,
  106. -metrics.getCapHeight(size) / 1000000f, // Because this is what Batik does in GVTLineMetrics
  107. metrics.getUnderlineThickness(size) / 1000000f);
  108. }
  109. public GVTLineMetrics getLineMetrics(CharacterIterator ci, int beginIndex, int limit,
  110. FontRenderContext frc) {
  111. return getLineMetrics(limit - beginIndex);
  112. }
  113. public GVTLineMetrics getLineMetrics(String str, FontRenderContext frc) {
  114. return getLineMetrics(str.length());
  115. }
  116. public GVTLineMetrics getLineMetrics(String str, int beginIndex, int limit, FontRenderContext frc) {
  117. return getLineMetrics(limit - beginIndex);
  118. }
  119. public float getSize() {
  120. return font.getFontSize() / 1000f;
  121. }
  122. public float getVKern(int glyphCode1, int glyphCode2) {
  123. return 0;
  124. }
  125. public float getHKern(int glyphCode1, int glyphCode2) {
  126. // TODO Cannot be implemented until getKernValue takes glyph indices instead of character codes
  127. return 0;
  128. }
  129. }