Доработки подгрузки шрифтов

This commit is contained in:
Book Pauk
2019-01-25 03:06:53 +07:00
parent 7e5314887a
commit 94b0c9def1
4 changed files with 69 additions and 42 deletions

View File

@@ -286,4 +286,9 @@ body, html, #app {
src: url('fonts/archivo.woff2') format('woff2'); src: url('fonts/archivo.woff2') format('woff2');
} }
@font-face {
font-family: 'Rubik';
src: url('fonts/rubik.woff2') format('woff2');
}
</style> </style>

View File

@@ -1,9 +1,9 @@
<template> <template>
<div ref="main" class="main"> <div ref="main" class="main">
<div v-show="activeCanvas" class="layout"> <div v-show="toggleLayout" class="layout">
<div v-html="page1"></div> <div v-html="page1"></div>
</div> </div>
<div v-show="!activeCanvas" class="layout"> <div v-show="!toggleLayout" class="layout">
<div v-html="page2"></div> <div v-html="page2"></div>
</div> </div>
<div v-show="showStatusBar" ref="statusBar" class="layout"> <div v-show="showStatusBar" ref="statusBar" class="layout">
@@ -17,7 +17,8 @@
@click.prevent.stop="onStatusBarClick"></div> @click.prevent.stop="onStatusBarClick"></div>
<div v-show="fontsLoading" ref="fontsLoading"></div> <div v-show="fontsLoading" ref="fontsLoading"></div>
</div> </div>
<canvas ref="offscreenCanvas" style="display: none"></canvas> <!-- невидимым делать нельзя, вовремя не подгружаютя шрифты -->
<canvas ref="offscreenCanvas" class="layout" style="width: 0px; height: 0px"></canvas>
</div> </div>
</template> </template>
@@ -41,7 +42,7 @@ export default @Component({
}, },
}) })
class TextPage extends Vue { class TextPage extends Vue {
activeCanvas = false; toggleLayout = false;
showStatusBar = false; showStatusBar = false;
page1 = null; page1 = null;
page2 = null; page2 = null;
@@ -115,8 +116,6 @@ class TextPage extends Vue {
this.$refs.layoutEvents.style.width = this.realWidth + 'px'; this.$refs.layoutEvents.style.width = this.realWidth + 'px';
this.$refs.layoutEvents.style.height = this.realHeight + 'px'; this.$refs.layoutEvents.style.height = this.realHeight + 'px';
this.activeCanvas = false;
this.w = this.realWidth - 2*this.indent; this.w = this.realWidth - 2*this.indent;
this.h = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0); this.h = this.realHeight - (this.showStatusBar ? this.statusBarHeight : 0);
this.lineHeight = this.fontSize + this.lineInterval; this.lineHeight = this.fontSize + this.lineInterval;
@@ -131,7 +130,7 @@ class TextPage extends Vue {
} }
//сообщение "Загрузка шрифтов..." //сообщение "Загрузка шрифтов..."
const flText = 'Загрузка шрифтов...'; const flText = 'Загрузка шрифта...';
this.$refs.fontsLoading.innerHTML = flText; this.$refs.fontsLoading.innerHTML = flText;
const fontsLoadingStyle = this.$refs.fontsLoading.style; const fontsLoadingStyle = this.$refs.fontsLoading.style;
fontsLoadingStyle.position = 'absolute'; fontsLoadingStyle.position = 'absolute';
@@ -172,18 +171,54 @@ class TextPage extends Vue {
return this.context.measureText(text).width; return this.context.measureText(text).width;
} }
async loadFonts() { async checkLoadedFonts() {
let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font))); let loaded = await Promise.all(this.fontList.map(font => document.fonts.check(font)));
if (loaded.some(r => !r)) { if (loaded.some(r => !r)) {
loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font))); loaded = await Promise.all(this.fontList.map(font => document.fonts.load(font)));
await document.fonts.ready;
if (loaded.some(r => !r.length)) if (loaded.some(r => !r.length))
throw new Error('some font not loaded'); throw new Error('some font not loaded');
} }
} }
async loadFonts() {
this.fontsLoading = true;
if (!this.fontsLoaded)
this.fontsLoaded = {};
//загрузка дин.шрифта
const loaded = this.fontsLoaded[this.fontCssUrl];
if (this.fontCssUrl && !loaded) {
loadCSS(this.fontCssUrl);
this.fontsLoaded[this.fontCssUrl] = 1;
}
const waitingTime = 10*1000;
const delay = 100;
let i = 0;
//ждем шрифты
while (i < waitingTime/delay) {
i++;
try {
await this.checkLoadedFonts();
i = waitingTime;
} catch (e) {
await sleep(delay);
}
}
if (i !== waitingTime) {
this.$notify.error({
title: 'Ошибка загрузки',
message: 'Некоторые шрифты не удалось загрузить'
});
}
this.fontsLoading = false;
}
showBook() { showBook() {
this.$refs.main.focus(); this.$refs.main.focus();
this.toggleLayout = false;
this.book = null; this.book = null;
this.meta = null; this.meta = null;
this.fb2 = null; this.fb2 = null;
@@ -196,7 +231,7 @@ class TextPage extends Vue {
this.textColor = '#000000'; this.textColor = '#000000';
this.backgroundColor = '#478355'; this.backgroundColor = '#478355';
this.fontStyle = '';// 'bold','italic' this.fontStyle = '';// 'bold','italic'
this.fontSize = 33;// px this.fontSize = 35;// px
this.fontName = 'Archivo'; this.fontName = 'Archivo';
this.fontCssUrl = ''; this.fontCssUrl = '';
this.lineInterval = 7;// px, межстрочный интервал this.lineInterval = 7;// px, межстрочный интервал
@@ -243,34 +278,20 @@ class TextPage extends Vue {
this.parsed = parsed; this.parsed = parsed;
this.calcDrawProps(); this.calcDrawProps();
//загрузка дин.шрифта
if (this.fontCssUrl)
loadCSS(this.fontCssUrl);
const waitingTime = 10*1000;
const delay = 100;
let i = 0;
this.fontsLoading = true;
//ждем шрифты
while (i < waitingTime/delay) {
i++;
try {
await this.loadFonts(); await this.loadFonts();
i = waitingTime;
} catch (e) {
await sleep(delay);
}
}
this.fontsLoading = false;
if (i !== waitingTime) {
this.$notify.error({
title: 'Ошибка загрузки',
message: 'Некоторые шрифты не удалось загрузить'
});
}
this.draw(); this.draw(true);
this.refreshTime(); this.refreshTime();
// шрифты хрен знает когда подгружаются, поэтому
let i = 0;
this.parsed.force = true;
while (i < 10) {
this.draw(true);
await sleep(1000);
i++;
}
this.parsed.force = false;
})(); })();
} }
} }
@@ -294,10 +315,10 @@ class TextPage extends Vue {
return; return;
} }
this.activeCanvas = !this.activeCanvas; this.toggleLayout = !this.toggleLayout;
if (immediate) { if (immediate || (this.parsed && this.parsed.force)) {
if (this.activeCanvas) if (this.toggleLayout)
this.page1 = this.drawPage(this.bookPos); this.page1 = this.drawPage(this.bookPos);
else else
this.page2 = this.drawPage(this.bookPos); this.page2 = this.drawPage(this.bookPos);
@@ -308,7 +329,7 @@ class TextPage extends Vue {
this.pagePrepared = false; this.pagePrepared = false;
this.debouncedPrepareNextPage(); this.debouncedPrepareNextPage();
} else { } else {
if (this.activeCanvas) if (this.toggleLayout)
this.page1 = this.drawPage(this.bookPos); this.page1 = this.drawPage(this.bookPos);
else else
this.page2 = this.drawPage(this.bookPos); this.page2 = this.drawPage(this.bookPos);
@@ -477,7 +498,7 @@ class TextPage extends Vue {
if (i >= 0 && this.linesDown.length > i) { if (i >= 0 && this.linesDown.length > i) {
this.bookPosPrepared = this.linesDown[i].begin; this.bookPosPrepared = this.linesDown[i].begin;
if (this.activeCanvas) if (this.toggleLayout)
this.page2 = this.drawPage(this.bookPosPrepared, true);//наоборот this.page2 = this.drawPage(this.bookPosPrepared, true);//наоборот
else else
this.page1 = this.drawPage(this.bookPosPrepared, true); this.page1 = this.drawPage(this.bookPosPrepared, true);

View File

@@ -346,7 +346,8 @@ export default class BookParser {
parsePara(paraIndex) { parsePara(paraIndex) {
const para = this.para[paraIndex]; const para = this.para[paraIndex];
if (para.parsed && if (!this.force &&
para.parsed &&
para.parsed.w === this.w && para.parsed.w === this.w &&
para.parsed.p === this.p && para.parsed.p === this.p &&
para.parsed.wordWrap === this.wordWrap && para.parsed.wordWrap === this.wordWrap &&

Binary file not shown.