SVG - Escaping character entities

We are constructing bits of SVG on the fly to display text within nicely formatted graphics.

In order to properly show characters like: äöüÄÖÜß
We are currently escaping them, like:

Replace string($html_t;“ä”;"& # 228;";) // spaces added so it shows in this forum message
Replace string($html_t;“ü”;"& # 252;";
)

This isn’t very practical since we don’t know what text the user might enter, and there are many characters that need escaping.

Is there a better way?

Here’s the svg snippet. I added spaces so the forum message displays the escaped text.

<?xml version="1.0" encoding="UTF-8"?> Some Text Here & #228;bc& #246;& #252;& #196;& #214;& #220;& #223;

FWIW, the SVG is being displayed in 4D using Rob Laveaux’s Canvas plug-in.
[]32314363;“Sample Display”[/]

Thanks,

Jim

4D v17.2
Windows 10

Hi Jim,

Why do you need to escape these characters?
This is not necessary for the 4D SVG engine.

This code :

<code 4D>
$t:=SVG_New
$tt:=SVG_New_text ($t;“Some Text Here äöüÄÖÜß”)
SVGTool_SHOW_IN_VIEWER ($t)
SVG_CLEAR ($t)

</code 4D>

Give this:

[]32315401;“Your comment here…”[/]

We’re displaying the SVG pictures using the Canvas plugin. We started using Canvas back in 2008 - maybe before 4D had an SVG tool?

Here’s the sequence. Our designer provided us with Sketch (Mac app) files.
We save the graphic parts as SVG files.
Then, we insert the 4D Tags so we can fill in text, set width etc dynamically, using PROCESS 4D TAGS.
The error comes when we process the tags.

Here’s an SVG template:

<?xml version="1.0" encoding="UTF-8"?>

Here’s some code we use to make that template into a picture for Canvas.
<code 4D>
// set globals for reference by PROCESS 4D TAGS
gLabel_t:=$label_t
gRectWidth_r:=TEXT_GetTextWidth ($label_t;gFontFamily_t;gFontSize_r;$fontStyle_t;“300”)
gHOffset_i:=$labelOffset_i // offset is applied to front and back of label
gRectWidth_r:=gHOffset_i+gRectWidth_r+(gHOffset_i\2)

// make the finished SVG
PROCESS 4D TAGS($svgTemplate_t;$processedSVG_t)
$processedSVG_t:=HTML_ApplyHTMLEntityNames ($processedSVG_t)

// convert to picture
TEXT TO BLOB($processedSVG_t;$pictBlob_x)
BLOB TO PICTURE($pictBlob_x;$2->)

SET BLOB SIZE($pictBlob_x;0)

</code 4D>

We get a runtime error on PROCESS 4D TAGS:
“XML parsing error in frommem(1:39): invalid byte ‘<?>’ at position 1 of a 1-byte sequence”

If we were starting from scratch today, we would consider 4D SVG areas or Web Areas to create this interactive interface.
That would be a major undertaking and we don’t have time.

I agree with Vincent, there is no need for escape (replace string) for entities,
all of your extended characters are safe to use directly.

the problem in your code is TEXT TO BLOB.
you are converting to a non-unicode charset.

use CONVERT FROM TEXT, or else specify the UTF8 encoding in TEXT TO BLOB.

keep in mind that TEXT TO BLOB is a left over from non-unicode era.
it is deprecated as of v11.

That fixed the problem. We replaced:

TEXT TO BLOB($processedSVG_t;$pictBlob_x)

with

CONVERT FROM TEXT($processedSVG_t;“UTF-8”;$pictBlob_x)

However, we kept our HTML_ApplyHTMLEntityNames method, which now only replaces “&” with &

Thanks Miyako!

Jim