fix: edit document reconciliation and value validation

This commit is contained in:
zhoubinjia committed 2026-11-18 16:29:50 +08:00
1 parent 9c813e7c05
commit 1b7877df71
8 files changed
+105 -20

No files matched your search

@@ -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<int>(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<double> stops = value.number();
if (stops && !stock.supportsDevelopment(static_cast<float>(*stops))) {
@@ -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);
@@ -631,13 +631,16 @@ std::string shutterName(double seconds) {
return buffer;
}
std::vector<EditorMenuChoice> viewingLights(PrintPaper paper) {
const std::vector<EditorMenuChoice>& 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<EditorMenuChoice> 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<EditorMenuChoice> 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<EditorMenuChoice> 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<EditorMenuChoice> 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<EditorControl>& all() {
@@ -33,7 +33,7 @@ const std::vector<double>& shutterLadder();
std::vector<double> shutterTimes(const FilmStock* stock);
std::string shutterName(double seconds);
std::vector<EditorMenuChoice> viewingLights(PrintPaper paper);
const std::vector<EditorMenuChoice>& viewingLights(PrintPaper paper);
const std::vector<EditorControl>& all();
@@ -294,6 +294,7 @@ struct FilmStock {
/// The exact non-reference conditions this stock can render, in slider order.
std::vector<float> supportedDevelopmentStops() const;
bool hasDevelopmentConditions() const;
std::optional<float> matchingDevelopmentStop(float stops) const;
bool supportsDevelopment(float stops) const;
/// The stock after the requested development; throws FilmDevelopmentError.
FilmStock pushed(float stops) const;
@@ -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 <charconv>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
@@ -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<float>(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<std::vector<EditorMenuChoice>>(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<EditorMenuChoice> 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<std::string> 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));
}
+7 -1
View File
@@ -47,8 +47,14 @@ std::vector<float> FilmStock::supportedDevelopmentStops() const {
bool FilmStock::hasDevelopmentConditions() const { return !supportedDevelopmentStops().empty(); }
std::optional<float> 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 {
@@ -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 =