memononen / nanosvg

Simple stupid SVG parser
zlib License
1.68k stars 355 forks source link

Rotated circle has lower image bounds than real #133

Open SergeySlice opened 6 years ago

SergeySlice commented 6 years ago

The svg is follow

<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 500 500" xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink"  preserveAspectRatio="xMin">
  <defs>
    <radialGradient id="gradient-0" gradientUnits="userSpaceOnUse" cx="201.47" cy="218.992" r="198.771" gradientTransform="matrix(0.263146, 1.23849, -0.978164, 0.207834, 362.664082, -76.040565)">
      <stop offset="0" style="stop-color: rgb(250, 248, 24);"/>
      <stop offset="1" style="stop-color: rgb(20, 20, 2);"/>
    </radialGradient>
  </defs>
  <ellipse id="shar" transform="matrix(0.840085, -0.542455, 0.542455, 0.840085, 63.999427, 142.873696)" cx="200.626" cy="282.443" rx="195.0" ry="195.0" style="fill: url(#gradient-0);"/>
</svg>

It is not fit into his bounds. I scaled it to real bounds instead of viewBox because it can be part of scene. 2018-09-11 19 13 44

SergeySlice commented 5 years ago

There is a potencial issue in nsvg__getLocalBounds() When matrix contains negative transform like transform="matrix(0.6453 0.7639 0.7639 -0.6453 79.8232 84.2069)" then these compare will be wrong

            bounds[0] = nsvg__minf(bounds[0], curveBounds[0]);
            bounds[1] = nsvg__minf(bounds[1], curveBounds[1]);
            bounds[2] = nsvg__maxf(bounds[2], curveBounds[2]);
            bounds[3] = nsvg__maxf(bounds[3], curveBounds[3]);

because curveBounds[3] < curveBounds[1] So this comparison must be extended to both limits bounds[0] = nsvgminf(bounds[0], curveBounds[2]); bounds[2] = nsvgmaxf(bounds[2], curveBounds[0]); and so on...