Skip to content

Commit e5b8066

Browse files
committed
Improved HTML export
1 parent 43e699e commit e5b8066

File tree

1 file changed

+39
-4
lines changed

1 file changed

+39
-4
lines changed

js/export/writeHtml.js

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export function writeHtml({
157157
newLine = true;
158158
}
159159

160+
let underlinePrev = false;
161+
160162
for (let i = 0; i < lineObj.words.length; i++) {
161163
const wordObj = lineObj.words[i];
162164
if (!wordObj) continue;
@@ -262,7 +264,6 @@ export function writeHtml({
262264

263265
styleStr += `font-weight:${fontI.fontFaceWeight};`;
264266
styleStr += `font-style:${fontI.fontFaceStyle};`;
265-
styleStr += `padding-left:${leftPad}px;`;
266267

267268
// Line height must match the height of the font bounding box for the font metrics to be accurate.
268269
styleStr += `line-height:${metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent}px;`;
@@ -272,7 +273,42 @@ export function writeHtml({
272273
styleStr += `vertical-align:${supOffset}px;`;
273274
}
274275

275-
activeLine.bodyWordsStr += ` <span class="scribe-word" id="${wordObj.id}" style="${styleStr}">${innerHTML}</span>\n`;
276+
if (wordObj.style.underline) {
277+
styleStr += 'text-decoration:underline;';
278+
styleStr += `text-decoration-color:${fill};`;
279+
styleStr += `text-decoration-thickness:${Math.ceil(fontSizeHTML / 12)}px;`;
280+
styleStr += `text-underline-offset:${Math.ceil(fontSizeHTML / 12) + Math.ceil(fontSizeHTML / 24)}px;`;
281+
}
282+
283+
if (i > 0) {
284+
let styleStrSpace = `font-family:${fontI.fontFaceName};`;
285+
const spaceAdvancePx = (fontI.opentype.charToGlyph(' ').advanceWidth / fontI.opentype.unitsPerEm);
286+
const fontSizeHTMLSpace = leftPad / spaceAdvancePx;
287+
if (fontSizeHTMLSpace > fontSizeHTML * 3) {
288+
styleStrSpace += `font-size:${fontSizeHTML}px;`;
289+
290+
const leftPadFinal = leftPad - spaceAdvancePx * fontSizeHTML;
291+
292+
styleStrSpace += `padding-left:${leftPadFinal}px;`;
293+
} else {
294+
styleStrSpace += `font-size:${fontSizeHTML}px;`;
295+
const leftPadFinal = leftPad - spaceAdvancePx * fontSizeHTML;
296+
styleStrSpace += `word-spacing:${leftPadFinal}px;`;
297+
}
298+
299+
if (underlinePrev) {
300+
styleStrSpace += 'text-decoration:underline;';
301+
styleStrSpace += `text-decoration-color:${fill};`;
302+
styleStrSpace += `text-decoration-thickness:${Math.ceil(fontSizeHTML / 12)}px;`;
303+
styleStrSpace += `text-underline-offset:${Math.ceil(fontSizeHTML / 12) + Math.ceil(fontSizeHTML / 24)}px;`;
304+
}
305+
306+
activeLine.bodyWordsStr += `<span class="scribe-space" style=${styleStrSpace}> </span>`;
307+
}
308+
309+
activeLine.bodyWordsStr += `<span class="scribe-word" id="${wordObj.id}" style="${styleStr}">${innerHTML}</span>`;
310+
311+
underlinePrev = wordObj.style.underline;
276312

277313
wordObjPrev = wordObj;
278314
rightSideBearingPrev = rightSideBearing;
@@ -299,7 +335,6 @@ export function writeHtml({
299335
styleStr += ' }\n';
300336

301337
styleStr += ' .scribe-line {\n';
302-
styleStr += ' font-size:0px;\n';
303338
styleStr += ' position:absolute;\n';
304339
styleStr += ' white-space:nowrap;\n';
305340
styleStr += ' }\n';
@@ -325,7 +360,7 @@ export function writeHtml({
325360

326361
const metaStr = '<meta charset="UTF-8">\n';
327362

328-
const htmlStr = `<html>\n<head>\n${metaStr}${styleStr}</head>\n${bodyStr}</html>`;
363+
const htmlStr = `<!doctype html>\n<html>\n<head>\n${metaStr}${styleStr}</head>\n${bodyStr}</html>`;
329364

330365
FontCont.state.enableOpt = enableOptSaved;
331366

0 commit comments

Comments
 (0)