Must I set resourceProvider for SkSVGDOM?

71 views
Skip to first unread message

Данил Буланов

unread,
Jan 26, 2024, 2:11:20 PM1/26/24
to skia-discuss
There is a field called fResourceProvide in SkSVGDOM, and in SvgSlide code example thid resource provider is set, but I noticed that if I won't set it -- the example still works. So my question is : is it neccessary to set that resuorce provider render svg?

Brian Osman

unread,
Jan 26, 2024, 2:31:57 PM1/26/24
to skia-d...@googlegroups.com
The resource provider is used when there are references to assets within the SVG (usually images). If the SVG doesn't contain anything like that - it will still work. Otherwise, you need a resource provider that knows how to take the resource path and name, and produce the data (by loading another file, decoding a data URI, etc...).

--
You received this message because you are subscribed to the Google Groups "skia-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to skia-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/skia-discuss/f3f9fe47-d0d9-41c2-8fdb-16491ed81d1an%40googlegroups.com.

Данил Буланов

unread,
Jan 26, 2024, 2:47:19 PM1/26/24
to skia-discuss
Ah, that makes sense, thanks!

I am struggling with another thing, I want to create SkSVGDOM from a regular string. And that if how I modified code of SvgSlide load method in order to acheive this. But it does not work, and I have no idea why. My small log says that data which I get fro string is different than the one I get from file :( But I have just copied the file and escaped all quotation marks

void SvgSlide::load(SkScalar w, SkScalar h) {
  auto data = SkData::MakeWithCString(
  "<?xmlversion=\"1.0\"encoding=\"UTF-8\"standalone=\"no\"?><!--"
  "Thedefaultglyfwillbemuchsmallerthanthecolrglyflayers.--><svgversion=\"1.1\"viewBox="
  "\"00158.75211.66667\"height=\"800\"width=\"600\"><gtransform=\"translate(0,-85.333351)"
  "\"id=\"layer1\"><pathtransform=\"matrix(1,0,0,1.3773044,0.80180858,-33.549844)\"d="
  "\"m79.11178,236.39392c-5.497887,0-73.0286729,-67.53078-73.028673,-73.02867-2e-7,-5."
  "4978967.530782,-73.02867173.028669,-73.0286725.497888,073.028674,67.53078273.028674,"
  "73.0286720,5.49789-67.530782,73.02867-73.02867,73.02867z\"id=\"path5299\"style=\"fill:"
  "#ff2a2a;stroke:none;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:none;stroke-"
  "opacity:1\"/"
  "><pathtransform=\"matrix(0.60436993,0,0,0.81236352,32.100808,58.741857)\"d=\"m79."
  "11178,236.39392c-5.497887,0-73.0286729,-67.53078-73.028673,-73.02867-2e-7,-5.4978967."
  "530782,-73.02867173.028669,-73.0286725.497888,073.028674,67.53078273.028674,73."
  "0286720,5.49789-67.530782,73.02867-73.02867,73.02867z\"id=\"path5299-5\"style=\"fill:"
  "none;stroke:#2b0000;stroke-width:14.19605827;stroke-miterlimit:4;stroke-dasharray:"
  "none;stroke-opacity:1\"/></g></svg>");
  auto st = SkMemoryStream::Make(data);
  auto fileData = SkData::MakeFromFileName(fPath.c_str());

  std::cout << fileData->equals(data.get()) << std::endl;

  auto stream = SkStream::MakeFromFile(fPath.c_str());

  if (!stream) {
    SkDebugf("Could not open %s.\n", fPath.c_str());
    return;
  }

  auto rp = skresources::DataURIResourceProviderProxy::Make(
    skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
    /*predecode=*/true),
    /*predecode=*/true);
  fDom = SkSVGDOM::Builder()
    .setFontManager(ToolUtils::TestFontMgr())
    .setResourceProvider(std::move(rp))
    .make(*stream);
  if (fDom) {
    fDom->setContainerSize(SkSize::Make(w, h));
  }
}

пятница, 26 января 2024 г. в 22:31:57 UTC+3, brian...@google.com:

John Stiles

unread,
Jan 26, 2024, 3:07:30 PM1/26/24
to skia-d...@googlegroups.com
All the spaces are missing from your string. For instance,
<?xmlversion=\"1.0\"encoding=\"UTF-8\"standalone=\"no\"?>
Is missing a required space between "xml" and "version".
<?xml version=\"1.0\"encoding=\"UTF-8\"standalone=\"no\"?>

Данил Буланов

unread,
Jan 26, 2024, 3:22:34 PM1/26/24
to skia-discuss
Yes< thank you, I have fixed that but it still does not work

I tried to run it with debug, and I see that the size of data from file is smaller than data from string by one

Here is my final version


