All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mabb.fontverter.woff.WoffFont Maven / Gradle / Ivy

There is a newer version: 1.2.22
Show newest version
/*
 * Copyright (C) Maddie Abboud 2016
 *
 * FontVerter is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * FontVerter is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with FontVerter. If not, see .
 */

package org.mabb.fontverter.woff;

import org.mabb.fontverter.io.FontDataOutputStream;
import org.mabb.fontverter.FVFont;
import org.mabb.fontverter.validator.RuleValidator;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public abstract class WoffFont implements FVFont {
    protected WoffHeader header;
    protected List tables = new ArrayList();
    protected List fonts = new ArrayList();

    public static WoffFont createBlankFont(int version) {
        WoffFont font;

        if (version == 1) {
            font = new Woff1Font();
            font.header = WoffHeader.createWoff1Header();
        } else {
            font = new Woff2Font();
            font.header = WoffHeader.createWoff2Header();
        }

        return font;
    }

    public WoffFont() {
    }

    public abstract WoffTable createTable();

    public abstract void addFontTable(byte[] unpaddedData, String tag, long checksum);

    public byte[] getData() throws IOException {
        // have to write out data twice for header calcs
        header.calculateValues(this);
        return getRawData();
    }

    byte[] getRawData() throws IOException {
        FontDataOutputStream out = new FontDataOutputStream(FontDataOutputStream.OPEN_TYPE_CHARSET);

        Collections.sort(tables, new Comparator() {
            public int compare(WoffTable o1, WoffTable o2) {
                String c1 = o1.getTag();
                String c2 = o2.getTag();
                return c1.compareTo(c2);
            }
        });

        out.write(header.getData());
        out.write(getTableDirectoryData());
        out.write(getCompressedDataBlock());

        return out.toByteArray();
    }

    byte[] getTableDirectoryData() throws IOException {
        FontDataOutputStream writer = new FontDataOutputStream(FontDataOutputStream.OPEN_TYPE_CHARSET);
        for (WoffTable tableOn : tables)
            writer.write(tableOn.getDirectoryData());

        return writer.toByteArray();
    }

    byte[] getCompressedDataBlock() throws IOException {
        FontDataOutputStream writer = new FontDataOutputStream(FontDataOutputStream.OPEN_TYPE_CHARSET);
        for (WoffTable tableOn : tables)
            writer.write(tableOn.getCompressedData());

        return writer.toByteArray();
    }

    public void addFont(FVFont adapter) {
        fonts.add(adapter);
    }

    public List getFonts() {
        return fonts;
    }

    public List getTables() {
        return tables;
    }

    public int getCompressedSize() throws IOException {
        return getCompressedDataBlock().length;
    }

    public String getName() {
        if (fonts.size() == 0)
            return "Unknown Font Name";

        return fonts.get(0).getName();
    }

    public void normalize() {
    }

    public boolean isValid() {
        return true;
    }

    public List getValidationErrors() {
        return new ArrayList();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy