|
| 1 | +import * as htmlToImage from '../../src' |
| 2 | +import { getSvgDocument } from './helper' |
| 3 | + |
| 4 | +describe('font embedding', () => { |
| 5 | + describe('should embed only used fonts', () => { |
| 6 | + it('should embed 1 font when use 1', async () => { |
| 7 | + const root = document.createElement('div') |
| 8 | + document.body.append(root) |
| 9 | + try { |
| 10 | + root.innerHTML = ` |
| 11 | + <style> |
| 12 | + @font-face { |
| 13 | + font-family: 'Font 0'; |
| 14 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 15 | + } |
| 16 | + @font-face { |
| 17 | + font-family: 'Font 1'; |
| 18 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 19 | + } |
| 20 | + @font-face { |
| 21 | + font-family: 'Font 2'; |
| 22 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 23 | + } |
| 24 | + </style> |
| 25 | + <p style="font-family: 'Font 1'">Hello world</p> |
| 26 | + ` |
| 27 | + const svg = await htmlToImage.toSvg(root) |
| 28 | + const doc = await getSvgDocument(svg) |
| 29 | + const [style] = Array.from(doc.getElementsByTagName('style')) |
| 30 | + expect(style.textContent).toContain('Font 1') |
| 31 | + expect(style.textContent).not.toContain('Font 0') |
| 32 | + expect(style.textContent).not.toContain('Font 2') |
| 33 | + } finally { |
| 34 | + root.remove() |
| 35 | + } |
| 36 | + }) |
| 37 | + it('should embed 2 fonts when use 2', async () => { |
| 38 | + const root = document.createElement('div') |
| 39 | + document.body.append(root) |
| 40 | + try { |
| 41 | + root.innerHTML = ` |
| 42 | + <style> |
| 43 | + @font-face { |
| 44 | + font-family: 'Font 0'; |
| 45 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 46 | + } |
| 47 | + @font-face { |
| 48 | + font-family: 'Font 1'; |
| 49 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 50 | + } |
| 51 | + @font-face { |
| 52 | + font-family: 'Font 2'; |
| 53 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 54 | + } |
| 55 | + </style> |
| 56 | + <p style="font-family: 'Font 0'">Hello world</p> |
| 57 | + <p style="font-family: 'Font 2'">Hello world</p> |
| 58 | + ` |
| 59 | + const svg = await htmlToImage.toSvg(root) |
| 60 | + const doc = await getSvgDocument(svg) |
| 61 | + const [style] = Array.from(doc.getElementsByTagName('style')) |
| 62 | + expect(style.textContent).toContain('Font 0') |
| 63 | + expect(style.textContent).toContain('Font 2') |
| 64 | + expect(style.textContent).not.toContain('Font 1') |
| 65 | + } finally { |
| 66 | + root.remove() |
| 67 | + } |
| 68 | + }) |
| 69 | + it('should embed font used by deeply nested child', async () => { |
| 70 | + const root = document.createElement('div') |
| 71 | + document.body.append(root) |
| 72 | + try { |
| 73 | + root.innerHTML = ` |
| 74 | + <style> |
| 75 | + @font-face { |
| 76 | + font-family: 'Font 0'; |
| 77 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 78 | + } |
| 79 | + @font-face { |
| 80 | + font-family: 'Font 1'; |
| 81 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 82 | + } |
| 83 | + @font-face { |
| 84 | + font-family: 'Font 2'; |
| 85 | + src: url('https://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Mu72xKKTU1Kvnz.woff2'); |
| 86 | + } |
| 87 | + </style> |
| 88 | + <div> |
| 89 | + <div> |
| 90 | + <div> |
| 91 | + <div style="font-family: 'Font 1'">Hello world</div> |
| 92 | + </div> |
| 93 | + </div> |
| 94 | + </div> |
| 95 | + ` |
| 96 | + const svg = await htmlToImage.toSvg(root) |
| 97 | + const doc = await getSvgDocument(svg) |
| 98 | + const [style] = Array.from(doc.getElementsByTagName('style')) |
| 99 | + expect(style.textContent).toContain('Font 1') |
| 100 | + expect(style.textContent).not.toContain('Font 0') |
| 101 | + expect(style.textContent).not.toContain('Font 2') |
| 102 | + } finally { |
| 103 | + root.remove() |
| 104 | + } |
| 105 | + }) |
| 106 | + }) |
| 107 | +}) |
0 commit comments