如何优化使用Apache Batik生成的SVG图像的权重?

时间:2015-12-11 15:05:46

标签: java svg batik

我使用Apache Batik编写了以下代码,以生成表示代码是否通过SonarQube质量门的徽章的SVG图像(full source available on GitHub):

private SVGGraphics2D generateFor(final QualityGateStatus status) {
    // new SVG graphics
    final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(this.svgGeneratorContext, false);
    // set SVG canvas size
    svgGraphics2D.setSVGCanvasSize(new Dimension(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT));
    // set font
    svgGraphics2D.setFont(FONT_FAMILY);
    // draw Label background
    svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
    svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
    svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
    // draw Label text shadow
    svgGraphics2D.setColor(COLOR_SHADOW);
    svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
    // draw Label text
    svgGraphics2D.setColor(COLOR_TEXT);
    svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
    // draw result background
    svgGraphics2D.setColor(status.displayBackgroundColor());
    svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
    svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
    // draw result text shadow
    svgGraphics2D.setColor(COLOR_SHADOW);
    svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
    // draw result text
    svgGraphics2D.setColor(COLOR_TEXT);
    svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
    return svgGraphics2D;
}

生成的图像示例(取决于传递给generateFor()方法的参数)可以在GitHub as well上看到。

此代码工作正常,但目前生成的图像的权重大约为2KB,几乎是重量大约700字节的travis-ci徽章的3倍。因此,我觉得生成的图像可以简化,大小可以减少到大约1KB。

如何使用Apache Batik实现这一目标?我应该使用其他图书馆还是使用其他方法?

提前感谢您的见解和建议!

1 个答案:

答案 0 :(得分:2)

我尝试使用JFreeSVG 3.0生成SVG,并使用GZIP选项创建大约570字节的文件。在纯文本中,SVG大约为1.7千字节。

以下是我运行的测试程序(根据您的代码,您还需要运行此QualityGateStatus类):

/*
 * qualinsight-plugins-sonarqube-status
 * Copyright (c) 2015, QualInsight
 * http://www.qualinsight.com/
 *
 * This program 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.
 *
 * This program 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 this program. If not, you can retrieve a copy
 * from <http://www.gnu.org/licenses/>.
 */

import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;

import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;

public final class SVGImageGenerator {

    private static final int FONT_SIZE = 11;

    private static final Font FONT_FAMILY = new Font("Verdana", Font.PLAIN, FONT_SIZE);

    private static final int X_MARGIN = 4;

    private static final int CANVAS_HEIGHT = 20;

    private static final int LABEL_WIDTH = 46;

    private static final String LABEL_TEXT = "quality";

    private static final int BACKGROUND_CORNER_ARC_DIAMETER = 6;

    private static final Color COLOR_BACKGROUND_LABEL = new Color(85, 85, 85, 255);

    private static final Color COLOR_SHADOW = new Color(0, 0, 0, 85);

    private static final Color COLOR_TEXT = new Color(255, 255, 255, 255);

    private static final int Y_OFFSET_SHADOW = 14;

    private static final int Y_OFFSET_TEXT = 14;


    private static String generateFor(final QualityGateStatus status) {
        // new SVG graphics
        final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT);
        // set font
        svgGraphics2D.setFont(FONT_FAMILY);
        // draw Label background
        svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
        svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
        svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
        // draw Label text shadow
        svgGraphics2D.setColor(COLOR_SHADOW);
        svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
        // draw Label text
        svgGraphics2D.setColor(COLOR_TEXT);
        svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
        // draw result background
        svgGraphics2D.setColor(status.displayBackgroundColor());
        svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
        svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
        // draw result text shadow
        svgGraphics2D.setColor(COLOR_SHADOW);
        svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
        // draw result text
        svgGraphics2D.setColor(COLOR_TEXT);
        svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
        return svgGraphics2D.getSVGElement();
    }

    public static void main(String[] args) {
        try {
            SVGUtils.writeToSVG(new File("error.svgz"), generateFor(QualityGateStatus.ERROR), true);
            SVGUtils.writeToSVG(new File("none.svgz"), generateFor(QualityGateStatus.NONE), true);
            SVGUtils.writeToSVG(new File("ok.svgz"), generateFor(QualityGateStatus.OK), true);
            SVGUtils.writeToSVG(new File("server_error.svgz"), generateFor(QualityGateStatus.SERVER_ERROR), true);
            SVGUtils.writeToSVG(new File("warn.svgz"), generateFor(QualityGateStatus.WARN), true);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}