new changes

This commit is contained in:
Niranjan
2026-04-07 05:05:28 +05:30
parent 7c070224bd
commit a18bba15f2
29975 changed files with 3247495 additions and 2761 deletions

View File

@@ -0,0 +1,16 @@
const testExpression = /(^|\/|\\)(?:((?:u[0-9a-f]{4,6},?)+)-)(.+)\.svg$/i;
export function fileSorter(fileA, fileB) {
const hasUnicodeA = testExpression.test(fileA);
const hasUnicodeB = testExpression.test(fileB);
if (hasUnicodeA == hasUnicodeB) {
// just compare alphabetically
const fileA_ = fileA.substr(0, fileA.lastIndexOf('.'));
const fileB_ = fileB.substr(0, fileB.lastIndexOf('.'));
return fileA_ < fileB_ ? -1 : 1;
} else {
// map true to 0, because we want it to be first
return (hasUnicodeA ? 0 : 1) - (hasUnicodeB ? 0 : 1);
}
}

View File

@@ -0,0 +1,122 @@
import { Readable } from 'node:stream';
import { createReadStream, readdir } from 'node:fs';
import { fileSorter } from './filesorter.js';
import {
getMetadataService,
FileMetadata,
MetadataServiceOptions,
} from './metadata.js';
import debug from 'debug';
const warn = debug('svgicons2svgfont');
export type SVGIconsDirStreamOptions = {
metadataProvider: ReturnType<typeof getMetadataService>;
};
export type SVGIconStream = Readable & {
metadata: Pick<FileMetadata, 'name' | 'unicode'>;
};
class SVGIconsDirStream extends Readable {
private _options: SVGIconsDirStreamOptions & Partial<MetadataServiceOptions>;
gotFilesInfos: boolean = false;
fileInfos: FileMetadata[] = [];
dir: string;
constructor(
dir: string[],
options: Partial<SVGIconsDirStreamOptions & MetadataServiceOptions>,
) {
super({ objectMode: true });
this._options = {
metadataProvider: options.metadataProvider || getMetadataService(options),
};
if (dir instanceof Array) {
this.dir = '';
this._getFilesInfos(dir);
} else {
this.dir = dir;
}
}
_getFilesInfos(files) {
let filesProcessed = 0;
this.fileInfos = [];
// Ensure prefixed files come first
files = files.slice(0).sort(fileSorter);
files.forEach((file) => {
this._options.metadataProvider(
(this.dir ? this.dir + '/' : '') + file,
(err, metadata) => {
filesProcessed++;
if (err) {
this.emit('error', err);
}
if (metadata) {
if (metadata.renamed) {
warn(
' - Saved codepoint: ' +
'u' +
metadata.unicode[0]
.codePointAt(0)
?.toString(16)
.toUpperCase() +
' for the glyph "' +
metadata.name +
'"',
);
}
this.fileInfos.push(metadata);
}
if (files.length === filesProcessed) {
// Reorder files
this.fileInfos.sort((infosA, infosB) =>
infosA.unicode[0] > infosB.unicode[0] ? 1 : -1,
);
// Mark directory as processed
this.gotFilesInfos = true;
// Start processing
this._pushSVGIcons();
}
},
);
});
}
_pushSVGIcons() {
let fileInfo: FileMetadata;
let svgIconStream: SVGIconStream;
while (this.fileInfos.length) {
fileInfo = this.fileInfos.shift() as FileMetadata;
svgIconStream = createReadStream(
fileInfo.path,
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: fileInfo.name,
unicode: fileInfo.unicode,
};
if (!this.push(svgIconStream)) {
return;
}
}
this.push(null);
}
_read() {
if (this.dir) {
readdir(this.dir, (err, files) => {
if (err) {
this.emit('error', err);
}
this._getFilesInfos(files);
});
return;
}
if (this.gotFilesInfos) {
this._pushSVGIcons();
}
}
}
export { SVGIconsDirStream };

View File

