import { Dimension , Glue } from '../public/core' ; import { PT_PER_EM } from './font-metrics' ; export function convertDimensionToPt ( value ? , precision ? ) { if ( ! value ) return 0 ; const f = { pt : 1 , mm : 7227 / 2540 , cm : 7227 / 254 , ex : 35271 / 8192 , px : 3 / 4 , em : PT_PER_EM , bp : 803 / 800 , dd : 1238 / 1157 , pc : 12 , in : 72.27 , mu : 10 / 18 , } [ value . unit ?? 'pt' ] ! ; if ( Number . isFinite ( precision ) ) { const factor = 10 ** precision ! ; return Math . round ( ( value . dimension / PT_PER_EM ) * f * factor ) / factor ; } return value . dimension * f ; } export function convertDimensionToEm ( value ? , precision ? ) { if ( value === null ) return 0 ; return convertDimensionToPt ( value , precision ) / PT_PER_EM ; } export function convertGlueToEm ( value ) { return convertDimensionToEm ( value . glue ) ; } export function convertDimensionToPixel ( value ) { return convertDimensionToEm ( value ) * ( 4 / 3 ) * PT_PER_EM ; } export function serializeDimension ( value ) { return `${ value . dimension }${ value . unit ?? 'pt' }` ; } export function serializeGlue ( value ) { let result = serializeDimension ( value . glue ) ; if ( value . grow && value . grow . dimension !== 0 ) { result += ` plus ${ serializeDimension ( value . grow ) }` ; } if ( value . shrink && value . shrink . dimension !== 0 ) { result += ` minus ${ serializeDimension ( value . shrink ) }` ; } return result ; } export function addDimension ( lhs , rhs ) { const lhsPt = convertDimensionToPt ( lhs ) ; const rhsPt = convertDimensionToPt ( rhs ) ; return { dimension : lhsPt + rhsPt , unit : 'pt' } ; }