Skip to content

Commit

Permalink
Support view woff2 font.
Browse files Browse the repository at this point in the history
  • Loading branch information
cweijan committed Mar 5, 2024
1 parent b6a0f3f commit f5ea47d
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This extension supports previewing these common office file formats.
- Word: .docx
- Svg: .svg
- Pdf: .pdf
- Font: .ttf, .otf, .woff
- Font: .ttf, .otf, .woff, .woff2
- Markdown: .md
- HttpRequest: .http
- Windows Reg: .reg
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@
{
"filenamePattern": "*.woff"
},
{
"filenamePattern": "*.woff2"
},
{
"filenamePattern": "*.otf"
},
Expand Down
1 change: 1 addition & 0 deletions resource/font/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<base target="_frame"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CharMap - Powered by OpenType.js</title>
<base href="{{baseUrl}}/">
<link href='css/index.css' rel='stylesheet' type='text/css'/>
<script src="js/opentype.min.js"></script>
<script src="../lib/vscode.js" type="text/javascript"></script>
Expand Down
12 changes: 11 additions & 1 deletion resource/font/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ onload = () => {
if (vscodeEvent) {
vscodeEvent.emit("init")
vscodeEvent.on("open", (content) => {
if (content.path.includes('woff2')) {
const loadScript = (src) => new Promise((onload) => document.documentElement.append(
Object.assign(document.createElement('script'), { src, onload })
));
loadScript('js/woff2_decompress_binding.js')
.then(() => fetch(content.path))
.then(f => f.arrayBuffer())
.then((buffer) => Module.decompress(buffer))
.then((buffer) => onFontLoaded(opentype.parse(Uint8Array.from(buffer).buffer)));
}
opentype.load(content.path, function (err, font) {
var amount, glyph, ctx, x, y, fontSize;
if (err) {
Expand Down Expand Up @@ -470,5 +480,5 @@ function hidpi(canvas, height, width) {
}

function showErrorMessage(message) {
if (message) alert(message);
if (message) console.error(message);
}
1 change: 1 addition & 0 deletions resource/font/js/woff2_decompress_binding.js

Large diffs are not rendered by default.

16 changes: 10 additions & 6 deletions src/provider/officeViewerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider
break;
case ".ttf":
case ".woff":
case ".woff2":
case ".otf":
this.handleFont(handler)
break;
Expand Down Expand Up @@ -145,19 +146,22 @@ export class OfficeViewerProvider implements vscode.CustomReadonlyEditorProvider


private handlePdf(webview: vscode.Webview) {
const baseUrl = webview.asWebviewUri(vscode.Uri.file(this.extensionPath + "/resource/pdf"))
.toString().replace(/\?.+$/, '').replace('https://git', 'https://file');
const baseUrl = this.getBaseUrl(webview, 'pdf')
webview.html = readFileSync(this.extensionPath + "/resource/pdf/viewer.html", 'utf8').replace("{{baseUrl}}", baseUrl)
}

private handleFont(handler: Handler) {
const webview = handler.panel.webview;
webview.html = Util.buildPath(
readFileSync(`${this.extensionPath}/resource/font/index.html`, 'utf8'),
webview, `${this.extensionPath}/resource/font`
)
const baseUrl = this.getBaseUrl(webview, 'font')
webview.html = readFileSync(`${this.extensionPath}/resource/font/index.html`, 'utf8')
.replace('{{baseUrl}}', baseUrl)
}

private getBaseUrl(webview: vscode.Webview, path: string) {
const baseUrl = webview.asWebviewUri(vscode.Uri.file(`${this.extensionPath}/resource/${path}`))
.toString().replace(/\?.+$/, '').replace('https://git', 'https://file')
return baseUrl;
}

private handleXlsx(uri: vscode.Uri, handler: Handler) {
const enc = new TextEncoder();
Expand Down

0 comments on commit f5ea47d

Please sign in to comment.