svg:: renderer now has more options in WEB

This commit is contained in:
Zeno Rogue
2026-09-17 09:36:43 +02:00
parent cc8497b63e
commit 0bfe518cd0
+44 -37
View File
@@ -22,11 +22,7 @@ EX always_false in;
#endif
#if CAP_SVG
#if ISWEB
shstream f;
#else
fhstream f;
#endif
hstream *svgf;
EX bool in = false;
@@ -90,20 +86,20 @@ EX always_false in;
EX void circle(int x, int y, int size, color_t col, color_t fillcol, double linewidth) {
if(!invisible(col) || !invisible(fillcol)) {
if(pconf.stretch == 1)
println(f, "<circle cx='", coord(x), "' cy='", coord(y), "' r='", coord(size), "' ", stylestr(fillcol, col, linewidth), "/>");
println(*svgf, "<circle cx='", coord(x), "' cy='", coord(y), "' r='", coord(size), "' ", stylestr(fillcol, col, linewidth), "/>");
else
println(f, "<ellipse cx='", coord(x), "' cy='", coord(y), "' rx='", coord(size), "' ry='", coord(size*pconf.stretch), "' ", stylestr(fillcol, col), "/>");
println(*svgf, "<ellipse cx='", coord(x), "' cy='", coord(y), "' rx='", coord(size), "' ry='", coord(size*pconf.stretch), "' ", stylestr(fillcol, col), "/>");
}
}
EX string link;
void startstring() {
if(link != "") print(f, "<a xlink:href=\"", link, "\" xlink:show=\"replace\">");
if(link != "") print(*svgf, "<a xlink:href=\"", link, "\" xlink:show=\"replace\">");
}
void stopstring() {
if(link != "") print(f, "</a>");
if(link != "") print(*svgf, "</a>");
}
string font = "Times";
@@ -142,18 +138,18 @@ EX always_false in;
else str2 += str[i];
if(uselatex) str2 = string("\\myfont{")+coord(size)+"}{" + str2 + "}";
print(f, "<text x='", coord(x), "' y='", coord(y+size*.4), "' text-anchor='", align == 8 ? "middle" :
print(*svgf, "<text x='", coord(x), "' y='", coord(y+size*.4), "' text-anchor='", align == 8 ? "middle" :
align < 8 ? "start" :
"end", "' ");
if(!uselatex)
print(f, "font-family='", font, "' font-size='", coord(size), "' ");
print(*svgf, "font-family='", font, "' font-size='", coord(size), "' ");
if(tspan != "") str2 =
"<tspan style=\"" + tspan + "\">" + str2 + "</tspan>";
print(f,
print(*svgf,
stylestr(col, frame ? 0x0000000FF : 0, (1<<get_sightrange())*dfc*text_width_multiplier, fontstyle),
">", str2, "</text>");
stopstring();
println(f);
println(*svgf);
}
}
@@ -173,43 +169,54 @@ EX always_false in;
startstring();
for(int i=0; i<polyi; i++) {
if(i == 0)
print(f, "<path d=\"M ");
print(*svgf, "<path d=\"M ");
else
print(f, " L ");
print(f, coord(polyx[i]), " ", coord(polyy[i]));
print(*svgf, " L ");
print(*svgf, coord(polyx[i]), " ", coord(polyy[i]));
}
print(f, "\" ", stylestr(col, outline, (hyperbolic ? current_display->radius : current_display->scrsize) * linewidth/256), "/>");
print(*svgf, "\" ", stylestr(col, outline, (hyperbolic ? current_display->radius : current_display->scrsize) * linewidth/256), "/>");
stopstring();
println(f);
println(*svgf);
}
// 0: file, 1: string, 2: web
EX int svg_mode = ISWEB ? 2 : 0;
EX shstream sout;
fhstream sfile;
EX void render(const string& fname, const function<void()>& what IS(shot::default_screenshot_content)) {
if(svg_mode) svgf = &sout;
else { svgf = &sfile; sfile.f = fopen(fname.c_str(), "wt"); if(!sfile.f) { printf("could not open the file\n"); return; } }
dynamicval<bool> v2(in, true);
dynamicval<bool> v3(vid.usingGL, false);
#if ISWEB
f.s = "";
#else
f.f = fopen(fname.c_str(), "wt");
#endif
println(f, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"", coord(vid.xres), "\" height=\"", coord(vid.yres), "\">");
println(hlog, "started rendering file ", fname, " with svg_mode = ", svg_mode);
println(*svgf, "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"", coord(vid.xres), "\" height=\"", coord(vid.yres), "\">");
if(!shot::transparent)
println(f, "<rect width=\"", coord(vid.xres), "\" height=\"", coord(vid.yres), "\" ", stylestr((backcolor << 8) | 0xFF, 0, 0), "/>");
println(*svgf, "<rect width=\"", coord(vid.xres), "\" height=\"", coord(vid.yres), "\" ", stylestr((backcolor << 8) | 0xFF, 0, 0), "/>");
what();
println(f, "</svg>");
println(*svgf, "</svg>");
#if ISWEB
EM_ASM_({
var x=window.open();
x.document.open();
x.document.write(UTF8ToString($0));
x.document.close();
}, f.s.c_str());
#else
fclose(f.f); f.f = NULL;
#endif
if(svg_mode == 2) {
#if ISWEB
EM_ASM_({
var x=window.open();
x.document.open();
x.document.write(UTF8ToString($0));
x.document.close();
}, sout.s.c_str());
#else
printf("%s\n", sout.s.c_str());
#endif
sout.s = "";
}
if(sfile.f) fclose(sfile.f), sfile.f = nullptr;
}
#if CAP_COMMANDLINE && CAP_SHOT