Hi,

I implemented autowrapping for tooltip texts, i.e. modified the method
batik.swing.JSVGCanvas.CanvasUserAgent.toFormattedHTML
such that it is possible to break lines at a certain threshold. So that the
formatting is not messed up, the code is attached separately.

Basically, the method which autowraps is a greedy algorithm with the
following properties:
--- for each word, it checks whether it would still fit in the line, and if
not, it breaks before the word.
--- if there should be a word which happens to be longer than the line size,
we put this word in a separate line which is as long as the word size (this
should happen very rarely if the line size is chosen appropriately; 50
characters seemed okay to me).

I know that hardcoding the line length of 50 characters right in the method
is not a good idea, but as I am not sure of how Batik handles such kind of
parameters, I left it there. If you should include it within the official
release at some time, which you should feel free to do if you want, I would
suggest putting the lengh definition somewhere with other variables of the type.

I tested the method with several test cases and I tried to comment the code
well. Should there be any questions, feel free to ask.  Of course, I am also
open for comments. Even with big, big SVG files (~2MB) with lots of shapes I
did not notice any performance drawback.

Regards,

Frank

-- 
GMX ProMail (250 MB Mailbox, 50 FreeSMS, Virenschutz, 2,99 EUR/Monat...)
jetzt 3 Monate GRATIS + 3x DER SPIEGEL +++ http://www.gmx.net/derspiegel +++
        // Replace batik.swing.JSVGCanvas.CanvasUserAgent.toFormattedHTML with this if you
        // want autowrapping tooltips.
        // Frank Walter

        /**
         * Converts line breaks to HTML breaks and encodes special entities.
         * Poor way of replacing '<', '>', '"', '&' and ''' in attribute values.
         * Auto-wraps the text at 50 characters.
         */
        public String toFormattedHTML(String str) {
            StringBuffer sb = new StringBuffer(str);
            replace(sb, XML_CHAR_AMP, XML_ENTITY_AMP);
            replace(sb, XML_CHAR_LT, XML_ENTITY_LT);
            replace(sb, XML_CHAR_GT, XML_ENTITY_GT);
            replace(sb, XML_CHAR_QUOT, XML_ENTITY_QUOT);
            replace(sb, XML_CHAR_APOS, XML_ENTITY_APOS);
			// Line length.
			int length = 50;
			// Offset.
			int offset = 0;
			// Index pointers.
			int i = 0; int j=0;
			while (true) {
				// Find the next space.
				j = sb.indexOf(" ",i);
				// If there is a space,...
				if (j != -1) {
					// Special case: a word can be longer than a line.
					// Treat this word as its own line.
					if (j-i > length) {
						// Insert break before word.
						sb.replace(i-1,i,"<br>");
						// Move index.
						i = ++j;
						// Insert break after word.
						sb.replace(j-1,j,"<br>");
						// Reset offset.
						offset = i;
					}
					// General case: a word cannot be longer than a line.
					// Check whether one has to break or not.
					else if (j - offset < length) {
						// No: move index.
						i = ++j;
					} else {
						// Yes: insert break.
						sb.replace(i-1,i,"<br>");
						// Reset offset.
						offset = i;
					}
				}
				// ...there is not, else, break.
				else {
					break;
				}
			}
            return sb.toString();
        }
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to