mapcomplete/scripts/generateIncludedImages.ts

136 lines
4 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import * as fs from "fs"
function genImages(dryrun = false) {
2020-11-06 01:58:26 +01:00
console.log("Generating images")
// These images are not referenced via 'Svg.ts' anymore and can be ignored
const blacklist: string[] = [
"add",
"addSmall",
"brick_wall_square",
"clock",
"community",
"copyright",
"cross",
"cross_bottom_right",
"crosshair_locked",
"delete_not_allowed",
"direction_gradient",
"direction_stroke",
"duplicate",
"elevator",
"elevator_wheelchair",
"liberapay",
"length_crosshair",
"speech_bubble_black_outline",
"square",
"star_half",
"star_outline",
"star",
"osm_logo_us",
"triangle",
"teardrop_with_hole_green",
"SocialImageForeground",
"wikipedia",
"Upload",
"pin",
"mapillary_black",
"plantnet_logo",
"mastodon",
"move-arrows",
"mapcomplete_logo",
"logo",
"logout",
"hand",
"help",
"home",
"reload",
"min",
"plus",
"not_found",
"osm_logo_us",
"party",
"filter",
"filter_disable",
"floppy",
"eye",
"gear",
"gender_bi",
"compass",
"blocked",
"brick_wall",
"brick_wall_raw",
"brick_wall_round",
"bug",
"back",
].map((s) => s.toLowerCase())
2020-11-06 01:58:26 +01:00
const dir = fs.readdirSync("./assets/svg")
2020-11-05 12:28:02 +01:00
2022-09-08 21:40:48 +02:00
let module =
2023-10-30 14:35:00 +01:00
'import Img from "./UI/Base/Img";\nimport {FixedUiElement} from "./UI/Base/FixedUiElement";\n\n/* @deprecated */\nexport default class Svg {\n\n\n'
2020-11-06 01:58:26 +01:00
for (const path of dir) {
if (path.endsWith("license_info.json")) {
2022-09-08 21:40:48 +02:00
continue
2021-04-10 15:01:28 +02:00
}
2023-07-27 14:15:30 +02:00
if (path.endsWith(".license")) {
continue
}
2020-11-06 01:58:26 +01:00
if (!path.endsWith(".svg")) {
2022-09-08 21:40:48 +02:00
throw "Non-svg file detected in the svg files: " + path
2020-11-06 01:58:26 +01:00
}
2022-09-08 21:40:48 +02:00
let svg: string = fs
.readFileSync("./assets/svg/" + path, "utf-8")
.replace(/<\?xml.*?>/, "")
.replace(/<!DOCTYPE [^>]*>/, "")
.replace(/fill: ?none;/g, "fill: none !important;") // This is such a brittle hack...
2020-11-06 01:58:26 +01:00
.replace(/\n/g, " ")
.replace(/\r/g, "")
.replace(/\\/g, "\\")
2022-09-08 21:40:48 +02:00
.replace(/"/g, '\\"')
2023-10-26 14:13:04 +02:00
.replaceAll(" ", " ")
2023-10-26 14:13:04 +02:00
let hasNonAsciiChars = Array.from(svg)
.filter((char) => char.charCodeAt(0) > 127)
.map((char) => char.charCodeAt(0))
.join(", ")
if (hasNonAsciiChars.length > 0) {
throw "The svg '" + path + "' has non-ascii characters: " + hasNonAsciiChars
}
2022-09-19 12:34:05 +02:00
const name = path.substring(0, path.length - 4).replace(/[ -]/g, "_")
const nameUC = name.toUpperCase().at(0) + name.substring(1)
const svelteCode =
'<script>\nexport let color = "#000000"\n</script>\n' +
svg
.replace(
"<svg ",
"<svg {...$$$$restProps} on:click on:mouseover on:mouseenter on:mouseleave on:keydown "
)
.replace(/\\"/g, '"')
.replace(/(rgb\(0%,0%,0%\)|#000000|#000)/g, "{color}")
fs.writeFileSync("./src/assets/svg/" + nameUC + ".svelte", svelteCode, "utf8")
if (blacklist.some((item) => path.toLowerCase().endsWith(item + ".svg"))) {
continue
}
if (dryrun) {
svg = "<omitting svg - dryrun>"
}
let rawName = name
module += ` public static ${name} = "${svg}"\n`
if (!dryrun) {
module += ` public static ${name}_svg() { return new Img(Svg.${rawName}, true);}\n`
} else {
module += ` public static ${name}_svg() { return new Img("", true);}\n`
}
2020-11-06 01:58:26 +01:00
}
2022-09-08 21:40:48 +02:00
module += "}\n"
2023-07-15 18:24:39 +02:00
fs.writeFileSync("src/Svg.ts", module)
2020-11-06 01:58:26 +01:00
console.log("Done")
}
2023-05-08 22:38:47 +02:00
genImages()