<commit_before>/* ============================================================
 * JRobin : Pure java implementation of RRDTool's functionality
 * ============================================================
 *
 * Project Info:  http://www.jrobin.org
 * Project Lead:  Sasa Markovic (<EMAIL>)
 *
 * Developers:    <NAME> (<EMAIL>)
 *
 *
 * (C) Copyright 2003-2005, by <NAME>.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package org.jrobin.graph;

import org.jrobin.core.RrdException;
import org.jrobin.core.Util;
import org.jrobin.data.Plottable;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Class which should be used to define new JRobin graph. Once constructed and populated with data
 * object of this class should be passed to the constructor of the {@link RrdGraph} class which
 * will actually create the graph.
 * <p/>
 * The text printed below the actual graph can be formated by appending
 * special escaped characters at the end of a text. When ever such a
 * character occurs, all pending text is pushed onto the graph according to
 * the character specified.
 * <p/>
 * Valid markers are: \j for justified, \l for left aligned, \r for right
 * aligned and \c for centered.
 * <p/>
 * Normally there are two space characters inserted between every two
 * items printed into the graph. The space following a string can be
 * suppressed by putting a \g at the end of the string. The \g also squashes
 * any space inside the string if it is at the very end of the string.
 * This can be used in connection with %s to suppress empty unit strings.
 * <p/>
 * A special case is COMMENT:\s this inserts some additional vertical
 * space before placing the next row of legends.
 * <p/>
 * When text has to be formated without special instructions from your
 * side, RRDTool will automatically justify the text as soon as one string
 * goes over the right edge. If you want to prevent the justification
 * without forcing a newline, you can use the special tag \J at the end of
 * the string to disable the auto justification.
 */
public class RrdGraphDef implements RrdGraphConstants {
	boolean poolUsed = false; // ok
	boolean antiAliasing = false; // ok
	String filename = RrdGraphConstants.IN_MEMORY_IMAGE; // ok
	long startTime, endTime; // ok
	TimeAxisSetting timeAxisSetting = null; // ok
	ValueAxisSetting valueAxisSetting = null; // ok
	boolean altYGrid = false; // ok
	boolean noMinorGrid = false; // ok
	boolean altYMrtg = false; // ok
	boolean altAutoscale = false; // ok
	boolean altAutoscaleMax = false; // ok
	int unitsExponent = Integer.MAX_VALUE; // ok
	int unitsLength = DEFAULT_UNITS_LENGTH; // ok
	String verticalLabel = null; // ok
	int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; // ok
	boolean interlaced = false; // ok
	String imageInfo = null; // ok
	String imageFormat = DEFAULT_IMAGE_FORMAT; // ok
	float imageQuality = DEFAULT_IMAGE_QUALITY; // ok
	String backgroundImage = null; // ok
	String overlayImage = null; // ok
	String unit = null; // ok
	String signature = "Created with JRobin"; // ok
	boolean lazy = false; // ok
	double minValue = Double.NaN; // ok
	double maxValue = Double.NaN; // ok
	boolean rigid = false; // ok
	double base = DEFAULT_BASE;  // ok
	boolean logarithmic = false; // ok
	Paint[] colors = new Paint[] {
			// ok
			DEFAULT_CANVAS_COLOR,
			DEFAULT_BACK_COLOR,
			DEFAULT_SHADEA_COLOR,
			DEFAULT_SHADEB_COLOR,
			DEFAULT_GRID_COLOR,
			DEFAULT_MGRID_COLOR,
			DEFAULT_FONT_COLOR,
			DEFAULT_FRAME_COLOR,
			DEFAULT_ARROW_COLOR
	};
	boolean noLegend = false; // ok
	boolean onlyGraph = false; // ok
	boolean forceRulesLegend = false; // ok
	String title = null; // ok
	long step = 0; // ok
	Font smallFont;
	Font largeFont;
	boolean drawXGrid = true; // ok
	boolean drawYGrid = true; // ok
	int firstDayOfWeek = FIRST_DAY_OF_WEEK; // ok
	boolean showSignature = true;

