I have a Swing Dialog with Font, size, style, and color, with a g2D preview.
The information is captured into a SimpleAttributeSet
I have a custom class (SVGStyleDescriptor) that translates back and forth the SimpleAttributeSet into a Style attribute. I also created additional constants for my SimpleAttributeSet, to assign stroke, stroke-width and pointer-events to the available.
As a head start for a swing font chooser, here is a good reference:
http://www.java2s.com/ExampleCode/Tiny-Application/AdvancedFontChooser.htm
I have decided to contribute the SVGStyleDescriptor class (please note that the helper methods for color handling do not perform any type of validation, they will fail if the value provided is wrong):
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import org.apache.batik.util.SVGConstants;
import com.onemileup.client.draw.StyleSelector;
/**
* Helper class that translates the font description from a SimpleAttributeSet into a XML "style" attribute
* that conforms to the SVG style definition, and also constructs a SimpleAttributeSet from a "style" attribute.
* All methods are static, so no instance needs to be created.
*
* @author Andres Toussaint
* [EMAIL PROTECTED]
*/
public class SVGStyleDescriptor {
/*
* Constructs a SVG font "style" attribute from a SimpleAttributeSet. It assigns the same default values as the
* ones in the SimpleAttributeSet definition.
* This method ONLY use the following attributes:
* Font Family, Font size, Fill color, Bold and Italic.
*/
public static String getSVGFontStyle(MutableAttributeSet a) {
try {
String retval = "";
retval += SVGConstants.SVG_FONT_FAMILY_ATTRIBUTE +":"+StyleConstants.getFontFamily(a)+";";
retval += SVGConstants.SVG_FONT_SIZE_ATTRIBUTE +":"+StyleConstants.getFontSize(a)+";";
retval += SVGConstants.SVG_FILL_ATTRIBUTE +":"+ toRGBString(StyleConstants.getForeground(a)) +";";
if (StyleConstants.isItalic(a))
retval += SVGConstants.SVG_FONT_STYLE_ATTRIBUTE + ":"+ SVGConstants.SVG_ITALIC_VALUE +";";
if (StyleConstants.isBold(a))
retval += SVGConstants.SVG_FONT_WEIGHT_ATTRIBUTE + ":"+ SVGConstants.SVG_BOLD_VALUE +";";
return retval;
} catch (Exception e) {
return null;
}
}
/*
* Populates a SimpleAttributeSet from a SVG font "style" attribute. It assigns the same default values as the
* ones in the SimpleAttributeSet definition.
* This method ONLY use the following attributes:
* Font Family, Font size, Fill color, Bold and Italic.
*/
public static void setAttributeSet(SimpleAttributeSet a, String style) {
String[] theValues = style.split(";",-1);
StyleConstants.setItalic(a, false);
StyleConstants.setBold(a, false);
int i=0;
while (i<theValues.length) {
String [] value = theValues[i].split(":");
if (theValues[i].startsWith(SVGConstants.SVG_FONT_FAMILY_ATTRIBUTE)) {
StyleConstants.setFontFamily(a, value[1]);
} else if (theValues[i].startsWith(SVGConstants.SVG_FONT_SIZE_ATTRIBUTE)) {
StyleConstants.setFontSize(a, new Integer(value[1]).intValue());
} else if (theValues[i].startsWith(SVGConstants.SVG_FILL_ATTRIBUTE)) {
StyleConstants.setForeground(a, toColor(value[1]));
} else if (theValues[i].startsWith(SVGConstants.SVG_FONT_STYLE_ATTRIBUTE)) {
if (value[1].equals(SVGConstants.SVG_ITALIC_VALUE)) {
StyleConstants.setItalic(a, true);
}
} else if (theValues[i].startsWith(SVGConstants.SVG_FONT_WEIGHT_ATTRIBUTE)) {
if (value[1].equals(SVGConstants.SVG_BOLD_VALUE)) {
StyleConstants.setBold(a, true);
}
}
i++;
}
}
/*
* Creates a SimpleAttributeSet from a SVG font "style" attribute. It assigns the same default values as the
* ones in the SimpleAttributeSet definition.
* This method ONLY use the following attributes:
* Font Family, Font size, Fill color, Bold and Italic.
*/
public static SimpleAttributeSet getAttributeSet(String style) {
SimpleAttributeSet a = new SimpleAttributeSet();
setAttributeSet(a, style);
return a;
}
/*
* Converts a color to a RGB color definition, as defined by the SVG spec.
*/
public String toRGBString(Color theColor) {
int thR=theColor.getRed();
int thG=theColor.getGreen();
int thB=theColor.getBlue();
String retval = new String("rgb("+thR+","+thG+","+thB+")");
return retval;
}
/*
* Parses a RGB color attribute and returns the java.awt.Color equivalent to it.
* (This method should have validation of the string contents)
*/
public static Color toColor(String theColorString) {
theColorString = theColorString.substring(4);
String[] theValues = theColorString.split(",",-1);
int thR=(new Integer(theValues[0])).intValue();
int thG=(new Integer(theValues[1])).intValue();
int thB=(new Integer((theValues[2].substring(0,(theValues[2].length()-1))))).intValue();
Color retval = new Color(thR, thG, thB);
return retval;
}
}
On Sep 30, 2005, at 11:31 AM, Bishop, Michael W. CONTR J9C880 wrote:
<x-tad-bigger>Has anyone ever mimicked the “text” component in a program like Paint or Photoshop where you click on the canvas, a box shows up, you type in your text and it’s there on the canvas? How would I go about implementing this in a way that can be easily translated to SVG? I’d like my Swing component to look as close as possible to the resultant SVG “text” component that gets added to the document and rendered to the JSVGCanvas. Anyone done something like this before?</x-tad-bigger>
<x-tad-bigger> </x-tad-bigger>
<x-tad-bigger>Michael Bishop</x-tad-bigger>
