UNPKG

react-native-unit-components

Version:

Unit React Native components

66 lines (63 loc) 2.13 kB
import { Platform } from 'react-native'; const getMimeType = fileName => { const ext = fileName.split('.').pop()?.toLowerCase(); switch (ext) { case 'woff': return 'font/woff'; case 'woff2': return 'font/woff2'; case 'otf': return 'font/otf'; default: return 'font/ttf'; } }; const getUrl = (source, iosFontBase64Map) => { if (Platform.OS == 'ios') { const base64 = iosFontBase64Map?.[source.fileName]; if (base64) { const mime = getMimeType(source.fileName); return `url('data:${mime};base64,${base64}')`; } return `url('${source.fileName}')`; } // Handle '/' of dir Relative path let relativeDirPath = source.assetDirRelativePath; if (source.assetDirRelativePath[-1] !== '/') { relativeDirPath = `${source.assetDirRelativePath}/`; } if (source.assetDirRelativePath[0] !== '/') { relativeDirPath = `/${source.assetDirRelativePath}`; } return `url('file:///android_asset${relativeDirPath}${source.fileName}')`; }; const getFontFace = (fontFamily, fontWeight, sources, iosFontBase64Map) => { const numOfSources = sources.length; const sourcesString = sources.map((source, index) => { const suffix = index === numOfSources - 1 ? ';' : ','; const url = getUrl(source, iosFontBase64Map); const format = source?.format ? `format('${source.format}')` : ''; return `${url} ${format}${suffix}`; }).join('\n'); return ` @font-face { font-family: '${fontFamily}'; src: ${sourcesString} font-weight: ${fontWeight.valueOf()}; } `; }; export const getFontFacesString = (fonts, iosFontBase64Map) => { if (!fonts) return ''; const fontFaces = []; // Iterate over all the fonts and their variants // create font-face for each variant Object.entries(fonts).forEach(([familyName, familyFonts]) => { familyFonts.forEach(fontData => { fontFaces.push(getFontFace(familyName, fontData.fontWeight, fontData.sources, iosFontBase64Map)); }); }); // Combine all the font faces into a single string return fontFaces.join('\n'); }; //# sourceMappingURL=fontFaces.js.map