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.

LazyFont.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * $Id$
  3. * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
  4. * For details on use and redistribution please refer to the
  5. * LICENSE file included with these sources.
  6. */
  7. package org.apache.fop.render.pdf.fonts;
  8. import org.apache.fop.render.pdf.Font;
  9. import org.apache.fop.layout.FontDescriptor;
  10. import org.apache.fop.pdf.PDFStream;
  11. import java.util.HashMap;
  12. import org.apache.fop.render.pdf.FontReader;
  13. public class LazyFont extends Font implements FontDescriptor {
  14. private String metricsFileName = null;
  15. private String fontEmbedPath = null;
  16. private boolean useKerning = false;
  17. private boolean isMetricsLoaded = false;
  18. private Font realFont = null;
  19. private FontDescriptor realFontDescriptor = null;
  20. public LazyFont(String fontEmbedPath, String metricsFileName, boolean useKerning){
  21. this.metricsFileName = metricsFileName;
  22. this.fontEmbedPath = fontEmbedPath;
  23. this.useKerning = useKerning;
  24. }
  25. private void load(){
  26. if(! isMetricsLoaded){
  27. isMetricsLoaded = true;
  28. try{
  29. FontReader reader = new FontReader(metricsFileName);
  30. reader.useKerning(useKerning);
  31. reader.setFontEmbedPath(fontEmbedPath);
  32. realFont = reader.getFont();
  33. if(realFont instanceof FontDescriptor){
  34. realFontDescriptor = (FontDescriptor) realFont;
  35. }
  36. // System.out.println("Metrics " + metricsFileName + " loaded.");
  37. } catch (Exception ex) {
  38. //log.error("Failed to read font metrics file "
  39. // + metricsFileName
  40. // + " : " + ex.getMessage());
  41. }
  42. }
  43. }
  44. public Font getRealFont(){
  45. return realFont;
  46. }
  47. // Font
  48. public String encoding(){
  49. load();
  50. return realFont.encoding();
  51. }
  52. public String fontName(){
  53. load();
  54. return realFont.fontName();
  55. }
  56. public byte getSubType(){
  57. load();
  58. return realFont.getSubType();
  59. }
  60. public char mapChar(char c){
  61. load();
  62. return realFont.mapChar(c);
  63. }
  64. // FontMetrics
  65. public int getAscender(int size){
  66. load();
  67. return realFont.getAscender(size);
  68. }
  69. public int getCapHeight(int size){
  70. load();
  71. return realFont.getCapHeight(size);
  72. }
  73. public int getDescender(int size){
  74. load();
  75. return realFont.getDescender(size);
  76. }
  77. public int getXHeight(int size){
  78. load();
  79. return realFont.getXHeight(size);
  80. }
  81. public int getFirstChar(){
  82. load();
  83. return realFont.getFirstChar();
  84. }
  85. public int getLastChar(){
  86. load();
  87. return realFont.getLastChar();
  88. }
  89. public int width(int i, int size){
  90. load();
  91. return realFont.width(i, size);
  92. }
  93. public int[] getWidths(int size){
  94. load();
  95. return realFont.getWidths(size);
  96. }
  97. // FontDescriptor
  98. public int getCapHeight(){
  99. load();
  100. return realFontDescriptor.getCapHeight();
  101. }
  102. public int getDescender(){
  103. load();
  104. return realFontDescriptor.getDescender();
  105. }
  106. public int getAscender(){
  107. load();
  108. return realFontDescriptor.getAscender();
  109. }
  110. public int getFlags(){
  111. load();
  112. return realFontDescriptor.getFlags();
  113. }
  114. public int[] getFontBBox(){
  115. load();
  116. return realFontDescriptor.getFontBBox();
  117. }
  118. public int getItalicAngle(){
  119. load();
  120. return realFontDescriptor.getItalicAngle();
  121. }
  122. public int getStemV(){
  123. load();
  124. return realFontDescriptor.getStemV();
  125. }
  126. public boolean hasKerningInfo(){
  127. load();
  128. return realFontDescriptor.hasKerningInfo();
  129. }
  130. public HashMap getKerningInfo(){
  131. load();
  132. return realFontDescriptor.getKerningInfo();
  133. }
  134. public boolean isEmbeddable(){
  135. load();
  136. return realFontDescriptor.isEmbeddable();
  137. }
  138. public PDFStream getFontFile(int objNum){
  139. load();
  140. return realFontDescriptor.getFontFile(objNum);
  141. }
  142. }