FontHelper.java 9.67 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.commons.osgi.OsgiUtil
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.apache.sling.jcr.resource.JcrResourceResolverFactory
 *  org.osgi.service.component.ComponentContext
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.image.internal.font;

import com.day.image.font.AbstractFont;
import com.day.image.font.FontListEntry;
import com.day.image.internal.font.FontFileProvider;
import com.day.image.internal.font.FontProvider;
import com.day.image.internal.font.PlatformFont;
import com.day.image.internal.font.resource.ResourceFontProvider;
import java.awt.GraphicsEnvironment;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Dictionary;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
@Component(name="com.day.cq.image.internal.font.FontHelper", metatype=1, label="Day Commons GFX Font Helper", description="Configures the GFX Font Helper which helps managing the font path and caching of font information.")
public class FontHelper {
    private static FontHelper INSTANCE;
    private final Logger log;
    @Property(cardinality=Integer.MAX_VALUE, label="Font Path", description="The list of Resource Tree locations providing fonts. Multiple entries may be listed. Each entry must be an absolute path; that is empty entries or entries not starting with a slash (/) character are ignored. The location of each entry must be existing. This may cause the creation of JCR Nodes at configured entry paths.")
    private static final String FONTPATH = "fontpath";
    private static final int DEFAULT_OVERSAMPLING_FACTOR = 16;
    @Property(intValue={16}, label="Oversampling Factor", description="The factor to apply internally to the font when drawing text with the TTOVERSAMPLING flag set. Default value is 16 which is also the value used by the Fonter.exe tool to prepare FonterFonts.")
    private static final String OVERSAMPLING_FACTOR = "oversamplingFactor";
    @Reference
    private SlingRepository slingRepository;
    @Reference
    private JcrResourceResolverFactory resourceResolverFactory;
    @Reference
    private FontFileProvider fontFileProvider;
    private ResourceResolver resourceResolver;
    private String[] fontPath;
    private FontProvider[] fontProviders;

    public FontHelper() {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.fontPath = new String[0];
        this.fontProviders = new FontProvider[0];
    }

    protected void activate(ComponentContext context) {
        try {
            Session session = this.slingRepository.loginAdministrative(null);
            this.resourceResolver = this.resourceResolverFactory.getResourceResolver(session);
        }
        catch (RepositoryException re) {
            this.log.error("activate: Cannot get administrative session to access fonts !", (Throwable)re);
        }
        Dictionary props = context.getProperties();
        this.initFontPath(OsgiUtil.toStringArray(props.get("fontpath")));
        PlatformFont.setOversamplingFactor(OsgiUtil.toInteger(props.get("oversamplingFactor"), (int)16));
        INSTANCE = this;
        if (this.resourceResolver != null) {
            this.addFontProvider(new ResourceFontProvider());
        }
    }

    protected void deactivate(ComponentContext context) {
        for (FontProvider fontProvider : this.fontProviders) {
            fontProvider.destroy();
        }
        if (INSTANCE == this) {
            INSTANCE = null;
        }
        if (this.resourceResolver != null) {
            Session session = (Session)this.resourceResolver.adaptTo(Session.class);
            if (session != null) {
                session.logout();
            }
            this.resourceResolver = null;
        }
        this.fontProviders = new FontProvider[0];
        this.fontPath = new String[0];
    }

    protected void addFontProvider(FontProvider provider) {
        provider.init(this.resourceResolver, this.getFontPath(), this.fontFileProvider);
        if (this.fontProviders.length == 0) {
            this.fontProviders = new FontProvider[]{provider};
        } else {
            FontProvider[] newfp = new FontProvider[this.fontProviders.length + 1];
            System.arraycopy(this.fontProviders, 0, newfp, 0, this.fontProviders.length);
            newfp[this.fontProviders.length] = provider;
            this.fontProviders = newfp;
        }
    }

