init
This commit is contained in:
48
svgmap/helpers/style-adaptor.js
Normal file
48
svgmap/helpers/style-adaptor.js
Normal file
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Преобразовать размер шрифта в единицы карты
|
||||
* @param { string | Number } v Значение для перевода "10mm", "22pt"
|
||||
* @param {*} ppu Pixel per unit
|
||||
* @returns
|
||||
*/
|
||||
function size2ppu(v, ppu) {
|
||||
if (typeof v !== "string") return v;
|
||||
|
||||
if (v.endsWith("mm")) {
|
||||
return parseFloat(v.substring(0, v.length - 2) * ppu * 100) / 69;
|
||||
}
|
||||
if (v.endsWith("pt")) {
|
||||
return (parseFloat(v.slice(0, v.length - 2)) * ppu * 100) / 283.46;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Перегоняет стиль в единицы карты (возвращает новый объект)
|
||||
* @param {*} style Объект стиля для преобразования
|
||||
* @param {*} ppu Pixel per unit
|
||||
*/
|
||||
function update_style(style, ppu) {
|
||||
let s = { ...style };
|
||||
const convertable = ["font-size", "stroke-width", "1pt", "1mm"];
|
||||
Object.keys(s).forEach((k) => {
|
||||
if (convertable.includes(k)) s[k] = size2ppu(s[k], ppu);
|
||||
});
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Перегоняет все стили в единицы карты (возвращает новый объект)
|
||||
* @param {*} style Объект стиля для преобразования
|
||||
* @param {*} ppu Pixel per unit
|
||||
*/
|
||||
function update_styles(styles, ppu) {
|
||||
return Object.keys(styles).reduce((s, c) => ({ ...s, [c]: update_style(styles[c], ppu) }), {});
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
size2ppu,
|
||||
update_style,
|
||||
update_styles
|
||||
};
|
||||
|
||||
module.exports.default = module.exports;
|
||||
50
svgmap/helpers/style-defs.js
Normal file
50
svgmap/helpers/style-defs.js
Normal file
@@ -0,0 +1,50 @@
|
||||
const SvgNode = require("../svg-node");
|
||||
|
||||
function simple_radial_gradient(id, color0, color1) {
|
||||
let s0 = new SvgNode("stop", { offset: "0%", "stop-color": color0 });
|
||||
let s1 = new SvgNode("stop", { offset: "100%", "stop-color": color1 });
|
||||
return new SvgNode("radialGradient", { id }).append(s0, s1);
|
||||
}
|
||||
|
||||
function radial_gradient(id, cx, cy, r) {
|
||||
return new SvgNode("radialGradient", { id, cx, cy, r });
|
||||
}
|
||||
|
||||
function stop(offset, color) {
|
||||
return new SvgNode("stop", { offset, "stop-color": color });
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts from styles items which must be in defs section.
|
||||
* @param {*} style
|
||||
*/
|
||||
function extract_style_defs(style) {
|
||||
const skeys = Object.keys(style).filter((x) => x.startsWith("$"));
|
||||
|
||||
return skeys.map((skey) => extract_class_def(style[skey], skey.substring(1)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts from styles items which must be in defs section.
|
||||
* @param {*} style
|
||||
*/
|
||||
function extract_class_def(cls, id) {
|
||||
if (cls.type == "radial-gradient") {
|
||||
const ckeys = Object.keys(cls);
|
||||
const stops = ckeys
|
||||
.filter((x) => x.endsWith("%"))
|
||||
.map((x) => new SvgNode("stop", { offset: x, "stop-color": cls[x] }));
|
||||
|
||||
return new SvgNode("radialGradient", { id }, stops);
|
||||
}
|
||||
|
||||
throw `Unknown system style ${cls.type}`
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
simple_radial_gradient,
|
||||
radial_gradient,
|
||||
extract_style_defs,
|
||||
};
|
||||
|
||||
module.exports.default = module.exports;
|
||||
Reference in New Issue
Block a user