123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
- /* $Id$ */
-
- package org.apache.fop.fonts.type1;
-
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
-
- import org.apache.commons.io.IOUtils;
-
- import org.apache.fop.fonts.CodePointMapping;
- import org.apache.fop.fonts.FontLoader;
- import org.apache.fop.fonts.FontResolver;
- import org.apache.fop.fonts.FontType;
- import org.apache.fop.fonts.SingleByteFont;
-
- /**
- * Loads a Type 1 font into memory directly from the original font file.
- */
- public class Type1FontLoader extends FontLoader {
-
- private SingleByteFont singleFont;
-
- /**
- * Constructs a new Type 1 font loader.
- * @param fontFileURI the URI to the PFB file of a Type 1 font
- * @param resolver the font resolver used to resolve URIs
- * @throws IOException In case of an I/O error
- */
- public Type1FontLoader(String fontFileURI, FontResolver resolver)
- throws IOException {
- super(fontFileURI, resolver);
- }
-
- private String getPFMURI(String pfbURI) {
- String pfbExt = pfbURI.substring(pfbURI.length() - 3, pfbURI.length());
- String pfmExt = pfbExt.substring(0, 2)
- + (Character.isUpperCase(pfbExt.charAt(2)) ? "M" : "m");
- return pfbURI.substring(0, pfbURI.length() - 4) + "." + pfmExt;
- }
-
- private static final String[] AFM_EXTENSIONS = new String[] {".AFM", ".afm", ".Afm"};
-
- /** {@inheritDoc} */
- protected void read() throws IOException {
- AFMFile afm = null;
- PFMFile pfm = null;
-
- InputStream afmIn = null;
- for (int i = 0; i < AFM_EXTENSIONS.length; i++) {
- try {
- String afmUri = this.fontFileURI.substring(0, this.fontFileURI.length() - 4)
- + AFM_EXTENSIONS[i];
- afmIn = openFontUri(resolver, afmUri);
- if (afmIn != null) {
- break;
- }
- } catch (IOException ioe) {
- //Ignore, AFM probably not available under the URI
- }
- }
- if (afmIn != null) {
- try {
- AFMParser afmParser = new AFMParser();
- afm = afmParser.parse(afmIn);
- } finally {
- IOUtils.closeQuietly(afmIn);
- }
- }
-
- String pfmUri = getPFMURI(this.fontFileURI);
- InputStream pfmIn = null;
- try {
- pfmIn = openFontUri(resolver, pfmUri);
- } catch (IOException ioe) {
- //Ignore, PFM probably not available under the URI
- }
- if (pfmIn != null) {
- try {
- pfm = new PFMFile();
- pfm.load(pfmIn);
- } finally {
- IOUtils.closeQuietly(pfmIn);
- }
- }
-
- if (afm == null && pfm == null) {
- throw new java.io.FileNotFoundException(
- "Neither an AFM nor a PFM file was found for " + this.fontFileURI);
- }
- if (pfm == null) {
- //Cannot do without for now
- throw new java.io.FileNotFoundException(
- "No PFM file was found for " + this.fontFileURI);
- }
- buildFont(afm, pfm);
- this.loaded = true;
- }
-
- private void buildFont(AFMFile afm, PFMFile pfm) {
- if (afm == null && pfm == null) {
- throw new IllegalArgumentException("Need at least an AFM or a PFM!");
- }
- singleFont = new SingleByteFont();
- singleFont.setFontType(FontType.TYPE1);
- if (pfm.getCharSet() >= 0 && pfm.getCharSet() <= 2) {
- singleFont.setEncoding(pfm.getCharSetName() + "Encoding");
- } else {
- log.warn("The PFM reports an unsupported encoding ("
- + pfm.getCharSetName() + "). The font may not work as expected.");
- singleFont.setEncoding("WinAnsiEncoding"); //Try fallback, no guarantees!
- }
- singleFont.setResolver(this.resolver);
- returnFont = singleFont;
-
- //Font name
- if (afm != null) {
- returnFont.setFontName(afm.getFontName()); //PostScript font name
- returnFont.setFullName(afm.getFullName());
- Set names = new java.util.HashSet();
- names.add(afm.getFamilyName());
- returnFont.setFamilyNames(names);
- } else {
- returnFont.setFontName(pfm.getPostscriptName());
- String fullName = pfm.getPostscriptName();
- fullName = fullName.replace('-', ' '); //Hack! Try to emulate full name
- returnFont.setFullName(fullName); //emulate afm.getFullName()
- Set names = new java.util.HashSet();
- names.add(pfm.getWindowsName()); //emulate afm.getFamilyName()
- returnFont.setFamilyNames(names);
- }
-
- //Encoding
- if (afm != null) {
- String encoding = afm.getEncodingScheme();
- if ("AdobeStandardEncoding".equals(encoding)) {
- //Use WinAnsi in this case as it better fits the usual character set people need
- singleFont.setEncoding(CodePointMapping.WIN_ANSI_ENCODING);
- } else {
- String effEncodingName;
- if ("FontSpecific".equals(encoding)) {
- effEncodingName = afm.getFontName() + "Encoding";
- } else {
- effEncodingName = encoding;
- }
- if (log.isDebugEnabled()) {
- log.debug("Unusual font encoding encountered: "
- + encoding + " -> " + effEncodingName);
- }
- CodePointMapping mapping = buildCustomEncoding(effEncodingName, afm);
- singleFont.setEncoding(mapping);
- }
- }
-
- //Basic metrics
- if (afm != null) {
- if (afm.getCapHeight() != null) {
- returnFont.setCapHeight(afm.getCapHeight().intValue());
- }
- if (afm.getXHeight() != null) {
- returnFont.setXHeight(afm.getXHeight().intValue());
- }
- if (afm.getAscender() != null) {
- returnFont.setAscender(afm.getAscender().intValue());
- }
- if (afm.getDescender() != null) {
- returnFont.setDescender(afm.getDescender().intValue());
- }
- returnFont.setFontBBox(afm.getFontBBoxAsIntArray());
- if (afm.getStdVW() != null) {
- returnFont.setStemV(afm.getStdVW().intValue());
- } else {
- returnFont.setStemV(80); //Arbitrary value
- }
- returnFont.setItalicAngle((int)afm.getWritingDirectionMetrics(0).getItalicAngle());
- } else {
- returnFont.setFontBBox(pfm.getFontBBox());
- returnFont.setStemV(pfm.getStemV());
- returnFont.setItalicAngle(pfm.getItalicAngle());
- }
- if (pfm != null) {
- if (returnFont.getCapHeight() == 0) {
- returnFont.setCapHeight(pfm.getCapHeight());
- }
- if (returnFont.getXHeight(1) == 0) {
- returnFont.setXHeight(pfm.getXHeight());
- }
- if (returnFont.getAscender() == 0) {
- returnFont.setAscender(pfm.getLowerCaseAscent());
- }
- if (returnFont.getDescender() == 0) {
- returnFont.setDescender(pfm.getLowerCaseDescent());
- }
- }
- returnFont.setFirstChar(pfm.getFirstChar());
- returnFont.setLastChar(pfm.getLastChar());
- returnFont.setFlags(pfm.getFlags());
- returnFont.setMissingWidth(0);
- for (short i = pfm.getFirstChar(); i <= pfm.getLastChar(); i++) {
- singleFont.setWidth(i, pfm.getCharWidth(i));
- }
- returnFont.replaceKerningMap(pfm.getKerning());
- singleFont.setEmbedFileName(this.fontFileURI);
- }
-
- private CodePointMapping buildCustomEncoding(String encodingName, AFMFile afm) {
- List chars = afm.getCharMetrics();
- int mappingCount = 0;
- //Just count the first time...
- Iterator iter = chars.iterator();
- while (iter.hasNext()) {
- AFMCharMetrics charMetrics = (AFMCharMetrics)iter.next();
- if (charMetrics.getCharCode() >= 0) {
- String u = charMetrics.getUnicodeChars();
- if (u != null) {
- mappingCount += u.length();
- }
- }
- }
- //...and now build the table.
- int[] table = new int[mappingCount * 2];
- iter = chars.iterator();
- int idx = 0;
- while (iter.hasNext()) {
- AFMCharMetrics charMetrics = (AFMCharMetrics)iter.next();
- if (charMetrics.getCharCode() >= 0) {
- String unicodes = charMetrics.getUnicodeChars();
- for (int i = 0, c = unicodes.length(); i < c; i++) {
- table[idx] = charMetrics.getCharCode();
- idx++;
- table[idx] = unicodes.charAt(i);
- idx++;
- }
- }
- }
- return new CodePointMapping(encodingName, table);
- }
- }
|