| 110 | }; |
| 111 | |
| 112 | async function loadGoogleFont(fonts: string | string[], text: string) { |
| 113 | // @TODO: Support multiple fonts. |
| 114 | const font = Array.isArray(fonts) ? fonts.at(-1) : fonts; |
| 115 | if (!font || !text) return; |
| 116 | |
| 117 | const API = `https://fonts.googleapis.com/css2?family=${font}&text=${ |
| 118 | encodeURIComponent( |
| 119 | text, |
| 120 | ) |
| 121 | }`; |
| 122 | |
| 123 | const css = await ( |
| 124 | await fetch(API, { |
| 125 | headers: { |
| 126 | // Make sure it returns TTF. |
| 127 | "User-Agent": |
| 128 | "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; de-at) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1", |
| 129 | }, |
| 130 | }) |
| 131 | ).text(); |
| 132 | |
| 133 | const resource = css.match( |
| 134 | /src: url\((.+)\) format\('(opentype|truetype)'\)/, |
| 135 | ); |
| 136 | if (!resource) throw new Error("Failed to load font"); |
| 137 | |
| 138 | return fetch(resource[1]).then((res) => res.arrayBuffer()); |
| 139 | } |
| 140 | |
| 141 | type Asset = SatoriOptions["fonts"][0] | string; |
| 142 | |