Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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