# @embedpdf/core-text — . FieldLine [InterfaceDeclaration] (from packages/core/text/src/field.ts): export interface FieldLine{ readonly baseline: number; readonly runs: readonly FieldRun[]; } FieldRun [InterfaceDeclaration] (from packages/core/text/src/field.ts): export interface FieldRun{ readonly font: F; readonly text: string; readonly rtl: boolean; readonly plain: boolean; readonly x: number; readonly width: number; readonly joinsPrevious: boolean; readonly joinsNext: boolean; } FieldText [InterfaceDeclaration] (from packages/core/text/src/field.ts): export interface FieldText{ readonly fontSize: number; readonly contentHeight: number; readonly lines: readonly FieldLine[]; readonly rtl: boolean; readonly missing: readonly string[]; } FieldTextOptions [InterfaceDeclaration] (from packages/core/text/src/field.ts): export interface FieldTextOptions{ readonly plate: PdfRect; readonly fontSize: number; readonly alignment: 0 | 1 | 2; readonly multiline: boolean; readonly password: boolean; readonly limit: number; readonly cells: number; readonly centered: boolean; } GraphemeMatch [InterfaceDeclaration] (from packages/core/text/src/fonts.ts): export interface GraphemeMatch{ readonly font: F; readonly covered: boolean; } JoiningType [TypeAliasDeclaration] (from packages/core/text/src/joining.ts): export type JoiningType = (typeof JOINING_TYPES)[number][2]; Joins [InterfaceDeclaration] (from packages/core/text/src/joining.ts): export interface Joins{ readonly previous: boolean; readonly next: boolean; } PaintFont [InterfaceDeclaration] (from packages/core/text/src/paint.ts): export interface PaintFont extends TextFont{ readonly fontFamily: string; readonly fontWeight: number; readonly fontStyle: 'normal' | 'italic'; readonly program: MountableFontProgram; } PaintFonts [TypeAliasDeclaration] (from packages/core/text/src/paint.ts): export type PaintFonts = TextFonts; TextFont [InterfaceDeclaration] (from packages/core/text/src/fonts.ts): export interface TextFont{ readonly key: string; readonly ascent: number; readonly descent: number; covers(codePoint: number): boolean; width(text: string, rtl: boolean): number; readonly standard: boolean; } TextFonts [InterfaceDeclaration] (from packages/core/text/src/fonts.ts): export interface TextFonts{ readonly face: F; readonly fallbacks: readonly F[]; readonly lastResort: F | null; } coversGrapheme [VariableDeclaration] (from packages/core/text/src/fonts.ts): coversGrapheme = (font: TextFont, grapheme: string): boolean => codePointsOf(grapheme).every((codePoint) => !needsGlyph(codePoint) || font.covers(codePoint)) defaultPaintFonts [FunctionDeclaration] (from packages/core/text/src/paint.ts): export function defaultPaintFonts(fontFamily: string | null): PaintFonts{ return{ face: standardPaintFont(standardFontOf(fontFamily ?? 'helvetica')), fallbacks: [], lastResort: lastResortPaintFont(), } ; } firstStrongIsRtl [FunctionDeclaration] (from packages/core/text/src/bidi.ts): export function firstStrongIsRtl(text: string): boolean | null{ let isolates = 0; for (let at = 0; at < text.length; ){ const codePoint = text.codePointAt(at)!; switch (classOf(codePoint)){ case 'L': if (isolates === 0) return false; break; case 'R': case 'AL': if (isolates === 0) return true; break; case 'LRI': case 'RLI': case 'FSI': isolates += 1; break; case 'PDI': if (isolates > 0) isolates -= 1; break; default: break; } at += unitsOf(codePoint); } return null; } graphemeJoiningType [FunctionDeclaration] (from packages/core/text/src/joining.ts): export function graphemeJoiningType(grapheme: string): JoiningType{ for (const codePoint of codePointsOf(grapheme)){ const type = joiningTypeOf(codePoint); if (type !== 'T') return type; } return 'T'; } graphemeStarts [FunctionDeclaration] (from packages/core/text/src/graphemes.ts): export function graphemeStarts(text: string): boolean[]{ const starts: boolean[] = new Array(text.length).fill(false); let afterJoiner = false; for (let at = 0; at < text.length; ){ const codePoint = text.codePointAt(at)!; const continues = at > 0 && (isMark(codePoint) || isVariationSelector(codePoint) || codePoint === ZERO_WIDTH_JOINER || afterJoiner); starts[at] = !continues; afterJoiner = codePoint === ZERO_WIDTH_JOINER; at += unitsOf(codePoint); } return starts; } graphemesOf [FunctionDeclaration] (from packages/core/text/src/graphemes.ts): export function graphemesOf(text: string): [number, number][]{ const starts = graphemeStarts(text); const graphemes: [number, number][] = []; for (let begin = 0; begin < text.length; ){ let end = begin + 1; while (end < text.length && !starts[end]) end += 1; graphemes.push([begin, end]); begin = end; } return graphemes; } isInvisibleGrapheme [FunctionDeclaration] (from packages/core/text/src/graphemes.ts): export function isInvisibleGrapheme(grapheme: string): boolean{ return codePointsOf(grapheme).every( (cp) => cp < 0x20 || (cp >= 0x7f && cp <= 0x9f) || cp === 0x00ad || cp === 0x034f || cp === 0x061c || (cp >= 0x115f && cp <= 0x1160) || (cp >= 0x17b4 && cp <= 0x17b5) || (cp >= 0x180b && cp <= 0x180f) || (cp >= 0x200b && cp <= 0x200f) || (cp >= 0x202a && cp <= 0x202e) || (cp >= 0x2060 && cp <= 0x206f) || cp === 0x3164 || (cp >= 0xfe00 && cp <= 0xfe0f) || cp === 0xfeff || cp === 0xffa0 || (cp >= 0xfff0 && cp <= 0xfff8) || (cp >= 0x1bca0 && cp <= 0x1bca3) || (cp >= 0x1d173 && cp <= 0x1d17a) || (cp >= 0xe0000 && cp <= 0xe0fff), ); } isSimpleScript [VariableDeclaration] (from packages/core/text/src/scripts.ts): isSimpleScript = (script: string): boolean => SIMPLE.has(script) joinedText [VariableDeclaration] (from packages/core/text/src/joining.ts): joinedText = (grapheme: string, joins: Joins): string => `${ joins.previous ? ZERO_WIDTH_JOINER : ''} ${ grapheme} ${ joins.next ? ZERO_WIDTH_JOINER : ''} ` joiningTypeOf [FunctionDeclaration] (from packages/core/text/src/joining.ts): export function joiningTypeOf(codePoint: number): JoiningType{ let low = 0; let high = JOINING_TYPES.length - 1; while (low <= high){ const middle = (low + high) >> 1; const [first, last, type] = JOINING_TYPES[middle]!; if (codePoint < first) high = middle - 1; else if (codePoint > last) low = middle + 1; else return type; } return TRANSPARENT.test(String.fromCodePoint(codePoint)) ? 'T' : 'U'; } joinsOf [FunctionDeclaration] (from packages/core/text/src/joining.ts): export function joinsOf( graphemes: readonly string[], together: (a: number, b: number) => boolean = () => true, ): Joins[]{ const types = graphemes.map(graphemeJoiningType); const joins = graphemes.map(() => ({ previous: false, next: false} )); let before = -1; types.forEach((type, index) =>{ if (type === 'T') return; if (before >= 0 && joinsAfter(types[before]!) && joinsBefore(type) && together(before, index)){ joins[before]!.next = true; joins[index]!.previous = true; } before = index; } ); return joins; } lastResortPaintFont [FunctionDeclaration] (from packages/core/text/src/paint.ts): export function lastResortPaintFont(): PaintFont{ if (lastResort) return lastResort; const program = lastResortFontProgram(); lastResort ={ key: LAST_RESORT_HASH, ascent: LAST_RESORT_METRICS.ascent, descent: LAST_RESORT_METRICS.descent, standard: false, covers: (codePoint) => codePoint >= 0x20 && (codePoint < 0xd800 || codePoint > 0xdfff), width: (text) =>{ let width = 0; for (const character of text){ const codePoint = character.codePointAt(0)!; if (codePoint !== ZERO_WIDTH_JOINER && !isVariationSelector(codePoint)){ width += LAST_RESORT_METRICS.advance; } } return width; } , fontFamily: `"${ fontProgramFamily(LAST_RESORT_HASH)} "`, fontWeight: program.weight, fontStyle: program.style, program, } ; return lastResort; } layoutField [FunctionDeclaration] (from packages/core/text/src/field.ts): export function layoutField( text: string, fonts: TextFonts, options: FieldTextOptions, ): FieldText{ const{ plate, alignment, multiline, cells} = options; const plateWidth = f32(plate.right - plate.left); const plateHeight = f32(plate.top - plate.bottom); const typesetWidth = Math.max(plateWidth, 0); const texts = sectionsOf(text, options); const rtl = firstStrongIsRtl(texts.join('')) ?? false; const comb = cells > 0; const sections: Section[] = texts.map((sectionText) =>{ const paragraph = itemize(sectionText, rtl, fonts); const units = paragraph.graphemes.map((grapheme, index): Unit =>{ const{ font} = paragraph.items[grapheme.item]!; return{ grapheme: index, code: sectionText.charCodeAt(grapheme.begin), ascent: font.ascent, descent: font.descent, } ; } ); return{ paragraph, units} ; } ); const missing = [...new Set(sections.flatMap((section) => section.paragraph.missing))]; const unitWidth = (section: Section, unit: Unit, size: number) => graphemesWidth(section.paragraph, unit.grapheme, unit.grapheme + 1, size); const cellJoins = ({ paragraph, units} : Section): Joins[] =>{ const graphemeOf = (unit: Unit) => paragraph.graphemes[unit.grapheme]!; return joinsOf( units.map((unit) => paragraph.text.slice(graphemeOf(unit).begin, graphemeOf(unit).end)), (a, b) => graphemeOf(units[a]!).item === graphemeOf(units[b]!).item, ); } ; const cellWidth = (section: Section, unit: Unit, joins: Joins, size: number) =>{ if (!joins.previous && !joins.next) return unitWidth(section, unit, size); const{ paragraph} = section; const grapheme = paragraph.graphemes[unit.grapheme]!; const item = paragraph.items[grapheme.item]!; const text = joinedText(paragraph.text.slice(grapheme.begin, grapheme.end), joins); return scaled(item.font.width(text, item.level % 2 === 1), size); } ; const faceAscent = (size: number) => scaled(fonts.face.ascent, size); const faceDescent = (size: number) => scaled(fonts.face.descent, size); let fontSize = options.fontSize; if (isFloatZero(fontSize)){ fontSize = 0; if (plateWidth > 0){ const steps = multiline ? FONT_SIZE_STEPS.slice(0, Math.trunc(FONT_SIZE_STEPS.length / 4)) : FONT_SIZE_STEPS; const tooBig = (size: number) =>{ let width = 0; let height = 0; for (const section of sections){ const extent = splitLines(section, size, null); width = Math.max(extent.width, width); height = f32(height + extent.height); if (isFloatBigger(width, plateWidth) || isFloatBigger(height, plateHeight)) return true; } return false; } ; let first = 0; let count = steps.length; while (count > 0){ const step = count >> 1; if (!tooBig(steps[first + step]!)){ first += step + 1; count -= step + 1; } else{ count = step; } } fontSize = first === steps.length ? steps[steps.length - 1]! : steps[first === 0 ? 0 : first - 1]!; } } const placed:{ section: Section; lines: Line[]; cellX: number[]; cellJoins: Joins[]; left: number; top: number; } [] = []; let positionY = 0; let contentHeight = 0; for (const section of sections){ let lines: Line[]; const cellX: number[] = []; let joins: Joins[] = []; let left = 0; let height: number; if (comb){ const nodeWidth = f32(plateWidth / cells); const units = section.units.length; let start = 0; if (alignment === 1) start = Math.trunc((cells - units) / 2); else if (alignment === 2) start = cells - units; const ascent = faceAscent(fontSize); let lineDescent = faceDescent(fontSize); joins = cellJoins(section); for (let index = 0; index < units && index < cells; index += 1){ const unit = section.units[index]!; const width = cellWidth(section, unit, joins[index]!, fontSize); cellX.push(f32(nodeWidth * (index + start + 0.5) - f32(width * 0.5))); lineDescent = Math.min(lineDescent, scaled(unit.descent, fontSize)); } lines = [{ begin: units > 0 ? 0 : -1, end: units - 1, width: 0, ascent, descent: lineDescent} , ]; height = f32(ascent - lineDescent); } else{ lines = []; const extent = splitLines(section, fontSize, lines); left = alignmentOffset(alignment, typesetWidth, extent.width); height = extent.height; } placed.push({ section, lines, cellX, cellJoins: joins, left, top: positionY} ); positionY = f32(positionY + height); contentHeight = f32(contentHeight + height); } const offsetY = options.centered ? f32(f32(contentHeight - plateHeight) / 2) : 0; const out: FieldLine[] = []; for (const{ section, lines, cellX, cellJoins: joins, left, top} of placed){ let lineTop = 0; for (const line of lines){ lineTop = f32(lineTop + line.ascent); const baselineY = f32(f32(lineTop + top) - offsetY); lineTop = f32(lineTop - line.descent); const runs: FieldRun[] = []; if (line.begin >= 0){ if (comb){ cellX.forEach((x, index) =>{ const unit = section.units[index]!; const cell = joins[index]!; for (const run of runsOf( section.paragraph, unit.grapheme, unit.grapheme + 1, fontSize, )){ runs.push({ ...fieldRun(run, x), width: cellWidth(section, unit, cell, fontSize), joinsPrevious: cell.previous, joinsNext: cell.next, } ); } } ); } else{ let x = alignmentOffset(alignment, typesetWidth, line.width); const from = section.units[line.begin]!.grapheme; const to = section.units[line.end]!.grapheme + 1; for (const run of runsOf(section.paragraph, from, to, fontSize)){ runs.push(fieldRun(run, f32(f32(x - left) + left))); x = f32(x + run.width); } } } out.push({ baseline: f32(plate.top - baselineY), runs} ); } } return{ fontSize, contentHeight, lines: out, rtl, missing} ; function fieldRun(run: Run, x: number): FieldRun{ const{ font, text, rtl, plain, width} = run; return{ font, text, rtl, plain, x: f32(plate.left + x), width, joinsPrevious: false, joinsNext: false, } ; } function splitLines( section: Section, size: number, lines: Line[] | null, ):{ width: number; height: number} { const{ units} = section; if (units.length === 0){ const ascent = faceAscent(size); const descent = faceDescent(size); lines?.push({ begin: -1, end: -1, width: 0, ascent, descent} ); return{ width: 0, height: f32(ascent - descent)} ; } let head = 0; let maxX = 0; let maxY = 0; let lineWidth = 0; let backupWidth = 0; let lineAscent = 0; let backupAscent = 0; let lineDescent = 0; let backupDescent = 0; let wordStart = 0; let fullWord = false; let fullWords = 0; let charIndex = 0; let opened = false; const addLine = (tail: number) =>{ lines?.push({ begin: head, end: tail, width: lineWidth, ascent: lineAscent, descent: lineDescent, } ); maxY = f32(maxY + lineAscent); maxY = f32(maxY - lineDescent); maxX = Math.max(lineWidth, maxX); } ; let index = 0; while (index < units.length){ const unit = units[index]!; const previous = units[index > 0 ? index - 1 : index]!; lineAscent = Math.max(lineAscent, scaled(unit.ascent, size)); lineDescent = Math.min(lineDescent, scaled(unit.descent, size)); if (!opened){ if (isOpenPunctuation(unit.code)){ opened = true; fullWord = true; } else if (needsBreak(previous.code, unit.code)){ fullWord = true; } } else if (!isSpace(unit.code) && !isOpenPunctuation(unit.code)){ opened = false; } if (fullWord){ fullWord = false; if (charIndex > 0) fullWords += 1; wordStart = index; backupWidth = lineWidth; backupAscent = lineAscent; backupDescent = lineDescent; } charIndex += 1; const width = unitWidth(section, unit, size); if (multiline && typesetWidth > 0 && f32(lineWidth + width) > typesetWidth){ if (fullWords > 0){ index = wordStart; lineWidth = backupWidth; lineAscent = backupAscent; lineDescent = backupDescent; } if (charIndex === 1){ lineWidth = width; index += 1; } addLine(index - 1); head = index; lineWidth = 0; lineAscent = 0; lineDescent = 0; charIndex = 0; fullWords = 0; fullWord = false; } else{ lineWidth = f32(lineWidth + width); index += 1; } } if (head <= units.length - 1) addLine(units.length - 1); return{ width: maxX, height: maxY} ; } } lineLevels [FunctionDeclaration] (from packages/core/text/src/bidi.ts): export function lineLevels( text: string, levels: readonly number[], begin: number, end: number, base: number, ): number[]{ return trailingAtLevel(text, levels.slice(begin, end), begin, end, base); } matchGrapheme [FunctionDeclaration] (from packages/core/text/src/fonts.ts): export function matchGrapheme( grapheme: string, fonts: TextFonts, ): GraphemeMatch{ if (coversGrapheme(fonts.face, grapheme)) return{ font: fonts.face, covered: true} ; const fallback = fonts.fallbacks.find((font) => coversGrapheme(font, grapheme)); if (fallback) return{ font: fallback, covered: true} ; const lastResort = isInvisibleGrapheme(grapheme) ? null : fonts.lastResort; return{ font: lastResort ?? fonts.face, covered: false} ; } mirrorOf [FunctionDeclaration] (from packages/core/text/src/bidi.ts): export function mirrorOf(codePoint: number): number{ const mirrored = bidi.getMirroredCharacter(String.fromCodePoint(codePoint)); return mirrored ? mirrored.codePointAt(0)! : codePoint; } paragraphLevels [FunctionDeclaration] (from packages/core/text/src/bidi.ts): export function paragraphLevels(text: string, rtl: boolean): number[]{ const base = rtl ? 1 : 0; if (text.length === 0) return []; const levels = Array.from(bidi.getEmbeddingLevels(bmpText(text), rtl ? 'rtl' : 'ltr').levels); return trailingAtLevel(text, levels, 0, text.length, base); } programPaintFont [FunctionDeclaration] (from packages/core/text/src/paint.ts): export function programPaintFont( info: FontProgramInfo, program: MountableFontProgram, measure: (text: string, rtl: boolean) => number, ): PaintFont{ const covers = coverageOf(info.covers); return{ key: program.hash, ascent: info.ascent, descent: info.descent, standard: false, covers, width: measure, fontFamily: `"${ program.family} "`, fontWeight: program.weight, fontStyle: program.style, program, } ; } scriptOf [FunctionDeclaration] (from packages/core/text/src/scripts.ts): export function scriptOf(codePoint: number): string{ const cached = known.get(codePoint); if (cached !== undefined) return cached; const character = String.fromCodePoint(codePoint); const script = scriptMatchers().find(([, matcher]) => matcher.test(character))?.[0] ?? UNKNOWN; known.set(codePoint, script); return script; } standardPaintFont [FunctionDeclaration] (from packages/core/text/src/paint.ts): export function standardPaintFont(font: StandardFont): PaintFont{ const known = standardFonts.get(font); if (known) return known; const{ fontFamily, fontWeight, fontStyle} = standardFontCss(font); const paintFont: PaintFont ={ ...standardTextFont(font), fontFamily, fontWeight, fontStyle, program: standardFontProgram(font), } ; standardFonts.set(font, paintFont); return paintFont; } standardTextFont [FunctionDeclaration] (from packages/core/text/src/fonts.ts): export function standardTextFont(font: StandardFont): TextFont{ const known = standardFonts.get(font); if (known) return known; const metrics = standardFontMetrics(font); const textFont: TextFont ={ key: font, ascent: metrics.ascent, descent: metrics.descent, standard: true, covers: (codePoint) => codePoint <= 0xffff && metrics.advance(codePoint) !== null, width: (text, rtl) => codePointsOf(text).reduce( (sum, codePoint) => sum + (metrics.advance(rtl ? mirrorOf(codePoint) : codePoint) ?? 0), 0, ), } ; standardFonts.set(font, textFont); return textFont; } visualOrder [FunctionDeclaration] (from packages/core/text/src/bidi.ts): export function visualOrder(levels: readonly number[]): number[]{ const order = levels.map((_, index) => index); if (levels.length === 0) return order; const highest = Math.max(...levels); const lowestOdd = Math.min(...levels.map((level) => (level % 2 ? level : level + 1))); for (let level = highest; level >= lowestOdd; level -= 1){ for (let start = 0; start < order.length; ){ if (levels[order[start]!]! < level){ start += 1; continue; } let end = start + 1; while (end < order.length && levels[order[end]!]! >= level) end += 1; order.splice(start, end - start, ...order.slice(start, end).reverse()); start = end; } } return order; }