v0.8.1 - CJK fonts for downloading Individual Parts

If there is no font, CJK (East Asian) characters will be rendered as "tofu"
This commit is contained in:
Xmader 2020-09-28 17:08:52 -04:00
parent a11e92a1de
commit 5e450f4fe2
4 changed files with 52 additions and 6 deletions

25
dist/main.js vendored
View file

@ -3,7 +3,7 @@
// @namespace https://www.xmader.com/
// @homepageURL https://github.com/Xmader/musescore-downloader/
// @supportURL https://github.com/Xmader/musescore-downloader/issues
// @version 0.8.0
// @version 0.8.1
// @description download sheet music from musescore.com for free, no login or Musescore Pro required | 免登录、免 Musescore Pro免费下载 musescore.com 上的曲谱
// @author Xmader
// @match https://musescore.com/*/*
@ -26445,6 +26445,9 @@ Please pipe the document into a Node stream.\
});
const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js';
// fonts for Chinese characters (CN) and Korean hangul (KR)
// JP characters are included in the CN font
const FONT_URLS = ['CN', 'KR'].map(l => `https://cdn.jsdelivr.net/npm/@librescore/fonts/SourceHanSans${l}-Regular.woff2`);
const initMscore = (w) => __awaiter(void 0, void 0, void 0, function* () {
if (!w['WebMscore']) {
// init webmscore (https://github.com/LibreScore/webmscore)
@ -26454,12 +26457,25 @@ Please pipe the document into a Node stream.\
yield new Promise(resolve => { script.onload = resolve; });
}
});
let fonts;
const initFonts = () => {
// load CJK fonts
// CJK (East Asian) characters will be rendered as "tofu" if there is no font
if (!fonts) {
fonts = Promise.all(FONT_URLS.map((url) => __awaiter(void 0, void 0, void 0, function* () {
const r = yield fetch(url);
const data = yield r.arrayBuffer();
return new Uint8Array(data);
})));
}
};
const loadMscore = (w) => __awaiter(void 0, void 0, void 0, function* () {
initFonts();
yield initMscore(w);
const WebMscore = w['WebMscore'];
// parse mscz data
const data = new Uint8Array(new Uint8Array(yield fetchMscz()));
const score = yield WebMscore.load('mscz', data);
const score = yield WebMscore.load('mscz', data, yield fonts);
yield score.generateExcerpts();
return score;
});
@ -26604,10 +26620,12 @@ Please pipe the document into a Node stream.\
action: BtnAction.mscoreWindow((w, score, txt) => __awaiter(void 0, void 0, void 0, function* () {
const metadata = yield score.metadata();
console.log('score metadata loaded by webmscore', metadata);
// add the "full score" option as a "part"
metadata.excerpts.unshift({ id: -1, title: 'Full score', parts: [] });
// render the part selection page
txt.remove();
const fieldset = w.document.createElement('fieldset');
metadata.excerpts.unshift({ id: -1, title: 'Full score', parts: [] });
// part selection
for (const excerpt of metadata.excerpts) {
const id = excerpt.id;
const partName = excerpt.title;
@ -26622,6 +26640,7 @@ Please pipe the document into a Node stream.\
const br = w.document.createElement('br');
fieldset.append(e, label, br);
}
// submit button
const submitBtn = w.document.createElement('input');
submitBtn.type = 'submit';
submitBtn.value = 'Download PDF';

View file

@ -1,6 +1,6 @@
{
"name": "musescore-downloader",
"version": "0.8.0",
"version": "0.8.1",
"description": "download sheet music from musescore.com for free, no login or Musescore Pro required | 免登录、免 Musescore Pro免费下载 musescore.com 上的曲谱",
"main": "dist/main.js",
"repository": {

View file

@ -53,10 +53,14 @@ const main = (): void => {
const metadata = await score.metadata()
console.log('score metadata loaded by webmscore', metadata)
// add the "full score" option as a "part"
metadata.excerpts.unshift({ id: -1, title: 'Full score', parts: [] })
// render the part selection page
txt.remove()
const fieldset = w.document.createElement('fieldset')
metadata.excerpts.unshift({ id: -1, title: 'Full score', parts: [] })
// part selection
for (const excerpt of metadata.excerpts) {
const id = excerpt.id
const partName = excerpt.title
@ -74,9 +78,12 @@ const main = (): void => {
const br = w.document.createElement('br')
fieldset.append(e, label, br)
}
// submit button
const submitBtn = w.document.createElement('input')
submitBtn.type = 'submit'
submitBtn.value = 'Download PDF'
fieldset.append(submitBtn)
w.document.body.append(fieldset)

View file

@ -3,6 +3,10 @@ import { fetchMscz } from './mscz'
const WEBMSCORE_URL = 'https://cdn.jsdelivr.net/npm/webmscore@0.10/webmscore.js'
// fonts for Chinese characters (CN) and Korean hangul (KR)
// JP characters are included in the CN font
const FONT_URLS = ['CN', 'KR'].map(l => `https://cdn.jsdelivr.net/npm/@librescore/fonts/SourceHanSans${l}-Regular.woff2`)
export type WebMscore = import('webmscore').default
const initMscore = async (w: Window) => {
@ -15,7 +19,23 @@ const initMscore = async (w: Window) => {
}
}
let fonts: Promise<Uint8Array[]> | undefined
const initFonts = () => {
// load CJK fonts
// CJK (East Asian) characters will be rendered as "tofu" if there is no font
if (!fonts) {
fonts = Promise.all(
FONT_URLS.map(async (url) => {
const r = await fetch(url)
const data = await r.arrayBuffer()
return new Uint8Array(data)
}),
)
}
}
export const loadMscore = async (w: Window): Promise<WebMscore> => {
initFonts()
await initMscore(w)
const WebMscore: typeof import('webmscore').default = w['WebMscore']
@ -23,7 +43,7 @@ export const loadMscore = async (w: Window): Promise<WebMscore> => {
const data = new Uint8Array(
new Uint8Array(await fetchMscz()), // copy its ArrayBuffer
)
const score = await WebMscore.load('mscz', data)
const score = await WebMscore.load('mscz', data, await fonts)
await score.generateExcerpts()
return score