Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ Given an SVG, `whaticon` renders it and compares against indexed icons using per
## Installation

```bash
npm install whaticon
npm install whaticon sharp
```

[sharp](https://sharp.pixelplumbing.com/) renders SVG for the CLI and the Node API. It is an optional peer dependency, so a host that rasterizes icons itself and uses only `whaticon/hash` can leave it out. To run the CLI without installing anything:

```bash
npx -p whaticon -p sharp whaticon icon.svg
```

## CLI Usage
Expand Down
8 changes: 7 additions & 1 deletion bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
},
"dependencies": {
"citty": "^0.2.2",
"sharp": "^0.35.5",
"valibot": "^1.5.0"
},
"devDependencies": {
Expand All @@ -84,6 +83,7 @@
"oxfmt": "^0.79.0",
"oxlint": "^1.94.0",
"oxlint-tsgolint": "^7.0.2007",
"sharp": "^0.35.5",
"typescript": "^7.0.2"
},
"packageManager": "bun@1.4.2",
Expand All @@ -92,5 +92,13 @@
},
"imports": {
"#src/*": "./src/*"
},
"peerDependencies": {
"sharp": "^0.35.5"
},
"peerDependenciesMeta": {
"sharp": {
"optional": true
}
}
}
25 changes: 24 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { gunzipSync } from 'node:zlib'
import sharp from 'sharp'

import {
DEFAULT_SIZE,
Expand All @@ -12,6 +11,8 @@ import {
type IconMatch
} from './hash.js'

import type { Sharp, SharpOptions } from 'sharp'

export {
DEFAULT_SIZE,
HASH_SCHEME,
Expand All @@ -27,13 +28,35 @@ export {
/** Indexes hold hashes of `DEFAULT_SIZE`, so a query is always hashed at that size. */
export type MatchOptions = HashMatchOptions

type SharpFactory = (input: Buffer, options?: SharpOptions) => Sharp

let sharpModule: Promise<SharpFactory> | undefined

/**
* sharp, which renders SVG here. It is an optional peer dependency, so hosts that rasterize
* themselves and use only `whaticon/hash` do not install it.
*/
function loadSharp(): Promise<SharpFactory> {
sharpModule ??= import('sharp').then(
(module) => module.default,
(error: unknown) => {
sharpModule = undefined
throw new Error('whaticon renders SVG with sharp; install it: npm install sharp', {
cause: error
})
}
)
return sharpModule
}

/**
* An icon drawn in a `width` by `height` box as darkness on white: how much of each pixel its
* shapes cover, whatever their color, so an icon drawn in white or in a brand color hashes as the
* same icon drawn in black. The SVG is rasterized at the size it is fitted to, not resampled from
* its own size, and centered as `HASH_SCHEME` describes.
*/
async function coverage(svg: string | Buffer, width: number, height: number): Promise<Buffer> {
const sharp = await loadSharp()
const input = typeof svg === 'string' ? Buffer.from(svg) : svg
const own = await sharp(input).metadata()
const scale = Math.min(width / (own.width || width), height / (own.height || height))
Expand Down
Loading