import * as THREE from "three" ; import { GlyphData } from "../rendering/GlyphData" ; import { FontUnit , FontVariant } from "../rendering/TextStyle" ; import { UnicodeUtils } from "../utils/UnicodeUtils" ; /**
 * Collection of different constants and utility functions used by [[Typesetter]]s.
 */ export namespace TypesettingUtils { /**
     * Ratio between EMs and Pixels.
     */ export const EM_TO_PX = 16.0 ; /**
     * Ratio between Points and Pixels.
     */ export const PT_TO_PX = 1.25 ; /**
     * Angle used when emulating italic fonts (oblique).
     */ export const OBLIQUE_ANGLE = 0.174533 ; /**
     * Horizontal offset used when emulating italic fonts (oblique).
     */ export const OBLIQUE_OFFSET = Math . tan ( OBLIQUE_ANGLE ) ; param size param unit param originalSize returns export function getPixelSize ( size , unit , originalSize ) { let result = size ; switch ( unit ) { case FontUnit . Em : result *= EM_TO_PX ; break ; case FontUnit . Point : result *= PT_TO_PX ; break ; case FontUnit . Percent : result *= ( 1.0 / 100 ) * originalSize ; break ; } return result ; } param glyphs param index param fontVariant returns export function getSmallCapsScale ( glyphs , smallCapsTransformations , index , fontVariant ) { const isSmallCaps = smallCapsTransformations [ index ] && fontVariant === FontVariant . SmallCaps ; return isSmallCaps ? glyphs [ index ] . font . metrics . xHeight / glyphs [ index ] . font . metrics . capHeight : 1.0 ; } param glyphs param offset returns export function getDirection ( glyphs , offset ) { let result = UnicodeUtils . Direction . LTR ; let index = offset ; while ( glyphs [ index ] . direction !== UnicodeUtils . Direction . LTR && glyphs [ index ] . direction !== UnicodeUtils . Direction . RTL && index < glyphs . length - 1 ) { ++ index ; } if ( Math . abs ( glyphs [ index ] . direction ) === 1.0 ) { result = glyphs [ index ] . direction ; } return result ; } param transform param position param scale param rotation param localRotation export function computeGlyphTransform ( transform , position , scale , rotation , localRotation ) { const cosAngle = Math . cos ( rotation ) ; const sinAngle = Math . sin ( rotation ) ; const localCosAngle = Math . cos ( localRotation ) ; const localSinAngle = Math . sin ( localRotation ) ; transform . set ( scale * localCosAngle , scale * - localSinAngle , cosAngle * position . x - sinAngle * position . y , scale * localSinAngle , scale * localCosAngle , sinAngle * position . x + cosAngle * position . y , 0 , 0 , 1.0 ) ; } param corners param globalBounds param individualBounds export function updateBounds ( corners , globalBounds , individualBounds ? ) { const minX = Math . min ( corners [ 0 ] . x , corners [ 1 ] . x , corners [ 2 ] . x , corners [ 3 ] . x ) ; const maxX = Math . max ( corners [ 0 ] . x , corners [ 1 ] . x , corners [ 2 ] . x , corners [ 3 ] . x ) ; const minY = Math . min ( corners [ 0 ] . y , corners [ 1 ] . y , corners [ 2 ] . y , corners [ 3 ] . y ) ; const maxY = Math . max ( corners [ 0 ] . y , corners [ 1 ] . y , corners [ 2 ] . y , corners [ 3 ] . y ) ; if ( individualBounds !== undefined ) { if ( individualBounds . array [ individualBounds . offset ] !== undefined ) { individualBounds . array [ individualBounds . offset ] . min . set ( minX , minY ) ; individualBounds . array [ individualBounds . offset ] . max . set ( maxX , maxY ) ; } else { individualBounds . array . push ( new THREE . Box2 ( new THREE . Vector2 ( minX , minY ) , new THREE . Vector2 ( maxX , maxY ) ) ) ; } ++ individualBounds . offset ; } globalBounds . min . set ( Math . min ( globalBounds . min . x , minX ) , Math . min ( globalBounds . min . y , minY ) ) ; globalBounds . max . set ( Math . max ( globalBounds . max . x , maxX ) , Math . max ( globalBounds . max . y , maxY ) ) ; } }