@@ -0,0 +1,580 @@
import { Transform } from 'stream';
import Sax from 'sax';
import { SVGPathData } from 'svg-pathdata';
import svgShapesToPath from './svgshapes2svgpath.js';
import {
type Matrix,
scale,
translate,
compose,
fromDefinition,
fromTransformAttribute,
} from 'transformation-matrix';
import { YError } from 'yerror';
import debug from 'debug';
const warn = debug('svgicons2svgfont');
export { fileSorter } from './filesorter.js';
export * from './iconsdir.js';
export * from './metadata.js';
function matrixFromTransformAttribute(transformAttributeString): Matrix {
return compose(
fromDefinition(fromTransformAttribute(transformAttributeString)),
);
}
// Rendering
function tagShouldRender(curTag, parents) {
let values;
return !parents.some((tag) => {
if (
'undefined' !== typeof tag.attributes.display &&
'none' === tag.attributes.display.toLowerCase()
) {
return true;
}
if (
'undefined' !== typeof tag.attributes.width &&
0 === parseFloat(tag.attributes.width)
) {
return true;
}
if (
'undefined' !== typeof tag.attributes.height &&
0 === parseFloat(tag.attributes.height)
) {
return true;
}
if ('undefined' !== typeof tag.attributes.viewBox) {
values = tag.attributes.viewBox.split(/\s*,*\s|\s,*\s*|,/);
if (0 === parseFloat(values[2]) || 0 === parseFloat(values[3])) {
return true;
}
}
return false;
});
}
// According to the document (http://www.w3.org/TR/SVG/painting.html#FillProperties)
// fill <paint> none|currentColor|inherit|<color>
// [<icccolor>]|<funciri> (not support yet)
function getTagColor(currTag, parents) {
const defaultColor = 'black';
const fillVal = currTag.attributes.fill;
let color;
const parentsLength = parents.length;
if ('none' === fillVal) {
return color;
}
if ('currentColor' === fillVal) {
return defaultColor;
}
if ('inherit' === fillVal) {
if (0 === parentsLength) {
return defaultColor;
}
return getTagColor(
parents[parentsLength - 1],
parents.slice(0, parentsLength - 1),
);
// this might be null.
// For example: <svg ><path fill="inherit" /> </svg>
// in this case getTagColor should return null
// recursive call, the bottom element should be svg,
// and svg didn't fill color, so just return null
}
return fillVal;
}
export type SVGIcons2SVGFontStreamOptions = {
fontName: string;
fontId: string;
fixedWidth: boolean;
descent: number;
ascent?: number;
round: number;
metadata: string;
usePathBounds: boolean;
normalize?: boolean;
preserveAspectRatio?: boolean;
centerHorizontally?: boolean;
centerVertically?: boolean;
fontWeight?: number;
fontHeight?: number;
fontStyle?: string;
callback?: (glyphs: Glyph[]) => void;
};
export type Glyph = {
name: string;
width: number;
height: number;
defaultHeight?: number;
defaultWidth?: number;
unicode: string[];
paths?: SVGPathData[];
};
export class SVGIcons2SVGFontStream extends Transform {
private _options: SVGIcons2SVGFontStreamOptions;
glyphs: Glyph[];
constructor(options: Partial<SVGIcons2SVGFontStreamOptions>) {
super({ objectMode: true });
this.glyphs = [];
this._options = {
...options,
fontName: options.fontName || 'iconfont',
fontId: options.fontId || options.fontName || 'iconfont',
fixedWidth: options.fixedWidth || false,
descent: options.descent || 0,
round: options.round || 10e12,
metadata: options.metadata || '',
usePathBounds: options.usePathBounds || false,
};
}
_transform(svgIconStream, _unused, svgIconStreamCallback) {
// Parsing each icons asynchronously
const saxStream = Sax.createStream(true);
const parents: (Sax.Tag | Sax.QualifiedTag)[] = [];
const transformStack: Matrix[] = [];
function applyTransform(d) {
const last = transformStack[transformStack.length - 1];
if (!last) return new SVGPathData(d);
return new SVGPathData(d).matrix(
last.a,
last.b,
last.c,
last.d,
last.e,
last.f,
);
}
const glyph = svgIconStream.metadata || {};
// init width and height os they aren't undefined if <svg> isn't renderable
glyph.width = 0;
glyph.height = 1;
glyph.paths = [];
this.glyphs.push(glyph);
if ('string' !== typeof glyph.name) {
this.emit(
'error',
new Error(
`Please provide a name for the glyph at index ${
this.glyphs.length - 1
}`,
),
);
}
if (
this.glyphs.some(
(anotherGlyph) =>
anotherGlyph !== glyph && anotherGlyph.name === glyph.name,
)
) {
this.emit(
'error',
new Error(`The glyph name "${glyph.name}" must be unique.`),
);
}
if (
glyph.unicode &&
glyph.unicode instanceof Array &&
glyph.unicode.length
) {
if (
glyph.unicode.some((unicodeA, i) =>
glyph.unicode.some((unicodeB, j) => i !== j && unicodeA === unicodeB),
)
) {
this.emit(
'error',
new Error(
`Given codepoints for the glyph "${glyph.name}" contain duplicates.`,
),
);
}
} else if ('string' !== typeof glyph.unicode) {
this.emit(
'error',
new Error(`Please provide a codepoint for the glyph "${glyph.name}"`),
);
}
if (
this.glyphs.some(
(anotherGlyph) =>
anotherGlyph !== glyph && anotherGlyph.unicode === glyph.unicode,
)
) {
this.emit(
'error',
new Error(
`The glyph "${glyph.name}" codepoint seems to be used already elsewhere.`,
),
);
}
saxStream.on('opentag', (tag) => {
let values;
let color;
parents.push(tag);
try {
const currentTransform = transformStack[transformStack.length - 1];
if ('undefined' !== typeof tag.attributes.transform) {
const transform = matrixFromTransformAttribute(
tag.attributes.transform,
);
transformStack.push(
compose([currentTransform, transform].filter(Boolean)),
);
} else {
transformStack.push(currentTransform);
}
// Checking if any parent rendering is disabled and exit if so
if (!tagShouldRender(tag, parents)) {
return;
}
// Save the view size
if ('svg' === tag.name) {
if ('viewBox' in tag.attributes) {
values = (tag.attributes.viewBox as string).split(
/\s*,*\s|\s,*\s*|,/,
);
const dX = parseFloat(values[0]);
const dY = parseFloat(values[1]);
const width = parseFloat(values[2]);
const height = parseFloat(values[3]);
// use the viewBox width/height if not specified explictly
glyph.width =
'width' in tag.attributes
? parseFloat(tag.attributes.width as string)
: width;
glyph.height =
'height' in tag.attributes
? parseFloat(tag.attributes.height as string)
: height;
transformStack[transformStack.length - 1] = compose(
[
transformStack[transformStack.length - 1],
translate(-dX, -dY),
scale(glyph.width / width, glyph.height / height),
].filter(Boolean),
);
} else {
if ('width' in tag.attributes) {
glyph.width = parseFloat(tag.attributes.width as string);
} else {
warn(
`⚠️ - Glyph "${glyph.name}" has no width attribute, using current glyph horizontal bounds.`,
);
glyph.defaultWidth = true;
}
if ('height' in tag.attributes) {
glyph.height = parseFloat(tag.attributes.height as string);
} else {
warn(
`⚠️ - Glyph "${glyph.name}" has no height attribute, using current glyph vertical bounds.`,
);
glyph.defaultHeight = true;
}
}
} else if ('clipPath' === tag.name) {
// Clipping path unsupported
warn(
`🤷 - Found a clipPath element in the icon "${glyph.name}" the result may be different than expected.`,
);
} else if ('rect' === tag.name && 'none' !== tag.attributes.fill) {
glyph.paths.push(
applyTransform(svgShapesToPath.rectToPath(tag.attributes)),
);
} else if ('line' === tag.name && 'none' !== tag.attributes.fill) {
warn(
`🤷 - Found a line element in the icon "${glyph.name}" the result could be different than expected.`,
);
glyph.paths.push(
applyTransform(svgShapesToPath.lineToPath(tag.attributes)),
);
} else if ('polyline' === tag.name && 'none' !== tag.attributes.fill) {
warn(
`🤷 - Found a polyline element in the icon "${glyph.name}" the result could be different than expected.`,
);
glyph.paths.push(
applyTransform(svgShapesToPath.polylineToPath(tag.attributes)),
);
} else if ('polygon' === tag.name && 'none' !== tag.attributes.fill) {
glyph.paths.push(
applyTransform(svgShapesToPath.polygonToPath(tag.attributes)),
);
} else if (
['circle', 'ellipse'].includes(tag.name) &&
'none' !== tag.attributes.fill
) {
glyph.paths.push(
applyTransform(svgShapesToPath.circleToPath(tag.attributes)),
);
} else if (
'path' === tag.name &&
tag.attributes.d &&
'none' !== tag.attributes.fill
) {
glyph.paths.push(applyTransform(tag.attributes.d));
}
// According to http://www.w3.org/TR/SVG/painting.html#SpecifyingPaint
// Map attribute fill to color property
if ('none' !== tag.attributes.fill) {
color = getTagColor(tag, parents);
if ('undefined' !== typeof color) {
glyph.color = color;
}
}
} catch (err) {
this.emit(
'error',
new Error(
`Got an error parsing the glyph "${glyph.name}": ${(err as Error)?.message}.`,
),
);
}
});
saxStream.on('error', (err) => {
this.emit('error', err);
});
saxStream.on('closetag', () => {
transformStack.pop();
parents.pop();
});
saxStream.on('end', () => {
svgIconStreamCallback();
});
svgIconStream.pipe(saxStream);
}
_flush(svgFontFlushCallback) {
this.glyphs.forEach((glyph) => {
if (
glyph.defaultHeight ||
glyph.defaultWidth ||
this._options.usePathBounds
) {
const glyphPath = new SVGPathData('');
(glyph.paths || []).forEach((path) => {
glyphPath.commands.push(...path.commands);
});
const bounds = glyphPath.getBounds();
if (glyph.defaultHeight || this._options.usePathBounds) {
glyph.height = bounds.maxY - bounds.minY;
}
if (glyph.defaultWidth || this._options.usePathBounds) {
glyph.width = bounds.maxX - bounds.minX;
}
}
});
const maxGlyphHeight = this.glyphs.reduce(
(curMax, glyph) => Math.max(curMax, glyph.height),
0,
);
const maxGlyphWidth = this.glyphs.reduce(
(curMax, glyph) => Math.max(curMax, glyph.width),
0,
);
const fontHeight = this._options.fontHeight || maxGlyphHeight;
let fontWidth = maxGlyphWidth;
if (this._options.normalize) {
fontWidth = this.glyphs.reduce(
(curMax, glyph) =>
Math.max(curMax, (fontHeight / glyph.height) * glyph.width),
0,
);
} else if (this._options.fontHeight) {
// even if normalize is off, we need to scale the fontWidth if we have a custom fontHeight
fontWidth *= fontHeight / maxGlyphHeight;
}
this._options.ascent =
'undefined' !== typeof this._options.ascent
? this._options.ascent
: fontHeight - this._options.descent;
if (
!this._options.normalize &&
fontHeight >
(1 < this.glyphs.length
? this.glyphs.reduce(
(curMin, glyph) => Math.min(curMin, glyph.height),
Infinity,
)
: this.glyphs[0].height)
) {
warn(
'🤷 - The provided icons do not have the same heights. This could lead' +
' to unexpected results. Using the normalize option may help.',
);
}
if (1000 > fontHeight) {
warn(
'🤷 - A fontHeight of at least than 1000 is recommended, otherwise ' +
'further steps (rounding in svg2ttf) could lead to ugly results.' +
' Use the fontHeight option to scale icons.',
);
}
// Output the SVG file
// (find a SAX parser that allows modifying SVG on the fly)
this.push(
'<?xml version="1.0" standalone="no"?>\n' +
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >\n' +
'<svg xmlns="http://www.w3.org/2000/svg">\n' +
(this._options.metadata
? '<metadata>' + this._options.metadata + '</metadata>\n'
: '') +
'<defs>\n' +
' <font id="' +
this._options.fontId +
'" horiz-adv-x="' +
fontWidth +
'">\n' +
' <font-face font-family="' +
this._options.fontName +
'"\n' +
' units-per-em="' +
fontHeight +
'" ascent="' +
this._options.ascent +
'"\n' +
' descent="' +
this._options.descent +
'"' +
(this._options.fontWeight
? '\n font-weight="' + this._options.fontWeight + '"'
: '') +
(this._options.fontStyle
? '\n font-style="' + this._options.fontStyle + '"'
: '') +
' />\n' +
' <missing-glyph horiz-adv-x="0" />\n',
);
this.glyphs.forEach((glyph) => {
const ratio = this._options.normalize
? fontHeight /
(this._options.preserveAspectRatio && glyph.width > glyph.height
? glyph.width
: glyph.height)
: fontHeight / maxGlyphHeight;
if (!isFinite(ratio)) {
throw new YError('E_BAD_COMPUTED_RATIO', ratio);
}
glyph.width *= ratio;
glyph.height *= ratio;
const glyphPath = new SVGPathData('');
if (this._options.fixedWidth) {
glyph.width = fontWidth;
}
const yOffset = glyph.height - this._options.descent;
let glyphPathTransform: Matrix = {
a: 1,
b: 0,
c: 0,
d: -1,
e: 0,
f: yOffset,
}; // ySymmetry
if (1 !== ratio) {
glyphPathTransform = compose(glyphPathTransform, scale(ratio, ratio));
}
(glyph.paths || []).forEach((path) => {
glyphPath.commands.push(
...path
.toAbs()
.matrix(
glyphPathTransform.a,
glyphPathTransform.b,
glyphPathTransform.c,
glyphPathTransform.d,
glyphPathTransform.e,
glyphPathTransform.f,
).commands,
);
});
const bounds =
(this._options.centerHorizontally || this._options.centerVertically) &&
glyphPath.getBounds();
if (this._options.centerHorizontally && bounds && 'maxX' in bounds) {
glyphPath.translate(
(glyph.width - (bounds.maxX - bounds.minX)) / 2 - bounds.minX,
);
}
if (this._options.centerVertically && bounds && 'maxX' in bounds) {
glyphPath.translate(
0,
(fontHeight - (bounds.maxY - bounds.minY)) / 2 -
bounds.minY -
this._options.descent,
);
}
delete glyph.paths;
const d = glyphPath.round(this._options.round).encode();
glyph.unicode.forEach((unicode, i) => {
const unicodeStr = [...unicode]
.map(
(char) =>
'&#x' + char.codePointAt(0)!.toString(16).toUpperCase() + ';',
)
.join('');
this.push(
' <glyph glyph-name="' +
glyph.name +
(0 === i ? '' : '-' + i) +
'"\n' +
' unicode="' +
unicodeStr +
'"\n' +
' horiz-adv-x="' +
glyph.width +
'" d="' +
d +
'" />\n',
);
});
});
this.push(' </font>\n' + '</defs>\n' + '</svg>\n');
warn('✅ - Font created');
if ('function' === typeof this._options.callback) {
this._options.callback(this.glyphs);
}
svgFontFlushCallback();
}
}

