From 1b7877df718be077ea792c36ed3773ce3311bbea Mon Sep 17 00:00:00 2001 From: zhoubinjia Date: Wed, 18 Nov 2026 16:29:50 +0800 Subject: [PATCH] fix: edit document reconciliation and value validation --- .../main/cpp/fotufilm/core/EditDocument.cpp | 21 +++++- app/src/main/cpp/fotufilm/core/EditDocument.h | 3 + .../fotufilm/core/EditorControlCatalogue.cpp | 15 ++-- .../fotufilm/core/EditorControlCatalogue.h | 2 +- app/src/main/cpp/fotufilm/core/FilmStock.h | 1 + .../cpp/fotufilm/core/FotufilmControls.cpp | 70 ++++++++++++++++--- app/src/main/cpp/fotufilm/core/PushPull.cpp | 8 ++- .../ui/components/FotufilmControls.kt | 5 +- 8 files changed, 105 insertions(+), 20 deletions(-) diff --git a/app/src/main/cpp/fotufilm/core/EditDocument.cpp b/app/src/main/cpp/fotufilm/core/EditDocument.cpp index 2a342055..d9a3f180 100644 --- a/app/src/main/cpp/fotufilm/core/EditDocument.cpp +++ b/app/src/main/cpp/fotufilm/core/EditDocument.cpp @@ -56,6 +56,22 @@ EditDocument EditDocument::decode(const JsonValue& json) { EditDocument EditDocument::decode(const std::string& text) { return decode(parseJson(text)); } +const EditDocument::Value* EditDocument::effectiveValue(EditorControlField field, PrintPaper paper) const { + const Value* value = (*this)[field]; + if (field != EditorControlField::printLight || !value || value->type != Value::Type::choice) return value; + + const auto offersLight = [&](PrintPaper medium) { + const auto& lights = EditorControlCatalogue::viewingLights(medium); + return std::any_of(lights.begin(), lights.end(), [&](const EditorMenuChoice& light) { + return light.id == value->choice; + }); + }; + if (offersLight(paper)) return value; + // Only a known lamp can be dormant. Leave malformed choices for engineValue to diagnose. + const auto& media = allPrintPapers(); + return std::any_of(media.begin(), media.end(), offersLight) ? nullptr : value; +} + bool EditDocument::isComposed(EditorControlField field) { switch (field) { case EditorControlField::gauge: @@ -110,12 +126,13 @@ FotufilmEngine::Options EditDocument::options(const FilmStock& stock, for (const auto& entry : sorted) { const EditorControlField field = entry.second; if (isComposed(field)) continue; - const Value& input = values.at(field); + const Value* input = effectiveValue(field, paper); + if (!input) continue; auto found = stockControls.find(static_cast(field)); const EditorControl* control = found != stockControls.end() ? &found->second : EditorControlCatalogue::control(field); if (!control || !control->binding) throw Failure(std::string("Unsupported control: ") + rawValue(field)); - const EditorControlValue value = engineValue(input, *control, stock, paper); + const EditorControlValue value = engineValue(*input, *control, stock, paper); if (field == EditorControlField::push) { std::optional stops = value.number(); if (stops && !stock.supportsDevelopment(static_cast(*stops))) { diff --git a/app/src/main/cpp/fotufilm/core/EditDocument.h b/app/src/main/cpp/fotufilm/core/EditDocument.h index fb8933bf..b8977e43 100644 --- a/app/src/main/cpp/fotufilm/core/EditDocument.h +++ b/app/src/main/cpp/fotufilm/core/EditDocument.h @@ -70,6 +70,9 @@ public: return found == values.end() ? nullptr : &found->second; } + /// A viewing lamp selected on another medium stays in the edit until that medium can use it. + const Value* effectiveValue(EditorControlField field, PrintPaper paper) const; + /// `EditDocument.init(from:)` over a JSON object. Throws DecodingError on malformed text and /// Failure on an unknown field. static EditDocument decode(const JsonValue& json); diff --git a/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.cpp b/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.cpp index cdb8fb5f..239de8b7 100644 --- a/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.cpp +++ b/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.cpp @@ -631,13 +631,16 @@ std::string shutterName(double seconds) { return buffer; } -std::vector viewingLights(PrintPaper paper) { +const std::vector& viewingLights(PrintPaper paper) { if (!acceptsViewingIlluminant(paper)) { - return {EditorMenuChoice(std::nullopt, "Display White \xC2\xB7 D65", - "The display's fixed white point; no viewing lamp is added", std::string("reference"))}; + static const std::vector display = { + EditorMenuChoice(std::nullopt, "Display White \xC2\xB7 D65", + "The display's fixed white point; no viewing lamp is added", std::string("reference")), + }; + return display; } if (isProjected(paper)) { - return { + static const std::vector projected = { EditorMenuChoice(std::nullopt, "Reference Projector \xC2\xB7 Xenon 5400 K", "How a cinema release print is intended to look on screen", std::string("reference")), EditorMenuChoice(6504.0, "Light Table \xC2\xB7 D65", "See the film directly under cool daylight", @@ -647,14 +650,16 @@ std::vector viewingLights(PrintPaper paper) { EditorMenuChoice(2856.0, "Tungsten \xC2\xB7 2856 K", "See the print under a warm indoor lamp", std::string("tungsten")), }; + return projected; } - return { + static const std::vector reflective = { EditorMenuChoice(std::nullopt, "Reference Booth \xC2\xB7 D50", "Standard neutral light for judging photo prints", std::string("reference")), EditorMenuChoice(6504.0, "Daylight \xC2\xB7 D65", "See the print in cooler daylight", std::string("d65")), EditorMenuChoice(2856.0, "Tungsten \xC2\xB7 2856 K", "See the print under a warm indoor lamp", std::string("tungsten")), }; + return reflective; } const std::vector& all() { diff --git a/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.h b/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.h index 1c99a75b..468a9231 100644 --- a/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.h +++ b/app/src/main/cpp/fotufilm/core/EditorControlCatalogue.h @@ -33,7 +33,7 @@ const std::vector& shutterLadder(); std::vector shutterTimes(const FilmStock* stock); std::string shutterName(double seconds); -std::vector viewingLights(PrintPaper paper); +const std::vector& viewingLights(PrintPaper paper); const std::vector& all(); diff --git a/app/src/main/cpp/fotufilm/core/FilmStock.h b/app/src/main/cpp/fotufilm/core/FilmStock.h index 8d1eccce..102e957e 100644 --- a/app/src/main/cpp/fotufilm/core/FilmStock.h +++ b/app/src/main/cpp/fotufilm/core/FilmStock.h @@ -294,6 +294,7 @@ struct FilmStock { /// The exact non-reference conditions this stock can render, in slider order. std::vector supportedDevelopmentStops() const; bool hasDevelopmentConditions() const; + std::optional matchingDevelopmentStop(float stops) const; bool supportsDevelopment(float stops) const; /// The stock after the requested development; throws FilmDevelopmentError. FilmStock pushed(float stops) const; diff --git a/app/src/main/cpp/fotufilm/core/FotufilmControls.cpp b/app/src/main/cpp/fotufilm/core/FotufilmControls.cpp index 0b0bb474..18733116 100644 --- a/app/src/main/cpp/fotufilm/core/FotufilmControls.cpp +++ b/app/src/main/cpp/fotufilm/core/FotufilmControls.cpp @@ -14,7 +14,7 @@ // "menu"?: "fixed" | dynamic menu name, "choices"?: [{"id", "label", "detail", "value" (number | null)}], // "defaultChoice"?: id, // "value": the document's value, else the resting value or default choice, -// "isSet": whether the document carries the field, "moved"?: slider/chips off neutral, +// "isSet": whether the document sets the field for this medium, "moved"?: slider/chips off neutral, // "admitted": the stock admits it, "offered": the current medium offers it, // "available": both // }] @@ -22,6 +22,7 @@ #include "FotufilmControls.h" #include +#include #include #include #include @@ -158,17 +159,68 @@ FilmStock registeredStock(const std::string& stockID) { return *stock; } +/// Reconcile persisted edits against the current catalogue before either UI or rendering reads them. +void reconcileDocument(EditDocument& document, const FilmStock& stock) { + using Field = EditorControlField; + using Type = EditDocument::Value::Type; + auto paperField = document.values.find(Field::paper); + if (paperField != document.values.end() && paperField->second.type == Type::choice && + !printPaperPreset(paperField->second.choice)) { + document.values.erase(paperField); + } + const auto selectedPaper = document.choice(Field::paper); + const PrintPaper paper = resolved(selectedPaper ? *printPaperPreset(*selectedPaper) : defaultPrintPaper(stock), stock); + for (auto entry = document.values.begin(); entry != document.values.end();) { + const Field field = entry->first; + auto& value = entry->second; + const EditorControl* control = EditorControlCatalogue::control(field); + bool supported = true; + if (field == Field::gauge && value.type == Type::choice) { + supported = FilmFormat::preset(value.choice).has_value(); + } else if (field == Field::lensFilterStack && value.type == Type::choices) { + supported = value.choices.empty(); + } else if (field == Field::metering) { + // Metering only qualifies lens filters, which this port does not implement. + } else if (!control || (!control->binding && !EditDocument::isComposed(field))) { + supported = false; + } else if (field == Field::push && value.type == Type::number) { + const auto stop = stock.matchingDevelopmentStop(static_cast(value.number)); + supported = stop.has_value(); + if (stop) value.number = *stop; + } else if (const auto* scale = control->kind.scale(); scale && value.type == Type::number) { + value.number = std::clamp(value.number, scale->admittedLower, scale->admittedUpper); + } else if (control->kind.type == EditorControlKind::Type::menu && value.type == Type::choice && + field != Field::paper && field != Field::gauge) { + const auto menu = field == Field::sceneLight + ? std::optional>(EditorControlCatalogue::sourceLights()) + : WebProfileCatalogue::choices(field, &stock, paper); + supported = menu && std::any_of(menu->begin(), menu->end(), [&](const EditorMenuChoice& item) { + return item.id == value.choice; + }); + } else if (control->kind.type == EditorControlKind::Type::curve && value.type == Type::curve) { + const auto& curve = control->kind.curveValue; + supported = value.curve.size() == curve.handles.size(); + for (double& point : value.curve) { + point = std::clamp(point, curve.rangeLower, curve.rangeUpper); + } + } + if (supported) ++entry; + else entry = document.values.erase(entry); + } +} + /// Decodes the document and takes out its `stock`, which must name `stockID`. -EditDocument documentFor(const std::string& stockID, const std::string& documentJSON) { +EditDocument documentFor(const std::string& stockID, const FilmStock& stock, const std::string& documentJSON) { EditDocument document = documentJSON.empty() ? EditDocument() : EditDocument::decode(documentJSON); - auto stock = document.values.find(EditorControlField::stock); - if (stock != document.values.end()) { - if (stock->second.type != EditDocument::Value::Type::choice || stock->second.choice != stockID) { + auto stockField = document.values.find(EditorControlField::stock); + if (stockField != document.values.end()) { + if (stockField->second.type != EditDocument::Value::Type::choice || stockField->second.choice != stockID) { throw EditDocument::Failure("The document's film is not " + stockID + "."); } - document.values.erase(stock); + document.values.erase(stockField); } + reconcileDocument(document, stock); return document; } @@ -242,7 +294,7 @@ std::vector dynamicChoices(EditorDynamicMenu menu, const FilmS std::string fotufilmCatalogueJSON(const std::string& stockID, const std::string& documentJSON) { const FilmStock stock = registeredStock(stockID); - const EditDocument document = documentFor(stockID, documentJSON); + const EditDocument document = documentFor(stockID, stock, documentJSON); // The medium the gates read: the document's choice resolved for this film, as options(for:) does. std::optional paperChoice = document.choice(EditorControlField::paper); @@ -292,7 +344,7 @@ std::string fotufilmCatalogueJSON(const std::string& stockID, const std::string& const EditorControl& control = adjusted ? *adjusted : base; const bool isAdmitted = adjusted != nullptr; const bool isOffered = EditorControlCatalogue::medium(paper, control.field, &stock, digitalReference); - const EditDocument::Value* value = document[control.field]; + const EditDocument::Value* value = document.effectiveValue(control.field, paper); writer.beginObject(); writer.field("field", rawValue(control.field)); @@ -408,7 +460,7 @@ std::string fotufilmCatalogueJSON(const std::string& stockID, const std::string& FotufilmEngine::Options optionsFromDocument(const std::string& stockID, const std::string& documentJSON) { const FilmStock stock = registeredStock(stockID); - const EditDocument document = documentFor(stockID, documentJSON); + const EditDocument document = documentFor(stockID, stock, documentJSON); return document.options(stock, FilmFormat::nativeID(stockID)); } diff --git a/app/src/main/cpp/fotufilm/core/PushPull.cpp b/app/src/main/cpp/fotufilm/core/PushPull.cpp index 6440c135..0286854a 100644 --- a/app/src/main/cpp/fotufilm/core/PushPull.cpp +++ b/app/src/main/cpp/fotufilm/core/PushPull.cpp @@ -47,8 +47,14 @@ std::vector FilmStock::supportedDevelopmentStops() const { bool FilmStock::hasDevelopmentConditions() const { return !supportedDevelopmentStops().empty(); } +std::optional FilmStock::matchingDevelopmentStop(float stops) const { + if (stops == 0) return 0.0f; + if (const auto* condition = developmentCondition(stops)) return condition->stops; + return std::nullopt; +} + bool FilmStock::supportsDevelopment(float stops) const { - return stops == 0 || developmentCondition(stops) != nullptr; + return matchingDevelopmentStop(stops).has_value(); } FilmStock FilmStock::pushed(float stops) const { diff --git a/app/src/main/java/com/hinnka/mycamera/ui/components/FotufilmControls.kt b/app/src/main/java/com/hinnka/mycamera/ui/components/FotufilmControls.kt index 839df030..bb76237f 100644 --- a/app/src/main/java/com/hinnka/mycamera/ui/components/FotufilmControls.kt +++ b/app/src/main/java/com/hinnka/mycamera/ui/components/FotufilmControls.kt @@ -376,8 +376,9 @@ private fun FotufilmMenu( /** Sets a slider's value; the resting value is the field's absence, as upstream stores it. */ private fun FotufilmDocument.withNumber(control: FotufilmCatalogue.Control, value: Double): FotufilmDocument { val kind = control.kind as FotufilmCatalogue.Kind.Slider - return if (value == kind.scale.neutral) without(control.field) - else with(control.field, FotufilmDocument.Value.Number(value)) + val bounded = value.coerceIn(kind.scale.min, kind.scale.max) + return if (bounded == kind.scale.neutral) without(control.field) + else with(control.field, FotufilmDocument.Value.Number(bounded)) } private fun FotufilmDocument.withValue(control: FotufilmCatalogue.Control, value: FotufilmDocument.Value?): FotufilmDocument =