Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ The hero is the first entry of a store's list in `store-assets/manifest.json`, w
```

- `file` is under `store-assets/<store>/`, like every other entry, and must contain `{locale}`: each locale gets its own image.
- `source` is the raw capture under `store-assets/`, with the capture helper's `<name>.json` sidecar beside it. The hero shows the whole window the sidecar records, never part of it. `hero.crop` (`[x, y, width, height]` in capture pixels) may widen that region, and is refused if it would cut the window. The one exception is a tall flyout in the `split` layout: with `"fade": "bottom"` beside it, `hero.crop` may cut the window's bottom edge (its top and sides stay whole), and the hero fades the UI out toward the cut so it clearly continues. Every other cut is still refused, and `fade` without a crop that really cuts the bottom fails. A capture taken at 200% display scale (sidecar `"scale": 200`) is drawn at its logical size and looks sharper; a plain run says when a 100% capture is enlarged enough to look soft. `split` needs a capture; for `brand-card` it is optional.
- `source` is the raw capture under `store-assets/`, with the capture helper's `<name>.json` sidecar beside it. It may contain `{locale}` (`raw/microsoft/{locale}/00-flyout-dark.png`) so each locale's hero shows Windows and the app in that language. Every listing locale with its own record then needs its own capture: `--check` fails and names each one that is missing, as winui-app-kit's screenshot check does. While captures are being made, a plain run draws a missing locale from the en-US capture as a preview and warns. A capture without its sidecar is an error. The hero shows the whole window the sidecar records, never part of it. `hero.crop` (`[x, y, width, height]` in logical pixels, which are capture pixels at 100% display scale, so one crop serves every locale and scale) may widen that region, and is refused if it would cut the window. The one exception is a tall flyout in the `split` layout: with `"fade": "bottom"` beside it, `hero.crop` may cut the window's bottom edge (its top and sides stay whole), and the hero fades the UI out toward the cut so it clearly continues. Every other cut is still refused, and `fade` without a crop that really cuts the bottom fails. A capture taken at 200% display scale (sidecar `"scale": 200`) is drawn at its logical size, so the layout is the same as from a 100% capture, and looks sharper; locales may mix the two; a plain run says when a 100% capture is enlarged enough to look soft. `split` needs a capture; for `brand-card` it is optional.
- `hero.icon` is repo-relative, a `.png` or `.svg`. `primary` and `accent` are `#RRGGBB`: the panel or gradient is drawn from `primary`, the accent bar runs from `primary` to `accent`.
- `hero.textFreeFile` (split layout only) also draws one text-free image: the panel's brand gradient across the whole canvas with the same window, whole and enlarged, centred on it, and no icon, name, headline or subline. It is for a page, such as a product page on a website, that sets the name and headline in its own text above the image. Without the panel the window has the whole canvas, so a wide window is drawn larger than in the hero. The path is under `store-assets/<store>/` like `file`, has no `{locale}` (the image has no text, so one serves every locale), and is best kept out of `screenshots/` so it is never uploaded as a listing screenshot, for example `"textFreeFile": "text-free/00-hero-dark.png"`. It is the manifest's `size`, and `--check` covers it like the heroes.
- `hero.textFreeFile` (split layout only) also draws one text-free image: the panel's brand gradient across the whole canvas with the same window, whole and enlarged, centred on it, and no icon, name, headline or subline. It is for a page, such as a product page on a website, that sets the name and headline in its own text above the image, and is always drawn from the en-US capture. Without the panel the window has the whole canvas, so a wide window is drawn larger than in the hero. The path is under `store-assets/<store>/` like `file`, has no `{locale}` (the image has no text, so one serves every locale), and is best kept out of `screenshots/` so it is never uploaded as a listing screenshot, for example `"textFreeFile": "text-free/00-hero-dark.png"`. It is the manifest's `size`, and `--check` covers it like the heroes.
- The output size is the manifest's `size` (1366 x 768 by default, up to 3840 x 2160, about 16:9), so the hero matches the listing's other screenshots. The PNG is 8-bit RGB without alpha, with unfiltered rows, the format winui-app-kit's screenshot renderer writes.

