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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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.Collections;
  21. import java.util.HashMap;
  22. import java.util.HashSet;
  23. import java.util.Map;
  24. import java.util.Set;
  25. import javax.xml.transform.Source;
  26. /**
  27. * Abstract base class for custom fonts loaded from files, for example.
  28. */
  29. public abstract class CustomFont extends Typeface
  30. implements FontDescriptor, MutableFont {
  31. private String fontName = null;
  32. private String fullName = null;
  33. private Set<String> familyNames = null;
  34. private String fontSubName = null;
  35. private String embedFileName = null;
  36. private String embedResourceName = null;
  37. private FontResolver resolver = null;
  38. private EmbeddingMode embeddingMode = EmbeddingMode.AUTO;
  39. private int capHeight = 0;
  40. private int xHeight = 0;
  41. private int ascender = 0;
  42. private int descender = 0;
  43. private int[] fontBBox = {0, 0, 0, 0};
  44. private int flags = 4;
  45. private int weight = 0; //0 means unknown weight
  46. private int stemV = 0;
  47. private int italicAngle = 0;
  48. private int missingWidth = 0;
  49. private FontType fontType = FontType.TYPE1;
  50. private int firstChar = 0;
  51. private int lastChar = 255;
  52. private Map<Integer, Map<Integer, Integer>> kerning;
  53. private boolean useKerning = true;
  54. /** the character map, mapping Unicode ranges to glyph indices. */
  55. protected BFEntry[] cmap;
  56. /** {@inheritDoc} */
  57. public String getFontName() {
  58. return fontName;
  59. }
  60. /** {@inheritDoc} */
  61. public String getEmbedFontName() {
  62. return getFontName();
  63. }
  64. /** {@inheritDoc} */
  65. public String getFullName() {
  66. return fullName;
  67. }
  68. /**
  69. * Returns the font family names.
  70. * @return the font family names (a Set of Strings)
  71. */
  72. public Set<String> getFamilyNames() {
  73. return Collections.unmodifiableSet(this.familyNames);
  74. }
  75. /**
  76. * Returns the font family name stripped of whitespace.
  77. * @return the stripped font family
  78. * @see FontUtil#stripWhiteSpace(String)
  79. */
  80. public String getStrippedFontName() {
  81. return FontUtil.stripWhiteSpace(getFontName());
  82. }
  83. /**
  84. * Returns font's subfamily name.
  85. * @return the font's subfamily name
  86. */
  87. public String getFontSubName() {
  88. return fontSubName;
  89. }
  90. /**
  91. * Returns an URI representing an embeddable font file. The URI will often
  92. * be a filename or an URL.
  93. * @return URI to an embeddable font file or null if not available.
  94. */
  95. public String getEmbedFileName() {
  96. return embedFileName;
  97. }
  98. /**
  99. * Returns the embedding mode for this font.
  100. * @return embedding mode
  101. */
  102. public EmbeddingMode getEmbeddingMode() {
  103. return embeddingMode;
  104. }
  105. /**
  106. * Returns a Source representing an embeddable font file.
  107. * @return Source for an embeddable font file
  108. * @throws IOException if embedFileName is not null but Source is not found
  109. */
  110. public Source getEmbedFileSource() throws IOException {
  111. Source result = null;
  112. if (resolver != null && embedFileName != null) {
  113. result = resolver.resolve(embedFileName);
  114. if (result == null) {
  115. throw new IOException("Unable to resolve Source '"
  116. + embedFileName + "' for embedded font");
  117. }
  118. }
  119. return result;
  120. }
  121. /**
  122. * Returns the lookup name to an embeddable font file available as a
  123. * resource.
  124. * (todo) Remove this method, this should be done using a resource: URI.
  125. * @return the lookup name
  126. */
  127. public String getEmbedResourceName() {
  128. return embedResourceName;
  129. }
  130. /**
  131. * {@inheritDoc}
  132. */
  133. public int getAscender() {
  134. return ascender;
  135. }
  136. /**
  137. * {@inheritDoc}
  138. */
  139. public int getDescender() {
  140. return descender;
  141. }
  142. /**
  143. * {@inheritDoc}
  144. */
  145. public int getCapHeight() {
  146. return capHeight;
  147. }
  148. /**
  149. * {@inheritDoc}
  150. */
  151. public int getAscender(int size) {
  152. return size * ascender;
  153. }
  154. /**
  155. * {@inheritDoc}
  156. */
  157. public int getDescender(int size) {
  158. return size * descender;
  159. }
  160. /**
  161. * {@inheritDoc}
  162. */
  163. public int getCapHeight(int size) {
  164. return size * capHeight;
  165. }
  166. /**
  167. * {@inheritDoc}
  168. */
  169. public int getXHeight(int size) {
  170. return size * xHeight;
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public int[] getFontBBox() {
  176. return fontBBox;
  177. }
  178. /** {@inheritDoc} */
  179. public int getFlags() {
  180. return flags;
  181. }
  182. /** {@inheritDoc} */
  183. public boolean isSymbolicFont() {
  184. return ((getFlags() & 4) != 0) || "ZapfDingbatsEncoding".equals(getEncodingName());
  185. //Note: The check for ZapfDingbats is necessary as the PFM does not reliably indicate
  186. //if a font is symbolic.
  187. }
  188. /**
  189. * Returns the font weight (100, 200...800, 900). This value may be different from the
  190. * one that was actually used to register the font.
  191. * @return the font weight (or 0 if the font weight is unknown)
  192. */
  193. public int getWeight() {
  194. return this.weight;
  195. }
  196. /**
  197. * {@inheritDoc}
  198. */
  199. public int getStemV() {
  200. return stemV;
  201. }
  202. /**
  203. * {@inheritDoc}
  204. */
  205. public int getItalicAngle() {
  206. return italicAngle;
  207. }
  208. /**
  209. * Returns the width to be used when no width is available.
  210. * @return a character width
  211. */
  212. public int getMissingWidth() {
  213. return missingWidth;
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public FontType getFontType() {
  219. return fontType;
  220. }
  221. /**
  222. * Returns the index of the first character defined in this font.
  223. * @return the index of the first character
  224. */
  225. public int getFirstChar() {
  226. return firstChar;
  227. }
  228. /**
  229. * Returns the index of the last character defined in this font.
  230. * @return the index of the last character
  231. */
  232. public int getLastChar() {
  233. return lastChar;
  234. }
  235. /**MutableFont
  236. * Used to determine if kerning is enabled.
  237. * @return True if kerning is enabled.
  238. */
  239. public boolean isKerningEnabled() {
  240. return useKerning;
  241. }
  242. /**
  243. * {@inheritDoc}
  244. */
  245. public final boolean hasKerningInfo() {
  246. return (isKerningEnabled() && (kerning != null) && !kerning.isEmpty());
  247. }
  248. /**
  249. * {@inheritDoc}
  250. */
  251. public final Map<Integer, Map<Integer, Integer>> getKerningInfo() {
  252. if (hasKerningInfo()) {
  253. return kerning;
  254. } else {
  255. return Collections.emptyMap();
  256. }
  257. }
  258. /* ---- MutableFont interface ---- */
  259. /** {@inheritDoc} */
  260. public void setFontName(String name) {
  261. this.fontName = name;
  262. }
  263. /** {@inheritDoc} */
  264. public void setFullName(String name) {
  265. this.fullName = name;
  266. }
  267. /** {@inheritDoc} */
  268. public void setFamilyNames(Set<String> names) {
  269. this.familyNames = new HashSet<String>(names);
  270. }
  271. /**
  272. * Sets the font's subfamily name.
  273. * @param subFamilyName the subfamily name of the font
  274. */
  275. public void setFontSubFamilyName(String subFamilyName) {
  276. this.fontSubName = subFamilyName;
  277. }
  278. /**
  279. * {@inheritDoc}
  280. */
  281. public void setEmbedFileName(String path) {
  282. this.embedFileName = path;
  283. }
  284. /**
  285. * {@inheritDoc}
  286. */
  287. public void setEmbedResourceName(String name) {
  288. this.embedResourceName = name;
  289. }
  290. /**
  291. * {@inheritDoc}
  292. */
  293. public void setEmbeddingMode(EmbeddingMode embeddingMode) {
  294. this.embeddingMode = embeddingMode;
  295. }
  296. /**
  297. * {@inheritDoc}
  298. */
  299. public void setCapHeight(int capHeight) {
  300. this.capHeight = capHeight;
  301. }
  302. /**
  303. * Returns the XHeight value of the font.
  304. * @param xHeight the XHeight value
  305. */
  306. public void setXHeight(int xHeight) {
  307. this.xHeight = xHeight;
  308. }
  309. /**
  310. * {@inheritDoc}
  311. */
  312. public void setAscender(int ascender) {
  313. this.ascender = ascender;
  314. }
  315. /**
  316. * {@inheritDoc}
  317. */
  318. public void setDescender(int descender) {
  319. this.descender = descender;
  320. }
  321. /**
  322. * {@inheritDoc}
  323. */
  324. public void setFontBBox(int[] bbox) {
  325. this.fontBBox = bbox;
  326. }
  327. /**
  328. * {@inheritDoc}
  329. */
  330. public void setFlags(int flags) {
  331. this.flags = flags;
  332. }
  333. /**
  334. * Sets the font weight. Valid values are 100, 200...800, 900.
  335. * @param weight the font weight
  336. */
  337. public void setWeight(int weight) {
  338. weight = (weight / 100) * 100;
  339. weight = Math.max(100, weight);
  340. weight = Math.min(900, weight);
  341. this.weight = weight;
  342. }
  343. /**
  344. * {@inheritDoc}
  345. */
  346. public void setStemV(int stemV) {
  347. this.stemV = stemV;
  348. }
  349. /**
  350. * {@inheritDoc}
  351. */
  352. public void setItalicAngle(int italicAngle) {
  353. this.italicAngle = italicAngle;
  354. }
  355. /**
  356. * {@inheritDoc}
  357. */
  358. public void setMissingWidth(int width) {
  359. this.missingWidth = width;
  360. }
  361. /**
  362. * {@inheritDoc}
  363. */
  364. public void setFontType(FontType fontType) {
  365. this.fontType = fontType;
  366. }
  367. /**
  368. * {@inheritDoc}
  369. */
  370. public void setFirstChar(int index) {
  371. this.firstChar = index;
  372. }
  373. /**
  374. * {@inheritDoc}
  375. */
  376. public void setLastChar(int index) {
  377. this.lastChar = index;
  378. }
  379. /**
  380. * {@inheritDoc}
  381. */
  382. public void setKerningEnabled(boolean enabled) {
  383. this.useKerning = enabled;
  384. }
  385. /**
  386. * Sets the font resolver. Needed for URI resolution.
  387. * @param resolver the font resolver
  388. */
  389. public void setResolver(FontResolver resolver) {
  390. this.resolver = resolver;
  391. }
  392. /** {@inheritDoc} */
  393. public void putKerningEntry(Integer key, Map<Integer, Integer> value) {
  394. if (kerning == null) {
  395. kerning = new HashMap<Integer, Map<Integer, Integer>>();
  396. }
  397. this.kerning.put(key, value);
  398. }
  399. /**
  400. * Replaces the existing kerning map with a new one.
  401. * @param kerningMap the kerning map (Map<Integer, Map<Integer, Integer>, the integers are
  402. * character codes)
  403. */
  404. public void replaceKerningMap(Map<Integer, Map<Integer, Integer>> kerningMap) {
  405. if (kerningMap == null) {
  406. this.kerning = Collections.emptyMap();
  407. } else {
  408. this.kerning = kerningMap;
  409. }
  410. }
  411. /**
  412. * Sets the identity character map for this font. It maps all available Unicode characters
  413. * to their glyph indices inside the font.
  414. * @param cmap the identity character map
  415. */
  416. public void setCMap(BFEntry[] cmap) {
  417. this.cmap = new BFEntry[cmap.length];
  418. System.arraycopy(cmap, 0, this.cmap, 0, cmap.length);
  419. }
  420. /**
  421. * Returns the identity character map for this font. It maps all available Unicode characters
  422. * to their glyph indices inside the font.
  423. * @return the identity character map
  424. */
  425. public BFEntry[] getCMap() {
  426. BFEntry[] copy = new BFEntry[cmap.length];
  427. System.arraycopy(this.cmap, 0, copy, 0, this.cmap.length);
  428. return copy;
  429. }
  430. }