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

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