    protected void removeFontProvider(FontProvider provider) {
        for (int i = 0; i < this.fontProviders.length; ++i) {
            if (this.fontProviders[i] != provider) continue;
            provider.destroy();
            if (this.fontProviders.length == 1) {
                this.fontProviders = new FontProvider[0];
                continue;
            }
            FontProvider[] newfp = new FontProvider[this.fontProviders.length - 1];
            if (i > 0) {
                System.arraycopy(this.fontProviders, 0, newfp, 0, i);
            }
            if (i >= this.fontProviders.length - 1) continue;
            System.arraycopy(this.fontProviders, i + 1, newfp, i, this.fontProviders.length - i);
        }
    }

    public static FontHelper getInstance() {
        if (INSTANCE == null) {
            return new FontHelper();
        }
        return INSTANCE;
    }

    String[] getFontPath() {
        return this.fontPath;
    }

    FontProvider[] getFontProviders() {
        return this.fontProviders;
    }

    public List<FontListEntry> getFontList() {
        ArrayList<FontListEntry> fontList = new ArrayList<FontListEntry>();
        for (FontProvider fontProvider : this.getFontProviders()) {
            fontList.addAll(fontProvider.getFontList());
        }
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        for (String platFont : env.getAvailableFontFamilyNames()) {
            fontList.add(new FontListEntry("[Java Platform]", platFont, 0, 255));
        }
        return fontList;
    }

    public AbstractFont getFont(String faceName, int size, int style) {
        AbstractFont fnt = null;
        for (int i = 0; i < this.fontProviders.length; ++i) {
            this.log.debug("Font: Asking provider {}", (Object)this.fontProviders[i]);
            fnt = this.fontProviders[i].getFont(faceName, size, style);
            if (fnt == null) continue;
            this.log.debug("Font: Using font {}", (Object)fnt);
            break;
        }
        if (fnt == null) {
            this.log.debug("Font: No provider has font, fallback to base class");
            fnt = new PlatformFont(faceName, size, style);
        }
        return fnt;
    }

    private void initFontPath(String[] fontPathConfig) {
        if (fontPathConfig != null && fontPathConfig.length > 0) {
            ArrayList<String> fontPathList = new ArrayList<String>();
            for (String fontPathEntry2 : fontPathConfig) {
                if (fontPathEntry2 == null || fontPathEntry2.length() == 0) {
                    this.log.info("initFontPath: Ignoring empty font path entry");
                    continue;
                }
                if (fontPathEntry2.charAt(0) != '/') {
                    this.log.info("initFontPath: Ignoring non-absolute font path entry {}", (Object)fontPathEntry2);
                    continue;
                }
                fontPathList.add(fontPathEntry2);
            }
            this.fontPath = fontPathList.toArray(new String[fontPathList.size()]);
            if (this.log.isDebugEnabled()) {
                this.log.debug("Communique font settings:");
                for (String fontPathEntry2 : this.fontPath) {
                    this.log.debug("  Path entry {}", (Object)fontPathEntry2);
                }
            }
        } else {
            this.log.debug("initFontPath: Font path not configured");
            this.fontPath = new String[0];
        }
    }

    protected void bindSlingRepository(SlingRepository slingRepository) {
        this.slingRepository = slingRepository;
    }

    protected void unbindSlingRepository(SlingRepository slingRepository) {
        if (this.slingRepository == slingRepository) {
            this.slingRepository = null;
        }
    }

    protected void bindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        this.resourceResolverFactory = jcrResourceResolverFactory;
    }

    protected void unbindResourceResolverFactory(JcrResourceResolverFactory jcrResourceResolverFactory) {
        if (this.resourceResolverFactory == jcrResourceResolverFactory) {
            this.resourceResolverFactory = null;
        }
    }

    protected void bindFontFileProvider(FontFileProvider fontFileProvider) {
        this.fontFileProvider = fontFileProvider;
    }

    protected void unbindFontFileProvider(FontFileProvider fontFileProvider) {
        if (this.fontFileProvider == fontFileProvider) {
            this.fontFileProvider = null;
        }
    }
}