View File

@@ -0,0 +1,104 @@
import { join, dirname, basename } from 'node:path';
import { rename } from 'node:fs';
export type MetadataServiceOptions = {
prependUnicode: boolean;
startUnicode: number;
};
export type FileMetadata = {
path: string;
name: string;
unicode: string[] | string;
renamed: boolean;
};
function getMetadataService(options: Partial<MetadataServiceOptions> = {}) {
const usedUnicodes = [] as string[];
// Default options
const _options: MetadataServiceOptions = {
prependUnicode: !!options.prependUnicode,
startUnicode:
'number' === typeof options.startUnicode ? options.startUnicode : 0xea01,
};
return function getMetadataFromFile(
file: string,
cb: (error: Error | null, metadata?: FileMetadata) => void,
) {
const fileBasename = basename(file);
const metadata: FileMetadata = {
path: file,
name: '',
unicode: [],
renamed: false,
};
const matches = fileBasename.match(
/^(?:((?:u[0-9a-f]{4,6},?)+)-)?(.+)\.svg$/i,
);
metadata.name =
matches && matches[2] ? matches[2] : 'icon' + _options.startUnicode;
if (matches && matches[1]) {
metadata.unicode = matches[1].split(',').map((match) => {
match = match.substring(1);
return match
.split('u')
.map((code) => String.fromCodePoint(parseInt(code, 16)))
.join('');
});
if (-1 !== usedUnicodes.indexOf(metadata.unicode[0])) {
cb(
new Error(
'The unicode codepoint of the glyph ' +
metadata.name +
' seems to be already used by another glyph.',
),
);
return;
}
usedUnicodes.push(...metadata.unicode);
} else {
do {
(metadata.unicode as string[])[0] = String.fromCodePoint(
_options.startUnicode++,
);
} while (usedUnicodes.includes(metadata.unicode[0]));
usedUnicodes.push(metadata.unicode[0]);
if (_options.prependUnicode) {
metadata.renamed = true;
metadata.path = join(
dirname(file),
'u' +
metadata.unicode[0].codePointAt(0)?.toString(16).toUpperCase() +
'-' +
fileBasename,
);
rename(file, metadata.path, (err) => {
if (err) {
cb(
new Error(
'Could not save codepoint: ' +
'u' +
metadata.unicode[0]
.codePointAt(0)
?.toString(16)
.toUpperCase() +
' for ' +
fileBasename,
),
);
return;
}
cb(null, metadata);
});
}
}
if (!metadata.renamed) {
setImmediate(() => cb(null, metadata));
}
};
}
export { getMetadataService };

