memononen / nanosvg

Simple stupid SVG parser
zlib License
1.7k stars 362 forks source link

how to add a xlink:href with png image #143

Open MacroGu opened 5 years ago

MacroGu commented 5 years ago

egg.zip

<image id="Egg" x="0" y="0" width="100" height="100" xlink:href="data:image/png;base64,

SergeySlice commented 5 years ago
  1. Add base64_decode: 120 lines

  2. Add PNG decode: 1330 lines

  3. Add parsing:

    //parse embedded PNG image
    static void parseImage(NSVGparser* p, const char** dict)
    {
    //  NSVGattrib* attr = nsvg__getAttr(p);
    NSVGpattern *pt = NULL;
    int i;
    UINTN len = 0;
    float w,h;
    const char *href = NULL;
    UINT8 *tmpData = NULL;
    EG_IMAGE *NewImage = NULL;
    
    for (i = 0; dict[i]; i += 2) {
    if (strcmp(dict[i], "width") == 0) {
      w = nsvg__parseCoordinate(p, dict[i+1], 0.0f, nsvg__actualWidth(p));
    } else if (strcmp(dict[i], "height") == 0) {
      h = nsvg__parseCoordinate(p, dict[i+1], 0.0f, nsvg__actualHeight(p));
    } else if (strcmp(dict[i], "xlink:href") == 0) {
      href = dict[i+1];
    } else {
      nsvg__parseAttr(p, dict[i], dict[i + 1]);
    }
    }
    if (!href || (strstr(href, "data:image/png;") == NULL)) {
    return;
    }
    href = strstr(href, "base64,") + 7;
    if (p->patternFlag) {
    pt = p->patterns; //the last one
    }
    tmpData = (UINT8 *)Base64Decode((char*)href, &len);
    if (len == 0) {
    DBG("image not decoded from base64\n");
    }
    NewImage = egDecodePNG(tmpData, len, TRUE);
    pt->image = (void *)NewImage;
    if (tmpData) {
    FreePool(tmpData);
    }
    }
  4. Then decide how have you use it. I applied the image as pattern to fill the shape which is quirky solution but compatible with nanosvgrast.

MacroGu commented 5 years ago

thanks, I will try it

MacroGu commented 5 years ago

would you please afford the egDecodePNG and Base64Decode functions, EG_IMAGE struct. thanks.

SergeySlice commented 5 years ago

See the full project. nanosvg located in rEFIt_UEFI/libeg/ folder little different from original.

MacroGu commented 5 years ago

would you please give a http link of the full project, where is the refit_uefi/libeg/ folder, I can not find it, thanks.

MacroGu commented 5 years ago

do you mean the https://github.com/Clover-EFI-Bootloader/clover/tree/master/rEFIt_UEFI/libeg this project ?

SergeySlice commented 5 years ago

Not exactly. github project is older then recent on sf.net. Why my link disappears? Clover

mikrosk commented 5 years ago

@MacroGu you might be interested in https://github.com/mtyberg/nanosvg/pull/1, I took @SergeySlice 's code and adapted it on top of the "plain" nanosvg.