Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/NonGeometricData.java
1463 views
package org.newdawn.slick.svg;12import java.util.Properties;34import org.newdawn.slick.Color;56/**7* A set of data about a shape that doesn't fit into it's geometric8* configuration.9*10* @author kevin11*/12public class NonGeometricData {13/** The ID of the figure */14public static final String ID = "id";15/** The fill type */16public static final String FILL = "fill";17/** The stroke type */18public static final String STROKE = "stroke";19/** The alpha value for filling */20public static final String OPACITY = "opacity";21/** The width of the line to draw */22public static final String STROKE_WIDTH = "stroke-width";23/** The mitre of the line to draw */24public static final String STROKE_MITERLIMIT = "stroke-miterlimit";25/** The dash definition of the line to draw */26public static final String STROKE_DASHARRAY = "stroke-dasharray";27/** The offset into the dash definition of the line to draw */28public static final String STROKE_DASHOFFSET = "stroke-dashoffset";29/** The alpha value for drawing */30public static final String STROKE_OPACITY = "stroke-opacity";3132/** Value indicating that no settings has been specified */33public static final String NONE = "none";3435/** The meta data stored for the figure */36private String metaData = "";37/** The attributes stored for the figure */38private Properties props = new Properties();3940/**41* Create a set of non-geometric data for a figure42*43* @param metaData The meta data (either label or id) for the figure44*/45public NonGeometricData(String metaData) {46this.metaData = metaData;47addAttribute(STROKE_WIDTH, "1");48}4950/**51* Morph the color from a string52*53* @param str The string to morph54* @return The new color string55*/56private String morphColor(String str) {57if (str.equals("")) {58return "#000000";59}60if (str.equals("white")) {61return "#ffffff";62}63if (str.equals("black")) {64return "#000000";65}6667return str;68}6970/**71* Add a configured style attribute into the data set72*73* @param attribute The attribute to add74* @param value The value to assign75*/76public void addAttribute(String attribute, String value) {77if (value == null) {78value = "";79}8081if (attribute.equals(FILL) ) {82value = morphColor(value);83}84if (attribute.equals(STROKE_OPACITY)) {85if (value.equals("0")) {86props.setProperty(STROKE, "none");87}88}89if (attribute.equals(STROKE_WIDTH)) {90if (value.equals("")) {91value = "1";92}93if (value.endsWith("px")) {94value = value.substring(0,value.length()-2);95}96}97if (attribute.equals(STROKE)) {98if ("none".equals(props.getProperty(STROKE))) {99return;100}101if ("".equals(props.getProperty(STROKE))) {102return;103}104value = morphColor(value);105}106107props.setProperty(attribute, value);108}109110/**111* Check if a given attribute is in colour format112*113* @param attribute The attribute to check114* @return True if the attirbute value is in colour format115*/116public boolean isColor(String attribute) {117return getAttribute(attribute).startsWith("#");118}119120/**121* Get the meta data assigned to the figure. Either the label or122* the id value.123*124* @return The meta data assigned to the figure125*/126public String getMetaData() {127return metaData;128}129130/**131* Get the attribtue value for a given attribute132*133* @param attribute The attribute whose value should be obtained134* @return The value for the given attribute135*/136public String getAttribute(String attribute) {137return props.getProperty(attribute);138}139140/**141* Get an attribute value converted to a color. isColor should first be checked142*143* @param attribute The attribute whose value should be interpreted as a color144* @return The color based on the attribute145*/146public Color getAsColor(String attribute) {147if (!isColor(attribute)) {148throw new RuntimeException("Attribute "+attribute+" is not specified as a color:"+getAttribute(attribute));149}150151int col = Integer.parseInt(getAttribute(attribute).substring(1), 16);152153return new Color(col);154}155156/**157* Get the attribute value as a reference to another entity158*159* @param attribute The name of the attribute to retrieve160* @return The reference part of the attribute value161*/162public String getAsReference(String attribute) {163String value = getAttribute(attribute);164if (value.length() < 7) {165return "";166}167168value = value.substring(5, value.length()-1);169170return value;171}172173/**174* Get an attribute converted to a float value175*176* @param attribute The attribute to retrieve177* @return The float value derived from the attribute178*/179public float getAsFloat(String attribute) {180String value = getAttribute(attribute);181if (value == null) {182return 0;183}184185try {186return Float.parseFloat(value);187} catch (NumberFormatException e) {188throw new RuntimeException("Attribute "+attribute+" is not specified as a float:"+getAttribute(attribute));189}190}191192/**193* True if the shape is meant to be filled194*195* @return True if the shape is meant to be filled196*/197public boolean isFilled() {198return isColor(NonGeometricData.FILL);199}200201/**202* True if the shape is meant to be outlined203*204* @return True if the shape is meant to be outlined205*/206public boolean isStroked() {207return isColor(NonGeometricData.STROKE) && (getAsFloat(NonGeometricData.STROKE_WIDTH) > 0);208}209}210211212