void SvgSlide::load(SkScalar w, SkScalar h) {
    if (fDom) {
        fDom->unref();
        fDom = nullptr;

    }

    auto data = SkData::MakeWithCString(
            "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n<!-- The default glyf "
            "will be much smaller than the colr glyf layers. -->\n<svg\n   version=\"1.1\"\n   "
            "viewBox=\"0 0 158.75 211.66667\"\n   height=\"800\"\n   width=\"600\">\n  <g\n     "
            "transform=\"translate(0,-85.333351)\"\n     id=\"layer1\">\n    <path\n       "
            "transform=\"matrix(1,0,0,1.3773044,0.80180858,-33.549844)\"\n       d=\"m "
            "79.11178,236.39392 c -5.497887,0 -73.0286729,-67.53078 -73.028673,-73.02867 "
            "-2e-7,-5.49789 67.530782,-73.028671 73.028669,-73.028672 5.497888,0 "
            "73.028674,67.530782 73.028674,73.028672 0,5.49789 -67.530782,73.02867 "
            "-73.02867,73.02867 z\"\n       id=\"path5299\"\n       "

            "style=\"fill:#ff2a2a;stroke:none;stroke-width:0;stroke-miterlimit:4;stroke-dasharray:"
            "none;stroke-opacity:1\" />\n    <path\n       "
            "transform=\"matrix(0.60436993,0,0,0.81236352,32.100808,58.741857)\"\n       d=\"m "
            "79.11178,236.39392 c -5.497887,0 -73.0286729,-67.53078 -73.028673,-73.02867 "
            "-2e-7,-5.49789 67.530782,-73.028671 73.028669,-73.028672 5.497888,0 "
            "73.028674,67.530782 73.028674,73.028672 0,5.49789 -67.530782,73.02867 "
            "-73.02867,73.02867 z\"\n       id=\"path5299-5\"\n       "
            "style=\"fill:none;stroke:#2b0000;stroke-width:14.19605827;stroke-miterlimit:4;stroke-"
            "dasharray:none;stroke-opacity:1\" />\n  </g>\n</svg>\n");

    auto st = SkMemoryStream::Make(data);
    auto fileData = SkData::MakeFromFileName(fPath.c_str());

    std::cout << fileData->equals(data.get()) << std::endl;

//    auto stream = SkStream::MakeFromFile(fPath.c_str());
    auto stream = SkMemoryStream::Make(fileData);


    if (!stream) {
        SkDebugf("Could not open %s.\n", fPath.c_str());
        return;
    }

    auto rp = skresources::DataURIResourceProviderProxy::Make(
            skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
                                                    /*predecode=*/true),
            /*predecode=*/true);
    fDom = SkSVGDOM::Builder()
                   .setFontManager(ToolUtils::TestFontMgr())
//                   .setResourceProvider(std::move(rp))

                   .make(*stream);
    if (fDom) {
        fDom->setContainerSize(SkSize::Make(w, h));
    }
}

пятница, 26 января 2024 г. в 23:07:30 UTC+3, johns...@google.com:

Данил Буланов

unread,
Jan 26, 2024, 3:27:20 PM1/26/24
to skia-discuss
Weeell, I have noticed this code: https://skia.googlesource.com/skia/+/refs/heads/main/src/core/SkData.cpp#201

It adds one to strlen() result, I tried to remove that, and It fixed my problem, that is weird

пятница, 26 января 2024 г. в 23:22:34 UTC+3, Данил Буланов:

John Stiles

unread,
Jan 26, 2024, 3:30:22 PM1/26/24
to skia-d...@googlegroups.com
That +1 is to ensure that the null terminator is kept, as explained in the doc-comment:

     *  Create a new dataref by copying the specified c-string
     *  (a null-terminated array of bytes). The returned SkData will have size()
     *  equal to strlen(cstr) + 1. If cstr is NULL, it will be treated the same
     *  as "".


If you don't want the null terminator, use MakeWithCopy(const void* data, size_t length) instead.



Данил Буланов

unread,
Jan 26, 2024, 3:36:34 PM1/26/24
to skia-discuss
Thank you, works great! It is interesting that it does not work with \0, wondering why

пятница, 26 января 2024 г. в 23:30:22 UTC+3, johns...@google.com:

John Stiles

unread,
Jan 26, 2024, 3:37:38 PM1/26/24
to skia-d...@googlegroups.com
I would assume it's no longer well-formed XML if it has a garbage character at the end.

Данил Буланов

unread,
Jan 26, 2024, 3:38:28 PM1/26/24
to skia-discuss
Makes sense, thanks again :)

пятница, 26 января 2024 г. в 23:37:38 UTC+3, johns...@google.com:
Reply all
Reply to author
Forward
0 new messages