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

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