react-native-unit-components
Version:
Unit React Native components
48 lines (45 loc) • 1.57 kB
JavaScript
import { Platform } from 'react-native';
const getUrl = source => {
if (Platform.OS == 'ios') {
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) => {
const numOfSources = sources.length;
const sourcesString = sources.map((source, index) => {
const suffix = index === numOfSources - 1 ? ';' : ',';
const url = getUrl(source);
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 => {
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));
});
});
// Combine all the font faces into a single string
return fontFaces.join('\n');
};
//# sourceMappingURL=fontFaces.js.map