r-lib / svglite

A lightweight svg graphics device for R
https://svglite.r-lib.org
180 stars 39 forks source link

try to write 2 significant digits for doubles instead of always truncating to 2 digits after the decimal point #94

Closed ilia-kats closed 5 years ago

ilia-kats commented 5 years ago

The current behavior of svglite is a problem e.g. when using heatmap.2 from gplots. If enough break points are used (in my case, 100 was sufficient), the width of the individual rectangles in the color key becomes smaller than 0.01, which results in svglite truncating it to 0 and no color key being drawn. This patch attempts to fix that by ensuring that the writing precision for doubles is set such that the two significant digits are written.

lionel- commented 5 years ago

I'm a little concerned about this diff:

modified   tests/testthat/test-scale-text.svg
@@ -12,6 +12,6 @@
   ]]></style>
 </defs>
 <rect width='100%' height='100%' style='stroke: none; fill: #FFFFFF;'/>
-<rect x='0.00' y='0.00' width='34.03' height='8.70' style='stroke-width: 0.75; stroke: #FF0000; fill: #FFFFFF;' />
-<text x='0.00' y='8.26' style='font-size: 12.00px; font-family: Liberation Sans;' textLength='34.03px' lengthAdjust='spacingAndGlyphs'>foobar</text>
+<rect x='0' y='0.0000000000000018' width='34.03' height='8.70' style='stroke-width: 0.75; stroke: #FF0000; fill: #FFFFFF;' />
+<text x='0' y='8.26' style='font-size: 12.00px; font-family: Liberation Sans;' textLength='34.03px' lengthAdjust='spacingAndGlyphs'>foobar</text>
 </svg>

We're now writing 0 instead of 0.00 in multiple cases.

ilia-kats commented 5 years ago

I can tweak the code to avoid that, but does it really matter? As far as I can tell, integer 0 is valid according to the SVG spec [1, 2, 3], and rendering behavior should be the same.

[1] https://www.w3.org/TR/SVG11/struct.html#SVGElementWidthAttribute [2] https://www.w3.org/TR/SVG11/types.html#DataTypeLength [3] https://www.w3.org/TR/SVG11/types.html#DataTypeNumber

lionel- commented 5 years ago

It looks like you are right. One advantage of your last commit is that the output looks a bit cleaner.

Thanks!