nanosvg icon indicating copy to clipboard operation
nanosvg copied to clipboard

how to add a xlink:href with png image

Open MacroGu opened this issue 6 years ago • 8 comments

egg.zip

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

MacroGu avatar Dec 09 '18 17:12 MacroGu

  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);
  }
}
  1. 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.

SergeySlice avatar Dec 11 '18 16:12 SergeySlice

thanks, I will try it

MacroGu avatar Dec 12 '18 01:12 MacroGu

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

MacroGu avatar Dec 12 '18 13:12 MacroGu

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

SergeySlice avatar Dec 12 '18 19:12 SergeySlice

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 avatar Dec 14 '18 01:12 MacroGu

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

MacroGu avatar Dec 14 '18 01:12 MacroGu

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

SergeySlice avatar Dec 15 '18 10:12 SergeySlice

@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.

mikrosk avatar Sep 23 '19 12:09 mikrosk