	List<Source> sources = new ArrayList<Source>();
	List<CommentText> comments = new ArrayList<CommentText>();
	List<PlotElement> plotElements = new ArrayList<PlotElement>();

	/**
	 * Creates RrdGraphDef object and sets default time span (default ending time is 'now',
	 * default starting time is 'end-1day'.
	 */
	public RrdGraphDef() {
		try {
			setTimeSpan(Util.getTimestamps(DEFAULT_START, DEFAULT_END));
		} catch (RrdException e) {
			throw new RuntimeException(e);
		}
		smallFont = this.getSmallFont();
		largeFont = this.getLargeFont();
	}

	protected Font getFontFromResourceName(String name) {
		Font font;
		
		try {
			InputStream fontStream = ClassLoader.getSystemClassLoader().getResourceAsStream(name);
			font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
			fontStream.close();
		} catch (Exception e) {
			System.err.println("An error occurred loading the font '" + name + "'. Falling back to the default.");
			e.printStackTrace();
			font = new Font(DEFAULT_FONT_NAME, Font.PLAIN, 10);
		}
		return font;
	}
	
	protected Font getSmallFont() {
		return this.getFontFromResourceName(RrdGraphConstants.DEFAULT_SMALL_FONT_FILE).deriveFont(Font.PLAIN).deriveFont(10);
	}
	
	protected Font getLargeFont() {
		return this.getFontFromResourceName(RrdGraphConstants.DEFAULT_LARGE_FONT_FILE).deriveFont(Font.BOLD).deriveFont(12);
	}
	
	/**
	 * Sets the signature string that runs along the right-side of the graph.
	 * Defaults to "Created with JRobin".
	 * 
	 * @param signature the string to print<commit_msg>fix verboseness, and do not use the system class loader<commit_after>/* ============================================================
 * JRobin : Pure java implementation of RRDTool's functionality
 * ============================================================
 *
 * Project Info:  http://www.jrobin.org
 * Project Lead:  <NAME> (<EMAIL>)
 *
 * Developers:    <NAME> (<EMAIL>)
 *
 *
 * (C) Copyright 2003-2005, by <NAME>.
 *
 * This library 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */
package org.jrobin.graph;

import org.jrobin.core.RrdException;
import org.jrobin.core.Util;
import org.jrobin.data.Plottable;

import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Class which should be used to define new JRobin graph. Once constructed and populated with data
 * object of this class should be passed to the constructor of the {@link RrdGraph} class which
 * will actually create the graph.
 * <p/>
 * The text printed below the actual graph can be formated by appending
 * special escaped characters at the end of a text. When ever such a
 * character occurs, all pending text is pushed onto the graph according to
 * the character specified.
 * <p/>
 * Valid markers are: \j for justified, \l for left aligned, \r for right
 * aligned and \c for centered.
 * <p/>
 * Normally there are two space characters inserted between every two
 * items printed into the graph. The space following a string can be
 * suppressed by putting a \g at the end of the string. The \g also squashes
 * any space inside the string if it is at the very end of the string.
 * This can be used in connection with %s to suppress empty unit strings.
 * <p/>
 * A special case is COMMENT:\s this inserts some additional vertical
 * space before placing the next row of legends.
 * <p/>
 * When text has to be formated without special instructions from your
 * side, RRDTool will automatically justify the text as soon as one string
 * goes over the right edge. If you want to prevent the justification
 * without forcing a newline, you can use the special tag \J at the end of
 * the string to disable the auto justification.
 */
public class RrdGraphDef implements RrdGraphConstants {
	boolean poolUsed = false; // ok
	boolean antiAliasing = false; // ok
	String filename = RrdGraphConstants.IN_MEMORY_IMAGE; // ok
	long startTime, endTime; // ok
	TimeAxisSetting timeAxisSetting = null; // ok
	ValueAxisSetting valueAxisSetting = null; // ok
	boolean altYGrid = false; // ok
	boolean noMinorGrid = false; // ok
	boolean altYMrtg = false; // ok
	boolean altAutoscale = false; // ok
	boolean altAutoscaleMax = false; // ok
	int unitsExponent = Integer.MAX_VALUE; // ok
	int unitsLength = DEFAULT_UNITS_LENGTH; // ok
	String verticalLabel = null; // ok
	int width = DEFAULT_WIDTH, height = DEFAULT_HEIGHT; // ok
	boolean interlaced = false; // ok
	String imageInfo = null; // ok
	String imageFormat = DEFAULT_IMAGE_FORMAT; // ok
	float imageQuality = DEFAULT_IMAGE_QUALITY; // ok
	String backgroundImage = null; // ok
	String overlayImage = null; // ok
	String unit = null; // ok
	String signature = "Created with JRobin"; // ok
	boolean lazy = false; // ok
	double minValue = Double.NaN; // ok
	double maxValue = Double.NaN; // ok
	boolean rigid = false; // ok
	double base = DEFAULT_BASE;  // ok
	boolean logarithmic = false; // ok
	Paint[] colors = new Paint[] {
			// ok
			DEFAULT_CANVAS_COLOR,
			DEFAULT_BACK_COLOR,
			DEFAULT_SHADEA_COLOR,
			DEFAULT_SHADEB_COLOR,
			DEFAULT_GRID_COLOR,
			DEFAULT_MGRID_COLOR,
			DEFAULT_FONT_COLOR,
			DEFAULT_FRAME_COLOR,
			DEFAULT_ARROW_COLOR
	};
	boolean noLegend = false; // ok
	boolean onlyGraph = false; // ok
	boolean forceRulesLegend = false; // ok
	String title = null; // ok
	long step = 0; // ok
	Font smallFont;
	Font largeFont;
	boolean drawXGrid = true; // ok
	boolean drawYGrid = true; // ok
	int firstDayOfWeek = FIRST_DAY_OF_WEEK; // ok
	boolean showSignature = true;

