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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ The hero is the first entry of a store's list in `store-assets/manifest.json`, w
- `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.
- 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; 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. 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.

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
82 changes: 68 additions & 14 deletions src/store-hero/text.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,30 @@ function lineText(units) {
/**
* Greedy line breaking: as many units per line as fit in `width`. A unit wider than `width` gets
* a line of its own and overflows it; the caller sees that in the measured widths.
*
* @returns {Array<Array<{ text: string, spaceAfter: boolean }>>} the units on each line
*/
function greedyLines(units, width, measure) {
const lines = []
let current = []
for (const unit of units) {
const candidate = [...current, unit]
if (current.length > 0 && measure(lineText(candidate)) > width) {
lines.push(lineText(current))
lines.push(current)
current = [unit]
} else {
current = candidate
}
}
if (current.length > 0) lines.push(lineText(current))
if (current.length > 0) lines.push(current)
return lines
}

/**
* Breaks one paragraph into balanced lines no wider than `maxWidth`: the fewest lines greedy
* breaking needs, then the narrowest width that still needs no more, so the lines come out even.
*
* @returns {Array<Array<{ text: string, spaceAfter: boolean }>>} the units on each line
*/
function balancedLines(units, maxWidth, measure) {
const lines = greedyLines(units, maxWidth, measure)
Expand All @@ -106,7 +110,7 @@ function balancedLines(units, maxWidth, measure) {
while (low <= high) {
const width = Math.floor((low + high) / 2)
const attempt = greedyLines(units, width, measure)
const overflows = attempt.some((line) => measure(line) > width)
const overflows = attempt.some((line) => measure(lineText(line)) > width)
if (attempt.length <= lines.length && !overflows) {
best = attempt
high = width - 1
Expand All @@ -117,6 +121,26 @@ function balancedLines(units, maxWidth, measure) {
return best
}

/**
* Wraps `text` into balanced lines, keeping each line's units. A `\n` in the text forces a break.
*
* @returns {{ lines: string[], loneWords: number }} the lines, and how many of them hold a single
* word from a paragraph of several (a line such as "every" between two fuller ones)
*/
function wrapParagraphs(text, { locale, maxWidth, measure }) {
const lines = []
let loneWords = 0
for (const paragraph of text.split('\n')) {
const units = breakUnits(paragraph.trim(), locale)
for (const line of balancedLines(units, maxWidth, measure)) {
lines.push(lineText(line))
// A one-word paragraph has no other way to break, so only count words that were split off.
if (line.length === 1 && units.length > 1) loneWords += 1
}
}
return { lines, loneWords }
}

/**
* Wraps `text` into balanced lines. A `\n` in the text forces a break, for a headline whose
* phrasing matters more than balance.
Expand All @@ -125,19 +149,27 @@ function balancedLines(units, maxWidth, measure) {
* @param {{ locale: string, maxWidth: number, measure: (line: string) => number }} options
* @returns {string[]}
*/
export function wrapText(text, { locale, maxWidth, measure }) {
return text
.split('\n')
.flatMap((paragraph) =>
balancedLines(breakUnits(paragraph.trim(), locale), maxWidth, measure),
)
export function wrapText(text, options) {
return wrapParagraphs(text, options).lines
}

/**
* How far below the largest size that fits `fitText` may go to avoid a lone word, as a fraction
* of that size. Small enough that the text still reads as the same size; large enough to pull a
* word that only just overflows back onto its line.
*/
export const LONE_WORD_SHRINK = 0.1

/**
* Finds the largest font size, from `sizes` (largest first), at which `text` wraps into lines
* that each fit `maxWidth`, number at most `maxLines`, and, with `lineHeight` (a multiple of the
* size), stack no taller than `maxHeight`.
*
* When that size leaves a word alone on a line (for example "Every printer, / every / project,
* on / every device."), it tries the sizes up to `LONE_WORD_SHRINK` smaller and takes the largest
* that leaves fewer lone words, so the headline breaks at phrases rather than one word short. If
* none does, the largest fitting size stands.
*
* @param {string} text
* @param {{ locale: string, sizes: number[], maxWidth: number, maxLines: number,
* maxHeight?: number, lineHeight?: number,
Expand All @@ -156,15 +188,37 @@ export function fitText(
measureAt,
},
) {
for (const size of sizes) {
/** The wrap at `size`, or null when it breaks one of the limits. */
function fitAt(size) {
const measure = measureAt(size)
const lines = wrapText(text, { locale, maxWidth, measure })
const wrapped = wrapParagraphs(text, { locale, maxWidth, measure })
const { lines } = wrapped
const fitsLines =
lines.length <= maxLines && lines.length * size * lineHeight <= maxHeight
if (fitsLines && lines.every((line) => measure(line) <= maxWidth))
return { size, lines }
const fitsWidth = lines.every((line) => measure(line) <= maxWidth)
return fitsLines && fitsWidth ? { size, ...wrapped } : null
}

// The largest size that fits at all.
let largestIndex = -1
let best = null
for (const [index, size] of sizes.entries()) {
best = fitAt(size)
if (best) {
largestIndex = index
break
}
}
if (!best) return null

// A lone word: look a little smaller for a size that keeps it with its neighbours.
const floor = best.size * (1 - LONE_WORD_SHRINK)
for (const size of sizes.slice(largestIndex + 1)) {
if (best.loneWords === 0 || size < floor) break
const candidate = fitAt(size)
if (candidate && candidate.loneWords < best.loneWords) best = candidate
}
return null
return { size: best.size, lines: best.lines }
}

/** Font sizes from `largest` down to `smallest` in steps of 2, for `fitText`. */
Expand Down
43 changes: 43 additions & 0 deletions test/store-hero.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,49 @@ test('fitText steps down to a size that fits, and gives up below the smallest',
assert.equal(tooTall, null)
})

test('fitText goes a little smaller to keep a word from standing alone on a line', () => {
const text = 'Every printer, every project, on every device.'
const options = {
locale: 'en-US',
sizes: [60, 58, 56, 54, 52, 50, 48],
maxLines: 5,
measureAt: monospace,
}

// At 60 a line holds 15 characters: "Every printer, / every project, / on every / device."
// At 56 it holds 16, so the last line keeps its words together.
const fitted = fitText(text, { ...options, maxWidth: 450 })
assert.deepEqual(fitted, {
size: 56,
lines: ['Every printer,', 'every project,', 'on every device.'],
})

// At 420 the same break needs 52, more than 10 percent below 60, so the larger size stands.
const bounded = fitText(text, { ...options, maxWidth: 420 })
assert.equal(bounded.size, 60)
assert.deepEqual(bounded.lines, [
'Every printer,',
'every project,',
'on every',
'device.',
])
})

test('fitText keeps the largest size when no word is alone, or a one-word line is forced', () => {
const options = {
locale: 'en-US',
sizes: [60, 58, 56],
maxWidth: 1000,
maxLines: 4,
measureAt: monospace,
}
assert.equal(fitText('Set up in one click.', options).size, 60)
assert.deepEqual(fitText('Ready.\nSet up in one click.', options), {
size: 60,
lines: ['Ready.', 'Set up in one click.'],
})
})

// --- PNG -------------------------------------------------------------------------------------

test('PNG rows round-trip as 8-bit RGB with unfiltered rows, and other PNGs are refused', () => {
Expand Down