UNPKG

html2canvas-pro

Version:

Screenshots with JavaScript. Next generation!

76 lines (75 loc) 2.86 kB
/** * Text Renderer * * Handles rendering of text content including: * - Text with letter spacing * - Text decorations (underline, overline, line-through) * - Text shadows * - Webkit line clamp * - Text overflow ellipsis * - Paint order (fill/stroke) * - Font styles */ import { Context } from '../../core/context'; import { TextContainer } from '../../dom/text-container'; import { CSSParsedDeclaration } from '../../css'; import { Bounds } from '../../css/layout/bounds'; import { TextBounds } from '../../css/layout/text'; /** * Dependencies required for TextRenderer */ export interface TextRendererDependencies { ctx: CanvasRenderingContext2D; context: Context; options: { scale: number; }; } export declare const hasCJKCharacters: (text: string) => boolean; /** * Text Renderer * * Specialized renderer for text content. * Extracted from CanvasRenderer to improve code organization and maintainability. */ export declare class TextRenderer { private readonly ctx; private readonly options; constructor(deps: TextRendererDependencies); /** * Iterate grapheme clusters one-by-one, applying correct letter-spacing and * per-script baseline for each character. * * Issue #73: When letter-spacing is non-zero, text must be rendered character by * character. This helper centralises two fixes applied during that iteration: * 1. Add `letterSpacing` to each character's advance width (was previously * omitted, causing characters to render without any spacing). * 2. Switch to the ideographic baseline for CJK glyphs so their vertical * position matches how browsers lay them out in the DOM. * * The `renderFn` callback receives (letter, x, y) and performs the actual draw * call (fillText or strokeText), allowing fill and stroke paths to share one * implementation. */ private iterateLettersWithLetterSpacing; /** * Render text with letter-spacing applied (fill pass). * When letterSpacing is 0 the whole string is drawn in one call; otherwise each * grapheme is drawn individually so spacing and CJK baseline are applied correctly. */ renderTextWithLetterSpacing(text: TextBounds, letterSpacing: number, baseline: number): void; /** * Helper method to render text with paint order support * Reduces code duplication in line-clamp and normal rendering */ private renderTextBoundWithPaintOrder; private renderTextDecoration; private drawDecorationLine; private truncateTextWithEllipsis; /** * Create font style array * Public method used by list rendering */ createFontStyle(styles: CSSParsedDeclaration): string[]; renderTextNode(text: TextContainer, styles: CSSParsedDeclaration, containerBounds?: Bounds): Promise<void>; }