Skip to content

Commit 9c929a7

Browse files
committed
add reset() method to FontLibrary
- removes any local fonts added with the use() method - requested in samizdatco#94
1 parent e412842 commit 9c929a7

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ In particular, Skia Canvas:
99
- is fast and compact since all the heavy lifting is done by native code written in Rust and C++
1010
- can generate output in both raster (JPEG & PNG) and vector (PDF & SVG) image formats
1111
- can save images to [files][saveAs], return them as [Buffers][toBuffer], or encode [dataURL][toDataURL_ext] strings
12-
- uses native threads and [channels](https://docs.rs/neon/0.9.0/neon/event/struct.Channel.html) for asynchronous rendering and file I/O
12+
- uses native threads and the Node [worker pool](https://github.com/neon-bindings/rfcs/pull/35) for asynchronous rendering and file I/O
1313
- can create [multiple ‘pages’][newPage] on a given canvas and then [output][saveAs] them as a single, multi-page PDF or an image-sequence saved to multiple files
1414
- can [simplify][p2d_simplify], [blunt][p2d_round], [combine][bool-ops], [excerpt][p2d_trim], and [atomize][p2d_points] bézier paths using [efficient](https://www.youtube.com/watch?v=OmfliNQsk88) boolean operations or point-by-point [interpolation][p2d_interpolate]
1515
- can apply [3D perspective][createProjection()] transformations in addition to [scaling][scale()], [rotation][rotate()], and [translation][translate()]
@@ -828,6 +828,10 @@ Asking for details about an unknown family will return `undefined`.
828828

829829
Returns `true` if the family is installed on the system or has been added via `FontLibrary.use()`.
830830

831+
##### `reset()`
832+
833+
Uninstalls any dynamically loaded fonts that had been added via `FontLibrary.use()`.
834+
831835
##### `use(familyName, [...fontPaths])`
832836

833837
The `FontLibrary.use()` method allows you to dynamically load local font files and use them with your canvases. By default it will use whatever family name is in the font metadata, but this can be overridden by an alias you provide. Since font-wrangling can be messy, `use` can be called in a number of different ways:
@@ -851,6 +855,8 @@ FontLibrary.use("Grizwald", [
851855

852856
###### with a list of ‘glob’ patterns
853857

858+
> Note to Windows users: Due to recent changes to the [glob][glob] module, you must write paths using unix-style forward slashes. Backslashes are now used solely for escaping wildcard characters.
859+
854860
```js
855861
// with default family name
856862
FontLibrary.use(['fonts/Crimson_Pro/*.ttf'])
@@ -997,4 +1003,6 @@ Many thanks to the [`node-canvas`](https://github.com/Automattic/node-canvas) de
9971003
[translate()]: https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/translate
9981004

9991005
[nonzero]: https://en.wikipedia.org/wiki/Nonzero-rule
1000-
[evenodd]: https://en.wikipedia.org/wiki/Even–odd_rule
1006+
[evenodd]: https://en.wikipedia.org/wiki/Even–odd_rule
1007+
1008+
[glob]: https://github.com/isaacs/node-glob/blob/main/changelog.md#80

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ export interface FontLibrary {
229229
use(
230230
families: Record<string, readonly string[] | string>
231231
): Record<string, Font[] | Font>
232+
233+
reset(): void
232234
}
233235

234236
export const FontLibrary: FontLibrary

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ class FontLibrary extends RustClass {
528528
throw new Error("Expected an array of file paths or an object mapping family names to font files")
529529
}
530530
}
531+
532+
reset(){ return this.ƒ('reset') }
531533
}
532534

533535
class Image extends RustClass {

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
9292
cx.export_function("FontLibrary_has", typography::has)?;
9393
cx.export_function("FontLibrary_family", typography::family)?;
9494
cx.export_function("FontLibrary_addFamily", typography::addFamily)?;
95+
cx.export_function("FontLibrary_reset", typography::reset)?;
9596

9697
// -- Canvas ------------------------------------------------------------------------------------
9798

src/typography.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,7 @@ impl FontLibrary{
392392

393393
fn families(&self) -> Vec<String>{
394394
let font_mgr = FontMgr::new();
395-
let count = font_mgr.count_families();
396-
let mut names:Vec<String> = (0..count).map(|i| font_mgr.family_name(i)).collect();
395+
let mut names:Vec<String> = font_mgr.family_names().collect();
397396
for (font, alias) in &self.fonts {
398397
names.push(match alias{
399398
Some(name) => name.clone(),
@@ -622,3 +621,8 @@ pub fn addFamily(mut cx: FunctionContext) -> JsResult<JsValue> {
622621
Ok(results.upcast())
623622
}
624623

624+
pub fn reset(mut cx: FunctionContext) -> JsResult<JsUndefined> {
625+
let mut library = FONT_LIBRARY.lock().unwrap();
626+
library.fonts.clear();
627+
Ok(cx.undefined())
628+
}

test/media.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ describe("FontLibrary", ()=>{
201201
expect(() => FontLibrary.use(alias, ttf)).not.toThrow()
202202
expect(FontLibrary.has(alias)).toBe(true)
203203
expect(FontLibrary.family(alias).weights).toContain(400)
204+
205+
FontLibrary.reset()
206+
expect(FontLibrary.has(name)).toBe(false)
207+
expect(FontLibrary.has(alias)).toBe(false)
204208
})
205209
})
206210

0 commit comments

Comments
 (0)