TinyVG / sdk

TinyVG software development kit
https://tinyvg.tech/
MIT License
269 stars 15 forks source link

svg2tvgt: fix ParseNumberGeneric #29

Closed marler8997 closed 4 months ago

marler8997 commented 4 months ago

This fixes a regression in recent commit b8e82bc8eb1ba516aed1ac3445568afed6558e45.

That commit added the characters eE+- to the list of accepted characters within the middle of a float string. However, the +- characters are only valid immediately after the eE characters. This broke SVGS that don't use whitespace between floats with negative numbers. Consider the following snippet from a real SVG:

<path d="M0 9.913V.487c0-.435.514-.651.812-.34l6.551 ...

With the commit referenced above, the string 0-.435 gets interpreted as a single float value which fails to parse as a float. I've fixed this by splitting the function to parse floats into 3 parts:

1) before the decimal point

    char first = AcceptChar("0123456789." + (allow_sign ? "+-" : ""));
    ScanWhile(char.IsDigit);

2) after the decimal point

    if (first != '.' && ScanOne(".")) {
        ScanWhile(char.IsDigit);
    }

3) the exponent

    if (ScanOne("eE")) {
        ScanOne("+-");
        ScanWhile(char.IsDigit);
    }

This fixes the parser so that it only includes the +- characters if they properly appear immediately after the eE characters.