app_mapbuilder/svgmap/svg-map-saver.js
2022-04-22 18:34:31 +05:00

54 lines
1.6 KiB
JavaScript

const CoordSystem = require("../libs/coord-system");
const BBox = require("../libs/bbox");
const transfrom = require("../libs/transform");
// const SvgMapBuilder = require("./svg-map-builder");
const SvgNodes = require("./drawers/svg-nodes");
const adaptor = require("./helpers/style-adaptor");
const { extract_style_defs } = require('./helpers/style-defs');
module.exports = class SvgSaver {
constructor(settings, style, bbox) {
// Функция перевода координат из глобальных в локальные.
const cs1 = new CoordSystem(bbox.l, bbox.t, bbox.r, bbox.b);
const cs_mm = cs1.clone().flipy().moveto(0, 0).scale(settings.map_scale * 1000);
const cs_ppu = cs_mm.clone().scale(settings.ppu);
const csw = Math.abs(cs_mm.x1 - cs_mm.x0);
const csh = Math.abs(cs_mm.y1 - cs_mm.y0);
const bbox_ppu = BBox.fromLTRB(cs_ppu.x0, cs_ppu.y0, cs_ppu.x1, cs_ppu.y1);
this.tr = transfrom.fromCoordSyses(cs1, cs_ppu);
this.settings = settings
this.style = adaptor.update_styles(style, settings.ppu);
const defs = extract_style_defs(style)
this.svg = SvgNodes.svg()
.set_attr("width", csw + "mm")
.set_attr("height", csh + "mm")
.set_attr("viewBox", `${bbox_ppu.l} ${bbox_ppu.t} ${bbox_ppu.w()} ${bbox_ppu.h()}`);
if (defs.length){
this.svg.append(SvgNodes.node('defs', null, defs))
}
// console.log(style)
}
append_defs(defs) {
this.svg.append(defs);
}
append_svg(svg) {
this.svg.append(svg);
}
append_layer(layer) {
this.append_defs(layer.defs);
this.append_svg(layer.svg);
}
render() {
return this.svg.render();
}
};