Skip to content
pixi-silk
GitHub repository
Class

Silk

import { Silk } from 'pixi-silk/i18n/pixi';

Baked localized text for Pixi (brief sec. 6.1). Holds the bundles in use, their atlas pages and glyph metrics on the GPU, the inline objects and the dev update events. Create it once.

Example#

import { Assets, extensions } from 'pixi.js';
import { Silk, SilkMessage, silkBundleLoader } from 'pixi-silk/i18n/pixi';

extensions.add(silkBundleLoader);
const silk = await Silk.create();
await silk.addBundle(await Assets.load('/i18n/ar.silk'));
const theme = silk.createTheme(await Assets.load('/i18n/theme.json'));
const set = await silk.prepare({ locale: 'ar', theme, messages: { title: { key: 'shop.title', size: 44 } } });
app.stage.addChild(new SilkMessage(set.get('title')));

Properties#

PropertyModifierTypeDescriptionDefined in
backendreadonlyPixiBackendThe PixiJS backend: atlas pages, text buffers and run sprites (brief sec. 2.3).packages/pixi-silk/src/i18n/pixi/silk.ts:269
bundlesreadonlyMap<string, SilkBundle>Bundles in use, by the app’s locale code (SilkBundle.id).packages/pixi-silk/src/i18n/pixi/silk.ts:263
dynamicTextpublicDynamicTextProvider | nullDraws text slots.packages/pixi-silk/src/i18n/pixi/silk.ts:265
layoutsreadonlyLayoutCacheLaid out messages, shared by every SilkMessage that shows the same thing.packages/pixi-silk/src/i18n/pixi/silk.ts:267

Methods#

addBundle()#

addBundle(bundle: SilkBundle, loadPage?: (url: string, page: BundlePage) => Promise<Uint8Array<ArrayBufferLike> | ImageBitmap>): Promise<void>;

Puts a bundle in use: uploads its glyph metrics and loads its atlas pages (lossless WebP or PNG, loaded without premultiplied alpha or colour conversion, so distances arrive unchanged). Page files resolve against the bundle’s URL, and bundles that share a page (locales of one script) load it once. loadPage replaces the loading, for tests or custom asset pipelines.

Parameters#

ParameterTypeDefault value
bundleSilkBundleundefined
loadPage(url: string, page: BundlePage) => Promise<Uint8Array<ArrayBufferLike> | ImageBitmap>loadAtlasPage

Returns#

Promise<void>


create()#

static create(options?: SilkOptions): Promise<Silk>;

Creates a Silk. With dynamicText: { mode: 'canvas' } it loads the canvas path first.

Parameters#

ParameterType
optionsSilkOptions

Returns#

Promise<Silk>


createTheme()#

createTheme(data: ThemeData): SilkTheme;

Makes a theme from a theme file ($silk: 2).

Parameters#

ParameterType
dataThemeData

Returns#

SilkTheme


on()#

on(event: "update", listener: (e: UpdateEvent) => void): () => void;

Listens to update events (dev builds): { tier, ms, key } for every update.

Parameters#

ParameterType
event"update"
listener(e: UpdateEvent) => void

Returns#

() => void


prepare()#

prepare<Names extends string>(options: PrepareOptions<Names>): Promise<PreparedSet<Names>>;

Resolves messages for a locale: finds each key (or falls back to fallbackLocale), fills the defaults, and returns them as a set. No shaping happens: plural variants are already baked.

Type Parameters#

Type Parameter
Names extends string

Parameters#

ParameterType
optionsPrepareOptions<Names>

Returns#

Promise<PreparedSet<Names>>


registerInline()#

registerInline(objects: Record<string, InlineObject>): void;

Registers inline objects, written <name/> in messages.

Parameters#

ParameterType
objectsRecord<string, InlineObject>

Returns#

void


releaseBundle()#

releaseBundle(bundle: SilkBundle): void;

Stops using a bundle. Its metrics are destroyed, and its pages once no bundle in use draws from them.

Parameters#

ParameterType
bundleSilkBundle

Returns#

void


stats()#

stats(): {
  atlasBytes: number;
  bundles: number;
  layouts: {
     bytes: number;
     entries: number;
     hits: number;
     misses: number;
  };
  metricsBytes: number;
  pages: number;
};

Memory and cache numbers (brief sec. 8). It counts bundles in use, atlas pages and their GPU bytes (4 per texel, since pages decode to RGBA), glyph metrics bytes, and the layout cache.

Returns#

{
  atlasBytes: number;
  bundles: number;
  layouts: {
     bytes: number;
     entries: number;
     hits: number;
     misses: number;
  };
  metricsBytes: number;
  pages: number;
}
NameTypeDefined in
atlasBytesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:565
bundlesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:563
layouts{ bytes: number; entries: number; hits: number; misses: number; }packages/pixi-silk/src/i18n/pixi/silk.ts:567
layouts.bytesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:567
layouts.entriesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:567
layouts.hitsnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:567
layouts.missesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:567
metricsBytesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:566
pagesnumberpackages/pixi-silk/src/i18n/pixi/silk.ts:564