Well I ended up with a different but elegant solution.  The user presses a text button that has a preview pane, font picker, size picker, and bold/italic buttons.  The preview pane is a JTextPane, but that doesn’t accurately represent stroke/fill so it will probably change to a Graphics2D preview like you suggest.

 

After typing the text in the JDialog, the user presses OK.  The text is then rendered (via G2D) to the GlassPane where the user moves the mouse to place the text.  Clicking places the text on the document.

 

Worked well, except it seems appending a text element without any text in it causes the application to hang.  Possible bug in the JSVGCanvas?  I was appending this:

 

<text id=”…”></text> and the thread I was updating the manager with would never return from document.appendChild(…).

 

Michael Bishop

 


From: Bishop, Michael W. CONTR J9C880 [mailto:[EMAIL PROTECTED]
Sent: Friday, September 30, 2005 2:34 PM
To: [email protected]
Subject: RE: Ideas for Swing Text -> SVG Text?

 

That is an awful lot of help.  I’ll need something like that as a precursor to what I’m trying to do.  Basically I want to click somewhere on the JSVGCanvas and start typing, having it show the (your) selected font, style, color, etc.  When I hit “Enter”, that gets captured as an SVG element.  That element is then appended to the document that dictates the JSVGCanvas rendering.

 

Michael Bishop

 


From: Andres Toussaint [mailto:[EMAIL PROTECTED]
Sent: Friday, September 30, 2005 2:09 PM
To: [email protected]
Subject: Re: Ideas for Swing Text -> SVG Text?

 

I have done a Text Style selector.

 

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:

 

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?

 

Michael Bishop

Reply via email to