dompdf / php-svg-lib

SVG file parsing / rendering library
GNU Lesser General Public License v3.0
1.4k stars 77 forks source link

Division by zero in arcToSegments function #97

Closed ljadrbln closed 7 months ago

ljadrbln commented 1 year ago

Hello,

I have «Division by zero» error with next svg file: rabbit

[C:/App/vendor/phenx/php-svg-lib/src/Svg/Tag/Path.php:504] Base->{closure}()
[C:/App/vendor/phenx/php-svg-lib/src/Svg/Tag/Path.php:457] Svg\Tag\Path->arcToSegments()
[C:/App/vendor/phenx/php-svg-lib/src/Svg/Tag/Path.php:395] Svg\Tag\Path->drawArc()
[C:/App/vendor/phenx/php-svg-lib/src/Svg/Tag/AbstractTag.php:64] Svg\Tag\Path->start()
[C:/App/vendor/phenx/php-svg-lib/src/Svg/Document.php:357] Svg\Tag\AbstractTag->handle()
[C:/App/vendor/phenx/php-svg-lib/src/Svg/Document.php:225] xml_parse()
[C:/App/vendor/dompdf/dompdf/lib/Cpdf.php:5910] Svg\Document->render()
[C:/App/vendor/dompdf/dompdf/src/Adapter/CPDF.php:675] Dompdf\Cpdf->addSvgFromFile()
[C:/App/vendor/dompdf/dompdf/src/Renderer/Image.php:65] Dompdf\Adapter\CPDF->image()
[C:/App/vendor/dompdf/dompdf/src/Renderer.php:289] Dompdf\Renderer\Image->render()
[C:/App/vendor/dompdf/dompdf/src/Renderer.php:128] Dompdf\Renderer->_render_frame()
[C:/App/vendor/dompdf/dompdf/src/Renderer.php:195] Dompdf\Renderer->render()
[C:/App/vendor/dompdf/dompdf/src/Renderer.php:195] Dompdf\Renderer->render()
[C:/App/vendor/dompdf/dompdf/src/Renderer.php:195] Dompdf\Renderer->render()
[C:/App/vendor/dompdf/dompdf/src/FrameReflower/Page.php:149] Dompdf\Renderer->render()
[C:/App/vendor/dompdf/dompdf/src/Dompdf.php:765] Dompdf\FrameDecorator\AbstractFrameDecorator->reflow()

The rough workaround is to modify arcToSegments function in the Svg\Surface\SurfaceInterface\Path:

$ry = $ry ? $ry : 0.1;
$py = $py ? $py : 0.1;
$rx = $rx ? $rx : 0.1;
$px = $px ? $px : 0.1;

But maybe more elegant solution exists?

christopheleblanc commented 1 year ago

Hello,

We faced the same bug. The solution proposed by ljadrbln fixes the bug.

DomPDF is an excellent library and we hope that the definitive fix will be released soon :) .

gvollant commented 1 year ago

Thank you for found a solution.

We can wait somes week for a perfect rendering, but an exception "Division by zero" create global problem in a application

So, I think i'll be great and save headache if you create quickly an hotfix release

Thank you for your great work

bsweeney commented 9 months ago

The issue appears to be caused by a path command defining an arc where one or both of the radii are zero. You can reproduce the error with the following simple example:

<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <path d="M 25 25 a 5 0 5 0 0 50 50" stroke="orange" stroke-width="5" fill="none"/>
</svg>
image

Without a radius the arc is actually just a line. That makes for a simple solution to the issue, render a line instead of an arc.


Note that I would definitely not recommend using the suggested workaround. Changing a radius from 0 to .1 can have a significant impact on the rendering, as you can see when the radius in the above sample is changed.

<svg id="svg" xmlns="http://www.w3.org/2000/svg" width="200" height="200">
  <path d="M 25 25 a 5 .1 5 0 0 50 50" stroke="orange" stroke-width="5" fill="none"/>
</svg>
image