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.

CharacterSetBuilder.java 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  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.afp.fonts;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.net.MalformedURLException;
  22. import java.net.URI;
  23. import java.net.URISyntaxException;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.WeakHashMap;
  30. import org.apache.commons.logging.Log;
  31. import org.apache.commons.logging.LogFactory;
  32. import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
  33. import org.apache.fop.afp.AFPConstants;
  34. import org.apache.fop.afp.AFPEventProducer;
  35. import org.apache.fop.afp.util.AFPResourceAccessor;
  36. import org.apache.fop.afp.util.StructuredFieldReader;
  37. import org.apache.fop.apps.io.InternalResourceResolver;
  38. import org.apache.fop.fonts.Typeface;
  39. /**
  40. * The CharacterSetBuilder is responsible building the a CharacterSet instance that holds
  41. * the font metric data. The data is either read from disk and passed to a CharacterSet (*)
  42. * or a FopCharacterSet is instantiated that is composed of a Typeface instance configured
  43. * with this data.<p/>
  44. * -*- For referenced fonts CharacterSetBuilder is responsible for reading the font attributes
  45. * from binary code page files and the character set metric files. In IBM font structure, a
  46. * code page maps each character of text to the characters in a character set.
  47. * Each character is translated into a code point. When the character is
  48. * printed, each code point is matched to a character ID on the code page
  49. * specified. The character ID is then matched to the image (raster pattern or
  50. * outline pattern) of the character in the character set specified. The image
  51. * in the character set is the image that is printed in the document. To be a
  52. * valid code page for a particular character set, all character IDs in the code
  53. * page must be included in that character set. <p/>This class will read the
  54. * font information from the binary code page files and character set metric
  55. * files in order to determine the correct metrics to use when rendering the
  56. * formatted object. <p/>
  57. *
  58. */
  59. public abstract class CharacterSetBuilder {
  60. /**
  61. * Static logging instance
  62. */
  63. protected static final Log LOG = LogFactory.getLog(CharacterSetBuilder.class);
  64. /**
  65. * Template used to convert lists to arrays.
  66. */
  67. private static final CharacterSetOrientation[] EMPTY_CSO_ARRAY = new CharacterSetOrientation[0];
  68. /** Codepage MO:DCA structured field. */
  69. private static final byte[] CODEPAGE_SF = new byte[] {
  70. (byte) 0xD3, (byte) 0xA8, (byte) 0x87};
  71. /** Character table MO:DCA structured field. */
  72. private static final byte[] CHARACTER_TABLE_SF = new byte[] {
  73. (byte) 0xD3, (byte) 0x8C, (byte) 0x87};
  74. /** Font descriptor MO:DCA structured field. */
  75. private static final byte[] FONT_DESCRIPTOR_SF = new byte[] {
  76. (byte) 0xD3, (byte) 0xA6, (byte) 0x89 };
  77. /** Font control MO:DCA structured field. */
  78. private static final byte[] FONT_CONTROL_SF = new byte[] {
  79. (byte) 0xD3, (byte) 0xA7, (byte) 0x89 };
  80. /** Font orientation MO:DCA structured field. */
  81. private static final byte[] FONT_ORIENTATION_SF = new byte[] {
  82. (byte) 0xD3, (byte) 0xAE, (byte) 0x89 };
  83. /** Font position MO:DCA structured field. */
  84. private static final byte[] FONT_POSITION_SF = new byte[] {
  85. (byte) 0xD3, (byte) 0xAC, (byte) 0x89 };
  86. /** Font index MO:DCA structured field. */
  87. private static final byte[] FONT_INDEX_SF = new byte[] {
  88. (byte) 0xD3, (byte) 0x8C, (byte) 0x89 };
  89. /**
  90. * The collection of code pages
  91. */
  92. private final Map<String, Map<String, String>> codePagesCache
  93. = Collections.synchronizedMap(new WeakHashMap<String, Map<String, String>>());
  94. /**
  95. * Cache of charactersets
  96. */
  97. private final SoftMapCache characterSetsCache = new SoftMapCache(true);
  98. /** Default constructor. */
  99. private CharacterSetBuilder() {
  100. }
  101. /**
  102. * Factory method for the single-byte implementation of AFPFontReader.
  103. * @return AFPFontReader
  104. */
  105. public static CharacterSetBuilder getSingleByteInstance() {
  106. return SingleByteLoader.getInstance();
  107. }
  108. /**
  109. * Factory method for the double-byte (CID Keyed font (Type 0)) implementation of AFPFontReader.
  110. * @return AFPFontReader
  111. */
  112. public static CharacterSetBuilder getDoubleByteInstance() {
  113. return DoubleByteLoader.getInstance();
  114. }
  115. /**
  116. * Returns an InputStream to a given file path and filename
  117. *
  118. * * @param accessor the resource accessor
  119. * @param uriStr the URI
  120. * @param eventProducer for handling AFP related events
  121. * @return an inputStream
  122. * @throws IOException in the event that an I/O exception of some sort has occurred
  123. */
  124. private InputStream openInputStream(AFPResourceAccessor accessor, String uriStr,
  125. AFPEventProducer eventProducer)
  126. throws IOException {
  127. URI uri;
  128. try {
  129. uri = InternalResourceResolver.cleanURI(uriStr.trim());
  130. } catch (URISyntaxException e) {
  131. throw new MalformedURLException("Invalid uri: " + uriStr + " (" + e.getMessage() + ")");
  132. }
  133. if (LOG.isDebugEnabled()) {
  134. LOG.debug("Opening " + uri);
  135. }
  136. return accessor.createInputStream(uri);
  137. }
  138. /**
  139. * Closes the inputstream
  140. *
  141. * @param inputStream the inputstream to close
  142. */
  143. private void closeInputStream(InputStream inputStream) {
  144. try {
  145. if (inputStream != null) {
  146. inputStream.close();
  147. }
  148. } catch (Exception ex) {
  149. // Lets log at least!
  150. LOG.error(ex.getMessage());
  151. }
  152. }
  153. /**
  154. * Load the font details and metrics into the CharacterSetMetric object, this will use the
  155. * actual afp code page and character set files to load the object with the necessary metrics.
  156. *
  157. * @param characterSetName name of the characterset
  158. * @param codePageName name of the code page file
  159. * @param encoding encoding name
  160. * @param accessor used to load codepage and characterset
  161. * @param eventProducer for handling AFP related events
  162. * @return CharacterSet object
  163. * @throws IOException if an I/O error occurs
  164. */
  165. public CharacterSet buildSBCS(String characterSetName, String codePageName, String encoding,
  166. AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException {
  167. return processFont(characterSetName, codePageName, encoding, CharacterSetType.SINGLE_BYTE,
  168. accessor, eventProducer);
  169. }
  170. /**
  171. * Load the font details and metrics into the CharacterSetMetric object, this will use the
  172. * actual afp code page and character set files to load the object with the necessary metrics.
  173. * This method is to be used for double byte character sets (DBCS).
  174. *
  175. * @param characterSetName name of the characterset
  176. * @param codePageName name of the code page file
  177. * @param encoding encoding name
  178. * @param charsetType the characterset type
  179. * @param accessor used to load codepage and characterset
  180. * @param eventProducer for handling AFP related events
  181. * @return CharacterSet object
  182. * @throws IOException if an I/O error occurs
  183. */
  184. public CharacterSet buildDBCS(String characterSetName, String codePageName, String encoding,
  185. CharacterSetType charsetType, AFPResourceAccessor accessor, AFPEventProducer eventProducer)
  186. throws IOException {
  187. return processFont(characterSetName, codePageName, encoding, charsetType, accessor,
  188. eventProducer);
  189. }
  190. /**
  191. * Load the font details and metrics into the CharacterSetMetric object, this will use the
  192. * actual afp code page and character set files to load the object with the necessary metrics.
  193. *
  194. * @param characterSetName the CharacterSetMetric object to populate
  195. * @param codePageName the name of the code page to use
  196. * @param encoding name of the encoding in use
  197. * @param typeface base14 font name
  198. * @param eventProducer for handling AFP related events
  199. * @return CharacterSet object
  200. * @throws IOException if an I/O error occurs
  201. */
  202. public CharacterSet build(String characterSetName, String codePageName, String encoding,
  203. Typeface typeface, AFPEventProducer eventProducer) throws IOException {
  204. return new FopCharacterSet(codePageName, encoding, characterSetName, typeface,
  205. eventProducer);
  206. }
  207. private CharacterSet processFont(String characterSetName, String codePageName, String encoding,
  208. CharacterSetType charsetType, AFPResourceAccessor accessor, AFPEventProducer eventProducer)
  209. throws IOException {
  210. // check for cached version of the characterset
  211. String descriptor = characterSetName + "_" + encoding + "_" + codePageName;
  212. CharacterSet characterSet = (CharacterSet) characterSetsCache.get(descriptor);
  213. if (characterSet != null) {
  214. return characterSet;
  215. }
  216. // characterset not in the cache, so recreating
  217. characterSet = new CharacterSet(codePageName, encoding, charsetType, characterSetName,
  218. accessor, eventProducer);
  219. InputStream inputStream = null;
  220. try {
  221. /**
  222. * Get the code page which contains the character mapping
  223. * information to map the unicode character id to the graphic
  224. * chracter global identifier.
  225. */
  226. Map<String, String> codePage;
  227. synchronized (codePagesCache) {
  228. codePage = codePagesCache.get(codePageName);
  229. if (codePage == null) {
  230. codePage = loadCodePage(codePageName, encoding, accessor, eventProducer);
  231. codePagesCache.put(codePageName, codePage);
  232. }
  233. }
  234. inputStream = openInputStream(accessor, characterSetName, eventProducer);
  235. StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream);
  236. // Process D3A689 Font Descriptor
  237. FontDescriptor fontDescriptor = processFontDescriptor(structuredFieldReader);
  238. characterSet.setNominalVerticalSize(fontDescriptor.getNominalFontSizeInMillipoints());
  239. // Process D3A789 Font Control
  240. FontControl fontControl = processFontControl(structuredFieldReader);
  241. if (fontControl != null) {
  242. //process D3AE89 Font Orientation
  243. CharacterSetOrientation[] characterSetOrientations
  244. = processFontOrientation(structuredFieldReader);
  245. double metricNormalizationFactor;
  246. if (fontControl.isRelative()) {
  247. metricNormalizationFactor = 1;
  248. } else {
  249. int dpi = fontControl.getDpi();
  250. metricNormalizationFactor = 1000.0d * 72000.0d
  251. / fontDescriptor.getNominalFontSizeInMillipoints() / dpi;
  252. }
  253. //process D3AC89 Font Position
  254. processFontPosition(structuredFieldReader, characterSetOrientations,
  255. metricNormalizationFactor);
  256. //process D38C89 Font Index (per orientation)
  257. for (int i = 0; i < characterSetOrientations.length; i++) {
  258. processFontIndex(structuredFieldReader,
  259. characterSetOrientations[i], codePage, metricNormalizationFactor);
  260. characterSet.addCharacterSetOrientation(characterSetOrientations[i]);
  261. }
  262. } else {
  263. throw new IOException("Missing D3AE89 Font Control structured field.");
  264. }
  265. } finally {
  266. closeInputStream(inputStream);
  267. }
  268. characterSetsCache.put(descriptor, characterSet);
  269. return characterSet;
  270. }
  271. /**
  272. * Load the code page information from the appropriate file. The file name
  273. * to load is determined by the code page name and the file extension 'CDP'.
  274. *
  275. * @param codePage
  276. * the code page identifier
  277. * @param encoding
  278. * the encoding to use for the character decoding
  279. * @param accessor the resource accessor
  280. * @param eventProducer for handling AFP related events
  281. * @return a code page mapping (key: GCGID, value: Unicode character)
  282. * @throws IOException if an I/O exception of some sort has occurred.
  283. */
  284. protected Map<String, String> loadCodePage(String codePage, String encoding,
  285. AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException {
  286. // Create the HashMap to store code page information
  287. Map<String, String> codePages = new HashMap<String, String>();
  288. InputStream inputStream = null;
  289. try {
  290. inputStream = openInputStream(accessor, codePage.trim(), eventProducer);
  291. } catch (IOException e) {
  292. eventProducer.codePageNotFound(this, e);
  293. throw e;
  294. }
  295. try {
  296. StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream);
  297. byte[] data = structuredFieldReader.getNext(CHARACTER_TABLE_SF);
  298. int position = 0;
  299. byte[] gcgiBytes = new byte[8];
  300. byte[] charBytes = new byte[1];
  301. // Read data, ignoring bytes 0 - 2
  302. for (int index = 3; index < data.length; index++) {
  303. if (position < 8) {
  304. // Build the graphic character global identifier key
  305. gcgiBytes[position] = data[index];
  306. position++;
  307. } else if (position == 9) {
  308. position = 0;
  309. // Set the character
  310. charBytes[0] = data[index];
  311. String gcgiString = new String(gcgiBytes,
  312. AFPConstants.EBCIDIC_ENCODING);
  313. //Use the 8-bit char index to find the Unicode character using the Java encoding
  314. //given in the configuration. If the code page and the Java encoding don't
  315. //match, a wrong Unicode character will be associated with the AFP GCGID.
  316. //Idea: we could use IBM's GCGID to Unicode map and build code pages ourselves.
  317. String charString = new String(charBytes, encoding);
  318. codePages.put(gcgiString, charString);
  319. } else {
  320. position++;
  321. }
  322. }
  323. } finally {
  324. closeInputStream(inputStream);
  325. }
  326. return codePages;
  327. }
  328. /**
  329. * Process the font descriptor details using the structured field reader.
  330. *
  331. * @param structuredFieldReader the structured field reader
  332. * @return a class representing the font descriptor
  333. * @throws IOException if an I/O exception of some sort has occurred.
  334. */
  335. private static FontDescriptor processFontDescriptor(
  336. StructuredFieldReader structuredFieldReader) throws IOException {
  337. byte[] fndData = structuredFieldReader.getNext(FONT_DESCRIPTOR_SF);
  338. return new FontDescriptor(fndData);
  339. }
  340. /**
  341. * Process the font control details using the structured field reader.
  342. *
  343. * @param structuredFieldReader
  344. * the structured field reader
  345. * @return the FontControl
  346. * @throws IOException if an I/O exception of some sort has occurred.
  347. */
  348. private FontControl processFontControl(StructuredFieldReader structuredFieldReader)
  349. throws IOException {
  350. byte[] fncData = structuredFieldReader.getNext(FONT_CONTROL_SF);
  351. FontControl fontControl = null;
  352. if (fncData != null) {
  353. fontControl = new FontControl();
  354. if (fncData[7] == (byte) 0x02) {
  355. fontControl.setRelative(true);
  356. }
  357. int metricResolution = getUBIN(fncData, 9);
  358. if (metricResolution == 1000) {
  359. //Special case: 1000 units per em (rather than dpi)
  360. fontControl.setUnitsPerEm(1000);
  361. } else {
  362. fontControl.setDpi(metricResolution / 10);
  363. }
  364. }
  365. return fontControl;
  366. }
  367. /**
  368. * Process the font orientation details from using the structured field
  369. * reader.
  370. *
  371. * @param structuredFieldReader
  372. * the structured field reader
  373. * @return CharacterSetOrientation array
  374. * @throws IOException if an I/O exception of some sort has occurred.
  375. */
  376. private CharacterSetOrientation[] processFontOrientation(
  377. StructuredFieldReader structuredFieldReader) throws IOException {
  378. byte[] data = structuredFieldReader.getNext(FONT_ORIENTATION_SF);
  379. int position = 0;
  380. byte[] fnoData = new byte[26];
  381. List<CharacterSetOrientation> orientations = new ArrayList<CharacterSetOrientation>();
  382. // Read data, ignoring bytes 0 - 2
  383. for (int index = 3; index < data.length; index++) {
  384. // Build the font orientation record
  385. fnoData[position] = data[index];
  386. position++;
  387. if (position == 26) {
  388. position = 0;
  389. int orientation = determineOrientation(fnoData[2]);
  390. // Space Increment
  391. int space = ((fnoData[8] & 0xFF ) << 8) + (fnoData[9] & 0xFF);
  392. // Em-Space Increment
  393. int em = ((fnoData[14] & 0xFF ) << 8) + (fnoData[15] & 0xFF);
  394. CharacterSetOrientation cso = new CharacterSetOrientation(orientation);
  395. cso.setSpaceIncrement(space);
  396. cso.setEmSpaceIncrement(em);
  397. orientations.add(cso);
  398. }
  399. }
  400. return orientations.toArray(EMPTY_CSO_ARRAY);
  401. }
  402. /**
  403. * Populate the CharacterSetOrientation object in the suplied array with the
  404. * font position details using the supplied structured field reader.
  405. *
  406. * @param structuredFieldReader
  407. * the structured field reader
  408. * @param characterSetOrientations
  409. * the array of CharacterSetOrientation objects
  410. * @param metricNormalizationFactor factor to apply to the metrics to get normalized
  411. * font metric values
  412. * @throws IOException if an I/O exception of some sort has occurred.
  413. */
  414. private void processFontPosition(StructuredFieldReader structuredFieldReader,
  415. CharacterSetOrientation[] characterSetOrientations, double metricNormalizationFactor)
  416. throws IOException {
  417. byte[] data = structuredFieldReader.getNext(FONT_POSITION_SF);
  418. int position = 0;
  419. byte[] fpData = new byte[26];
  420. int characterSetOrientationIndex = 0;
  421. // Read data, ignoring bytes 0 - 2
  422. for (int index = 3; index < data.length; index++) {
  423. if (position < 22) {
  424. // Build the font orientation record
  425. fpData[position] = data[index];
  426. if (position == 9) {
  427. CharacterSetOrientation characterSetOrientation
  428. = characterSetOrientations[characterSetOrientationIndex];
  429. int xHeight = getSBIN(fpData, 2);
  430. int capHeight = getSBIN(fpData, 4);
  431. int ascHeight = getSBIN(fpData, 6);
  432. int dscHeight = getSBIN(fpData, 8);
  433. dscHeight = dscHeight * -1;
  434. characterSetOrientation.setXHeight(
  435. (int)Math.round(xHeight * metricNormalizationFactor));
  436. characterSetOrientation.setCapHeight(
  437. (int)Math.round(capHeight * metricNormalizationFactor));
  438. characterSetOrientation.setAscender(
  439. (int)Math.round(ascHeight * metricNormalizationFactor));
  440. characterSetOrientation.setDescender(
  441. (int)Math.round(dscHeight * metricNormalizationFactor));
  442. }
  443. } else if (position == 22) {
  444. position = 0;
  445. characterSetOrientationIndex++;
  446. fpData[position] = data[index];
  447. }
  448. position++;
  449. }
  450. }
  451. /**
  452. * Process the font index details for the character set orientation.
  453. *
  454. * @param structuredFieldReader the structured field reader
  455. * @param cso the CharacterSetOrientation object to populate
  456. * @param codepage the map of code pages
  457. * @param metricNormalizationFactor factor to apply to the metrics to get normalized
  458. * font metric values
  459. * @throws IOException if an I/O exception of some sort has occurred.
  460. */
  461. private void processFontIndex(StructuredFieldReader structuredFieldReader,
  462. CharacterSetOrientation cso, Map<String, String> codepage,
  463. double metricNormalizationFactor)
  464. throws IOException {
  465. byte[] data = structuredFieldReader.getNext(FONT_INDEX_SF);
  466. int position = 0;
  467. byte[] gcgid = new byte[8];
  468. byte[] fiData = new byte[20];
  469. char lowest = 255;
  470. char highest = 0;
  471. String firstABCMismatch = null;
  472. // Read data, ignoring bytes 0 - 2
  473. for (int index = 3; index < data.length; index++) {
  474. if (position < 8) {
  475. gcgid[position] = data[index];
  476. position++;
  477. } else if (position < 27) {
  478. fiData[position - 8] = data[index];
  479. position++;
  480. } else if (position == 27) {
  481. fiData[position - 8] = data[index];
  482. position = 0;
  483. String gcgiString = new String(gcgid, AFPConstants.EBCIDIC_ENCODING);
  484. String idx = codepage.get(gcgiString);
  485. if (idx != null) {
  486. char cidx = idx.charAt(0);
  487. int width = getUBIN(fiData, 0);
  488. int a = getSBIN(fiData, 10);
  489. int b = getUBIN(fiData, 12);
  490. int c = getSBIN(fiData, 14);
  491. int abc = a + b + c;
  492. int diff = Math.abs(abc - width);
  493. if (diff != 0 && width != 0) {
  494. double diffPercent = 100 * diff / (double)width;
  495. if (diffPercent > 2) {
  496. if (LOG.isTraceEnabled()) {
  497. LOG.trace(gcgiString + ": "
  498. + a + " + " + b + " + " + c + " = " + (a + b + c)
  499. + " but found: " + width);
  500. }
  501. if (firstABCMismatch == null) {
  502. firstABCMismatch = gcgiString;
  503. }
  504. }
  505. }
  506. if (cidx < lowest) {
  507. lowest = cidx;
  508. }
  509. if (cidx > highest) {
  510. highest = cidx;
  511. }
  512. int normalizedWidth = (int)Math.round(width * metricNormalizationFactor);
  513. cso.setWidth(cidx, normalizedWidth);
  514. }
  515. }
  516. }
  517. cso.setFirstChar(lowest);
  518. cso.setLastChar(highest);
  519. if (LOG.isDebugEnabled() && firstABCMismatch != null) {
  520. //Debug level because it usually is no problem.
  521. LOG.debug("Font has metrics inconsitencies where A+B+C doesn't equal the"
  522. + " character increment. The first such character found: "
  523. + firstABCMismatch);
  524. }
  525. }
  526. private static int getUBIN(byte[] data, int start) {
  527. return ((data[start] & 0xFF) << 8) + (data[start + 1] & 0xFF);
  528. }
  529. private static int getSBIN(byte[] data, int start) {
  530. int ubin = ((data[start] & 0xFF) << 8) + (data[start + 1] & 0xFF);
  531. if ((ubin & 0x8000) != 0) {
  532. //extend sign
  533. return ubin | 0xFFFF0000;
  534. } else {
  535. return ubin;
  536. }
  537. }
  538. private class FontControl {
  539. private int dpi;
  540. private int unitsPerEm;
  541. private boolean isRelative = false;
  542. public int getDpi() {
  543. return dpi;
  544. }
  545. public void setDpi(int i) {
  546. dpi = i;
  547. }
  548. public int getUnitsPerEm() {
  549. return this.unitsPerEm;
  550. }
  551. public void setUnitsPerEm(int value) {
  552. this.unitsPerEm = value;
  553. }
  554. public boolean isRelative() {
  555. return isRelative;
  556. }
  557. public void setRelative(boolean b) {
  558. isRelative = b;
  559. }
  560. }
  561. private static class FontDescriptor {
  562. private byte[] data;
  563. public FontDescriptor(byte[] data) {
  564. this.data = data;
  565. }
  566. public int getNominalFontSizeInMillipoints() {
  567. int nominalFontSize = 100 * getUBIN(data, 39);
  568. return nominalFontSize;
  569. }
  570. }
  571. private static final class SingleByteLoader extends CharacterSetBuilder {
  572. private static final SingleByteLoader INSTANCE = new SingleByteLoader();
  573. private SingleByteLoader() {
  574. super();
  575. }
  576. private static SingleByteLoader getInstance() {
  577. return INSTANCE;
  578. }
  579. }
  580. /**
  581. * Double-byte (CID Keyed font (Type 0)) implementation of AFPFontReader.
  582. */
  583. private static final class DoubleByteLoader extends CharacterSetBuilder {
  584. private static final DoubleByteLoader INSTANCE = new DoubleByteLoader();
  585. private DoubleByteLoader() {
  586. }
  587. static DoubleByteLoader getInstance() {
  588. return INSTANCE;
  589. }
  590. @Override
  591. protected Map<String, String> loadCodePage(String codePage, String encoding,
  592. AFPResourceAccessor accessor, AFPEventProducer eventProducer) throws IOException {
  593. // Create the HashMap to store code page information
  594. Map<String, String> codePages = new HashMap<String, String>();
  595. InputStream inputStream = null;
  596. try {
  597. inputStream = super.openInputStream(accessor, codePage.trim(), eventProducer);
  598. } catch (IOException e) {
  599. eventProducer.codePageNotFound(this, e);
  600. throw e;
  601. }
  602. try {
  603. StructuredFieldReader structuredFieldReader = new StructuredFieldReader(inputStream);
  604. byte[] data;
  605. while ((data = structuredFieldReader.getNext(CHARACTER_TABLE_SF)) != null) {
  606. int position = 0;
  607. byte[] gcgiBytes = new byte[8];
  608. byte[] charBytes = new byte[2];
  609. // Read data, ignoring bytes 0 - 2
  610. for (int index = 3; index < data.length; index++) {
  611. if (position < 8) {
  612. // Build the graphic character global identifier key
  613. gcgiBytes[position] = data[index];
  614. position++;
  615. } else if (position == 9) {
  616. // Set the character
  617. charBytes[0] = data[index];
  618. position++;
  619. } else if (position == 10) {
  620. position = 0;
  621. // Set the character
  622. charBytes[1] = data[index];
  623. String gcgiString = new String(gcgiBytes,
  624. AFPConstants.EBCIDIC_ENCODING);
  625. String charString = new String(charBytes, encoding);
  626. codePages.put(gcgiString, charString);
  627. } else {
  628. position++;
  629. }
  630. }
  631. }
  632. } finally {
  633. super.closeInputStream(inputStream);
  634. }
  635. return codePages;
  636. }
  637. }
  638. private static int determineOrientation(byte orientation) {
  639. int degrees = 0;
  640. switch (orientation) {
  641. case 0x00:
  642. degrees = 0;
  643. break;
  644. case 0x2D:
  645. degrees = 90;
  646. break;
  647. case 0x5A:
  648. degrees = 180;
  649. break;
  650. case (byte) 0x87:
  651. degrees = 270;
  652. break;
  653. default:
  654. throw new IllegalStateException("Invalid orientation: " + orientation);
  655. }
  656. return degrees;
  657. }
  658. }