react-cheminfo 0.30.0 → 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/README.md +20 -0
  2. package/lib/download/core/downloadFigure.d.ts +7 -5
  3. package/lib/download/core/downloadFigure.d.ts.map +1 -1
  4. package/lib/download/core/downloadFigure.js +7 -6
  5. package/lib/download/core/downloadFigure.js.map +1 -1
  6. package/lib/download/core/figureLayout.d.ts +55 -0
  7. package/lib/download/core/figureLayout.d.ts.map +1 -0
  8. package/lib/download/core/figureLayout.js +89 -0
  9. package/lib/download/core/figureLayout.js.map +1 -0
  10. package/lib/download/core/figureScale.d.ts +9 -0
  11. package/lib/download/core/figureScale.d.ts.map +1 -1
  12. package/lib/download/core/figureScale.js +10 -0
  13. package/lib/download/core/figureScale.js.map +1 -1
  14. package/lib/download/core/figureSettle.d.ts +51 -0
  15. package/lib/download/core/figureSettle.d.ts.map +1 -0
  16. package/lib/download/core/figureSettle.js +81 -0
  17. package/lib/download/core/figureSettle.js.map +1 -0
  18. package/lib/download/core/figureSvg.d.ts +7 -1
  19. package/lib/download/core/figureSvg.d.ts.map +1 -1
  20. package/lib/download/core/figureSvg.js +1 -0
  21. package/lib/download/core/figureSvg.js.map +1 -1
  22. package/lib/download/core/figureSvgDocument.d.ts +7 -0
  23. package/lib/download/core/figureSvgDocument.d.ts.map +1 -1
  24. package/lib/download/core/figureSvgDocument.js +11 -2
  25. package/lib/download/core/figureSvgDocument.js.map +1 -1
  26. package/lib/download/core/index.d.ts +5 -1
  27. package/lib/download/core/index.d.ts.map +1 -1
  28. package/lib/download/core/index.js +3 -1
  29. package/lib/download/core/index.js.map +1 -1
  30. package/lib/download/ui/FigureDownload.d.ts +18 -4
  31. package/lib/download/ui/FigureDownload.d.ts.map +1 -1
  32. package/lib/download/ui/FigureDownload.js +35 -9
  33. package/lib/download/ui/FigureDownload.js.map +1 -1
  34. package/lib/download/ui/FigureDownloadPanel.d.ts +17 -7
  35. package/lib/download/ui/FigureDownloadPanel.d.ts.map +1 -1
  36. package/lib/download/ui/FigureDownloadPanel.js +30 -22
  37. package/lib/download/ui/FigureDownloadPanel.js.map +1 -1
  38. package/lib/download/ui/FigureSizeField.d.ts +27 -0
  39. package/lib/download/ui/FigureSizeField.d.ts.map +1 -0
  40. package/lib/download/ui/FigureSizeField.js +87 -0
  41. package/lib/download/ui/FigureSizeField.js.map +1 -0
  42. package/lib/download/ui/FigureSizeRows.d.ts +34 -0
  43. package/lib/download/ui/FigureSizeRows.d.ts.map +1 -0
  44. package/lib/download/ui/FigureSizeRows.js +54 -0
  45. package/lib/download/ui/FigureSizeRows.js.map +1 -0
  46. package/lib/download/ui/index.d.ts +2 -0
  47. package/lib/download/ui/index.d.ts.map +1 -1
  48. package/lib/download/ui/index.js.map +1 -1
  49. package/lib/download/ui/useFigureRedraw.d.ts +27 -0
  50. package/lib/download/ui/useFigureRedraw.d.ts.map +1 -0
  51. package/lib/download/ui/useFigureRedraw.js +78 -0
  52. package/lib/download/ui/useFigureRedraw.js.map +1 -0
  53. package/lib/i18n/core/chromeCatalog.d.ts +1 -1
  54. package/lib/i18n/core/chromeCatalog.d.ts.map +1 -1
  55. package/lib/locales/de.json +10 -2
  56. package/lib/locales/en.json +10 -2
  57. package/lib/locales/es.json +10 -2
  58. package/lib/locales/fr.json +10 -2
  59. package/lib/overlay/ui/OverlayValueMenu.d.ts.map +1 -1
  60. package/lib/overlay/ui/OverlayValueMenu.js +7 -1
  61. package/lib/overlay/ui/OverlayValueMenu.js.map +1 -1
  62. package/package.json +1 -1
  63. package/src/download/core/downloadFigure.ts +12 -9
  64. package/src/download/core/figureLayout.ts +114 -0
  65. package/src/download/core/figureScale.ts +13 -0
  66. package/src/download/core/figureSettle.ts +112 -0
  67. package/src/download/core/figureSvg.ts +8 -1
  68. package/src/download/core/figureSvgDocument.ts +19 -2
  69. package/src/download/core/index.ts +13 -0
  70. package/src/download/ui/FigureDownload.tsx +88 -35
  71. package/src/download/ui/FigureDownloadPanel.tsx +45 -22
  72. package/src/download/ui/FigureSizeField.tsx +153 -0
  73. package/src/download/ui/FigureSizeRows.tsx +108 -0
  74. package/src/download/ui/index.ts +2 -0
  75. package/src/download/ui/useFigureRedraw.tsx +115 -0
  76. package/src/locales/de.json +10 -2
  77. package/src/locales/en.json +10 -2
  78. package/src/locales/es.json +10 -2
  79. package/src/locales/fr.json +10 -2
  80. package/src/overlay/ui/OverlayValueMenu.tsx +7 -1
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Knowing when a figure drawn for a file has finished drawing.
3
+ *
4
+ * A chart mounted off the page to be saved at another size does not arrive in
5
+ * one piece: it may render once at a guessed size, measure its box, and render
6
+ * again, and a chart behind a lazy import arrives whenever its chunk does. So
7
+ * nothing here waits a fixed time. The figure is watched frame by frame, and
8
+ * it is taken the moment it has stood still for two frames running.
9
+ */
10
+
11
+ import { figureSize } from './figureTarget.ts';
12
+
13
+ /** How the wait is run. */
14
+ export interface FigureSettleOptions {
15
+ /**
16
+ * Stops the wait, for a panel closed or a page left while the figure is
17
+ * still drawing.
18
+ * @default undefined — the wait runs to its end
19
+ */
20
+ signal?: AbortSignal;
21
+ /**
22
+ * How many frames the figure is given to settle before the save is given
23
+ * up, about ten seconds at sixty frames a second. It is how a figure that
24
+ * never draws is reported, not how long a slow one is expected to take.
25
+ * @default 600
26
+ */
27
+ maxFrames?: number;
28
+ /**
29
+ * Resolves at the next frame.
30
+ * @default the browser's `requestAnimationFrame`
31
+ */
32
+ nextFrame?: () => Promise<void>;
33
+ }
34
+
35
+ /** How many frames in a row the figure has to hold still. */
36
+ const STILL_FRAMES = 2;
37
+
38
+ /** What {@link FigureSettleOptions.maxFrames} is unless the caller says. */
39
+ const DEFAULT_MAX_FRAMES = 600;
40
+
41
+ /**
42
+ * Wait until the figure mounted in a box has finished drawing.
43
+ * @param element - The box the figure is being drawn in.
44
+ * @param options - See {@link FigureSettleOptions}.
45
+ * @returns Resolves once the figure has stood still.
46
+ * @throws {Error} When the wait is stopped, or the figure never settles.
47
+ */
48
+ export async function whenFigureSettles(
49
+ element: Element,
50
+ options: FigureSettleOptions = {},
51
+ ): Promise<void> {
52
+ let changes = 0;
53
+ const observer = new MutationObserver(() => {
54
+ changes++;
55
+ });
56
+ observer.observe(element, {
57
+ subtree: true,
58
+ childList: true,
59
+ attributes: true,
60
+ characterData: true,
61
+ });
62
+ try {
63
+ await settle(() => {
64
+ const size = figureSize(element);
65
+ return size === null ? null : `${changes}:${size.width}×${size.height}`;
66
+ }, options);
67
+ } finally {
68
+ observer.disconnect();
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Wait until a reading of the figure comes back the same frames in a row.
74
+ *
75
+ * The reading is whatever says the figure has changed — for a drawing on the
76
+ * page, how many times it was touched and the size it came out at — and
77
+ * `null` while there is nothing drawn yet.
78
+ * @param read - Takes the reading.
79
+ * @param options - See {@link FigureSettleOptions}.
80
+ * @returns Resolves once the reading has held.
81
+ * @throws {Error} When the wait is stopped, or the reading never holds.
82
+ */
83
+ export async function settle(
84
+ read: () => string | null,
85
+ options: FigureSettleOptions = {},
86
+ ): Promise<void> {
87
+ const { signal, maxFrames = DEFAULT_MAX_FRAMES } = options;
88
+ const { nextFrame = animationFrame } = options;
89
+
90
+ let last: string | null = null;
91
+ let still = 0;
92
+ for (let frame = 0; frame < maxFrames; frame++) {
93
+ // eslint-disable-next-line no-await-in-loop -- each frame is compared with the one before it
94
+ await nextFrame();
95
+ if (signal?.aborted) throw new Error('The figure was not saved.');
96
+ const reading = read();
97
+ still = reading !== null && reading === last ? still + 1 : 0;
98
+ if (still >= STILL_FRAMES) return;
99
+ last = reading;
100
+ }
101
+ throw new Error('The figure did not finish drawing, so nothing was saved.');
102
+ }
103
+
104
+ /**
105
+ * The next frame the browser paints.
106
+ * @returns Resolves at that frame.
107
+ */
108
+ function animationFrame(): Promise<void> {
109
+ return new Promise((resolve) => {
110
+ requestAnimationFrame(() => resolve());
111
+ });
112
+ }
@@ -27,13 +27,19 @@ export interface FigureSvgOptions {
27
27
  * @default the surface the figure is drawn on, which is the page's own
28
28
  */
29
29
  background?: string;
30
+ /**
31
+ * How many times the figure on screen the document says it is. The drawing
32
+ * is unchanged; only the size the file opens at grows.
33
+ * @default 1
34
+ */
35
+ scale?: number;
30
36
  }
31
37
 
32
38
  /** A figure, as the file that leaves the page. */
33
39
  export interface FigureSvg {
34
40
  /** The whole SVG document, tokens resolved. */
35
41
  markup: string;
36
- /** Its width in pixels, at the size the figure was drawn. */
42
+ /** Its width in pixels, at the size the figure was drawn on screen. */
37
43
  width: number;
38
44
  /** Its height. */
39
45
  height: number;
@@ -94,6 +100,7 @@ export function figureSvg(
94
100
  background: options.background ?? surfaceOf(styles),
95
101
  fontFamily: styles.fontFamily,
96
102
  fontSize: Number.parseFloat(styles.fontSize),
103
+ scale: options.scale,
97
104
  });
98
105
 
99
106
  return {
@@ -53,6 +53,13 @@ export interface FigureSvgDocumentOptions {
53
53
  * @default undefined — the reader's own default
54
54
  */
55
55
  fontSize?: number;
56
+ /**
57
+ * How many times the figure on screen the document says it is. Only the
58
+ * size it opens at changes — the drawing keeps its own coordinates, so every
59
+ * line and every word grows together and stays as sharp as before.
60
+ * @default 1
61
+ */
62
+ scale?: number;
56
63
  }
57
64
 
58
65
  /** Where an SVG document says it belongs. */
@@ -69,7 +76,8 @@ export function figureSvgDocument(
69
76
  pieces: readonly FigurePiece[],
70
77
  options: FigureSvgDocumentOptions,
71
78
  ): string {
72
- const { width, height, background, fontFamily, fontSize } = options;
79
+ const { width, height, background, fontFamily, fontSize, scale } = options;
80
+ const factor = positiveOrOne(scale);
73
81
  const box = `0 0 ${round(width)} ${round(height)}`;
74
82
  const style = documentStyle(fontFamily, fontSize);
75
83
 
@@ -86,7 +94,7 @@ export function figureSvgDocument(
86
94
 
87
95
  return (
88
96
  `<?xml version="1.0" encoding="UTF-8"?>` +
89
- `<svg xmlns="${SVG_NAMESPACE}" width="${round(width)}" height="${round(height)}" viewBox="${box}"${style}>` +
97
+ `<svg xmlns="${SVG_NAMESPACE}" width="${round(width * factor)}" height="${round(height * factor)}" viewBox="${box}"${style}>` +
90
98
  `${inside}</svg>`
91
99
  );
92
100
  }
@@ -113,6 +121,15 @@ function documentStyle(
113
121
  : ` style="${escapeAttribute(rules.join(';'))}"`;
114
122
  }
115
123
 
124
+ /**
125
+ * A multiple that makes sense, or one.
126
+ * @param value - The multiple asked for.
127
+ * @returns It, where it is a positive number; one otherwise.
128
+ */
129
+ function positiveOrOne(value: number | undefined): number {
130
+ return value !== undefined && Number.isFinite(value) && value > 0 ? value : 1;
131
+ }
132
+
116
133
  /**
117
134
  * A number as an attribute reads it: whole pixels where it is one, and two
118
135
  * decimals otherwise, because a serialized figure carrying sixteen digits per
@@ -3,12 +3,25 @@ export { downloadBlob } from './downloadBlob.ts';
3
3
  export type { DownloadFigureOptions, FigureFormat } from './downloadFigure.ts';
4
4
  export { downloadFigure } from './downloadFigure.ts';
5
5
  export { downloadText } from './downloadText.ts';
6
+ export type { FigureLayout } from './figureLayout.ts';
7
+ export {
8
+ FIGURE_COLUMN_WIDTH,
9
+ FIGURE_LAYOUTS,
10
+ FIGURE_LAYOUT_MAX,
11
+ FIGURE_LAYOUT_MIN,
12
+ figureLayoutRedraws,
13
+ figureLayoutSide,
14
+ figureLayoutSize,
15
+ } from './figureLayout.ts';
6
16
  export { FIGURE_PNG_TYPE, figurePng } from './figurePng.ts';
17
+ export type { FigureSettleOptions } from './figureSettle.ts';
18
+ export { whenFigureSettles } from './figureSettle.ts';
7
19
  export type { FigurePixels } from './figureScale.ts';
8
20
  export {
9
21
  DEFAULT_FIGURE_SCALE,
10
22
  FIGURE_MAX_PIXELS,
11
23
  FIGURE_SCALES,
24
+ defaultFigureScale,
12
25
  figurePixels,
13
26
  figureScaleFits,
14
27
  figureScaleLabel,
@@ -7,11 +7,19 @@ import { useChromeT } from '../../i18n/ui/useT.ts';
7
7
  import { OverlayIconButton } from '../../overlay/ui/OverlayIconButton.tsx';
8
8
  import type { FigureFormat } from '../core/downloadFigure.ts';
9
9
  import { downloadFigure } from '../core/downloadFigure.ts';
10
+ import type { FigureLayout } from '../core/figureLayout.ts';
11
+ import { figureLayoutRedraws, figureLayoutSize } from '../core/figureLayout.ts';
10
12
  import type { FigurePixels } from '../core/figureScale.ts';
11
- import { DEFAULT_FIGURE_SCALE, FIGURE_SCALES } from '../core/figureScale.ts';
13
+ import {
14
+ DEFAULT_FIGURE_SCALE,
15
+ FIGURE_SCALES,
16
+ defaultFigureScale,
17
+ } from '../core/figureScale.ts';
12
18
  import { figureSize } from '../core/figureTarget.ts';
13
19
 
14
20
  import { FigureDownloadPanel } from './FigureDownloadPanel.tsx';
21
+ import type { FigureRenderer } from './useFigureRedraw.tsx';
22
+ import { useFigureRedraw } from './useFigureRedraw.tsx';
15
23
 
16
24
  /** What {@link FigureDownload} needs. */
17
25
  export interface FigureDownloadProps {
@@ -34,7 +42,9 @@ export interface FigureDownloadProps {
34
42
  */
35
43
  defaultFormat?: FigureFormat;
36
44
  /**
37
- * The resolution it opens on, as a multiple of the figure on screen.
45
+ * The resolution a PNG opens on, as a multiple of the figure on screen. An
46
+ * SVG opens at 1×, the size it has on screen, and each format keeps the
47
+ * multiple the reader last picked for it.
38
48
  * @default 2
39
49
  */
40
50
  defaultScale?: number;
@@ -69,6 +79,16 @@ export interface FigureDownloadProps {
69
79
  * @default undefined
70
80
  */
71
81
  testId?: string;
82
+ /**
83
+ * Draws the figure at a size, for a file. When it is given the panel offers
84
+ * a size — as shown, 4:3, 16:9, a journal column, or typed — and a figure
85
+ * saved at a size other than its own is drawn again at that size, off the
86
+ * page, so its axes and labels are laid out for the new shape rather than
87
+ * stretched into it. Draw exactly what is on screen, with the same data,
88
+ * zoom and colours; only the box changes.
89
+ * @default undefined — the figure is saved at the size it has on screen
90
+ */
91
+ renderFigure?: FigureRenderer;
72
92
  }
73
93
 
74
94
  /**
@@ -84,9 +104,10 @@ export interface FigureDownloadProps {
84
104
  * Both formats are offered because they answer different questions. An SVG is
85
105
  * the figure itself, sharp at any size and still editable, which is what a
86
106
  * paper wants; a PNG is a picture of it, which is what every chat window and
87
- * slide deck accepts. The resolution belongs to the second alone, and the
88
- * panel writes out the pixels it is about to produce so nobody has to guess
89
- * what `3×` means for the figure in front of them.
107
+ * slide deck accepts. The resolution paints a PNG with more pixels and makes
108
+ * an SVG open larger, and the panel writes out the size it is about to
109
+ * produce so nobody has to guess what `3×` means for the figure in front of
110
+ * them.
90
111
  * @param props - See {@link FigureDownloadProps}.
91
112
  * @returns The glyph and its panel.
92
113
  */
@@ -94,15 +115,22 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
94
115
  const { targetId, fileName, background, scales = FIGURE_SCALES } = props;
95
116
  const { defaultFormat = 'png', defaultScale = DEFAULT_FIGURE_SCALE } = props;
96
117
  const { label, title } = props;
97
- const { icon = 'download', testId } = props;
118
+ const { icon = 'download', testId, renderFigure } = props;
98
119
 
99
120
  const t = useChromeT();
100
121
  const [open, setOpen] = useState(false);
101
122
  const [format, setFormat] = useState<FigureFormat>(defaultFormat);
102
- const [scale, setScale] = useState(defaultScale);
123
+ const [picked, setPicked] = useState<Record<FigureFormat, number>>(() => ({
124
+ png: defaultScale,
125
+ svg: defaultFigureScale('svg'),
126
+ }));
127
+ const scale = picked[format];
103
128
  const [size, setSize] = useState<FigurePixels | null>(null);
104
129
  const [failure, setFailure] = useState<string | null>(null);
105
130
  const [saving, setSaving] = useState(false);
131
+ const [layout, setLayout] = useState<FigureLayout>('screen');
132
+ const [custom, setCustom] = useState<FigurePixels | null>(null);
133
+ const { redraw, portal } = useFigureRedraw(renderFigure);
106
134
 
107
135
  // The panel is held open from here rather than left to itself, because the
108
136
  // figure has to be measured at the moment it opens: its size is what the
@@ -118,7 +146,17 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
118
146
  async function save(): Promise<void> {
119
147
  setSaving(true);
120
148
  try {
121
- await downloadFigure(targetId, { format, scale, fileName, background });
149
+ const options = { format, scale, fileName, background };
150
+ if (
151
+ renderFigure !== undefined &&
152
+ size !== null &&
153
+ figureLayoutRedraws(layout, size, custom ?? size)
154
+ ) {
155
+ const drawn = figureLayoutSize(layout, size, custom ?? size);
156
+ await redraw(targetId, drawn, (box) => downloadFigure(box, options));
157
+ } else {
158
+ await downloadFigure(targetId, options);
159
+ }
122
160
  setFailure(null);
123
161
  } catch (error) {
124
162
  setFailure(error instanceof Error ? error.message : String(error));
@@ -128,33 +166,48 @@ export function FigureDownload(props: FigureDownloadProps): ReactElement {
128
166
  }
129
167
 
130
168
  return (
131
- <PopoverNext
132
- isOpen={open}
133
- placement="bottom-end"
134
- onInteraction={interact}
135
- content={
136
- <FigureDownloadPanel
137
- title={title ?? t('download.saveFigure')}
138
- format={format}
139
- scale={scale}
140
- scales={scales}
141
- size={size}
142
- failure={failure}
143
- saving={saving}
144
- onFormatChange={setFormat}
145
- onScaleChange={setScale}
146
- onSave={() => void save()}
169
+ <>
170
+ <PopoverNext
171
+ isOpen={open}
172
+ placement="bottom-end"
173
+ onInteraction={interact}
174
+ content={
175
+ <FigureDownloadPanel
176
+ title={title ?? t('download.saveFigure')}
177
+ format={format}
178
+ scale={scale}
179
+ scales={scales}
180
+ size={size}
181
+ failure={failure}
182
+ saving={saving}
183
+ onFormatChange={setFormat}
184
+ onScaleChange={(next) =>
185
+ setPicked((current) => ({ ...current, [format]: next }))
186
+ }
187
+ onSave={() => void save()}
188
+ sizing={
189
+ renderFigure === undefined
190
+ ? undefined
191
+ : {
192
+ layout,
193
+ custom,
194
+ onLayoutChange: setLayout,
195
+ onCustomChange: setCustom,
196
+ }
197
+ }
198
+ />
199
+ }
200
+ >
201
+ <OverlayIconButton
202
+ icon={icon}
203
+ label={label ?? t('download.saveThisFigure')}
204
+ value={format.toUpperCase()}
205
+ active={open}
206
+ testId={testId}
207
+ opensMenu
147
208
  />
148
- }
149
- >
150
- <OverlayIconButton
151
- icon={icon}
152
- label={label ?? t('download.saveThisFigure')}
153
- value={format.toUpperCase()}
154
- active={open}
155
- testId={testId}
156
- opensMenu
157
- />
158
- </PopoverNext>
209
+ </PopoverNext>
210
+ {portal}
211
+ </>
159
212
  );
160
213
  }
@@ -8,6 +8,7 @@ import { OverlayPanel } from '../../overlay/ui/OverlayPanel.tsx';
8
8
  import type { OverlayOption } from '../../overlay/ui/OverlayRow.tsx';
9
9
  import { OverlaySegmented } from '../../overlay/ui/OverlaySegmented.tsx';
10
10
  import type { FigureFormat } from '../core/downloadFigure.ts';
11
+ import { figureLayoutSize } from '../core/figureLayout.ts';
11
12
  import type { FigurePixels } from '../core/figureScale.ts';
12
13
  import {
13
14
  figurePixels,
@@ -16,6 +17,9 @@ import {
16
17
  formatFigurePixels,
17
18
  } from '../core/figureScale.ts';
18
19
 
20
+ import type { FigureSizing } from './FigureSizeRows.tsx';
21
+ import { FigureSizeRows } from './FigureSizeRows.tsx';
22
+
19
23
  /** What {@link FigureDownloadPanel} is drawn from. */
20
24
  export interface FigureDownloadPanelProps {
21
25
  /** What the panel is called. */
@@ -40,21 +44,30 @@ export interface FigureDownloadPanelProps {
40
44
  onSave: () => void;
41
45
  /**
42
46
  * Whether the SVG holds a rendered picture rather than vector drawings, as
43
- * for a WebGL scene: its resolution then applies, and the hint says so.
47
+ * for a WebGL scene: its resolution is then the pixels of that picture, it
48
+ * meets the same ceiling as a PNG, and the hint says so.
44
49
  * @default false
45
50
  */
46
51
  rasterSvg?: boolean;
52
+ /**
53
+ * The shape the figure is drawn at, for a figure that can be drawn again at
54
+ * another size. Without it the figure is saved at the size it has on screen,
55
+ * and no size is offered.
56
+ * @default undefined
57
+ */
58
+ sizing?: FigureSizing;
47
59
  }
48
60
 
49
61
  /**
50
- * The two choices behind the save glyph, and the button that writes the file.
62
+ * The choices behind the save glyph, and the button that writes the file.
51
63
  *
52
- * The resolution stays in place and greyed while an SVG is selected rather
53
- * than disappearing, because a control that vanishes is a control the reader
54
- * concludes the figure does not have — and the sentence at the foot says why
55
- * it is greyed, which is that an SVG has no resolution to choose.
64
+ * The resolution applies to both formats, and means what the reader needs it
65
+ * to: a PNG is painted with that many more pixels, and an SVG, drawn the same,
66
+ * opens that many times larger in the slide or the document it is dropped
67
+ * into. Only a PNG meets the ceiling of what a browser can paint, so only its
68
+ * choices past that ceiling are greyed.
56
69
  *
57
- * That sentence is the panel's own hint line rather than a row of its own: it
70
+ * The sentence at the foot is the panel's own hint line rather than a row of its own: it
58
71
  * is not a setting, it is what the two settings above it currently amount to,
59
72
  * and it is the only thing here that answers the question a reader actually
60
73
  * has, which is how big the file is going to be. Saving itself is a command
@@ -68,14 +81,21 @@ export function FigureDownloadPanel(
68
81
  ): ReactElement {
69
82
  const { title, format, scale, scales, size, failure, saving } = props;
70
83
  const { onFormatChange, onScaleChange, onSave, rasterSvg = false } = props;
84
+ const { sizing } = props;
71
85
  const t = useChromeT();
72
86
 
73
- const vector = format === 'svg' && !rasterSvg;
87
+ // What the file is drawn at: the figure on screen, or the shape picked.
88
+ const drawn =
89
+ size === null || sizing === undefined
90
+ ? size
91
+ : figureLayoutSize(sizing.layout, size, sizing.custom ?? size);
92
+
93
+ const painted = format === 'png' || rasterSvg;
74
94
 
75
95
  return (
76
96
  <OverlayPanel
77
97
  title={title}
78
- hint={hintOf(format, size, scale, failure, rasterSvg, t)}
98
+ hint={hintOf(format, drawn, scale, failure, rasterSvg, t)}
79
99
  actions={
80
100
  <OverlayAction
81
101
  text={t('download.save')}
@@ -96,6 +116,9 @@ export function FigureDownloadPanel(
96
116
  options={FORMAT_CHOICES}
97
117
  onChange={onFormatChange}
98
118
  />
119
+ {sizing === undefined ? null : (
120
+ <FigureSizeRows sizing={sizing} size={size} />
121
+ )}
99
122
  <OverlaySegmented
100
123
  label={t('download.resolution')}
101
124
  help={{
@@ -107,8 +130,7 @@ export function FigureDownloadPanel(
107
130
  },
108
131
  }}
109
132
  value={String(scale)}
110
- options={scaleChoices(scales, size)}
111
- disabled={vector}
133
+ options={scaleChoices(scales, drawn, painted)}
112
134
  onChange={(picked) => onScaleChange(Number(picked))}
113
135
  />
114
136
  </OverlayPanel>
@@ -125,14 +147,17 @@ const FORMAT_CHOICES: ReadonlyArray<OverlayOption<FigureFormat>> = [
125
147
  * The multiples offered, each saying what it would actually produce.
126
148
  *
127
149
  * One too large for the browser to paint is greyed rather than dropped, so the
128
- * reader learns the ceiling exists before they meet it as a blank file.
150
+ * reader learns the ceiling exists before they meet it as a blank file. An SVG
151
+ * is not painted, so it has no such ceiling.
129
152
  * @param scales - The multiples the caller offers.
130
- * @param size - How big the figure is on the page.
153
+ * @param size - How big the figure is drawn for the file.
154
+ * @param painted - Whether the file is painted into pixels.
131
155
  * @returns The choices, in the order offered.
132
156
  */
133
157
  function scaleChoices(
134
158
  scales: readonly number[],
135
159
  size: FigurePixels | null,
160
+ painted: boolean,
136
161
  ): readonly OverlayOption[] {
137
162
  const choices: OverlayOption[] = [];
138
163
  for (const scale of scales) {
@@ -141,7 +166,7 @@ function scaleChoices(
141
166
  value: String(scale),
142
167
  label: figureScaleLabel(scale),
143
168
  title: pixels === null ? undefined : formatFigurePixels(pixels),
144
- disabled: size !== null && !figureScaleFits(size, scale),
169
+ disabled: painted && size !== null && !figureScaleFits(size, scale),
145
170
  });
146
171
  }
147
172
  return choices;
@@ -150,8 +175,8 @@ function scaleChoices(
150
175
  /**
151
176
  * The line at the foot of the panel: what pressing save is about to do.
152
177
  * @param format - Which file the reader is about to write.
153
- * @param size - How big the figure is on the page.
154
- * @param scale - The multiple it is painted at.
178
+ * @param size - How big the figure is drawn for the file.
179
+ * @param scale - The multiple it is saved at.
155
180
  * @param failure - What went wrong last time, if anything.
156
181
  * @param rasterSvg - Whether the SVG embeds a rendered picture.
157
182
  * @param t - The chrome's formatter, so the sentence is in the language of
@@ -173,10 +198,8 @@ function hintOf(
173
198
  pixels: formatFigurePixels(figurePixels(size, scale)),
174
199
  });
175
200
  }
176
- if (format === 'svg') {
177
- return t('download.hintSvg', { pixels: formatFigurePixels(size) });
178
- }
179
- return t('download.hintPng', {
180
- pixels: formatFigurePixels(figurePixels(size, scale)),
181
- });
201
+ const pixels = formatFigurePixels(figurePixels(size, scale));
202
+ return format === 'svg'
203
+ ? t('download.hintSvg', { pixels })
204
+ : t('download.hintPng', { pixels });
182
205
  }