vtracer implementation in converter directory

This commit is contained in:
Sahil
2025-08-12 20:53:23 +05:30
parent 554edf5a27
commit e78de6f6de
2 changed files with 97 additions and 0 deletions

View File

@@ -21,6 +21,8 @@ import { convert as convertPotrace, properties as propertiesPotrace } from "./po
import { convert as convertresvg, properties as propertiesresvg } from "./resvg";
import { convert as convertImage, properties as propertiesImage } from "./vips";
import { convert as convertxelatex, properties as propertiesxelatex } from "./xelatex";
import {convert as convertVtracer, properties as propertiesVtracer} from './vtracer';
// This should probably be reconstructed so that the functions are not imported instead the functions hook into this to make the converters more modular
@@ -117,6 +119,10 @@ const properties: Record<
properties: propertiesPotrace,
converter: convertPotrace,
},
vtracer: {
properties: propertiesVtracer,
converter: convertVtracer,
}
};
function chunks<T>(arr: T[], size: number): T[][] {

91
src/converters/vtracer.ts Normal file
View File

@@ -0,0 +1,91 @@
import { execFile as execFileOriginal } from "node:child_process";
import { ExecFileFn } from "./types";
export const properties = {
from: {
images: ["jpg", "jpeg", "png", "bmp", "gif", "tiff", "tif", "webp"],
},
to: {
images: ["svg"],
},
};
export function convert(
filePath: string,
fileType: string,
convertTo: string,
targetPath: string,
options?: unknown,
execFile: ExecFileFn = execFileOriginal, // to make it mockable
): Promise<string> {
return new Promise((resolve, reject) => {
// Build vtracer arguments
const args = ["--input", filePath, "--output", targetPath];
// Add option parameter if provided
if (options && typeof options === "object") {
const opts = options as Record<string, any>;
if (opts.colormode) {
args.push("--colormode", opts.colormode);
}
if (opts.hierarchical) {
args.push("--hierarchical", opts.hierarchical);
}
if (opts.mode) {
args.push("--mode", opts.mode);
}
if (opts.filter_speckle) {
args.push("--filter_speckle", opts.filter_speckle);
}
if (opts.color_precision) {
args.push("--color_precision", opts.color_precision);
}
if (opts.layer_difference) {
args.push("--layer_difference", opts.layer_difference);
}
if (opts.corner_threshold) {
args.push("--corner_threshold", opts.corner_threshold);
}
if (opts.length_threshold) {
args.push("--length_threshold", opts.length_threshold);
}
if (opts.max_iterations) {
args.push("--max_iterations", opts.max_iterations);
}
if (opts.splice_threshold) {
args.push("--splice_threshold", opts.splice_threshold);
}
if (opts.path_precision) {
args.push("--path_precision", opts.path_precision);
}
execFile("vtracer", args, (error, stdout, stderr) => {
if (error) {
reject(`error: ${error}`);
return;
}
if (stdout) {
console.log(`stdout: ${stdout}`);
}
if (stderr) {
console.log(`stderr: ${stderr}`);
}
resolve("Done");
});
}
});
}