| 1 | import { afterEach, describe, expect, it } from 'vitest' |
| 2 | import { copyFile, mkdtemp, mkdir, rm, writeFile } from 'fs/promises' |
| 3 | import os from 'os' |
| 4 | import path from 'path' |
| 5 | import { createFont } from 'fonteditor-core' |
| 6 | import type { HtmlToPptxSlide } from '@arcsin1/html2pptx' |
| 7 | import { collectEmbeddedFonts } from '../../../src/main/io/html-pptx/font-collect' |
| 8 | |
| 9 | const tempDirectories: string[] = [] |
| 10 | |
| 11 | const readEotHeaderNames = (data: Uint8Array): { family: string; style: string } => { |
| 12 | const view = new DataView(data.buffer, data.byteOffset, data.byteLength) |
| 13 | let offset = 82 |
| 14 | const readName = (): string => { |
| 15 | const byteLength = view.getUint16(offset, true) |
| 16 | offset += 2 |
| 17 | const value = new TextDecoder('utf-16le').decode(data.slice(offset, offset + byteLength)) |
| 18 | offset += byteLength + 2 |
| 19 | return value |
| 20 | } |
| 21 | return { family: readName(), style: readName() } |
| 22 | } |
| 23 | |
| 24 | const isPlainSfnt = (data: Uint8Array): boolean => |
| 25 | (data[0] === 0x00 && data[1] === 0x01 && data[2] === 0x00 && data[3] === 0x00) || |
| 26 | (data[0] === 0x4f && data[1] === 0x54 && data[2] === 0x54 && data[3] === 0x4f) |
| 27 | |
| 28 | const readSfntNames = (data: Uint8Array): { family: string; style: string } => { |
| 29 | const arrayBuffer = new ArrayBuffer(data.byteLength) |
| 30 | new Uint8Array(arrayBuffer).set(data) |
| 31 | const isCff = data[0] === 0x4f && data[1] === 0x54 && data[2] === 0x54 && data[3] === 0x4f |
| 32 | const font = createFont(arrayBuffer, { type: isCff ? 'otf' : 'ttf', hinting: false }) |
| 33 | const ttf = font.get() |
| 34 | return { family: ttf.name.fontFamily, style: ttf.name.fontSubFamily } |
| 35 | } |
| 36 | |
| 37 | afterEach(async () => { |
| 38 | await Promise.all(tempDirectories.splice(0).map((directory) => rm(directory, { recursive: true, force: true }))) |
| 39 | }) |
| 40 | |
| 41 | describe('PPTX font embedding', () => { |
| 42 | it('embeds only the user font faces actually used by exported text', async () => { |
| 43 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'oh-my-ppt-font-')) |
| 44 | tempDirectories.push(projectDir) |
| 45 | const userFontsDir = path.join(projectDir, 'assets', 'fonts', 'user-fonts') |
| 46 | const titleDir = path.join(userFontsDir, 'title-font') |
| 47 | const bodyDir = path.join(userFontsDir, 'body-font') |
| 48 | await Promise.all([mkdir(titleDir, { recursive: true }), mkdir(bodyDir, { recursive: true })]) |
| 49 | |
| 50 | const regularSource = path.resolve('resources/google-fonts/Poppins/400-normal-0.woff2') |
| 51 | const boldSource = path.resolve('resources/google-fonts/Poppins/700-normal-0.woff2') |
| 52 | await Promise.all([ |
| 53 | copyFile(boldSource, path.join(titleDir, 'title.woff2')), |
| 54 | copyFile(regularSource, path.join(bodyDir, 'body.woff2')) |
| 55 | ]) |
| 56 | |
| 57 | const htmlPath = path.join(projectDir, 'page-1.html') |
| 58 | await writeFile( |
| 59 | htmlPath, |
| 60 | `<style> |
| 61 | @font-face { font-family: "User Title"; src: url("./assets/fonts/user-fonts/title-font/title.woff2") format("woff2"); font-weight: 700; font-style: normal; } |
| 62 | @font-face { font-family: "User Body"; src: url("./assets/fonts/user-fonts/body-font/body.woff2") format("woff2"); font-weight: 400; font-style: normal; } |
| 63 | </style>`, |
| 64 | 'utf-8' |
| 65 | ) |
| 66 | |
| 67 | const slides: HtmlToPptxSlide[] = [ |
| 68 | { |
| 69 | texts: [ |
| 70 | { text: 'Title', x: 0, y: 0, w: 2, h: 0.4, fontSize: 20, fontFace: 'User Title', bold: true }, |
| 71 | { text: 'Body', x: 0, y: 1, w: 2, h: 0.3, fontSize: 12, fontFace: 'User Body' } |
| 72 | ] |
| 73 | } |
| 74 | ] |
| 75 | |
| 76 | const embeddedFonts = await collectEmbeddedFonts(projectDir, slides, { |
| 77 | mode: 'always', |
| 78 | pageHtmlPaths: [htmlPath] |
| 79 | }) |
| 80 | |
| 81 | expect(embeddedFonts).toHaveLength(2) |
| 82 | expect(embeddedFonts.map((font) => [font.fontFace, font.style]).sort()).toEqual([ |
| 83 | ['User Body', 'regular'], |
| 84 | ['User Title', 'bold'] |
| 85 | ]) |
| 86 | // 默认载荷必须是裸 TTF(sfnt):PowerPoint for Mac 对 EOT 容器载荷不应用 b="1" |
| 87 | expect(embeddedFonts.every((font) => isPlainSfnt(font.ttfBuffer))).toBe(true) |
| 88 | expect( |
| 89 | embeddedFonts.map((font) => readSfntNames(font.ttfBuffer)).sort((a, b) => a.family.localeCompare(b.family)) |
| 90 | ).toEqual([ |
| 91 | { family: 'User Body', style: 'Regular' }, |
| 92 | { family: 'User Title', style: 'Bold' } |
| 93 | ]) |
| 94 | }) |
| 95 | |
| 96 | it('still emits an EOT container payload when explicitly requested', async () => { |
| 97 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'oh-my-ppt-font-')) |
| 98 | tempDirectories.push(projectDir) |
| 99 | const fontDir = path.join(projectDir, 'assets', 'fonts', 'user-fonts', 'legacy') |
| 100 | await mkdir(fontDir, { recursive: true }) |
| 101 | await copyFile( |
| 102 | path.resolve('resources/google-fonts/Poppins/400-normal-0.woff2'), |
| 103 | path.join(fontDir, 'font.woff2') |
| 104 | ) |
| 105 | |
| 106 | const htmlPath = path.join(projectDir, 'page-1.html') |
| 107 | await writeFile( |
| 108 | htmlPath, |
| 109 | `<style>@font-face { font-family: "EOT Face"; src: url("./assets/fonts/user-fonts/legacy/font.woff2") format("woff2"); font-weight: 400; font-style: normal; }</style>`, |
| 110 | 'utf-8' |
| 111 | ) |
| 112 | |
| 113 | const slides: HtmlToPptxSlide[] = [ |
| 114 | { |
| 115 | texts: [{ text: 'Body', x: 0, y: 1, w: 2, h: 0.3, fontSize: 12, fontFace: 'EOT Face' }] |
| 116 | } |
| 117 | ] |
| 118 | |
| 119 | const embeddedFonts = await collectEmbeddedFonts(projectDir, slides, { |
| 120 | mode: 'always', |
| 121 | payloadFormat: 'eot', |
| 122 | pageHtmlPaths: [htmlPath] |
| 123 | }) |
| 124 | |
| 125 | expect(embeddedFonts).toHaveLength(1) |
| 126 | const names = readEotHeaderNames(embeddedFonts[0].ttfBuffer) |
| 127 | expect(names.family).toBe('EOT Face') |
| 128 | expect(names.style).toBe('Regular') |
| 129 | }) |
| 130 | |
| 131 | it('produces a plain sfnt payload when payloadFormat is ttf', async () => { |
| 132 | const projectDir = await mkdtemp(path.join(os.tmpdir(), 'oh-my-ppt-font-')) |
| 133 | tempDirectories.push(projectDir) |
| 134 | const fontDir = path.join(projectDir, 'assets', 'fonts', 'user-fonts', 'exp') |
| 135 | await mkdir(fontDir, { recursive: true }) |
| 136 | await copyFile( |
| 137 | path.resolve('resources/google-fonts/Poppins/400-normal-0.woff2'), |
| 138 | path.join(fontDir, 'font.woff2') |
| 139 | ) |
| 140 | |
| 141 | const htmlPath = path.join(projectDir, 'page-1.html') |
| 142 | await writeFile( |
| 143 | htmlPath, |
| 144 | `<style>@font-face { font-family: "TTF Face"; src: url("./assets/fonts/user-fonts/exp/font.woff2") format("woff2"); font-weight: 400; font-style: normal; }</style>`, |
| 145 | 'utf-8' |
| 146 | ) |
| 147 | |
| 148 | const slides: HtmlToPptxSlide[] = [ |
| 149 | { |
| 150 | texts: [{ text: 'Body', x: 0, y: 1, w: 2, h: 0.3, fontSize: 12, fontFace: 'TTF Face' }] |
| 151 | } |
| 152 | ] |
| 153 | |
| 154 | const embeddedFonts = await collectEmbeddedFonts(projectDir, slides, { |
| 155 | mode: 'always', |
| 156 | payloadFormat: 'ttf', |
| 157 | pageHtmlPaths: [htmlPath] |
| 158 | }) |
| 159 | |
| 160 | expect(embeddedFonts).toHaveLength(1) |
| 161 | const payload = embeddedFonts[0].ttfBuffer |
| 162 | expect(isPlainSfnt(payload)).toBe(true) |
| 163 | |
| 164 | const names = readSfntNames(payload) |
| 165 | expect(names.family).toBe('TTF Face') |
| 166 | expect(names.style).toBe('Regular') |
| 167 | }) |
| 168 | }) |
| 169 |