From b66ef3c8ea7706cdda0c5df19231b7489a22b03b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=B7=E7=94=B5=E8=8A=BD=E8=A1=A3?= Date: Sun, 11 Oct 2026 09:58:21 -0400 Subject: [PATCH] 14.18 round 2: size fields read Swift's Int field, the mask and a tall page tested --- AGENTS.md | 2 +- src/UI/ExportAsSheet.cpp | 30 ++++++++++++++++++++++-------- tests/ExportAsSheetTests.cpp | 17 +++++++++++++++++ tests/ExportPDFFailureTests.cpp | 12 +++++------- tests/ExportPDFTests.cpp | 10 ++++++++++ 5 files changed, 55 insertions(+), 16 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 3afc28c..15773ef 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -197,7 +197,7 @@ Platform adaptations, each forced by a missing Qt analog: - Revealing the chosen layer (14.15, Swift 1.4.8: `d2b8aaf`, `b4e147b`): `selectLayer` and `selectLayers` end with `revealActiveLayer` (declared in `LayerGroups+Session.h`, as Swift's extension), which opens every folder above the active layer and says whether one opened; `selectLayer` then signals again, since its setter signalled before, so the last signal sees the folders open. Swift's `seen` set, a guard against a parent cycle, is left out: a session's document always passes `LayerHierarchy::validate`, so the chain ends at the root. `NativeLayerList::update` remembers the active layer it last scrolled to and scrolls a newly active row into view once its row exists (`ensureWidgetVisible`, no margin), so a later rename or refresh leaves a scroll made by hand: the session's setter signals while a shut folder still hides the row, where SwiftUI sees the selection and the open folders in one update, so the list waits for the row, and first hands the scroll area its posted layout request, since rows made anew have not yet grown the scroll range. Swift's own tests went with `b4e147b`; the twin keeps them. Equivalent mutant: the guard on an empty collapsed set (the walk then removes nothing; Swift's too). Tests: `LayerRevealTests` (Swift's five, the last signal with both and only the inner folder shut, a primary outside every folder opening nothing, a child of a shut folder below the view scrolled to by either call); `ProjectSessionTests` now collapses its folder after choosing a layer inside it. - Camera Raw's Shadows and Highlights (14.16, Swift 1.4.8: `0b4c56c`): `kernels/AdjustPixels.c` is 0b4c56c's, unchanged: each follows a bump, u(1 − 2u)², over its half of the tones (Shadows from black, Highlights from white), two and a half times the bump to lift Shadows or pull Highlights down and once the other way, so tones never swap, and black, middle gray and white stay. Tests: `CameraRawToneOrderTests` (Swift's ramp kept in order at ±100 and the dark tones lifted; every level of a gray ramp against the curve written apart, each way at full and partial amounts and both together, Highlights first). - The Layers panel's gray (14.17, Swift 1.4.8: `06313a9`): `layersLookEditable` is Swift's: `canEditLayers` without what lasts only while the mouse is down (a stroke, a warp, a drag's transform edit, a pixel move) or a moment (`isProjectBusy`, read as `showsBusy`, which shows after 250 ms). The list's rows and their eyes (an effect's eye keeps `canEditLayers`, as Swift makes each effect row anew with it), the footer's buttons (new layer, folder, adjustments, delete with an active layer), the effects menu (with a layer that has pixels) and the mask button (one layer selected, without a mask) follow it, so nothing flashes gray for a click; every action still asks the session's own gate, which refuses meanwhile. The effects menu's folder term is left out as dead: a folder never holds pixels, so the pixel term refuses it. Tests: `LayersLookEditableTests` (each moment looks editable while the gate stays shut, each lasting state grays, the footer and list mid-stroke and under a pending transform, the effects menu's and mask button's own terms); the busy cases in `BlendModePickerTests`, `LayersPanelTests`, `NativeLayerListTests` and `LayerEffectRowTests` now wait for the indicator, and a click before it changes nothing. -- Export As (14.18, Swift 1.4.8: `9287cf1`, `450af5f`, `b1db93d`): `ProjectController::exportAs(start, done)` replaces `exportJPEG`; `ExportFormat` (png, jpeg, pdf, Swift's raw values through `rawValue`, `allExportFormats`) starts the sheet, else the last format exported this run (`m_lastExportFormat`, PNG at first), and the File menu's Export JPEG… opens it on JPEG; Export As… (Ctrl+Alt+Shift+W, a new shortcut definition) on the last. The flow is the JPEG one: the render in a worker, the sheet, then the format's panel (Export PNG, Export JPEG or Export PDF, suggesting `.png`, `.jpeg` or `.pdf`), the write in a worker, one alert titled "Couldn’t export" for a failure past the render. `UI/ExportAsSheet` (Swift's sheet, renamed from the JPEG one): titled Export As; the format as three checkable buttons; the size as two `PickerField`s keeping the proportions (a number applies on Return or leaving, else both fields show the size again; a scrub or menu replaces their typing) and a scale menu of 25, 50, 75, 100 and 200%, every scale held between a hundredth and the side and surface limits; Quality for JPEG alone (saved only from a JPEG export), the page size in inches at the document's DPI for PDF alone, the background swatch ("Export Background") for JPEG and PDF; "Transparency kept · sRGB" for PNG, else "sRGB"; a change of format or size asks again only when the settings change, as Swift's `task(id:)`. The encoding resizes first (`ImageExporter::resized`: smooth, the same raster at the same size, `tooLarge` past the limits) and encodes by format (`pngData`; `jpeg`; `pdfData` over `flattened` for the preview). `JPEGPreview` takes the export's size beside its image (`setImage(image, pixels, updating)`), so Fit and 100% follow the chosen size, and draws Swift 1.4.8's checkerboard (20-point tiles of grays 0.30 and 0.35, anchored at the image) behind what it shows. Swift's PDF comes from `CGContext`'s PDF writer; `QPdfWriter` rounds a page to whole points, so `ImageExporter::pdfData` writes PDF 1.4 by hand: one page whose MediaBox is the print size in points (72 / resolution a pixel, 72 when none), the background as a filled rectangle when given, the pixels as a Flate RGB image with a gray soft mask, straight colour (adaptation; checked against poppler's `pdfinfo` and `pdftoppm`). A Flate stream that `qCompress` cannot make (it returns nothing when memory runs out) is `ExportError(render)`, never an empty stream. A zoom set before the first image (Ctrl+1 while the sheet still encodes) centres that image when it lands, as Swift's preview, made with its first result, centres on appearing. The page's inches round as Swift's formatter: its shortest digits, halves to even (45 pixels at 72 DPI are 0.62 in). Tests: `ExportPDFTests` (the page, its size, the straight pixels and alpha, rows and channels, the background, a refusal, resizing, flattening), `ExportPDFFailureTests` (a binary of its own: a 1000-pixel PDF under limits from 2 to 48 MB throws or inflates whole, both reached), `ExportAsSheetTests` (formats and their rows and bytes, the size and its limits, the checkerboard, the page against Swift 6.0.3's formatter), `JPEGPreviewTests::aZoomBeforeTheFirstImageCentresIt`, `ProjectExportTests` (each format through the controller, the menu's Export As on the last format), the entry in `CompositorMenusTests` and `KeyboardShortcutsTests` (its chord, group and place). Equivalent mutants: none found. +- Export As (14.18, Swift 1.4.8: `9287cf1`, `450af5f`, `b1db93d`): `ProjectController::exportAs(start, done)` replaces `exportJPEG`; `ExportFormat` (png, jpeg, pdf, Swift's raw values through `rawValue`, `allExportFormats`) starts the sheet, else the last format exported this run (`m_lastExportFormat`, PNG at first), and the File menu's Export JPEG… opens it on JPEG; Export As… (Ctrl+Alt+Shift+W, a new shortcut definition) on the last. The flow is the JPEG one: the render in a worker, the sheet, then the format's panel (Export PNG, Export JPEG or Export PDF, suggesting `.png`, `.jpeg` or `.pdf`), the write in a worker, one alert titled "Couldn’t export" for a failure past the render. `UI/ExportAsSheet` (Swift's sheet, renamed from the JPEG one): titled Export As; the format as three checkable buttons; the size as two `PickerField`s keeping the proportions (a number applies on Return or leaving, else both fields show the size again; a scrub or menu replaces their typing) and a scale menu of 25, 50, 75, 100 and 200%, every scale held between a hundredth and the side and surface limits; Quality for JPEG alone (saved only from a JPEG export), the page size in inches at the document's DPI for PDF alone, the background swatch ("Export Background") for JPEG and PDF; "Transparency kept · sRGB" for PNG, else "sRGB"; a change of format or size asks again only when the settings change, as Swift's `task(id:)`. The encoding resizes first (`ImageExporter::resized`: smooth, the same raster at the same size, `tooLarge` past the limits) and encodes by format (`pngData`; `jpeg`; `pdfData` over `flattened` for the preview). `JPEGPreview` takes the export's size beside its image (`setImage(image, pixels, updating)`), so Fit and 100% follow the chosen size, and draws Swift 1.4.8's checkerboard (20-point tiles of grays 0.30 and 0.35, anchored at the image) behind what it shows. Swift's PDF comes from `CGContext`'s PDF writer; `QPdfWriter` rounds a page to whole points, so `ImageExporter::pdfData` writes PDF 1.4 by hand: one page whose MediaBox is the print size in points (72 / resolution a pixel, 72 when none), the background as a filled rectangle when given, the pixels as a Flate RGB image with a gray soft mask, straight colour (adaptation; checked against poppler's `pdfinfo` and `pdftoppm`). A Flate stream that `qCompress` cannot make (it returns nothing when memory runs out) is `ExportError(render)`, never an empty stream. A zoom set before the first image (Ctrl+1 while the sheet still encodes) centres that image when it lands, as Swift's preview, made with its first result, centres on appearing. The page's inches round as Swift's formatter: its shortest digits, halves to even (45 pixels at 72 DPI are 0.62 in). A size field reads Swift's `Int` field as far as QLocale can: a whole number within Int64, else a number truncated toward zero inside ±2^63 (`1e3` is 1000, `12.7` is 12), grouping refused; known difference: ICU's lenient parse also reads a leading number before other text (`7abc` is 7, `1,000` is 1), where the twin puts the size back. Tests: `ExportPDFTests` (the page, its size, the straight pixels and alpha, rows and channels, the background, a refusal, resizing, flattening), `ExportPDFFailureTests` (a binary of its own: a 360-pixel PDF, small enough that Qt converts it on the calling thread (a pool thread that cannot start hangs Qt 6.4's conversion), under limits from 64 KB to 4 MB throws or inflates whole, both reached), `ExportAsSheetTests` (formats and their rows and bytes, the size and its limits, the checkerboard, the page against Swift 6.0.3's formatter), `JPEGPreviewTests::aZoomBeforeTheFirstImageCentresIt`, `ProjectExportTests` (each format through the controller, the menu's Export As on the last format), the entry in `CompositorMenusTests` and `KeyboardShortcutsTests` (its chord, group and place). Equivalent mutants: none found. - Drops between projects: `ProjectWorkspace::draggedLayerSource` finds the tab whose document holds the dragged id (Swift reads the drag pasteboard; the twin reads the drag's `QMimeData`, which every drop target has), `canReceiveDrag(data, destination)` refuses only a layer's own tab, and `receiveProviders` copies several ids one after another, as Swift awaits each provider, so a two-row drop on the New button opens two tabs as Swift's does. `ContentView` takes such a drop over its whole area (`setWorkspace` hands it the workspace; `acceptsDrop` keeps Swift's guards: busy, the sheet, the importer, a rename; `levels` arrives with 9.x), shows the 3-point accent ring inside the canvas while targeted and hands the drop's document point on when it fell on the canvas. `ProjectTabButton` takes drops for its tab (accent fill and rim, "Add to "), and `NewCanvasButton`, the toolbar's New canvas button with the action as its default, for a fresh tab ("Open in a new project tab"), both refusing an Alt drag, which duplicates in the panel; Swift's transient `NewTabDropSlot` in the strip has no twin, the New canvas button is `NewProjectDropTarget` already. Files and pictures arrived with 10.4. - `CanvasThumbnail` draws a layer or a mask inside the canvas's box at `backingScale` 2 (the panel's pixmaps carry that ratio); a mask's ground is `LayerMask::background`, white or black by its edge's majority, as the canvas treats it (Swift's `edgeTone` mean went with 12.12b). `LayerIcons` draws the panel's SF Symbols as paths, in one palette colour, like `ToolIcons`. `BlendModePicker` is a `QComboBox` whose popup previews the highlighted mode through `previewBlendMode` for the layer it opened on (Qt calls `hidePopup` before `activated`, so the preview, the target and the highlight are cleared from the event loop, after a choice had its chance) and refuses a choice once another layer is active. `LayerAppearanceControls` is Swift's blend and opacity row: the slider's press and release are one opacity drag (the release is watched application-wide while the slider is down, so a popup opened mid-drag cannot keep the step open), the percent field applies once through its `FocusOut`, which Return and Escape cause (Swift's `releasesFocusOnCommit`), and steps with Up and Down (Swift's `isFinite` test and the steps' clamp are left out: `setLayerOpacity` refuses a value that is no finite number and clamps the rest), and another active layer makes the controls anew, as Swift's `.id` does. - `ProjectWorkspaceView` is the `QMainWindow`: the front tab's `ContentView` as its central widget, made anew when another tab comes to the front, as SwiftUI's `.id` rebuilds it, and disabled while the workspace manages; the window holds the shown tab (`std::shared_ptr`) until the old editor is deleted, as Swift's views hold their session, so a replaced or closed tab's session never ends under its editor (`ProjectWorkspace::tab` is public for this); a `QToolBar` with the New canvas action, the `ProjectTabStrip` (which takes the toolbar's free width) and the four zoom actions, since SwiftUI's `.toolbar` modifier lives in `ContentView` but the toolbar belongs to the window. Its actions follow the front session's `changed()`, and on that signal the window syncs every tab's button as well, because a busy front tab dims the other tabs' buttons, which their own sessions' signals do not reach; the strip syncs its buttons on the workspace's signal, each button on its own session's. The window's title is the front tab's, with Qt's `[*]` placeholder, so `setWindowModified` can show the mark that `isDocumentEdited` shows; `setWindowFilePath` carries `representedURL`. The editor's own minimum size is cleared inside the window, which owns the 800 × 520 minimum for the whole, toolbar included. Closing the window ignores the event and asks the workspace (`closeWindow`), which confirms tab by tab and then closes the window again with the property `closeConfirmed` set, since the workspace may not include `UI/` headers; a confirmed close is accepted and the mark cleared, so a window shown again asks again. `ProjectTabStrip` is a plain `QWidget` whose buttons are kept for the tabs that stay, made for new tabs and deleted for tabs that went, and placed by hand from `projectTabOverflow` (13.14): every button at its own size hint, so no title is squeezed below Swift's 35 points, laid out again when the strip is resized, the workspace changes or any tab's session changes; it fills no background, so the toolbar shows through (a stylesheet would do the same but freezes the widgets' palette, so a theme switch would pass them by); the unsaved dot is a character before the title. Files named on the command line go to `ProjectWorkspace::receive`, as Finder's Open With reaches the delegate. Layer drops on tabs and the New canvas button are 4.7's; file and picture drops arrived with 10.4. diff --git a/src/UI/ExportAsSheet.cpp b/src/UI/ExportAsSheet.cpp index c02fa3e..d93af46 100644 --- a/src/UI/ExportAsSheet.cpp +++ b/src/UI/ExportAsSheet.cpp @@ -64,6 +64,24 @@ QString inches(double value) return shown; } +// Swift's Int field: a whole Int64, else a number truncated. +std::optional<qint64> typedInteger(const QLineEdit &field) +{ + QLocale locale = field.locale(); + locale.setNumberOptions(locale.numberOptions() | QLocale::RejectGroupSeparator); + // QLocale's readers skip spaces at either end. + const QString text = field.text(); + bool number = false; + const qint64 whole = locale.toLongLong(text, &number); + if (number) + return whole; + const double value = locale.toDouble(text, &number); + // Int64 holds whatever lies strictly inside ±2^63. + if (!number || !(std::abs(value) < 0x1p63)) + return std::nullopt; + return qint64(std::trunc(value)); +} + struct Made { std::optional<QByteArray> data; QImage preview; @@ -76,19 +94,15 @@ ExportAsSheet::ExportAsSheet(ExportRaster raster, EditorSession &session, Export m_settings{format, JPEGOptions(), m_raster.image.width(), m_raster.image.height()}, m_wait(new QTimer(this)), m_preview(new JPEGPreview([this] { synchronize(); }, this)), m_spinner(new QProgressBar(m_preview)), m_formats(new QButtonGroup(this)), m_width(new PickerField([this] { - bool number = false; - const int value = m_width->text().toInt(&number); - if (number) - setScale(double(value) / m_raster.image.width()); + if (const std::optional<qint64> value = typedInteger(*m_width)) + setScale(double(*value) / m_raster.image.width()); // A number applies; other text goes back. showSize(); synchronize(); }, [](int) {}, this)), m_height(new PickerField([this] { - bool number = false; - const int value = m_height->text().toInt(&number); - if (number) - setScale(double(value) / m_raster.image.height()); + if (const std::optional<qint64> value = typedInteger(*m_height)) + setScale(double(*value) / m_raster.image.height()); // A number applies; other text goes back. showSize(); synchronize(); diff --git a/tests/ExportAsSheetTests.cpp b/tests/ExportAsSheetTests.cpp index 2a68de3..b5761ae 100644 --- a/tests/ExportAsSheetTests.cpp +++ b/tests/ExportAsSheetTests.cpp @@ -345,6 +345,23 @@ void ExportAsSheetTests::theSizeKeepsTheProportionsWithinTheLimits() // The surface's limit caps a growth: 312.5 times here. type(width, "100000"); QVERIFY(width.text() == "20000" && height.text() == "10000"); + // Swift's Int field: past Int32, exponents, fractions truncated. + const auto both = [&](QLineEdit &field, const char *typed) { + type(field, QString::fromLatin1(typed)); + return width.text() + QLatin1Char('x') + height.text(); + }; + QCOMPARE(both(width, "1e3"), QString("1000x500")); + QCOMPARE(both(height, "2147483648"), QString("20000x10000")); + QCOMPARE(both(width, " 12.7 "), QString("12x6")); + QCOMPARE(both(width, "1E2"), QString("100x50")); + QCOMPARE(both(height, "-12.7"), QString("1x1")); + QCOMPARE(both(width, "9223372036854775807"), QString("20000x10000")); + QCOMPARE(both(width, "64"), QString("64x32")); + // Past Int64, or grouped, Swift reads nothing here. + QCOMPARE(both(width, "9223372036854775808"), QString("64x32")); + QCOMPARE(both(width, "1e19"), QString("64x32")); + QCOMPARE(both(width, "-1e19"), QString("64x32")); + QCOMPARE(both(width, "1,000"), QString("64x32")); // The menu's steps. QStringList steps; for (QAction *each : scale.menu()->actions()) diff --git a/tests/ExportPDFFailureTests.cpp b/tests/ExportPDFFailureTests.cpp index 49cb65b..1960130 100644 --- a/tests/ExportPDFFailureTests.cpp +++ b/tests/ExportPDFFailureTests.cpp @@ -1,6 +1,5 @@ #include "AddressSpaceLimit.h" #include "IO/ImageExporter.h" -#include <QThreadPool> #include <QtEndian> #include <QtTest> #include <malloc.h> @@ -30,7 +29,7 @@ QList<QByteArray> inflated(const QByteArray &pdf) void ExportPDFFailureTests::aStarvedPDFThrowsOrIsWhole() { // Noise, so the compressed streams stay near their size. - QImage noise(1000, 1000, QImage::Format_RGBA8888_Premultiplied); + QImage noise(360, 360, QImage::Format_RGBA8888_Premultiplied); quint32 seed = 7; for (int y = 0; y < noise.height(); ++y) { auto *row = reinterpret_cast<quint32 *>(noise.scanLine(y)); @@ -38,14 +37,13 @@ void ExportPDFFailureTests::aStarvedPDFThrowsOrIsWhole() row[x] = (seed = seed * 1664525u + 1013904223u) | 0xff000000u; } const ExportRaster raster{noise, 72}; - // Qt converts on pool threads: start them before any limit. - QThreadPool::globalInstance()->setExpiryTimeout(-1); + // Under 2^17 pixels Qt converts on this thread alone. ImageExporter::pdfData(raster); int refused = 0, whole = 0; - for (qint64 megabytes = 2; megabytes <= 48; ++megabytes) { + for (qint64 kilobytes = 64; kilobytes <= 4096; kilobytes += 32) { QByteArray pdf; try { - const AddressSpaceLimit limit(megabytes << 20); + const AddressSpaceLimit limit(kilobytes << 10); pdf = ImageExporter::pdfData(raster); } catch (const ExportError &error) { QCOMPARE(error.kind, ExportError::Kind::render); @@ -54,7 +52,7 @@ void ExportPDFFailureTests::aStarvedPDFThrowsOrIsWhole() } const QList<QByteArray> streams = inflated(pdf); QCOMPARE(streams.size(), qsizetype(2)); - QVERIFY2(streams[0].size() == 1000 * 1000 * 3 && streams[1].size() == 1000 * 1000, qPrintable(QString::number(megabytes))); + QVERIFY2(streams[0].size() == 360 * 360 * 3 && streams[1].size() == 360 * 360, qPrintable(QString::number(kilobytes))); ++whole; } // The sweep reaches both sides of the limit. diff --git a/tests/ExportPDFTests.cpp b/tests/ExportPDFTests.cpp index fc0cbc8..e476ded 100644 --- a/tests/ExportPDFTests.cpp +++ b/tests/ExportPDFTests.cpp @@ -87,6 +87,16 @@ void ExportPDFTests::pdfIsOnePageAtThePrintedSizeWithLosslessPixels() const QByteArray rows = ImageExporter::pdfData(ExportRaster{grid, 72}); QCOMPARE(streamOf(object(rows, 5)), QByteArray("\x0a\x64\xc8\x1e\x5a\xcd\x32\x82\x96\x46\x78\x9b\x5a\xa0\x64\x6e\x96\x69", 18)); QVERIFY(object(rows, 5).contains("/Width 2 /Height 3")); + // A tall page draws its image tall: 2 by 3. + QVERIFY(object(rows, 3).contains("/MediaBox [0 0 2.0000 3.0000]")); + QCOMPARE(streamOf(object(rows, 4)), QByteArray("q\n2.0000 0 0 3.0000 0 0 cm /Im0 Do\nQ")); + // The mask's rows and columns where the pixels are. + QImage clear = BrushRaster::context(2, 3, false); + clear.setPixel(1, 0, qRgba(0, 0, 255, 255)); + clear.setPixel(0, 2, qRgba(0, 64, 0, 64)); + const QByteArray masked = ImageExporter::pdfData(ExportRaster{clear, 72}); + QCOMPARE(streamOf(object(masked, 6)), QByteArray("\x00\xff\x00\x00\x40\x00", 6)); + QCOMPARE(streamOf(object(masked, 5)), QByteArray("\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00", 18)); // A soft edge goes straight: colour whole, alpha apart. QImage soft = BrushRaster::context(1, 1, false); soft.setPixelColor(0, 0, QColor(255, 0, 0, 128));