/**
* Fontello icon set component.
* Usage:
*/
import { type CreateIconSetOptions, createIconSet, type IconComponent } from '@react-native-vector-icons/common';
type IcoMoonIcon = {
properties: {
name: string;
code: number;
};
};
type IcoMoonGlyph = {
extras: {
name: string;
codePoint: number;
};
};
type IcoMoonConfig =
| {
// Old IcoMoon app (https://icomoon.io/app)
icons: IcoMoonIcon[];
preferences?: {
fontPref?: {
metadata?: {
fontFamily?: string;
};
};
};
}
| {
// New IcoMoon app (https://icomoon.io/new-app)
glyphs: IcoMoonGlyph[];
preferences?: {
fontPref?: {
metadata?: {
fontFamily?: string;
};
};
};
};
// entries are optional because they can be derived from the config
type Options = Partial;
export default function createIconSetFromIcoMoon(
config: IcoMoonConfig,
postScriptName?: string,
fontFileName?: string,
): IconComponent>;
export default function createIconSetFromIcoMoon(
config: IcoMoonConfig,
options: Options,
): IconComponent>;
export default function createIconSetFromIcoMoon(
config: IcoMoonConfig,
postScriptNameOrOptions?: string | Options,
fontFileNameParam?: string,
): IconComponent> {
const { postScriptName, fontFileName, fontSource, fontStyle } =
typeof postScriptNameOrOptions === 'object'
? postScriptNameOrOptions
: {
postScriptName: postScriptNameOrOptions,
fontFileName: fontFileNameParam,
};
const glyphMap: Record = {};
if ('icons' in config) {
// Old IcoMoon app
config.icons.forEach((icon) => {
icon.properties.name.split(/\s*,\s*/g).forEach((name) => {
glyphMap[name] = icon.properties.code;
});
});
} else if ('glyphs' in config) {
// New IcoMoon app
config.glyphs.forEach((glyph) => {
glyph.extras.name.split(/\s*,\s*/g).forEach((name) => {
glyphMap[name] = glyph.extras.codePoint;
});
});
} else {
throw new Error('Invalid IcoMoon config: expected "icons" (old format) or "glyphs" (new format)');
}
const fontFamily = postScriptName || (config.preferences?.fontPref?.metadata?.fontFamily ?? 'icomoon');
return createIconSet>(glyphMap as Record, {
postScriptName: fontFamily,
fontFileName: fontFileName || `${fontFamily}.ttf`,
fontSource,
fontStyle,
});
}