ratishphilip / CompositionProToolkit

Collection of Helper classes and controls (using Win2d) for Windows.UI.Composition
MIT License
235 stars 29 forks source link

Parsing using other Culture #10

Closed 9110vlado closed 6 years ago

9110vlado commented 7 years ago

Hello,

Thank you, it is a great library, but I have found an issue when SVG path is parsed to CanvasGeometry. When CurrentCulture has other decimal separator than "." (e. g. ",") SVG path is not parsed correctly.

My workaround is: CultureInfo currentCulture = CultureInfo.CurrentCulture; CanvasGeometry icon = null;

        // Temporary parse fix error
        try
        {
            System.Globalization.CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("en");

            icon = CanvasObject.CreateGeometry(drawingSeesion, pathData);
        }
        finally
        {
            CultureInfo.CurrentCulture = currentCulture;
        }
ratishphilip commented 7 years ago

According to the SVG Specification

The only allowable decimal point is a Unicode U+0046 FULL STOP (".") character (also referred to in Unicode as PERIOD, dot and decimal point) and no other delimiter characters are allowed [UNICODE]. (For example, the following is an invalid numeric value in a path data stream: "13,000.56". Instead, say: "13000.56".)

So, to conform to this, the parser internally uses Regular Expressions which expect the dot character as the decimal separator.

I'm not fully sure if your workaround will work. However, you can use a similar workaround while constructing the pathData i.e. when you convert numbers to string format.

9110vlado commented 7 years ago

There is not problem with the SVG path, but there is problem with the CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator which is in my case ",". This decimal separator is used when the SVG path is parsed.

E. g.:

So my workaround temporarily set English culture which has decimal separator ".".

Fix in your code is really simple. You only change Single.TryParseto the other overload which uses IFormatProviderinterface: public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out float result)

E.g.: Single.TryParse(match.Groups["Y"].Value, NumberStyles.Any, CultureInfo.InvariantCulture, out _y);

ratishphilip commented 7 years ago

I get your point. Thanks a lot. The fix will be included in the next update.

ratishphilip commented 6 years ago

@9110vlado

This has been fixed in v0.8 release.