View File

@@ -0,0 +1,132 @@
const svgShapesToPath = {
rectToPath: svgShapesToPathRectToPath,
polylineToPath: svgShapesToPathPolylineToPath,
lineToPath: svgShapesToPathLineToPath,
circleToPath: svgShapesToPathCircleToPath,
polygonToPath: svgShapesToPathPolygonToPath,
};
export default svgShapesToPath;
// Shapes helpers (should also move elsewhere)
function svgShapesToPathRectToPath(attributes) {
const x = 'undefined' !== typeof attributes.x ? parseFloat(attributes.x) : 0;
const y = 'undefined' !== typeof attributes.y ? parseFloat(attributes.y) : 0;
const width =
'undefined' !== typeof attributes.width ? parseFloat(attributes.width) : 0;
const height =
'undefined' !== typeof attributes.height
? parseFloat(attributes.height)
: 0;
const rx =
'undefined' !== typeof attributes.rx
? parseFloat(attributes.rx)
: 'undefined' !== typeof attributes.ry
? parseFloat(attributes.ry)
: 0;
const ry =
'undefined' !== typeof attributes.ry ? parseFloat(attributes.ry) : rx;
return (
'' +
// start at the left corner
'M' +
(x + rx) +
' ' +
y +
// top line
'h' +
(width - rx * 2) +
// upper right corner
(rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry : '') +
// Draw right side
'v' +
(height - ry * 2) +
// Draw bottom right corner
(rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry : '') +
// Down the down side
'h' +
(width - rx * 2) * -1 +
// Draw bottom right corner
(rx || ry
? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx * -1 + ' ' + ry * -1
: '') +
// Down the left side
'v' +
(height - ry * 2) * -1 +
// Draw bottom right corner
(rx || ry ? 'a ' + rx + ' ' + ry + ' 0 0 1 ' + rx + ' ' + ry * -1 : '') +
// Close path
'z'
);
}
function svgShapesToPathPolylineToPath(attributes) {
return 'M' + attributes.points;
}
function svgShapesToPathLineToPath(attributes) {
// Move to the line start
return (
'' +
'M' +
(parseFloat(attributes.x1) || 0).toString(10) +
' ' +
(parseFloat(attributes.y1) || 0).toString(10) +
' ' +
((parseFloat(attributes.x1) || 0) + 1).toString(10) +
' ' +
((parseFloat(attributes.y1) || 0) + 1).toString(10) +
' ' +
((parseFloat(attributes.x2) || 0) + 1).toString(10) +
' ' +
((parseFloat(attributes.y2) || 0) + 1).toString(10) +
' ' +
(parseFloat(attributes.x2) || 0).toString(10) +
' ' +
(parseFloat(attributes.y2) || 0).toString(10) +
'Z'
);
}
function svgShapesToPathCircleToPath(attributes) {
const cx = parseFloat(attributes.cx || 0);
const cy = parseFloat(attributes.cy || 0);
const rx =
'undefined' !== typeof attributes.rx
? parseFloat(attributes.rx)
: parseFloat(attributes.r);
const ry =
'undefined' !== typeof attributes.ry
? parseFloat(attributes.ry)
: parseFloat(attributes.r);
// use two A commands because one command which returns to origin is invalid
return (
'' +
'M' +
(cx - rx) +
',' +
cy +
'A' +
rx +
',' +
ry +
' 0,0,0 ' +
(cx + rx) +
',' +
cy +
'A' +
rx +
',' +
ry +
' 0,0,0 ' +
(cx - rx) +
',' +
cy
);
}
function svgShapesToPathPolygonToPath(attributes) {
return 'M' + attributes.points + 'Z';
}

