SVG to image

hh hh at hyperhh.de
Sun Sep 16 12:50:19 EDT 2018


Import the source of a SVG icon widget to a PNG file:
+++++++++++++++++++++++++++++++++++++++++++++++++++++

The iconPath of an SVG icon is path data only (could be used as
"instructions" for a path in LCB).

So try to write a wrapper around it to make it valid SSVG data.

The problem is the bounding box of the path (the values for w
and h), there is no info about and it's hard to estimate from all
the control points of the path.

I tried just now to do it this way (works here, tested several times):

on mouseUp
  lock screen; lock messages
  put the iconPath of widget "SVG Icon" into path
  put 2048 into w -- estimate general width of the icons
  put 2048 into h -- estimate general height of the icons
  put merge(svgWrapper()) into t
  if there is no img "import" then create img "import"
  set text of img "import" to drawingSVGCompile(t)
  cropIt "import" --> because w and h may be too large
  set width of img "import" to 100 --> your target width
  set height of img "import" to 100 --> your target height
  set topleft of img "import" to 10,10
end mouseUp

function svgWrapper
  return "<?xml version=" &quote& "1.0" &quote& " encoding=" &quote& "UTF-8" &quote& "?>" &CR& \
        "<svg" &CR& \
        "   xmlns:svg=" &quote& "http://www.w3.org/2000/svg" &quote& "" &CR& \
        "   xmlns=" &quote& "http://www.w3.org/2000/svg" &quote& "" &CR& \
        "   version=" &quote& "1.0" &quote& "" &CR& \
        "   width=" &quote& "[[w]]" &quote& "  height=" &quote& "[[h]]" &quote& "" &CR& \
        ">" &CR& \
        "<path d=" &quote& "[[path]]" &quote& " />" &CR& \
        "</svg>"end svgWrapper
  
## CROP image ft to its opaque pixels [-hh fecit 2018]
## from my stack SVGHandles89 of "Sample Stacks"
  on cropIt ft
  put the width of img ft into w
  put the height of img ft into h
  put the maskdata of img ft into mData
  put numToByte(0) into c0
  -- left and right transparency limits
  put w into cmin; put 1 into cmax
  repeat with i=0 to h-1
    put i*w into h1
    repeat with j=1 to cmin
      if byte h1+j of mData is not c0 then
        put j into cmin; exit repeat
      end if
    end repeat
    repeat with j=w down to max(cmax,cmin)
      if byte h1+j of mData is not c0 then
        put j into cmax; exit repeat
      end if
    end repeat
  end repeat
  put max(0,cmin-1) into cmin
  put max(1+cmin,min(w,1+cmax)) into cmax
  -- top and bottom transparency limits
  put h into rmin; put 1 into rmax
  repeat with j=cmin to cmax
    repeat with i=0 to rmin-1
      if byte i*w+j of mData is not c0 then
        put i into rmin; exit repeat
      end if
    end repeat
    repeat with i=h-1 down to max(rmax,rmin)
      if byte i*w+j of mData is not c0 then
        put i into rmax; exit repeat
      end if
    end repeat
  end repeat
  put max(0,rmin-1) into rmin
  put max(1+rmin,min(h,1+rmax)) into rmax
  --
  put the left of img ft into L; put the top of img ft into T
  crop img ft to L+cmin,T+rmin,L+cmax,T+rmax
  -- LC Bug: resizes instead of cropping when image has angle <> 0
end cropIt






More information about the use-livecode mailing list