feat(studio): letters filled with a gradient or a pattern

This commit is contained in:
Maurice
2026-10-24 09:25:28 +02:00
parent cd1e7d8fa8
commit 8893e3afe0
14 changed files with 255 additions and 60 deletions
+24 -17
View File
@@ -68,32 +68,39 @@ type ObjectFactory = typeof z.object | typeof z.strictObject;
function paintSchemas(obj: ObjectFactory, pattern: z.ZodType<BookPattern, string>) {
const stop = obj({ at: z.number().min(0).max(1), color: hex, alpha: alpha.optional() });
const stops = z.array(stop).min(2).max(MAX_PAINT_STOPS);
const linear = obj({ type: z.literal('linear'), angle, stops });
const radial = obj({
type: z.literal('radial'),
cx: z.number().min(0).max(1),
cy: z.number().min(0).max(1),
r: z.number().min(RADIAL_RADIUS.min).max(RADIAL_RADIUS.max),
stops,
});
const patterned = obj({
type: z.literal('pattern'),
pattern,
fg: hex,
bg: hex.nullable(),
scale: z.number().min(PATTERN_SCALE.min).max(PATTERN_SCALE.max),
angle,
});
const paint = z.discriminatedUnion('type', [
obj({ type: z.literal('solid'), color: hex, alpha: alpha.optional() }),
obj({ type: z.literal('linear'), angle, stops }),
obj({
type: z.literal('radial'),
cx: z.number().min(0).max(1),
cy: z.number().min(0).max(1),
r: z.number().min(RADIAL_RADIUS.min).max(RADIAL_RADIUS.max),
stops,
}),
obj({
type: z.literal('pattern'),
pattern,
fg: hex,
bg: hex.nullable(),
scale: z.number().min(PATTERN_SCALE.min).max(PATTERN_SCALE.max),
angle,
}),
linear,
radial,
patterned,
]);
return { stop, paint };
return { stop, paint, linear, radial, patterned };
}
const docSchemas = paintSchemas(z.object, z.string().pipe(z.enum(BOOK_PATTERNS)));
export const bookPaintStopSchema = docSchemas.stop;
export const bookPaintSchema = docSchemas.paint;
/** The paints that are more than one colour, as the letters of a text take them too (book-text-effects.schema.ts). */
export const bookLinearPaintSchema = docSchemas.linear;
export const bookRadialPaintSchema = docSchemas.radial;
export const bookPatternPaintSchema = docSchemas.patterned;
export type BookPaint = z.infer<typeof bookPaintSchema>;
export type BookPaintStop = z.infer<typeof bookPaintStopSchema>;
export type BookGradientPaint = Extract<BookPaint, { type: 'linear' | 'radial' }>;
+3 -1
View File
@@ -43,8 +43,10 @@ import type { BookDocument } from './book.schema';
* - 9: text effects and fitting (`transform`, `vAlign`, `outline`, `shadow`, `highlight`,
* `curve`, `fillPaint`, `fit`, `fitMax`, book-text-effects.schema.ts), text up to 400 pt and
* tracking up to 1.5 em.
* - 10: letters filled with a gradient or a pattern (`fillPaint` of type `linear`, `radial`,
* `pattern`, book-text-effects.schema.ts).
*/
export const BOOK_SCHEMA_REV = 9;
export const BOOK_SCHEMA_REV = 10;
/** The `code` a refused save carries in its body when the stored book is newer than the writer. */
export const BOOK_SCHEMA_NEWER_CODE = 'book_schema_newer';
+9
View File
@@ -10,10 +10,19 @@ const n = (v: number) => `${Math.round(v * 10) / 10}`;
const CASES: Record<string, string> = { upper: 'capitals', lower: 'lower case', smallcaps: 'small caps' };
function stopWords(stops: readonly { color: string }[]): string {
return `from ${stops[0]?.color ?? ''} to ${stops[stops.length - 1]?.color ?? ''}`;
}
function fillWords(fill: NonNullable<BookTextEffects['fillPaint']>): string {
if (fill.type === 'photo') {
return `letters filled with photo ${fill.photoId} (focal ${fill.focalX.toFixed(2)}/${fill.focalY.toFixed(2)}, zoom ${n(fill.zoom)})`;
}
if (fill.type === 'linear') return `letters in a linear gradient at ${n(fill.angle)} deg, ${stopWords(fill.stops)}`;
if (fill.type === 'radial') return `letters in a radial gradient, ${stopWords(fill.stops)}`;
if (fill.type === 'pattern') {
return `letters patterned ${fill.pattern} in ${fill.fg} on ${fill.bg ?? 'clear'} at scale ${n(fill.scale)}`;
}
return fill.alpha === 0 ? 'hollow letters (outline only)' : `fill ${fill.color} at alpha ${n(fill.alpha)}`;
}
@@ -1,4 +1,6 @@
import { bookTextIsDrawn } from './book-text-effects.schema';
import { bookTextEffectWords } from './book-text-describe';
import { bookTextIsDrawn, bookTextPaintFill } from './book-text-effects.schema';
import { bookTextHasOwnGround } from './book-text-findings';
import { bookTextElementSchema } from './book.schema';
import { describe, expect, it } from 'vitest';
@@ -75,3 +77,36 @@ describe('the effect fields of a text', () => {
expect(bookTextIsDrawn(parse({ fillPaint: { type: 'photo', photoId: 3 } }))).toBe(true);
});
});
describe('letters filled with a gradient or a pattern', () => {
const gradient = {
type: 'linear',
angle: 90,
stops: [
{ at: 0, color: '#ff6600' },
{ at: 1, color: '#ffcc00' },
],
};
const gingham = { type: 'pattern', pattern: 'gingham', fg: '#d62828', bg: '#ffffff', scale: 1, angle: 0 };
it('BKFX-005: reads a gradient and a pattern in the shape a shape takes them, and draws them as SVG', () => {
const lin = parse({ fillPaint: gradient });
const pat = parse({ fillPaint: gingham });
expect(bookTextPaintFill(lin)).toEqual(gradient);
expect(bookTextPaintFill(pat)).toEqual(gingham);
expect(bookTextIsDrawn(lin)).toBe(true);
expect(bookTextIsDrawn(pat)).toBe(true);
expect(bookTextPaintFill(parse({ fillPaint: { type: 'photo', photoId: 3 } }))).toBeNull();
expect(() => parse({ fillPaint: { ...gingham, pattern: 'plaid' } })).toThrow();
});
it('BKFX-006: names the paint for a model that cannot see the page, and judges no contrast against it', () => {
expect(bookTextEffectWords({ ...parse({ fillPaint: gradient }), size: 40 })).toContain(
'linear gradient at 90 deg, from #ff6600 to #ffcc00',
);
expect(bookTextEffectWords({ ...parse({ fillPaint: gingham }), size: 40 })).toContain(
'patterned gingham in #d62828 on #ffffff',
);
expect(bookTextHasOwnGround(parse({ fillPaint: gingham }))).toBe(true);
});
});
+29 -9
View File
@@ -1,3 +1,10 @@
import {
bookLinearPaintSchema,
bookPatternPaintSchema,
bookRadialPaintSchema,
type BookPaint,
} from './book-paint.schema';
import { z } from 'zod';
/**
@@ -27,10 +34,11 @@ import { z } from 'zod';
*
* ── The fill and the paint system ──────────────────────────────────────────
*
* `fillPaint` takes a solid colour with its own alpha or a photograph today.
* Its solid variant has the shape the book's paint type gives a solid fill, so
* gradients and patterns join this union as further members, without a
* reader of today's fields changing.
* `fillPaint` takes a solid colour with its own alpha, a photograph, or one
* of the book's paints of more than one colour: a linear or radial gradient or
* a pattern, in exactly the shape a shape or a page takes them, so one picker
* and one renderer serve all three. A gradient or pattern fill is drawn as SVG
* like a photo fill, the paint clipped to the letters.
*/
const hex = z.string().regex(/^#[0-9a-fA-F]{6}$/, 'expected #rrggbb');
@@ -84,7 +92,10 @@ export const bookTextCurveSchema = z.object({
});
export type BookTextCurve = z.infer<typeof bookTextCurveSchema>;
/** What fills the letters in place of `color`: a colour with its own alpha (0 leaves an outline only), or a photograph. */
/**
* What fills the letters in place of `color`: a colour with its own alpha (0
* leaves an outline only), a photograph, or a gradient or pattern.
*/
export const bookTextFillSchema = z.discriminatedUnion('type', [
z.object({ type: z.literal('solid'), color: hex, alpha: alpha.default(1) }),
z.object({
@@ -95,9 +106,18 @@ export const bookTextFillSchema = z.discriminatedUnion('type', [
focalY: z.number().min(0).max(1).default(0.5),
zoom: z.number().min(1).max(4).default(1),
}),
bookLinearPaintSchema,
bookRadialPaintSchema,
bookPatternPaintSchema,
]);
export type BookTextFill = z.infer<typeof bookTextFillSchema>;
/** The letters' gradient or pattern, as the paint a shape would take, or null for any other fill. */
export function bookTextPaintFill(el: { fillPaint?: BookTextFill | null }): BookPaint | null {
const fill = el.fillPaint;
return fill && (fill.type === 'linear' || fill.type === 'radial' || fill.type === 'pattern') ? fill : null;
}
/** `shrink` sets the text at the largest size up to `fitMax` that holds the frame, solved on edit and stored in `size`. */
export const BOOK_TEXT_FITS = ['none', 'shrink'] as const;
@@ -133,10 +153,10 @@ export interface BookTextEffects {
/**
* Whether a text is drawn as SVG rather than as a box of HTML type: on a
* curve, or filled with a photograph. Such a text is set line by line as
* written, never wrapped, which is what word art wants and what the checks
* measure it by.
* curve, or filled with a photograph, a gradient or a pattern. Such a text is
* set line by line as written, never wrapped, which is what word art wants and
* what the checks measure it by.
*/
export function bookTextIsDrawn(el: BookTextEffects): boolean {
return !!el.curve || el.fillPaint?.type === 'photo';
return !!el.curve || el.fillPaint?.type === 'photo' || !!bookTextPaintFill(el);
}
+2 -1
View File
@@ -30,7 +30,8 @@ const MIN_FILL_PX_PER_MM = 200 / 25.4;
*/
export function bookTextHasOwnGround(el: BookTextElement): boolean {
if (el.highlight) return true;
if (el.fillPaint && (el.fillPaint.type === 'photo' || el.fillPaint.alpha < 0.5)) return true;
// A photo, a gradient or a pattern in the letters is no one colour the page could be judged against.
if (el.fillPaint && (el.fillPaint.type !== 'solid' || el.fillPaint.alpha < 0.5)) return true;
return !!el.outline && el.outline.width >= ptToMm(el.size) * 0.04;
}
+3 -1
View File
@@ -23,7 +23,9 @@ export const TEXT_EFFECT_PROPS = {
shadow: f.shadow.describe('Hard, never blurred; dx, dy in mm.'),
highlight: f.highlight.describe('A marker band behind each line; pads in mm.'),
curve: f.curve.describe('One line on an arc (bend > 0 arches up) or round a circle (start: degrees, 0 top).'),
fillPaint: f.fillPaint.describe('Replaces color: solid (alpha 0 is hollow, with an outline) or a gallery photo.'),
fillPaint: f.fillPaint.describe(
'Replaces color: solid (alpha 0 is hollow, with an outline), a gallery photo, or a linear/radial gradient or pattern shaped as on a shape.',
),
fit: f.fit.describe('shrink: the largest size up to size that holds the frame.'),
};