Skip to content

Commit eb678f7

Browse files
committed
fixed svg favicons when the input size is smaller than output
1 parent 08f7a16 commit eb678f7

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Go to the `v1` branch to see the changelog of Lume 1.
2626
### Fixed
2727
- Ensure console output for validate_html and seo plugins is shown after the build process.
2828
- Ensure `metas` plugin is registered after `og_images`.
29+
- `favicon` plugin: scale the SVG icons before render to PNG.
2930
- `run` command.
3031
- Added missing tests for SEO plugin.
3132
- Renamed `Config` interface of `seo` and `validate_html` plugins to `Options`.

deps/resvg.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { initWasm, Resvg } from "npm:@resvg/[email protected]";
1+
import {
2+
initWasm,
3+
Resvg,
4+
type ResvgRenderOptions,
5+
} from "npm:@resvg/[email protected]";
6+
export type { ResvgRenderOptions };
27

38
const url =
49
"https://cdn.jsdelivr.net/npm/@resvg/[email protected]/index_bg.wasm";
510
await initWasm(url);
611

7-
export function toPng(svg: string): Uint8Array {
8-
const resvg = new Resvg(svg, { fitTo: { mode: "original" } });
12+
export function toPng(
13+
svg: string,
14+
options: ResvgRenderOptions = { fitTo: { mode: "original" } },
15+
): Uint8Array {
16+
const resvg = new Resvg(svg, options);
917
const pngData = resvg.render();
1018
return pngData.asPng();
1119
}

deps/sharp.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export { default } from "npm:[email protected]";
22
import sharp from "npm:[email protected]";
33
import icoEndec from "npm:[email protected]";
4-
import { toPng } from "./resvg.ts";
4+
import { type ResvgRenderOptions, toPng } from "./resvg.ts";
55

66
export async function sharpsToIco(...images: sharp.Sharp[]) {
77
const buffers = await Promise.all(
@@ -15,10 +15,11 @@ export async function sharpsToIco(...images: sharp.Sharp[]) {
1515
export function create(
1616
content: Uint8Array | string,
1717
config: sharp.SharpOptions = {},
18+
svgOptions?: ResvgRenderOptions,
1819
): sharp.Sharp {
1920
// It's a SVG
2021
if (typeof content === "string") {
21-
return sharp(toPng(content));
22+
return sharp(toPng(content, svgOptions));
2223
}
2324

2425
return sharp(content, config);

plugins/favicon.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,19 @@ async function buildIco(
176176
}
177177
}
178178

179+
const svgOptions = {
180+
fitTo: { mode: "width", value: Math.max(...size) },
181+
} as const;
179182
let image: Uint8Array;
180183

181184
if (format === "ico") {
182185
const resizeOptions = { background: { r: 0, g: 0, b: 0, alpha: 0 } };
183-
const img = create(content);
186+
const img = create(content, undefined, svgOptions);
184187
image = await sharpsToIco(
185188
...size.map((size) => img.clone().resize(size, size, resizeOptions)),
186189
);
187190
} else {
188-
image = await create(content)
191+
image = await create(content, undefined, svgOptions)
189192
.resize(size[0], size[0])
190193
.toFormat(format)
191194
.toBuffer();

0 commit comments

Comments
 (0)