	List<Source> sources = new ArrayList<Source>();
	List<CommentText> comments = new ArrayList<CommentText>();
	List<PlotElement> plotElements = new ArrayList<PlotElement>();

	/**
	 * Creates RrdGraphDef object and sets default time span (default ending time is 'now',
	 * default starting time is 'end-1day'.
	 */
	public RrdGraphDef() {
		try {
			setTimeSpan(Util.getTimestamps(DEFAULT_START, DEFAULT_END));
		} catch (RrdException e) {
			throw new RuntimeException(e);
		}
		smallFont = this.getSmallFont();
		largeFont = this.getLargeFont();
	}

	protected Font getFontFromResourceName(String name) {
		Font font;
		
		try {
			InputStream fontStream = this.getClass().getResourceAsStream(name);
			font = Font.createFont(Font.TRUETYPE_FONT, fontStream);
			fontStream.close();
		} catch (Exception e) {
			System.err.println("An error occurred loading the font '" + name + "'. Falling back to the default.");
			// e.printStackTrace();
			font = new Font(DEFAULT_FONT_NAME, Font.PLAIN, 10);
		}
		return font;
	}
	
	protected Font getSmallFont() {
		return this.getFontFromResourceName(RrdGraphConstants.DEFAULT_SMALL_FONT_FILE).deriveFont(Font.PLAIN).deriveFont(10);
	}
	
	protected Font getLargeFont() {
		return this.getFontFromResourceName(RrdGraphConstants.DEFAULT_LARGE_FONT_FILE).deriveFont(Font.BOLD).deriveFont(12);
	}
	
	/**
	 * Sets the signature string that runs along the right-side of the graph.
	 * Defaults to "Created with JRobin".
	 * 
	 * @param signature the string to print
{'fraction_non_alphanumeric': 0.10719219829337667, 'fraction_numerical': 0.005201137748882568, 'mean_word_length': 3.875594294770206, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4424892', 'n_tokens_mistral': 3726, 'n_tokens_neox': 3479, 'n_words': 1389}