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.

AFMFile.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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.type1;
  19. import java.awt.geom.Dimension2D;
  20. import java.awt.geom.RectangularShape;
  21. import java.util.Collections;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import org.apache.xmlgraphics.java2d.Dimension2DDouble;
  26. /**
  27. * Represents the contents of a Type 1 AFM font metrics file.
  28. */
  29. public class AFMFile {
  30. private String fontName;
  31. private String fullName;
  32. private String familyName;
  33. private String weight;
  34. private RectangularShape fontBBox;
  35. private String encodingScheme;
  36. private String characterSet;
  37. private Number capHeight;
  38. private Number xHeight;
  39. private Number ascender;
  40. private Number descender;
  41. private Number stdHW;
  42. private Number stdVW;
  43. private AFMWritingDirectionMetrics[] writingDirectionMetrics
  44. = new AFMWritingDirectionMetrics[3];
  45. private List charMetrics = new java.util.ArrayList();
  46. //List<AFMCharMetrics>
  47. private Map charNameToMetrics = new java.util.HashMap();
  48. //Map<String, AFMCharMetrics>
  49. private int firstChar = -1;
  50. private int lastChar = -1;
  51. private Map kerningMap;
  52. //Map<String, Map<String, Dimension2D>>
  53. /**
  54. * Default constructor.
  55. */
  56. public AFMFile() {
  57. //nop
  58. }
  59. /**
  60. * Returns the FontName value.
  61. * @return the font name
  62. */
  63. public String getFontName() {
  64. return fontName;
  65. }
  66. /**
  67. * Sets the FontName value.
  68. * @param fontName the font name to set
  69. */
  70. public void setFontName(String fontName) {
  71. this.fontName = fontName;
  72. }
  73. /**
  74. * Returns the FullName value.
  75. * @return the full name of the font
  76. */
  77. public String getFullName() {
  78. return fullName;
  79. }
  80. /**
  81. * Sets the FullName value.
  82. * @param fullName the full name to set
  83. */
  84. public void setFullName(String fullName) {
  85. this.fullName = fullName;
  86. }
  87. /**
  88. * Returns the FamilyName value.
  89. * @return the family name of the font
  90. */
  91. public String getFamilyName() {
  92. return familyName;
  93. }
  94. /**
  95. * Sets the FamilyName value.
  96. * @param familyName the family name to set
  97. */
  98. public void setFamilyName(String familyName) {
  99. this.familyName = familyName;
  100. }
  101. /**
  102. * Returns the Weight value.
  103. * @return the weight
  104. */
  105. public String getWeight() {
  106. return weight;
  107. }
  108. /**
  109. * Sets the Weight value.
  110. * @param weight the weight to set
  111. */
  112. public void setWeight(String weight) {
  113. this.weight = weight;
  114. }
  115. /**
  116. * Returns the FontBBox value.
  117. * @return the font's bounding box
  118. */
  119. public RectangularShape getFontBBox() {
  120. return fontBBox;
  121. }
  122. /**
  123. * Returns the FontBBox value as integer array.
  124. * @return the font's bounding box
  125. */
  126. public int[] getFontBBoxAsIntArray() {
  127. RectangularShape rect = getFontBBox();
  128. return new int[] {
  129. (int)Math.floor(rect.getMinX()), (int)Math.floor(rect.getMinY()),
  130. (int)Math.ceil(rect.getMaxX()), (int)Math.ceil(rect.getMaxY())};
  131. }
  132. /**
  133. * Sets the FontBBox value.
  134. * @param fontBBox the fontBBox to set
  135. */
  136. public void setFontBBox(RectangularShape fontBBox) {
  137. this.fontBBox = fontBBox;
  138. }
  139. /**
  140. * Returns the EncodingScheme value.
  141. * @return the encoding scheme
  142. */
  143. public String getEncodingScheme() {
  144. return encodingScheme;
  145. }
  146. /**
  147. * Sets the EncodingScheme value
  148. * @param encodingScheme the encodingScheme to set
  149. */
  150. public void setEncodingScheme(String encodingScheme) {
  151. this.encodingScheme = encodingScheme;
  152. }
  153. /**
  154. * Returns the CharacterSet value.
  155. * @return the characterSet
  156. */
  157. public String getCharacterSet() {
  158. return characterSet;
  159. }
  160. /**
  161. * Sets the CharacterSet value.
  162. * @param characterSet the characterSet to set
  163. */
  164. public void setCharacterSet(String characterSet) {
  165. this.characterSet = characterSet;
  166. }
  167. /**
  168. * Returns the CapHeight value.
  169. * @return the capHeight
  170. */
  171. public Number getCapHeight() {
  172. return capHeight;
  173. }
  174. /**
  175. * Sets the CapHeight value.
  176. * @param capHeight the capHeight to set
  177. */
  178. public void setCapHeight(Number capHeight) {
  179. this.capHeight = capHeight;
  180. }
  181. /**
  182. * Returns the XHeight value.
  183. * @return the xHeight
  184. */
  185. public Number getXHeight() {
  186. return xHeight;
  187. }
  188. /**
  189. * Sets the XHeight value.
  190. * @param height the xHeight to set
  191. */
  192. public void setXHeight(Number height) {
  193. xHeight = height;
  194. }
  195. /**
  196. * Returns the Ascender value.
  197. * @return the ascender
  198. */
  199. public Number getAscender() {
  200. return ascender;
  201. }
  202. /**
  203. * Sets the Ascender value.
  204. * @param ascender the ascender to set
  205. */
  206. public void setAscender(Number ascender) {
  207. this.ascender = ascender;
  208. }
  209. /**
  210. * Returns the Descender value.
  211. * @return the descender
  212. */
  213. public Number getDescender() {
  214. return descender;
  215. }
  216. /**
  217. * Sets the Descender value.
  218. * @param descender the descender to set
  219. */
  220. public void setDescender(Number descender) {
  221. this.descender = descender;
  222. }
  223. /**
  224. * Returns the StdHW value.
  225. * @return the descender
  226. */
  227. public Number getStdHW() {
  228. return stdHW;
  229. }
  230. /**
  231. * Sets the StdHW value.
  232. * @param stdHW the StdHW to set
  233. */
  234. public void setStdHW(Number stdHW) {
  235. this.stdHW = stdHW;
  236. }
  237. /**
  238. * Returns the StdVW value.
  239. * @return the descender
  240. */
  241. public Number getStdVW() {
  242. return stdVW;
  243. }
  244. /**
  245. * Sets the StdVW value.
  246. * @param stdVW the StdVW to set
  247. */
  248. public void setStdVW(Number stdVW) {
  249. this.stdVW = stdVW;
  250. }
  251. /**
  252. * Gets writing direction metrics.
  253. * @param index the writing direction (0, 1 or 2)
  254. * @return the writing direction metrics
  255. */
  256. public AFMWritingDirectionMetrics getWritingDirectionMetrics(int index) {
  257. return this.writingDirectionMetrics[index];
  258. }
  259. /**
  260. * Sets writing direction metrics.
  261. * @param index the writing direction (0, 1 or 2)
  262. * @param metrics the writing direction metrics
  263. */
  264. public void setWritingDirectionMetrics(int index, AFMWritingDirectionMetrics metrics) {
  265. this.writingDirectionMetrics[index] = metrics;
  266. }
  267. /**
  268. * Adds new character metrics.
  269. * @param metrics the character metrics
  270. */
  271. public void addCharMetrics(AFMCharMetrics metrics) {
  272. String name = metrics.getCharName();
  273. if (metrics.getUnicodeSequence() == null) {
  274. //Ignore as no Unicode assignment is possible
  275. return;
  276. }
  277. this.charMetrics.add(metrics);
  278. if (name != null) {
  279. this.charNameToMetrics.put(name, metrics);
  280. }
  281. int idx = metrics.getCharCode();
  282. if (idx >= 0) { //Only if the character is part of the encoding
  283. if (firstChar < 0 || idx < firstChar) {
  284. firstChar = idx;
  285. }
  286. if (lastChar < 0 || idx > lastChar) {
  287. lastChar = idx;
  288. }
  289. }
  290. }
  291. /**
  292. * Returns the number of character available for this font.
  293. * @return the number of character
  294. */
  295. public int getCharCount() {
  296. return this.charMetrics.size();
  297. }
  298. /**
  299. * Returns the first character index in the encoding that has a glyph.
  300. * @return the first character index with a glyph
  301. */
  302. public int getFirstChar() {
  303. return this.firstChar;
  304. }
  305. /**
  306. * Returns the last character index in the encoding that has a glyph.
  307. * @return the last character index with a glyph
  308. */
  309. public int getLastChar() {
  310. return this.lastChar;
  311. }
  312. /**
  313. * Returns the character metrics associated with the character name.
  314. * @param name the character name
  315. * @return the character metrics or null if there's no such character
  316. */
  317. public AFMCharMetrics getChar(String name) {
  318. return (AFMCharMetrics)this.charNameToMetrics.get(name);
  319. }
  320. /**
  321. * Returns the list of AFMCharMetrics instances representing all the available characters.
  322. * @return a List of AFMCharMetrics instances
  323. */
  324. public List getCharMetrics() {
  325. return Collections.unmodifiableList(this.charMetrics);
  326. }
  327. /**
  328. * Adds a X-kerning entry.
  329. * @param name1 the name of the first character
  330. * @param name2 the name of the second character
  331. * @param kx kerning value in x-direction
  332. */
  333. public void addXKerning(String name1, String name2, double kx) {
  334. if (this.kerningMap == null) {
  335. this.kerningMap = new java.util.HashMap();
  336. }
  337. Map entries = (Map)this.kerningMap.get(name1);
  338. if (entries == null) {
  339. entries = new java.util.HashMap();
  340. this.kerningMap.put(name1, entries);
  341. }
  342. entries.put(name2, new Dimension2DDouble(kx, 0));
  343. }
  344. /**
  345. * Indicates whether the font has kerning information.
  346. * @return true if there is kerning information
  347. */
  348. public boolean hasKerning() {
  349. return this.kerningMap != null;
  350. }
  351. /**
  352. * Creates and returns a kerning map for writing mode 0 (ltr) with character codes.
  353. * @return the kerning map or null if there is no kerning information.
  354. */
  355. public Map createXKerningMapEncoded() {
  356. if (!hasKerning()) {
  357. return null;
  358. }
  359. Map m = new java.util.HashMap();
  360. Iterator iterFrom = this.kerningMap.entrySet().iterator();
  361. while (iterFrom.hasNext()) {
  362. Map.Entry entryFrom = (Map.Entry)iterFrom.next();
  363. String name1 = (String)entryFrom.getKey();
  364. AFMCharMetrics chm1 = getChar(name1);
  365. if (!chm1.hasCharCode()) {
  366. continue;
  367. }
  368. Map container = null;
  369. Map entriesTo = (Map)entryFrom.getValue();
  370. Iterator iterTo = entriesTo.entrySet().iterator();
  371. while (iterTo.hasNext()) {
  372. Map.Entry entryTo = (Map.Entry)iterTo.next();
  373. String name2 = (String)entryTo.getKey();
  374. AFMCharMetrics chm2 = getChar(name2);
  375. if (!chm2.hasCharCode()) {
  376. continue;
  377. }
  378. if (container == null) {
  379. Integer k1 = new Integer(chm1.getCharCode());
  380. container = (Map)m.get(k1);
  381. if (container == null) {
  382. container = new java.util.HashMap();
  383. m.put(k1, container);
  384. }
  385. }
  386. Dimension2D dim = (Dimension2D)entryTo.getValue();
  387. container.put(new Integer(chm2.getCharCode()),
  388. new Integer((int)Math.round(dim.getWidth())));
  389. }
  390. }
  391. return m;
  392. }
  393. /** {@inheritDoc} */
  394. public String toString() {
  395. return "AFM: " + getFullName();
  396. }
  397. }