MCPcopy
hub / github.com/vercel/satori / lengthToNumber

Function lengthToNumber

src/utils.ts:65–114  ·  view source on GitHub ↗
(
  length: string | number,
  baseFontSize: number,
  baseLength: number,
  inheritedStyle: Record<string, string | number>,
  percentage = false
)

Source from the content-addressed store, hash-verified

63}
64
65export function lengthToNumber(
66 length: string | number,
67 baseFontSize: number,
68 baseLength: number,
69 inheritedStyle: Record<string, string | number>,
70 percentage = false
71): number | undefined {
72 if (typeof length === 'number') return length
73
74 // Convert em and rem values to number (px), convert rad to deg.
75 try {
76 length = length.trim()
77
78 // Not length: `1px/2px`, `1px 2px`, `1px, 2px`, `calc(1px)`.
79 if (/[ /\(,]/.test(length)) return
80
81 // Just a number as string: '100'
82 if (length === String(+length)) return +length
83
84 const parsed = new CssDimension(length)
85 if (parsed.type === 'length') {
86 switch (parsed.unit) {
87 case 'em':
88 return parsed.value * baseFontSize
89 case 'rem':
90 return parsed.value * 16
91 case 'vw':
92 return ~~(
93 (parsed.value * (inheritedStyle._viewportWidth as number)) /
94 100
95 )
96 case 'vh':
97 return ~~(
98 (parsed.value * (inheritedStyle._viewportHeight as number)) /
99 100
100 )
101 default:
102 return parsed.value
103 }
104 } else if (parsed.type === 'angle') {
105 return calcDegree(length)
106 } else if (parsed.type === 'percentage') {
107 if (percentage) {
108 return (parsed.value / 100) * baseLength
109 }
110 }
111 } catch {
112 // Not a length unit, silently ignore.
113 }
114}
115
116export function calcDegree(deg: string) {
117 const parsed = new CssDimension(deg)

Callers 14

parseCircleFunction · 0.85
parseEllipseFunction · 0.85
parsePolygonFunction · 0.85
parseInsetFunction · 0.85
resolveRadiusFunction · 0.85
calcColorStopTotalLengthFunction · 0.85
calcRadialGradientFunction · 0.85
calcRadialGradientPropsFunction · 0.85
calcRadiusFunction · 0.85
normalizeStopsFunction · 0.85
calcPercentageFunction · 0.85
buildTextNodesFunction · 0.85

Calls 1

calcDegreeFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…