View File

@@ -0,0 +1,98 @@
import { describe, test, expect } from '@jest/globals';
import { readFile, mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
try {
await mkdir(join('fixtures', 'results'));
} catch (err) {
// empty
}
describe('Testing CLI', () => {
test('should work for simple SVG', async () => {
const command =
`${'node' + ' '}${join('bin', 'svgicons2svgfont.js')} -o ${join(
'fixtures',
'results',
'originalicons-cli.svg',
)} -s 0xE001` + ` ${join('fixtures', 'icons', 'originalicons', '*.svg')}`;
await promisify(exec)(command);
expect(
await readFile(join('fixtures', 'results', 'originalicons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(join('fixtures', 'expected', 'originalicons-cli.svg'), {
encoding: 'utf8',
}),
);
});
test('should work for more than 32 SVG icons', async () => {
const command =
'node' +
' ' +
join('bin', 'svgicons2svgfont.js') +
' -o ' +
join('fixtures', 'results', 'lotoficons-cli.svg') +
' -s 0xE001' +
' -r 1e4' +
' ' +
join('fixtures', 'icons', 'cleanicons', '*.svg') +
' ' +
join('fixtures', 'icons', 'hiddenpathesicons', '*.svg') +
' ' +
join('fixtures', 'icons', 'multipathicons', 'kikoolol.svg') +
' ' +
join('fixtures', 'icons', 'originalicons', '*.svg') +
' ' +
join('fixtures', 'icons', 'realicons', '*.svg') +
' ' +
join('fixtures', 'icons', 'roundedcorners', '*.svg') +
' ' +
join('fixtures', 'icons', 'shapeicons', '*.svg') +
' ' +
join('fixtures', 'icons', 'tocentericons', '*.svg');
await promisify(exec)(command);
expect(
await readFile(join('fixtures', 'results', 'lotoficons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(join('fixtures', 'expected', 'lotoficons-cli.svg'), {
encoding: 'utf8',
}),
);
});
describe('with nested icons', () => {
test('should work', async () => {
const command = `${'node' + ' '}${join(
'bin',
'svgicons2svgfont.js',
)} -o ${join(
'fixtures',
'results',
'nestedicons-cli.svg',
)} ${join('fixtures', 'icons', 'nestedicons', '*.svg')}`;
await promisify(exec)(command);
expect(
await readFile(join('fixtures', 'results', 'nestedicons-cli.svg'), {
encoding: 'utf8',
}),
).toEqual(
await readFile(join('fixtures', 'expected', 'nestedicons-cli.svg'), {
encoding: 'utf8',
}),
);
});
});
});

View File

@@ -0,0 +1,90 @@
import { describe, test, expect } from '@jest/globals';
import { fileSorter } from '../filesorter.js';
describe('fileSorter', () => {
test('should sort files per filename', () => {
expect(
[
'/var/plop/c.svg',
'/var/plop/a.svg',
'/var/plop/A.svg',
'/var/plop/C.svg',
'/var/plop/B.svg',
'/var/plop/b.svg',
].sort(fileSorter),
).toEqual([
'/var/plop/A.svg',
'/var/plop/B.svg',
'/var/plop/C.svg',
'/var/plop/a.svg',
'/var/plop/b.svg',
'/var/plop/c.svg',
]);
});
test('should sort files per codepoints', () => {
expect(
[
'/var/plop/uAE01-c.svg',
'/var/plop/uAE03-a.svg',
'/var/plop/uAE02-A.svg',
'/var/plop/uAE06-C.svg',
'/var/plop/uAE04-B.svg',
'/var/plop/uAE05-b.svg',
].sort(fileSorter),
).toEqual([
'/var/plop/uAE01-c.svg',
'/var/plop/uAE02-A.svg',
'/var/plop/uAE03-a.svg',
'/var/plop/uAE04-B.svg',
'/var/plop/uAE05-b.svg',
'/var/plop/uAE06-C.svg',
]);
});
test('should put codepoints first', () => {
expect(
[
'/var/plop/uAE01-c.svg',
'/var/plop/uAE03-a.svg',
'/var/plop/uAE02-A.svg',
'/var/plop/C.svg',
'/var/plop/B.svg',
'/var/plop/b.svg',
].sort(fileSorter),
).toEqual([
'/var/plop/uAE01-c.svg',
'/var/plop/uAE02-A.svg',
'/var/plop/uAE03-a.svg',
'/var/plop/B.svg',
'/var/plop/C.svg',
'/var/plop/b.svg',
]);
});
test('should work with the @pinin files', () => {
expect(
[
'bell-disabled.svg',
'bell-disabled-o.svg',
'bell-o.svg',
'UEA01-calendar-agenda.svg',
'UEA02-calendar-alert.svg',
'UEA03-calendar.svg',
'uEA04-bookmark-favorite.svg',
'uEA05-bookmark-o.svg',
'uEA06-bookmark.svg',
].sort(fileSorter),
).toEqual([
'UEA01-calendar-agenda.svg',
'UEA02-calendar-alert.svg',
'UEA03-calendar.svg',
'uEA04-bookmark-favorite.svg',
'uEA05-bookmark-o.svg',
'uEA06-bookmark.svg',
'bell-disabled.svg',
'bell-disabled-o.svg',
'bell-o.svg',
]);
});
});

View File

@@ -0,0 +1,775 @@
import { describe, test, expect } from '@jest/globals';
import assert from 'assert';
import { Readable } from 'node:stream';
import fs from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
import { SVGIcons2SVGFontStream } from '../index.js';
import { SVGIconsDirStream, type SVGIconStream } from '../iconsdir.js';
import streamtest from 'streamtest';
import { BufferStream } from 'bufferstreams';
try {
await mkdir(join('fixtures', 'results'));
} catch (err) {
// empty
}
const codepoint = JSON.parse(
fs.readFileSync('./fixtures/expected/test-codepoint.json').toString(),
);
// Helpers
async function generateFontToFile(options, fileSuffix?, startUnicode?, files?) {
const dest = join(
'fixtures',
'results',
`${options.fontName + (fileSuffix || '')}.svg`,
);
let resolve;
let reject;
const promise = new Promise((_resolve, _reject) => {
resolve = _resolve;
reject = _reject;
});
options.log = () => {};
options.round = options.round || 1e3;
const svgFontStream = new SVGIcons2SVGFontStream(options);
svgFontStream.pipe(fs.createWriteStream(dest)).on('finish', () => {
try {
expect(fs.readFileSync(dest, { encoding: 'utf8' })).toEqual(
fs.readFileSync(
join(
'fixtures',
'expected',
`${options.fontName + (fileSuffix || '')}.svg`,
),
{ encoding: 'utf8' },
),
);
resolve();
} catch (err) {
reject(err);
}
});
new SVGIconsDirStream(files || join('fixtures', 'icons', options.fontName), {
startUnicode: startUnicode || 0xe001,
}).pipe(svgFontStream);
return await promise;
}
async function generateFontToMemory(options, files?, startUnicode?) {
options.log = () => {};
options.round = options.round || 1e3;
options.callback = (glyphs) => {
const fontName = options.fontName;
expect(glyphs).toEqual(codepoint[fontName]);
};
const svgFontStream = new SVGIcons2SVGFontStream(options);
const promise = bufferStream(svgFontStream);
new SVGIconsDirStream(files || join('fixtures', 'icons', options.fontName), {
startUnicode: startUnicode || 0xe001,
}).pipe(svgFontStream);
expect((await promise).toString()).toEqual(
fs.readFileSync(join('fixtures', 'expected', `${options.fontName}.svg`), {
encoding: 'utf8',
}),
);
}
// Tests
describe('Generating fonts to files', () => {
test('should work for simple SVG', async () => {
await generateFontToFile({
fontName: 'originalicons',
});
});
test('should work for simple fixedWidth and normalize option', async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fixedWidth: true,
normalize: true,
},
'n',
);
});
test('should work for simple SVG', async () => {
await generateFontToFile({
fontName: 'cleanicons',
});
});
test('should work for simple SVG and custom ascent', async () => {
await generateFontToFile(
{
fontName: 'cleanicons',
ascent: 100,
},
'-ascent',
);
});
test('should work for simple SVG and custom properties', async () => {
await generateFontToFile(
{
fontName: 'cleanicons',
fontStyle: 'italic',
fontWeight: 'bold',
},
'-stw',
);
});
test('should work for codepoint mapped SVG icons', async () => {
await generateFontToFile({
fontName: 'prefixedicons',
callback: () => {},
});
});
test('should work with multipath SVG icons', async () => {
await generateFontToFile({
fontName: 'multipathicons',
});
});
test('should work with simple shapes SVG icons', async () => {
await generateFontToFile({
fontName: 'shapeicons',
});
});
test('should work with variable height icons', async () => {
await generateFontToFile({
fontName: 'variableheighticons',
});
});
test('should work with variable height icons and the normalize option', async () => {
await generateFontToFile(
{
fontName: 'variableheighticons',
normalize: true,
},
'n',
);
});
test('should work with variable height icons, the normalize option and the preserveAspectRatio option', async () => {
await generateFontToFile(
{
fontName: 'variableheighticons',
normalize: true,
preserveAspectRatio: true,
},
'np',
);
});
test('should work with variable width icons', async () => {
await generateFontToFile({
fontName: 'variablewidthicons',
});
});
test('should work with centered variable width icons and the fixed width option', async () => {
await generateFontToFile(
{
fontName: 'variablewidthicons',
fixedWidth: true,
centerHorizontally: true,
},
'n',
);
});
test('should calculate bounds when not specified in the svg file', async () => {
await generateFontToFile({
fontName: 'calcbounds',
});
});
test('should work with a font id', async () => {
await generateFontToFile(
{
fontName: 'variablewidthicons',
fixedWidth: true,
centerHorizontally: true,
fontId: 'plop',
},
'id',
);
});
test('should work with scaled icons', async () => {
await generateFontToFile({
fontName: 'scaledicons',
fixedWidth: true,
centerHorizontally: true,
fontId: 'plop',
});
});
test('should not display hidden paths', async () => {
await generateFontToFile({
fontName: 'hiddenpathesicons',
});
});
test('should work with real world icons', async () => {
await generateFontToFile({
fontName: 'realicons',
});
});
test('should work with rendering test SVG icons', async () => {
await generateFontToFile({
fontName: 'rendricons',
});
});
test('should work with a single SVG icon', async () => {
await generateFontToFile({
fontName: 'singleicon',
});
});
test('should work with transformed SVG icons', async () => {
await generateFontToFile({
fontName: 'transformedicons',
});
});
test('should work when horizontally centering SVG icons', async () => {
await generateFontToFile({
fontName: 'tocentericons',
centerHorizontally: true,
});
});
test('should work when vertically centering SVG icons', async () => {
await generateFontToFile({
fontName: 'toverticalcentericons',
centerVertically: true,
});
});
test('should work with a icons with path with fill none', async () => {
await generateFontToFile({
fontName: 'pathfillnone',
});
});
test('should work with shapes with rounded corners', async () => {
await generateFontToFile({
fontName: 'roundedcorners',
});
});
test('should work with realworld icons', async () => {
await generateFontToFile({
fontName: 'realworld',
});
});
test('should work with a lot of icons', async () => {
await generateFontToFile(
{
fontName: 'lotoficons',
},
'',
0,
[
'fixtures/icons/cleanicons/account.svg',
'fixtures/icons/cleanicons/arrow-down.svg',
'fixtures/icons/cleanicons/arrow-left.svg',
'fixtures/icons/cleanicons/arrow-right.svg',
'fixtures/icons/cleanicons/arrow-up.svg',
'fixtures/icons/cleanicons/basket.svg',
'fixtures/icons/cleanicons/close.svg',
'fixtures/icons/cleanicons/minus.svg',
'fixtures/icons/cleanicons/plus.svg',
'fixtures/icons/cleanicons/search.svg',
'fixtures/icons/hiddenpathesicons/sound--off.svg',
'fixtures/icons/hiddenpathesicons/sound--on.svg',
'fixtures/icons/multipathicons/kikoolol.svg',
'fixtures/icons/originalicons/mute.svg',
'fixtures/icons/originalicons/sound.svg',
'fixtures/icons/originalicons/speaker.svg',
'fixtures/icons/realicons/diegoliv.svg',
'fixtures/icons/realicons/hannesjohansson.svg',
'fixtures/icons/realicons/roelvanhitum.svg',
'fixtures/icons/realicons/safety-icon.svg',
'fixtures/icons/realicons/sb-icon.svg',
'fixtures/icons/realicons/settings-icon.svg',
'fixtures/icons/realicons/track-icon.svg',
'fixtures/icons/realicons/web-icon.svg',
'fixtures/icons/roundedcorners/roundedrect.svg',
'fixtures/icons/shapeicons/circle.svg',
'fixtures/icons/shapeicons/ellipse.svg',
'fixtures/icons/shapeicons/lines.svg',
'fixtures/icons/shapeicons/polygon.svg',
'fixtures/icons/shapeicons/polyline.svg',
'fixtures/icons/shapeicons/rect.svg',
'fixtures/icons/tocentericons/bottomleft.svg',
'fixtures/icons/tocentericons/center.svg',
'fixtures/icons/tocentericons/topright.svg',
],
);
});
test('should work with rotated rectangle icon', async () => {
await generateFontToFile({
fontName: 'rotatedrectangle',
});
});
/**
* Issue #6
* icon by @paesku
* https://github.com/nfroidure/svgicons2svgfont/issues/6#issuecomment-125545925
*/
test('should work with complicated nested transforms', async () => {
await generateFontToFile({
fontName: 'paesku',
round: 1e3,
});
});
/**
* Issue #76
* https://github.com/nfroidure/svgicons2svgfont/issues/76#issue-259831969
*/
test('should work with transform=translate(x) without y', async () => {
await generateFontToFile({
fontName: 'translatex',
round: 1e3,
});
});
test('should work with skew', async () => {
await generateFontToFile({
fontName: 'skew',
});
});
test('should work when only rx is present', async () => {
await generateFontToFile({
fontName: 'onlywithrx',
});
});
test('should work when only ry is present', async () => {
await generateFontToFile({
fontName: 'onlywithry',
});
});
});
describe('Generating fonts to memory', () => {
test('should work for simple SVG', async () => {
await generateFontToMemory({
fontName: 'originalicons',
});
});
test('should work for simple SVG', async () => {
await generateFontToMemory({
fontName: 'cleanicons',
});
});
test('should work for codepoint mapped SVG icons', async () => {
await generateFontToMemory({
fontName: 'prefixedicons',
});
});
test('should work with multipath SVG icons', async () => {
await generateFontToMemory({
fontName: 'multipathicons',
});
});
test('should work with simple shapes SVG icons', async () => {
await generateFontToMemory({
fontName: 'shapeicons',
});
});
});
describe('Using options', () => {
test('should work with fixedWidth option set to true', async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fixedWidth: true,
},
'2',
);
});
test('should work with custom fontHeight option', async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fontHeight: 800,
},
'3',
);
});
test('should work with custom descent option', async () => {
await generateFontToFile(
{
fontName: 'originalicons',
descent: 200,
},
'4',
);
});
test('should work with fixedWidth set to true and with custom fontHeight option', async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fontHeight: 800,
fixedWidth: true,
},
'5',
);
});
test(
'should work with fixedWidth and centerHorizontally set to true and with' +
' custom fontHeight option',
async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fontHeight: 800,
fixedWidth: true,
centerHorizontally: true,
round: 1e5,
},
'6',
);
},
);
test(
'should work with fixedWidth, normalize and centerHorizontally set to' +
' true and with custom fontHeight option',
async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fontHeight: 800,
normalize: true,
fixedWidth: true,
centerHorizontally: true,
round: 1e5,
},
'7',
);
},
);
test(
'should work with fixedWidth, normalize and centerHorizontally set to' +
' true and with a large custom fontHeight option',
async () => {
await generateFontToFile(
{
fontName: 'originalicons',
fontHeight: 5000,
normalize: true,
fixedWidth: true,
centerHorizontally: true,
round: 1e5,
},
'8',
);
},
);
test('should work with nested icons', async () => {
await generateFontToFile(
{
fontName: 'nestedicons',
},
'',
0xea01,
);
});
});
describe('Passing code points', () => {
test('should work with multiple unicode values for a single icon', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'account',
unicode: ['\uE001', '\uE002'],
};
const promise = bufferStream(svgFontStream);
svgFontStream.write(svgIconStream);
svgFontStream.end();
assert.equal(
await promise,
fs.readFileSync(join('fixtures', 'expected', 'cleanicons-multi.svg'), {
encoding: 'utf8',
}),
);
});
test('should work with ligatures', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'account',
unicode: ['\uE001\uE002'],
};
const promise = bufferStream(svgFontStream);
svgFontStream.write(svgIconStream);
svgFontStream.end();
assert.equal(
await promise,
fs.readFileSync(join('fixtures', 'expected', 'cleanicons-lig.svg'), {
encoding: 'utf8',
}),
);
});
test('should work with high code points', async () => {
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'account',
unicode: ['\u{1f63a}'],
};
const promise = bufferStream(svgFontStream);
svgFontStream.write(svgIconStream);
svgFontStream.end();
assert.equal(
(await promise).toString(),
fs.readFileSync(join('fixtures', 'expected', 'cleanicons-high.svg'), {
encoding: 'utf8',
}),
);
});
});
describe('Providing bad glyphs', () => {
test('should fail when not providing glyph name', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: undefined as unknown as string,
unicode: '\uE001',
};
new SVGIcons2SVGFontStream({ round: 1e3 })
.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(
err.message,
'Please provide a name for the glyph at index 0',
);
})
.write(svgIconStream);
});
test('should fail when not providing codepoints', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'test',
unicode: undefined as unknown as string[],
};
new SVGIcons2SVGFontStream({ round: 1e3 })
.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(
err.message,
'Please provide a codepoint for the glyph "test"',
);
})
.write(svgIconStream);
});
test('should fail when providing unicode value with duplicates', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'test',
unicode: ['\uE002', '\uE002'],
};
new SVGIcons2SVGFontStream({ round: 1e3 })
.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(
err.message,
'Given codepoints for the glyph "test" contain duplicates.',
);
})
.write(svgIconStream);
});
test('should fail when providing the same codepoint twice', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgIconStream2 = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgFontStream = new SVGIcons2SVGFontStream({
round: 1e3,
});
svgIconStream.metadata = {
name: 'test',
unicode: '\uE002',
};
svgIconStream2.metadata = {
name: 'test2',
unicode: '\uE002',
};
svgFontStream.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(
err.message,
'The glyph "test2" codepoint seems to be used already elsewhere.',
);
});
svgFontStream.write(svgIconStream);
svgFontStream.write(svgIconStream2);
});
test('should fail when providing the same name twice', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgIconStream2 = fs.createReadStream(
join('fixtures', 'icons', 'cleanicons', 'account.svg'),
) as unknown as SVGIconStream;
const svgFontStream = new SVGIcons2SVGFontStream({ round: 1e3 });
svgIconStream.metadata = {
name: 'test',
unicode: '\uE001',
};
svgIconStream2.metadata = {
name: 'test',
unicode: '\uE002',
};
svgFontStream.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(err.message, 'The glyph name "test" must be unique.');
});
svgFontStream.write(svgIconStream);
svgFontStream.write(svgIconStream2);
});
test('should fail when providing bad pathdata', async () => {
const svgIconStream = fs.createReadStream(
join('fixtures', 'icons', 'badicons', 'pathdata.svg'),
) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'test',
unicode: ['\uE002'],
};
new SVGIcons2SVGFontStream({ round: 1e3 })
.on('error', (err) => {
assert.equal(err instanceof Error, true);
assert.equal(
err.message,
'Got an error parsing the glyph "test":' +
' Expected a flag, got "20" at index "23".',
);
})
.on('end', () => {})
.write(svgIconStream);
});
test('should fail when providing bad XML', async () => {
const svgIconStream = streamtest.fromChunks([
Buffer.from('bad'),
Buffer.from('xml'),
]) as unknown as SVGIconStream;
svgIconStream.metadata = {
name: 'test',
unicode: ['\uE002'],
};
let firstError = true;
new SVGIcons2SVGFontStream({ round: 1e3 })
.on('error', (err) => {
assert.equal(err instanceof Error, true);
if (firstError) {
firstError = false;
assert.equal(
err.message,
'Non-whitespace before first tag.\nLine: 0\nColumn: 1\nChar: b',
);
}
})
.write(svgIconStream);
});
});
async function bufferStream(readableStream: Readable) {
return await new Promise<Buffer>((resolve, reject) => {
readableStream.pipe(
new BufferStream((err, buf) => {
if (err) {
return reject(err);
}
resolve(buf);
}),
);
});
}

