123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 'use strict'
- const weights = 'bold|bolder|lighter|[1-9]00'
- const styles = 'italic|oblique'
- const variants = 'small-caps'
- const stretches = 'ultra-condensed|extra-condensed|condensed|semi-condensed|semi-expanded|expanded|extra-expanded|ultra-expanded'
- const units = 'px|pt|pc|in|cm|mm|%|em|ex|ch|rem|q'
- const string = '\'([^\']+)\'|"([^"]+)"|[\\w\\s-]+'
- const weightRe = new RegExp(`(${weights}) +`, 'i')
- const styleRe = new RegExp(`(${styles}) +`, 'i')
- const variantRe = new RegExp(`(${variants}) +`, 'i')
- const stretchRe = new RegExp(`(${stretches}) +`, 'i')
- const sizeFamilyRe = new RegExp(
- `([\\d\\.]+)(${units}) *((?:${string})( *, *(?:${string}))*)`)
- const cache = {}
- const defaultHeight = 16
- module.exports = str => {
-
- if (cache[str]) return cache[str]
-
- const sizeFamily = sizeFamilyRe.exec(str)
- if (!sizeFamily) return
-
- const font = {
- weight: 'normal',
- style: 'normal',
- stretch: 'normal',
- variant: 'normal',
- size: parseFloat(sizeFamily[1]),
- unit: sizeFamily[2],
- family: sizeFamily[3].replace(/["']/g, '').replace(/ *, */g, ',')
- }
-
- let weight, style, variant, stretch
-
- const substr = str.substring(0, sizeFamily.index)
- if ((weight = weightRe.exec(substr))) font.weight = weight[1]
- if ((style = styleRe.exec(substr))) font.style = style[1]
- if ((variant = variantRe.exec(substr))) font.variant = variant[1]
- if ((stretch = stretchRe.exec(substr))) font.stretch = stretch[1]
-
-
- switch (font.unit) {
- case 'pt':
- font.size /= 0.75
- break
- case 'pc':
- font.size *= 16
- break
- case 'in':
- font.size *= 96
- break
- case 'cm':
- font.size *= 96.0 / 2.54
- break
- case 'mm':
- font.size *= 96.0 / 25.4
- break
- case '%':
-
-
- break
- case 'em':
- case 'rem':
- font.size *= defaultHeight / 0.75
- break
- case 'q':
- font.size *= 96 / 25.4 / 4
- break
- }
- return (cache[str] = font)
- }
|