Skip to content

Commit

Permalink
Merge pull request #11275 from quarto-dev/feature/brand-default-font-…
Browse files Browse the repository at this point in the history
…source

Feature/brand default font sources
  • Loading branch information
cscheid authored Nov 1, 2024
2 parents 4ef4ab8 + f6053cb commit 6e5b32f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 33 deletions.
22 changes: 17 additions & 5 deletions src/command/render/pandoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ import {
} from "../../core/markdown-pipeline.ts";
import { getEnv } from "../../../package/src/util/utils.ts";
import { canonicalizeTitlePostprocessor } from "../../format/html/format-html-title.ts";
import {
BrandFontBunny,
BrandFontFile,
BrandFontGoogle,
} from "../../resources/types/schema-types.ts";

// in case we are running multiple pandoc processes
// we need to make sure we capture all of the trace files
Expand Down Expand Up @@ -1359,18 +1364,25 @@ async function resolveExtras(
const ttf_urls = [], woff_urls: Array<string> = [];
if (brand?.data.typography) {
const fonts = brand.data.typography.fonts || [];
for (const font of fonts) {
if (font.source === "file") {
for (const _font of fonts) {
// if font lacks a source, we assume google in typst output

// deno-lint-ignore no-explicit-any
const source: string = (_font as any).source ?? "google";
if (source === "file") {
const font = _font as BrandFontFile;
for (const file of font.files || []) {
const path = typeof file === "object" ? file.path : file;
fontdirs.add(dirname(join(brand.brandDir, path)));
}
} else if (font.source === "bunny") {
} else if (source === "bunny") {
const font = _font as BrandFontBunny;
console.log(
"Font bunny is not yet supported for Typst, skipping",
font.family,
);
} else if (font.source === "google" /* || font.source === "bunny" */) {
} else if (source === "google" /* || font.source === "bunny" */) {
const font = _font as BrandFontGoogle;
let { family, style, weight } = font;
const parts = [family!];
if (style) {
Expand All @@ -1382,7 +1394,7 @@ async function resolveExtras(
parts.push(weight.join(","));
}
const response = await fetch(
`${base_urls[font.source]}?family=${parts.join(":")}`,
`${base_urls[source]}?family=${parts.join(":")}`,
);
const lines = (await response.text()).split("\n");
for (const line of lines) {
Expand Down
41 changes: 38 additions & 3 deletions src/core/sass/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { ProjectContext } from "../../project/types.ts";
import {
BrandFont,
BrandFontBunny,
BrandFontCommon,
BrandFontGoogle,
BrandFontWeight,
} from "../../resources/types/schema-types.ts";
Expand Down Expand Up @@ -110,7 +111,7 @@ const fontFileFormat = (file: string): string => {
}
};

const bunnyFontImportString = (description: BrandFontBunny) => {
const bunnyFontImportString = (description: BrandFontCommon) => {
const bunnyName = (name: string) => name.replace(/ /g, "-");
const bunnyFamily = description.family;
if (!bunnyFamily) {
Expand All @@ -137,7 +138,6 @@ const bunnyFontImportString = (description: BrandFontBunny) => {
return `@import url('https://fonts.bunny.net/css?family=${
bunnyName(bunnyFamily)
}:${weights}&display=${display}');`;
// }
};

const googleFontImportString = (description: BrandFontGoogle) => {
Expand Down Expand Up @@ -371,6 +371,41 @@ const brandTypographyBundle = (
return googleFamily;
};

const resolveBunnyFontFamily = (
font: BrandFont[],
): string | undefined => {
let googleFamily = "";
for (const _resolvedFont of font) {
const resolvedFont =
_resolvedFont as (BrandFont | BrandFontGoogle | BrandFontBunny);
// Typescript's type checker doesn't understand that it's ok to attempt
// to access a property that might not exist on a type when you're
// only testing for its existence.

// deno-lint-ignore no-explicit-any
const source = (resolvedFont as any).source;
if (source && source !== "bunny") {
return undefined;
}
const thisFamily = resolvedFont.family;
if (!thisFamily) {
continue;
}
if (googleFamily === "") {
googleFamily = thisFamily;
} else if (googleFamily !== thisFamily) {
throw new Error(
`Inconsistent Google font families found: ${googleFamily} and ${thisFamily}`,
);
}
typographyImports.add(bunnyFontImportString(resolvedFont));
}
if (googleFamily === "") {
return undefined;
}
return googleFamily;
};

type HTMLFontInformation = { [key: string]: unknown };

type FontKind =
Expand All @@ -392,7 +427,7 @@ const brandTypographyBundle = (
const font = getFontFamilies(family);
const result: HTMLFontInformation = {};
result.family = resolveGoogleFontFamily(font) ??
// resolveBunnyFontFamily(font) ??
resolveBunnyFontFamily(font) ??
// resolveFilesFontFamily(font) ??
family;
for (
Expand Down
18 changes: 11 additions & 7 deletions src/resources/editor/tools/vs-code.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12301,6 +12301,9 @@ var require_yaml_intelligence_resources = __commonJS({
},
{
ref: "brand-font-system"
},
{
ref: "brand-font-common"
}
]
},
Expand Down Expand Up @@ -21778,6 +21781,7 @@ var require_yaml_intelligence_resources = __commonJS({
"A link or path to the brand\u2019s dark-colored logo or icon.",
"Alternative text for the logo, used for accessibility.",
"Provide definitions and defaults for brand\u2019s logo in various formats\nand sizes.",
"A dictionary of named logo resources.",
"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.",
"A link or path to the brand\u2019s medium-sized logo, or a link or path to\nboth the light and dark versions.",
"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.",
Expand All @@ -21801,13 +21805,13 @@ var require_yaml_intelligence_resources = __commonJS({
"Typography definitions for the brand.",
"Font files and definitions for the brand.",
"The base font settings for the brand. These are used as the default\nfor all text.",
"Settings for headings",
"Settings for monospace text",
"Settings for inline code",
"Settings for code blocks",
"Settings for links",
"Typographic options.",
"Typographic options without a font size.",
"Settings for headings, or a string specifying the font family\nonly.",
"Settings for monospace text, or a string specifying the font family\nonly.",
"Settings for inline code, or a string specifying the font family\nonly.",
"Settings for code blocks, or a string specifying the font family\nonly.",
"Settings for links.",
"Base typographic options.",
"Typographic options for headings.",
"Typographic options for monospace elements.",
"Typographic options for inline monospace elements.",
"Line height",
Expand Down
18 changes: 11 additions & 7 deletions src/resources/editor/tools/yaml/web-worker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 11 additions & 7 deletions src/resources/editor/tools/yaml/yaml-intelligence-resources.json
Original file line number Diff line number Diff line change
Expand Up @@ -5273,6 +5273,9 @@
},
{
"ref": "brand-font-system"
},
{
"ref": "brand-font-common"
}
]
},
Expand Down Expand Up @@ -14750,6 +14753,7 @@
"A link or path to the brand’s dark-colored logo or icon.",
"Alternative text for the logo, used for accessibility.",
"Provide definitions and defaults for brand’s logo in various formats\nand sizes.",
"A dictionary of named logo resources.",
"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.",
"A link or path to the brand’s medium-sized logo, or a link or path to\nboth the light and dark versions.",
"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.",
Expand All @@ -14773,13 +14777,13 @@
"Typography definitions for the brand.",
"Font files and definitions for the brand.",
"The base font settings for the brand. These are used as the default\nfor all text.",
"Settings for headings",
"Settings for monospace text",
"Settings for inline code",
"Settings for code blocks",
"Settings for links",
"Typographic options.",
"Typographic options without a font size.",
"Settings for headings, or a string specifying the font family\nonly.",
"Settings for monospace text, or a string specifying the font family\nonly.",
"Settings for inline code, or a string specifying the font family\nonly.",
"Settings for code blocks, or a string specifying the font family\nonly.",
"Settings for links.",
"Base typographic options.",
"Typographic options for headings.",
"Typographic options for monospace elements.",
"Typographic options for inline monospace elements.",
"Line height",
Expand Down
11 changes: 8 additions & 3 deletions src/resources/schema/definitions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2688,7 +2688,7 @@
description: Settings for headings, or a string specifying the font family only.
ref: brand-typography-options-headings
monospace:
description: Settings for monospace text, or a string specifying the font family only.
description: Settings for monospace text, or a string specifying the font family only.
ref: brand-typography-options-monospace
monospace-inline:
description: Settings for inline code, or a string specifying the font family only.
Expand All @@ -2697,7 +2697,7 @@
description: Settings for code blocks, or a string specifying the font family only.
ref: brand-typography-options-monospace-block
link:
description: Settings for links.
description: Settings for links.
ref: brand-typography-options-link

- id: brand-typography-options-base
Expand Down Expand Up @@ -2811,7 +2811,12 @@
- ref: brand-font-bunny
- ref: brand-font-file
- ref: brand-font-system

# a font definition missing source information,
# from which we will assume a default source
#
# in Quarto, the default source for typst is `google`
# and the default source for html formats is `bunny`
- ref: brand-font-common
- id: brand-font-weight
description: A font weight.
enum:
Expand Down
3 changes: 3 additions & 0 deletions src/resources/schema/json-schemas.json
Original file line number Diff line number Diff line change
Expand Up @@ -3593,6 +3593,9 @@
},
{
"$ref": "#/$defs/BrandFontSystem"
},
{
"$ref": "#/$defs/BrandFontCommon"
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/resources/types/schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,8 @@ export type BrandFont =
| BrandFontGoogle
| BrandFontBunny
| BrandFontFile
| BrandFontSystem; /* Font files and definitions for the brand. */
| BrandFontSystem
| BrandFontCommon; /* Font files and definitions for the brand. */

export type BrandFontWeight =
| 100
Expand Down

0 comments on commit 6e5b32f

Please sign in to comment.