import { cssFontShorthand , Font } from '../../common/text_utils' ; import { Size } from '../dimensions' ; internal export const withTextMeasure = < T > ( fun ) => { const canvas = document . createElement ( 'canvas' ) ; const ctx = canvas . getContext ( '2d' ) ; return fun ( ctx ? measureText ( ctx ) : ( ) => ( { width : 0 , height : 0 } ) ) ; } ; internal export type TextMeasure = ( text : string , font : Omit < Font , 'textColor' > , fontSize : number , lineHeight ? : number ) => Size ; internal export function measureText ( ctx ) { return ( text , font , fontSize , lineHeight = 1 ) => { if ( text . length === 0 ) { return { width : 0 , height : fontSize * lineHeight } ; } ctx . font = cssFontShorthand ( font , fontSize ) ; const { actualBoundingBoxLeft , actualBoundingBoxRight } = ctx . measureText ( text ) ; return { width : actualBoundingBoxLeft + actualBoundingBoxRight , height : fontSize * lineHeight } ; } ; }