FontServiceImpl.java 4.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.guide.dor.FontService
 *  com.adobe.fontengine.font.Font
 *  com.adobe.fontengine.font.FontLoadingException
 *  com.adobe.fontengine.font.InvalidFontException
 *  com.adobe.fontengine.font.PDFFontDescription
 *  com.adobe.fontengine.font.UnsupportedFontException
 *  com.adobe.fontengine.fontmanagement.FontLoader
 *  com.day.cq.dam.handler.gibson.fontmanager.FontManagerService
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.ReferenceCardinality
 *  org.apache.felix.scr.annotations.ReferencePolicy
 *  org.apache.felix.scr.annotations.Service
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.guide.addon.dor.service.impl;

import com.adobe.aemds.guide.dor.FontService;
import com.adobe.fontengine.font.Font;
import com.adobe.fontengine.font.FontLoadingException;
import com.adobe.fontengine.font.InvalidFontException;
import com.adobe.fontengine.font.PDFFontDescription;
import com.adobe.fontengine.font.UnsupportedFontException;
import com.adobe.fontengine.fontmanagement.FontLoader;
import com.day.cq.dam.handler.gibson.fontmanager.FontManagerService;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={FontService.class})
public class FontServiceImpl
implements FontService {
    private static final Logger logger = LoggerFactory.getLogger(FontServiceImpl.class);
    @Reference(policy=ReferencePolicy.DYNAMIC, cardinality=ReferenceCardinality.OPTIONAL_UNARY)
    FontManagerService fontManagerService;

    public ArrayList<String> getFontList() {
        if (this.fontManagerService == null) {
            logger.error("FontManagerService not found");
            return new ArrayList<String>(0);
        }
        return this.getAllAvailableFonts();
    }

    private ArrayList<String> getAllAvailableFonts() {
        String[] directories;
        ArrayList<String> fontList = new ArrayList<String>();
        String systemFontDirectoryPath = this.fontManagerService.getSystemFontDirectory();
        String customerFontDirectoryPath = this.fontManagerService.getCustomerFontDirectory();
        String adobeServerFontDirectoryPath = this.fontManagerService.getAdobeServerFontDirectory();
        for (String directory : directories = new String[]{systemFontDirectoryPath, customerFontDirectoryPath, adobeServerFontDirectoryPath}) {
            if (!StringUtils.isNotEmpty((CharSequence)directory)) continue;
            File fontDirectory = new File(directory);
            fontList.addAll(this.loadFontInfoFromDirectory(fontDirectory));
        }
        return fontList;
    }

    private ArrayList<String> loadFontInfoFromDirectory(File fontDirectory) {
        Font[] fonts;
        ArrayList<String> fontList = new ArrayList<String>();
        FontLoader fontLoader = new FontLoader();
        for (Font font : fonts = fontLoader.load(fontDirectory, true, null)) {
            try {
                PDFFontDescription pdfFontDescription = font.getPDFFontDescription();
                fontList.add(pdfFontDescription.getFontFamily());
                continue;
            }
            catch (FontLoadingException | InvalidFontException | UnsupportedFontException e) {
                logger.error("Error while getting font", (Throwable)e);
            }
        }
        return fontList;
    }

    protected void bindFontManagerService(FontManagerService fontManagerService) {
        this.fontManagerService = fontManagerService;
    }

    protected void unbindFontManagerService(FontManagerService fontManagerService) {
        if (this.fontManagerService == fontManagerService) {
            this.fontManagerService = null;
        }
    }
}