( ctx: FontContext, buffer: Uint8Array, formats: FontFileFormat[], name?: string, )
| 47 | * @returns A promise that resolves to an array of conversion results with data and filenames. |
| 48 | */ |
| 49 | export const convertFont = async ( |
| 50 | ctx: FontContext, |
| 51 | buffer: Uint8Array, |
| 52 | formats: FontFileFormat[], |
| 53 | name?: string, |
| 54 | ): Promise<ConversionResult[]> => { |
| 55 | const { glyphtContext, compressionContext } = ctx; |
| 56 | const uniqueFormats = [...new Set(formats)]; |
| 57 | |
| 58 | // Identify and decompress compressed inputs to get a raw TTF buffer. |
| 59 | const type = WoffCompressionContext.compressionType(buffer); |
| 60 | const ttfBuffer = type |
| 61 | ? await compressionContext.decompressToTTF(buffer) |
| 62 | : buffer; |
| 63 | |
| 64 | const baseName = name |
| 65 | ? stripExtension(name) |
| 66 | : await deriveBaseName(glyphtContext, ttfBuffer); |
| 67 | |
| 68 | return Promise.all( |
| 69 | uniqueFormats.map(async (format): Promise<ConversionResult> => { |
| 70 | if (format === 'ttf') { |
| 71 | return { |
| 72 | filename: `${baseName}.ttf`, |
| 73 | format: 'ttf', |
| 74 | data: ttfBuffer, |
| 75 | }; |
| 76 | } |
| 77 | |
| 78 | // Recompress the TTF buffer into the requested webfont format. |
| 79 | const data = await compressionContext.compressFromTTF(ttfBuffer, { |
| 80 | algorithm: format, |
| 81 | }); |
| 82 | |
| 83 | return { |
| 84 | filename: `${baseName}.${format}`, |
| 85 | format, |
| 86 | data, |
| 87 | }; |
| 88 | }), |
| 89 | ); |
| 90 | }; |
no test coverage detected