View File

@@ -0,0 +1,215 @@
import { describe, test, expect } from '@jest/globals';
import { writeFile, readFile, unlink } from 'node:fs/promises';
import { promisify } from 'node:util';
import { getMetadataService } from '../metadata.js';
import { YError } from 'yerror';
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
try {
await mkdir(join('fixtures', 'results'));
} catch (err) {
// empty
}
describe('Metadata service', () => {
describe('for code generation', () => {
test('should extract right unicodes from files', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)('/var/plop/hello.svg');
expect(infos).toEqual({
path: '/var/plop/hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0xea01)],
renamed: false,
});
});
test('should append unicodes to files when the option is set', async () => {
const metadataService = getMetadataService({
prependUnicode: true,
});
await writeFile(join('fixtures', 'results', 'plop.svg'), 'plop', 'utf-8');
const infos = await promisify(metadataService)(
join('fixtures', 'results', 'plop.svg'),
);
expect(infos).toEqual({
path: join('fixtures', 'results', 'uEA01-plop.svg'),
name: 'plop',
unicode: [String.fromCharCode(0xea01)],
renamed: true,
});
expect(
await readFile(join('fixtures', 'results', 'uEA01-plop.svg')),
).toBeTruthy();
unlink(join('fixtures', 'results', 'uEA01-plop.svg'));
try {
await readFile(join('fixtures', 'results', 'plop.svg'));
throw new YError('E_UNEXPECTED_SUCCESS');
} catch (err) {
expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy();
}
});
test('should log file rename errors', async () => {
const metadataService = getMetadataService({
prependUnicode: true,
startUnicode: 0xea02,
});
try {
await promisify(metadataService)(
join('fixtures', 'results', 'plop.svg'),
);
throw new YError('E_UNEXPECTED_SUCCESS');
} catch (err) {
expect(err).toBeTruthy();
expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy();
}
try {
await readFile(join('fixtures', 'results', 'uEA02-plop.svg'));
throw new YError('E_UNEXPECTED_SUCCESS');
} catch (err) {
expect((err as YError).code === 'E_UNEXPECTED_SUCCESS').toBeFalsy();
}
});
});
describe('for code extraction', () => {
test('should work for simple codes', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/u0001-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/u0001-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0x0001)],
renamed: false,
});
});
test('should work for several codes', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/u0001,u0002-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/u0001,u0002-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0x0001), String.fromCharCode(0x0002)],
renamed: false,
});
});
test('should work for higher codepoint codes', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/u1F63A-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/u1F63A-hello.svg',
name: 'hello',
unicode: [String.fromCodePoint(0x1f63a)],
renamed: false,
});
});
test('should work for ligature codes', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/u0001u0002-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/u0001u0002-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0x0001) + String.fromCharCode(0x0002)],
renamed: false,
});
});
test('should work for nested codes', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/u0001u0002,u0001-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/u0001u0002,u0001-hello.svg',
name: 'hello',
unicode: [
String.fromCharCode(0x0001) + String.fromCharCode(0x0002),
String.fromCharCode(0x0001),
],
renamed: false,
});
});
test('should not set the same codepoint twice', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/uEA01-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/uEA01-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0xea01)],
renamed: false,
});
const infos2 = await promisify(metadataService)('/var/plop/plop.svg');
expect(infos2).toEqual({
path: '/var/plop/plop.svg',
name: 'plop',
unicode: [String.fromCharCode(0xea02)],
renamed: false,
});
});
test('should not set the same codepoint twice with different cases', async () => {
const metadataService = getMetadataService();
const infos = await promisify(metadataService)(
'/var/plop/UEA01-hello.svg',
);
expect(infos).toEqual({
path: '/var/plop/UEA01-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0xea01)],
renamed: false,
});
const infos2 = await promisify(metadataService)(
'/var/plop/uEA02-hello.svg',
);
expect(infos2).toEqual({
path: '/var/plop/uEA02-hello.svg',
name: 'hello',
unicode: [String.fromCharCode(0xea02)],
renamed: false,
});
const infos3 = await promisify(metadataService)('/var/plop/bell-o.svg');
expect(infos3).toEqual({
path: '/var/plop/bell-o.svg',
name: 'bell-o',
unicode: [String.fromCharCode(0xea03)],
renamed: false,
});
});
});
});