net.padaf.preflight.font.FontValidatorFactory.java Source code

Java tutorial

Introduction

Here is the source code for net.padaf.preflight.font.FontValidatorFactory.java

Source

/*******************************************************************************
 * Copyright 2010 Atos Worldline SAS
 * 
 * Licensed by Atos Worldline SAS under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * Atos Worldline SAS 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.
 ******************************************************************************/
package net.padaf.preflight.font;

import static net.padaf.preflight.ValidationConstants.DICTIONARY_KEY_SUBTYPE;
import static net.padaf.preflight.ValidationConstants.DICTIONARY_KEY_TYPE;
import static net.padaf.preflight.ValidationConstants.ERROR_FONTS_DICTIONARY_INVALID;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_COMPOSITE;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_MMTYPE;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TRUETYPE;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE0;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE0C;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE1;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE1C;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE2;
import static net.padaf.preflight.ValidationConstants.FONT_DICTIONARY_VALUE_TYPE3;
import net.padaf.preflight.DocumentHandler;
import net.padaf.preflight.ValidationException;

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSObject;

/**
 * This class returns a FontValidator object according to the Font dictionary.
 */
public class FontValidatorFactory {

    public FontValidator getFontValidator(COSObject cObj, DocumentHandler handler) throws ValidationException {
        COSDictionary dic = (COSDictionary) cObj.getObject();
        String type = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_TYPE));
        String subtype = dic.getNameAsString(COSName.getPDFName(DICTIONARY_KEY_SUBTYPE));

        if ((type == null || "".equals(type)) || (subtype == null || "".equals(subtype))) {
            throw new ValidationException(
                    "Type and/or Subtype keys are missing : " + ERROR_FONTS_DICTIONARY_INVALID);
        } else {
            if (FONT_DICTIONARY_VALUE_TRUETYPE.equals(subtype)) {
                return new TrueTypeFontValidator(handler, cObj);
            } else if (FONT_DICTIONARY_VALUE_MMTYPE.equals(subtype)
                    || FONT_DICTIONARY_VALUE_TYPE1.equals(subtype)) {
                return new Type1FontValidator(handler, cObj);
            } else if (FONT_DICTIONARY_VALUE_TYPE3.equals(subtype)) {
                return new Type3FontValidator(handler, cObj);
            } else if (FONT_DICTIONARY_VALUE_COMPOSITE.equals(subtype)) {
                return new CompositeFontValidator(handler, cObj);
            } else if (FONT_DICTIONARY_VALUE_TYPE2.equals(subtype) || FONT_DICTIONARY_VALUE_TYPE1C.equals(subtype)
                    || FONT_DICTIONARY_VALUE_TYPE0C.equals(subtype)
                    || FONT_DICTIONARY_VALUE_TYPE0.equals(subtype)) {
                // ---- Font managed by a Composite font.
                // this dictionary will be checked by a CompositeFontValidator
                return null;
            } else {
                throw new ValidationException("Unknown font type : " + subtype);
            }
        }
    }
}