Skip to content

Commit 0f4bc19

Browse files
committed
Updated HTML export
1 parent ef02a17 commit 0f4bc19

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

js/export/writeHtml.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,12 @@ export function writeHtml({
188188
const scale = 1;
189189
const angle = 0;
190190

191-
// HTML exports currently only use raw fonts, as the fonts are retrieved from a CDN.
192-
const fontI = FontCont.getWordFont(wordObj);
193-
fontsUsed.add(fontI);
194-
195191
const {
196192
charSpacing, leftSideBearing, rightSideBearing, fontSize, charArr, advanceArr, kerningArr, font,
197193
} = calcWordMetrics(wordObj);
198194

195+
fontsUsed.add(font);
196+
199197
const wordStr = charArr.join('');
200198

201199
const charSpacingHTML = charSpacing * scale;
@@ -204,9 +202,9 @@ export function writeHtml({
204202

205203
const fontSizeHTML = fontSize * scale;
206204

207-
const metrics = calcFontMetrics(fontI, fontSizeHTML);
205+
const metrics = calcFontMetrics(font, fontSizeHTML);
208206

209-
const fontSizeHTMLSmallCaps = fontSize * scale * fontI.smallCapsMult;
207+
const fontSizeHTMLSmallCaps = fontSize * scale * font.smallCapsMult;
210208

211209
if (metrics.fontBoundingBoxAscent > activeLine.maxFontBoundingBoxAscentLine) {
212210
activeLine.maxFontBoundingBoxAscentLine = metrics.fontBoundingBoxAscent;
@@ -218,7 +216,7 @@ export function writeHtml({
218216
let styleStr = '';
219217

220218
styleStr += `font-size:${fontSizeHTML}px;`;
221-
styleStr += `font-family:${fontI.fontFaceName};`;
219+
styleStr += `font-family:${font.fontFaceName};`;
222220

223221
if (Math.abs(angle ?? 0) > 0.05) {
224222
styleStr += `transform-origin:left ${y1 - topHTML}px;`;
@@ -265,8 +263,8 @@ export function writeHtml({
265263

266264
styleStr += `letter-spacing:${formatNum(charSpacingHTML)}px;`;
267265

268-
styleStr += `font-weight:${fontI.fontFaceWeight};`;
269-
styleStr += `font-style:${fontI.fontFaceStyle};`;
266+
styleStr += `font-weight:${font.fontFaceWeight};`;
267+
styleStr += `font-style:${font.fontFaceStyle};`;
270268

271269
// Line height must match the height of the font bounding box for the font metrics to be accurate.
272270
styleStr += `line-height:${metrics.fontBoundingBoxAscent + metrics.fontBoundingBoxDescent}px;`;
@@ -284,10 +282,9 @@ export function writeHtml({
284282
}
285283

286284
if (i > 0) {
287-
let styleStrSpace = `font-family:${fontI.fontFaceName};`;
288-
styleStrSpace += `font-style:${fontI.fontFaceStyle};`;
289-
const spaceAdvance = fontI.opentype.charToGlyph(' ').advanceWidth || fontI.opentype.unitsPerEm * 0.35;
290-
const spaceAdvancePx = (spaceAdvance / fontI.opentype.unitsPerEm);
285+
let styleStrSpace = '';
286+
const spaceAdvance = font.opentype.charToGlyph(' ').advanceWidth || font.opentype.unitsPerEm * 0.35;
287+
const spaceAdvancePx = (spaceAdvance / font.opentype.unitsPerEm);
291288
const fontSizeHTMLSpace = leftPad / spaceAdvancePx;
292289
if (fontSizeHTMLSpace > fontSizeHTML * 3) {
293290
styleStrSpace += `font-size:${fontSizeHTML}px;`;
@@ -301,6 +298,10 @@ export function writeHtml({
301298
styleStrSpace += `word-spacing:${formatNum(leftPadFinal)}px;`;
302299
}
303300

301+
styleStrSpace += `font-family:${font.fontFaceName};`;
302+
styleStrSpace += `font-style:${font.fontFaceStyle};`;
303+
styleStrSpace += `font-weight:${font.fontFaceWeight};`;
304+
304305
if (underlinePrev) {
305306
styleStrSpace += `color:${fill};`;
306307
styleStrSpace += `opacity:${opacity};`;
@@ -362,9 +363,16 @@ export function writeHtml({
362363
styleStr += ' }\n';
363364

364365
for (const fontI of fontsUsed) {
365-
const cdnPath = 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/all/';
366-
let styleTitleCase = fontI.style.charAt(0).toUpperCase() + fontI.style.slice(1).toLowerCase();
367-
if (styleTitleCase === 'Normal') styleTitleCase = 'Regular';
366+
const cdnPath = 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/all/';
367+
let styleTitleCase = 'Regular';
368+
if (fontI.style === 'italic') {
369+
styleTitleCase = 'Italic';
370+
} else if (fontI.style === 'bold') {
371+
styleTitleCase = 'Bold';
372+
} else if (fontI.style === 'boldItalic') {
373+
styleTitleCase = 'BoldItalic';
374+
}
375+
368376
const fontName = `${fontI.family}-${styleTitleCase}.woff`;
369377
const fontPath = cdnPath + fontName;
370378

0 commit comments

Comments
 (0)