Skip to content

Commit 6e5b32f

Browse files
authored
Merge pull request #11275 from quarto-dev/feature/brand-default-font-source
Feature/brand default font sources
2 parents 4ef4ab8 + f6053cb commit 6e5b32f

File tree

8 files changed

+101
-33
lines changed

8 files changed

+101
-33
lines changed

src/command/render/pandoc.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,11 @@ import {
202202
} from "../../core/markdown-pipeline.ts";
203203
import { getEnv } from "../../../package/src/util/utils.ts";
204204
import { canonicalizeTitlePostprocessor } from "../../format/html/format-html-title.ts";
205+
import {
206+
BrandFontBunny,
207+
BrandFontFile,
208+
BrandFontGoogle,
209+
} from "../../resources/types/schema-types.ts";
205210

206211
// in case we are running multiple pandoc processes
207212
// we need to make sure we capture all of the trace files
@@ -1359,18 +1364,25 @@ async function resolveExtras(
13591364
const ttf_urls = [], woff_urls: Array<string> = [];
13601365
if (brand?.data.typography) {
13611366
const fonts = brand.data.typography.fonts || [];
1362-
for (const font of fonts) {
1363-
if (font.source === "file") {
1367+
for (const _font of fonts) {
1368+
// if font lacks a source, we assume google in typst output
1369+
1370+
// deno-lint-ignore no-explicit-any
1371+
const source: string = (_font as any).source ?? "google";
1372+
if (source === "file") {
1373+
const font = _font as BrandFontFile;
13641374
for (const file of font.files || []) {
13651375
const path = typeof file === "object" ? file.path : file;
13661376
fontdirs.add(dirname(join(brand.brandDir, path)));
13671377
}
1368-
} else if (font.source === "bunny") {
1378+
} else if (source === "bunny") {
1379+
const font = _font as BrandFontBunny;
13691380
console.log(
13701381
"Font bunny is not yet supported for Typst, skipping",
13711382
font.family,
13721383
);
1373-
} else if (font.source === "google" /* || font.source === "bunny" */) {
1384+
} else if (source === "google" /* || font.source === "bunny" */) {
1385+
const font = _font as BrandFontGoogle;
13741386
let { family, style, weight } = font;
13751387
const parts = [family!];
13761388
if (style) {
@@ -1382,7 +1394,7 @@ async function resolveExtras(
13821394
parts.push(weight.join(","));
13831395
}
13841396
const response = await fetch(
1385-
`${base_urls[font.source]}?family=${parts.join(":")}`,
1397+
`${base_urls[source]}?family=${parts.join(":")}`,
13861398
);
13871399
const lines = (await response.text()).split("\n");
13881400
for (const line of lines) {

src/core/sass/brand.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ProjectContext } from "../../project/types.ts";
1818
import {
1919
BrandFont,
2020
BrandFontBunny,
21+
BrandFontCommon,
2122
BrandFontGoogle,
2223
BrandFontWeight,
2324
} from "../../resources/types/schema-types.ts";
@@ -110,7 +111,7 @@ const fontFileFormat = (file: string): string => {
110111
}
111112
};
112113

113-
const bunnyFontImportString = (description: BrandFontBunny) => {
114+
const bunnyFontImportString = (description: BrandFontCommon) => {
114115
const bunnyName = (name: string) => name.replace(/ /g, "-");
115116
const bunnyFamily = description.family;
116117
if (!bunnyFamily) {
@@ -137,7 +138,6 @@ const bunnyFontImportString = (description: BrandFontBunny) => {
137138
return `@import url('https://fonts.bunny.net/css?family=${
138139
bunnyName(bunnyFamily)
139140
}:${weights}&display=${display}');`;
140-
// }
141141
};
142142

143143
const googleFontImportString = (description: BrandFontGoogle) => {
@@ -371,6 +371,41 @@ const brandTypographyBundle = (
371371
return googleFamily;
372372
};
373373

374+
const resolveBunnyFontFamily = (
375+
font: BrandFont[],
376+
): string | undefined => {
377+
let googleFamily = "";
378+
for (const _resolvedFont of font) {
379+
const resolvedFont =
380+
_resolvedFont as (BrandFont | BrandFontGoogle | BrandFontBunny);
381+
// Typescript's type checker doesn't understand that it's ok to attempt
382+
// to access a property that might not exist on a type when you're
383+
// only testing for its existence.
384+
385+
// deno-lint-ignore no-explicit-any
386+
const source = (resolvedFont as any).source;
387+
if (source && source !== "bunny") {
388+
return undefined;
389+
}
390+
const thisFamily = resolvedFont.family;
391+
if (!thisFamily) {
392+
continue;
393+
}
394+
if (googleFamily === "") {
395+
googleFamily = thisFamily;
396+
} else if (googleFamily !== thisFamily) {
397+
throw new Error(
398+
`Inconsistent Google font families found: ${googleFamily} and ${thisFamily}`,
399+
);
400+
}
401+
typographyImports.add(bunnyFontImportString(resolvedFont));
402+
}
403+
if (googleFamily === "") {
404+
return undefined;
405+
}
406+
return googleFamily;
407+
};
408+
374409
type HTMLFontInformation = { [key: string]: unknown };
375410

376411
type FontKind =
@@ -392,7 +427,7 @@ const brandTypographyBundle = (
392427
const font = getFontFamilies(family);
393428
const result: HTMLFontInformation = {};
394429
result.family = resolveGoogleFontFamily(font) ??
395-
// resolveBunnyFontFamily(font) ??
430+
resolveBunnyFontFamily(font) ??
396431
// resolveFilesFontFamily(font) ??
397432
family;
398433
for (

src/resources/editor/tools/vs-code.mjs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12301,6 +12301,9 @@ var require_yaml_intelligence_resources = __commonJS({
1230112301
},
1230212302
{
1230312303
ref: "brand-font-system"
12304+
},
12305+
{
12306+
ref: "brand-font-common"
1230412307
}
1230512308
]
1230612309
},
@@ -21778,6 +21781,7 @@ var require_yaml_intelligence_resources = __commonJS({
2177821781
"A link or path to the brand\u2019s dark-colored logo or icon.",
2177921782
"Alternative text for the logo, used for accessibility.",
2178021783
"Provide definitions and defaults for brand\u2019s logo in various formats\nand sizes.",
21784+
"A dictionary of named logo resources.",
2178121785
"A link or path to the brand\u2019s small-sized logo or icon, or a link or\npath to both the light and dark versions.",
2178221786
"A link or path to the brand\u2019s medium-sized logo, or a link or path to\nboth the light and dark versions.",
2178321787
"A link or path to the brand\u2019s large- or full-sized logo, or a link or\npath to both the light and dark versions.",
@@ -21801,13 +21805,13 @@ var require_yaml_intelligence_resources = __commonJS({
2180121805
"Typography definitions for the brand.",
2180221806
"Font files and definitions for the brand.",
2180321807
"The base font settings for the brand. These are used as the default\nfor all text.",
21804-
"Settings for headings",
21805-
"Settings for monospace text",
21806-
"Settings for inline code",
21807-
"Settings for code blocks",
21808-
"Settings for links",
21809-
"Typographic options.",
21810-
"Typographic options without a font size.",
21808+
"Settings for headings, or a string specifying the font family\nonly.",
21809+
"Settings for monospace text, or a string specifying the font family\nonly.",
21810+
"Settings for inline code, or a string specifying the font family\nonly.",
21811+
"Settings for code blocks, or a string specifying the font family\nonly.",
21812+
"Settings for links.",
21813+
"Base typographic options.",
21814+
"Typographic options for headings.",
2181121815
"Typographic options for monospace elements.",
2181221816
"Typographic options for inline monospace elements.",
2181321817
"Line height",

src/resources/editor/tools/yaml/web-worker.js

Lines changed: 11 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/resources/editor/tools/yaml/yaml-intelligence-resources.json

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5273,6 +5273,9 @@
52735273
},
52745274
{
52755275
"ref": "brand-font-system"
5276+
},
5277+
{
5278+
"ref": "brand-font-common"
52765279
}
52775280
]
52785281
},
@@ -14750,6 +14753,7 @@
1475014753
"A link or path to the brand’s dark-colored logo or icon.",
1475114754
"Alternative text for the logo, used for accessibility.",
1475214755
"Provide definitions and defaults for brand’s logo in various formats\nand sizes.",
14756+
"A dictionary of named logo resources.",
1475314757
"A link or path to the brand’s small-sized logo or icon, or a link or\npath to both the light and dark versions.",
1475414758
"A link or path to the brand’s medium-sized logo, or a link or path to\nboth the light and dark versions.",
1475514759
"A link or path to the brand’s large- or full-sized logo, or a link or\npath to both the light and dark versions.",
@@ -14773,13 +14777,13 @@
1477314777
"Typography definitions for the brand.",
1477414778
"Font files and definitions for the brand.",
1477514779
"The base font settings for the brand. These are used as the default\nfor all text.",
14776-
"Settings for headings",
14777-
"Settings for monospace text",
14778-
"Settings for inline code",
14779-
"Settings for code blocks",
14780-
"Settings for links",
14781-
"Typographic options.",
14782-
"Typographic options without a font size.",
14780+
"Settings for headings, or a string specifying the font family\nonly.",
14781+
"Settings for monospace text, or a string specifying the font family\nonly.",
14782+
"Settings for inline code, or a string specifying the font family\nonly.",
14783+
"Settings for code blocks, or a string specifying the font family\nonly.",
14784+
"Settings for links.",
14785+
"Base typographic options.",
14786+
"Typographic options for headings.",
1478314787
"Typographic options for monospace elements.",
1478414788
"Typographic options for inline monospace elements.",
1478514789
"Line height",

src/resources/schema/definitions.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2688,7 +2688,7 @@
26882688
description: Settings for headings, or a string specifying the font family only.
26892689
ref: brand-typography-options-headings
26902690
monospace:
2691-
description: Settings for monospace text, or a string specifying the font family only.
2691+
description: Settings for monospace text, or a string specifying the font family only.
26922692
ref: brand-typography-options-monospace
26932693
monospace-inline:
26942694
description: Settings for inline code, or a string specifying the font family only.
@@ -2697,7 +2697,7 @@
26972697
description: Settings for code blocks, or a string specifying the font family only.
26982698
ref: brand-typography-options-monospace-block
26992699
link:
2700-
description: Settings for links.
2700+
description: Settings for links.
27012701
ref: brand-typography-options-link
27022702

27032703
- id: brand-typography-options-base
@@ -2811,7 +2811,12 @@
28112811
- ref: brand-font-bunny
28122812
- ref: brand-font-file
28132813
- ref: brand-font-system
2814-
2814+
# a font definition missing source information,
2815+
# from which we will assume a default source
2816+
#
2817+
# in Quarto, the default source for typst is `google`
2818+
# and the default source for html formats is `bunny`
2819+
- ref: brand-font-common
28152820
- id: brand-font-weight
28162821
description: A font weight.
28172822
enum:

src/resources/schema/json-schemas.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3593,6 +3593,9 @@
35933593
},
35943594
{
35953595
"$ref": "#/$defs/BrandFontSystem"
3596+
},
3597+
{
3598+
"$ref": "#/$defs/BrandFontCommon"
35963599
}
35973600
]
35983601
},

src/resources/types/schema-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,8 @@ export type BrandFont =
14051405
| BrandFontGoogle
14061406
| BrandFontBunny
14071407
| BrandFontFile
1408-
| BrandFontSystem; /* Font files and definitions for the brand. */
1408+
| BrandFontSystem
1409+
| BrandFontCommon; /* Font files and definitions for the brand. */
14091410

14101411
export type BrandFontWeight =
14111412
| 100

0 commit comments

Comments
 (0)