[metapost] Clipped svg output

Lutz Haseloff lutz.haseloff at toppkieker.info
Fri Jan 20 09:49:40 CET 2023


Hi Luigi,

your hint, only to use the "positive quadrant" led me to this ugly 
workaround until mpost's svg output is debugged:

def korrektur =
z1 = llcorner currentpicture ;
numeric dw, dh ;
if x1 < 0: dw := abs x1; else: dw := 0 fi ;
if y1 < 0: dh := abs y1; else: dh := 0 fi ;
currentpicture := currentpicture shifted (dw,dh) ;
enddef ;
outputformat := "svg";
outputtemplate := "%j-%c-corrected.%{outputformat}";
for i = 17 upto 23 :
beginfig(i)
fill fullcircle scaled i withcolor red ;
korrektur ;
endfig ;
endfor ;
end

(seems to work so far...)

Thank you for your work on METAPOST.

Greetings Lutz

Am 19.01.2023 um 09:15 schrieb luigi scarso:
>
>
> On Wed, 18 Jan 2023 at 13:04, luigi scarso <luigi.scarso at gmail.com> wrote:
>
>
>
>     On Wed, 18 Jan 2023 at 12:53, Lutz Haseloff
>     <lutz.haseloff at toppkieker.info> wrote:
>
>         Hi Luigi,
>
>         I tested a bit further.
>
>         MP:
>
>         outputformat := "svg";
>         outputtemplate := "%j-%c.%{outputformat}";
>         beginfig(19)
>         fill fullcircle scaled 19 withcolor red ;
>         endfig;
>         end
>
>         SVG:
>
>         <?xml version="1.0"?>
>         <!-- Created by MetaPost 2.02 on 2023.01.18:1229 -->
>         <svg version="1.1" xmlns="http://www.w3.org/2000/svg"
>         <http://www.w3.org/2000/svg>
>         xmlns:xlink="http://www.w3.org/1999/xlink"
>         <http://www.w3.org/1999/xlink> width="19.000000"
>         height="19.000000" viewBox="0 0 19.000000 19.000000">
>         <!-- Original BoundingBox: -9.500000 -9.500000 9.500000
>         9.500000 -->
>           <path d="M18.500000 9.000000 (should be 19.0 9.5) ...
>         11.519557 -0.500000 (should be 0) ... -0.500000 9.000000
>         (should be 0) ... ,18.500000 9.000000Z" style="fill:
>         rgb(100.000000%,0.000000%,0.000000%);stroke: none;"></path>
>         </svg>
>
>         It seems, that the path d is not correctly transformed from
>         the ps hires boundingbox (-9.5 -9.5 9.5 9.5) to the svg
>         viewbox (0 0 19 19).
>
>         With even sizes ( say 20) it works ok.
>
>
>
>     indeed, I was looking at it ... this evening can check it better
>
>     --
>     luigi
>
>
> looking at this now, maybe the integer dx and dy are not ok.
>
> @ We often need to print a pair of coordinates.
>
> Because of bugs in svg rendering software, it is necessary to
> change the point coordinates so that there are all in the "positive"
> quadrant of the SVG field. This means an shift and a vertical flip.
>
> The two correction values are calculated by the function that writes
> the initial |<svg>| tag, and  are stored in two globals:
>
> @<Globals@>=
> integer dx;
> integer dy;
>
> @ @c
> void mp_svg_pair_out (MP mp,double x, double y) {
>   mp_svg_store_double(mp, (x+mp->svg->dx));
>   append_char(' ');
>   mp_svg_store_double(mp, (-(y+mp->svg->dy)));
> }
>
> If I use double, i.e.
> @<Globals@>=
> double dx;
> double dy;
>
> I have now
>
> <?xml version="1.0"?>
> <!-- Created by MetaPost 2.02 on 2023.01.19:0912 -->
> <svg version="1.1" xmlns="http://www.w3.org/2000/svg" 
> xmlns:xlink="http://www.w3.org/1999/xlink" width="19.000000" 
> height="19.000000" viewBox="0 0 19.000000 19.000000">
> <!-- Original BoundingBox: -9.500000 -9.500000 9.500000 9.500000 -->
>   <path d="M19.000000 9.500000C19.000000 6.980331,17.998917 
> 4.564163,16.217377 2.782623C14.435837 1.001083,12.019669 
> -0.000000,9.500000 -0.000000C6.980331 -0.000000,4.564163 
> 1.001083,2.782623 2.782623C1.001083 4.564163,0.000000 
> 6.980331,0.000000 9.500000C0.000000 12.019669,1.001083 
> 14.435837,2.782623 16.217377C4.564163 17.998917,6.980331 
> 19.000000,9.500000 19.000000C12.019669 19.000000,14.435837 
> 17.998917,16.217377 16.217377C17.998917 14.435837,19.000000 
> 12.019669,19.000000 9.500000Z" style="fill: 
> rgb(100.000000%,0.000000%,0.000000%);stroke: none;"></path>
> </svg>
>
> --
> luigi
>
> --
> http://tug.org/metapost/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://tug.org/pipermail/metapost/attachments/20230120/1ae852c4/attachment.html>


More information about the metapost mailing list.