@cf-wasm/og
Version:
Generate Open Graph Images dynamically from HTML/CSS without a browser.
155 lines (154 loc) • 5.62 kB
TypeScript
import type { FontStyle, FontWeight } from './satori';
import type { MayBePromise } from './types';
/** Font cache map */
export declare const FONT_CACHE_MAP: Map<string, ArrayBuffer>;
export type FontDisplay = 'auto' | 'block' | 'swap' | 'fallback' | 'optional';
/** An interface representing options for {@link loadGoogleFont} function */
export interface LoadGoogleFontOptions {
text?: string;
/** The font weight to load */
weight?: string | number;
/** The font style to load */
style?: FontStyle;
/** The `font-display` */
display?: FontDisplay;
}
/** Default user agent for loading css */
export declare const GOOGLE_FONT_CSS_DEFAULT_USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1";
/** Google fonts utils */
declare class GoogleFontsUtils {
/** Default Google font css loader */
private _defaultCssLoader;
/** Google font css loader */
private _cssLoader;
/**
* Loads Google font css
*
* @param cssUrl The Google font css url
* @param userAgent The user agent header to be used
*
* @returns On success, the css as string
*/
loadCss(cssUrl: string, userAgent?: string): Promise<string>;
/**
* Sets Google font css loader
*
* @param loader The {@link GoogleFontCssLoader}
*/
setCssLoader(loader: (cssUrl: string, userAgent: string) => MayBePromise<string | undefined>): void;
}
export declare const googleFonts: GoogleFontsUtils;
/** Constructs Google font css url */
export declare const constructGoogleFontCssUrl: (family: string, { text, weight, style, display }?: {
text?: string;
weight?: string | number;
style?: FontStyle;
display?: FontDisplay;
}) => string;
/**
* A helper function for loading google fonts as {@link ArrayBuffer}
*
* @param family The name of the font family
* @param param1 Options
*
* @returns A promise which resolved to {@link ArrayBuffer}
*/
export declare const loadGoogleFont: (family: string, { text, weight, style, display }?: LoadGoogleFontOptions) => Promise<ArrayBuffer>;
/** Base font options */
export interface BaseFontOptions {
weight?: FontWeight;
style?: FontStyle;
}
/** Base font class */
export declare class BaseFont {
protected input: MayBePromise<ArrayBuffer> | (() => MayBePromise<ArrayBuffer>) | undefined;
name: string;
style: FontStyle;
weight: FontWeight;
constructor(name: string, input: MayBePromise<ArrayBuffer> | (() => MayBePromise<ArrayBuffer>) | undefined, { weight, style }?: BaseFontOptions);
get data(): MayBePromise<ArrayBuffer> | (() => MayBePromise<ArrayBuffer>) | undefined;
}
/** An interface representing options for {@link CustomFont} */
export interface CustomFontOptions extends BaseFontOptions {
lang?: string;
}
/** A helper class to load Custom font */
export declare class CustomFont extends BaseFont {
protected input: MayBePromise<ArrayBuffer> | (() => MayBePromise<ArrayBuffer>);
private promise?;
lang: string | undefined;
/**
* Creates an instance of {@link CustomFont}
*
* @param name The name of the font (can be used for font-family css property)
* @param input Font data as `ArrayBuffer` or a promise which resolves to `ArrayBuffer`
* @param options
*/
constructor(name: string, input: MayBePromise<ArrayBuffer> | (() => MayBePromise<ArrayBuffer>), options?: CustomFontOptions);
/**
* A promise which resolves to font data as `ArrayBuffer`
*/
get data(): Promise<ArrayBuffer>;
}
/** An interface representing options for {@link GoogleFont} */
export interface GoogleFontOptions extends BaseFontOptions {
/** The name of the font (can be used for font-family css property) */
name?: string;
/** Loads font only for particular text (for better performance) */
text?: string;
}
/** A helper class to load Google font */
export declare class GoogleFont extends BaseFont {
protected input: Promise<ArrayBuffer> | undefined;
private promise?;
/** The font family name */
family: string;
/** Text for which the font is loaded */
text: string | undefined;
/**
* Creates an instance of {@link GoogleFont}
*
* @param family The name of font family to load
* @param options The {@link GoogleFontOptions}
*/
constructor(family: string, options?: GoogleFontOptions);
/** A promise which resolves to font data as `ArrayBuffer` */
get data(): Promise<ArrayBuffer>;
/**
* Checks whether font can load buffer
*
* @returns On success, true otherwise the error object thrown
*/
canLoad(): Promise<unknown>;
}
export interface FontBuffer {
data: MayBePromise<ArrayBuffer>;
}
export type FontInput = MayBePromise<Response | ArrayBuffer> | FontBuffer;
/** Default font utils */
declare class DefaultFont {
private _fallbackFont?;
private _fontData?;
private _fontShouldResolve;
/**
* Sets default font for image rendering
*
* @param input {@link FontInput}
*/
set(input: FontInput | (() => FontInput)): void;
/**
* Gets default font buffer for image rendering if it is set
*
* @returns A Promise which resolves to ArrayBuffer if default font is set,
* otherwise undefined
*/
get(): Promise<ArrayBuffer | undefined>;
/**
* Check whether default font is set or not
*
* @returns true if default font is set, otherwise false
*/
has(): boolean;
}
export declare const defaultFont: DefaultFont;
export {};