printmaker
Version:
Generate PDF documents and from JavaScript objects
97 lines (96 loc) • 3.49 kB
TypeScript
import { PDFFont } from 'pdf-lib';
import { BoxEdges } from './box.js';
import { Color } from './colors.js';
import { Alignment } from './content.js';
import { Font } from './fonts.js';
import { GraphicsObject } from './graphics.js';
import { Obj } from './types.js';
export declare type TextSegment = {
text: string;
width: number;
height: number;
lineHeight: number;
font: PDFFont;
fontSize: number;
color?: Color;
link?: string;
};
/**
* A range of text with attributes, similar to a `<span>` in HTML.
*/
export declare type TextSpan = {
text: string;
attrs: TextAttrs;
};
export declare type Block = Columns | Rows | Paragraph;
export declare type Columns = {
columns: Block[];
} & BlockAttrs;
export declare type Rows = {
rows: Block[];
} & BlockAttrs;
export declare type ImageBlock = {
image?: string;
padding?: BoxEdges;
} & BlockAttrs;
export declare type Paragraph = {
text?: TextSpan[];
graphics?: GraphicsObject[];
padding?: BoxEdges;
} & BlockAttrs & InheritableAttrs;
export declare type TextAttrs = {
fontFamily?: string;
fontSize?: number;
lineHeight?: number;
bold?: boolean;
italic?: boolean;
color?: Color;
link?: string;
};
declare type BlockAttrs = {
margin?: BoxEdges;
width?: number;
height?: number;
id?: string;
};
declare type InheritableAttrs = TextAttrs & {
textAlign?: Alignment;
};
export declare function parseContent(content: unknown[], defaultStyle: InheritableAttrs): Paragraph[];
export declare function parseBlock(input: Obj, defaultAttrs?: InheritableAttrs): Block;
export declare function parseColumns(input: Obj, defaultAttrs?: InheritableAttrs): Columns;
export declare function parseRows(input: Obj, defaultAttrs?: InheritableAttrs): Columns;
export declare function parseParagraph(input: Obj, defaultAttrs?: InheritableAttrs): Paragraph;
export declare function parseTextAttrs(input: Obj): TextAttrs;
export declare function parseInheritableAttrs(input: Obj): TextAttrs;
export declare function parseText(text: unknown, attrs: TextAttrs): TextSpan[];
export declare function extractTextSegments(textSpans: TextSpan[], fonts: Font[]): TextSegment[];
/**
* Split the given text into chunks of subsequent whitespace (`\s`) and non-whitespace (`\S`)
* characters. For example, the string `foo bar` would be split into `['foo', ' ', 'bar']`.
* Newlines (`\n`) are preserved, each in a chunk of its own. Any whitespace that surrounds
* newlines is removed.
*
* @param text The input string
* @returns an array of chunks
*/
export declare function splitChunks(text: string): string[];
export declare function breakLine(segments: TextSegment[], maxWidth: number): TextSegment[][];
/**
* Finds the next appropriate segment that allows for a linebreak, starting at the given index, and
* returns its index.
*
* @param segments A list of text segments.
* @param index The index of the element to start at.
* @returns The index of the next segment that allows for linebreak if found, `undefined` otherwise.
*/
export declare function findLinebreakOpportunity(segments: TextSegment[], index: number): number | undefined;
/**
* Flatten a list of text segments by merging subsequent segments that have identical text
* attributes.
*
* @param segments a list of text segments
* @returns a possibly shorter list of text segments
*/
export declare function flattenTextSegments(segments: TextSegment[]): TextSegment[];
export {};