The text comes from the same store's `listings.json`. Every locale with its own record needs `heroHeadline`, and the command names every locale that lacks one before drawing anything. `heroSubline` is required for `brand-card`, and for `split` is set in every locale or none. A `copyFrom` locale reuses its target's image and needs neither. The app name is the locale's `title`. A `\n` in either field forces a line break; otherwise lines are broken at word boundaries (Japanese and Chinese words too) and balanced. The headline starts large and shrinks in steps until it fits above the bottom third of the image, which Microsoft reserves for overlays. When the largest size that fits leaves a word alone on a line ("Every printer, / every / project, on / every device."), the headline and subline go up to 10 percent smaller if that keeps the word with its neighbours, and otherwise keep the larger size. If it still does not fit at the smallest size that reads on a 320 x 180 search card, the command fails and names the locale, so shorten it there.
The text comes from the same store's `listings.json`. Every locale with its own record needs `heroHeadline`, and the command names every locale that lacks one before drawing anything. `heroSubline` is required for `brand-card`, and for `split` is set in every locale or none. A `copyFrom` locale reuses its target's image and needs neither. The app name is the locale's `title`. A `\n` in either field forces a line break; otherwise lines are broken at word boundaries (Japanese and Chinese words too) and balanced. When balancing leaves a word alone on a line ("Your / reminders, one / click from the / Windows tray."), the same text is broken again into as many lines wherever keeps fewer words alone ("Your reminders, / one click / from the / Windows tray."), but never by running one sentence into the next on a line. The headline starts large and shrinks in steps until it fits above the bottom third of the image, which Microsoft reserves for overlays. When the largest size that fits still leaves a word alone ("Every printer, / every / project, on / every device."), the headline and subline go up to 15 percent smaller if that keeps the word with its neighbours, or leaves as many alone in fewer lines, and otherwise keep the larger size. If it still does not fit at the smallest size that reads on a 320 x 180 search card, the command fails and names the locale, so shorten it there.

Text is drawn in Noto Sans, and in Noto Sans CJK JP or SC for Japanese and Simplified Chinese (whose shared characters differ by region), because Segoe UI ships only with Windows and is absent from Linux, CI and containers. Other Chinese scripts and Korean fail until a font is pinned for them. The fonts (about 27 MB, SIL Open Font License) are downloaded from the upstream Noto repositories on first use, checked against pinned SHA-256 hashes, and kept in `~/.cache/devkit/fonts` (`%LOCALAPPDATA%\devkit\fonts` on Windows). Set `DEVKIT_FONTS_DIR` to use another folder, such as a CI cache, and run `devkit-store-hero --fetch-fonts` to fill it without drawing anything. System fonts are never used, so a machine's installed fonts cannot change a hero.

