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.

CustomFont.java 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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.io.IOException;
  20. import java.util.Map;
  21. import javax.xml.transform.Source;
  22. /**
  23. * Abstract base class for custom fonts loaded from files, for example.
  24. */
  25. public abstract class CustomFont extends Typeface
  26. implements FontDescriptor, MutableFont {
  27. private String fontName = null;
  28. private String fontSubName = null;
  29. private String embedFileName = null;
  30. private String embedResourceName = null;
  31. private FontResolver resolver = null;
  32. private int capHeight = 0;
  33. private int xHeight = 0;
  34. private int ascender = 0;
  35. private int descender = 0;
  36. private int[] fontBBox = {0, 0, 0, 0};
  37. private int flags = 4;
  38. private int stemV = 0;
  39. private int italicAngle = 0;
  40. private int missingWidth = 0;
  41. private FontType fontType = FontType.TYPE1;
  42. private int firstChar = 0;
  43. private int lastChar = 255;
  44. private Map kerning;
  45. private boolean useKerning = true;
  46. /**
  47. * @see org.apache.fop.fonts.FontMetrics#getFontName()
  48. */
  49. public String getFontName() {
  50. return fontName;
  51. }
  52. /**
  53. * @see org.apache.fop.fonts.FontMetrics#getStrippedFontName()
  54. */
  55. public String getStrippedFontName() {
  56. return FontUtil.stripWhiteSpace(fontName);
  57. }
  58. /**
  59. * Returns font's subfamily name.
  60. * @return the font's subfamily name
  61. */
  62. public String getFontSubName() {
  63. return fontSubName;
  64. }
  65. /**
  66. * Returns an URI representing an embeddable font file. The URI will often
  67. * be a filename or an URL.
  68. * @return URI to an embeddable font file or null if not available.
  69. */
  70. public String getEmbedFileName() {
  71. return embedFileName;
  72. }
  73. /**
  74. * Returns a Source representing an embeddable font file.
  75. * @return Source for an embeddable font file
  76. * @throws IOException if embedFileName is not null but Source is not found
  77. */
  78. public Source getEmbedFileSource() throws IOException {
  79. Source result = null;
  80. if (resolver != null && embedFileName != null) {
  81. result = resolver.resolve(embedFileName);
  82. if(result == null) throw new IOException("Unable to resolve Source '" + embedFileName + "' for embedded font");
  83. }
  84. return result;
  85. }
  86. /**
  87. * Returns the lookup name to an embeddable font file available as a
  88. * resource.
  89. * (todo) Remove this method, this should be done using a resource: URI.
  90. * @return the lookup name
  91. */
  92. public String getEmbedResourceName() {
  93. return embedResourceName;
  94. }
  95. /**
  96. * @see org.apache.fop.fonts.FontDescriptor#getAscender()
  97. */
  98. public int getAscender() {
  99. return ascender;
  100. }
  101. /**
  102. * @see org.apache.fop.fonts.FontDescriptor#getDescender()
  103. */
  104. public int getDescender() {
  105. return descender;
  106. }
  107. /**
  108. * @see org.apache.fop.fonts.FontDescriptor#getCapHeight()
  109. */
  110. public int getCapHeight() {
  111. return capHeight;
  112. }
  113. /**
  114. * @see org.apache.fop.fonts.FontMetrics#getAscender(int)
  115. */
  116. public int getAscender(int size) {
  117. return size * ascender;
  118. }
  119. /**
  120. * @see org.apache.fop.fonts.FontMetrics#getDescender(int)
  121. */
  122. public int getDescender(int size) {
  123. return size * descender;
  124. }
  125. /**
  126. * @see org.apache.fop.fonts.FontMetrics#getCapHeight(int)
  127. */
  128. public int getCapHeight(int size) {
  129. return size * capHeight;
  130. }
  131. /**
  132. * @see org.apache.fop.fonts.FontMetrics#getXHeight(int)
  133. */
  134. public int getXHeight(int size) {
  135. return size * xHeight;
  136. }
  137. /**
  138. * @see org.apache.fop.fonts.FontDescriptor#getFontBBox()
  139. */
  140. public int[] getFontBBox() {
  141. return fontBBox;
  142. }
  143. /**
  144. * @see org.apache.fop.fonts.FontDescriptor#getFlags()
  145. */
  146. public int getFlags() {
  147. return flags;
  148. }
  149. /**
  150. * @see org.apache.fop.fonts.FontDescriptor#getStemV()
  151. */
  152. public int getStemV() {
  153. return stemV;
  154. }
  155. /**
  156. * @see org.apache.fop.fonts.FontDescriptor#getItalicAngle()
  157. */
  158. public int getItalicAngle() {
  159. return italicAngle;
  160. }
  161. /**
  162. * Returns the width to be used when no width is available.
  163. * @return a character width
  164. */
  165. public int getMissingWidth() {
  166. return missingWidth;
  167. }
  168. /**
  169. * @see org.apache.fop.fonts.FontDescriptor#getFontType()
  170. */
  171. public FontType getFontType() {
  172. return fontType;
  173. }
  174. /**
  175. * Returns the index of the first character defined in this font.
  176. * @return the index of the first character
  177. */
  178. public int getFirstChar() {
  179. return 0;
  180. // return firstChar;
  181. /**(todo) Why is this hardcoded??? This code was in SingleByteFont.java */
  182. }
  183. /**
  184. * Returns the index of the last character defined in this font.
  185. * @return the index of the last character
  186. */
  187. public int getLastChar() {
  188. return lastChar;
  189. }
  190. /**
  191. * Used to determine if kerning is enabled.
  192. * @return True if kerning is enabled.
  193. */
  194. public boolean isKerningEnabled() {
  195. return useKerning;
  196. }
  197. /**
  198. * @see org.apache.fop.fonts.FontMetrics#hasKerningInfo()
  199. */
  200. public final boolean hasKerningInfo() {
  201. return (isKerningEnabled() && (kerning != null) && !kerning.isEmpty());
  202. }
  203. /**
  204. * @see org.apache.fop.fonts.FontMetrics#getKerningInfo()
  205. */
  206. public final Map getKerningInfo() {
  207. if (hasKerningInfo()) {
  208. return kerning;
  209. } else {
  210. return java.util.Collections.EMPTY_MAP;
  211. }
  212. }
  213. /* ---- MutableFont interface ---- */
  214. /**
  215. * @see org.apache.fop.fonts.MutableFont#setFontName(String)
  216. */
  217. public void setFontName(String name) {
  218. this.fontName = name;
  219. }
  220. /**
  221. * Sets the font's subfamily name.
  222. * @param subFamilyName the subfamily name of the font
  223. */
  224. public void setFontSubFamilyName(String subFamilyName) {
  225. this.fontSubName = subFamilyName;
  226. }
  227. /**
  228. * @see org.apache.fop.fonts.MutableFont#setEmbedFileName(String)
  229. */
  230. public void setEmbedFileName(String path) {
  231. this.embedFileName = path;
  232. }
  233. /**
  234. * @see org.apache.fop.fonts.MutableFont#setEmbedResourceName(String)
  235. */
  236. public void setEmbedResourceName(String name) {
  237. this.embedResourceName = name;
  238. }
  239. /**
  240. * @see org.apache.fop.fonts.MutableFont#setCapHeight(int)
  241. */
  242. public void setCapHeight(int capHeight) {
  243. this.capHeight = capHeight;
  244. }
  245. /**
  246. * Returns the XHeight value of the font.
  247. * @param xHeight the XHeight value
  248. */
  249. public void setXHeight(int xHeight) {
  250. this.xHeight = xHeight;
  251. }
  252. /**
  253. * @see org.apache.fop.fonts.MutableFont#setAscender(int)
  254. */
  255. public void setAscender(int ascender) {
  256. this.ascender = ascender;
  257. }
  258. /**
  259. * @see org.apache.fop.fonts.MutableFont#setDescender(int)
  260. */
  261. public void setDescender(int descender) {
  262. this.descender = descender;
  263. }
  264. /**
  265. * @see org.apache.fop.fonts.MutableFont#setFontBBox(int[])
  266. */
  267. public void setFontBBox(int[] bbox) {
  268. this.fontBBox = bbox;
  269. }
  270. /**
  271. * @see org.apache.fop.fonts.MutableFont#setFlags(int)
  272. */
  273. public void setFlags(int flags) {
  274. this.flags = flags;
  275. }
  276. /**
  277. * @see org.apache.fop.fonts.MutableFont#setStemV(int)
  278. */
  279. public void setStemV(int stemV) {
  280. this.stemV = stemV;
  281. }
  282. /**
  283. * @see org.apache.fop.fonts.MutableFont#setItalicAngle(int)
  284. */
  285. public void setItalicAngle(int italicAngle) {
  286. this.italicAngle = italicAngle;
  287. }
  288. /**
  289. * @see org.apache.fop.fonts.MutableFont#setMissingWidth(int)
  290. */
  291. public void setMissingWidth(int width) {
  292. this.missingWidth = width;
  293. }
  294. /**
  295. * @see org.apache.fop.fonts.MutableFont#setFontType(FontType)
  296. */
  297. public void setFontType(FontType fontType) {
  298. this.fontType = fontType;
  299. }
  300. /**
  301. * @see org.apache.fop.fonts.MutableFont#setFirstChar(int)
  302. */
  303. public void setFirstChar(int index) {
  304. this.firstChar = index;
  305. }
  306. /**
  307. * @see org.apache.fop.fonts.MutableFont#setLastChar(int)
  308. */
  309. public void setLastChar(int index) {
  310. this.lastChar = index;
  311. }
  312. /**
  313. * @see org.apache.fop.fonts.MutableFont#setKerningEnabled(boolean)
  314. */
  315. public void setKerningEnabled(boolean enabled) {
  316. this.useKerning = enabled;
  317. }
  318. /**
  319. * Sets the font resolver. Needed for URI resolution.
  320. * @param resolver the font resolver
  321. */
  322. public void setResolver(FontResolver resolver) {
  323. this.resolver = resolver;
  324. }
  325. /**
  326. * @see org.apache.fop.fonts.MutableFont#putKerningEntry(Integer, Map)
  327. */
  328. public void putKerningEntry(Integer key, Map value) {
  329. if (kerning == null) {
  330. kerning = new java.util.HashMap();
  331. }
  332. this.kerning.put(key, value);
  333. }
  334. }