diff --git a/client/src/components/Studio/TextEffectsView.test.tsx b/client/src/components/Studio/TextEffectsView.test.tsx
index 4625a5abf..abdbdaa90 100644
--- a/client/src/components/Studio/TextEffectsView.test.tsx
+++ b/client/src/components/Studio/TextEffectsView.test.tsx
@@ -108,6 +108,40 @@ describe('a text drawn as SVG', () => {
expect(Array.from(clip?.querySelectorAll('tspan') ?? []).map((t) => t.textContent)).toEqual(['LIS', 'BON']);
});
+ it('fills the letters with a gradient through a gradient of their own frame', () => {
+ const el = text({
+ fillPaint: {
+ type: 'linear',
+ angle: 90,
+ stops: [
+ { at: 0, color: '#ff6600' },
+ { at: 1, color: '#ffcc00' },
+ ],
+ },
+ });
+ const { container } = render(
);
+ const gradient = container.querySelector('linearGradient');
+ expect(gradient?.getAttribute('gradientUnits')).toBe('userSpaceOnUse');
+ expect(Array.from(gradient?.querySelectorAll('stop') ?? []).map((s) => s.getAttribute('stop-color'))).toEqual([
+ '#ff6600',
+ '#ffcc00',
+ ]);
+ const letters = container.querySelector('svg > text');
+ expect(letters?.getAttribute('fill')).toBe(`url(#${gradient?.getAttribute('id')})`);
+ });
+
+ it('fills the letters with a pattern, its ground and its marks clipped to them', () => {
+ const el = text({
+ fillPaint: { type: 'pattern', pattern: 'stripes', fg: '#d62828', bg: '#ffffff', scale: 1, angle: 0 },
+ });
+ const { container } = render(
);
+ const clip = container.getElementsByTagName('clipPath')[0];
+ const group = container.querySelector('g[clip-path]');
+ expect(group?.getAttribute('clip-path')).toBe(`url(#${clip?.getAttribute('id')})`);
+ expect(group?.querySelector('rect')?.getAttribute('fill')).toBe('#ffffff');
+ expect(group?.querySelectorAll('path[fill="#d62828"]').length).toBeGreaterThan(0);
+ });
+
it('gives every copy of a page its own ids, so a page drawn twice never clips to the other copy', () => {
const el = text({ fillPaint: { type: 'photo', photoId: 12 }, curve: { type: 'circle' } });
const { container } = render(
diff --git a/client/src/components/Studio/TextEffectsView.tsx b/client/src/components/Studio/TextEffectsView.tsx
index 4868853e8..016b403a3 100644
--- a/client/src/components/Studio/TextEffectsView.tsx
+++ b/client/src/components/Studio/TextEffectsView.tsx
@@ -2,10 +2,11 @@ import type { CSSProperties, ReactNode } from 'react';
import { useId } from 'react';
import type { BookTextElement } from '@trek/shared';
-import { bookDrawnLines, bookTextCurvePath, bookTextIsDrawn } from '@trek/shared';
+import { bookDrawnLines, bookTextCurvePath, bookTextIsDrawn, bookTextPaintFill } from '@trek/shared';
import { fontStack } from './bookFonts';
import { photoSrc } from './bookRender';
+import { paintParts } from './paint/paintRender';
import {
caseStyle,
curveAnchor,
@@ -149,11 +150,16 @@ function DrawnText({ el, frameStyle, big }: { el: BookTextElement; frameStyle: C
const reactId = useId();
const pathId = svgId('tp-', reactId);
const clipId = svgId('tc-', reactId);
+ const paintId = svgId('tf-', reactId);
const { w, h } = el.frame;
const fontMm = ptToMm(el.size);
const lines = bookDrawnLines(el);
const curve = el.curve ? bookTextCurvePath(el.curve, w, h, fontMm) : null;
const photo = el.fillPaint?.type === 'photo' ? el.fillPaint : null;
+ // A gradient or pattern in the letters, laid out over the frame as a shape's paint is over its box.
+ const fillPaint = bookTextPaintFill(el);
+ const letterPaint = fillPaint ? paintParts(fillPaint, { x: 0, y: 0, w, h }, paintId) : null;
+ const pattern = letterPaint?.pattern ?? null;
const hollow = isHollow(el);
const { outline, shadow } = el;
@@ -211,8 +217,20 @@ function DrawnText({ el, frameStyle, big }: { el: BookTextElement; frameStyle: C
if (photo) {
const cover = photoCover(w, h, photo);
layers.push(
);
+ } else if (pattern && letterPaint) {
+ // The pattern's ground and its marks, clipped to the letters.
+ layers.push(
+
+ {letterPaint.fill !== 'none' && }
+
+ {pattern.layers.map((layer, i) => (
+
+ ))}
+
+
+ );
} else {
- layers.push(glyphs({ fill: letterFill(el) ?? el.color }, 'fill'));
+ layers.push(glyphs({ fill: letterPaint ? letterPaint.fill : (letterFill(el) ?? el.color) }, 'fill'));
}
return (
@@ -227,7 +245,8 @@ function DrawnText({ el, frameStyle, big }: { el: BookTextElement; frameStyle: C
>
{curve && }
- {photo && (
+ {letterPaint?.defs}
+ {(photo || pattern) && (
{glyphs(null, 'clip')}
diff --git a/client/src/components/Studio/paint/paintRender.tsx b/client/src/components/Studio/paint/paintRender.tsx
index 4aa97b6cb..95b28640c 100644
--- a/client/src/components/Studio/paint/paintRender.tsx
+++ b/client/src/components/Studio/paint/paintRender.tsx
@@ -42,7 +42,7 @@ function Stops({ stops }: { stops: readonly BookPaintStop[] }) {
}
/** What a painted outline needs: the defs to put in the SVG, and how to fill the outline. */
-interface PaintParts {
+export interface PaintParts {
defs: ReactNode;
fill: string;
fillOpacity?: number;
@@ -50,7 +50,12 @@ interface PaintParts {
pattern: { clipId: string; layers: { d: string; opacity: number }[]; transform?: string; ink: string } | null;
}
-function paintParts(paint: BookPaint, box: PatternBox, id: string): PaintParts {
+/**
+ * What drawing a paint over a box takes: the gradient definition and the fill
+ * to point at it, or a pattern's geometry to draw clipped to the outline over
+ * its ground. A shape's outline and the letters of a text both draw from it.
+ */
+export function paintParts(paint: BookPaint, box: PatternBox, id: string): PaintParts {
switch (paint.type) {
case 'solid':
return {
diff --git a/client/src/components/Studio/textEffects.ts b/client/src/components/Studio/textEffects.ts
index 8349d750d..a6cc6e3fe 100644
--- a/client/src/components/Studio/textEffects.ts
+++ b/client/src/components/Studio/textEffects.ts
@@ -28,7 +28,7 @@ export function withAlpha(hex: string, alpha = 1): string {
return `rgba(${(n >> 16) & 255}, ${(n >> 8) & 255}, ${n & 255}, ${Math.max(0, alpha)})`;
}
-/** The colour the letters are filled with, or null when a photograph fills them. */
+/** The colour the letters are filled with, or null when a photograph, a gradient or a pattern fills them. */
export function letterFill(el: BookTextElement): string | null {
const fill = el.fillPaint;
if (!fill) return el.color;
diff --git a/shared/src/book/book-paint.schema.ts b/shared/src/book/book-paint.schema.ts
index 04f48bd8c..601475b09 100644
--- a/shared/src/book/book-paint.schema.ts
+++ b/shared/src/book/book-paint.schema.ts
@@ -68,32 +68,39 @@ type ObjectFactory = typeof z.object | typeof z.strictObject;
function paintSchemas(obj: ObjectFactory, pattern: z.ZodType) {
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;
export type BookPaintStop = z.infer;
export type BookGradientPaint = Extract;
diff --git a/shared/src/book/book-schema-rev.ts b/shared/src/book/book-schema-rev.ts
index afdb4a2cf..1821e9d84 100644
--- a/shared/src/book/book-schema-rev.ts
+++ b/shared/src/book/book-schema-rev.ts
@@ -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';
diff --git a/shared/src/book/book-text-describe.ts b/shared/src/book/book-text-describe.ts
index 8019ae40a..2707a2c34 100644
--- a/shared/src/book/book-text-describe.ts
+++ b/shared/src/book/book-text-describe.ts
@@ -10,10 +10,19 @@ const n = (v: number) => `${Math.round(v * 10) / 10}`;
const CASES: Record = { 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): 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)}`;
}
diff --git a/shared/src/book/book-text-effects.schema.spec.ts b/shared/src/book/book-text-effects.schema.spec.ts
index bd24ed91d..f88a2432d 100644
--- a/shared/src/book/book-text-effects.schema.spec.ts
+++ b/shared/src/book/book-text-effects.schema.spec.ts
@@ -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);
+ });
+});
diff --git a/shared/src/book/book-text-effects.schema.ts b/shared/src/book/book-text-effects.schema.ts
index e98551f94..73a0b5675 100644
--- a/shared/src/book/book-text-effects.schema.ts
+++ b/shared/src/book/book-text-effects.schema.ts
@@ -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;
-/** 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;
+/** 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);
}
diff --git a/shared/src/book/book-text-findings.ts b/shared/src/book/book-text-findings.ts
index edc10d007..b229c9c8c 100644
--- a/shared/src/book/book-text-findings.ts
+++ b/shared/src/book/book-text-findings.ts
@@ -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;
}
diff --git a/shared/src/book/book-text-ops.schema.ts b/shared/src/book/book-text-ops.schema.ts
index a878942d8..949260ed8 100644
--- a/shared/src/book/book-text-ops.schema.ts
+++ b/shared/src/book/book-text-ops.schema.ts
@@ -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.'),
};