Expand Down
27 changes: 25 additions & 2 deletions bin/devkit-store-hero.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
*
* `--check` writes nothing and fails when a hero is missing, differs from what its inputs draw,
* or was saved by another tool, for the repo's `validate` gate. Both modes name hero files left
* from a locale the listing no longer has; `--check` fails on them. `--assets` names the
* from a locale the listing no longer has; `--check` fails on them. Both also name each locale
* whose own capture is missing: a plain run draws it from the en-US capture as a preview, and
* `--check` fails, as winui-app-kit's screenshot check does. `--assets` names the
* store-assets folder when it is not `store-assets/` at the repo root. `--fetch-fonts` only
* downloads the pinned fonts, for a CI cache step.
*
* Like the other `devkit-*` commands it needs no bootstrapped machine. It needs `@resvg/resvg-js`
* (as `devkit-icons` does) and downloads its fonts once (see "Store heroes" in the README).
*/
import {
captureFallbacks,
checkHeroes,
orphanedHeroes,
writeHeroes,
Expand Down Expand Up @@ -46,6 +49,24 @@ function reportOrphans(options) {
return true
}

/**
* Names on stderr each locale whose own capture is missing; returns true when there were any.
* `check` says whether this fails the run (`--check`) or only previews it with the en-US capture.
*/
function reportFallbacks(options, check) {
const fallbacks = captureFallbacks(root, options)
if (fallbacks.length === 0) return false

console.error(
check
? 'devkit-store-hero: these locales have no capture of their own; capture them:'
: 'devkit-store-hero: warning: these locales have no capture of their own, so their heroes are previews drawn from the en-US one, and --check fails until they are captured:',
)
for (const { store, locale, source } of fallbacks)
console.error(` ${store} ${locale}: ${source} is missing`)
return true
}

try {
const known = new Set(['--check', '--assets', '--fetch-fonts'])
const unknown = args.find(
Expand All @@ -69,14 +90,16 @@ try {
console.error('Run `devkit-store-hero` and commit the result.')
}
const hasOrphans = reportOrphans(options)
const hasFallbacks = reportFallbacks(options, true)

if (stale.length > 0 || hasOrphans) process.exitCode = 1
if (stale.length > 0 || hasOrphans || hasFallbacks) process.exitCode = 1
else console.log('devkit-store-hero: every hero matches its inputs')
} else {
const { written, advice } = await writeHeroes(root, options)
console.log(`devkit-store-hero: wrote ${written.length} files`)
for (const line of advice) console.error(`devkit-store-hero: note: ${line}`)
reportOrphans(options)
reportFallbacks(options, false)
}
} catch (error) {
console.error(error.message)
Expand Down
136 changes: 121 additions & 15 deletions src/store-hero/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const DEFAULT_ASSETS_DIR = 'store-assets'
export const LAYOUTS = ['split', 'brand-card']
const DEFAULT_SIZE = '1366x768'
const LOCALE_TOKEN = '{locale}'
// The locale whose capture stands in for a missing one, and the text-free image's capture.
export const FALLBACK_LOCALE = 'en-US'
const COLOUR = /^#[0-9A-Fa-f]{6}$/

// Microsoft takes desktop screenshots from 1366 x 768 to 3840 x 2160. The layouts are drawn for
Expand Down Expand Up @@ -65,11 +67,16 @@ export function parseSize(value = DEFAULT_SIZE) {
* exception is `fade: "bottom"`: the crop may then cut the window's bottom edge, and the layout
* fades the UI out there so it reads as continuing, not as ending.
*
* The sidecar's rectangles are in the capture's own (physical) pixels. `crop` is in logical
* pixels, which are the same at 100% display scale, so one crop serves a 100% and a 200% capture
* of the same window, and a locale captured at another scale than the rest.
*
* @param {string} assets the store-assets folder
* @param {string} source the capture, relative to `assets`
* @param {number[] | undefined} crop `[x, y, width, height]` in capture pixels
* @param {number[] | undefined} crop `[x, y, width, height]` in logical pixels
* @param {string} label names the entry in errors
* @param {'bottom' | undefined} fade where a crop may cut the window
* @returns {object} the capture: its file, pixel size, `region` in capture pixels and `scale`
*/
export function resolveCapture(assets, source, crop, label, fade) {
const file = path.join(assets, source)
Expand All @@ -91,18 +98,25 @@ export function resolveCapture(assets, source, crop, label, fade) {
fail(`${path.basename(sidecarFile)} has no window rectangle`)
const windowRect = [window.x, window.y, window.width, window.height]

const scale = sidecar.scale ?? 100
if (!Number.isFinite(scale) || scale <= 0)
fail(`${path.basename(sidecarFile)} has an unreadable scale`)

const region =
crop === undefined ? windowRect : checkCrop(crop, windowRect, label, fade)
crop === undefined
? windowRect
: cropToCapture(
checkCrop(crop, toLogical(windowRect, scale), label, fade),
scale,
`${label} (${source})`,
)
const [x, y, w, h] = region
if (x < 0 || y < 0 || x + w > width || y + h > height) {
fail(
`${label}: the region ${region.join(', ')} is not inside the ${width} x ${height} capture`,
`${label}: the region ${region.join(', ')} is not inside the ${width} x ${height} capture (${source})`,
)
}

const scale = sidecar.scale ?? 100
if (!Number.isFinite(scale) || scale <= 0)
fail(`${path.basename(sidecarFile)} has an unreadable scale`)
return {
file,
width,
Expand All @@ -114,15 +128,36 @@ export function resolveCapture(assets, source, crop, label, fade) {
}
}

/** A rectangle in capture pixels, in logical pixels at the capture's display `scale` percent. */
function toLogical(rect, scale) {
return rect.map((value) => (value * 100) / scale)
}

/**
* A logical crop in the capture's own pixels. A scale that does not land the crop on whole
* capture pixels (150% with an odd coordinate) is refused rather than rounded, so the region
* never shifts by a pixel between locales.
*/
function cropToCapture(crop, scale, label) {
const region = crop.map((value) => (value * scale) / 100)
if (!region.every(Number.isInteger)) {
fail(
`${label}: hero.crop ${crop.join(', ')} does not fall on whole pixels at ${scale}% display scale; capture at 100% or 200%`,
)
}
return region
}

/**
* Checks an explicit `[x, y, width, height]` crop: it keeps the whole window, or, with
* `fade: "bottom"`, the whole window except part of its bottom, which it must then really cut.
* Both rectangles are in logical pixels.
*/
function checkCrop(crop, windowRect, label, fade) {
const valid =
Array.isArray(crop) && crop.length === 4 && crop.every(Number.isInteger)
if (!valid || crop[2] <= 0 || crop[3] <= 0)
fail(`${label}.hero.crop must be [x, y, width, height] in capture pixels`)
fail(`${label}.hero.crop must be [x, y, width, height] in logical pixels`)

const [x, y, w, h] = crop
const [wx, wy, ww, wh] = windowRect
Expand Down Expand Up @@ -191,6 +226,8 @@ function checkHeroBlock(root, entry, label) {
}
if (hero.crop !== undefined && typeof entry.source !== 'string')
fail(`${label}.hero.crop needs a source`)
if (typeof entry.source === 'string' && !entry.source.endsWith('.png'))
fail(`${label}.source must be a .png capture`)

// A faded cut is for a split hero's enlarged UI only, and only with a crop saying where to cut.
if (hero.fade !== undefined) {
Expand Down Expand Up @@ -218,6 +255,66 @@ function checkHeroBlock(root, entry, label) {
}
}

/**
* Each output's capture. A `source` without `{locale}` is one capture for every locale. With
* `{locale}`, each locale draws its own capture (Windows and the app in that language); a locale
* whose capture is missing falls back to the en-US capture, for a preview, and is listed in
* `fallbacks`, which `--check` fails on. A capture that exists
* without its sidecar is still an error. The text-free image has no language and always uses the
* en-US capture, since the website it serves is English.
*
* @param {string} assets the store-assets folder
* @param {object} entry the manifest entry
* @param {string[]} locales the locales that get their own hero
* @param {string} label names the entry in errors
* @returns {{ forLocale: Map<string, object>, textFree: object | null,
* fallbacks: Array<{ locale: string, source: string }> }}
*/
function resolveCaptures(assets, entry, locales, label) {
const { hero, source } = entry
const forLocale = new Map()
const fallbacks = []
if (typeof source !== 'string')
return { forLocale, textFree: null, fallbacks }

// Resolved once per file, so locales that share a capture share its object (and data URI).
const resolved = new Map()
const capture = (file) => {
if (!resolved.has(file))
resolved.set(
file,
resolveCapture(assets, file, hero.crop, label, hero.fade),
)
return resolved.get(file)
}

if (!source.includes(LOCALE_TOKEN)) {
for (const locale of locales) forLocale.set(locale, capture(source))
return { forLocale, textFree: capture(source), fallbacks }
}

const fallbackSource = source.replaceAll(LOCALE_TOKEN, FALLBACK_LOCALE)
for (const locale of locales) {
const own = source.replaceAll(LOCALE_TOKEN, locale)
if (existsSync(path.join(assets, own))) {
forLocale.set(locale, capture(own))
continue
}
if (locale === FALLBACK_LOCALE)
fail(`${label}.source (${own}) is missing; other locales fall back to it`)
if (!existsSync(path.join(assets, fallbackSource))) {
fail(
`${label}.source: ${own} is missing, and so is the ${FALLBACK_LOCALE} capture (${fallbackSource}) it would fall back to`,
)
}
forLocale.set(locale, capture(fallbackSource))
fallbacks.push({ locale, source: own })
}
const textFree =
hero.textFreeFile === undefined ? null : capture(fallbackSource)
return { forLocale, textFree, fallbacks }
}

/**
* The locales that get their own hero and the text each one draws. A `copyFrom` locale reuses its
* target's image and needs no text. Every missing headline is named at once.
Expand Down Expand Up @@ -279,8 +376,10 @@ export function heroLocales(listings, layout, label) {
*
* @param {string} root the repo root; `hero.icon` is relative to it
* @param {string} assetsDir the store-assets folder, relative to `root`
* @returns {Array<object>} jobs with their layout, size, colours, icon, capture and outputs: one
* per locale, then the text-free variant (`textFree: true`, no locale) when one is configured
* @returns {Array<object>} jobs with their layout, size, colours, icon and outputs: one per
* locale, then the text-free variant (`textFree: true`, no locale) when one is configured, each
* with the capture it draws (null without a `source`); `fallbacks` names the locales whose own
* capture is missing and that draw the en-US one instead
*/
export function loadHeroJobs(root, assetsDir = DEFAULT_ASSETS_DIR) {
const assets = path.join(root, assetsDir)
Expand Down Expand Up @@ -320,12 +419,15 @@ export function loadHeroJobs(root, assetsDir = DEFAULT_ASSETS_DIR) {
listingsPath,
)

const capture =
typeof entry.source === 'string'
? resolveCapture(assets, entry.source, hero.crop, label, hero.fade)
: null
const captures = resolveCaptures(
assets,
entry,
locales.map(({ locale }) => locale),
label,
)
const outputs = locales.map((text) => ({
...text,
capture: captures.forLocale.get(text.locale) ?? null,
path: path.join(
assetsDir,
store,
Expand All @@ -339,7 +441,11 @@ export function loadHeroJobs(root, assetsDir = DEFAULT_ASSETS_DIR) {
`${label}.hero.textFreeFile (${hero.textFreeFile}) is also a locale's hero; give it its own name`,
)
}
outputs.push({ textFree: true, path: textFreePath })
outputs.push({
textFree: true,
capture: captures.textFree,
path: textFreePath,
})
}

jobs.push({
Expand All @@ -349,8 +455,8 @@ export function loadHeroJobs(root, assetsDir = DEFAULT_ASSETS_DIR) {
primary: hero.primary.toUpperCase(),
accent: hero.accent.toUpperCase(),
icon: path.join(root, hero.icon),
capture,
outputs,
fallbacks: captures.fallbacks,
pattern: path.join(assetsDir, store, entry.file),
})
}
Expand Down
Loading