From 2401f81425fa47668a7ed79f1169aac0a88c32d8 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Sun, 22 Nov 2026 20:48:43 +0200 Subject: [PATCH 1/5] Show one drawing as a stamp, a mark in a widget and a push button's icon A picture is the same thing wherever it shows: a form the document holds once, with a small placement around it. Only stamps worked that way. A mark in a signature field was imported again for every widget, so the same 17 KB picture in ten fields saved as 504 KB, where ten stamps of it save as 56 KB. And the fork could not write a push button's icon at all, which is what a field that holds a picture is in a file: /MK /TP 1, the picture as a form XObject in /MK /I, and an /AP /N that draws it. Drawings serve widgets now: - EPDFAnnot_SetDrawing (was EPDFAnnot_SetStampDrawing) places a drawing in a widget as it does on a stamp: a new wrapper, fitted and turned exactly as EPDFAnnot_UpdateAppearanceToRect fits a widget's mark (/Rect and /MK /R, neither written). It checks the drawing and the box before it opens the annotation for writing. It takes a stamp and a widget. - EPDFAnnot_GetDrawing (was EPDFAnnot_GetStampDrawing) reads a widget's wrapper too, a mark placed the old way included. - EPDFAnnot_SetMKIcon, GetMKIcon, ClearMKIcon and HasMKIcon name, read and remove a widget's /MK icons (/I, /RI, /IX) by drawing. - EPDFDoc_GetDrawings (was EPDFDoc_GetStampDrawings) also lists the drawings widgets' marks place and the forms /MK names as icons, so a reopened file's pictures are found and placed again. An appearance whose resources name no XObject is not decoded for it, and it writes nothing. A drawing is only read by a placement, so one an earlier revision holds can be placed after a signature without touching it. The generator draws a push button with /MK /TP 1 from its normal icon (GenerateIconButtonAP): the background and border, then the icon inside the border's width, or in the whole button when /MK /IF /FB says so, clipped there. The fit is the file's /MK /IF (ISO 32000-2 Table 250), and the table's defaults without one. It is read through upstream's CPDF_IconFit and worked out in ours: upstream's placement puts an icon whose fit names no /A at the bottom left. The icon's box is its /BBox through its /Matrix. The button is laid out in the frame its /MK /R gives. The appearance names the icon /FRM, as Acrobat does, so the file holds the picture once. It draws no caption and needs no /DA, it is a new stream, and the generator can draw it in memory as well. The engine's calls to the three renamed functions change with the pin. A push button in the layouts that show a caption and an icon (/MK /TP 2 to 6) is drawn as it was: the caption, without the icon. --- core/fpdfdoc/cpdf_appearancegenerator.h | 4 +- .../cpdf_appearancegenerator_forms.cpp | 185 +- docs/embedpdf/fork-ledger.md | 34 +- fpdfsdk/BUILD.gn | 4 + fpdfsdk/epdf_annot.cpp | 173 +- fpdfsdk/epdf_annot_embeddertest.cpp | 41 +- fpdfsdk/epdf_c_api_test.c | 10 +- fpdfsdk/epdf_signature_embeddertest.cpp | 196 ++ fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 2225 +++++++++++++++++ fpdfsdk/epdf_wrapped_appearance.cpp | 11 + public/epdf_annot.h | 146 +- testing/embedpdf_layer_mode_writers.txt | 30 + .../embedpdf_widget_marks_from_page.pdf | Bin 0 -> 2469 bytes 13 files changed, 2960 insertions(+), 99 deletions(-) create mode 100644 fpdfsdk/epdf_widget_drawing_embeddertest.cpp create mode 100644 testing/resources/embedpdf_widget_marks_from_page.pdf diff --git a/core/fpdfdoc/cpdf_appearancegenerator.h b/core/fpdfdoc/cpdf_appearancegenerator.h index 0834af454f..323a18022e 100644 --- a/core/fpdfdoc/cpdf_appearancegenerator.h +++ b/core/fpdfdoc/cpdf_appearancegenerator.h @@ -37,7 +37,9 @@ using fxge::BlendMode; class CPDF_AppearanceGenerator { public: // kPushButton: the button's background and border, and its caption (/MK - // /CA) centred in the /DA font and colour. + // /CA) centred in the /DA font and colour. One that shows its icon alone + // (/MK /TP 1) draws the normal icon (/MK /I) inside the border, fitted as + // /MK /IF says, and no caption. enum FormType { kTextField, kComboBox, kListBox, kPushButton }; static void GenerateFormAP(CPDF_Document* doc, diff --git a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp index 57eec08c92..e445c38f75 100644 --- a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp +++ b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp @@ -10,6 +10,7 @@ // See docs/embedpdf/fork-ledger.md, rule own-appearance-generator. EmbedPDF's // changes are Copyright 2026 CloudPDF LTD. +#include #include #include #include @@ -37,6 +38,7 @@ #include "core/fpdfdoc/cpdf_color_utils.h" #include "core/fpdfdoc/cpdf_defaultappearance.h" #include "core/fpdfdoc/cpdf_formfield.h" +#include "core/fpdfdoc/cpdf_iconfit.h" #include "core/fpdfdoc/cpdf_interactiveform.h" #include "core/fpdfdoc/cpdf_richtextlayout.h" #include "core/fxcrt/fx_string_wrappers.h" @@ -478,10 +480,89 @@ void BuildCheckboxBoxStream(fxcrt::ostringstream& stream, } } -// A push button: background and border as a text field draws them, and the -// caption (/MK /CA) on one line, centred both ways, in the /DA font and -// colour. An icon (/MK /I) is not drawn, so the caption takes the whole -// button whatever /MK /TP says. +// How a push button lays out its caption and its icon: /MK /TP (ISO 32000-2 +// Table 192). A number the table doesn't list is 0, the caption alone, as an +// absent one is. +constexpr int kButtonShowsIconAlone = 1; + +int ButtonTextPosition(const CPDF_Dictionary* annot_dict) { + RetainPtr mk = annot_dict->GetDictFor("MK"); + const int position = mk ? mk->GetIntegerFor("TP") : 0; + return position >= 0 && position <= 6 ? position : 0; +} + +// The box an icon takes where it is drawn: its /BBox through its /Matrix. +CFX_FloatRect IconBox(const CPDF_Stream* icon) { + RetainPtr dict = icon->GetDict(); + CFX_FloatRect box = dict->GetRectFor("BBox"); + box.Normalize(); + box = dict->GetMatrixFor("Matrix").TransformRect(box); + box.Normalize(); + return box; +} + +// The matrix that draws an icon whose box is `icon` in `plate`, as the icon +// fit dictionary says (ISO 32000-2 Table 250): when the icon is scaled (/SW), +// with or without its proportions (/S), and how the room left over is shared +// between its left and its bottom (/A). Without a dictionary these are the +// table's defaults: always scaled, keeping its proportions, in the middle. +// Nothing for an icon or a plate without area. +std::optional IconPlacement(const CFX_FloatRect& icon, + const CFX_FloatRect& plate, + const CPDF_IconFit& fit) { + const float icon_width = icon.Width(); + const float icon_height = icon.Height(); + const float plate_width = plate.Width(); + const float plate_height = plate.Height(); + if (!(icon_width > 0) || !(icon_height > 0) || !(plate_width > 0) || + !(plate_height > 0)) { + return std::nullopt; + } + + float scale_x = 1; + float scale_y = 1; + switch (fit.GetScaleMethod()) { + case CPDF_IconFit::ScaleMethod::kAlways: + scale_x = plate_width / icon_width; + scale_y = plate_height / icon_height; + break; + case CPDF_IconFit::ScaleMethod::kBigger: + if (icon_width > plate_width) { + scale_x = plate_width / icon_width; + } + if (icon_height > plate_height) { + scale_y = plate_height / icon_height; + } + break; + case CPDF_IconFit::ScaleMethod::kSmaller: + if (icon_width < plate_width) { + scale_x = plate_width / icon_width; + } + if (icon_height < plate_height) { + scale_y = plate_height / icon_height; + } + break; + case CPDF_IconFit::ScaleMethod::kNever: + break; + } + if (fit.IsProportionalScale()) { + scale_x = scale_y = std::min(scale_x, scale_y); + } + + const CFX_PointF share = fit.GetIconBottomLeftPosition(); + const float left = plate.left + (plate_width - icon_width * scale_x) * + std::clamp(share.x, 0.0f, 1.0f); + const float bottom = plate.bottom + (plate_height - icon_height * scale_y) * + std::clamp(share.y, 0.0f, 1.0f); + return CFX_Matrix(scale_x, 0, 0, scale_y, left - icon.left * scale_x, + bottom - icon.bottom * scale_y); +} + +// A push button in every layout but the icon alone, which +// GenerateIconButtonAP() draws: background and border as a text field draws +// them, and the caption (/MK /CA) on one line, centred both ways, in the /DA +// font and colour. In the layouts that show a caption and an icon (/MK /TP 2 +// to 6) the icon is not drawn, and the caption takes the whole button. void GeneratePushButtonFormAP(fxcrt::ostringstream& app_stream, const CPDF_Dictionary* annot_dict, const CFX_FloatRect& bbox, @@ -672,22 +753,100 @@ void WriteCheckSymbol(fxcrt::ostringstream& stream, stream << (stroked ? "S\n" : "f\n") << "Q\n"; } -uint32_t CreateFormXObjectStream(CPDF_Document* doc, - fxcrt::ostringstream& content, - const CFX_FloatRect& bbox, - const CFX_Matrix& matrix) { +// The dictionary of a form XObject that draws in `bbox`, placed by `matrix`. +RetainPtr NewFormXObjectDict( + const CFX_FloatRect& bbox, + const CFX_Matrix& matrix, + RetainPtr resources = nullptr) { auto stream_dict = pdfium::MakeRetain(); stream_dict->SetNewFor("FormType", 1); stream_dict->SetNewFor("Type", "XObject"); stream_dict->SetNewFor("Subtype", "Form"); stream_dict->SetMatrixFor("Matrix", matrix); stream_dict->SetRectFor("BBox", bbox); + if (resources) { + stream_dict->SetFor("Resources", std::move(resources)); + } + return stream_dict; +} - auto stream = doc->NewIndirect(std::move(stream_dict)); +uint32_t CreateFormXObjectStream( + CPDF_Document* doc, + fxcrt::ostringstream& content, + const CFX_FloatRect& bbox, + const CFX_Matrix& matrix, + RetainPtr resources = nullptr) { + auto stream = doc->NewIndirect( + NewFormXObjectDict(bbox, matrix, std::move(resources))); stream->SetDataFromStringstreamAndRemoveFilter(&content); return stream->GetObjNum(); } +// A push button that shows its icon alone (/MK /TP 1): background and border +// as every push button has them, and the normal icon (/MK /I), a form +// XObject of its own, drawn inside the border. The icon is fitted into the +// button less the border's width at each edge, or into the whole button when +// /MK /IF /FB says so (IconPlacement()), and clipped to that plate. The +// appearance names it /FRM in its resources, as Acrobat writes an image +// field, so the file holds the picture once, for /MK and for /AP. The button +// is laid out in the frame its quarter turn gives (/MK /R), as every field's +// contents are. +// +// It draws no caption, so it needs no /DA and no font, and without an icon +// it is the box alone. Written into the file, the appearance is a new +// stream, as a check box's are: the old /N may be another widget's too, or +// the icon itself. The old /N is not deleted, nor is an icon it drew: a save +// writes only what the document still reaches. +void GenerateIconButtonAP(APGenerationTarget* target, + const CPDF_Dictionary* annot_dict) { + CPDF_Document* const doc = target->doc; + RetainPtr mk_dict = annot_dict->GetDictFor("MK"); + const AppearanceCharacteristics mk = + GetAppearanceCharacteristics(mk_dict.Get()); + const BorderStyleInfo bs = GetBorderStyleInfo(annot_dict->GetDictFor("BS")); + const AnnotationDimensionsAndColor dims = + GetAnnotationDimensionsAndColor(annot_dict); + + fxcrt::ostringstream content; + content << "q\n"; + BuildCheckboxBoxStream(content, mk, bs, dims.bbox); + content << "Q\n"; + + // An icon is drawn by reference, so only one that is an object of its own + // can be: a file always holds a stream so. + RetainPtr resources; + RetainPtr icon = + mk_dict ? mk_dict->GetStreamFor(pdfium::appearance::kI) : nullptr; + if (icon && icon->GetObjNum() != 0) { + const CPDF_IconFit fit(mk_dict->GetDictFor("IF")); + CFX_FloatRect plate = dims.bbox; + if (!fit.GetFittingBounds()) { + plate.Deflate(bs.width, bs.width); + } + if (const std::optional placement = + IconPlacement(IconBox(icon.Get()), plate, fit)) { + content << "q\n"; + WriteRect(content, plate) << " re W n\n"; + WriteMatrix(content, placement.value()) << " cm\n/FRM Do\nQ\n"; + resources = doc->New(); + resources->SetNewFor("XObject") + ->SetNewFor("FRM", doc, icon->GetObjNum()); + } + } + + if (!target->IsPersistent()) { + // Drawn in memory: a stream of no document, and nothing is written. + target->normal_stream = pdfium::MakeRetain( + NewFormXObjectDict(dims.bbox, dims.matrix, std::move(resources))); + target->normal_stream->SetDataFromStringstream(&content); + return; + } + const uint32_t normal_obj_num = CreateFormXObjectStream( + doc, content, dims.bbox, dims.matrix, std::move(resources)); + target->persistent_annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP) + ->SetNewFor("N", doc, normal_obj_num); +} + bool GenerateFormAPToTarget(APGenerationTarget* target, CPDF_Dictionary* annot_dict, CPDF_AppearanceGenerator::FormType type, @@ -698,6 +857,14 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, return false; } + // A push button that shows its icon alone draws no text: it needs no /DA + // and no font. + if (type == CPDF_AppearanceGenerator::kPushButton && + ButtonTextPosition(annot_dict) == kButtonShowsIconAlone) { + GenerateIconButtonAP(target, annot_dict); + return true; + } + RetainPtr form_dict = root_dict->GetDictFor("AcroForm"); RetainPtr ephemeral_form_dict; diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index a04c153b09..545f0a03f1 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -340,13 +340,21 @@ measurement, redaction overlays, form fields. Mixed into upstream's file, it was file. Apart, upstream's file merges cleanly. **Where.** `cpdf_appearancegenerator*` and its unit test; the calls in `cpdf_annot.cpp`, upstream's `FPDFAnnot_AppendObject` and `FPDFAnnot_SetFontColor` in `fpdf_annot.cpp`, -`epdf_annot.cpp`, `epdf_form.cpp`, `epdf_redact.cpp` and the rich text writer. +`epdf_annot.cpp`, `epdf_form.cpp`, `epdf_redact.cpp` and the rich text writer. A push button that +shows its icon alone (`/MK /TP 1`, an image field) is drawn from `/MK /I` by +`GenerateIconButtonAP`. It reads the icon fit (`/MK /IF`) through upstream's `CPDF_IconFit` and +places the icon itself (`IconPlacement`), by ISO 32000-2 Table 250: upstream's own placement +(`CPDF_IconFit::GetScale` and `GetImageOffset`, used by its form filler) puts an icon whose fit +names no `/A` at the bottom left, where the table's default is the middle, and reads an icon +narrower than one unit as one unit wide. **Guarded by.** `check-fork-tags.py` fails when upstream's files differ from upstream (`fork-pristine.txt`); `upstream-preview.py` lists new upstream callers of `CPDF_GenerateAP::`, and -upstream's commits to its generator. +upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for a button's icon. **When upstream touches this.** A commit to `cpdf_generateap.*`: port it to ours or skip it, with the reason in the sync report. A new upstream caller of `CPDF_GenerateAP::`: route it to ours, or -accept it in the report. +accept it in the report. A change to what `CPDF_IconFit`'s readers return (`GetScaleMethod`, +`IsProportionalScale`, `GetIconBottomLeftPosition`, `GetFittingBounds`): `IconPlacement` reads the +file through them. ## Text @@ -374,11 +382,21 @@ calls FreeType. ### annot-appearance **Rule.** Our appearance model: appearance states, box kinds drawn in a frame they measure, dashes, -note icons, canonical stamp drawings, widget rendering, and the hooks into our appearance -generator. -**Why.** Annotations look the same in every viewer and survive edits. -**Where.** `cpdf_annot.cpp`, `cpdf_annotlist.cpp`, `cpdf_annotcontext.cpp`. -**Guarded by.** `epdf_appearance_state_embeddertest.cpp`, the annotation embedder tests. +note icons, canonical drawings, widget rendering, and the hooks into our appearance generator. A +drawing is a picture the document holds once: a form XObject, known by the digest of its canonical +bytes. A stamp's artwork and a mark in a widget each place it with a wrapper of their own, and a +push button's `/MK` names it as an icon, so one drawing serves all three. A drawing is never +written after it is made: placing one only reads it. +**Why.** Annotations look the same in every viewer and survive edits. A picture shown in many +places is in the file once, and a place added after a signature leaves the drawing the signature +covers as it was. +**Where.** `cpdf_annot.cpp`, `cpdf_annotlist.cpp`, `cpdf_annotcontext.cpp`; the drawing calls in +`epdf_annot.cpp` (`EPDFDoc_CanonicalDrawing`, `EPDFDoc_ImportDrawing`, `EPDFDoc_ExportDrawing`, +`EPDFDoc_GetDrawings`, `EPDFAnnot_SetDrawing`, `EPDFAnnot_GetDrawing`, `EPDFAnnot_SetMKIcon`, +`EPDFAnnot_GetMKIcon`) and the wrapper in `epdf_wrapped_appearance.{h,cpp}`. +**Guarded by.** `epdf_appearance_state_embeddertest.cpp`, the annotation embedder tests, +`epdf_widget_drawing_embeddertest.cpp`, and +`EPDFSignatureEmbedderTest.PlacingADrawingAgainLeavesTheSignedRevisionAlone`. **When upstream touches this.** Annotation drawing and appearance lookup. ### forms-model diff --git a/fpdfsdk/BUILD.gn b/fpdfsdk/BUILD.gn index 1ab04ad4d4..976c7ca96e 100644 --- a/fpdfsdk/BUILD.gn +++ b/fpdfsdk/BUILD.gn @@ -260,6 +260,10 @@ pdfium_embeddertest_source_set("embeddertests") { # EmbedPDF: covers annotation calls that read a page's dictionaries # without loading the page. "epdf_annot_raw_embeddertest.cpp", + # EmbedPDF: covers drawings in widgets (public/epdf_annot.h): a push + # button's icons and the appearance drawn from the normal one, a mark a + # wrapper places, and one drawing shared with stamps. + "epdf_widget_drawing_embeddertest.cpp", # EmbedPDF: helpers for tests that compare a document before and after # writes. "epdf_edit_test_util.cpp", diff --git a/fpdfsdk/epdf_annot.cpp b/fpdfsdk/epdf_annot.cpp index 27281cf708..a8ad9b6c4a 100644 --- a/fpdfsdk/epdf_annot.cpp +++ b/fpdfsdk/epdf_annot.cpp @@ -3728,12 +3728,17 @@ static RetainPtr NewDrawingWrapper(CPDF_Document* doc, return wrapper; } -// The drawing our wrapper places on a stamp, or null: a stamp without our -// wrapper, or not a stamp. -static RetainPtr StampDrawingOf( +// The drawing our wrapper places on a stamp or in a widget (a signature +// field's mark), or null: an appearance without our wrapper, or another kind +// of annotation. +static RetainPtr PlacedDrawingOf( const CPDF_Dictionary* annot_dict) { - if (!annot_dict || - annot_dict->GetNameFor(pdfium::annotation::kSubtype) != "Stamp") { + if (!annot_dict) { + return nullptr; + } + const ByteString subtype = + annot_dict->GetNameFor(pdfium::annotation::kSubtype); + if (subtype != "Stamp" && subtype != "Widget") { return nullptr; } RetainPtr ap = @@ -4017,9 +4022,9 @@ EPDFDoc_ExportDrawing(FPDF_DOCUMENT document, unsigned int drawing) { } FPDF_EXPORT unsigned long FPDF_CALLCONV -EPDFDoc_GetStampDrawings(FPDF_DOCUMENT document, - unsigned int* buffer, - unsigned long buflen) { +EPDFDoc_GetDrawings(FPDF_DOCUMENT document, + unsigned int* buffer, + unsigned long buflen) { CPDF_Document* doc = CPDFDocumentFromFPDFDocument(document); if (!doc) { return 0; @@ -4028,6 +4033,12 @@ EPDFDoc_GetStampDrawings(FPDF_DOCUMENT document, CPDF_DocumentViewScope document_view(doc); std::vector drawings; std::set seen; + auto add = [&drawings, &seen](const CPDF_Stream* drawing) { + if (drawing && drawing->GetObjNum() != 0 && + seen.insert(drawing->GetObjNum()).second) { + drawings.push_back(drawing->GetObjNum()); + } + }; for (int i = 0; i < doc->GetPageCount(); ++i) { RetainPtr page = doc->GetPageDictionary(i); RetainPtr annots = @@ -4037,9 +4048,22 @@ EPDFDoc_GetStampDrawings(FPDF_DOCUMENT document, } for (size_t j = 0; j < annots->size(); ++j) { RetainPtr annot = annots->GetDictAt(j); - RetainPtr drawing = StampDrawingOf(annot.Get()); - if (drawing && seen.insert(drawing->GetObjNum()).second) { - drawings.push_back(drawing->GetObjNum()); + // What a wrapper places: a stamp's artwork, a mark in a widget. + add(PlacedDrawingOf(annot.Get()).Get()); + // What a widget's /MK names as an icon: a form, whoever made it. + RetainPtr mk = + annot && annot->GetNameFor(pdfium::annotation::kSubtype) == "Widget" + ? annot->GetDictFor("MK") + : nullptr; + if (!mk) { + continue; + } + for (const char* key : {pdfium::appearance::kI, pdfium::appearance::kRI, + pdfium::appearance::kIX}) { + RetainPtr icon = mk->GetStreamFor(key); + if (icon && icon->GetDict()->GetNameFor("Subtype") == "Form") { + add(icon.Get()); + } } } } @@ -4053,40 +4077,47 @@ EPDFDoc_GetStampDrawings(FPDF_DOCUMENT document, return static_cast(drawings.size()); } -FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV -EPDFAnnot_SetStampDrawing(FPDF_ANNOTATION annot, - unsigned int drawing, - EPDF_STAMP_FIT fit) { +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetDrawing(FPDF_ANNOTATION annot, + unsigned int drawing, + EPDF_STAMP_FIT fit) { ScopedFPDFAnnotationView annot_view(annot); CPDF_AnnotContext* ctx = CPDFAnnotContextFromFPDFAnnotation(annot); - if (!ctx || drawing == 0 || FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_STAMP) { + const FPDF_ANNOTATION_SUBTYPE subtype = FPDFAnnot_GetSubtype(annot); + if (!ctx || drawing == 0 || + (subtype != FPDF_ANNOT_STAMP && subtype != FPDF_ANNOT_WIDGET)) { return false; } - RetainPtr ad = ctx->GetMutableAnnotDict(); CPDF_Document* doc = ctx->GetPage() ? ctx->GetPage()->GetDocument() : nullptr; - if (!ad || !doc) { + const CPDF_Dictionary* annot_dict = ctx->GetAnnotDict(); + if (!doc || !annot_dict) { return false; } + // The drawing and the box are checked before the annotation is opened for + // writing: a refused call changes nothing, and on a layer copies nothing + // up. The drawing is only read, so it can be one an earlier revision holds. RetainPtr form = ToStream(doc->GetOrParseIndirectObject(drawing)); - if (!form || form->GetDict()->GetNameFor("Subtype") != "Form") { + if (!form || form->GetDict()->GetNameFor("Subtype") != "Form" || + GetFormDisplayBox(form->GetDict().Get()).IsEmpty() || + !HasArea(ReadPlacementBox(annot_dict))) { return false; } - const PlacementBox box = ReadPlacementBox(ad.Get()); + RetainPtr ad = ctx->GetMutableAnnotDict(); RetainPtr wrapper = NewDrawingWrapper(doc, form.Get()); - if (!HasArea(box) || !wrapper) { + if (!ad || !wrapper) { return false; } + const PlacementBox box = ReadPlacementBox(ad.Get()); ad->GetOrCreateDictFor(pdfium::annotation::kAP); return PlaceWrapper(ctx, ad.Get(), doc, std::move(wrapper), GetFormDisplayBox(form->GetDict().Get()), box, fit); } FPDF_EXPORT unsigned int FPDF_CALLCONV -EPDFAnnot_GetStampDrawing(FPDF_ANNOTATION annot) { +EPDFAnnot_GetDrawing(FPDF_ANNOTATION annot) { ScopedFPDFAnnotationView annot_view(annot); RetainPtr drawing = - StampDrawingOf(GetAnnotDictFromFPDFAnnotation(annot)); + PlacedDrawingOf(GetAnnotDictFromFPDFAnnotation(annot)); return drawing ? drawing->GetObjNum() : 0; } @@ -4576,6 +4607,102 @@ EPDFAnnot_GetMKTextPosition(FPDF_ANNOTATION annot) { return mk ? mk->GetIntegerFor("TP") : 0; } +namespace { + +// The /MK key of icon |which| (EPDF_MK_ICON_*), or null. +const char* MKIconKey(int which) { + switch (which) { + case EPDF_MK_ICON_I: + return pdfium::appearance::kI; + case EPDF_MK_ICON_RI: + return pdfium::appearance::kRI; + case EPDF_MK_ICON_IX: + return pdfium::appearance::kIX; + default: + return nullptr; + } +} + +} // namespace + +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKIcon(FPDF_ANNOTATION annot, + int which, + unsigned int drawing) { + ScopedFPDFAnnotationView annot_view(annot); + const char* key = MKIconKey(which); + CPDF_AnnotContext* ctx = CPDFAnnotContextFromFPDFAnnotation(annot); + if (!key || !ctx || drawing == 0 || + FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_WIDGET) { + return false; + } + CPDF_Document* doc = ctx->GetPage() ? ctx->GetPage()->GetDocument() : nullptr; + if (!doc) { + return false; + } + // The drawing is only read: it can be one other widgets and stamps place, + // or one an earlier revision holds. + RetainPtr form = + ToStream(doc->GetOrParseIndirectObject(drawing)); + if (!form || form->GetDict()->GetNameFor("Subtype") != "Form") { + return false; + } + // Nothing to change: no write. + RetainPtr mk = WidgetMKFromFPDFAnnotation(annot); + RetainPtr named = mk ? mk->GetStreamFor(key) : nullptr; + if (named && named->GetObjNum() == drawing) { + return true; + } + RetainPtr annot_dict = ctx->GetMutableAnnotDict(); + if (!annot_dict) { + return false; + } + annot_dict->GetOrCreateDictFor("MK")->SetNewFor(key, doc, + drawing); + return true; +} + +FPDF_EXPORT unsigned int FPDF_CALLCONV +EPDFAnnot_GetMKIcon(FPDF_ANNOTATION annot, int which) { + ScopedFPDFAnnotationView annot_view(annot); + const char* key = MKIconKey(which); + RetainPtr mk = + key ? WidgetMKFromFPDFAnnotation(annot) : nullptr; + RetainPtr icon = mk ? mk->GetStreamFor(key) : nullptr; + return icon ? icon->GetObjNum() : 0; +} + +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_ClearMKIcon(FPDF_ANNOTATION annot, + int which) { + ScopedFPDFAnnotationView annot_view(annot); + const char* key = MKIconKey(which); + if (!key || FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_WIDGET) { + return false; + } + // Nothing to remove: no write. + RetainPtr mk = WidgetMKFromFPDFAnnotation(annot); + if (!mk || !mk->KeyExist(key)) { + return true; + } + RetainPtr dict = + GetMutableAnnotDictFromFPDFAnnotation(annot); + RetainPtr mutable_mk = + dict ? dict->GetMutableDictFor("MK") : nullptr; + if (!mutable_mk) { + return false; + } + mutable_mk->RemoveFor(key); + return true; +} + +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_HasMKIcon(FPDF_ANNOTATION annot, + int which) { + ScopedFPDFAnnotationView annot_view(annot); + const char* key = MKIconKey(which); + RetainPtr mk = + key ? WidgetMKFromFPDFAnnotation(annot) : nullptr; + return mk && mk->GetStreamFor(key); +} + FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKRotation(FPDF_ANNOTATION annot, int degrees) { ScopedFPDFAnnotationView annot_view(annot); diff --git a/fpdfsdk/epdf_annot_embeddertest.cpp b/fpdfsdk/epdf_annot_embeddertest.cpp index 5e48587db0..0874b4f378 100644 --- a/fpdfsdk/epdf_annot_embeddertest.cpp +++ b/fpdfsdk/epdf_annot_embeddertest.cpp @@ -9987,18 +9987,17 @@ TEST_F(EPDFStampResizeEmbedderTest, StampsPlacingOneDrawingShareIt) { ASSERT_NE(0u, drawing); ASSERT_TRUE( - EPDFAnnot_SetStampDrawing(annot_.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EPDFAnnot_SetDrawing(annot_.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); ScopedFPDFAnnotation other(FPDFPage_CreateAnnot(page_.get(), FPDF_ANNOT_STAMP)); const FS_RECTF other_rect{300, 400, 500, 300}; ASSERT_TRUE(EPDFAnnot_SetRect(other.get(), &other_rect)); CPDFAnnotContextFromFPDFAnnotation(other.get()) ->GetMutableAnnotDict() ->SetNewFor("CA", 0.5f); - ASSERT_TRUE( - EPDFAnnot_SetStampDrawing(other.get(), drawing, EPDF_STAMP_FIT_COVER)); + ASSERT_TRUE(EPDFAnnot_SetDrawing(other.get(), drawing, EPDF_STAMP_FIT_COVER)); - EXPECT_EQ(drawing, EPDFAnnot_GetStampDrawing(annot_.get())); - EXPECT_EQ(drawing, EPDFAnnot_GetStampDrawing(other.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(annot_.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(other.get())); RetainPtr other_ap = GetAnnotAP( CPDFAnnotContextFromFPDFAnnotation(other.get())->GetAnnotDict(), CPDF_Annot::AppearanceMode::kNormal); @@ -10007,9 +10006,9 @@ TEST_F(EPDFStampResizeEmbedderTest, StampsPlacingOneDrawingShareIt) { EXPECT_EQ(bytes, ExportedBytes(other.get())); unsigned int drawings[4] = {}; - EXPECT_EQ(1u, EPDFDoc_GetStampDrawings(doc_.get(), drawings, 4)); + EXPECT_EQ(1u, EPDFDoc_GetDrawings(doc_.get(), drawings, 4)); EXPECT_EQ(drawing, drawings[0]); - EXPECT_EQ(1u, EPDFDoc_GetStampDrawings(doc_.get(), nullptr, 0)); + EXPECT_EQ(1u, EPDFDoc_GetDrawings(doc_.get(), nullptr, 0)); const std::string saved_bytes = SavedBytes(doc_.get()); EXPECT_EQ(1u, StreamsWithContent(saved_bytes, kBlueArtwork)); @@ -10020,7 +10019,7 @@ TEST_F(EPDFStampResizeEmbedderTest, StampsPlacingOneDrawingShareIt) { embedpdf_test::ScopedTestDocument saved( embedpdf_test::LoadMemDocumentForTest(saved_bytes.data(), saved_bytes.size(), nullptr)); - ASSERT_EQ(1u, EPDFDoc_GetStampDrawings(saved.get(), &saved_drawing, 1)); + ASSERT_EQ(1u, EPDFDoc_GetDrawings(saved.get(), &saved_drawing, 1)); } embedpdf_test::ScopedTestDocument reopened( embedpdf_test::LoadMemDocumentForTest(saved_bytes.data(), @@ -10034,8 +10033,8 @@ TEST_F(EPDFStampResizeEmbedderTest, StampsPlacingOneDrawingShareIt) { FPDFPage_CreateAnnot(reopened_page.get(), FPDF_ANNOT_STAMP)); const FS_RECTF placed_rect{100, 500, 200, 450}; ASSERT_TRUE(EPDFAnnot_SetRect(placed.get(), &placed_rect)); - EXPECT_TRUE(EPDFAnnot_SetStampDrawing(placed.get(), saved_drawing, - EPDF_STAMP_FIT_CONTAIN)); + EXPECT_TRUE(EPDFAnnot_SetDrawing(placed.get(), saved_drawing, + EPDF_STAMP_FIT_CONTAIN)); // A stamp without our wrapper places none; nor does any other annotation. ScopedFPDFAnnotation foreign( @@ -10047,18 +10046,18 @@ TEST_F(EPDFStampResizeEmbedderTest, StampsPlacingOneDrawingShareIt) { ->GetMutableAnnotDict() .Get(), 0.3f); - EXPECT_EQ(0u, EPDFAnnot_GetStampDrawing(foreign.get())); - EXPECT_EQ(1u, EPDFDoc_GetStampDrawings(doc_.get(), nullptr, 0)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(foreign.get())); + EXPECT_EQ(1u, EPDFDoc_GetDrawings(doc_.get(), nullptr, 0)); ScopedFPDFAnnotation square( FPDFPage_CreateAnnot(page_.get(), FPDF_ANNOT_SQUARE)); EXPECT_FALSE( - EPDFAnnot_SetStampDrawing(square.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); - EXPECT_EQ(0u, EPDFAnnot_GetStampDrawing(square.get())); + EPDFAnnot_SetDrawing(square.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(square.get())); // Only a form is a drawing. - EXPECT_FALSE(EPDFAnnot_SetStampDrawing( + EXPECT_FALSE(EPDFAnnot_SetDrawing( annot_.get(), EPDFDoc_GetPageObjectNumberByIndex(doc_.get(), 0), EPDF_STAMP_FIT_CONTAIN)); - EXPECT_EQ(drawing, EPDFAnnot_GetStampDrawing(annot_.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(annot_.get())); } // Two stamps place one drawing. Resize, re-fit, turn, fade, redraw, flatten @@ -10097,7 +10096,7 @@ TEST_F(EPDFStampResizeEmbedderTest, FPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_STAMP)); EXPECT_TRUE(EPDFAnnot_SetRect(stamp.get(), &rect)); EXPECT_TRUE( - EPDFAnnot_SetStampDrawing(stamp.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EPDFAnnot_SetDrawing(stamp.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); return stamp; }; ScopedFPDFAnnotation changed = place(FS_RECTF{10, 110, 110, 10}); @@ -10142,9 +10141,9 @@ TEST_F(EPDFStampResizeEmbedderTest, case Change::kNewDrawing: { const unsigned int other = import(green); ASSERT_NE(0u, other); - ASSERT_TRUE(EPDFAnnot_SetStampDrawing(changed.get(), other, - EPDF_STAMP_FIT_CONTAIN)); - EXPECT_EQ(other, EPDFAnnot_GetStampDrawing(changed.get())); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(changed.get(), other, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(other, EPDFAnnot_GetDrawing(changed.get())); break; } case Change::kFlatten: { @@ -10165,7 +10164,7 @@ TEST_F(EPDFStampResizeEmbedderTest, GetAnnotAP(kept_dict, CPDF_Annot::AppearanceMode::kNormal).Get()); EXPECT_EQ(kept_before, StreamSnapshot(kept_ap.Get())); EXPECT_EQ(drawing_before, StreamSnapshot(drawing_stream.Get())); - EXPECT_EQ(drawing, EPDFAnnot_GetStampDrawing(kept.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(kept.get())); } } diff --git a/fpdfsdk/epdf_c_api_test.c b/fpdfsdk/epdf_c_api_test.c index e714f176cb..0c89a5f79a 100644 --- a/fpdfsdk/epdf_c_api_test.c +++ b/fpdfsdk/epdf_c_api_test.c @@ -90,6 +90,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_ClearEmbedMetadata); CHK(EPDFAnnot_ClearEmbedMetadataKey); CHK(EPDFAnnot_ClearMKColor); + CHK(EPDFAnnot_ClearMKIcon); CHK(EPDFAnnot_ClearRectangleDifferences); CHK(EPDFAnnot_ExportAppearance); CHK(EPDFAnnot_GenerateAppearance); @@ -113,6 +114,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_GetDefaultAppearance); CHK(EPDFAnnot_GetDefaultAppearanceFontFace); CHK(EPDFAnnot_GetDefaultAppearanceFontPostScriptName); + CHK(EPDFAnnot_GetDrawing); CHK(EPDFAnnot_GetDrawingRect); CHK(EPDFAnnot_GetEmbedMetadataBoolean); CHK(EPDFAnnot_GetEmbedMetadataJSON); @@ -123,6 +125,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_GetIntent); CHK(EPDFAnnot_GetLineEndings); CHK(EPDFAnnot_GetMKColor); + CHK(EPDFAnnot_GetMKIcon); CHK(EPDFAnnot_GetMKRotation); CHK(EPDFAnnot_GetMKText); CHK(EPDFAnnot_GetMKTextPosition); @@ -137,11 +140,11 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_GetRichContent); CHK(EPDFAnnot_GetRichTextJSON); CHK(EPDFAnnot_GetRotate); - CHK(EPDFAnnot_GetStampDrawing); CHK(EPDFAnnot_GetStructurePosition); CHK(EPDFAnnot_GetTextAlignment); CHK(EPDFAnnot_HasAppearanceStream); CHK(EPDFAnnot_HasEmbedMetadata); + CHK(EPDFAnnot_HasMKIcon); CHK(EPDFAnnot_IsValid); CHK(EPDFAnnot_RemoveAction); CHK(EPDFAnnot_RemoveDest); @@ -158,6 +161,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_SetDefaultAppearance); CHK(EPDFAnnot_SetDefaultAppearanceDocumentFont); CHK(EPDFAnnot_SetDefaultAppearanceRegisteredFont); + CHK(EPDFAnnot_SetDrawing); CHK(EPDFAnnot_SetEmbedMetadataBoolean); CHK(EPDFAnnot_SetEmbedMetadataJSON); CHK(EPDFAnnot_SetEmbedMetadataNumber); @@ -168,6 +172,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_SetLineEndings); CHK(EPDFAnnot_SetLinkedAnnot); CHK(EPDFAnnot_SetMKColor); + CHK(EPDFAnnot_SetMKIcon); CHK(EPDFAnnot_SetMKRotation); CHK(EPDFAnnot_SetMKText); CHK(EPDFAnnot_SetMKTextPosition); @@ -182,7 +187,6 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_SetRichTextJSON); CHK(EPDFAnnot_SetRichTextXHTML); CHK(EPDFAnnot_SetRotate); - CHK(EPDFAnnot_SetStampDrawing); CHK(EPDFAnnot_SetStampOpacity); CHK(EPDFAnnot_SetSubtype); CHK(EPDFAnnot_SetTextAlignment); @@ -190,7 +194,7 @@ int CheckEmbedPDFCApi(void) { CHK(EPDFAnnot_UpdateAppearanceToRect); CHK(EPDFDoc_CanonicalDrawing); CHK(EPDFDoc_ExportDrawing); - CHK(EPDFDoc_GetStampDrawings); + CHK(EPDFDoc_GetDrawings); CHK(EPDFDoc_GetTypographicFeatures); CHK(EPDFDoc_ImportDrawing); CHK(EPDFDoc_SetTypographicFeatures); diff --git a/fpdfsdk/epdf_signature_embeddertest.cpp b/fpdfsdk/epdf_signature_embeddertest.cpp index 721ffe441f..a3b7acbd39 100644 --- a/fpdfsdk/epdf_signature_embeddertest.cpp +++ b/fpdfsdk/epdf_signature_embeddertest.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include "core/fdrm/fx_crypt_sha.h" @@ -19,6 +20,7 @@ #include "core/fpdfapi/parser/cpdf_string.h" #include "core/fxcrt/data_vector.h" #include "fpdfsdk/cpdfsdk_helpers.h" +#include "fpdfsdk/epdf_edit_test_util.h" #include "public/cpp/fpdf_scopers.h" #include "public/epdf_annot.h" #include "public/epdf_form.h" @@ -31,6 +33,7 @@ #include "public/fpdfview.h" #include "testing/embedder_test.h" #include "testing/embedpdf_form_writes.h" +#include "testing/embedpdf_layer_fixture.h" #include "testing/embedpdf_layer_mode.h" #include "testing/fx_string_testhelpers.h" #include "testing/gtest/include/gtest/gtest.h" @@ -907,6 +910,199 @@ TEST_F(EPDFSignatureEmbedderTest, CertifyThenRefuseSecondCertification) { EXPECT_NE(0u, EPDFSig_Prepare(signed_doc.get(), second, &again)); } +// A signed revision holds a picture twice: as the signer's mark and as an +// image field's icon. Placing the same picture again afterwards, in a +// signature widget and in an image field the signed revision already had, +// changes those two widgets and nothing else the signature covers: the +// drawing is placed by its number and only read. +TEST_F(EPDFSignatureEmbedderTest, + PlacingADrawingAgainLeavesTheSignedRevisionAlone) { + ASSERT_TRUE(OpenDocument("text_form.pdf")); + ScopedFPDFPage page(FPDF_LoadPage(document(), 0)); + ASSERT_TRUE(page); + const uint32_t signer = + CreateSignatureField(document(), page.get(), L"first"); + const uint32_t later = CreateSignatureField(document(), page.get(), L"later"); + ASSERT_NE(0u, signer); + ASSERT_NE(0u, later); + // A push button with one widget: an image field once it shows an icon. + auto new_button = [this, &page](const wchar_t* name, float left) { + const uint32_t field = + EPDFForm_CreateField(document(), EPDF_FORMFIELD_FAMILY_PUSHBUTTON, + GetFPDFWideString(name).get(), 0); + ScopedFPDFAnnotation widget( + EPDFPage_CreateAnnot(page.get(), FPDF_ANNOT_WIDGET)); + const FS_RECTF rect{left, 190, left + 100, 140}; + EXPECT_TRUE(EPDFAnnot_SetRect(widget.get(), &rect)); + const unsigned int number = EPDFAnnot_GetObjectNumber(widget.get()); + widget.reset(); + EXPECT_TRUE(EPDFForm_AttachWidget(document(), field, number, nullptr, 0)); + return number; + }; + const unsigned int first_button = new_button(L"initials", 50); + const unsigned int later_button = new_button(L"initials again", 170); + ASSERT_NE(0u, first_button); + ASSERT_NE(0u, later_button); + // The widgets of the two signature fields. + unsigned int first_widget = 0; + unsigned int later_widget = 0; + { + EPDF_SIGNATURE_MODEL model = EPDFSig_LoadModel(document()); + ASSERT_TRUE(model); + first_widget = EPDFSig_GetWidgetObjNum( + model, EPDFSig_GetIndexByFieldObjNum(model, signer)); + later_widget = EPDFSig_GetWidgetObjNum( + model, EPDFSig_GetIndexByFieldObjNum(model, later)); + EPDFSig_CloseModel(model); + } + ASSERT_NE(0u, first_widget); + ASSERT_NE(0u, later_widget); + // Each stands in a box of its own, 200 by 60. + for (const auto& [widget, bottom] : + {std::pair{first_widget, 50}, + std::pair{later_widget, 220}}) { + ScopedFPDFAnnotation annot( + EPDFPage_GetAnnotByObjectNumber(page.get(), widget)); + ASSERT_TRUE(annot); + const FS_RECTF rect{50, bottom + 60, 250, bottom}; + ASSERT_TRUE(EPDFAnnot_SetRect(annot.get(), &rect)); + } + + // The picture, a page that is one red box, as a drawing of the document. + unsigned int drawing = 0; + { + embedpdf_test::ScopedTestDocument picture(NewTestDocument()); + ScopedFPDFPage picture_page(FPDFPage_New(picture.get(), 0, 200, 100)); + FPDF_PAGEOBJECT box = FPDFPageObj_CreateNewRect(0, 0, 200, 100); + ASSERT_TRUE(FPDFPageObj_SetFillColor(box, 255, 0, 0, 255)); + ASSERT_TRUE(FPDFPath_SetDrawMode(box, FPDF_FILLMODE_ALTERNATE, false)); + FPDFPage_InsertObject(picture_page.get(), box); + ASSERT_TRUE(FPDFPage_GenerateContent(picture_page.get())); + ScopedFPDFDocument canonical(EPDFDoc_CanonicalDrawing(picture.get(), 0)); + ASSERT_TRUE(canonical); + drawing = EPDFDoc_ImportDrawing(document(), canonical.get()); + } + ASSERT_NE(0u, drawing); + // Shows `drawing` as the mark of `widget` and as the icon of `button`. + auto show = [](FPDF_PAGE on, unsigned int widget, unsigned int button, + unsigned int drawing) { + ScopedFPDFAnnotation mark(EPDFPage_GetAnnotByObjectNumber(on, widget)); + ASSERT_TRUE(mark); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(mark.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + ScopedFPDFAnnotation image(EPDFPage_GetAnnotByObjectNumber(on, button)); + ASSERT_TRUE(image); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(image.get(), EPDF_MK_ICON_I, drawing)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(image.get(), 1)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(image.get())); + }; + show(page.get(), first_widget, first_button, drawing); + page.reset(); + + EPDF_SIG_PREPARE opts = {}; + opts.subfilter = EPDF_SIG_SUBFILTER_ETSI_CADES_DETACHED; + opts.digest = EPDF_DIGEST_SHA256; + opts.contents_size = 2048; + Sealed sealed; + ASSERT_TRUE(SignField(document(), signer, opts, &sealed)); + + // Listing the signed file's drawings writes nothing: no object is copied + // into a layer over it, outside a transaction or inside one, and the layer + // has no change to save. + { + embedpdf_test::LayerFixture signed_layer; + ASSERT_TRUE(signed_layer.OpenBytes(sealed.bytes)); + unsigned int in_layer = 0; + EXPECT_EQ(1u, EPDFDoc_GetDrawings(signed_layer.layer, &in_layer, 1)); + EXPECT_EQ(drawing, in_layer); + ASSERT_TRUE(EPDFLayer_BeginTransaction(signed_layer.layer)); + EXPECT_EQ(1u, EPDFDoc_GetDrawings(signed_layer.layer, nullptr, 0)); + ASSERT_TRUE(EPDFLayer_CommitTransaction(signed_layer.layer)); + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(signed_layer.layer)); + EXPECT_TRUE(signed_layer.SaveDelta().empty()); + } + + // The signed file, opened again: it lists the one drawing, and the same + // picture is placed by that number. + embedpdf_test::ScopedTestDocument signed_doc( + embedpdf_test::LoadMemDocumentForTest(sealed.bytes.data(), + sealed.bytes.size(), nullptr)); + ASSERT_TRUE(signed_doc); + unsigned int listed = 0; + ASSERT_EQ(1u, EPDFDoc_GetDrawings(signed_doc.get(), &listed, 1)); + EXPECT_EQ(drawing, listed); + { + ScopedFPDFPage signed_page(FPDF_LoadPage(signed_doc.get(), 0)); + ASSERT_TRUE(signed_page); + show(signed_page.get(), later_widget, later_button, listed); + } + EXPECT_EQ(1u, EPDFDoc_GetDrawings(signed_doc.get(), nullptr, 0)); + + // Saved after the signature: the signed bytes are kept as they were. + ClearString(); + ASSERT_TRUE(FPDF_SaveAsCopy(signed_doc.get(), this, FPDF_INCREMENTAL)); + const std::string appended = GetString(); + ASSERT_GT(appended.size(), sealed.bytes.size()); + EXPECT_EQ(0, + memcmp(appended.data(), sealed.bytes.data(), sealed.bytes.size())); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(appended.data(), appended.size(), + nullptr)); + ASSERT_TRUE(reopened); + const int revisions = EPDFDoc_GetRevisionCount(reopened.get()); + ASSERT_GE(revisions, 2); + unsigned long long signed_end = 0; + ASSERT_TRUE( + EPDFDoc_GetRevision(reopened.get(), revisions - 2, &signed_end, nullptr)); + EXPECT_EQ(sealed.bytes.size(), signed_end); + ScopedFPDFDocument signed_revision( + EPDFDoc_OpenRevision(reopened.get(), signed_end)); + ASSERT_TRUE(signed_revision); + EPDF_OBJECT_DIFF diff = + EPDFDoc_CompareRevisions(signed_revision.get(), reopened.get()); + ASSERT_TRUE(diff); + const std::vector rows = ReadDiffRows(diff); + EPDFObjectDiff_Close(diff); + + // Of the objects the signature covers, only the two widgets that now show + // the picture changed. What is new is theirs: a wrapper and an appearance. + std::vector changed; + int added = 0; + for (const DiffRow& row : rows) { + if (row.kind == EPDF_DIFF_OBJ_TRAILER || row.kind == EPDF_DIFF_OBJ_XREF || + row.kind == EPDF_DIFF_OBJ_OBJSTM) { + continue; + } + if (row.change == EPDF_DIFF_ADDED) { + ++added; + } else { + changed.push_back(row.obj_num); + } + } + std::sort(changed.begin(), changed.end()); + std::vector expected = {later_widget, later_button}; + std::sort(expected.begin(), expected.end()); + EXPECT_EQ(expected, changed); + EXPECT_EQ(2, added); + for (unsigned int untouched : + {drawing, first_widget, first_button, signer, later}) { + EXPECT_EQ(-1, FindRow(rows, untouched)) << untouched; + } + + // The signature is still there, signed, over its own revision. + EPDF_SIGNATURE_MODEL model = EPDFSig_LoadModel(reopened.get()); + ASSERT_TRUE(model); + const int index = EPDFSig_GetIndexByFieldObjNum(model, signer); + ASSERT_NE(-1, index); + EXPECT_TRUE(EPDFSig_IsSigned(model, index)); + unsigned long long range[4]; + ASSERT_TRUE(EPDFSig_GetByteRange(model, index, range)); + EXPECT_EQ(sealed.bytes.size(), range[2] + range[3]); + EPDFSig_CloseModel(model); + EXPECT_EQ(1u, EPDFDoc_GetDrawings(reopened.get(), nullptr, 0)); +} + TEST_F(EPDFSignatureEmbedderTest, PermissionOneAllowsOnlyTimestamps) { ASSERT_TRUE(OpenDocument("text_form.pdf")); ScopedFPDFPage page(FPDF_LoadPage(document(), 0)); diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp new file mode 100644 index 0000000000..97d74fc472 --- /dev/null +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -0,0 +1,2225 @@ +// Copyright 2026 CloudPDF LTD +// SPDX-License-Identifier: Apache-2.0 + +// Drawings in widgets (public/epdf_annot.h). A drawing is a picture the +// document holds once. A widget shows one in two ways: as a push button's +// icon, which /MK names and the generator draws for a button that shows its +// icon alone (/MK /TP 1, what Acrobat writes for an image field), and as a +// mark, which a wrapper places as it places a stamp's artwork (a signature +// field's). Stamps, marks and icons share one drawing. + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/fpdfapi/page/cpdf_annotcontext.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_boolean.h" +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/cpdf_name.h" +#include "core/fpdfapi/parser/cpdf_number.h" +#include "core/fpdfapi/parser/cpdf_reference.h" +#include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "core/fpdfdoc/cpdf_annot.h" +#include "core/fpdfdoc/cpdf_appearancegenerator.h" +#include "core/fxcrt/fx_coordinates.h" +#include "core/fxcrt/fx_stream.h" +#include "core/fxcrt/retain_ptr.h" +#include "fpdfsdk/cpdfsdk_helpers.h" +#include "fpdfsdk/epdf_edit_test_util.h" +#include "public/cpp/fpdf_scopers.h" +#include "public/epdf_annot.h" +#include "public/epdf_form.h" +#include "public/epdf_layer.h" +#include "public/epdf_view.h" +#include "public/fpdf_annot.h" +#include "public/fpdf_edit.h" +#include "public/fpdf_save.h" +#include "public/fpdfview.h" +#include "testing/embedder_test.h" +#include "testing/embedpdf_form_writes.h" +#include "testing/embedpdf_layer_fixture.h" +#include "testing/embedpdf_layer_mode.h" +#include "testing/fx_string_testhelpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace { + +constexpr uint32_t kRed = 0xFF0000; +constexpr uint32_t kBlue = 0x0000FF; +constexpr uint32_t kGreen = 0x00FF00; +constexpr uint32_t kYellow = 0xFFFF00; +constexpr uint32_t kWhite = 0xFFFFFF; + +// The picture the tests draw is 200 by 100, red on its left half and blue on +// its right, so its direction shows. A second one is green and yellow. +constexpr CFX_FloatRect kPictureBox(0, 0, 200, 100); + +// What only the content of the picture's page holds, when the page is drawn +// with paths, and only the second picture's: how a test finds either in a +// saved file. +constexpr char kPictureMark[] = "1 0 0 rg"; +constexpr char kSecondPictureMark[] = "0 1 0 rg"; + +// How a picture's page is made: drawn with paths, or holding one image at its +// own size, as the engine makes a page from a PNG or a JPEG. +enum class Source { kVector, kImage }; +constexpr Source kSources[] = {Source::kVector, Source::kImage}; + +const char* NameOf(Source source) { + return source == Source::kVector ? "a vector page" : "an image page"; +} + +// A one-page document whose page is 200 by 100, its left half `left` and its +// right half `right`. +embedpdf_test::ScopedTestDocument NewPicturePage(Source source, + uint32_t left = kRed, + uint32_t right = kBlue) { + embedpdf_test::ScopedTestDocument document(NewTestDocument()); + EXPECT_TRUE(document); + ScopedFPDFPage page(FPDFPage_New(document.get(), 0, 200, 100)); + EXPECT_TRUE(page); + if (source == Source::kImage) { + ScopedFPDFBitmap bitmap(FPDFBitmap_Create(200, 100, 0)); + EXPECT_TRUE( + FPDFBitmap_FillRect(bitmap.get(), 0, 0, 100, 100, 0xFF000000 | left)); + EXPECT_TRUE(FPDFBitmap_FillRect(bitmap.get(), 100, 0, 100, 100, + 0xFF000000 | right)); + FPDF_PAGEOBJECT image = FPDFPageObj_NewImageObj(document.get()); + EXPECT_TRUE(FPDFImageObj_SetBitmap(nullptr, 0, image, bitmap.get())); + const FS_MATRIX at_its_size{200, 0, 0, 100, 0, 0}; + EXPECT_TRUE(FPDFPageObj_SetMatrix(image, &at_its_size)); + FPDFPage_InsertObject(page.get(), image); + } else { + const struct { + float left; + uint32_t colour; + } kHalves[] = {{0, left}, {100, right}}; + for (const auto& half : kHalves) { + FPDF_PAGEOBJECT box = FPDFPageObj_CreateNewRect(half.left, 0, 100, 100); + EXPECT_TRUE(FPDFPageObj_SetFillColor(box, (half.colour >> 16) & 0xFF, + (half.colour >> 8) & 0xFF, + half.colour & 0xFF, 255)); + EXPECT_TRUE(FPDFPath_SetDrawMode(box, FPDF_FILLMODE_ALTERNATE, false)); + FPDFPage_InsertObject(page.get(), box); + } + } + EXPECT_TRUE(FPDFPage_GenerateContent(page.get())); + return document; +} + +// The picture as a drawing of `document`, by object number: the canonical +// drawing of its page, added, as the engine adds one. Each call adds a +// drawing; a test that shares one keeps the number. +unsigned int AddDrawing(FPDF_DOCUMENT document, + Source source = Source::kVector, + uint32_t left = kRed, + uint32_t right = kBlue) { + embedpdf_test::ScopedTestDocument page = NewPicturePage(source, left, right); + ScopedFPDFDocument canonical(EPDFDoc_CanonicalDrawing(page.get(), 0)); + EXPECT_TRUE(canonical); + return canonical ? EPDFDoc_ImportDrawing(document, canonical.get()) : 0; +} + +// A page rendered once with its form fields' own appearances, for the colour +// at any point of it as 0xRRGGBB. (ColorAt() draws no widget.) +class Shown { + public: + explicit Shown(FPDF_PAGE page) + : bitmap_(EmbedderTest::RenderPageWithFlags( + page, + nullptr, + FPDF_ANNOT | EPDF_RENDER_WIDGETS)) {} + + uint32_t At(int x, int y_from_bottom) const { + const int height = FPDFBitmap_GetHeight(bitmap_.get()); + const int stride = FPDFBitmap_GetStride(bitmap_.get()); + const auto* pixels = + static_cast(FPDFBitmap_GetBuffer(bitmap_.get())); + const uint8_t* pixel = + pixels + (height - 1 - y_from_bottom) * stride + x * 4; + return (pixel[2] << 16) | (pixel[1] << 8) | pixel[0]; + } + + private: + ScopedFPDFBitmap bitmap_; +}; + +uint32_t ShownColorAt(FPDF_PAGE page, int x, int y_from_bottom) { + return Shown(page).At(x, y_from_bottom); +} + +const CPDF_Dictionary* DictOf(FPDF_ANNOTATION annot) { + return CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); +} + +RetainPtr MutableDictOf(FPDF_ANNOTATION annot) { + return CPDFAnnotContextFromFPDFAnnotation(annot)->GetMutableAnnotDict(); +} + +// The annotation's normal appearance. +RetainPtr AppearanceOf(FPDF_ANNOTATION annot) { + return GetAnnotAP(DictOf(annot), CPDF_Annot::AppearanceMode::kNormal); +} + +// The bytes of a stream, filters applied. +std::string StreamBytes(const CPDF_Stream* stream) { + auto acc = pdfium::MakeRetain(pdfium::WrapRetain(stream)); + acc->LoadAllDataFiltered(); + const auto span = acc->GetSpan(); + return std::string(span.begin(), span.end()); +} + +// The object number of the form the appearance names /FRM, or 0: the icon a +// generated button draws. +uint32_t DrawnIconNumber(FPDF_ANNOTATION annot) { + RetainPtr appearance = AppearanceOf(annot); + RetainPtr resources = + appearance ? appearance->GetDict()->GetDictFor("Resources") : nullptr; + RetainPtr forms = + resources ? resources->GetDictFor("XObject") : nullptr; + RetainPtr icon = + forms ? forms->GetStreamFor("FRM") : nullptr; + return icon ? icon->GetObjNum() : 0; +} + +// How an appearance draws its icon: the plate it is clipped to, and where +// the picture's box lands, both in the appearance's own space. +struct DrawnIcon { + CFX_FloatRect plate; + CFX_FloatRect picture; +}; + +std::optional DrawnIconOf(FPDF_ANNOTATION annot) { + RetainPtr appearance = AppearanceOf(annot); + if (!appearance) { + return std::nullopt; + } + const std::string content = StreamBytes(appearance.Get()); + const size_t drawn = content.find("/FRM Do"); + const size_t start = + drawn == std::string::npos ? drawn : content.rfind("q\n", drawn); + if (start == std::string::npos) { + return std::nullopt; + } + float plate[4]; + float matrix[6]; + if (sscanf(content.c_str() + start, + "q\n%f %f %f %f re W n\n%f %f %f %f %f %f cm\n/FRM Do\nQ\n", + &plate[0], &plate[1], &plate[2], &plate[3], &matrix[0], &matrix[1], + &matrix[2], &matrix[3], &matrix[4], &matrix[5]) != 10) { + return std::nullopt; + } + DrawnIcon icon; + icon.plate = CFX_FloatRect(plate[0], plate[1], plate[0] + plate[2], + plate[1] + plate[3]); + icon.picture = CFX_Matrix(matrix[0], matrix[1], matrix[2], matrix[3], + matrix[4], matrix[5]) + .TransformRect(kPictureBox); + return icon; +} + +void ExpectRectNear(const CFX_FloatRect& expected, + const CFX_FloatRect& actual) { + EXPECT_NEAR(expected.left, actual.left, 1e-3f); + EXPECT_NEAR(expected.bottom, actual.bottom, 1e-3f); + EXPECT_NEAR(expected.right, actual.right, 1e-3f); + EXPECT_NEAR(expected.top, actual.top, 1e-3f); +} + +// How many streams of the file `bytes` hold pass `counts`: every object the +// file lists, reached from its root or not. +template +int StreamsOf(const std::string& bytes, Counts counts) { + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + EXPECT_TRUE(reopened); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(reopened.get()); + int count = 0; + for (uint32_t number = 1; doc && number <= doc->GetLastObjNum(); ++number) { + RetainPtr stream = + ToStream(doc->GetOrParseIndirectObject(number)); + if (stream && counts(stream.Get())) { + ++count; + } + } + return count; +} + +// How many streams of a saved file have `mark` in their data. +int StreamsHolding(const std::string& bytes, const char* mark) { + return StreamsOf(bytes, [mark](const CPDF_Stream* stream) { + return StreamBytes(stream).find(mark) != std::string::npos; + }); +} + +// How many times a saved file holds the picture: the streams with its mark +// for a vector page, the image XObjects for an image page. +int PicturesIn(const std::string& bytes, Source source) { + if (source == Source::kVector) { + return StreamsHolding(bytes, kPictureMark); + } + return StreamsOf(bytes, [](const CPDF_Stream* stream) { + return stream->GetDict()->GetNameFor("Subtype") == "Image"; + }); +} + +// The drawings `document` lists. +std::vector DrawingsOf(FPDF_DOCUMENT document) { + std::vector drawings(EPDFDoc_GetDrawings(document, nullptr, 0)); + if (!drawings.empty()) { + EXPECT_EQ(drawings.size(), + EPDFDoc_GetDrawings(document, drawings.data(), drawings.size())); + } + return drawings; +} + +// Writes `bytes` as `.pdf` into the folder EPDF_BUTTON_ICON_SAMPLE_DIR +// names, when it names one: files to open in another viewer. +void KeepSample(const std::string& bytes, const char* name) { + const char* directory = std::getenv("EPDF_BUTTON_ICON_SAMPLE_DIR"); + if (!directory || !*directory) { + return; + } + std::ofstream pdf(std::string(directory) + "/" + name + ".pdf", + std::ios::binary); + pdf.write(bytes.data(), bytes.size()); + ASSERT_TRUE(pdf.good()); +} + +// A file as another app writes a button with a picture, built by hand from +// ISO 32000-2 12.5.6.19 and 12.7.8.3.3 (it is not Acrobat's): one push +// button field with seven widgets, one for each layout /MK /TP names, in +// that order. Every widget has the caption "Go", the same icon (object 6) +// with an icon fit dictionary that spells out the defaults, and an +// appearance that draws the icon inside a border of 1. Each stands 102 by +// 42, 50 apart, from y 10 up. With `with_fonts` false, nothing names a /DA +// or a font. With `with_appearances` false, no widget has an /AP. +std::string ForeignButtonsPdf(bool with_fonts = true, + bool with_appearances = true) { + std::vector objects = { + std::string("<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields " + "[5 0 R]") + + (with_fonts + ? " /DA (/Helv 0 Tf 0 g) /DR << /Font << /Helv 4 0 R >> >>" + : "") + + " >> >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 400] /Annots " + "[7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R] >>", + "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica " + "/Encoding /WinAnsiEncoding >>", + "<< /FT /Btn /Ff 65536 /T (picture) " + "/Kids [7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R] >>", + Stream("/Type /XObject /Subtype /Form /BBox [0 0 200 100] " + "/Resources << >>", + "1 0 0 rg 0 0 100 100 re f 0 0 1 rg 100 0 100 100 re f"), + }; + for (int layout = 0; layout <= 6; ++layout) { + const int bottom = 10 + 50 * layout; + objects.push_back( + "<< /Type /Annot /Subtype /Widget /Parent 5 0 R /P 3 0 R /F 4 " + "/Rect [20 " + + std::to_string(bottom) + " 122 " + std::to_string(bottom + 42) + + "] /MK << /I 6 0 R /TP " + std::to_string(layout) + + " /CA (Go) /IF << /SW /A /S /P /A [0.5 0.5] >> >>" + + (with_fonts ? " /DA (/Helv 12 Tf 0 g)" : "") + + (with_appearances + ? " /AP << /N " + std::to_string(14 + layout) + " 0 R >>" + : "") + + " >>"); + } + for (int layout = 0; layout <= 6; ++layout) { + objects.push_back( + Stream("/Type /XObject /Subtype /Form /BBox [0 0 102 42] " + "/Resources << /XObject << /FRM 6 0 R >> >>", + "q 1 1 100 40 re W n q .4 0 0 .4 11 1 cm /FRM Do Q Q")); + } + return MakePdf(objects); +} + +constexpr uint32_t kForeignIcon = 6; + +// A canonical drawing whose placement is folded into its form's /Matrix, +// built by hand: the form draws the picture at half size, off the origin, +// in the box [10 20 110 70], and its /Matrix [2 0 0 2 -20 -40] shows that box +// as [0 0 200 100]. +std::string CanonicalPageWithAMatrix() { + return MakePdf({ + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 100] /Resources " + "<< /XObject << /EPDFDRAWING 5 0 R >> >> /Contents 4 0 R >>", + Stream("/EPDFDRAWING Do"), + Stream("/Type /XObject /Subtype /Form /BBox [10 20 110 70] " + "/Matrix [2 0 0 2 -20 -40]", + "1 0 0 rg 10 20 50 50 re f 0 0 1 rg 60 20 50 50 re f"), + }); +} + +// A file with one annotation of every subtype ISO 32000-2 Table 171 names, +// a stamp and a widget last. Every one of them has the same appearance: a +// wrapper of ours (object 5) around a drawing (object 4), as only a stamp +// and a widget are given one. +constexpr const char* kOtherSubtypes[] = { + "Text", "Link", "FreeText", "Line", "Square", + "Circle", "Polygon", "PolyLine", "Highlight", "Underline", + "Squiggly", "StrikeOut", "Caret", "Ink", "Popup", + "FileAttachment", "Sound", "Movie", "Screen", "PrinterMark", + "TrapNet", "Watermark", "3D", "Redact", "Projection", + "RichMedia"}; +constexpr uint32_t kWrappedDrawing = 4; + +std::string EverySubtypePdf() { + const int count = static_cast(std::size(kOtherSubtypes)) + 2; + std::string annots; + for (int i = 0; i < count; ++i) { + annots += std::to_string(6 + i) + " 0 R "; + } + std::vector objects = { + "<< /Type /Catalog /Pages 2 0 R >>", + "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 400] /Annots [" + + annots + "] >>", + Stream("/Type /XObject /Subtype /Form /BBox [0 0 200 100]", + "1 0 0 rg 0 0 100 100 re f 0 0 1 rg 100 0 100 100 re f"), + Stream("/Type /XObject /Subtype /Form /BBox [0 0 100 50] " + "/Resources << /XObject << /EPDFWRAP 4 0 R >> >>", + "q .5 0 0 .5 0 0 cm /EPDFWRAP Do Q"), + }; + auto annotation = [](const std::string& subtype) { + return "<< /Type /Annot /Subtype /" + subtype + + " /Rect [20 20 120 70] /AP << /N 5 0 R >> >>"; + }; + for (const char* subtype : kOtherSubtypes) { + objects.push_back(annotation(subtype)); + } + objects.push_back(annotation("Stamp")); + objects.push_back(annotation("Widget")); + return MakePdf(objects); +} + +// A stream's data that counts how often it is fetched: reading a stream's +// content (to decode or to parse it) fetches its data, and reading its +// dictionary doesn't. +class CountedData final : public IFX_SeekableReadStream { + public: + CONSTRUCT_VIA_MAKE_RETAIN; + + FX_FILESIZE GetSize() override { + return static_cast(data_.size()); + } + bool ReadBlockAtOffset(pdfium::span buffer, + FX_FILESIZE offset) override { + ++fetches_; + if (offset < 0 || static_cast(offset) > data_.size() || + buffer.size() > data_.size() - static_cast(offset)) { + return false; + } + std::copy_n(data_.begin() + offset, buffer.size(), buffer.begin()); + return true; + } + bool IsSelfContained() const override { return true; } + + int fetches() const { return fetches_; } + + private: + explicit CountedData(std::string data) : data_(std::move(data)) {} + ~CountedData() override = default; + + const std::string data_; + int fetches_ = 0; +}; + +} // namespace + +class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { + protected: + void SetUp() override { + EmbedderTest::SetUp(); + doc_ = NewTestDocument(); + ASSERT_TRUE(doc_); + page_.reset(FPDFPage_New(doc_.get(), 0, 400, 400)); + ASSERT_TRUE(page_); + } + + void TearDown() override { + page_.reset(); + doc_.reset(); + EmbedderTest::TearDown(); + } + + CPDF_Document* pdf() { return CPDFDocumentFromFPDFDocument(doc_.get()); } + + // A new field of `family` in `document`, without a widget, under a name no + // other field of the test has. + uint32_t NewField(FPDF_DOCUMENT document, int family) { + return EPDFForm_CreateField( + document, family, + GetFPDFWideString(L"field" + std::to_wstring(++fields_)).get(), 0); + } + + // A new widget of `field` on `page`, a page of `document`, standing at + // `rect`, with a border of `border_width` when one is given: /BS /W, and no + // colour to paint it. + ScopedFPDFAnnotation NewWidget(FPDF_DOCUMENT document, + uint32_t field, + FPDF_PAGE page, + const FS_RECTF& rect, + std::optional border_width = {}) { + uint32_t number = 0; + { + ScopedFPDFAnnotation widget( + EPDFPage_CreateAnnot(page, FPDF_ANNOT_WIDGET)); + EXPECT_TRUE(widget); + EXPECT_TRUE(EPDFAnnot_SetRect(widget.get(), &rect)); + if (border_width) { + EXPECT_TRUE(EPDFAnnot_SetBorderStyle(widget.get(), FPDF_ANNOT_BS_SOLID, + *border_width)); + } + number = EPDFAnnot_GetObjectNumber(widget.get()); + } + EXPECT_TRUE(EPDFForm_AttachWidget(document, field, number, nullptr, 0)); + return ScopedFPDFAnnotation(EPDFPage_GetAnnotByObjectNumber(page, number)); + } + + // A new push button with one widget, on a page of the test's document. + ScopedFPDFAnnotation NewButton(const FS_RECTF& rect, + std::optional border_width = {}, + FPDF_PAGE page = nullptr) { + const uint32_t field = + NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_PUSHBUTTON); + EXPECT_GT(field, 0u); + return NewWidget(doc_.get(), field, page ? page : page_.get(), rect, + border_width); + } + + // A new signature field with one widget, on a page of the test's document: + // where a mark goes. + ScopedFPDFAnnotation NewSignatureWidget(const FS_RECTF& rect, + FPDF_PAGE page = nullptr) { + const uint32_t field = + NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_SIGNATURE); + EXPECT_GT(field, 0u); + return NewWidget(doc_.get(), field, page ? page : page_.get(), rect); + } + + // What the engine does to show a drawing as one box's image: the drawing + // as the normal icon, the layout that shows the icon alone, and a new + // appearance. + void SetIcon(FPDF_ANNOTATION widget, unsigned int drawing) { + ASSERT_TRUE(EPDFAnnot_SetMKIcon(widget, EPDF_MK_ICON_I, drawing)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(widget, 1)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget)); + } + + // A save of `doc`: a full one, or with FPDF_INCREMENTAL one appended to + // the bytes the document was opened from. + std::string SavedBytes(FPDF_DOCUMENT doc, + FPDF_DWORD flags = FPDF_NO_INCREMENTAL) { + ClearString(); + EXPECT_TRUE(FPDF_SaveAsCopy(doc, this, flags)); + return GetString(); + } + + // The canonical bytes of the picture: what the engine hashes to know a + // drawing by its content. + std::string CanonicalBytes(Source source) { + embedpdf_test::ScopedTestDocument page = NewPicturePage(source); + ScopedFPDFDocument canonical(EPDFDoc_CanonicalDrawing(page.get(), 0)); + EXPECT_TRUE(canonical); + return canonical ? SavedBytes(canonical.get()) : std::string(); + } + + embedpdf_test::ScopedTestDocument doc_; + ScopedFPDFPage page_; + int fields_ = 0; +}; + +namespace { + +// The two kinds of save a test looks into. +const struct { + const char* name; + FPDF_DWORD flags; +} kSaves[] = {{"a full save", FPDF_NO_INCREMENTAL}, + {"an incremental save", FPDF_INCREMENTAL}}; + +} // namespace + +// Naming a drawing as a button's icon writes /MK /I and nothing else: no +// object is made, and the layout and the appearance are the caller's next +// two calls. Drawn, the appearance names the same form /FRM, so the file +// holds the picture once. Clearing takes /MK /I away and leaves the layout, +// and the next appearance is the box alone. +TEST_F(EPDFWidgetDrawingEmbedderTest, SetsReadsAndClearsAnIcon) { + // 202 by 102, with the border of 1 a widget has when nothing says + // otherwise: the picture fits the inside at its own size. + ScopedFPDFAnnotation button = NewButton({100, 302, 302, 200}); + ASSERT_TRUE(button); + const unsigned int drawing = AddDrawing(doc_.get()); + const unsigned int second = + AddDrawing(doc_.get(), Source::kVector, kGreen, kYellow); + ASSERT_NE(0u, drawing); + ASSERT_NE(0u, second); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + const uint32_t first_appearance = AppearanceOf(button.get())->GetObjNum(); + const uint32_t objects = pdf()->GetLastObjNum(); + const std::string drawing_before = ObjectGraph(pdf())[drawing]; + + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + { + // /MK names the drawing by reference, and nothing else is written: the + // layout is the caption alone still, no object was made, and the + // appearance is the one the button had. + RetainPtr mk = + DictOf(button.get())->GetDictFor("MK"); + EXPECT_TRUE(mk->GetObjectFor("I")->IsReference()); + EXPECT_FALSE(mk->KeyExist("TP")); + EXPECT_FALSE(mk->KeyExist("IF")); + } + EXPECT_EQ(objects, pdf()->GetLastObjNum()); + EXPECT_EQ(0, EPDFAnnot_GetMKTextPosition(button.get())); + EXPECT_EQ(first_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(kWhite, ShownColorAt(page_.get(), 150, 250)); + + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 1)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + RetainPtr appearance = AppearanceOf(button.get()); + ASSERT_TRUE(appearance); + EXPECT_NE(first_appearance, appearance->GetObjNum()); + EXPECT_EQ("Form", appearance->GetDict()->GetNameFor("Subtype")); + EXPECT_EQ(CFX_FloatRect(0, 0, 202, 102), + appearance->GetDict()->GetRectFor("BBox")); + EXPECT_EQ("q\nQ\nq\n1 1 200 100 re W n\n1 0 0 1 1 1 cm\n/FRM Do\nQ\n", + StreamBytes(appearance.Get())); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + // A generated button's appearance is no wrapper: its picture is its icon. + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(button.get())); + EXPECT_EQ(kRed, ShownColorAt(page_.get(), 150, 250)); + EXPECT_EQ(kBlue, ShownColorAt(page_.get(), 250, 250)); + EXPECT_EQ(kWhite, ShownColorAt(page_.get(), 50, 250)); + + // Naming the drawing the entry names already writes nothing. + const std::string widget_before = + ObjectGraph(pdf())[EPDFAnnot_GetObjectNumber(button.get())]; + EXPECT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); + EXPECT_EQ(widget_before, + ObjectGraph(pdf())[EPDFAnnot_GetObjectNumber(button.get())]); + + // Another drawing replaces the first, which is as it was. + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, second)); + EXPECT_EQ(second, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(drawing_before, ObjectGraph(pdf())[drawing]); + + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(DictOf(button.get())->GetDictFor("MK")->KeyExist("I")); + // The layout stays: the button still shows an icon alone, and has none. + EXPECT_EQ(1, EPDFAnnot_GetMKTextPosition(button.get())); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + appearance = AppearanceOf(button.get()); + EXPECT_EQ("q\nQ\n", StreamBytes(appearance.Get())); + EXPECT_FALSE(appearance->GetDict()->KeyExist("Resources")); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_EQ(kWhite, ShownColorAt(page_.get(), 150, 250)); + // Clearing what isn't there succeeds and writes nothing. + EXPECT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); +} + +// The rollover and down icons are kept as the normal one is, each under its +// own key. Only the normal icon is drawn: the generator makes the normal +// appearance. +TEST_F(EPDFWidgetDrawingEmbedderTest, TheOtherIconsAreKeptAndNotDrawn) { + ScopedFPDFAnnotation button = NewButton({100, 302, 302, 200}); + ASSERT_TRUE(button); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 1)); + const struct { + int which; + const char* key; + } kOthers[] = {{EPDF_MK_ICON_RI, "RI"}, {EPDF_MK_ICON_IX, "IX"}}; + for (const auto& other : kOthers) { + SCOPED_TRACE(other.key); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), other.which)); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), other.which, drawing)); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), other.which)); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), other.which)); + EXPECT_TRUE(DictOf(button.get())->GetDictFor("MK")->KeyExist(other.key)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + } + // The document lists the drawing once, though two entries name it. + EXPECT_EQ(std::vector({drawing}), DrawingsOf(doc_.get())); + // Clearing one leaves the other. + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_RI)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_RI)); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_IX)); +} + +// A call that can't show a drawing writes nothing: the widget has no /MK, +// the document no new object, and on a layer nothing is copied up. +TEST_F(EPDFWidgetDrawingEmbedderTest, RefusesWithoutWriting) { + ScopedFPDFAnnotation button = NewButton({100, 302, 302, 200}); + ASSERT_TRUE(button); + ScopedFPDFAnnotation flat = NewSignatureWidget({100, 100, 300, 100}); + ASSERT_TRUE(flat); + ScopedFPDFAnnotation square( + FPDFPage_CreateAnnot(page_.get(), FPDF_ANNOT_SQUARE)); + ASSERT_TRUE(square); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + const unsigned int page_number = + EPDFDoc_GetPageObjectNumberByIndex(doc_.get(), 0); + const uint32_t objects = pdf()->GetLastObjNum(); + const uint32_t flat_appearance = AppearanceOf(flat.get())->GetObjNum(); + + for (int which : {-1, 3}) { + EXPECT_FALSE(EPDFAnnot_SetMKIcon(button.get(), which, drawing)); + EXPECT_FALSE(EPDFAnnot_ClearMKIcon(button.get(), which)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), which)); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(button.get(), which)); + } + // Only a widget has icons. + EXPECT_FALSE(EPDFAnnot_SetMKIcon(square.get(), EPDF_MK_ICON_I, drawing)); + EXPECT_FALSE(EPDFAnnot_ClearMKIcon(square.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(square.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(square.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(EPDFAnnot_SetMKIcon(nullptr, EPDF_MK_ICON_I, drawing)); + EXPECT_FALSE(EPDFAnnot_ClearMKIcon(nullptr, EPDF_MK_ICON_I)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(nullptr, EPDF_MK_ICON_I)); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(nullptr, EPDF_MK_ICON_I)); + // Only a form of the document is a drawing: not no object, not a page, + // not a number the document doesn't have. + for (unsigned int no_drawing : {0u, page_number, 999999u}) { + SCOPED_TRACE(no_drawing); + EXPECT_FALSE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, no_drawing)); + EXPECT_FALSE( + EPDFAnnot_SetDrawing(button.get(), no_drawing, EPDF_STAMP_FIT_CONTAIN)); + } + // Only a stamp and a widget place a drawing, and only one with area. + EXPECT_FALSE( + EPDFAnnot_SetDrawing(square.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_FALSE(EPDFAnnot_SetDrawing(nullptr, drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_FALSE( + EPDFAnnot_SetDrawing(flat.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(square.get())); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(nullptr)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(flat.get())); + + EXPECT_FALSE(DictOf(button.get())->KeyExist("MK")); + EXPECT_FALSE(DictOf(square.get())->KeyExist("MK")); + EXPECT_EQ(flat_appearance, AppearanceOf(flat.get())->GetObjNum()); + EXPECT_EQ(objects, pdf()->GetLastObjNum()); + + // On a layer, a refused call and a call that changes nothing copy nothing + // up: object 3 is the page, and the second widget names object 6 already. + embedpdf_test::LayerFixture foreign; + const std::string bytes = ForeignButtonsPdf(); + ASSERT_TRUE(foreign.OpenBytes({bytes.begin(), bytes.end()})); + { + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation widget(FPDFPage_GetAnnot(page.get(), 1)); + ASSERT_TRUE(widget); + EXPECT_FALSE(EPDFAnnot_SetMKIcon(widget.get(), EPDF_MK_ICON_I, 3)); + EXPECT_FALSE(EPDFAnnot_SetDrawing(widget.get(), 3, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_TRUE( + EPDFAnnot_SetMKIcon(widget.get(), EPDF_MK_ICON_I, kForeignIcon)); + EXPECT_TRUE(EPDFAnnot_ClearMKIcon(widget.get(), EPDF_MK_ICON_RI)); + EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(widget.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(widget.get())); + } + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(foreign.layer)); +} + +// The icon is fitted into the button less the border's width at each edge, +// the width /BS gives whether or not a colour paints it, and clipped there. +// /MK /IF /FB takes the whole button. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconIsFittedInsideTheBorder) { + const FS_RECTF kStands{100, 308, 308, 200}; // 208 by 108 + const struct { + const char* name; + std::optional border_width; + bool painted; + bool fit_to_bounds; + CFX_FloatRect plate; + CFX_FloatRect picture; + } kCases[] = { + // No /BS: a border of 1. 206 by 106 inside, so the picture is 206 by + // 103 in its middle. + {"no /BS", + {}, + false, + false, + CFX_FloatRect(1, 1, 207, 107), + CFX_FloatRect(1, 2.5f, 207, 105.5f)}, + {"no border", 0.0f, false, false, CFX_FloatRect(0, 0, 208, 108), + CFX_FloatRect(0, 2, 208, 106)}, + {"a border of 4", 4.0f, true, false, CFX_FloatRect(4, 4, 204, 104), + CFX_FloatRect(4, 4, 204, 104)}, + {"a border of 4, fitted to the bounds", 4.0f, true, true, + CFX_FloatRect(0, 0, 208, 108), CFX_FloatRect(0, 2, 208, 106)}, + }; + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + const unsigned int drawing = AddDrawing(doc_.get(), source); + ASSERT_NE(0u, drawing); + for (const auto& expected : kCases) { + SCOPED_TRACE(expected.name); + ScopedFPDFAnnotation button = NewButton(kStands, expected.border_width); + ASSERT_TRUE(button); + if (expected.painted) { + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + } + if (expected.fit_to_bounds) { + MutableDictOf(button.get()) + ->GetOrCreateDictFor("MK") + ->SetNewFor("IF") + ->SetNewFor("FB", true); + } + SetIcon(button.get(), drawing); + + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(expected.plate, drawn->plate); + ExpectRectNear(expected.picture, drawn->picture); + // The border is stroked before the picture is drawn, when it has a + // colour. + const std::string content = StreamBytes(AppearanceOf(button.get()).Get()); + EXPECT_EQ(expected.painted, content.find(" re S\n") != std::string::npos); + } + } +} + +// The icon is fitted as the file's icon fit dictionary says (ISO 32000-2 +// Table 250), and by the table's defaults without one: always scaled, +// keeping its proportions, in the middle. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconIsFittedAsTheIconFitSays) { + struct Fit { + const char* scale_when = nullptr; // /SW + const char* scale_type = nullptr; // /S + std::optional share; // /A + }; + const struct { + const char* name; + float width; + float height; + std::optional fit; + CFX_FloatRect picture; + } kCases[] = { + {"no /IF", 300, 100, {}, CFX_FloatRect(50, 0, 250, 100)}, + {"an empty /IF", 300, 100, Fit{}, CFX_FloatRect(50, 0, 250, 100)}, + {"the defaults spelled out", 300, 100, + Fit{"A", "P", CFX_PointF(0.5f, 0.5f)}, CFX_FloatRect(50, 0, 250, 100)}, + {"without its proportions", 300, 100, Fit{nullptr, "A", {}}, + CFX_FloatRect(0, 0, 300, 100)}, + {"at the left", 300, 100, Fit{nullptr, nullptr, CFX_PointF(0, 0)}, + CFX_FloatRect(0, 0, 200, 100)}, + {"at the right", 300, 100, Fit{nullptr, nullptr, CFX_PointF(1, 1)}, + CFX_FloatRect(100, 0, 300, 100)}, + {"at the top", 100, 100, Fit{nullptr, nullptr, CFX_PointF(0.5f, 1)}, + CFX_FloatRect(0, 50, 100, 100)}, + // A share outside 0 to 1 is brought back to it. + {"past the right", 300, 100, Fit{nullptr, nullptr, CFX_PointF(5, 0)}, + CFX_FloatRect(100, 0, 300, 100)}, + // Never scaled: its own size, in the middle, clipped where it is + // larger. + {"never, in a larger button", 300, 200, Fit{"N", nullptr, {}}, + CFX_FloatRect(50, 50, 250, 150)}, + {"never, in a smaller button", 100, 40, Fit{"N", nullptr, {}}, + CFX_FloatRect(-50, -30, 150, 70)}, + // Only when it is bigger than the button. + {"when bigger, in a larger button", 300, 200, Fit{"B", nullptr, {}}, + CFX_FloatRect(50, 50, 250, 150)}, + {"when bigger, in a smaller button", 100, 40, Fit{"B", nullptr, {}}, + CFX_FloatRect(10, 0, 90, 40)}, + // Only when it is smaller than the button. + {"when smaller, in a larger button", 300, 200, Fit{"S", nullptr, {}}, + CFX_FloatRect(0, 25, 300, 175)}, + {"when smaller, in a smaller button", 100, 40, Fit{"S", nullptr, {}}, + CFX_FloatRect(-50, -30, 150, 70)}, + }; + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + const unsigned int drawing = AddDrawing(doc_.get(), source); + ASSERT_NE(0u, drawing); + for (const auto& expected : kCases) { + SCOPED_TRACE(expected.name); + ScopedFPDFAnnotation button = NewButton( + {50, 150 + expected.height, 50 + expected.width, 150}, 0.0f); + ASSERT_TRUE(button); + if (expected.fit) { + RetainPtr fit = MutableDictOf(button.get()) + ->GetOrCreateDictFor("MK") + ->SetNewFor("IF"); + if (expected.fit->scale_when) { + fit->SetNewFor("SW", expected.fit->scale_when); + } + if (expected.fit->scale_type) { + fit->SetNewFor("S", expected.fit->scale_type); + } + if (expected.fit->share) { + RetainPtr share = fit->SetNewFor("A"); + share->AppendNew(expected.fit->share->x); + share->AppendNew(expected.fit->share->y); + } + } + SetIcon(button.get(), drawing); + + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(0, 0, expected.width, expected.height), + drawn->plate); + ExpectRectNear(expected.picture, drawn->picture); + } + } +} + +namespace { + +// What a picture in a box turned a quarter turn looks like, for a box that +// stands 40 wide and 160 tall: the frame it is laid out in, the turn, where +// the picture lands in the frame, and a point of its red half and of its +// blue half, from the box's lower left corner. An icon without a border and +// a mark land the same. +struct Turn { + std::optional written; // /MK /R + CFX_FloatRect frame; + CFX_Matrix turn; + CFX_FloatRect picture; + int red_x; + int red_y; + int blue_x; + int blue_y; +}; +const Turn kTurns[] = { + // As it stands, 40 by 160: the picture is 40 by 20 in its middle, red at + // the left. + {{}, + CFX_FloatRect(0, 0, 40, 160), + CFX_Matrix(), + CFX_FloatRect(0, 70, 40, 90), + 10, + 80, + 30, + 80}, + // Laid out 160 by 40: the picture is 80 by 40 in its middle. Turned + // counterclockwise, its left is at the bottom. + {90, CFX_FloatRect(0, 0, 160, 40), CFX_Matrix(0, 1, -1, 0, 40, 0), + CFX_FloatRect(40, 0, 120, 40), 20, 60, 20, 100}, + // Upside down: its left is at the right. + {180, CFX_FloatRect(0, 0, 40, 160), CFX_Matrix(-1, 0, 0, -1, 40, 160), + CFX_FloatRect(0, 70, 40, 90), 30, 80, 10, 80}, + // Turned clockwise, its left is at the top; -90 is the same turn. + {270, CFX_FloatRect(0, 0, 160, 40), CFX_Matrix(0, -1, 1, 0, 0, 160), + CFX_FloatRect(40, 0, 120, 40), 20, 100, 20, 60}, + {-90, CFX_FloatRect(0, 0, 160, 40), CFX_Matrix(0, -1, 1, 0, 0, 160), + CFX_FloatRect(40, 0, 120, 40), 20, 100, 20, 60}, +}; + +// Where the box for the turn at `index` stands, in a row that starts at +// `bottom`: 40 wide and 160 tall, 70 from the one before it. +FS_RECTF TurnedBoxAt(int index, int bottom) { + const float left = 20 + 70 * index; + return {left, bottom + 160.0f, left + 40, static_cast(bottom)}; +} + +void WriteTurn(FPDF_ANNOTATION widget, const Turn& turn) { + if (turn.written) { + MutableDictOf(widget)->GetOrCreateDictFor("MK")->SetNewFor( + "R", *turn.written); + } +} + +} // namespace + +// A turned button's icon is fitted into the frame its quarter turn gives, +// /Rect with its sides swapped at 90 and 270, and turned with it: the +// appearance's /Matrix is the turn a field's own appearance has. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconTurnsWithItsButton) { + int page_index = 0; + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + const unsigned int drawing = AddDrawing(doc_.get(), source); + ASSERT_NE(0u, drawing); + int index = 0; + for (const Turn& expected : kTurns) { + SCOPED_TRACE(expected.written.value_or(0)); + const FS_RECTF stands = TurnedBoxAt(index++, 200); + ScopedFPDFAnnotation button = NewButton(stands, 0.0f, page.get()); + ASSERT_TRUE(button); + WriteTurn(button.get(), expected); + SetIcon(button.get(), drawing); + + RetainPtr appearance = AppearanceOf(button.get()); + ASSERT_TRUE(appearance); + EXPECT_EQ(expected.frame, appearance->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(expected.turn, appearance->GetDict()->GetMatrixFor("Matrix")); + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(expected.frame, drawn->plate); + ExpectRectNear(expected.picture, drawn->picture); + const Shown shown(page.get()); + const int left = static_cast(stands.left); + EXPECT_EQ(kRed, shown.At(left + expected.red_x, 200 + expected.red_y)); + EXPECT_EQ(kBlue, shown.At(left + expected.blue_x, 200 + expected.blue_y)); + // /Rect is where the button stands, whatever its turn. + EXPECT_EQ(CFX_FloatRect(stands.left, 200, stands.right, 360), + DictOf(button.get())->GetRectFor("Rect")); + } + } +} + +// A mark is placed in a widget as EPDFAnnot_UpdateAppearanceToRect() fits one +// today: a widget given the picture's page and fitted, and a widget given +// the picture's drawing, get the same wrapper, at every turn and with every +// fit. The first makes a drawing of its own; the second places one the +// document has. +TEST_F(EPDFWidgetDrawingEmbedderTest, AMarkIsPlacedAsAPageIsFitted) { + int page_index = 0; + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + embedpdf_test::ScopedTestDocument picture = NewPicturePage(source); + const unsigned int drawing = AddDrawing(doc_.get(), source); + ASSERT_NE(0u, drawing); + const std::string drawing_before = ObjectGraph(pdf())[drawing]; + + int index = 0; + for (const Turn& expected : kTurns) { + SCOPED_TRACE(expected.written.value_or(0)); + // The widget fitted from the page stands above the one given the + // drawing. + const FS_RECTF from_page_at = TurnedBoxAt(index, 220); + const FS_RECTF from_drawing_at = TurnedBoxAt(index++, 20); + ScopedFPDFAnnotation from_page = + NewSignatureWidget(from_page_at, page.get()); + ScopedFPDFAnnotation from_drawing = + NewSignatureWidget(from_drawing_at, page.get()); + ASSERT_TRUE(from_page); + ASSERT_TRUE(from_drawing); + WriteTurn(from_page.get(), expected); + WriteTurn(from_drawing.get(), expected); + ASSERT_TRUE( + EPDFAnnot_SetAppearanceFromPage(from_page.get(), picture.get(), 0)); + ASSERT_TRUE(EPDFAnnot_UpdateAppearanceToRect(from_page.get(), + EPDF_STAMP_FIT_CONTAIN)); + ASSERT_TRUE(EPDFAnnot_SetDrawing(from_drawing.get(), drawing, + EPDF_STAMP_FIT_CONTAIN)); + + RetainPtr fitted = AppearanceOf(from_page.get()); + RetainPtr placed = AppearanceOf(from_drawing.get()); + ASSERT_TRUE(fitted); + ASSERT_TRUE(placed); + EXPECT_EQ(StreamBytes(fitted.Get()), StreamBytes(placed.Get())); + EXPECT_EQ(expected.frame, placed->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(fitted->GetDict()->GetRectFor("BBox"), + placed->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(fitted->GetDict()->GetMatrixFor("Matrix"), + placed->GetDict()->GetMatrixFor("Matrix")); + EXPECT_EQ(fitted->GetDict()->KeyExist("Matrix"), + placed->GetDict()->KeyExist("Matrix")); + if (placed->GetDict()->KeyExist("Matrix")) { + EXPECT_EQ(expected.turn, placed->GetDict()->GetMatrixFor("Matrix")); + } + // Each reads its drawing: the page's own, and the document's. + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(from_drawing.get())); + EXPECT_NE(0u, EPDFAnnot_GetDrawing(from_page.get())); + EXPECT_NE(drawing, EPDFAnnot_GetDrawing(from_page.get())); + // A widget's /Rect and /MK are read, never written. + EXPECT_EQ( + CFX_FloatRect(from_drawing_at.left, 20, from_drawing_at.right, 180), + DictOf(from_drawing.get())->GetRectFor("Rect")); + if (expected.written) { + EXPECT_EQ( + *expected.written, + DictOf(from_drawing.get())->GetDictFor("MK")->GetIntegerFor("R")); + } + const Shown shown(page.get()); + for (const FS_RECTF& stands : {from_page_at, from_drawing_at}) { + const int left = static_cast(stands.left); + const int bottom = static_cast(stands.bottom); + EXPECT_EQ(kRed, + shown.At(left + expected.red_x, bottom + expected.red_y)); + EXPECT_EQ(kBlue, + shown.At(left + expected.blue_x, bottom + expected.blue_y)); + } + } + + // Every fit, in a box 120 by 30: the same wrapper either way. + for (EPDF_STAMP_FIT fit : {EPDF_STAMP_FIT_CONTAIN, EPDF_STAMP_FIT_COVER, + EPDF_STAMP_FIT_STRETCH}) { + SCOPED_TRACE(fit); + const FS_RECTF stands{260, 390, 380, 360}; + ScopedFPDFAnnotation from_page = NewSignatureWidget(stands, page.get()); + ScopedFPDFAnnotation from_drawing = + NewSignatureWidget(stands, page.get()); + ASSERT_TRUE( + EPDFAnnot_SetAppearanceFromPage(from_page.get(), picture.get(), 0)); + ASSERT_TRUE(EPDFAnnot_UpdateAppearanceToRect(from_page.get(), fit)); + ASSERT_TRUE(EPDFAnnot_SetDrawing(from_drawing.get(), drawing, fit)); + EXPECT_EQ(StreamBytes(AppearanceOf(from_page.get()).Get()), + StreamBytes(AppearanceOf(from_drawing.get()).Get())); + EXPECT_EQ( + AppearanceOf(from_page.get())->GetDict()->GetRectFor("BBox"), + AppearanceOf(from_drawing.get())->GetDict()->GetRectFor("BBox")); + } + // Placing a drawing never writes it. + EXPECT_EQ(drawing_before, ObjectGraph(pdf())[drawing]); + } +} + +// A drawing whose placement is folded into its form's /Matrix, as a +// canonical drawing can have it, is placed by the box it shows: /BBox +// through /Matrix. As an icon and as a mark it lands as the plain picture +// does. +TEST_F(EPDFWidgetDrawingEmbedderTest, ADrawingWithAMatrixIsPlacedByItsBox) { + const std::string bytes = CanonicalPageWithAMatrix(); + embedpdf_test::ScopedTestDocument canonical( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(canonical); + const unsigned int drawing = + EPDFDoc_ImportDrawing(doc_.get(), canonical.get()); + ASSERT_NE(0u, drawing); + { + // Adopted as it is: its box off the origin, and its matrix. + RetainPtr form = + ToStream(pdf()->GetOrParseIndirectObject(drawing)); + ASSERT_TRUE(form); + EXPECT_EQ(CFX_FloatRect(10, 20, 110, 70), + form->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(CFX_Matrix(2, 0, 0, 2, -20, -40), + form->GetDict()->GetMatrixFor("Matrix")); + } + + // An icon: a button 204 by 104 with a border of 2 shows it at its size. + ScopedFPDFAnnotation button = NewButton({100, 304, 304, 200}, 2.0f); + ASSERT_TRUE(button); + SetIcon(button.get(), drawing); + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(2, 2, 202, 102), drawn->picture); + + // A mark: a widget 100 by 50 shows it at half its size. + ScopedFPDFAnnotation widget = NewSignatureWidget({100, 150, 200, 100}); + ASSERT_TRUE(widget); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(widget.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(widget.get())); + + const Shown shown(page_.get()); + EXPECT_EQ(kRed, shown.At(150, 250)); + EXPECT_EQ(kBlue, shown.At(250, 250)); + EXPECT_EQ(kWhite, shown.At(150, 310)); + EXPECT_EQ(kRed, shown.At(125, 125)); + EXPECT_EQ(kBlue, shown.At(175, 125)); + EXPECT_EQ(kWhite, shown.At(125, 160)); +} + +// A field with several widgets: each names the same drawing as its icon, and +// the file holds the picture once. +TEST_F(EPDFWidgetDrawingEmbedderTest, EachWidgetOfAFieldShowsTheSameIcon) { + ScopedFPDFPage second(FPDFPage_New(doc_.get(), 1, 400, 400)); + ASSERT_TRUE(second); + const uint32_t field = NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_PUSHBUTTON); + ASSERT_GT(field, 0u); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + const FS_RECTF kLow{100, 150, 200, 100}; // 100 by 50, at (100, 100) + const FS_RECTF kHigh{100, 350, 200, 300}; // 100 by 50, at (100, 300) + ScopedFPDFAnnotation widgets[] = { + NewWidget(doc_.get(), field, page_.get(), kLow, 0.0f), + NewWidget(doc_.get(), field, page_.get(), kHigh, 0.0f), + NewWidget(doc_.get(), field, second.get(), kLow, 0.0f), + }; + for (const ScopedFPDFAnnotation& widget : widgets) { + ASSERT_TRUE(widget); + SetIcon(widget.get(), drawing); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(widget.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(drawing, DrawnIconNumber(widget.get())); + const std::optional drawn = DrawnIconOf(widget.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(0, 0, 100, 50), drawn->picture); + } + EXPECT_EQ(std::vector({drawing}), DrawingsOf(doc_.get())); + + EPDF_FORM_MODEL model = EPDFForm_LoadModel(doc_.get()); + ASSERT_TRUE(model); + EXPECT_EQ(3, EPDFForm_CountFieldWidgets( + model, EPDFForm_GetFieldIndexByObjNum(model, field))); + EPDFForm_CloseModel(model); + + const Shown first_page(page_.get()); + EXPECT_EQ(kRed, first_page.At(125, 125)); + EXPECT_EQ(kBlue, first_page.At(175, 125)); + EXPECT_EQ(kRed, first_page.At(125, 325)); + EXPECT_EQ(kBlue, first_page.At(175, 325)); + EXPECT_EQ(kRed, ShownColorAt(second.get(), 125, 125)); + EXPECT_EQ(kBlue, ShownColorAt(second.get(), 175, 125)); + for (ScopedFPDFAnnotation& widget : widgets) { + widget.reset(); + } + EXPECT_EQ(1, PicturesIn(SavedBytes(doc_.get()), Source::kVector)); +} + +// A saved file holds the icon in both places, as Acrobat writes an image +// field: /MK /I with /MK /TP 1, and an /AP /N that draws the same form. +TEST_F(EPDFWidgetDrawingEmbedderTest, ASavedFileHoldsTheIconInBothPlaces) { + { + ScopedFPDFAnnotation button = NewButton({100, 304, 304, 200}, 2.0f); + ASSERT_TRUE(button); + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BG, 255, 255, 204)); + SetIcon(button.get(), AddDrawing(doc_.get())); + } + const std::string bytes = SavedBytes(doc_.get()); + ASSERT_FALSE(bytes.empty()); + KeepSample(bytes, "button-picture-upright"); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(reopened); + ScopedFPDFPage page(FPDF_LoadPage(reopened.get(), 0)); + ASSERT_TRUE(page); + ASSERT_EQ(1, FPDFPage_GetAnnotCount(page.get())); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 0)); + ASSERT_TRUE(button); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(1, EPDFAnnot_GetMKTextPosition(button.get())); + const unsigned int icon = EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I); + EXPECT_GT(icon, 0u); + EXPECT_EQ(icon, DrawnIconNumber(button.get())); + EXPECT_EQ(std::vector({icon}), DrawingsOf(reopened.get())); + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(2, 2, 202, 102), drawn->picture); + const Shown shown(page.get()); + EXPECT_EQ(kRed, shown.At(150, 250)); + EXPECT_EQ(kBlue, shown.At(250, 250)); + // The border, in the middle of its 2 points. + EXPECT_EQ(kGreen, shown.At(101, 250)); + button.reset(); +} + +// On a page turned a quarter turn, a button turned the other way reads +// upright: the picture's left is at the reader's left. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconReadsUprightOnATurnedPage) { + // The page is shown turned 90 clockwise. The button stands 52 wide and 102 + // tall on it, and is turned 90 counterclockwise, so the reader sees it 102 + // wide and 52 tall. + FPDFPage_SetRotation(page_.get(), 1); + { + ScopedFPDFAnnotation button = NewButton({100, 202, 152, 100}); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKRotation(button.get(), 90)); + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + SetIcon(button.get(), AddDrawing(doc_.get())); + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(1, 1, 101, 51), drawn->picture); + } + const std::string bytes = SavedBytes(doc_.get()); + ASSERT_FALSE(bytes.empty()); + KeepSample(bytes, "button-picture-on-turned-page"); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(reopened); + ScopedFPDFPage page(FPDF_LoadPage(reopened.get(), 0)); + ASSERT_TRUE(page); + // Shown, a page point (x, y) is at (y, x) from the top left: the button + // spans 100 to 202 across and 100 to 152 down, so 248 to 300 up from the + // bottom. + const Shown shown(page.get()); + EXPECT_EQ(kRed, shown.At(125, 274)); + EXPECT_EQ(kBlue, shown.At(175, 274)); + EXPECT_EQ(kWhite, shown.At(125, 230)); +} + +// A button another app made with a picture reads as having one in every +// layout, and drawing its appearance again never takes the picture out of +// the file: /MK /I still names the same form, and the form is as it was. +// The document lists that form as a drawing, once, whoever made it. +TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { + const std::string bytes = ForeignButtonsPdf(); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + const std::string icon_before = ObjectGraph(doc)[kForeignIcon]; + ASSERT_FALSE(icon_before.empty()); + EXPECT_EQ(std::vector({kForeignIcon}), + DrawingsOf(foreign.get())); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + ASSERT_EQ(7, FPDFPage_GetAnnotCount(page.get())); + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + EXPECT_EQ(layout, EPDFAnnot_GetMKTextPosition(button.get())); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + // Its appearance draws the icon and is no wrapper of ours. + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(button.get())); + + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + } + EXPECT_EQ(icon_before, ObjectGraph(doc)[kForeignIcon]); + EXPECT_EQ(std::vector({kForeignIcon}), + DrawingsOf(foreign.get())); + + // The icon exports as a drawing: its canonical page shows it. + ScopedFPDFDocument exported( + EPDFDoc_ExportDrawing(foreign.get(), kForeignIcon)); + ASSERT_TRUE(exported); + ScopedFPDFPage exported_page(FPDF_LoadPage(exported.get(), 0)); + ASSERT_TRUE(exported_page); + EXPECT_EQ(200, FPDF_GetPageWidthF(exported_page.get())); + EXPECT_EQ(100, FPDF_GetPageHeightF(exported_page.get())); + EXPECT_EQ(kRed, ShownColorAt(exported_page.get(), 50, 50)); + EXPECT_EQ(kBlue, ShownColorAt(exported_page.get(), 150, 50)); +} + +// A button another app made that shows its icon alone keeps showing it when +// its appearance is drawn again: as it is, after a restyle, at a new place +// and size, and turned. +TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignIconOnlyButtonSurvivesARedraw) { + const std::string bytes = ForeignButtonsPdf(); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + // The second widget: /MK /TP 1, standing at (20, 60), 102 by 42. + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 1)); + ASSERT_TRUE(button); + ASSERT_EQ(1, EPDFAnnot_GetMKTextPosition(button.get())); + // As the other app drew it: 80 by 40 in the middle, inside a border of 1. + EXPECT_EQ(kRed, ShownColorAt(page.get(), 51, 81)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 91, 81)); + const uint32_t foreign_appearance = AppearanceOf(button.get())->GetObjNum(); + + // Drawn again, it is the same picture in the same place, in a stream of + // our own. + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_NE(foreign_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(1, 1, 101, 41), drawn->plate); + ExpectRectNear(CFX_FloatRect(11, 1, 91, 41), drawn->picture); + EXPECT_EQ(kRed, ShownColorAt(page.get(), 51, 81)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 91, 81)); + // An icon-only button shows no caption, though the file has one. + EXPECT_EQ(std::string::npos, + StreamBytes(AppearanceOf(button.get()).Get()).find("BT")); + + // A restyle: a background and a wider border. + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BG, 255, 255, 204)); + ASSERT_TRUE(EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + ASSERT_TRUE(EPDFAnnot_SetBorderStyle(button.get(), FPDF_ANNOT_BS_SOLID, 3)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(3, 3, 99, 39), drawn->plate); + ExpectRectNear(CFX_FloatRect(15, 3, 87, 39), drawn->picture); + EXPECT_EQ(kRed, ShownColorAt(page.get(), 51, 81)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 91, 81)); + + // A new place and size: 206 by 106 at (150, 250). + const FS_RECTF kMoved{150, 356, 356, 250}; + ASSERT_TRUE(EPDFAnnot_SetRect(button.get(), &kMoved)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(3, 3, 203, 103), drawn->picture); + EXPECT_EQ(kRed, ShownColorAt(page.get(), 200, 300)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 300, 300)); + + // Turned a quarter turn: laid out 106 by 206, the picture is 100 by 50 in + // its middle and its left is at the bottom. + ASSERT_TRUE(EPDFAnnot_SetMKRotation(button.get(), 90)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + ExpectRectNear(CFX_FloatRect(3, 78, 103, 128), drawn->picture); + EXPECT_EQ(kRed, ShownColorAt(page.get(), 253, 280)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 253, 326)); + button.reset(); +} + +// A button that shows its icon alone draws no text, so its appearance needs +// no /DA and no font: a file that names neither still gets its picture. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconOnlyButtonNeedsNoFont) { + const std::string bytes = ForeignButtonsPdf(/*with_fonts=*/false); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 1)); + ASSERT_TRUE(button); + const uint32_t foreign_appearance = AppearanceOf(button.get())->GetObjNum(); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_NE(foreign_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + EXPECT_EQ(kRed, ShownColorAt(page.get(), 51, 81)); + EXPECT_EQ(kBlue, ShownColorAt(page.get(), 91, 81)); + // Nothing was added for a font it doesn't use. + EXPECT_FALSE(CPDFDocumentFromFPDFDocument(foreign.get()) + ->GetRoot() + ->GetDictFor("AcroForm") + ->KeyExist("DR")); + button.reset(); +} + +// An appearance that is the icon itself (/AP /N and /MK /I naming one form) +// is replaced by a stream of its own: drawing the button never writes into +// its picture. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AnAppearanceThatIsTheIconIsNotWrittenInto) { + ScopedFPDFAnnotation button = NewButton({100, 250, 300, 150}, 0.0f); + ASSERT_TRUE(button); + const unsigned int icon = AddDrawing(doc_.get()); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, icon)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 1)); + MutableDictOf(button.get()) + ->GetOrCreateDictFor("AP") + ->SetNewFor("N", pdf(), icon); + const std::string icon_before = ObjectGraph(pdf())[icon]; + + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_NE(icon, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(icon, DrawnIconNumber(button.get())); + EXPECT_EQ(icon_before, ObjectGraph(pdf())[icon]); + EXPECT_EQ(kRed, ShownColorAt(page_.get(), 150, 200)); + EXPECT_EQ(kBlue, ShownColorAt(page_.get(), 250, 200)); +} + +// Every job of the engine runs in a layer transaction. Inside one, a drawing +// is added and shown as a button's icon and as a mark in another widget; a +// transaction that is dropped leaves both as they were. +TEST_F(EPDFWidgetDrawingEmbedderTest, WorksInsideALayerTransaction) { + const std::string bytes = ForeignButtonsPdf(); + embedpdf_test::LayerFixture foreign; + ASSERT_TRUE(foreign.OpenBytes({bytes.begin(), bytes.end()})); + // The second widget shows its icon alone and stands at (20, 60); the third + // stands at (20, 110). Both are 102 by 42. + constexpr int kIconOnly = 1; + constexpr int kMarked = 2; + + // The picture's page is made before the transaction: a test's own + // documents are layers in layer mode, and no layer opens while a + // transaction is open on its thread. Its canonical drawing is made and + // added inside, as the engine does both in the job. + embedpdf_test::ScopedTestDocument picture = NewPicturePage(Source::kVector); + ASSERT_TRUE(picture); + unsigned int drawing = 0; + ASSERT_TRUE(EPDFLayer_BeginTransaction(foreign.layer)); + { + ScopedFPDFDocument canonical(EPDFDoc_CanonicalDrawing(picture.get(), 0)); + ASSERT_TRUE(canonical); + drawing = EPDFDoc_ImportDrawing(foreign.layer, canonical.get()); + ASSERT_NE(0u, drawing); + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), kIconOnly)); + ScopedFPDFAnnotation marked(FPDFPage_GetAnnot(page.get(), kMarked)); + ASSERT_TRUE(button); + ASSERT_TRUE(marked); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(marked.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(marked.get())); + } + ASSERT_TRUE(EPDFLayer_CommitTransaction(foreign.layer)); + auto expect_both_show = [&foreign, &drawing]() { + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), kIconOnly)); + ScopedFPDFAnnotation marked(FPDFPage_GetAnnot(page.get(), kMarked)); + ASSERT_TRUE(button); + ASSERT_TRUE(marked); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(marked.get())); + // The file's own icon, which five other widgets still name, and ours. + EXPECT_EQ(std::vector({kForeignIcon, drawing}), + DrawingsOf(foreign.layer)); + const Shown shown(page.get()); + EXPECT_EQ(kRed, shown.At(51, 81)); + EXPECT_EQ(kBlue, shown.At(91, 81)); + // The mark fills the widget's height: 84 by 42, in its middle. + EXPECT_EQ(kRed, shown.At(45, 131)); + EXPECT_EQ(kBlue, shown.At(95, 131)); + }; + expect_both_show(); + + ASSERT_TRUE(EPDFLayer_BeginTransaction(foreign.layer)); + { + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), kIconOnly)); + ScopedFPDFAnnotation marked(FPDFPage_GetAnnot(page.get(), kMarked)); + ASSERT_TRUE(button); + ASSERT_TRUE(marked); + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + // Drawing the third widget again takes its mark away. + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(marked.get())); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(marked.get())); + EXPECT_EQ(kWhite, ShownColorAt(page.get(), 51, 81)); + } + ASSERT_TRUE(EPDFLayer_AbortTransaction(foreign.layer)); + expect_both_show(); +} + +// An icon that is replaced or cleared stays an object of the document: the +// appearance still draws it until it is drawn again, and another widget may +// name it. A save writes only what the document still reaches, so once +// nothing shows the old drawing it is in no saved file, full or incremental. +// A drawing nothing shows yet is in none either. +TEST_F(EPDFWidgetDrawingEmbedderTest, AReplacedOrClearedIconIsInNoSavedFile) { + ScopedFPDFAnnotation button = NewButton({100, 250, 300, 150}, 0.0f); + ASSERT_TRUE(button); + const unsigned int first = AddDrawing(doc_.get()); + const unsigned int second = + AddDrawing(doc_.get(), Source::kVector, kGreen, kYellow); + SetIcon(button.get(), first); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(1, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(0, StreamsHolding(bytes, kSecondPictureMark)); + } + + // Replaced and not drawn again: /MK names the new drawing and the + // appearance still draws the old one, so a save holds both. + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, second)); + EXPECT_EQ(first, DrawnIconNumber(button.get())); + EXPECT_EQ(kRed, ShownColorAt(page_.get(), 150, 200)); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(1, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(bytes, kSecondPictureMark)); + } + + // Drawn again, nothing shows the old drawing: neither save holds it. + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(second, DrawnIconNumber(button.get())); + EXPECT_EQ(kGreen, ShownColorAt(page_.get(), 150, 200)); + EXPECT_EQ(kYellow, ShownColorAt(page_.get(), 250, 200)); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(0, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(bytes, kSecondPictureMark)); + } + + // Cleared and drawn again: no picture in either save. + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(0, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(0, StreamsHolding(bytes, kSecondPictureMark)); + } +} + +// A picture the file already held when it was opened is not in a full save +// once it is replaced. An incremental save appends to the file's bytes, so +// the earlier revision's picture is still in them; the reader sees the new +// one. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIncrementalSaveKeepsWhatTheFileHeld) { + { + ScopedFPDFAnnotation button = NewButton({100, 250, 300, 150}, 0.0f); + ASSERT_TRUE(button); + SetIcon(button.get(), AddDrawing(doc_.get())); + } + const std::string earlier = SavedBytes(doc_.get()); + ASSERT_EQ(1, StreamsHolding(earlier, kPictureMark)); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(earlier.data(), earlier.size(), + nullptr)); + ASSERT_TRUE(reopened); + { + ScopedFPDFPage page(FPDF_LoadPage(reopened.get(), 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 0)); + ASSERT_TRUE(button); + SetIcon(button.get(), + AddDrawing(reopened.get(), Source::kVector, kGreen, kYellow)); + } + + const std::string full = SavedBytes(reopened.get()); + EXPECT_EQ(0, StreamsHolding(full, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(full, kSecondPictureMark)); + + const std::string appended = SavedBytes(reopened.get(), FPDF_INCREMENTAL); + ASSERT_GT(appended.size(), earlier.size()); + EXPECT_EQ(earlier, appended.substr(0, earlier.size())); + EXPECT_EQ(1, StreamsHolding(appended, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(appended, kSecondPictureMark)); + embedpdf_test::ScopedTestDocument read(embedpdf_test::LoadMemDocumentForTest( + appended.data(), appended.size(), nullptr)); + ASSERT_TRUE(read); + ScopedFPDFPage page(FPDF_LoadPage(read.get(), 0)); + ASSERT_TRUE(page); + EXPECT_EQ(kGreen, ShownColorAt(page.get(), 150, 200)); + EXPECT_EQ(kYellow, ShownColorAt(page.get(), 250, 200)); +} + +// A push button that has no appearance is not drawn in memory, in any +// layout: a render draws a missing appearance for a text field and a choice +// field only. A button another app left without an /AP shows nothing, its +// picture included, and the render writes nothing. +TEST_F(EPDFWidgetDrawingEmbedderTest, + APushButtonWithoutAnAppearanceIsNotDrawn) { + const std::string bytes = + ForeignButtonsPdf(/*with_fonts=*/true, /*with_appearances=*/false); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + const uint32_t objects = doc->GetLastObjNum(); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + const Shown shown(page.get()); + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + const int bottom = 10 + 50 * layout; + // Where the icon's two halves and the caption would be. + EXPECT_EQ(kWhite, shown.At(51, bottom + 21)); + EXPECT_EQ(kWhite, shown.At(91, bottom + 21)); + EXPECT_EQ(kWhite, shown.At(71, bottom + 21)); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(DictOf(button.get())->KeyExist("AP")); + } + EXPECT_EQ(objects, doc->GetLastObjNum()); +} + +// The generator can draw an icon-only button in memory: a stream of no +// document that draws the file's icon, and nothing is written. No render +// asks for it yet (the test above). +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconOnlyButtonCanBeDrawnInMemory) { + const std::string bytes = + ForeignButtonsPdf(/*with_fonts=*/false, /*with_appearances=*/false); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + const uint32_t objects = doc->GetLastObjNum(); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 1)); + ASSERT_TRUE(button); + + std::optional drawn = + CPDF_AppearanceGenerator::GenerateEphemeralFormAP( + doc, DictOf(button.get()), CPDF_AppearanceGenerator::kPushButton); + ASSERT_TRUE(drawn.has_value()); + ASSERT_TRUE(drawn->normal_stream); + EXPECT_EQ(0u, drawn->normal_stream->GetObjNum()); + EXPECT_EQ(CFX_FloatRect(0, 0, 102, 42), + drawn->normal_stream->GetDict()->GetRectFor("BBox")); + EXPECT_EQ("q\nQ\nq\n1 1 100 40 re W n\n.4 0 0 .4 11 1 cm\n/FRM Do\nQ\n", + StreamBytes(drawn->normal_stream.Get())); + EXPECT_EQ(kForeignIcon, drawn->normal_stream->GetDict() + ->GetDictFor("Resources") + ->GetDictFor("XObject") + ->GetStreamFor("FRM") + ->GetObjNum()); + EXPECT_FALSE(DictOf(button.get())->KeyExist("AP")); + EXPECT_EQ(objects, doc->GetLastObjNum()); +} + +// A refit after a move and a turn gives a widget a new wrapper around the +// same drawing: the drawing stays shared, and the other widget that places +// it is not touched. +TEST_F(EPDFWidgetDrawingEmbedderTest, ARefitKeepsTheDrawingShared) { + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + ScopedFPDFAnnotation moved = NewSignatureWidget({20, 80, 140, 20}); + ScopedFPDFAnnotation kept = NewSignatureWidget({20, 380, 140, 320}); + ASSERT_TRUE(moved); + ASSERT_TRUE(kept); + for (FPDF_ANNOTATION widget : {moved.get(), kept.get()}) { + ASSERT_TRUE(EPDFAnnot_SetDrawing(widget, drawing, EPDF_STAMP_FIT_CONTAIN)); + } + const uint32_t first_wrapper = AppearanceOf(moved.get())->GetObjNum(); + const uint32_t kept_wrapper = AppearanceOf(kept.get())->GetObjNum(); + const std::map before = ObjectGraph(pdf()); + + // Moved to (200, 100), 40 wide and 160 tall, and turned a quarter turn. + const FS_RECTF kStands{200, 260, 240, 100}; + ASSERT_TRUE(EPDFAnnot_SetRect(moved.get(), &kStands)); + ASSERT_TRUE(EPDFAnnot_SetMKRotation(moved.get(), 90)); + ASSERT_TRUE( + EPDFAnnot_UpdateAppearanceToRect(moved.get(), EPDF_STAMP_FIT_CONTAIN)); + + RetainPtr wrapper = AppearanceOf(moved.get()); + ASSERT_TRUE(wrapper); + EXPECT_NE(first_wrapper, wrapper->GetObjNum()); + EXPECT_EQ(CFX_FloatRect(0, 0, 160, 40), + wrapper->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(CFX_Matrix(0, 1, -1, 0, 40, 0), + wrapper->GetDict()->GetMatrixFor("Matrix")); + EXPECT_EQ("q .4 0 0 .4 40 0 cm /EPDFWRAP Do Q", StreamBytes(wrapper.Get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(moved.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(kept.get())); + EXPECT_EQ(kept_wrapper, AppearanceOf(kept.get())->GetObjNum()); + const std::map after = ObjectGraph(pdf()); + EXPECT_EQ(before.at(drawing), after.at(drawing)); + EXPECT_EQ(before.at(kept_wrapper), after.at(kept_wrapper)); + EXPECT_EQ(before.at(EPDFAnnot_GetObjectNumber(kept.get())), + after.at(EPDFAnnot_GetObjectNumber(kept.get()))); + EXPECT_EQ(std::vector({drawing}), DrawingsOf(doc_.get())); + + const Shown shown(page_.get()); + EXPECT_EQ(kRed, shown.At(220, 160)); + EXPECT_EQ(kBlue, shown.At(220, 200)); + EXPECT_EQ(kRed, shown.At(50, 350)); + EXPECT_EQ(kBlue, shown.At(110, 350)); + moved.reset(); + kept.reset(); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + EXPECT_EQ(1, + PicturesIn(SavedBytes(doc_.get(), save.flags), Source::kVector)); + } +} + +// The same picture as ten stamps, in ten signature widgets and in ten image +// fields is one drawing: the document lists one, and a saved file holds the +// picture once, full and incremental. +TEST_F(EPDFWidgetDrawingEmbedderTest, StampsMarksAndIconsShareOneDrawing) { + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + embedpdf_test::ScopedTestDocument document(NewTestDocument()); + ASSERT_TRUE(document); + ScopedFPDFPage page(FPDFPage_New(document.get(), 0, 400, 400)); + ASSERT_TRUE(page); + const unsigned int drawing = AddDrawing(document.get(), source); + ASSERT_NE(0u, drawing); + + // Three columns of ten boxes, 100 by 30: stamps, marks, icons. + auto box_at = [](int column, int row) { + const float left = 20 + 130 * column; + const float bottom = 20 + 36 * row; + return FS_RECTF{left, bottom + 30, left + 100, bottom}; + }; + for (int row = 0; row < 10; ++row) { + ScopedFPDFAnnotation stamp = + NewAnnot(page.get(), FPDF_ANNOT_STAMP, box_at(0, row)); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(stamp.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(stamp.get())); + + ScopedFPDFAnnotation marked = + NewWidget(document.get(), + NewField(document.get(), EPDF_FORMFIELD_FAMILY_SIGNATURE), + page.get(), box_at(1, row)); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(marked.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(drawing, EPDFAnnot_GetDrawing(marked.get())); + + ScopedFPDFAnnotation button = + NewWidget(document.get(), + NewField(document.get(), EPDF_FORMFIELD_FAMILY_PUSHBUTTON), + page.get(), box_at(2, row), 0.0f); + SetIcon(button.get(), drawing); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + } + EXPECT_EQ(std::vector({drawing}), DrawingsOf(document.get())); + + // Each of the thirty shows it: 60 by 30, in the middle of its box. + const Shown shown(page.get()); + for (int column = 0; column < 3; ++column) { + for (int row = 0; row < 10; ++row) { + SCOPED_TRACE(testing::Message() << column << ", " << row); + const FS_RECTF box = box_at(column, row); + const int left = static_cast(box.left); + const int middle = static_cast(box.bottom) + 15; + EXPECT_EQ(kRed, shown.At(left + 35, middle)); + EXPECT_EQ(kBlue, shown.At(left + 65, middle)); + EXPECT_EQ(kWhite, shown.At(left + 10, middle)); + } + } + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(document.get(), save.flags); + EXPECT_EQ(1, PicturesIn(bytes, source)); + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(reopened); + EXPECT_EQ(1u, DrawingsOf(reopened.get()).size()); + } + } +} + +// After a reopen the document lists the drawing its stamp, its mark or its +// icon shows, and that drawing exports as the picture's canonical bytes: the +// engine knows it by its content, and places it again instead of adding a +// copy. +TEST_F(EPDFWidgetDrawingEmbedderTest, AReopenedFilePlacesItsDrawingAgain) { + enum class Place { kStamp, kMark, kIcon }; + const struct { + const char* name; + Place place; + } kPlaces[] = {{"a stamp", Place::kStamp}, + {"a mark", Place::kMark}, + {"an icon", Place::kIcon}}; + // Shows `drawing` in a new place of one kind, on a page of `document`. + auto show = [this](FPDF_DOCUMENT document, FPDF_PAGE page, Place place, + unsigned int drawing, const FS_RECTF& rect) { + if (place == Place::kStamp) { + ScopedFPDFAnnotation stamp = NewAnnot(page, FPDF_ANNOT_STAMP, rect); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(stamp.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + return; + } + ScopedFPDFAnnotation widget = + NewWidget(document, + NewField(document, place == Place::kMark + ? EPDF_FORMFIELD_FAMILY_SIGNATURE + : EPDF_FORMFIELD_FAMILY_PUSHBUTTON), + page, rect, 0.0f); + ASSERT_TRUE(widget); + if (place == Place::kMark) { + ASSERT_TRUE( + EPDFAnnot_SetDrawing(widget.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + } else { + SetIcon(widget.get(), drawing); + } + }; + + for (Source source : kSources) { + SCOPED_TRACE(NameOf(source)); + const std::string canonical = CanonicalBytes(source); + ASSERT_FALSE(canonical.empty()); + for (const auto& first : kPlaces) { + SCOPED_TRACE(first.name); + std::string saved; + { + embedpdf_test::ScopedTestDocument document(NewTestDocument()); + ScopedFPDFPage page(FPDFPage_New(document.get(), 0, 400, 400)); + ASSERT_TRUE(page); + show(document.get(), page.get(), first.place, + AddDrawing(document.get(), source), {20, 80, 140, 20}); + saved = SavedBytes(document.get()); + } + ASSERT_EQ(1, PicturesIn(saved, source)); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(saved.data(), saved.size(), + nullptr)); + ASSERT_TRUE(reopened); + const std::vector drawings = DrawingsOf(reopened.get()); + ASSERT_EQ(1u, drawings.size()); + { + ScopedFPDFDocument exported( + EPDFDoc_ExportDrawing(reopened.get(), drawings[0])); + ASSERT_TRUE(exported); + EXPECT_EQ(canonical, SavedBytes(exported.get())); + } + // The same picture again, in a place of every kind. + { + ScopedFPDFPage page(FPDF_LoadPage(reopened.get(), 0)); + ASSERT_TRUE(page); + int row = 0; + for (const auto& again : kPlaces) { + const float bottom = 120 + 80 * row++; + show(reopened.get(), page.get(), again.place, drawings[0], + {20, bottom + 60, 140, bottom}); + } + const Shown shown(page.get()); + for (int place = 0; place < 4; ++place) { + // 120 by 60 in each box: red on the left, blue on the right. + const int middle = (place == 0 ? 20 : 40 + 80 * place) + 30; + EXPECT_EQ(kRed, shown.At(50, middle)) << place; + EXPECT_EQ(kBlue, shown.At(110, middle)) << place; + } + } + EXPECT_EQ(drawings, DrawingsOf(reopened.get())); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + EXPECT_EQ(1, + PicturesIn(SavedBytes(reopened.get(), save.flags), source)); + } + } + } +} + +// Replacing one mark and clearing one icon leaves the stamp, the mark and +// the icon that show the same drawing as they were. Once the last place +// that showed a drawing shows another, or none, a saved file no longer +// holds it. +TEST_F(EPDFWidgetDrawingEmbedderTest, ChangingOnePlaceLeavesTheOthers) { + const unsigned int first = AddDrawing(doc_.get()); + const unsigned int second = + AddDrawing(doc_.get(), Source::kVector, kGreen, kYellow); + ASSERT_NE(0u, first); + ASSERT_NE(0u, second); + // Five boxes, 120 by 60, one above the other. + auto box_at = [](int row) { + const float bottom = 20 + 70 * row; + return FS_RECTF{20, bottom + 60, 140, bottom}; + }; + ScopedFPDFAnnotation stamp = + NewAnnot(page_.get(), FPDF_ANNOT_STAMP, box_at(0)); + ScopedFPDFAnnotation replaced_mark = NewSignatureWidget(box_at(1)); + ScopedFPDFAnnotation kept_mark = NewSignatureWidget(box_at(2)); + ScopedFPDFAnnotation cleared_icon = NewButton(box_at(3), 0.0f); + ScopedFPDFAnnotation kept_icon = NewButton(box_at(4), 0.0f); + for (FPDF_ANNOTATION placed : + {stamp.get(), replaced_mark.get(), kept_mark.get()}) { + ASSERT_TRUE(EPDFAnnot_SetDrawing(placed, first, EPDF_STAMP_FIT_CONTAIN)); + } + SetIcon(cleared_icon.get(), first); + SetIcon(kept_icon.get(), first); + EXPECT_EQ(std::vector({first}), DrawingsOf(doc_.get())); + + FPDF_ANNOTATION kept[] = {stamp.get(), kept_mark.get(), kept_icon.get()}; + std::vector kept_appearances; + for (FPDF_ANNOTATION annot : kept) { + kept_appearances.push_back(AppearanceOf(annot)->GetObjNum()); + } + const std::map before = ObjectGraph(pdf()); + + // One mark shows the second drawing; one icon is cleared. + ASSERT_TRUE(EPDFAnnot_SetDrawing(replaced_mark.get(), second, + EPDF_STAMP_FIT_CONTAIN)); + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(cleared_icon.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(cleared_icon.get())); + + const std::map after = ObjectGraph(pdf()); + EXPECT_EQ(before.at(first), after.at(first)); + for (size_t i = 0; i < std::size(kept); ++i) { + SCOPED_TRACE(i); + EXPECT_EQ(kept_appearances[i], AppearanceOf(kept[i])->GetObjNum()); + EXPECT_EQ(before.at(kept_appearances[i]), after.at(kept_appearances[i])); + } + EXPECT_EQ(first, EPDFAnnot_GetDrawing(stamp.get())); + EXPECT_EQ(first, EPDFAnnot_GetDrawing(kept_mark.get())); + EXPECT_EQ(first, EPDFAnnot_GetMKIcon(kept_icon.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(second, EPDFAnnot_GetDrawing(replaced_mark.get())); + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(cleared_icon.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(std::vector({first, second}), DrawingsOf(doc_.get())); + { + const Shown shown(page_.get()); + const uint32_t kLeft[] = {kRed, kGreen, kRed, kWhite, kRed}; + const uint32_t kRight[] = {kBlue, kYellow, kBlue, kWhite, kBlue}; + for (int row = 0; row < 5; ++row) { + SCOPED_TRACE(row); + EXPECT_EQ(kLeft[row], shown.At(50, 50 + 70 * row)); + EXPECT_EQ(kRight[row], shown.At(110, 50 + 70 * row)); + } + } + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(1, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(bytes, kSecondPictureMark)); + } + + // The last three places of the first drawing: the stamp and the icon show + // the second, and the mark is cleared (a signature widget drawn again is + // its box). + ASSERT_TRUE( + EPDFAnnot_SetDrawing(stamp.get(), second, EPDF_STAMP_FIT_CONTAIN)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(kept_mark.get())); + SetIcon(kept_icon.get(), second); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(kept_mark.get())); + EXPECT_EQ(std::vector({second}), DrawingsOf(doc_.get())); + for (const auto& save : kSaves) { + SCOPED_TRACE(save.name); + const std::string bytes = SavedBytes(doc_.get(), save.flags); + EXPECT_EQ(0, StreamsHolding(bytes, kPictureMark)); + EXPECT_EQ(1, StreamsHolding(bytes, kSecondPictureMark)); + } +} + +// The document's list is the drawings its annotations show: none for a form +// of text fields, whose appearances draw no form at all. +TEST_F(EPDFWidgetDrawingEmbedderTest, AFormWithoutPicturesListsNoDrawing) { + for (int row = 0; row < 3; ++row) { + const uint32_t field = NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_TEXT); + ASSERT_GT(field, 0u); + const float bottom = 20 + 40 * row; + ScopedFPDFAnnotation widget = NewWidget(doc_.get(), field, page_.get(), + {20, bottom + 30, 220, bottom}); + ASSERT_TRUE(widget); + ASSERT_TRUE(embedpdf_test::FillTextField(doc_.get(), field, L"Hello")); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(widget.get())); + } + // A drawing nothing shows is not listed either. + ASSERT_NE(0u, AddDrawing(doc_.get())); + EXPECT_TRUE(DrawingsOf(doc_.get()).empty()); + EXPECT_EQ(0u, EPDFDoc_GetDrawings(nullptr, nullptr, 0)); +} + +// Only a stamp and a widget place a drawing. Every other kind of annotation +// is refused, and reads as placing none, even when its appearance is a +// wrapper of ours; nothing is written, and on a layer nothing is copied up. +// The document lists what its stamps and widgets show, and nothing else. +TEST_F(EPDFWidgetDrawingEmbedderTest, OnlyAStampAndAWidgetPlaceADrawing) { + const std::string bytes = EverySubtypePdf(); + embedpdf_test::LayerFixture every; + ASSERT_TRUE(every.OpenBytes({bytes.begin(), bytes.end()})); + const int others = static_cast(std::size(kOtherSubtypes)); + { + ScopedFPDFPage page(FPDF_LoadPage(every.layer, 0)); + ASSERT_TRUE(page); + ASSERT_EQ(others + 2, FPDFPage_GetAnnotCount(page.get())); + for (int i = 0; i < others; ++i) { + SCOPED_TRACE(kOtherSubtypes[i]); + ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), i)); + ASSERT_TRUE(annot); + EXPECT_EQ(kOtherSubtypes[i], DictOf(annot.get())->GetNameFor("Subtype")); + EXPECT_FALSE(EPDFAnnot_SetDrawing(annot.get(), kWrappedDrawing, + EPDF_STAMP_FIT_CONTAIN)); + EXPECT_EQ(0u, EPDFAnnot_GetDrawing(annot.get())); + } + // The stamp and the widget read the drawing their wrapper places. + for (int i = others; i < others + 2; ++i) { + ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), i)); + ASSERT_TRUE(annot); + EXPECT_EQ(kWrappedDrawing, EPDFAnnot_GetDrawing(annot.get())) << i; + } + } + EXPECT_EQ(std::vector({kWrappedDrawing}), + DrawingsOf(every.layer)); + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(every.layer)); + EXPECT_TRUE(every.SaveDelta().empty()); + + // And they take one: each gets a wrapper of its own around it. + ScopedFPDFPage page(FPDF_LoadPage(every.layer, 0)); + ASSERT_TRUE(page); + for (int i = others; i < others + 2; ++i) { + ScopedFPDFAnnotation annot(FPDFPage_GetAnnot(page.get(), i)); + ASSERT_TRUE(annot); + ASSERT_TRUE(EPDFAnnot_SetDrawing(annot.get(), kWrappedDrawing, + EPDF_STAMP_FIT_CONTAIN)) + << i; + EXPECT_EQ(kWrappedDrawing, EPDFAnnot_GetDrawing(annot.get())) << i; + EXPECT_NE(5u, AppearanceOf(annot.get())->GetObjNum()) << i; + } + EXPECT_FALSE(EPDFLayer_IsObjectPromoted(every.layer, kWrappedDrawing)); +} + +// A mark placed the way the engine placed one before drawings served +// widgets (a page given to the widget, then fitted) is a wrapper around a +// drawing of its own. The file here was saved by the fork before this +// change, with the same picture marked into two signature widgets. Each +// mark reads as a drawing and the document lists both; both export as the +// picture's canonical bytes, so the engine knows them as one picture and +// places one of them from then on. +TEST_F(EPDFWidgetDrawingEmbedderTest, AMarkSavedBeforeThisChangeIsADrawing) { + ASSERT_TRUE(OpenDocument("embedpdf_widget_marks_from_page.pdf")); + FPDF_PAGE page = LoadPage(0); + ASSERT_TRUE(page); + ASSERT_EQ(2, FPDFPage_GetAnnotCount(page)); + std::vector marks; + for (int i = 0; i < 2; ++i) { + ScopedFPDFAnnotation widget(FPDFPage_GetAnnot(page, i)); + ASSERT_TRUE(widget); + ASSERT_EQ(FPDF_ANNOT_WIDGET, FPDFAnnot_GetSubtype(widget.get())); + marks.push_back(EPDFAnnot_GetDrawing(widget.get())); + EXPECT_NE(0u, marks.back()); + } + // A drawing each, as each call made one. + EXPECT_NE(marks[0], marks[1]); + EXPECT_EQ(marks, DrawingsOf(document())); + const std::string canonical = CanonicalBytes(Source::kVector); + for (unsigned int mark : marks) { + ScopedFPDFDocument exported(EPDFDoc_ExportDrawing(document(), mark)); + ASSERT_TRUE(exported); + EXPECT_EQ(canonical, SavedBytes(exported.get())) << mark; + } + { + const Shown shown(page); + EXPECT_EQ(kRed, shown.At(50, 50)); + EXPECT_EQ(kBlue, shown.At(110, 50)); + EXPECT_EQ(kRed, shown.At(50, 150)); + EXPECT_EQ(kBlue, shown.At(110, 150)); + } + EXPECT_EQ(2, PicturesIn(SavedBytes(document()), Source::kVector)); + + // The picture again: the first drawing is placed, in a new signature + // widget and as a new image field's icon. No third copy. + { + const uint32_t field = + NewField(document(), EPDF_FORMFIELD_FAMILY_SIGNATURE); + ScopedFPDFAnnotation marked = + NewWidget(document(), field, page, {20, 280, 140, 220}); + ASSERT_TRUE(marked); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(marked.get(), marks[0], EPDF_STAMP_FIT_CONTAIN)); + const uint32_t button = + NewField(document(), EPDF_FORMFIELD_FAMILY_PUSHBUTTON); + ScopedFPDFAnnotation image = + NewWidget(document(), button, page, {20, 380, 140, 320}, 0.0f); + ASSERT_TRUE(image); + SetIcon(image.get(), marks[0]); + } + EXPECT_EQ(marks, DrawingsOf(document())); + EXPECT_EQ(2, PicturesIn(SavedBytes(document()), Source::kVector)); + { + const Shown shown(page); + EXPECT_EQ(kRed, shown.At(50, 250)); + EXPECT_EQ(kBlue, shown.At(110, 250)); + EXPECT_EQ(kRed, shown.At(50, 350)); + EXPECT_EQ(kBlue, shown.At(110, 350)); + } + + // The second mark placed again from the first drawing: one picture is + // left in a full save. An incremental save keeps the file it appends to. + { + ScopedFPDFAnnotation second(FPDFPage_GetAnnot(page, 1)); + ASSERT_TRUE(second); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(second.get(), marks[0], EPDF_STAMP_FIT_CONTAIN)); + } + EXPECT_EQ(std::vector({marks[0]}), DrawingsOf(document())); + EXPECT_EQ(1, PicturesIn(SavedBytes(document()), Source::kVector)); + EXPECT_EQ( + 2, PicturesIn(SavedBytes(document(), FPDF_INCREMENTAL), Source::kVector)); + UnloadPage(page); +} + +// Listing a document's drawings reads the content of the appearances that +// draw a form, and of no other: a text field's appearance names no XObject, +// so nothing is decoded for it, however many such widgets a form has. +TEST_F(EPDFWidgetDrawingEmbedderTest, ListingReadsNoAppearanceThatDrawsNoForm) { + // Gives `widget` an appearance whose data counts its fetches. + auto count_appearance = [this](FPDF_ANNOTATION widget, + const std::string& content, + RetainPtr resources) { + RetainPtr data = pdfium::MakeRetain(content); + auto dict = pdfium::MakeRetain(); + dict->SetNewFor("Type", "XObject"); + dict->SetNewFor("Subtype", "Form"); + dict->SetRectFor("BBox", DictOf(widget)->GetRectFor("Rect")); + dict->SetFor("Resources", std::move(resources)); + auto stream = pdf()->NewIndirect(data, std::move(dict)); + MutableDictOf(widget)->GetOrCreateDictFor("AP")->SetNewFor( + "N", pdf(), stream->GetObjNum()); + return data; + }; + + // A text field with three widgets, as a form has them: each appearance + // draws text in a font and names no XObject. + const uint32_t text = NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_TEXT); + ASSERT_GT(text, 0u); + std::vector> text_appearances; + for (int row = 0; row < 3; ++row) { + const float bottom = 20 + 40 * row; + ScopedFPDFAnnotation widget = NewWidget(doc_.get(), text, page_.get(), + {20, bottom + 30, 220, bottom}); + ASSERT_TRUE(widget); + auto resources = pdfium::MakeRetain(); + resources->SetNewFor("Font"); + text_appearances.push_back(count_appearance( + widget.get(), "/Tx BMC BT /Helv 12 Tf 2 10 Td (Hello) Tj ET EMC", + std::move(resources))); + } + // An image field: its appearance draws its icon, a form. + const unsigned int drawing = AddDrawing(doc_.get()); + ScopedFPDFAnnotation button = NewButton({20, 300, 122, 258}); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); + auto resources = pdfium::MakeRetain(); + resources->SetNewFor("XObject")->SetNewFor( + "FRM", pdf(), drawing); + RetainPtr icon_appearance = count_appearance( + button.get(), "q 1 1 100 40 re W n q .4 0 0 .4 11 1 cm /FRM Do Q Q", + std::move(resources)); + + EXPECT_EQ(std::vector({drawing}), DrawingsOf(doc_.get())); + for (const RetainPtr& appearance : text_appearances) { + EXPECT_EQ(0, appearance->fetches()); + } + // The push button's appearance draws a form, so its content is read to + // see whether it is a wrapper. It is not: what is listed is its icon. + EXPECT_GT(icon_appearance->fetches(), 0); +} + +// Listing writes nothing: on a layer, outside a transaction and inside one, +// no object is copied up and the layer has no change to save. +TEST_F(EPDFWidgetDrawingEmbedderTest, ListingDrawingsWritesNothing) { + // A file with a stamp, a mark and an icon that share one drawing. + std::string saved; + { + const unsigned int drawing = AddDrawing(doc_.get()); + ScopedFPDFAnnotation stamp = + NewAnnot(page_.get(), FPDF_ANNOT_STAMP, {20, 80, 140, 20}); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(stamp.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + ScopedFPDFAnnotation marked = NewSignatureWidget({20, 180, 140, 120}); + ASSERT_TRUE( + EPDFAnnot_SetDrawing(marked.get(), drawing, EPDF_STAMP_FIT_CONTAIN)); + ScopedFPDFAnnotation button = NewButton({20, 280, 140, 220}, 0.0f); + SetIcon(button.get(), drawing); + stamp.reset(); + marked.reset(); + button.reset(); + saved = SavedBytes(doc_.get()); + } + embedpdf_test::LayerFixture file; + ASSERT_TRUE(file.OpenBytes({saved.begin(), saved.end()})); + EXPECT_EQ(1u, DrawingsOf(file.layer).size()); + ASSERT_TRUE(EPDFLayer_BeginTransaction(file.layer)); + EXPECT_EQ(1u, DrawingsOf(file.layer).size()); + ASSERT_TRUE(EPDFLayer_CommitTransaction(file.layer)); + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(file.layer)); + EXPECT_TRUE(file.SaveDelta().empty()); +} diff --git a/fpdfsdk/epdf_wrapped_appearance.cpp b/fpdfsdk/epdf_wrapped_appearance.cpp index 760dd2d866..f79c324224 100644 --- a/fpdfsdk/epdf_wrapped_appearance.cpp +++ b/fpdfsdk/epdf_wrapped_appearance.cpp @@ -317,6 +317,17 @@ EpdfDrawnForm EpdfFindDrawnForm(const CPDF_Stream* ap, std::optional EpdfFindWrappedAppearance( const CPDF_Stream* ap, std::optional opacity) { + // A wrapper draws its drawing as an XObject, and so does everything an + // editor puts around one. An appearance that names none holds no wrapper, + // and its content needn't be read to know: a form's text fields are asked + // this one by one (EPDFDoc_GetDrawings()). + RetainPtr resources = + ap->GetDict()->GetDictFor("Resources"); + RetainPtr xobjects = + resources ? resources->GetDictFor("XObject") : nullptr; + if (!xobjects || xobjects->size() == 0) { + return std::nullopt; + } // Our wrapper is never a form that adds nothing (it places its drawing), so // it is where the walk ends, when there is one. EpdfDrawnForm drawn = EpdfFindDrawnForm(ap, opacity); diff --git a/public/epdf_annot.h b/public/epdf_annot.h index 9a70f5d6c5..df6e2a8509 100644 --- a/public/epdf_annot.h +++ b/public/epdf_annot.h @@ -1252,8 +1252,8 @@ EPDFAnnot_SetAppearanceFromPage(FPDF_ANNOTATION annot, // dictionary describes, as a new single-page document: the drawing a copy of // the annotation is created from. // -// - EmbedPDF's own wrapper (the appearance a stamp gets from -// EPDFAnnot_SetStampDrawing or EPDFAnnot_UpdateAppearanceToRect), also +// - EmbedPDF's own wrapper (the appearance a stamp or a widget gets from +// EPDFAnnot_SetDrawing or EPDFAnnot_UpdateAppearanceToRect), also // when another editor has put forms that only draw it around it: the // drawing it places, as EPDFDoc_ExportDrawing() exports it, without the // wrapper's placement, rotation and opacity. @@ -1268,9 +1268,10 @@ EPDFAnnot_SetAppearanceFromPage(FPDF_ANNOTATION annot, // The page is the canonical form of a drawing: its content is exactly // `/EPDFDRAWING Do`, the placement is folded into the form's /Matrix, and // the document has no /Info and an /ID made from its content. The same -// drawing is the same bytes on every runtime, and EPDFAnnot_SetAppearanceFromPage -// and EPDFDoc_ImportDrawing adopt the form as it is, so exporting what they -// made gives the same bytes again. The source document is untouched. +// drawing is the same bytes on every runtime, and +// EPDFAnnot_SetAppearanceFromPage and EPDFDoc_ImportDrawing adopt the form as +// it is, so exporting what they made gives the same bytes again. The source +// document is untouched. // // annot - handle to an annotation with a normal appearance. // degrees - the turn the annotation's data describes, counterclockwise @@ -1284,14 +1285,20 @@ EPDFAnnot_ExportAppearance(FPDF_ANNOTATION annot, float degrees, const FS_RECTF* unrotated_box); -// Stamp drawings. +// Drawings. // -// A stamp's drawing is its artwork, one form. Each stamp places it with a -// wrapper of its own, which holds the stamp's box, fit, rotation and -// opacity, so stamps with the same artwork can share one drawing. A drawing -// is never edited in place: a stamp that changes gets a new wrapper, and new -// artwork is a new drawing. A drawing is known by its content, the -// EPDF_DIGEST_SHA256 (EPDF_DigestBuffer()) of its canonical bytes. +// A drawing is a picture the document holds once: one form XObject. It shows +// in three places, and every place can share it: +// - a stamp's artwork, and a mark in a widget (a signature field's): each +// places the drawing with a wrapper of its own, which holds the box, +// fit, rotation and opacity (EPDFAnnot_SetDrawing()); +// - a push button's icon: /MK names the drawing, and the button's +// appearance draws it (EPDFAnnot_SetMKIcon()). +// A drawing is never edited in place: a place that changes gets a new +// wrapper or a new appearance, and a new picture is a new drawing. Placing a +// drawing only reads it, so it can be one an earlier revision of the file +// holds. A drawing is known by its content, the EPDF_DIGEST_SHA256 +// (EPDF_DigestBuffer()) of its canonical bytes. // Experimental EmbedPDF Extension API. // The canonical drawing of a page, in a new single-page document: the bytes @@ -1334,9 +1341,11 @@ FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV EPDFDoc_ExportDrawing(FPDF_DOCUMENT document, unsigned int drawing); // Experimental EmbedPDF Extension API. -// The drawings EmbedPDF wrappers place on the stamps of |document|'s pages, -// each once, in page and /Annots order. Reads the page dictionaries; no page -// is loaded. A stamp without our wrapper has none. +// The drawings the annotations on |document|'s pages show, each once, in +// page and /Annots order: the one an EmbedPDF wrapper places on a stamp or +// in a widget, then the forms a widget's /MK names as its icons (/I, /RI, +// /IX), whoever wrote them. Reads the page dictionaries; no page is loaded, +// and an appearance that draws no form is not read at all. // // document - handle to the document. // buffer - receives up to |buflen| object numbers; may be NULL. @@ -1344,35 +1353,45 @@ EPDFDoc_ExportDrawing(FPDF_DOCUMENT document, unsigned int drawing); // // Returns the number of drawings, which may exceed |buflen|. FPDF_EXPORT unsigned long FPDF_CALLCONV -EPDFDoc_GetStampDrawings(FPDF_DOCUMENT document, - unsigned int* buffer, - unsigned long buflen); +EPDFDoc_GetDrawings(FPDF_DOCUMENT document, + unsigned int* buffer, + unsigned long buflen); // Experimental EmbedPDF Extension API. -// Make a stamp place |drawing|: a new wrapper that fits it into the stamp's -// box with |fit|, honouring /EMBD_Metadata rotation, under a layer that -// paints /CA below full opacity, as EPDFAnnot_UpdateAppearanceToRect() does. -// The drawing is not changed, so other stamps can place it too. +// Make a stamp or a widget place |drawing|: a new wrapper that fits it into +// the annotation's box with |fit|, exactly as +// EPDFAnnot_UpdateAppearanceToRect() fits one. A stamp's box and turn are the +// ones /EMBD_Metadata records; a widget's are /Rect and /MK /R, and its +// /Rect and /MK are not written. Below full opacity the wrapper sits under a +// layer that paints /CA. The drawing is not changed, so other annotations +// can place it too, and a later EPDFAnnot_UpdateAppearanceToRect() gives the +// annotation a new wrapper around the same drawing. // -// annot - handle to a Stamp annotation. +// In a widget this is a mark, a signature field's: the wrapper is the whole +// appearance, and the field's box is not drawn under it. A push button's +// picture is its icon (EPDFAnnot_SetMKIcon()). +// +// annot - handle to a Stamp or a Widget annotation. // drawing - object number of a drawing of the annotation's document. // fit - one of EPDF_STAMP_FIT_*. // -// Returns true on success. -FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV -EPDFAnnot_SetStampDrawing(FPDF_ANNOTATION annot, - unsigned int drawing, - EPDF_STAMP_FIT fit); +// Returns true on success. False, writing nothing, for another subtype, a +// |drawing| that is no form XObject of the document, and an annotation +// without area. +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetDrawing(FPDF_ANNOTATION annot, + unsigned int drawing, + EPDF_STAMP_FIT fit); // Experimental EmbedPDF Extension API. -// The drawing a stamp's wrapper places. +// The drawing the wrapper of a stamp or a widget places. // -// annot - handle to a Stamp annotation. +// annot - handle to a Stamp or a Widget annotation. // -// Returns the drawing's object number, or 0 for a stamp without our wrapper, -// or an annotation that is not a stamp. +// Returns the drawing's object number, or 0 for an appearance that is not +// our wrapper (a push button's icon is read with EPDFAnnot_GetMKIcon()), or +// another subtype. FPDF_EXPORT unsigned int FPDF_CALLCONV -EPDFAnnot_GetStampDrawing(FPDF_ANNOTATION annot); +EPDFAnnot_GetDrawing(FPDF_ANNOTATION annot); // Experimental EmbedPDF Extension API. // Get the annotation rectangle with normalization applied. @@ -1615,6 +1634,59 @@ EPDFAnnot_SetMKTextPosition(FPDF_ANNOTATION annot, int position); FPDF_EXPORT int FPDF_CALLCONV EPDFAnnot_GetMKTextPosition(FPDF_ANNOTATION annot); +// The icons of a widget's /MK dictionary (push buttons). +#define EPDF_MK_ICON_I 0 // /I, the normal icon +#define EPDF_MK_ICON_RI 1 // /RI, the rollover icon +#define EPDF_MK_ICON_IX 2 // /IX, the down icon + +// Experimental EmbedPDF Extension API. +// Make a widget's /MK name |drawing| as its icon |which| (EPDF_MK_ICON_*): a +// drawing of the widget's document, by object number (EPDFDoc_ImportDrawing() +// adds one; other widgets and stamps can show the same one). Only that entry +// is written: not /MK /TP (EPDFAnnot_SetMKTextPosition()), not /MK /IF, and +// the appearance is not regenerated (EPDFAnnot_GenerateFormFieldAP()). An +// image field as Acrobat writes one is a push button with the normal icon, +// /MK /TP 1 and an appearance drawn from them. The drawing is only read. +// +// An icon the entry named before is not deleted: the appearance may still +// draw it, and another widget may share it. A save writes only what the +// document still reaches, so once the appearance is drawn again an icon +// nothing else shows is in no saved file. An incremental save appends, and +// keeps the bytes of the file it appends to. +// +// annot - handle to a widget annotation. +// which - one of EPDF_MK_ICON_*. +// drawing - object number of a drawing of the annotation's document. +// +// Returns false, writing nothing, for another subtype, an unknown |which| +// and a |drawing| that is no form XObject of the document. Naming the +// drawing the entry already names writes nothing. +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKIcon(FPDF_ANNOTATION annot, + int which, + unsigned int drawing); + +// Experimental EmbedPDF Extension API. +// The form a widget's /MK names as its icon |which| (EPDF_MK_ICON_*), by +// object number: a drawing EPDFDoc_ExportDrawing() exports, whoever wrote it. +// 0 without one, for another subtype and an unknown |which|. +FPDF_EXPORT unsigned int FPDF_CALLCONV +EPDFAnnot_GetMKIcon(FPDF_ANNOTATION annot, int which); + +// Experimental EmbedPDF Extension API. +// Remove a widget's /MK icon |which| (EPDF_MK_ICON_*). The appearance is not +// regenerated (EPDFAnnot_GenerateFormFieldAP()), and the icon is not deleted, +// as EPDFAnnot_SetMKIcon() leaves the one it replaces. Returns false +// for another subtype and an unknown |which|; true when there was no icon to +// remove. +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_ClearMKIcon(FPDF_ANNOTATION annot, + int which); + +// Experimental EmbedPDF Extension API. +// Whether a widget's /MK holds the icon |which| (EPDF_MK_ICON_*): a stream. +// False for another subtype and an unknown |which|. +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_HasMKIcon(FPDF_ANNOTATION annot, + int which); + // Experimental EmbedPDF Extension API. // Set a widget's /MK /R: how far its appearance turns, in degrees // counterclockwise, a multiple of 90 from 0 to 270 (0 removes /R). The @@ -1637,7 +1709,13 @@ FPDF_EXPORT int FPDF_CALLCONV EPDFAnnot_GetMKRotation(FPDF_ANNOTATION annot); // - /Btn -> a check box or radio button appearance: both states, the // symbol /MK /CA names in the /DA colour. A push button: its // background and border, and its caption (/MK /CA) centred in -// the /DA font and colour; an icon (/MK /I) is not drawn. +// the /DA font and colour. A push button that shows its icon +// alone (/MK /TP 1): its background and border, and the normal +// icon (/MK /I) inside the border, fitted as /MK /IF says (ISO +// 32000-2 Table 250; without one: always scaled, keeping its +// proportions, in the middle). It draws no caption, and without +// an icon it is the box alone. In the layouts that show a +// caption and an icon (/MK /TP 2 to 6) the icon is not drawn. // - /Sig, and a widget in no field -> its box: the background and border // as a text field draws them. It replaces whatever the widget // shows, a signature drawn into it included. diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 8b7ffc529e..725e17a606 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -340,6 +340,7 @@ EPDFSaveEmbedderTest.SavePreservesConsistentObjectGenerations 1 EPDFSignatureEmbedderTest.CertifyThenRefuseSecondCertification 9 EPDFSignatureEmbedderTest.CreateSignatureFieldAndLock 6 EPDFSignatureEmbedderTest.PermissionOneAllowsOnlyTimestamps 9 +EPDFSignatureEmbedderTest.PlacingADrawingAgainLeavesTheSignedRevisionAlone 17 EPDFSignatureEmbedderTest.ProbeContactInfoCannotBecomePlaceholder 3 EPDFSignatureEmbedderTest.ProbeExistingLockBecomesFieldMdp 4 EPDFSignatureEmbedderTest.ProbeLockParentCoversChildren 5 @@ -392,6 +393,35 @@ EPDFViewEmbedderTest.EPDFGetPageRotateByIndex 2 EPDFViewEmbedderTest.EPDFGetPageTabOrderByIndex 2 EPDFViewEmbedderTest.EPDFGetPageUserUnitByIndex 1 EPDFViewEmbedderTest.ImageOfAnotherDocumentDraws 4 +EPDFWidgetDrawingEmbedderTest.ADrawingWithAMatrixIsPlacedByItsBox 14 +EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 14 +EPDFWidgetDrawingEmbedderTest.AForeignIconOnlyButtonSurvivesARedraw 5 +EPDFWidgetDrawingEmbedderTest.AFormWithoutPicturesListsNoDrawing 16 +EPDFWidgetDrawingEmbedderTest.AMarkIsPlacedAsAPageIsFitted 196 +EPDFWidgetDrawingEmbedderTest.AMarkSavedBeforeThisChangeIsADrawing 13 +EPDFWidgetDrawingEmbedderTest.APushButtonWithoutAnAppearanceIsNotDrawn 3 +EPDFWidgetDrawingEmbedderTest.ARefitKeepsTheDrawingShared 16 +EPDFWidgetDrawingEmbedderTest.AReopenedFilePlacesItsDrawingAgain 14 +EPDFWidgetDrawingEmbedderTest.AReplacedOrClearedIconIsInNoSavedFile 15 +EPDFWidgetDrawingEmbedderTest.ASavedFileHoldsTheIconInBothPlaces 11 +EPDFWidgetDrawingEmbedderTest.AnAppearanceThatIsTheIconIsNotWrittenInto 11 +EPDFWidgetDrawingEmbedderTest.AnIconIsFittedAsTheIconFitSays 122 +EPDFWidgetDrawingEmbedderTest.AnIconIsFittedInsideTheBorder 42 +EPDFWidgetDrawingEmbedderTest.AnIconOnlyButtonCanBeDrawnInMemory 3 +EPDFWidgetDrawingEmbedderTest.AnIconOnlyButtonNeedsNoFont 3 +EPDFWidgetDrawingEmbedderTest.AnIconReadsUprightOnATurnedPage 11 +EPDFWidgetDrawingEmbedderTest.AnIconTurnsWithItsButton 52 +EPDFWidgetDrawingEmbedderTest.AnIncrementalSaveKeepsWhatTheFileHeld 11 +EPDFWidgetDrawingEmbedderTest.ChangingOnePlaceLeavesTheOthers 32 +EPDFWidgetDrawingEmbedderTest.EachWidgetOfAFieldShowsTheSameIcon 18 +EPDFWidgetDrawingEmbedderTest.ListingDrawingsWritesNothing 17 +EPDFWidgetDrawingEmbedderTest.ListingReadsNoAppearanceThatDrawsNoForm 21 +EPDFWidgetDrawingEmbedderTest.OnlyAStampAndAWidgetPlaceADrawing 3 +EPDFWidgetDrawingEmbedderTest.RefusesWithoutWriting 14 +EPDFWidgetDrawingEmbedderTest.SetsReadsAndClearsAnIcon 14 +EPDFWidgetDrawingEmbedderTest.StampsMarksAndIconsShareOneDrawing 108 +EPDFWidgetDrawingEmbedderTest.TheOtherIconsAreKeptAndNotDrawn 12 +EPDFWidgetDrawingEmbedderTest.WorksInsideALayerTransaction 5 FPDFAnnotEmbedderTest.AddAndModifyImage 8 FPDFAnnotEmbedderTest.AddAndModifyPath 10 FPDFAnnotEmbedderTest.AddAndModifyText 6 diff --git a/testing/resources/embedpdf_widget_marks_from_page.pdf b/testing/resources/embedpdf_widget_marks_from_page.pdf new file mode 100644 index 0000000000000000000000000000000000000000..e752f1bf0e98d9b509cf190d6295804242c4e271 GIT binary patch literal 2469 zcmd5;&uiR96gHuCV5y*ooa)P@#ISB?XGXtP3(LZKS4z^vv7%LLQ)8&SJ604n(jtvh zC-I00D3Z3Y<=#mG_^k0t@O<4H1& z;t=md@gUBk>VAB5zy)cMmFBrcXg#$kyiD!*qVA%iPFYd=FiJDNQ4f+dmqyWC8z9oo z*d&RkXW|6QGHejX2?ypm4yppT*fa_@ljA*@OOQ_FcORTg8R%75>9H(1N+&Gq^}qx# z;Q?oeoT^tZRsd9>E_iqo(OhofAJVZ~s9$2_USf|;_#RS;_)wi7i-4_#pgYktWIV@{ zMCyQ&N&`_9UFxB~K`h$l6p|L0^z}jO+HO|*i9{w8Zo{hCPNQ%L_JfW&HzBYJ0#gDr zoM^^bilJ*c8-aCGhOeZ_(HsODV?PbzY%VOA+y@RfSRC@($grT|$_Tu>eQ*afg8+fH z;|*LyOWF`rYA4obu3Qn&?`J$^!Cj?w{OGd(n>HpcS-JJogH`n9EVzAe7{%{BTl?aJ zhil*OKHRwU;-i;8ub!NIeZITeK6!rmopUSqt|@DBHJ541g)K!djd*)B=@zZ>leTM@%;~5<8$Br;$L|3?#iE!E`&>Mr$XTWhobeiP!w8M z6u0nASX03a4+n6dOoFqA4i!be=G^0y9V#u@6;${wFAJH5p&3X#DJ}||s<=>GjZq75 z8e_T+=V4=vSg?mT#?&SAG(c?5Gfdb|8nJ|c-Eyp6Ux`tT(N1@vW~%A1Yc}&y*#8^( zI^9KFZ3$nqvxIM@RaFZiuq!t(sHHDpP@_}MJJlKD7GjB6 Date: Sun, 22 Nov 2026 20:48:43 +0200 Subject: [PATCH 2/5] Draw a push button's caption and icon together A push button in a layout that shows a caption and an icon (/MK /TP 2 to 6) was drawn as its caption alone, so a button another app made lost its picture from the appearance when it was drawn again. /MK /I still named it. The generator draws both now. The button inside its border, or the whole button when /MK /IF /FB says so, is shared by the rule of PDFium's form filler (GetPushButtonAppStream), so a button looks the same whichever drew it: the caption takes a strip as high as its line below or above the icon, or as wide as its text right or left of it, and the icon the rest. A caption whose size is fitted (a font size of 0) takes a third. Over the icon both take all of it, and the caption is drawn last. The icon is fitted into its part as /MK /IF says, by the placement an icon alone gets, and the caption is drawn as the caption-only layout draws it, in the middle of its part. A caption that needs more than the button has is drawn alone, as the form filler draws it. A button with an icon and no caption draws the icon in the whole button and needs no /DA. A button with a caption and no /DA is left as it was, as every field that shows text is. An appearance that draws an icon is a new stream: the one the widget had may be another widget's, and its resources may be shared. No appearance is written into a stream that is one of the widget's icons, in any layout. The engine writes the caption-only and the icon-only layouts, so this touches only buttons other apps made. Upstream's files are unchanged; a test runs upstream's form filler on the same buttons and compares the two. --- core/fpdfdoc/cpdf_appearancegenerator.h | 3 +- .../cpdf_appearancegenerator_forms.cpp | 384 ++++-- docs/embedpdf/fork-ledger.md | 26 +- fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 1145 ++++++++++++++++- public/epdf_annot.h | 11 +- testing/embedpdf_layer_mode_writers.txt | 17 +- 6 files changed, 1452 insertions(+), 134 deletions(-) diff --git a/core/fpdfdoc/cpdf_appearancegenerator.h b/core/fpdfdoc/cpdf_appearancegenerator.h index 323a18022e..bb11c8a1ea 100644 --- a/core/fpdfdoc/cpdf_appearancegenerator.h +++ b/core/fpdfdoc/cpdf_appearancegenerator.h @@ -39,7 +39,8 @@ class CPDF_AppearanceGenerator { // kPushButton: the button's background and border, and its caption (/MK // /CA) centred in the /DA font and colour. One that shows its icon alone // (/MK /TP 1) draws the normal icon (/MK /I) inside the border, fitted as - // /MK /IF says, and no caption. + // /MK /IF says, and no caption. One that shows both (/MK /TP 2 to 6) + // draws both, each in its part of the button. enum FormType { kTextField, kComboBox, kListBox, kPushButton }; static void GenerateFormAP(CPDF_Document* doc, diff --git a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp index e445c38f75..69a4e14811 100644 --- a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp +++ b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp @@ -72,19 +72,29 @@ using appearance_generator::ResolveDaFontForPersistentTarget; namespace { // A field's text laid out in |plate| (CPDF_RichTextLayout::ArrangeField()) -// and written in the /DA font, which is |map|'s first entry: Td, Tf and a -// string per run. |content_height| receives the height the lines take. -ByteString FieldText(CPDF_AnnotFontMap& map, - const CPDF_RichTextLayout::Options& options, - CPDF_RichTextLayout::Field field, - const CFX_FloatRect& plate, - float* content_height = nullptr) { +// in the /DA font, which is |map|'s first entry. +CPDF_RichTextLayout::Result ArrangeFieldText( + CPDF_AnnotFontMap& map, + const CPDF_RichTextLayout::Options& options, + CPDF_RichTextLayout::Field field, + const CFX_FloatRect& plate) { const CPDF_AnnotFontMap::EntryFacts face = map.DescribeEntry(0); field.face_entry = 0; field.weight = face.weight; field.italic = face.italic; CPDF_RichTextLayout layout(&map, options); - const CPDF_RichTextLayout::Result laid = layout.ArrangeField(field, plate); + return layout.ArrangeField(field, plate); +} + +// A field's text laid out in |plate| (ArrangeFieldText()) and written: Td, Tf +// and a string per run. |content_height| receives the height the lines take. +ByteString FieldText(CPDF_AnnotFontMap& map, + const CPDF_RichTextLayout::Options& options, + CPDF_RichTextLayout::Field field, + const CFX_FloatRect& plate, + float* content_height = nullptr) { + const CPDF_RichTextLayout::Result laid = + ArrangeFieldText(map, options, std::move(field), plate); if (content_height) { *content_height = laid.content_height; } @@ -483,12 +493,64 @@ void BuildCheckboxBoxStream(fxcrt::ostringstream& stream, // How a push button lays out its caption and its icon: /MK /TP (ISO 32000-2 // Table 192). A number the table doesn't list is 0, the caption alone, as an // absent one is. -constexpr int kButtonShowsIconAlone = 1; - -int ButtonTextPosition(const CPDF_Dictionary* annot_dict) { +enum class ButtonLayout { + kCaptionAlone = 0, + kIconAlone = 1, + kCaptionBelowIcon = 2, + kCaptionAboveIcon = 3, + kCaptionRightOfIcon = 4, + kCaptionLeftOfIcon = 5, + kCaptionOverIcon = 6, +}; + +ButtonLayout ButtonLayoutOf(const CPDF_Dictionary* annot_dict) { RetainPtr mk = annot_dict->GetDictFor("MK"); const int position = mk ? mk->GetIntegerFor("TP") : 0; - return position >= 0 && position <= 6 ? position : 0; + return position >= 0 && position <= 6 ? static_cast(position) + : ButtonLayout::kCaptionAlone; +} + +// The normal icon of a push button (/MK /I) when it can be drawn. An icon is +// drawn by reference, so only one that is an object of its own can be: a +// file always holds a stream so. +RetainPtr ButtonIconOf(const CPDF_Dictionary* mk_dict) { + RetainPtr icon = + mk_dict ? mk_dict->GetStreamFor(pdfium::appearance::kI) : nullptr; + return icon && icon->GetObjNum() != 0 ? icon : nullptr; +} + +// True when `stream` is one of the icons the widget's /MK names. An +// appearance is never written into one: it would draw over the picture. +bool IsAnIconOf(const CPDF_Dictionary* annot_dict, const CPDF_Stream* stream) { + RetainPtr mk = annot_dict->GetDictFor("MK"); + if (!mk || stream->GetObjNum() == 0) { + return false; + } + for (const char* key : {pdfium::appearance::kI, pdfium::appearance::kRI, + pdfium::appearance::kIX}) { + RetainPtr icon = mk->GetStreamFor(key); + if (icon && icon->GetObjNum() == stream->GetObjNum()) { + return true; + } + } + return false; +} + +// True for a push button that draws no caption beside its icon: one that +// shows its icon alone (/MK /TP 1), and one that shows both (2 to 6) when it +// has an icon and no caption. GenerateIconButtonAP() draws these. +bool ButtonDrawsNoCaption(const CPDF_Dictionary* annot_dict) { + switch (ButtonLayoutOf(annot_dict)) { + case ButtonLayout::kCaptionAlone: + return false; + case ButtonLayout::kIconAlone: + return true; + default: { + RetainPtr mk = annot_dict->GetDictFor("MK"); + return ButtonIconOf(mk.Get()) && + mk->GetUnicodeTextFor(pdfium::appearance::kCA).IsEmpty(); + } + } } // The box an icon takes where it is drawn: its /BBox through its /Matrix. @@ -558,19 +620,174 @@ std::optional IconPlacement(const CFX_FloatRect& icon, bottom - icon.bottom * scale_y); } -// A push button in every layout but the icon alone, which -// GenerateIconButtonAP() draws: background and border as a text field draws -// them, and the caption (/MK /CA) on one line, centred both ways, in the /DA -// font and colour. In the layouts that show a caption and an icon (/MK /TP 2 -// to 6) the icon is not drawn, and the caption takes the whole button. -void GeneratePushButtonFormAP(fxcrt::ostringstream& app_stream, - const CPDF_Dictionary* annot_dict, - const CFX_FloatRect& bbox, - const DefaultAppearanceInfo& da_info, - CPDF_AnnotFontMap& map, - const CPDF_RichTextLayout::Options& options) { +// The part of a push button its icon is fitted into, alone or with the +// caption beside it: the button less the border's width at each edge, or the +// whole button when /MK /IF /FB says the icon reaches the edges. +CFX_FloatRect ButtonPlate(const CFX_FloatRect& bbox, + float border_width, + const CPDF_IconFit& fit) { + CFX_FloatRect plate = bbox; + if (!fit.GetFittingBounds()) { + plate.Deflate(border_width, border_width); + } + return plate; +} + +// Draws `icon` in `box`, fitted as `fit` says (IconPlacement()) and clipped +// to the box, by the name /FRM. False when nothing is drawn: an icon or a +// box without area. +bool WriteButtonIcon(fxcrt::ostringstream& content, + const CPDF_Stream* icon, + const CFX_FloatRect& box, + const CPDF_IconFit& fit) { + const std::optional placement = + IconPlacement(IconBox(icon), box, fit); + if (!placement.has_value()) { + return false; + } + content << "q\n"; + WriteRect(content, box) << " re W n\n"; + WriteMatrix(content, placement.value()) << " cm\n/FRM Do\nQ\n"; + return true; +} + +// Names the icon an appearance draws /FRM in its resources, as Acrobat +// writes an image field, so the file holds the picture once, for /MK and +// for /AP. +void NameButtonIcon(CPDF_Dictionary* resources, + CPDF_Document* doc, + uint32_t icon_number) { + resources->SetNewFor("XObject")->SetNewFor( + "FRM", doc, icon_number); +} + +// Where a push button that shows both draws its caption and its icon. +struct ButtonParts { + CFX_FloatRect caption; + CFX_FloatRect icon; // without area when the caption takes the whole plate +}; + +// Shares `plate` between a caption and an icon by the rule of PDFium's form +// filler (GetPushButtonAppStream() in fpdfsdk/cpdfsdk_appstream.cpp), so a +// button looks the same whichever drew it. Below or above the icon, the +// caption takes a strip as high as its line; right or left of it, a strip +// as wide as its text; the icon takes the rest. A caption that needs more +// than the plate has takes all of it, and no icon is drawn. A caption whose +// size is fitted (a font size of 0) takes a third of the plate, and is +// fitted to that. Over the icon, both take the whole plate. `caption` is the +// room the caption takes at its font size, and is not read when `fitted`. +ButtonParts SplitButton(ButtonLayout layout, + const CFX_FloatRect& plate, + const CFX_SizeF& caption, + bool fitted) { + ButtonParts parts = {plate, plate}; + if (layout == ButtonLayout::kCaptionOverIcon) { + return parts; + } + const bool stacked = layout == ButtonLayout::kCaptionBelowIcon || + layout == ButtonLayout::kCaptionAboveIcon; + const float room = stacked ? plate.Height() : plate.Width(); + const float strip = + fitted ? room / 3.0f : (stacked ? caption.height : caption.width); + if (strip > room) { + parts.icon = CFX_FloatRect(); + return parts; + } + switch (layout) { + case ButtonLayout::kCaptionBelowIcon: + parts.caption.top = plate.bottom + strip; + parts.icon.bottom = parts.caption.top; + break; + case ButtonLayout::kCaptionAboveIcon: + parts.caption.bottom = plate.top - strip; + parts.icon.top = parts.caption.bottom; + break; + case ButtonLayout::kCaptionRightOfIcon: + parts.caption.left = plate.right - strip; + parts.icon.right = parts.caption.left; + break; + case ButtonLayout::kCaptionLeftOfIcon: + parts.caption.right = plate.left + strip; + parts.icon.left = parts.caption.right; + break; + default: + break; + } + return parts; +} + +// A push button's caption as the layout takes it: one line, centred both +// ways. +CPDF_RichTextLayout::Field ButtonCaptionField(const WideString& caption, + float font_size) { + CPDF_RichTextLayout::Field field; + field.text = caption; + field.size = font_size; + field.alignment = 1; + field.centered = true; + return field; +} + +// The room a caption takes at `font_size`: its line's width and height. +CFX_SizeF ButtonCaptionSize(CPDF_AnnotFontMap& map, + const CPDF_RichTextLayout::Options& options, + const WideString& caption, + float font_size, + const CFX_FloatRect& plate) { + const CPDF_RichTextLayout::Result laid = ArrangeFieldText( + map, options, ButtonCaptionField(caption, font_size), plate); + float width = 0; + for (const CPDF_RichTextLayout::Line& line : laid.lines) { + width = std::max(width, line.width); + } + return CFX_SizeF(width, laid.content_height); +} + +// Writes a push button's caption laid out in `body`, in the /DA font and +// colour, clipped to `clip`. +void WriteButtonCaption(fxcrt::ostringstream& app_stream, + const WideString& caption, + const CFX_FloatRect& body, + const CFX_FloatRect& clip, + const DefaultAppearanceInfo& da_info, + CPDF_AnnotFontMap& map, + const CPDF_RichTextLayout::Options& options) { + const ByteString text = FieldText( + map, options, ButtonCaptionField(caption, da_info.font_size), body); + if (text.IsEmpty()) { + return; + } + app_stream << "q\n"; + WriteRect(app_stream, clip) << " re W n\n"; + app_stream << "BT\n" + << GenerateColorAP(da_info.text_color, PaintOperation::kFill) + << text << "ET\nQ\n"; +} + +// A push button that draws a caption (GenerateIconButtonAP() draws the ones +// that draw none): background and border as a text field draws them, and the +// caption (/MK /CA) on one line, centred both ways, in the /DA font and +// colour. +// +// The caption alone (/MK /TP 0, or any layout without an icon) takes the +// whole button inside its border. In the layouts that show a caption and an +// icon (/MK /TP 2 to 6), a button that has an icon (/MK /I) draws both, each +// in the part SplitButton() gives it: the icon first, fitted into its part +// as /MK /IF says and clipped to it, then the caption in the middle of its +// part, so over the icon the caption is on top. When the caption leaves the +// icon no room, the caption is drawn alone. +// +// Returns the object number of the icon it drew, which the appearance's +// resources must name /FRM (NameButtonIcon()), or 0. +uint32_t GeneratePushButtonFormAP(fxcrt::ostringstream& app_stream, + const CPDF_Dictionary* annot_dict, + const CFX_FloatRect& bbox, + const DefaultAppearanceInfo& da_info, + CPDF_AnnotFontMap& map, + const CPDF_RichTextLayout::Options& options) { + RetainPtr mk_dict = annot_dict->GetDictFor("MK"); const AppearanceCharacteristics mk = - GetAppearanceCharacteristics(annot_dict->GetDictFor("MK")); + GetAppearanceCharacteristics(mk_dict.Get()); const BorderStyleInfo bs = GetBorderStyleInfo(annot_dict->GetDictFor("BS")); const float bw = bs.width; @@ -578,29 +795,37 @@ void GeneratePushButtonFormAP(fxcrt::ostringstream& app_stream, BuildCheckboxBoxStream(app_stream, mk, bs, bbox); app_stream << "Q\n"; if (mk.caption.IsEmpty()) { - return; + return 0; } - CFX_FloatRect body_rect = bbox; - body_rect.Deflate(2.0f * bw, bw); - CPDF_RichTextLayout::Field field; - field.text = mk.caption; - field.size = da_info.font_size; - field.alignment = 1; - field.centered = true; - const ByteString caption = - FieldText(map, options, std::move(field), body_rect); - if (caption.IsEmpty()) { - return; + const ButtonLayout layout = ButtonLayoutOf(annot_dict); + RetainPtr icon = layout == ButtonLayout::kCaptionAlone + ? nullptr + : ButtonIconOf(mk_dict.Get()); + if (icon) { + const CPDF_IconFit fit(mk_dict->GetDictFor("IF")); + const CFX_FloatRect plate = ButtonPlate(bbox, bw, fit); + const bool fitted = FXSYS_IsFloatZero(da_info.font_size); + const ButtonParts parts = + SplitButton(layout, plate, + fitted ? CFX_SizeF() + : ButtonCaptionSize(map, options, mk.caption, + da_info.font_size, plate), + fitted); + if (WriteButtonIcon(app_stream, icon.Get(), parts.icon, fit)) { + WriteButtonCaption(app_stream, mk.caption, parts.caption, parts.caption, + da_info, map, options); + return icon->GetObjNum(); + } } - app_stream << "q\n"; + CFX_FloatRect body_rect = bbox; + body_rect.Deflate(2.0f * bw, bw); CFX_FloatRect clip_rect = bbox; clip_rect.Deflate(bw, bw); - WriteRect(app_stream, clip_rect) << " re W n\n"; - app_stream << "BT\n" - << GenerateColorAP(da_info.text_color, PaintOperation::kFill) - << caption << "ET\nQ\n"; + WriteButtonCaption(app_stream, mk.caption, body_rect, clip_rect, da_info, map, + options); + return 0; } // The symbol a check box or radio button shows when on, named by the @@ -782,21 +1007,18 @@ uint32_t CreateFormXObjectStream( return stream->GetObjNum(); } -// A push button that shows its icon alone (/MK /TP 1): background and border -// as every push button has them, and the normal icon (/MK /I), a form -// XObject of its own, drawn inside the border. The icon is fitted into the -// button less the border's width at each edge, or into the whole button when -// /MK /IF /FB says so (IconPlacement()), and clipped to that plate. The -// appearance names it /FRM in its resources, as Acrobat writes an image -// field, so the file holds the picture once, for /MK and for /AP. The button -// is laid out in the frame its quarter turn gives (/MK /R), as every field's -// contents are. +// A push button that draws no caption (ButtonDrawsNoCaption()): background +// and border as every push button has them, and the normal icon (/MK /I), a +// form XObject of its own, drawn inside the border. The icon is fitted into +// the button's plate (ButtonPlate()) as /MK /IF says, and clipped to it. The +// button is laid out in the frame its quarter turn gives (/MK /R), as every +// field's contents are. // -// It draws no caption, so it needs no /DA and no font, and without an icon -// it is the box alone. Written into the file, the appearance is a new -// stream, as a check box's are: the old /N may be another widget's too, or -// the icon itself. The old /N is not deleted, nor is an icon it drew: a save -// writes only what the document still reaches. +// It needs no /DA and no font, and without an icon it is the box alone. +// Written into the file, the appearance is a new stream, as a check box's +// are: the old /N may be another widget's too, or the icon itself. The old +// /N is not deleted, nor is an icon it drew: a save writes only what the +// document still reaches. void GenerateIconButtonAP(APGenerationTarget* target, const CPDF_Dictionary* annot_dict) { CPDF_Document* const doc = target->doc; @@ -812,25 +1034,13 @@ void GenerateIconButtonAP(APGenerationTarget* target, BuildCheckboxBoxStream(content, mk, bs, dims.bbox); content << "Q\n"; - // An icon is drawn by reference, so only one that is an object of its own - // can be: a file always holds a stream so. RetainPtr resources; - RetainPtr icon = - mk_dict ? mk_dict->GetStreamFor(pdfium::appearance::kI) : nullptr; - if (icon && icon->GetObjNum() != 0) { + if (RetainPtr icon = ButtonIconOf(mk_dict.Get())) { const CPDF_IconFit fit(mk_dict->GetDictFor("IF")); - CFX_FloatRect plate = dims.bbox; - if (!fit.GetFittingBounds()) { - plate.Deflate(bs.width, bs.width); - } - if (const std::optional placement = - IconPlacement(IconBox(icon.Get()), plate, fit)) { - content << "q\n"; - WriteRect(content, plate) << " re W n\n"; - WriteMatrix(content, placement.value()) << " cm\n/FRM Do\nQ\n"; + if (WriteButtonIcon(content, icon.Get(), + ButtonPlate(dims.bbox, bs.width, fit), fit)) { resources = doc->New(); - resources->SetNewFor("XObject") - ->SetNewFor("FRM", doc, icon->GetObjNum()); + NameButtonIcon(resources.Get(), doc, icon->GetObjNum()); } } @@ -857,10 +1067,10 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, return false; } - // A push button that shows its icon alone draws no text: it needs no /DA - // and no font. + // A push button that draws no caption draws no text: it needs no /DA and + // no font. if (type == CPDF_AppearanceGenerator::kPushButton && - ButtonTextPosition(annot_dict) == kButtonShowsIconAlone) { + ButtonDrawsNoCaption(annot_dict)) { GenerateIconButtonAP(target, annot_dict); return true; } @@ -951,6 +1161,8 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, const AnnotationDimensionsAndColor dims = GetAnnotationDimensionsAndColor(annot_dict); fxcrt::ostringstream app_stream; + // The icon a push button drew beside its caption, by object number. + uint32_t drawn_icon = 0; switch (type) { case CPDF_AppearanceGenerator::kTextField: GenerateTextFieldFormAP(app_stream, annot_dict, dims.bbox, @@ -967,8 +1179,9 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, default_appearance_info.value(), map, options); break; case CPDF_AppearanceGenerator::kPushButton: - GeneratePushButtonFormAP(app_stream, annot_dict, dims.bbox, - default_appearance_info.value(), map, options); + drawn_icon = GeneratePushButtonFormAP(app_stream, annot_dict, dims.bbox, + default_appearance_info.value(), + map, options); break; } @@ -979,6 +1192,9 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, } auto resources_dict = GenerateResourcesDict( doc, GenerateExtGStateDict(*annot_dict, "Normal"), std::move(fonts)); + if (drawn_icon != 0) { + NameButtonIcon(resources_dict.Get(), doc, drawn_icon); + } target->ephemeral_fonts = std::move(owned_map); return GenerateAPDict( target, annot_dict, &app_stream, std::move(resources_dict), @@ -992,7 +1208,13 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, RetainPtr ap_dict = annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP); RetainPtr normal_stream = ap_dict->GetMutableStreamFor("N"); - if (!normal_stream) { + // An appearance that draws an icon is a new stream, as an icon-only + // button's is: the old /N may be another widget's too, and its resources + // may be shared, where /FRM would name this button's icon for all of them. + // And no appearance is written into a stream that is one of the widget's + // icons. + if (!normal_stream || drawn_icon != 0 || + IsAnIconOf(annot_dict, normal_stream.Get())) { normal_stream = doc->NewIndirect(pdfium::MakeRetain()); ap_dict->SetNewFor("N", doc, normal_stream->GetObjNum()); @@ -1005,8 +1227,12 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, stream_dict->SetNewFor("Subtype", "Form"); stream_dict->SetMatrixFor("Matrix", dims.matrix); stream_dict->SetRectFor("BBox", dims.bbox); - stream_dict->GetOrCreateDictFor("Resources") - ->SetFor("Font", std::move(resource_font_dict)); + RetainPtr resources = + stream_dict->GetOrCreateDictFor("Resources"); + resources->SetFor("Font", std::move(resource_font_dict)); + if (drawn_icon != 0) { + NameButtonIcon(resources.Get(), doc, drawn_icon); + } return true; } diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 545f0a03f1..cae25462db 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -340,21 +340,29 @@ measurement, redaction overlays, form fields. Mixed into upstream's file, it was file. Apart, upstream's file merges cleanly. **Where.** `cpdf_appearancegenerator*` and its unit test; the calls in `cpdf_annot.cpp`, upstream's `FPDFAnnot_AppendObject` and `FPDFAnnot_SetFontColor` in `fpdf_annot.cpp`, -`epdf_annot.cpp`, `epdf_form.cpp`, `epdf_redact.cpp` and the rich text writer. A push button that -shows its icon alone (`/MK /TP 1`, an image field) is drawn from `/MK /I` by -`GenerateIconButtonAP`. It reads the icon fit (`/MK /IF`) through upstream's `CPDF_IconFit` and -places the icon itself (`IconPlacement`), by ISO 32000-2 Table 250: upstream's own placement -(`CPDF_IconFit::GetScale` and `GetImageOffset`, used by its form filler) puts an icon whose fit -names no `/A` at the bottom left, where the table's default is the middle, and reads an icon -narrower than one unit as one unit wide. +`epdf_annot.cpp`, `epdf_form.cpp`, `epdf_redact.cpp` and the rich text writer. A push button's +icon (`/MK /I`) is drawn in every layout that shows one. Alone (`/MK /TP 1`, an image field, and +any layout 2 to 6 without a caption) `GenerateIconButtonAP` draws it. With the caption (`/MK /TP` +2 to 6) `GeneratePushButtonFormAP` draws both, and shares the button between them by the rule of +upstream's form filler (`SplitButton`, after `GetPushButtonAppStream` in +`fpdfsdk/cpdfsdk_appstream.cpp`, which stays unchanged), so a button looks the same whichever drew +it. The engine writes layouts 0 and 1 only, a caption or an image field, so layouts 2 to 6 are +met only in buttons other apps made. Either way the icon fit (`/MK /IF`) is read through +upstream's `CPDF_IconFit` and the icon is placed by our own `IconPlacement`, by ISO 32000-2 Table +250: upstream's own placement (`CPDF_IconFit::GetScale` and `GetImageOffset`, used by its form +filler) puts an icon whose fit names no `/A` at the bottom left, where the table's default is the +middle, and reads an icon narrower than one unit as one unit wide. **Guarded by.** `check-fork-tags.py` fails when upstream's files differ from upstream (`fork-pristine.txt`); `upstream-preview.py` lists new upstream callers of `CPDF_GenerateAP::`, and -upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for a button's icon. +upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for a button's icon; +its `AButtonIsSharedAsTheFormFillerSharesIt` runs upstream's form filler on the same buttons and +compares the two. **When upstream touches this.** A commit to `cpdf_generateap.*`: port it to ours or skip it, with the reason in the sync report. A new upstream caller of `CPDF_GenerateAP::`: route it to ours, or accept it in the report. A change to what `CPDF_IconFit`'s readers return (`GetScaleMethod`, `IsProportionalScale`, `GetIconBottomLeftPosition`, `GetFittingBounds`): `IconPlacement` reads the -file through them. +file through them. A change to how `GetPushButtonAppStream` shares a button: `SplitButton` +follows it, and the comparing test fails until it does. ## Text diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp index 97d74fc472..2b93254de0 100644 --- a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -3,10 +3,11 @@ // Drawings in widgets (public/epdf_annot.h). A drawing is a picture the // document holds once. A widget shows one in two ways: as a push button's -// icon, which /MK names and the generator draws for a button that shows its -// icon alone (/MK /TP 1, what Acrobat writes for an image field), and as a -// mark, which a wrapper places as it places a stamp's artwork (a signature -// field's). Stamps, marks and icons share one drawing. +// icon, which /MK names and the generator draws in every layout that shows +// an icon, alone (/MK /TP 1, what Acrobat writes for an image field) or with +// the button's caption (/MK /TP 2 to 6), and as a mark, which a wrapper +// places as it places a stamp's artwork (a signature field's). Stamps, marks +// and icons share one drawing. #include @@ -16,11 +17,14 @@ #include #include #include +#include #include #include #include +#include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/page/cpdf_annotcontext.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_boolean.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" @@ -35,7 +39,10 @@ #include "core/fxcrt/fx_coordinates.h" #include "core/fxcrt/fx_stream.h" #include "core/fxcrt/retain_ptr.h" +#include "fpdfsdk/cpdfsdk_formfillenvironment.h" #include "fpdfsdk/cpdfsdk_helpers.h" +#include "fpdfsdk/cpdfsdk_pageview.h" +#include "fpdfsdk/cpdfsdk_widget.h" #include "fpdfsdk/epdf_edit_test_util.h" #include "public/cpp/fpdf_scopers.h" #include "public/epdf_annot.h" @@ -44,6 +51,7 @@ #include "public/epdf_view.h" #include "public/fpdf_annot.h" #include "public/fpdf_edit.h" +#include "public/fpdf_formfill.h" #include "public/fpdf_save.h" #include "public/fpdfview.h" #include "testing/embedder_test.h" @@ -132,27 +140,62 @@ unsigned int AddDrawing(FPDF_DOCUMENT document, } // A page rendered once with its form fields' own appearances, for the colour -// at any point of it as 0xRRGGBB. (ColorAt() draws no widget.) +// at any point of it as 0xRRGGBB. (ColorAt() draws no widget.) Points are +// the page's as it is shown, from its lower left corner. At a `scale` above +// 1 the page is drawn that many times larger, so text has pixels of its own +// colour. class Shown { public: - explicit Shown(FPDF_PAGE page) - : bitmap_(EmbedderTest::RenderPageWithFlags( - page, - nullptr, - FPDF_ANNOT | EPDF_RENDER_WIDGETS)) {} + explicit Shown(FPDF_PAGE page, int scale = 1) : scale_(scale) { + if (scale == 1) { + bitmap_ = EmbedderTest::RenderPageWithFlags( + page, nullptr, FPDF_ANNOT | EPDF_RENDER_WIDGETS); + return; + } + const int width = static_cast(FPDF_GetPageWidthF(page)) * scale; + const int height = static_cast(FPDF_GetPageHeightF(page)) * scale; + bitmap_.reset(FPDFBitmap_Create(width, height, 0)); + EXPECT_TRUE( + FPDFBitmap_FillRect(bitmap_.get(), 0, 0, width, height, 0xFFFFFFFF)); + FPDF_RenderPageBitmap(bitmap_.get(), page, 0, 0, width, height, 0, + FPDF_ANNOT | EPDF_RENDER_WIDGETS); + } + + uint32_t At(float x, float y_from_bottom) const { + return Pixel(static_cast(x * scale_), + static_cast(y_from_bottom * scale_)); + } + + // True when text is drawn inside `box`: one of its pixels is black, which + // nothing but a caption is in these tests. + bool HasInkIn(const CFX_FloatRect& box) const { + const int right = static_cast(box.right * scale_); + const int top = static_cast(box.top * scale_); + for (int y = static_cast(box.bottom * scale_) + 1; y < top; ++y) { + for (int x = static_cast(box.left * scale_) + 1; x < right; ++x) { + const uint32_t colour = Pixel(x, y); + if (((colour >> 16) & 0xFF) < 0x40 && ((colour >> 8) & 0xFF) < 0x40 && + (colour & 0xFF) < 0x40) { + return true; + } + } + } + return false; + } - uint32_t At(int x, int y_from_bottom) const { + private: + // The pixel `x` from the left and `y` up from the bottom row. + uint32_t Pixel(int x, int y) const { const int height = FPDFBitmap_GetHeight(bitmap_.get()); const int stride = FPDFBitmap_GetStride(bitmap_.get()); const auto* pixels = static_cast(FPDFBitmap_GetBuffer(bitmap_.get())); - const uint8_t* pixel = - pixels + (height - 1 - y_from_bottom) * stride + x * 4; + const uint8_t* pixel = pixels + (height - 1 - y) * stride + x * 4; return (pixel[2] << 16) | (pixel[1] << 8) | pixel[0]; } - private: ScopedFPDFBitmap bitmap_; + const int scale_; }; uint32_t ShownColorAt(FPDF_PAGE page, int x, int y_from_bottom) { @@ -229,12 +272,33 @@ std::optional DrawnIconOf(FPDF_ANNOTATION annot) { return icon; } +// The part an appearance clips its caption to, in the appearance's own +// space, or nothing when it draws no caption. +std::optional DrawnCaptionOf(FPDF_ANNOTATION annot) { + RetainPtr appearance = AppearanceOf(annot); + if (!appearance) { + return std::nullopt; + } + const std::string content = StreamBytes(appearance.Get()); + const size_t text = content.find("BT\n"); + const size_t start = + text == std::string::npos ? text : content.rfind("q\n", text); + float part[4]; + if (start == std::string::npos || + sscanf(content.c_str() + start, "q\n%f %f %f %f re W n\nBT\n", &part[0], + &part[1], &part[2], &part[3]) != 4) { + return std::nullopt; + } + return CFX_FloatRect(part[0], part[1], part[0] + part[2], part[1] + part[3]); +} + void ExpectRectNear(const CFX_FloatRect& expected, - const CFX_FloatRect& actual) { - EXPECT_NEAR(expected.left, actual.left, 1e-3f); - EXPECT_NEAR(expected.bottom, actual.bottom, 1e-3f); - EXPECT_NEAR(expected.right, actual.right, 1e-3f); - EXPECT_NEAR(expected.top, actual.top, 1e-3f); + const CFX_FloatRect& actual, + float within = 1e-3f) { + EXPECT_NEAR(expected.left, actual.left, within); + EXPECT_NEAR(expected.bottom, actual.bottom, within); + EXPECT_NEAR(expected.right, actual.right, within); + EXPECT_NEAR(expected.top, actual.top, within); } // How many streams of the file `bytes` hold pass `counts`: every object the @@ -305,9 +369,11 @@ void KeepSample(const std::string& bytes, const char* name) { // with an icon fit dictionary that spells out the defaults, and an // appearance that draws the icon inside a border of 1. Each stands 102 by // 42, 50 apart, from y 10 up. With `with_fonts` false, nothing names a /DA -// or a font. With `with_appearances` false, no widget has an /AP. +// or a font. With `with_appearances` false, no widget has an /AP. The +// caption's font size is `font_size`: 0 for one fitted to its room. std::string ForeignButtonsPdf(bool with_fonts = true, - bool with_appearances = true) { + bool with_appearances = true, + int font_size = 12) { std::vector objects = { std::string("<< /Type /Catalog /Pages 2 0 R /AcroForm << /Fields " "[5 0 R]") + @@ -316,8 +382,8 @@ std::string ForeignButtonsPdf(bool with_fonts = true, : "") + " >> >>", "<< /Type /Pages /Kids [3 0 R] /Count 1 >>", - "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 400] /Annots " - "[7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R] >>", + "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 400 400] /Resources << >> " + "/Annots [7 0 R 8 0 R 9 0 R 10 0 R 11 0 R 12 0 R 13 0 R] >>", "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica " "/Encoding /WinAnsiEncoding >>", "<< /FT /Btn /Ff 65536 /T (picture) " @@ -334,7 +400,8 @@ std::string ForeignButtonsPdf(bool with_fonts = true, std::to_string(bottom) + " 122 " + std::to_string(bottom + 42) + "] /MK << /I 6 0 R /TP " + std::to_string(layout) + " /CA (Go) /IF << /SW /A /S /P /A [0.5 0.5] >> >>" + - (with_fonts ? " /DA (/Helv 12 Tf 0 g)" : "") + + (with_fonts ? " /DA (/Helv " + std::to_string(font_size) + " Tf 0 g)" + : "") + (with_appearances ? " /AP << /N " + std::to_string(14 + layout) + " 0 R >>" : "") + @@ -442,6 +509,192 @@ class CountedData final : public IFX_SeekableReadStream { int fetches_ = 0; }; +// The layouts that show a caption and an icon (/MK /TP, ISO 32000-2 Table +// 192). +constexpr int kCaptionBelow = 2; +constexpr int kCaptionAbove = 3; +constexpr int kCaptionRight = 4; +constexpr int kCaptionLeft = 5; +constexpr int kCaptionOver = 6; +const struct { + int layout; + const char* name; +} kCaptionAndIconLayouts[] = {{kCaptionBelow, "the caption below the icon"}, + {kCaptionAbove, "the caption above the icon"}, + {kCaptionRight, "the caption right of the icon"}, + {kCaptionLeft, "the caption left of the icon"}, + {kCaptionOver, "the caption over the icon"}}; + +// The room the caption "Go" takes at `size` in the font the button's +// appearance writes it in: its width, and the height of its line, which is +// the font's ascent and descent, as PDFium's form filler measures a line. +// The tests' stand-in for Helvetica has its own. +CFX_SizeF CaptionRoomOf(FPDF_ANNOTATION annot, float size = 12) { + RetainPtr appearance = AppearanceOf(annot); + RetainPtr resources = + appearance ? appearance->GetDict()->GetDictFor("Resources") : nullptr; + RetainPtr fonts = + resources ? resources->GetDictFor("Font") : nullptr; + if (!fonts || fonts->size() == 0) { + ADD_FAILURE() << "the appearance names no font"; + return CFX_SizeF(); + } + RetainPtr dict; + { + CPDF_DictionaryLocker locker(fonts); + dict = ToDictionary(locker.begin()->second->GetDirect()); + } + CPDF_Document* doc = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetPage()->GetDocument(); + RetainPtr font = CPDF_DocPageData::FromDocument(doc)->GetFont( + pdfium::WrapRetain(const_cast(dict.Get()))); + if (!font) { + ADD_FAILURE() << "the appearance's font doesn't load"; + return CFX_SizeF(); + } + return CFX_SizeF( + font->GetStringWidth("Go") * size / 1000, + (font->GetTypeAscent() - font->GetTypeDescent()) * size / 1000); +} + +// The size an appearance writes its caption at: the number before Tf. +float DrawnCaptionSizeOf(FPDF_ANNOTATION annot) { + const std::string content = StreamBytes(AppearanceOf(annot).Get()); + const size_t set = content.find(" Tf"); + const size_t start = + set == std::string::npos ? set : content.rfind(' ', set - 1); + return start == std::string::npos ? 0 + : strtof(content.c_str() + start, nullptr); +} + +// A point of the picture's red half and one of its blue half, when it lands +// in `box`: in the middle of each half, near the bottom, where no caption +// over the picture reaches. +CFX_PointF RedPointOf(const CFX_FloatRect& box) { + return CFX_PointF(box.left + box.Width() / 4, box.bottom + box.Height() / 8); +} +CFX_PointF BluePointOf(const CFX_FloatRect& box) { + return CFX_PointF(box.right - box.Width() / 4, box.bottom + box.Height() / 8); +} + +// Where "Go" at 12 is when it is in the middle of `part`: 16 by 9 around the +// middle, with room to spare. +CFX_FloatRect CaptionInkOf(const CFX_FloatRect& part) { + const float x = (part.left + part.right) / 2; + const float y = (part.bottom + part.top) / 2; + return CFX_FloatRect(x - 9, y - 6, x + 9, y + 6); +} + +// Checks that `caption` and `icon` share `plate` as `layout` says: the +// caption in a strip as high as its line, at the bottom or the top, or as +// wide as its text, at the right or the left, and the icon in the rest; or +// both in all of it. `room` is what the caption takes. +void ExpectPartsShare(int layout, + const CFX_FloatRect& plate, + const CFX_SizeF& room, + const CFX_FloatRect& caption, + const CFX_FloatRect& icon) { + constexpr float kWithin = 1e-2f; + if (layout == kCaptionOver) { + ExpectRectNear(plate, caption, kWithin); + ExpectRectNear(plate, icon, kWithin); + return; + } + CFX_FloatRect both = caption; + both.Union(icon); + ExpectRectNear(plate, both, kWithin); + switch (layout) { + case kCaptionBelow: + EXPECT_NEAR(plate.bottom, caption.bottom, kWithin); + EXPECT_NEAR(room.height, caption.Height(), kWithin); + EXPECT_NEAR(caption.top, icon.bottom, kWithin); + break; + case kCaptionAbove: + EXPECT_NEAR(plate.top, caption.top, kWithin); + EXPECT_NEAR(room.height, caption.Height(), kWithin); + EXPECT_NEAR(caption.bottom, icon.top, kWithin); + break; + case kCaptionRight: + EXPECT_NEAR(plate.right, caption.right, kWithin); + EXPECT_NEAR(room.width, caption.Width(), kWithin); + EXPECT_NEAR(caption.left, icon.right, kWithin); + break; + case kCaptionLeft: + EXPECT_NEAR(plate.left, caption.left, kWithin); + EXPECT_NEAR(room.width, caption.Width(), kWithin); + EXPECT_NEAR(caption.right, icon.left, kWithin); + break; + default: + ADD_FAILURE() << "not a layout that shows both"; + } +} + +// What a button's appearance draws, read from its content whoever wrote it: +// the part its icon is clipped to and where the picture's box lands, and the +// size its caption is written at and the point its text starts. Nothing for +// what it doesn't draw. +struct ButtonContent { + std::optional icon_part; + std::optional picture; + std::optional caption_size; + std::optional caption_start; +}; + +ButtonContent ReadButtonContent(const std::string& content) { + struct State { + CFX_Matrix placed; + std::optional clip; + }; + ButtonContent read; + std::vector saved; + State state; + std::vector numbers; + std::istringstream tokens(content); + std::string token; + auto last = [&numbers](size_t count) { + return numbers.size() < count ? nullptr + : numbers.data() + numbers.size() - count; + }; + while (tokens >> token) { + char* end = nullptr; + const float number = strtof(token.c_str(), &end); + if (end != token.c_str() && *end == '\0') { + numbers.push_back(number); + continue; + } + if (token == "q") { + saved.push_back(state); + } else if (token == "Q" && !saved.empty()) { + state = saved.back(); + saved.pop_back(); + } else if (token == "re") { + if (const float* box = last(4)) { + state.clip = + CFX_FloatRect(box[0], box[1], box[0] + box[2], box[1] + box[3]); + } + } else if (token == "cm") { + if (const float* m = last(6)) { + CFX_Matrix placed(m[0], m[1], m[2], m[3], m[4], m[5]); + placed.Concat(state.placed); + state.placed = placed; + } + } else if (token == "Do") { + read.icon_part = state.clip; + read.picture = state.placed.TransformRect(kPictureBox); + } else if (token == "Tf" && !read.caption_size) { + if (const float* size = last(1)) { + read.caption_size = *size; + } + } else if (token == "Td" && !read.caption_start) { + if (const float* start = last(2)) { + read.caption_start = CFX_PointF(start[0], start[1]); + } + } + numbers.clear(); + } + return read; +} + } // namespace class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { @@ -524,6 +777,22 @@ class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget)); } + // What shows a drawing beside a caption in one box: the caption "Go" in + // Helvetica at `font_size` (0 for a size fitted to the box), the drawing as + // the normal icon, the layout, and a new appearance. + void SetCaptionAndIcon(FPDF_ANNOTATION widget, + unsigned int drawing, + int layout, + float font_size = 12) { + ASSERT_TRUE(EPDFAnnot_SetMKText(widget, EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance(widget, FPDF_FONT_HELVETICA, + font_size, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(widget, EPDF_MK_ICON_I, drawing)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(widget, layout)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget)); + } + // A save of `doc`: a full one, or with FPDF_INCREMENTAL one appended to // the bytes the document was opened from. std::string SavedBytes(FPDF_DOCUMENT doc, @@ -1266,7 +1535,10 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconReadsUprightOnATurnedPage) { // A button another app made with a picture reads as having one in every // layout, and drawing its appearance again never takes the picture out of // the file: /MK /I still names the same form, and the form is as it was. -// The document lists that form as a drawing, once, whoever made it. +// Drawn again, every layout that shows an icon shows this one, in a stream +// of its own that leaves the other app's appearance as it was, and every +// layout that shows a caption shows it. The document lists the form as a +// drawing, once, whoever made it. TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { const std::string bytes = ForeignButtonsPdf(); embedpdf_test::ScopedTestDocument foreign( @@ -1274,8 +1546,8 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { nullptr)); ASSERT_TRUE(foreign); CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); - const std::string icon_before = ObjectGraph(doc)[kForeignIcon]; - ASSERT_FALSE(icon_before.empty()); + const std::map before = ObjectGraph(doc); + ASSERT_FALSE(before.at(kForeignIcon).empty()); EXPECT_EQ(std::vector({kForeignIcon}), DrawingsOf(foreign.get())); ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); @@ -1290,12 +1562,41 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); // Its appearance draws the icon and is no wrapper of ours. EXPECT_EQ(0u, EPDFAnnot_GetDrawing(button.get())); + const uint32_t foreign_appearance = AppearanceOf(button.get())->GetObjNum(); ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); EXPECT_TRUE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + + // The button stands at (20, 10 + 50 * layout), 102 by 42. + const float bottom = 10 + 50 * layout; + const Shown shown(page.get(), 4); + const bool shows_icon = layout != 0; + const bool shows_caption = layout != 1; + EXPECT_EQ(shows_icon, DrawnIconOf(button.get()).has_value()); + if (shows_icon) { + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + EXPECT_NE(foreign_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(before.at(foreign_appearance), + ObjectGraph(doc)[foreign_appearance]); + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const CFX_PointF red = RedPointOf(icon->picture); + const CFX_PointF blue = BluePointOf(icon->picture); + EXPECT_EQ(kRed, shown.At(20 + red.x, bottom + red.y)); + EXPECT_EQ(kBlue, shown.At(20 + blue.x, bottom + blue.y)); + } + const std::optional caption = DrawnCaptionOf(button.get()); + EXPECT_EQ(shows_caption, caption.has_value()); + EXPECT_EQ(shows_caption, + shown.HasInkIn(CFX_FloatRect(20, bottom, 122, bottom + 42))); + if (shows_icon && shows_caption) { + ExpectPartsShare(layout, CFX_FloatRect(1, 1, 101, 41), + CaptionRoomOf(button.get()), *caption, + DrawnIconOf(button.get())->plate); + } } - EXPECT_EQ(icon_before, ObjectGraph(doc)[kForeignIcon]); + EXPECT_EQ(before.at(kForeignIcon), ObjectGraph(doc)[kForeignIcon]); EXPECT_EQ(std::vector({kForeignIcon}), DrawingsOf(foreign.get())); @@ -1412,25 +1713,46 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconOnlyButtonNeedsNoFont) { // An appearance that is the icon itself (/AP /N and /MK /I naming one form) // is replaced by a stream of its own: drawing the button never writes into -// its picture. +// its picture, in any layout, whether the layout shows the icon or not. TEST_F(EPDFWidgetDrawingEmbedderTest, AnAppearanceThatIsTheIconIsNotWrittenInto) { - ScopedFPDFAnnotation button = NewButton({100, 250, 300, 150}, 0.0f); - ASSERT_TRUE(button); const unsigned int icon = AddDrawing(doc_.get()); - ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, icon)); - ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 1)); - MutableDictOf(button.get()) - ->GetOrCreateDictFor("AP") - ->SetNewFor("N", pdf(), icon); + ASSERT_NE(0u, icon); const std::string icon_before = ObjectGraph(pdf())[icon]; + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), layout + 1, 400, 400)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button = + NewButton({100, 250, 300, 150}, 0.0f, page.get()); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKText(button.get(), EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance( + button.get(), FPDF_FONT_HELVETICA, 12, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, icon)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), layout)); + MutableDictOf(button.get()) + ->GetOrCreateDictFor("AP") + ->SetNewFor("N", pdf(), icon); - ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); - EXPECT_NE(icon, AppearanceOf(button.get())->GetObjNum()); - EXPECT_EQ(icon, DrawnIconNumber(button.get())); - EXPECT_EQ(icon_before, ObjectGraph(pdf())[icon]); - EXPECT_EQ(kRed, ShownColorAt(page_.get(), 150, 200)); - EXPECT_EQ(kBlue, ShownColorAt(page_.get(), 250, 200)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_NE(icon, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(icon_before, ObjectGraph(pdf())[icon]); + EXPECT_EQ(icon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + if (layout == 0) { + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + continue; + } + EXPECT_EQ(icon, DrawnIconNumber(button.get())); + const std::optional drawn = DrawnIconOf(button.get()); + ASSERT_TRUE(drawn); + const CFX_PointF red = RedPointOf(drawn->picture); + const CFX_PointF blue = BluePointOf(drawn->picture); + const Shown shown(page.get(), 4); + EXPECT_EQ(kRed, shown.At(100 + red.x, 150 + red.y)); + EXPECT_EQ(kBlue, shown.At(100 + blue.x, 150 + blue.y)); + } } // Every job of the engine runs in a layer transaction. Inside one, a drawing @@ -2223,3 +2545,742 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, ListingDrawingsWritesNothing) { EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(file.layer)); EXPECT_TRUE(file.SaveDelta().empty()); } + +// In the layouts that show a caption and an icon, a button that has both +// draws both, each in its part of the button inside the border: the caption +// in a strip as high as its line below or above the icon, or as wide as its +// text right or left of it, and the icon fitted into the rest. Over the icon +// both take all of it, and the caption is drawn last, on top. /MK /I still +// names the drawing, and the appearance draws that same form. +TEST_F(EPDFWidgetDrawingEmbedderTest, ACaptionAndAnIconEachTakeTheirPart) { + // 204 by 104 with a border of 2: 200 by 100 inside it, from (2, 2). + const FS_RECTF kStands{100, 254, 304, 150}; + ScopedFPDFAnnotation button = NewButton(kStands, 2.0f); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + SetCaptionAndIcon(button.get(), drawing, kCaptionBelow); + const CFX_SizeF room = CaptionRoomOf(button.get()); + ASSERT_GT(room.width, 0); + ASSERT_GT(room.height, 0); + + const float low = 2 + room.height; + const float high = 102 - room.height; + const float near = 2 + room.width; + const float far = 202 - room.width; + // The picture is twice as wide as high, and stands in the middle of its + // part. Above or below the caption's strip it is as high as the room left, + // so it is the strip's height in from each side. Beside the caption's + // column it is as wide as the room left, so it is a quarter of the + // column's width in from the bottom and from the top. + const float in_x = room.height; + const float in_y = room.width / 4; + const struct { + CFX_FloatRect caption; + CFX_FloatRect icon; + CFX_FloatRect picture; + } kExpected[] = { + {CFX_FloatRect(2, 2, 202, low), CFX_FloatRect(2, low, 202, 102), + CFX_FloatRect(2 + in_x, low, 202 - in_x, 102)}, + {CFX_FloatRect(2, high, 202, 102), CFX_FloatRect(2, 2, 202, high), + CFX_FloatRect(2 + in_x, 2, 202 - in_x, high)}, + {CFX_FloatRect(far, 2, 202, 102), CFX_FloatRect(2, 2, far, 102), + CFX_FloatRect(2, 2 + in_y, far, 102 - in_y)}, + {CFX_FloatRect(2, 2, near, 102), CFX_FloatRect(near, 2, 202, 102), + CFX_FloatRect(near, 2 + in_y, 202, 102 - in_y)}, + {CFX_FloatRect(2, 2, 202, 102), CFX_FloatRect(2, 2, 202, 102), + CFX_FloatRect(2, 2, 202, 102)}, + }; + int index = 0; + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + const auto& expected = kExpected[index++]; + SetCaptionAndIcon(button.get(), drawing, layout.layout); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + ExpectRectNear(expected.icon, icon->plate, 1e-2f); + ExpectRectNear(expected.picture, icon->picture, 1e-2f); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectRectNear(expected.caption, *caption, 1e-2f); + // The icon first, the caption after it. + const std::string content = StreamBytes(AppearanceOf(button.get()).Get()); + EXPECT_LT(content.find("/FRM Do"), content.find("BT\n")); + + // Shown: the picture where it lands, red at its left, and the caption in + // the middle of its part and nowhere beside it. + const Shown shown(page_.get(), 4); + const CFX_PointF red = RedPointOf(expected.picture); + const CFX_PointF blue = BluePointOf(expected.picture); + EXPECT_EQ(kRed, shown.At(100 + red.x, 150 + red.y)); + EXPECT_EQ(kBlue, shown.At(100 + blue.x, 150 + blue.y)); + CFX_FloatRect ink = CaptionInkOf(expected.caption); + ink.Translate(100, 150); + EXPECT_TRUE(shown.HasInkIn(ink)); + if (layout.layout != kCaptionOver) { + CFX_FloatRect beside = expected.icon; + beside.Translate(100, 150); + EXPECT_FALSE(shown.HasInkIn(beside)); + } + } +} + +// The caption and the icon share the button less the border's width at each +// edge, the width /BS gives whether or not a colour paints it. /MK /IF /FB +// shares the whole button, as it gives an icon alone the whole button. +TEST_F(EPDFWidgetDrawingEmbedderTest, + ACaptionAndAnIconShareWhatTheBorderLeaves) { + const FS_RECTF kStands{100, 308, 308, 200}; // 208 by 108 + const struct { + const char* name; + std::optional border_width; + bool fit_to_bounds; + CFX_FloatRect plate; + } kCases[] = { + // No /BS: a border of 1. + {"no /BS", {}, false, CFX_FloatRect(1, 1, 207, 107)}, + {"no border", 0.0f, false, CFX_FloatRect(0, 0, 208, 108)}, + {"a border of 4", 4.0f, false, CFX_FloatRect(4, 4, 204, 104)}, + {"a border of 4, fitted to the bounds", 4.0f, true, + CFX_FloatRect(0, 0, 208, 108)}, + }; + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + for (const auto& expected : kCases) { + SCOPED_TRACE(expected.name); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFAnnotation button = NewButton(kStands, expected.border_width); + ASSERT_TRUE(button); + if (expected.fit_to_bounds) { + MutableDictOf(button.get()) + ->GetOrCreateDictFor("MK") + ->SetNewFor("IF") + ->SetNewFor("FB", true); + } + SetCaptionAndIcon(button.get(), drawing, layout.layout); + + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectPartsShare(layout.layout, expected.plate, + CaptionRoomOf(button.get()), *caption, icon->plate); + } + } +} + +// Beside a caption the icon is fitted into its own part as the icon fit +// dictionary says (ISO 32000-2 Table 250), as an icon alone is fitted into +// the whole button. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AnIconBesideACaptionIsFittedAsTheIconFitSays) { + enum class Lands { + kInTheMiddle, // the table's defaults + kFillingItsPart, // /S /A: without its proportions + kAtTheLowerLeft, // /A [0 0] + kAtTheUpperRight, // /A [1 1] + kAtItsOwnSize, // /SW /N: never scaled + }; + const struct { + const char* name; + Lands lands; + } kFits[] = {{"no /IF", Lands::kInTheMiddle}, + {"without its proportions", Lands::kFillingItsPart}, + {"at the lower left", Lands::kAtTheLowerLeft}, + {"at the upper right", Lands::kAtTheUpperRight}, + {"never scaled", Lands::kAtItsOwnSize}}; + constexpr float kWithin = 1e-2f; + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + for (const auto& fit : kFits) { + SCOPED_TRACE(fit.name); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + // 300 by 200, without a border. + ScopedFPDFAnnotation button = NewButton({50, 350, 350, 150}, 0.0f); + ASSERT_TRUE(button); + if (fit.lands != Lands::kInTheMiddle) { + RetainPtr written = + MutableDictOf(button.get()) + ->GetOrCreateDictFor("MK") + ->SetNewFor("IF"); + if (fit.lands == Lands::kFillingItsPart) { + written->SetNewFor("S", "A"); + } else if (fit.lands == Lands::kAtItsOwnSize) { + written->SetNewFor("SW", "N"); + } else { + const float share = fit.lands == Lands::kAtTheLowerLeft ? 0 : 1; + RetainPtr shares = written->SetNewFor("A"); + shares->AppendNew(share); + shares->AppendNew(share); + } + } + SetCaptionAndIcon(button.get(), drawing, layout.layout); + + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const CFX_FloatRect& part = icon->plate; + const CFX_FloatRect& picture = icon->picture; + if (fit.lands == Lands::kFillingItsPart) { + ExpectRectNear(part, picture, kWithin); + continue; + } + if (fit.lands == Lands::kAtItsOwnSize) { + EXPECT_NEAR(200, picture.Width(), kWithin); + EXPECT_NEAR(100, picture.Height(), kWithin); + } else { + // Its proportions kept, and as large as its part lets it be: it + // reaches across the part or up it. + EXPECT_NEAR(picture.Width(), 2 * picture.Height(), kWithin); + EXPECT_NEAR(1, + std::max(picture.Width() / part.Width(), + picture.Height() / part.Height()), + 1e-4f); + } + switch (fit.lands) { + case Lands::kAtTheLowerLeft: + EXPECT_NEAR(part.left, picture.left, kWithin); + EXPECT_NEAR(part.bottom, picture.bottom, kWithin); + break; + case Lands::kAtTheUpperRight: + EXPECT_NEAR(part.right, picture.right, kWithin); + EXPECT_NEAR(part.top, picture.top, kWithin); + break; + default: + EXPECT_NEAR(part.left + part.right, picture.left + picture.right, + kWithin); + EXPECT_NEAR(part.bottom + part.top, picture.bottom + picture.top, + kWithin); + break; + } + } + } +} + +// A turned button's caption and icon are laid out in the frame its quarter +// turn gives, and turn with it: the caption below the icon is at the +// reader's right of a button turned counterclockwise. +TEST_F(EPDFWidgetDrawingEmbedderTest, ACaptionAndAnIconTurnWithTheirButton) { + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + int page_index = 0; + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + struct Drawn { + FS_RECTF stands; + CFX_Matrix turn; + DrawnIcon icon; + CFX_FloatRect caption; + }; + std::vector buttons; + for (const Turn& expected : kTurns) { + SCOPED_TRACE(expected.written.value_or(0)); + const FS_RECTF stands = + TurnedBoxAt(static_cast(buttons.size()), 200); + ScopedFPDFAnnotation button = NewButton(stands, 0.0f, page.get()); + ASSERT_TRUE(button); + WriteTurn(button.get(), expected); + SetCaptionAndIcon(button.get(), drawing, layout.layout); + + RetainPtr appearance = AppearanceOf(button.get()); + ASSERT_TRUE(appearance); + EXPECT_EQ(expected.frame, appearance->GetDict()->GetRectFor("BBox")); + EXPECT_EQ(expected.turn, appearance->GetDict()->GetMatrixFor("Matrix")); + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectPartsShare(layout.layout, expected.frame, + CaptionRoomOf(button.get()), *caption, icon->plate); + buttons.push_back({stands, expected.turn, *icon, *caption}); + } + + // Shown, a point of the frame is turned, then moved to where the button + // stands. + const Shown shown(page.get(), 4); + for (const Drawn& button : buttons) { + auto on_page = [&button](const CFX_PointF& point) { + const CFX_PointF turned = button.turn.Transform(point); + return CFX_PointF(button.stands.left + turned.x, 200 + turned.y); + }; + const CFX_PointF red = on_page(RedPointOf(button.icon.picture)); + const CFX_PointF blue = on_page(BluePointOf(button.icon.picture)); + EXPECT_EQ(kRed, shown.At(red.x, red.y)); + EXPECT_EQ(kBlue, shown.At(blue.x, blue.y)); + CFX_FloatRect ink = + button.turn.TransformRect(CaptionInkOf(button.caption)); + ink.Translate(button.stands.left, 200); + EXPECT_TRUE(shown.HasInkIn(ink)); + if (layout.layout != kCaptionOver) { + CFX_FloatRect beside = button.turn.TransformRect(button.icon.plate); + beside.Translate(button.stands.left, 200); + EXPECT_FALSE(shown.HasInkIn(beside)); + } + } + } +} + +// On a page turned a quarter turn, buttons turned the other way read upright +// in every layout: the picture's left is at the reader's left, and the +// caption is where the layout puts it for the reader. +TEST_F(EPDFWidgetDrawingEmbedderTest, + ACaptionAndAnIconReadUprightOnATurnedPage) { + // The page is shown turned 90 clockwise. Each button stands 52 wide and + // 102 tall on it, 60 from the one before, and is turned 90 + // counterclockwise, so the reader sees it 102 wide and 52 tall: 100 by 50 + // inside its border of 1. + FPDFPage_SetRotation(page_.get(), 1); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + CFX_SizeF room; + int index = 0; + for (const auto& layout : kCaptionAndIconLayouts) { + const float left = 100 + 60 * index++; + ScopedFPDFAnnotation button = NewButton({left, 202, left + 52, 100}); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKRotation(button.get(), 90)); + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + SetCaptionAndIcon(button.get(), drawing, layout.layout); + room = CaptionRoomOf(button.get()); + } + const std::string bytes = SavedBytes(doc_.get()); + ASSERT_FALSE(bytes.empty()); + + embedpdf_test::ScopedTestDocument reopened( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(reopened); + ScopedFPDFPage page(FPDF_LoadPage(reopened.get(), 0)); + ASSERT_TRUE(page); + ASSERT_EQ(5, FPDFPage_GetAnnotCount(page.get())); + const Shown shown(page.get(), 4); + index = 0; + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), index)); + ASSERT_TRUE(button); + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectPartsShare(layout.layout, CFX_FloatRect(1, 1, 101, 51), room, + *caption, icon->plate); + // Shown, a page point (x, y) is at (y, x) from the top left. This button + // spans 100 to 202 across, and 100 + 60 * index to 152 + 60 * index + // down: its frame's lower left corner is 248 - 60 * index up from the + // bottom. + const float bottom = 248 - 60 * index++; + const CFX_PointF red = RedPointOf(icon->picture); + const CFX_PointF blue = BluePointOf(icon->picture); + EXPECT_EQ(kRed, shown.At(100 + red.x, bottom + red.y)); + EXPECT_EQ(kBlue, shown.At(100 + blue.x, bottom + blue.y)); + EXPECT_LT(red.x, blue.x); + CFX_FloatRect ink = CaptionInkOf(*caption); + ink.Translate(100, bottom); + EXPECT_TRUE(shown.HasInkIn(ink)); + if (layout.layout != kCaptionOver) { + CFX_FloatRect beside = icon->plate; + beside.Translate(100, bottom); + EXPECT_FALSE(shown.HasInkIn(beside)); + } + } +} + +// Without an icon a button draws its caption alone in every layout that +// shows a caption, exactly as the layout that shows the caption alone does, +// and into the appearance it had. And that layout shows no icon, though the +// button has one. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AButtonWithoutAnIconDrawsItsCaptionAlone) { + ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_SetMKText(button.get(), EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance(button.get(), FPDF_FONT_HELVETICA, + 12, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + const uint32_t appearance = AppearanceOf(button.get())->GetObjNum(); + const std::string alone = StreamBytes(AppearanceOf(button.get()).Get()); + ASSERT_NE(std::string::npos, alone.find("BT\n")); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), layout.layout)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + } + + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 0)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); +} + +// A button that has an icon and no caption draws the icon in the whole of +// the button inside its border, in every layout that shows one, as the +// layout that shows the icon alone does. Nothing is kept free for a caption +// that isn't there, and no text is drawn, so it needs no /DA and no font. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AnIconWithoutACaptionTakesTheWholeButton) { + ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); + ASSERT_TRUE(button); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + SetIcon(button.get(), drawing); + const std::string alone = StreamBytes(AppearanceOf(button.get()).Get()); + ASSERT_NE(std::string::npos, alone.find("/FRM Do")); + for (bool empty_caption : {false, true}) { + SCOPED_TRACE(empty_caption ? "an empty caption" : "no caption"); + if (empty_caption) { + ASSERT_TRUE(EPDFAnnot_SetMKText(button.get(), EPDF_MK_TEXT_CA, + GetFPDFWideString(L"").get())); + } + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), layout.layout)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + EXPECT_FALSE(DictOf(button.get())->KeyExist("DA")); + const Shown shown(page_.get()); + EXPECT_EQ(kRed, shown.At(152, 202)); + EXPECT_EQ(kBlue, shown.At(252, 202)); + } + } +} + +// A caption needs a /DA: without one no field that shows text is drawn. A +// button with a caption and an icon is then left as it was, the other app's +// appearance included, and not drawn with its caption missing. Nothing is +// written, and the picture is still shown and still named. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AButtonWhoseCaptionCannotBeDrawnIsLeftAsItWas) { + const std::string bytes = ForeignButtonsPdf(/*with_fonts=*/false); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + const std::map before = ObjectGraph(doc); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout.layout)); + ASSERT_TRUE(button); + const uint32_t appearance = AppearanceOf(button.get())->GetObjNum(); + EPDFAnnot_GenerateFormFieldAP(button.get()); + EXPECT_EQ(appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(kForeignIcon, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + // As the other app drew it: 80 by 40 in the middle, inside a border of 1. + const int bottom = 10 + 50 * layout.layout; + const Shown shown(page.get()); + EXPECT_EQ(kRed, shown.At(51, bottom + 21)); + EXPECT_EQ(kBlue, shown.At(91, bottom + 21)); + } + EXPECT_EQ(before, ObjectGraph(doc)); +} + +// A /DA that names a font the form doesn't have is drawn in a fallback font, +// as every field's text is: the caption and the icon are both drawn. +TEST_F(EPDFWidgetDrawingEmbedderTest, + ACaptionWhoseFontIsMissingIsStillDrawnBesideItsIcon) { + const std::string bytes = ForeignButtonsPdf(); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + ASSERT_TRUE( + doc->GetMutableRoot()->GetMutableDictFor("AcroForm")->RemoveFor("DR")); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout.layout)); + ASSERT_TRUE(button); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectPartsShare(layout.layout, CFX_FloatRect(1, 1, 101, 41), + CaptionRoomOf(button.get()), *caption, icon->plate); + const float bottom = 10 + 50 * layout.layout; + const Shown shown(page.get(), 4); + const CFX_PointF red = RedPointOf(icon->picture); + const CFX_PointF blue = BluePointOf(icon->picture); + EXPECT_EQ(kRed, shown.At(20 + red.x, bottom + red.y)); + EXPECT_EQ(kBlue, shown.At(20 + blue.x, bottom + blue.y)); + CFX_FloatRect ink = CaptionInkOf(*caption); + ink.Translate(20, bottom); + EXPECT_TRUE(shown.HasInkIn(ink)); + } +} + +// A caption that needs more than the button has takes all of it, and the +// icon is not drawn: the rule of PDFium's form filler. The button is then +// drawn as the layout that shows the caption alone draws it. /MK /I still +// names the picture. Over the icon nothing is shared, so both are drawn. +TEST_F(EPDFWidgetDrawingEmbedderTest, + ACaptionTooLargeForTheButtonIsDrawnAlone) { + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + const struct { + const char* name; + FS_RECTF stands; + float font_size; + int layouts[2]; + } kCases[] = { + // 100 by 40 inside the border. A line at 48 is higher than 40. + {"too high", {100, 244, 204, 200}, 48, {kCaptionBelow, kCaptionAbove}}, + // 14 by 100 inside the border. "Go" at 12 is 16 wide. + {"too wide", {100, 304, 118, 200}, 12, {kCaptionRight, kCaptionLeft}}, + }; + for (const auto& expected : kCases) { + SCOPED_TRACE(expected.name); + ScopedFPDFAnnotation button = NewButton(expected.stands, 2.0f); + ASSERT_TRUE(button); + SetCaptionAndIcon(button.get(), drawing, 0, expected.font_size); + const std::string alone = StreamBytes(AppearanceOf(button.get()).Get()); + ASSERT_NE(std::string::npos, alone.find("BT\n")); + for (int layout : expected.layouts) { + SCOPED_TRACE(layout); + SetCaptionAndIcon(button.get(), drawing, layout, expected.font_size); + EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + } + SetCaptionAndIcon(button.get(), drawing, kCaptionOver, expected.font_size); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + EXPECT_TRUE(DrawnCaptionOf(button.get())); + } +} + +// A caption whose size is fitted to its room (a font size of 0) takes a +// third of the button, of its height below or above the icon and of its +// width beside it, and is fitted to that third: the rule of PDFium's form +// filler. +TEST_F(EPDFWidgetDrawingEmbedderTest, AFittedCaptionTakesAThirdOfTheButton) { + // 204 by 104 with a border of 2: 200 by 100 inside it, from (2, 2). + ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); + ASSERT_TRUE(button); + const unsigned int drawing = AddDrawing(doc_.get()); + ASSERT_NE(0u, drawing); + const float third_up = 2 + 100.0f / 3; + const float third_down = 102 - 100.0f / 3; + const float third_along = 2 + 200.0f / 3; + const float third_back = 202 - 200.0f / 3; + const struct { + CFX_FloatRect caption; + CFX_FloatRect icon; + } kExpected[] = { + {CFX_FloatRect(2, 2, 202, third_up), + CFX_FloatRect(2, third_up, 202, 102)}, + {CFX_FloatRect(2, third_down, 202, 102), + CFX_FloatRect(2, 2, 202, third_down)}, + {CFX_FloatRect(third_back, 2, 202, 102), + CFX_FloatRect(2, 2, third_back, 102)}, + {CFX_FloatRect(2, 2, third_along, 102), + CFX_FloatRect(third_along, 2, 202, 102)}, + {CFX_FloatRect(2, 2, 202, 102), CFX_FloatRect(2, 2, 202, 102)}, + }; + int index = 0; + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + const auto& expected = kExpected[index++]; + SetCaptionAndIcon(button.get(), drawing, layout.layout, 0); + EXPECT_EQ(drawing, DrawnIconNumber(button.get())); + const std::optional icon = DrawnIconOf(button.get()); + ASSERT_TRUE(icon); + ExpectRectNear(expected.icon, icon->plate, 1e-2f); + const std::optional caption = DrawnCaptionOf(button.get()); + ASSERT_TRUE(caption); + ExpectRectNear(expected.caption, *caption, 1e-2f); + // The size the caption is written at fits its part. + const float size = DrawnCaptionSizeOf(button.get()); + EXPECT_GT(size, 0); + const CFX_SizeF room = CaptionRoomOf(button.get(), size); + EXPECT_LE(room.width, caption->Width()); + EXPECT_LE(room.height, caption->Height()); + const Shown shown(page_.get(), 4); + CFX_FloatRect part = *caption; + part.Translate(100, 150); + EXPECT_TRUE(shown.HasInkIn(part)); + } +} + +// The generator can draw a button that shows a caption and an icon in +// memory: a stream of no document that draws the file's icon and names a +// font for the caption, and nothing is written. No render asks for it yet. +TEST_F(EPDFWidgetDrawingEmbedderTest, ACaptionAndAnIconCanBeDrawnInMemory) { + const std::string bytes = + ForeignButtonsPdf(/*with_fonts=*/true, /*with_appearances=*/false); + embedpdf_test::ScopedTestDocument foreign( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(foreign); + CPDF_Document* doc = CPDFDocumentFromFPDFDocument(foreign.get()); + const std::map before = ObjectGraph(doc); + ScopedFPDFPage page(FPDF_LoadPage(foreign.get(), 0)); + ASSERT_TRUE(page); + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout.layout)); + ASSERT_TRUE(button); + std::optional drawn = + CPDF_AppearanceGenerator::GenerateEphemeralFormAP( + doc, DictOf(button.get()), CPDF_AppearanceGenerator::kPushButton); + ASSERT_TRUE(drawn.has_value()); + ASSERT_TRUE(drawn->normal_stream); + EXPECT_EQ(0u, drawn->normal_stream->GetObjNum()); + EXPECT_EQ(CFX_FloatRect(0, 0, 102, 42), + drawn->normal_stream->GetDict()->GetRectFor("BBox")); + const std::string content = StreamBytes(drawn->normal_stream.Get()); + EXPECT_NE(std::string::npos, content.find("/FRM Do")); + EXPECT_LT(content.find("/FRM Do"), content.find("BT\n")); + RetainPtr resources = + drawn->normal_stream->GetDict()->GetDictFor("Resources"); + ASSERT_TRUE(resources); + EXPECT_EQ( + kForeignIcon, + resources->GetDictFor("XObject")->GetStreamFor("FRM")->GetObjNum()); + ASSERT_TRUE(resources->GetDictFor("Font")); + EXPECT_EQ(1u, resources->GetDictFor("Font")->size()); + EXPECT_FALSE(DictOf(button.get())->KeyExist("AP")); + } + EXPECT_EQ(before, ObjectGraph(doc)); +} + +// How a button is shared between its caption and its icon is the rule of +// PDFium's own form filler (GetPushButtonAppStream() in +// fpdfsdk/cpdfsdk_appstream.cpp), which draws a button for upstream's +// viewers. Both draw the same seven buttons here, one in each layout, at a +// font size of 12 and at a fitted one: the icon is clipped to the same part +// and lands in the same place, and the caption is written at the same size +// from the same point. +TEST_F(EPDFWidgetDrawingEmbedderTest, AButtonIsSharedAsTheFormFillerSharesIt) { + constexpr float kWithin = 1e-2f; + // A border and a background that show, for the files kept to look at. + auto paint = [](FPDF_ANNOTATION button) { + ASSERT_TRUE(EPDFAnnot_SetMKColor(button, EPDF_MK_COLOR_BC, 0, 153, 0)); + ASSERT_TRUE(EPDFAnnot_SetMKColor(button, EPDF_MK_COLOR_BG, 255, 255, 204)); + }; + for (int font_size : {12, 0}) { + SCOPED_TRACE(font_size); + const std::string bytes = ForeignButtonsPdf( + /*with_fonts=*/true, /*with_appearances=*/false, font_size); + + // The form filler's, in a document of its own. + std::vector theirs; + std::string theirs_saved; + { + ScopedFPDFDocument document( + FPDF_LoadMemDocument(bytes.data(), bytes.size(), nullptr)); + ASSERT_TRUE(document); + FPDF_FORMHANDLE form = SetupFormFillEnvironment( + document.get(), JavaScriptOption::kDisableJavaScript); + ASSERT_TRUE(form); + { + ScopedFPDFPage page(FPDF_LoadPage(document.get(), 0)); + ASSERT_TRUE(page); + FORM_OnAfterLoadPage(page.get(), form); + CPDFSDK_PageView* view = + CPDFSDKFormFillEnvironmentFromFPDFFormHandle(form) + ->GetOrCreatePageView(IPDFPageFromFPDFPage(page.get())); + ASSERT_TRUE(view); + for (int layout = 0; layout <= 6; ++layout) { + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + paint(button.get()); + CPDFSDK_Widget* widget = + ToCPDFSDKWidget(view->GetAnnotByDict(DictOf(button.get()))); + ASSERT_TRUE(widget); + widget->ResetAppearance(std::nullopt, + CPDFSDK_Widget::kValueUnchanged); + RetainPtr appearance = AppearanceOf(button.get()); + ASSERT_TRUE(appearance); + theirs.push_back(ReadButtonContent(StreamBytes(appearance.Get()))); + } + FORM_OnBeforeClosePage(page.get(), form); + } + FPDFDOC_ExitFormFillEnvironment(form); + theirs_saved = SavedBytes(document.get()); + } + + // Ours. + embedpdf_test::ScopedTestDocument document( + embedpdf_test::LoadMemDocumentForTest(bytes.data(), bytes.size(), + nullptr)); + ASSERT_TRUE(document); + { + ScopedFPDFPage page(FPDF_LoadPage(document.get(), 0)); + ASSERT_TRUE(page); + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + paint(button.get()); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + const ButtonContent ours = + ReadButtonContent(StreamBytes(AppearanceOf(button.get()).Get())); + const ButtonContent& their = theirs[layout]; + + const bool shows_icon = layout != 0; + ASSERT_EQ(shows_icon, their.picture.has_value()); + ASSERT_EQ(shows_icon, ours.picture.has_value()); + if (shows_icon) { + ASSERT_TRUE(their.icon_part); + ASSERT_TRUE(ours.icon_part); + ExpectRectNear(*their.icon_part, *ours.icon_part, kWithin); + ExpectRectNear(*their.picture, *ours.picture, kWithin); + } + const bool shows_caption = layout != 1; + ASSERT_EQ(shows_caption, their.caption_start.has_value()); + ASSERT_EQ(shows_caption, ours.caption_start.has_value()); + if (shows_caption) { + ASSERT_TRUE(their.caption_size); + ASSERT_TRUE(ours.caption_size); + EXPECT_EQ(*their.caption_size, *ours.caption_size); + EXPECT_NEAR(their.caption_start->x, ours.caption_start->x, kWithin); + EXPECT_NEAR(their.caption_start->y, ours.caption_start->y, kWithin); + } + } + } + if (font_size == 12) { + KeepSample(theirs_saved, "button-layouts-drawn-by-pdfium-form-filler"); + KeepSample(SavedBytes(document.get()), "button-layouts-drawn-by-us"); + // And the same buttons without an appearance, for a viewer that draws + // its own. + ScopedFPDFDocument undrawn( + FPDF_LoadMemDocument(bytes.data(), bytes.size(), nullptr)); + ASSERT_TRUE(undrawn); + ScopedFPDFPage page(FPDF_LoadPage(undrawn.get(), 0)); + ASSERT_TRUE(page); + for (int layout = 0; layout <= 6; ++layout) { + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + paint(button.get()); + } + CPDFDocumentFromFPDFDocument(undrawn.get()) + ->GetMutableRoot() + ->GetMutableDictFor("AcroForm") + ->SetNewFor("NeedAppearances", true); + KeepSample(SavedBytes(undrawn.get()), "button-layouts-not-drawn"); + } + } +} diff --git a/public/epdf_annot.h b/public/epdf_annot.h index df6e2a8509..bf685df430 100644 --- a/public/epdf_annot.h +++ b/public/epdf_annot.h @@ -1715,7 +1715,16 @@ FPDF_EXPORT int FPDF_CALLCONV EPDFAnnot_GetMKRotation(FPDF_ANNOTATION annot); // 32000-2 Table 250; without one: always scaled, keeping its // proportions, in the middle). It draws no caption, and without // an icon it is the box alone. In the layouts that show a -// caption and an icon (/MK /TP 2 to 6) the icon is not drawn. +// caption and an icon (/MK /TP 2 to 6), a button that has both +// draws both inside the border: the caption in a strip as high +// as its line below or above the icon, or as wide as its text +// right or left of it, and the icon fitted into the rest; over +// the icon both take all of it, the caption on top. A caption +// whose font size is 0 takes a third instead, and is fitted to +// it. A caption too large for the button is drawn alone. Without +// an icon the caption is drawn alone, and without a caption the +// icon. A caption needs a /DA: a button that has a caption and +// none is left as it was. // - /Sig, and a widget in no field -> its box: the background and border // as a text field draws them. It replaces whatever the widget // shows, a signature drawn into it included. diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 725e17a606..48891d2ba4 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -393,8 +393,19 @@ EPDFViewEmbedderTest.EPDFGetPageRotateByIndex 2 EPDFViewEmbedderTest.EPDFGetPageTabOrderByIndex 2 EPDFViewEmbedderTest.EPDFGetPageUserUnitByIndex 1 EPDFViewEmbedderTest.ImageOfAnotherDocumentDraws 4 +EPDFWidgetDrawingEmbedderTest.AButtonIsSharedAsTheFormFillerSharesIt 14 +EPDFWidgetDrawingEmbedderTest.AButtonWhoseCaptionCannotBeDrawnIsLeftAsItWas 5 +EPDFWidgetDrawingEmbedderTest.AButtonWithoutAnIconDrawsItsCaptionAlone 10 +EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconCanBeDrawnInMemory 3 +EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconEachTakeTheirPart 16 +EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconReadUprightOnATurnedPage 27 +EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconShareWhatTheBorderLeaves 87 +EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconTurnWithTheirButton 112 +EPDFWidgetDrawingEmbedderTest.ACaptionTooLargeForTheButtonIsDrawnAlone 15 +EPDFWidgetDrawingEmbedderTest.ACaptionWhoseFontIsMissingIsStillDrawnBesideItsIcon 17 EPDFWidgetDrawingEmbedderTest.ADrawingWithAMatrixIsPlacedByItsBox 14 -EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 14 +EPDFWidgetDrawingEmbedderTest.AFittedCaptionTakesAThirdOfTheButton 15 +EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 19 EPDFWidgetDrawingEmbedderTest.AForeignIconOnlyButtonSurvivesARedraw 5 EPDFWidgetDrawingEmbedderTest.AFormWithoutPicturesListsNoDrawing 16 EPDFWidgetDrawingEmbedderTest.AMarkIsPlacedAsAPageIsFitted 196 @@ -404,13 +415,15 @@ EPDFWidgetDrawingEmbedderTest.ARefitKeepsTheDrawingShared 16 EPDFWidgetDrawingEmbedderTest.AReopenedFilePlacesItsDrawingAgain 14 EPDFWidgetDrawingEmbedderTest.AReplacedOrClearedIconIsInNoSavedFile 15 EPDFWidgetDrawingEmbedderTest.ASavedFileHoldsTheIconInBothPlaces 11 -EPDFWidgetDrawingEmbedderTest.AnAppearanceThatIsTheIconIsNotWrittenInto 11 +EPDFWidgetDrawingEmbedderTest.AnAppearanceThatIsTheIconIsNotWrittenInto 42 +EPDFWidgetDrawingEmbedderTest.AnIconBesideACaptionIsFittedAsTheIconFitSays 107 EPDFWidgetDrawingEmbedderTest.AnIconIsFittedAsTheIconFitSays 122 EPDFWidgetDrawingEmbedderTest.AnIconIsFittedInsideTheBorder 42 EPDFWidgetDrawingEmbedderTest.AnIconOnlyButtonCanBeDrawnInMemory 3 EPDFWidgetDrawingEmbedderTest.AnIconOnlyButtonNeedsNoFont 3 EPDFWidgetDrawingEmbedderTest.AnIconReadsUprightOnATurnedPage 11 EPDFWidgetDrawingEmbedderTest.AnIconTurnsWithItsButton 52 +EPDFWidgetDrawingEmbedderTest.AnIconWithoutACaptionTakesTheWholeButton 21 EPDFWidgetDrawingEmbedderTest.AnIncrementalSaveKeepsWhatTheFileHeld 11 EPDFWidgetDrawingEmbedderTest.ChangingOnePlaceLeavesTheOthers 32 EPDFWidgetDrawingEmbedderTest.EachWidgetOfAFieldShowsTheSameIcon 18 From 9267b488d24d4dec7b61ef964a1785c82a3702f6 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Mon, 23 Nov 2026 13:48:04 +0200 Subject: [PATCH 3/5] Give a redrawn push button a stream, an /AP and an /MK of its own A review of this pull request found five ways a button's redraw or an /MK setter reached something another widget may still use, or kept something the file should no longer hold. A push button that draws its caption alone wrote into the stream it had. When that stream was the button's icon itself, as another app may write one, and the icon had been cleared (EPDFAnnot_ClearMKIcon), the caption was drawn into the picture, and a second button that showed the picture went blank. And a button drawn with a caption and an icon kept /XObject /FRM in the reused stream's resources after its icon was cleared, so a full save still held the picture. A push button's appearance is a new stream on every redraw now, as a check box's are. The stream it had is neither written into nor opened for writing, so nothing of it is copied into a layer either. Two widgets may name one indirect /AP. Replacing /N in it showed the new appearance in both. Before a form drawer replaces an appearance, the widget gets a copy of the /AP as its own (OwnAppearanceDict), as a placed drawing's appearance already does (SetDetachedNormalAppearance). This covers the push button, the check box, the radio button and the box of a signature field. Two widgets may name one indirect /MK. EPDFAnnot_SetMKIcon and EPDFAnnot_ClearMKIcon changed both, and so did every older /MK setter: colour, text, text position and rotation. Each writes the annotation's own /MK now (OwnMKForWriting), and a call that changes nothing copies nothing. EPDFAnnot_ClearMKColor no longer writes when there is nothing to remove. An /MK /I that names an image, not a form, was read as an icon by EPDFAnnot_GetMKIcon and EPDFAnnot_HasMKIcon, though the list leaves it out and the export refuses it. An icon is a form XObject everywhere now: the getter gives 0 for anything else, and the generator draws none. Text fields, combo boxes and list boxes still write into the stream they have. A border given by /Border alone is still drawn 1 wide in every form field: that is a change of its own, for every field. --- .../cpdf_appearancegenerator_forms.cpp | 73 +-- docs/embedpdf/fork-ledger.md | 8 +- fpdfsdk/epdf_annot.cpp | 77 ++- fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 453 +++++++++++++++++- public/epdf_annot.h | 31 +- testing/embedpdf_layer_mode_writers.txt | 16 +- 6 files changed, 576 insertions(+), 82 deletions(-) diff --git a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp index 69a4e14811..b01b3f9045 100644 --- a/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp +++ b/core/fpdfdoc/cpdf_appearancegenerator_forms.cpp @@ -512,28 +512,34 @@ ButtonLayout ButtonLayoutOf(const CPDF_Dictionary* annot_dict) { // The normal icon of a push button (/MK /I) when it can be drawn. An icon is // drawn by reference, so only one that is an object of its own can be: a -// file always holds a stream so. +// file always holds a stream so. And an icon is a form XObject (ISO 32000-2 +// Table 192): an entry that names anything else, an image say, is no icon, +// here as for EPDFAnnot_GetMKIcon() and EPDFDoc_GetDrawings(). RetainPtr ButtonIconOf(const CPDF_Dictionary* mk_dict) { RetainPtr icon = mk_dict ? mk_dict->GetStreamFor(pdfium::appearance::kI) : nullptr; - return icon && icon->GetObjNum() != 0 ? icon : nullptr; + return icon && icon->GetObjNum() != 0 && + icon->GetDict()->GetNameFor("Subtype") == "Form" + ? icon + : nullptr; } -// True when `stream` is one of the icons the widget's /MK names. An -// appearance is never written into one: it would draw over the picture. -bool IsAnIconOf(const CPDF_Dictionary* annot_dict, const CPDF_Stream* stream) { - RetainPtr mk = annot_dict->GetDictFor("MK"); - if (!mk || stream->GetObjNum() == 0) { - return false; - } - for (const char* key : {pdfium::appearance::kI, pdfium::appearance::kRI, - pdfium::appearance::kIX}) { - RetainPtr icon = mk->GetStreamFor(key); - if (icon && icon->GetObjNum() == stream->GetObjNum()) { - return true; +// A widget's /AP, to replace an appearance in: a dictionary of the widget's +// own. One that is an indirect object may be another widget's too, and a new +// /N in it would show in that widget as well. So the widget gets a copy +// first, which names the same appearances. +RetainPtr OwnAppearanceDict(CPDF_Document* doc, + CPDF_Dictionary* annot_dict) { + RetainPtr entry = + annot_dict->GetObjectFor(pdfium::annotation::kAP); + if (entry && entry->IsReference()) { + RetainPtr shared = + annot_dict->GetDictFor(pdfium::annotation::kAP); + if (shared) { + annot_dict->SetFor(pdfium::annotation::kAP, shared->CloneForHolder(doc)); } } - return false; + return annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP); } // True for a push button that draws no caption beside its icon: one that @@ -1053,7 +1059,7 @@ void GenerateIconButtonAP(APGenerationTarget* target, } const uint32_t normal_obj_num = CreateFormXObjectStream( doc, content, dims.bbox, dims.matrix, std::move(resources)); - target->persistent_annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP) + OwnAppearanceDict(doc, target->persistent_annot_dict) ->SetNewFor("N", doc, normal_obj_num); } @@ -1205,19 +1211,24 @@ bool GenerateFormAPToTarget(APGenerationTarget* target, if (!resource_font_dict) { return false; // registered /DA font without a resource: no appearance } - RetainPtr ap_dict = - annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP); - RetainPtr normal_stream = ap_dict->GetMutableStreamFor("N"); - // An appearance that draws an icon is a new stream, as an icon-only - // button's is: the old /N may be another widget's too, and its resources - // may be shared, where /FRM would name this button's icon for all of them. - // And no appearance is written into a stream that is one of the widget's - // icons. - if (!normal_stream || drawn_icon != 0 || - IsAnIconOf(annot_dict, normal_stream.Get())) { + // A push button's appearance is a new stream every time, as a check box's + // are. The stream it had may be another widget's too; it may be a picture + // (the button's icon, now or before it was cleared), which is never drawn + // over; and its resources may still name an icon the button no longer + // shows, which would keep that picture in the file. The old stream is not + // opened for writing, so nothing of it is copied into a layer. The other + // fields write into the stream they have. + RetainPtr normal_stream; + if (type != CPDF_AppearanceGenerator::kPushButton) { + RetainPtr ap_dict = + annot_dict->GetMutableDictFor(pdfium::annotation::kAP); + normal_stream = ap_dict ? ap_dict->GetMutableStreamFor("N") : nullptr; + } + if (!normal_stream) { normal_stream = doc->NewIndirect(pdfium::MakeRetain()); - ap_dict->SetNewFor("N", doc, normal_stream->GetObjNum()); + OwnAppearanceDict(doc, annot_dict) + ->SetNewFor("N", doc, normal_stream->GetObjNum()); } normal_stream->SetDataFromStringstreamAndRemoveFilter(&app_stream); RetainPtr stream_dict = normal_stream->GetMutableDict(); @@ -1405,7 +1416,7 @@ void CPDF_AppearanceGenerator::GenerateBoxFormAP(CPDF_Document* doc, // too, or a drawing someone placed in this one. const uint32_t normal_obj_num = CreateFormXObjectStream(doc, content, dims.bbox, dims.matrix); - annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP) + OwnAppearanceDict(doc, annot_dict) ->SetNewFor("N", doc, normal_obj_num); } @@ -1440,8 +1451,7 @@ void CPDF_AppearanceGenerator::GenerateCheckboxFormAP( const uint32_t yes_obj_num = CreateFormXObjectStream(doc, yes_content, dims.bbox, dims.matrix); - RetainPtr ap_dict = - annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP); + RetainPtr ap_dict = OwnAppearanceDict(doc, annot_dict); ByteString on_state; RetainPtr old_n = ap_dict->GetDictFor("N"); @@ -1550,8 +1560,7 @@ void CPDF_AppearanceGenerator::GenerateRadioButtonFormAP( const uint32_t yes_obj_num = CreateFormXObjectStream(doc, yes_content, dims.bbox, dims.matrix); - RetainPtr ap_dict = - annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP); + RetainPtr ap_dict = OwnAppearanceDict(doc, annot_dict); ByteString on_state; RetainPtr old_n = ap_dict->GetDictFor("N"); diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index cae25462db..21b8d409ca 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -351,7 +351,13 @@ met only in buttons other apps made. Either way the icon fit (`/MK /IF`) is read upstream's `CPDF_IconFit` and the icon is placed by our own `IconPlacement`, by ISO 32000-2 Table 250: upstream's own placement (`CPDF_IconFit::GetScale` and `GetImageOffset`, used by its form filler) puts an icon whose fit names no `/A` at the bottom left, where the table's default is the -middle, and reads an icon narrower than one unit as one unit wide. +middle, and reads an icon narrower than one unit as one unit wide. A push button's appearance is +a new stream on every redraw, as a check box's are: the stream it had may be another widget's or +a picture the button showed, so it is never written into, and the icon its resources named goes +with it. Before a form drawer replaces an appearance, a widget whose `/AP` is an indirect object +gets a copy of its own (`OwnAppearanceDict`), and `epdf_annot.cpp` does the same for `/MK` before +a setter writes it (`OwnMKForWriting`): another widget may name the same dictionary. An icon is a +form XObject everywhere (`ButtonIconOf`, `MKIconForm`). **Guarded by.** `check-fork-tags.py` fails when upstream's files differ from upstream (`fork-pristine.txt`); `upstream-preview.py` lists new upstream callers of `CPDF_GenerateAP::`, and upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for a button's icon; diff --git a/fpdfsdk/epdf_annot.cpp b/fpdfsdk/epdf_annot.cpp index a8ad9b6c4a..7947cff327 100644 --- a/fpdfsdk/epdf_annot.cpp +++ b/fpdfsdk/epdf_annot.cpp @@ -4385,6 +4385,46 @@ EPDFAnnot_GetDrawingRect(FPDF_ANNOTATION annot, return true; } +namespace { + +// An annotation's /MK, to write an entry in: a dictionary of the +// annotation's own. One that is an indirect object may be another widget's +// too, and a change to it would show in that widget as well. So the +// annotation gets a copy first, with the same entries. With |create|, one +// that has no /MK gets an empty one; without, null. +RetainPtr OwnMKForWriting(FPDF_ANNOTATION annot, + CPDF_Dictionary* annot_dict, + bool create) { + RetainPtr entry = annot_dict->GetObjectFor("MK"); + if (entry && entry->IsReference()) { + RetainPtr shared = annot_dict->GetDictFor("MK"); + CPDF_AnnotContext* ctx = CPDFAnnotContextFromFPDFAnnotation(annot); + CPDF_Document* doc = + ctx && ctx->GetPage() ? ctx->GetPage()->GetDocument() : nullptr; + if (shared) { + annot_dict->SetFor("MK", + doc ? shared->CloneForHolder(doc) : shared->Clone()); + } + } + return create ? annot_dict->GetOrCreateDictFor("MK") + : annot_dict->GetMutableDictFor("MK"); +} + +// The form a /MK names as its icon |key|, or null. An icon is a form XObject +// that is an object of its own (ISO 32000-2 Table 192): an entry that names +// anything else, an image say, is no icon. Nothing draws, lists or exports +// it. +RetainPtr MKIconForm(const CPDF_Dictionary* mk, + const char* key) { + RetainPtr icon = mk ? mk->GetStreamFor(key) : nullptr; + return icon && icon->GetObjNum() != 0 && + icon->GetDict()->GetNameFor("Subtype") == "Form" + ? icon + : nullptr; +} + +} // namespace + static ByteString GetMKColorKey(EPDF_MK_COLORTYPE type) { switch (type) { case EPDF_MK_COLOR_BG: @@ -4407,7 +4447,8 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKColor(FPDF_ANNOTATION annot, return false; } - RetainPtr pMK = pAnnotDict->GetOrCreateDictFor("MK"); + RetainPtr pMK = + OwnMKForWriting(annot, pAnnotDict.Get(), /*create=*/true); ByteString key = GetMKColorKey(type); RetainPtr pColor = pMK->GetMutableArrayFor(key.AsStringView()); @@ -4466,13 +4507,18 @@ EPDFAnnot_ClearMKColor(FPDF_ANNOTATION annot, EPDF_MK_COLORTYPE type) { return false; } - RetainPtr pMK = pAnnotDict->GetMutableDictFor("MK"); - if (!pMK) { + // Nothing to remove: no write. + ByteString key = GetMKColorKey(type); + RetainPtr mk = pAnnotDict->GetDictFor("MK"); + if (!mk || !mk->KeyExist(key.AsStringView())) { return true; } - ByteString key = GetMKColorKey(type); - pMK->RemoveFor(key.AsStringView()); + RetainPtr pMK = + OwnMKForWriting(annot, pAnnotDict.Get(), /*create=*/false); + if (pMK) { + pMK->RemoveFor(key.AsStringView()); + } return true; } @@ -4527,9 +4573,10 @@ FPDF_BOOL SetMKNumber(FPDF_ANNOTATION annot, return false; } if (value != absent_value) { - dict->GetOrCreateDictFor("MK")->SetNewFor(key, value); + OwnMKForWriting(annot, dict.Get(), /*create=*/true) + ->SetNewFor(key, value); } else if (RetainPtr mutable_mk = - dict->GetMutableDictFor("MK")) { + OwnMKForWriting(annot, dict.Get(), /*create=*/false)) { mutable_mk->RemoveFor(key); } return true; @@ -4561,10 +4608,10 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKText(FPDF_ANNOTATION annot, return false; } if (next) { - dict->GetOrCreateDictFor("MK")->SetNewFor( - key, next->AsStringView()); + OwnMKForWriting(annot, dict.Get(), /*create=*/true) + ->SetNewFor(key, next->AsStringView()); } else if (RetainPtr mutable_mk = - dict->GetMutableDictFor("MK")) { + OwnMKForWriting(annot, dict.Get(), /*create=*/false)) { mutable_mk->RemoveFor(key); } return true; @@ -4656,8 +4703,8 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKIcon(FPDF_ANNOTATION annot, if (!annot_dict) { return false; } - annot_dict->GetOrCreateDictFor("MK")->SetNewFor(key, doc, - drawing); + OwnMKForWriting(annot, annot_dict.Get(), /*create=*/true) + ->SetNewFor(key, doc, drawing); return true; } @@ -4667,7 +4714,7 @@ EPDFAnnot_GetMKIcon(FPDF_ANNOTATION annot, int which) { const char* key = MKIconKey(which); RetainPtr mk = key ? WidgetMKFromFPDFAnnotation(annot) : nullptr; - RetainPtr icon = mk ? mk->GetStreamFor(key) : nullptr; + RetainPtr icon = key ? MKIconForm(mk.Get(), key) : nullptr; return icon ? icon->GetObjNum() : 0; } @@ -4686,7 +4733,7 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_ClearMKIcon(FPDF_ANNOTATION annot, RetainPtr dict = GetMutableAnnotDictFromFPDFAnnotation(annot); RetainPtr mutable_mk = - dict ? dict->GetMutableDictFor("MK") : nullptr; + dict ? OwnMKForWriting(annot, dict.Get(), /*create=*/false) : nullptr; if (!mutable_mk) { return false; } @@ -4700,7 +4747,7 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_HasMKIcon(FPDF_ANNOTATION annot, const char* key = MKIconKey(which); RetainPtr mk = key ? WidgetMKFromFPDFAnnotation(annot) : nullptr; - return mk && mk->GetStreamFor(key); + return key && !!MKIconForm(mk.Get(), key); } FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp index 2b93254de0..43d9dce1ac 100644 --- a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -29,11 +30,13 @@ #include "core/fpdfapi/parser/cpdf_boolean.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/cpdf_layer_document.h" #include "core/fpdfapi/parser/cpdf_name.h" #include "core/fpdfapi/parser/cpdf_number.h" #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" #include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "core/fpdfapi/parser/cpdf_string.h" #include "core/fpdfdoc/cpdf_annot.h" #include "core/fpdfdoc/cpdf_appearancegenerator.h" #include "core/fxcrt/fx_coordinates.h" @@ -725,12 +728,13 @@ class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { // A new widget of `field` on `page`, a page of `document`, standing at // `rect`, with a border of `border_width` when one is given: /BS /W, and no - // colour to paint it. + // colour to paint it. A check box's names its checked state `on_state`. ScopedFPDFAnnotation NewWidget(FPDF_DOCUMENT document, uint32_t field, FPDF_PAGE page, const FS_RECTF& rect, - std::optional border_width = {}) { + std::optional border_width = {}, + const char* on_state = nullptr) { uint32_t number = 0; { ScopedFPDFAnnotation widget( @@ -743,7 +747,7 @@ class EPDFWidgetDrawingEmbedderTest : public EmbedderTest { } number = EPDFAnnot_GetObjectNumber(widget.get()); } - EXPECT_TRUE(EPDFForm_AttachWidget(document, field, number, nullptr, 0)); + EXPECT_TRUE(EPDFForm_AttachWidget(document, field, number, on_state, 0)); return ScopedFPDFAnnotation(EPDFPage_GetAnnotByObjectNumber(page, number)); } @@ -1535,9 +1539,9 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconReadsUprightOnATurnedPage) { // A button another app made with a picture reads as having one in every // layout, and drawing its appearance again never takes the picture out of // the file: /MK /I still names the same form, and the form is as it was. -// Drawn again, every layout that shows an icon shows this one, in a stream -// of its own that leaves the other app's appearance as it was, and every -// layout that shows a caption shows it. The document lists the form as a +// Drawn again, every layout that shows an icon shows this one, and every +// layout that shows a caption shows it, in a stream of its own that leaves +// the other app's appearance as it was. The document lists the form as a // drawing, once, whoever made it. TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { const std::string bytes = ForeignButtonsPdf(); @@ -1574,11 +1578,13 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AForeignButtonKeepsItsIconInEveryLayout) { const bool shows_icon = layout != 0; const bool shows_caption = layout != 1; EXPECT_EQ(shows_icon, DrawnIconOf(button.get()).has_value()); + // A new stream in every layout, and it names the icon only where it + // draws it. + EXPECT_NE(foreign_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(before.at(foreign_appearance), + ObjectGraph(doc)[foreign_appearance]); + EXPECT_EQ(shows_icon ? kForeignIcon : 0u, DrawnIconNumber(button.get())); if (shows_icon) { - EXPECT_EQ(kForeignIcon, DrawnIconNumber(button.get())); - EXPECT_NE(foreign_appearance, AppearanceOf(button.get())->GetObjNum()); - EXPECT_EQ(before.at(foreign_appearance), - ObjectGraph(doc)[foreign_appearance]); const std::optional icon = DrawnIconOf(button.get()); ASSERT_TRUE(icon); const CFX_PointF red = RedPointOf(icon->picture); @@ -2895,9 +2901,9 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, } // Without an icon a button draws its caption alone in every layout that -// shows a caption, exactly as the layout that shows the caption alone does, -// and into the appearance it had. And that layout shows no icon, though the -// button has one. +// shows a caption, exactly as the layout that shows the caption alone does. +// And that layout shows no icon, though the button has one. Each time the +// appearance is a new stream, and the one before is left as it was. TEST_F(EPDFWidgetDrawingEmbedderTest, AButtonWithoutAnIconDrawsItsCaptionAlone) { ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); @@ -2907,25 +2913,30 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance(button.get(), FPDF_FONT_HELVETICA, 12, 0, 0, 0)); ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); - const uint32_t appearance = AppearanceOf(button.get())->GetObjNum(); + uint32_t appearance = AppearanceOf(button.get())->GetObjNum(); const std::string alone = StreamBytes(AppearanceOf(button.get()).Get()); ASSERT_NE(std::string::npos, alone.find("BT\n")); - for (const auto& layout : kCaptionAndIconLayouts) { - SCOPED_TRACE(layout.name); - ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), layout.layout)); + // Drawn again, the button has a new stream that holds the same, and the + // one it had is untouched. + auto expect_drawn_alone_again = [&]() { + const std::string before = ObjectGraph(pdf())[appearance]; ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); - EXPECT_EQ(appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_NE(appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_EQ(before, ObjectGraph(pdf())[appearance]); EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); EXPECT_EQ(0u, DrawnIconNumber(button.get())); + appearance = AppearanceOf(button.get())->GetObjNum(); + }; + for (const auto& layout : kCaptionAndIconLayouts) { + SCOPED_TRACE(layout.name); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), layout.layout)); + expect_drawn_alone_again(); } const unsigned int drawing = AddDrawing(doc_.get()); ASSERT_TRUE(EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_I, drawing)); ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 0)); - ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); - EXPECT_EQ(appearance, AppearanceOf(button.get())->GetObjNum()); - EXPECT_EQ(alone, StreamBytes(AppearanceOf(button.get()).Get())); - EXPECT_EQ(0u, DrawnIconNumber(button.get())); + expect_drawn_alone_again(); EXPECT_EQ(drawing, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); } @@ -3284,3 +3295,401 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, AButtonIsSharedAsTheFormFillerSharesIt) { } } } + +// A button whose appearance was its icon itself, as another app may write +// one, has the icon cleared and is drawn again, in every layout. /MK no +// longer names the picture, but another button still shows it: it is not +// written into. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AClearedIconThatWasTheAppearanceIsNotWrittenInto) { + const unsigned int picture = AddDrawing(doc_.get()); + ASSERT_NE(0u, picture); + const std::string picture_before = ObjectGraph(pdf())[picture]; + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), layout + 1, 400, 400)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation cleared = + NewButton({10, 60, 130, 10}, 0.0f, page.get()); + ScopedFPDFAnnotation other = + NewButton({140, 60, 260, 10}, 0.0f, page.get()); + ASSERT_TRUE(cleared); + ASSERT_TRUE(other); + SetIcon(other.get(), picture); + ASSERT_TRUE(EPDFAnnot_SetMKText(cleared.get(), EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance( + cleared.get(), FPDF_FONT_HELVETICA, 12, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_SetMKIcon(cleared.get(), EPDF_MK_ICON_I, picture)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(cleared.get(), layout)); + MutableDictOf(cleared.get()) + ->GetOrCreateDictFor("AP") + ->SetNewFor("N", pdf(), picture); + + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(cleared.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(cleared.get())); + + EXPECT_NE(picture, AppearanceOf(cleared.get())->GetObjNum()); + EXPECT_EQ(picture_before, ObjectGraph(pdf())[picture]); + // The other button's picture is 100 by 50, in the middle of its box. + const Shown shown(page.get()); + EXPECT_EQ(kRed, shown.At(170, 35)); + EXPECT_EQ(kBlue, shown.At(230, 35)); + } +} + +// A picture a button no longer names is in no file saved in full once the +// button is drawn again, whichever layout drew it: the new appearance names +// no icon, and nothing reaches the appearance that did. +TEST_F(EPDFWidgetDrawingEmbedderTest, AClearedPictureIsInNoFullSave) { + ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); + ASSERT_TRUE(button); + const unsigned int picture = AddDrawing(doc_.get()); + ASSERT_NE(0u, picture); + for (int layout = 1; layout <= 6; ++layout) { + SCOPED_TRACE(layout); + SetCaptionAndIcon(button.get(), picture, layout); + ASSERT_EQ(picture, DrawnIconNumber(button.get())); + ASSERT_EQ(1, StreamsHolding(SavedBytes(doc_.get()), kPictureMark)); + + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_TRUE(DrawingsOf(doc_.get()).empty()); + EXPECT_EQ(0, StreamsHolding(SavedBytes(doc_.get()), kPictureMark)); + } +} + +// Two widgets may name one /MK. Every setter writes the widget's own: the +// widget takes a copy first, with the same entries, and the other widget +// keeps what it had. A call that changes nothing copies nothing. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AWidgetKeepsAnMKItSharesWhenTheOtherIsWritten) { + const unsigned int old_picture = AddDrawing(doc_.get()); + const unsigned int new_picture = + AddDrawing(doc_.get(), Source::kVector, kGreen, kYellow); + ASSERT_NE(0u, old_picture); + ASSERT_NE(0u, new_picture); + struct Write { + const char* name; + // The /MK key it writes. + const char* key; + std::function write; + }; + const Write kWrites[] = { + {"EPDFAnnot_SetMKIcon", "I", + [&](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKIcon(widget, EPDF_MK_ICON_I, new_picture); + }}, + {"EPDFAnnot_ClearMKIcon", "I", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_ClearMKIcon(widget, EPDF_MK_ICON_I); + }}, + {"EPDFAnnot_SetMKText", "CA", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKText(widget, EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Stop").get()); + }}, + {"EPDFAnnot_SetMKText, none", "CA", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKText(widget, EPDF_MK_TEXT_CA, nullptr); + }}, + {"EPDFAnnot_SetMKTextPosition", "TP", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKTextPosition(widget, 4); + }}, + {"EPDFAnnot_SetMKRotation", "R", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKRotation(widget, 90); + }}, + {"EPDFAnnot_SetMKColor", "BC", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKColor(widget, EPDF_MK_COLOR_BC, 0, 255, 0); + }}, + {"EPDFAnnot_ClearMKColor", "BC", + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_ClearMKColor(widget, EPDF_MK_COLOR_BC); + }}, + }; + int page_index = 0; + for (const Write& write : kWrites) { + SCOPED_TRACE(write.name); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation first = NewButton({10, 60, 130, 10}, 0.0f, page.get()); + ScopedFPDFAnnotation second = + NewButton({140, 60, 260, 10}, 0.0f, page.get()); + ASSERT_TRUE(first); + ASSERT_TRUE(second); + RetainPtr shared = pdf()->NewIndirect(); + shared->SetNewFor("I", pdf(), old_picture); + shared->SetNewFor("CA", "Go"); + shared->SetNewFor("TP", 2); + RetainPtr red = shared->SetNewFor("BC"); + red->AppendNew(1); + red->AppendNew(0); + red->AppendNew(0); + for (FPDF_ANNOTATION widget : {first.get(), second.get()}) { + MutableDictOf(widget)->SetNewFor("MK", pdf(), + shared->GetObjNum()); + } + const std::string shared_before = ObjectGraph(pdf())[shared->GetObjNum()]; + + // A call that changes nothing leaves the widget naming the shared one. + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(first.get(), 2)); + EXPECT_TRUE(DictOf(first.get())->GetObjectFor("MK")->IsReference()); + + ASSERT_TRUE(write.write(first.get())); + + // The shared dictionary is as it was, and the second still names it. + EXPECT_EQ(shared_before, ObjectGraph(pdf())[shared->GetObjNum()]); + RetainPtr seconds_entry = + DictOf(second.get())->GetObjectFor("MK"); + ASSERT_TRUE(seconds_entry->IsReference()); + EXPECT_EQ(shared->GetObjNum(), + seconds_entry->AsReference()->GetRefObjNum()); + EXPECT_EQ(old_picture, EPDFAnnot_GetMKIcon(second.get(), EPDF_MK_ICON_I)); + EXPECT_EQ(2, EPDFAnnot_GetMKTextPosition(second.get())); + + // The first has its own, with everything it didn't write as it was. + RetainPtr firsts_entry = + DictOf(first.get())->GetObjectFor("MK"); + ASSERT_TRUE(firsts_entry); + EXPECT_FALSE(firsts_entry->IsReference()); + RetainPtr own = + DictOf(first.get())->GetDictFor("MK"); + ASSERT_TRUE(own); + for (const char* key : {"I", "CA", "TP", "BC"}) { + if (ByteStringView(key) == ByteStringView(write.key)) { + continue; + } + SCOPED_TRACE(key); + ASSERT_TRUE(own->KeyExist(key)); + } + if (ByteStringView(write.key) != "I") { + EXPECT_EQ(old_picture, EPDFAnnot_GetMKIcon(first.get(), EPDF_MK_ICON_I)); + } + if (ByteStringView(write.key) != "CA") { + EXPECT_EQ(L"Go", own->GetUnicodeTextFor("CA")); + } + if (ByteStringView(write.key) != "TP") { + EXPECT_EQ(2, EPDFAnnot_GetMKTextPosition(first.get())); + } + } +} + +// Two widgets may name one /AP. A widget that is drawn again gets an /AP of +// its own first, and the other widget keeps the shared one and every +// appearance it names: it shows what it showed. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AWidgetKeepsAnAPItSharesWhenTheOtherIsDrawnAgain) { + const unsigned int old_picture = AddDrawing(doc_.get()); + const unsigned int new_picture = + AddDrawing(doc_.get(), Source::kVector, kGreen, kYellow); + ASSERT_NE(0u, old_picture); + ASSERT_NE(0u, new_picture); + enum class Kind { + kIconButton, + kCaptionAndIconButton, + kCaptionButton, + kCheckBox, + kSignatureBox + }; + struct Case { + const char* name; + Kind kind; + }; + const Case kCases[] = { + {"a button that shows its icon alone", Kind::kIconButton}, + {"a button with a caption and an icon", Kind::kCaptionAndIconButton}, + {"a button with a caption", Kind::kCaptionButton}, + {"a check box", Kind::kCheckBox}, + {"a signature box", Kind::kSignatureBox}, + }; + // The objects a dictionary names, and those the dictionaries in it name. + std::function*)> named = + [&](const CPDF_Dictionary* dict, std::vector* numbers) { + CPDF_DictionaryLocker locker(dict); + for (const auto& entry : locker) { + if (const CPDF_Reference* reference = entry.second->AsReference()) { + numbers->push_back(reference->GetRefObjNum()); + } else if (const CPDF_Dictionary* inner = + entry.second->AsDictionary()) { + named(inner, numbers); + } + } + }; + int page_index = 0; + for (const Case& c : kCases) { + SCOPED_TRACE(c.name); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + auto make = [&](const FS_RECTF& rect) { + switch (c.kind) { + case Kind::kCheckBox: + return NewWidget(doc_.get(), + NewField(doc_.get(), EPDF_FORMFIELD_FAMILY_CHECKBOX), + page.get(), rect, {}, "Yes"); + case Kind::kSignatureBox: + return NewSignatureWidget(rect, page.get()); + default: + return NewButton(rect, 0.0f, page.get()); + } + }; + // As the widget stands first, and then changed so that it draws + // something else. + auto draw = [&](FPDF_ANNOTATION widget, bool changed) { + switch (c.kind) { + case Kind::kIconButton: + SetIcon(widget, changed ? new_picture : old_picture); + return; + case Kind::kCaptionAndIconButton: + SetCaptionAndIcon(widget, changed ? new_picture : old_picture, + kCaptionBelow); + return; + case Kind::kCaptionButton: + ASSERT_TRUE(EPDFAnnot_SetMKText( + widget, EPDF_MK_TEXT_CA, + GetFPDFWideString(changed ? L"Stop here" : L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance( + widget, FPDF_FONT_HELVETICA, 12, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget)); + return; + case Kind::kCheckBox: + case Kind::kSignatureBox: + ASSERT_TRUE(EPDFAnnot_SetMKColor(widget, EPDF_MK_COLOR_BG, + changed ? 0 : 255, 255, 0)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(widget)); + return; + } + }; + ScopedFPDFAnnotation first = make({10, 60, 130, 10}); + ScopedFPDFAnnotation second = make({140, 60, 260, 10}); + ASSERT_TRUE(first); + ASSERT_TRUE(second); + draw(first.get(), /*changed=*/false); + draw(second.get(), /*changed=*/false); + + // The second's /AP becomes an object of its own, and both name it. + RetainPtr seconds_own = + DictOf(second.get())->GetDictFor("AP"); + ASSERT_TRUE(seconds_own); + const uint32_t shared = pdf()->AddIndirectObject(seconds_own->Clone()); + ASSERT_NE(0u, shared); + for (FPDF_ANNOTATION widget : {first.get(), second.get()}) { + MutableDictOf(widget)->SetNewFor("AP", pdf(), shared); + } + std::vector kept = {shared}; + named(ToDictionary(pdf()->GetOrParseIndirectObject(shared)).Get(), &kept); + ASSERT_GT(kept.size(), 1u); + std::map before = ObjectGraph(pdf()); + const uint32_t shown_before = Shown(page.get()).At(170, 35); + + draw(first.get(), /*changed=*/true); + + std::map after = ObjectGraph(pdf()); + for (uint32_t number : kept) { + SCOPED_TRACE(number); + EXPECT_EQ(before[number], after[number]); + } + RetainPtr seconds_entry = + DictOf(second.get())->GetObjectFor("AP"); + ASSERT_TRUE(seconds_entry->IsReference()); + EXPECT_EQ(shared, seconds_entry->AsReference()->GetRefObjNum()); + EXPECT_EQ(shown_before, Shown(page.get()).At(170, 35)); + // The first has its own, which names its new appearance. + RetainPtr firsts_entry = + DictOf(first.get())->GetObjectFor("AP"); + ASSERT_TRUE(firsts_entry); + EXPECT_FALSE(firsts_entry->IsReference()); + std::vector firsts; + named(DictOf(first.get())->GetDictFor("AP").Get(), &firsts); + ASSERT_FALSE(firsts.empty()); + for (uint32_t number : firsts) { + EXPECT_FALSE(before.contains(number)) << number; + } + } +} + +// On a layer, drawing a button again copies nothing into the layer that it +// leaves behind: not the appearance the button had, and not a picture, even +// when the appearance was the picture itself. In every layout. +TEST_F(EPDFWidgetDrawingEmbedderTest, + ARedrawnButtonCopiesNoOldAppearanceIntoALayer) { + for (bool appearance_is_the_icon : {false, true}) { + for (int layout = 0; layout <= 6; ++layout) { + SCOPED_TRACE(appearance_is_the_icon ? "/AP /N is the icon" + : "/AP /N is a stream of its own"); + SCOPED_TRACE(layout); + std::string bytes = ForeignButtonsPdf(); + if (appearance_is_the_icon) { + // Of the same length, so the file's offsets hold. + const std::string was = "/N " + std::to_string(14 + layout) + " 0 R >>"; + const size_t at = bytes.find(was); + ASSERT_NE(std::string::npos, at); + bytes.replace(at, was.size(), "/N 6 0 R >>"); + } + embedpdf_test::LayerFixture foreign; + ASSERT_TRUE(foreign.OpenBytes({bytes.begin(), bytes.end()})); + auto* layer = static_cast( + CPDFDocumentFromFPDFDocument(foreign.layer)); + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), layout)); + ASSERT_TRUE(button); + const uint32_t old_appearance = AppearanceOf(button.get())->GetObjNum(); + ASSERT_EQ(appearance_is_the_icon, old_appearance == kForeignIcon); + ASSERT_FALSE(layer->FindPromotedObject(old_appearance)); + ASSERT_FALSE(layer->FindPromotedObject(kForeignIcon)); + + ASSERT_TRUE(EPDFLayer_BeginTransaction(foreign.layer)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + ASSERT_TRUE(EPDFLayer_CommitTransaction(foreign.layer)); + + EXPECT_NE(old_appearance, AppearanceOf(button.get())->GetObjNum()); + EXPECT_FALSE(layer->FindPromotedObject(old_appearance)); + EXPECT_FALSE(layer->FindPromotedObject(kForeignIcon)); + } + } +} + +// An icon is a form XObject. A /MK entry that names anything else, an image +// say, is no icon: it is not read as one, not listed, not drawn, and no call +// sets one. The entry can still be removed. +TEST_F(EPDFWidgetDrawingEmbedderTest, AnIconEntryThatNamesNoFormIsNoIcon) { + ScopedFPDFAnnotation button = NewButton({100, 254, 304, 150}, 2.0f); + ASSERT_TRUE(button); + RetainPtr image = + pdf()->NewIndirect(pdfium::MakeRetain()); + RetainPtr image_dict = image->GetMutableDict(); + image_dict->SetNewFor("Type", "XObject"); + image_dict->SetNewFor("Subtype", "Image"); + MutableDictOf(button.get()) + ->GetOrCreateDictFor("MK") + ->SetNewFor("I", pdf(), image->GetObjNum()); + + EXPECT_EQ(0u, EPDFAnnot_GetMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(EPDFAnnot_HasMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_TRUE(DrawingsOf(doc_.get()).empty()); + EXPECT_FALSE( + EPDFAnnot_SetMKIcon(button.get(), EPDF_MK_ICON_RI, image->GetObjNum())); + + // Where the icon shows alone the button is its box, and beside a caption + // the caption is drawn alone. + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), 1)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_FALSE(DrawnIconOf(button.get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + ASSERT_TRUE(EPDFAnnot_SetMKText(button.get(), EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get())); + ASSERT_TRUE(EPDFAnnot_SetDefaultAppearance(button.get(), FPDF_FONT_HELVETICA, + 12, 0, 0, 0)); + ASSERT_TRUE(EPDFAnnot_SetMKTextPosition(button.get(), kCaptionBelow)); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(button.get())); + EXPECT_FALSE(DrawnIconOf(button.get())); + EXPECT_EQ(0u, DrawnIconNumber(button.get())); + EXPECT_TRUE(DrawnCaptionOf(button.get())); + + ASSERT_TRUE(EPDFAnnot_ClearMKIcon(button.get(), EPDF_MK_ICON_I)); + EXPECT_FALSE(DictOf(button.get())->GetDictFor("MK")->KeyExist("I")); +} diff --git a/public/epdf_annot.h b/public/epdf_annot.h index bf685df430..9776fce088 100644 --- a/public/epdf_annot.h +++ b/public/epdf_annot.h @@ -1563,6 +1563,10 @@ typedef enum { // Experimental EmbedPDF Extension API. // Set a color in the widget annotation's /MK dictionary. // +// Every function here that writes a /MK entry writes the annotation's own +// /MK. One that is an indirect object, which another widget may name too, is +// copied into the annotation first, so the other widget keeps what it had. +// // annot - handle to an annotation. // type - EPDF_MK_COLOR_BC (border) or EPDF_MK_COLOR_BG (background). // R,G,B - color components in 0..255. @@ -1668,22 +1672,26 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKIcon(FPDF_ANNOTATION annot, // Experimental EmbedPDF Extension API. // The form a widget's /MK names as its icon |which| (EPDF_MK_ICON_*), by // object number: a drawing EPDFDoc_ExportDrawing() exports, whoever wrote it. -// 0 without one, for another subtype and an unknown |which|. +// 0 without one, for an entry that names anything but a form XObject (an +// image, say: it is no icon, and nothing draws, lists or exports it), for +// another subtype and an unknown |which|. FPDF_EXPORT unsigned int FPDF_CALLCONV EPDFAnnot_GetMKIcon(FPDF_ANNOTATION annot, int which); // Experimental EmbedPDF Extension API. -// Remove a widget's /MK icon |which| (EPDF_MK_ICON_*). The appearance is not -// regenerated (EPDFAnnot_GenerateFormFieldAP()), and the icon is not deleted, -// as EPDFAnnot_SetMKIcon() leaves the one it replaces. Returns false -// for another subtype and an unknown |which|; true when there was no icon to +// Remove a widget's /MK entry for the icon |which| (EPDF_MK_ICON_*), +// whatever it names. The appearance is not regenerated +// (EPDFAnnot_GenerateFormFieldAP()), and the icon is not deleted, as +// EPDFAnnot_SetMKIcon() leaves the one it replaces. Returns false for +// another subtype and an unknown |which|; true when there was no entry to // remove. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_ClearMKIcon(FPDF_ANNOTATION annot, int which); // Experimental EmbedPDF Extension API. -// Whether a widget's /MK holds the icon |which| (EPDF_MK_ICON_*): a stream. -// False for another subtype and an unknown |which|. +// Whether a widget's /MK names an icon |which| (EPDF_MK_ICON_*): a form +// XObject, as EPDFAnnot_GetMKIcon() reads it. False for another subtype and +// an unknown |which|. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_HasMKIcon(FPDF_ANNOTATION annot, int which); @@ -1729,6 +1737,15 @@ FPDF_EXPORT int FPDF_CALLCONV EPDFAnnot_GetMKRotation(FPDF_ANNOTATION annot); // as a text field draws them. It replaces whatever the widget // shows, a signature drawn into it included. // +// A push button, a check box, a radio button and a box get new appearance +// streams every time. The ones they had are not written into and not +// deleted: one may be another widget's too, or a picture the widget showed, +// and a save writes only what the document still reaches. So a picture a +// button no longer names or draws is in no file saved in full afterwards. A +// text field, a combo box and a list box write into the stream they have. +// A widget whose /AP is an indirect object, which another widget may name +// too, gets a copy of its own before an appearance in it is replaced. +// // annot - handle to a widget annotation. // // Returns true on success, false for another subtype or a field type it diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 48891d2ba4..e4270a313b 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -240,7 +240,7 @@ EPDFFormEmbedderTest.ListBoxValuesAreAnArray 1 EPDFFormEmbedderTest.MalformedDefaultReadsAsUnsupported 1 EPDFFormEmbedderTest.NewFieldAppearanceIsAFormXObject 7 EPDFFormEmbedderTest.NoneShadowsAParentFieldsValue 2 -EPDFFormEmbedderTest.PushButtonIsAuthorableAndShowsItsCaption 7 +EPDFFormEmbedderTest.PushButtonIsAuthorableAndShowsItsCaption 8 EPDFFormEmbedderTest.RepairBakeClearsNeedAppearances 24 EPDFFormEmbedderTest.RepairBakesMissingAppearances 4 EPDFFormEmbedderTest.RepairCreatesAcroFormAndLinksKids 4 @@ -395,28 +395,34 @@ EPDFViewEmbedderTest.EPDFGetPageUserUnitByIndex 1 EPDFViewEmbedderTest.ImageOfAnotherDocumentDraws 4 EPDFWidgetDrawingEmbedderTest.AButtonIsSharedAsTheFormFillerSharesIt 14 EPDFWidgetDrawingEmbedderTest.AButtonWhoseCaptionCannotBeDrawnIsLeftAsItWas 5 -EPDFWidgetDrawingEmbedderTest.AButtonWithoutAnIconDrawsItsCaptionAlone 10 +EPDFWidgetDrawingEmbedderTest.AButtonWithoutAnIconDrawsItsCaptionAlone 17 EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconCanBeDrawnInMemory 3 EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconEachTakeTheirPart 16 EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconReadUprightOnATurnedPage 27 EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconShareWhatTheBorderLeaves 87 EPDFWidgetDrawingEmbedderTest.ACaptionAndAnIconTurnWithTheirButton 112 -EPDFWidgetDrawingEmbedderTest.ACaptionTooLargeForTheButtonIsDrawnAlone 15 -EPDFWidgetDrawingEmbedderTest.ACaptionWhoseFontIsMissingIsStillDrawnBesideItsIcon 17 +EPDFWidgetDrawingEmbedderTest.ACaptionTooLargeForTheButtonIsDrawnAlone 21 +EPDFWidgetDrawingEmbedderTest.ACaptionWhoseFontIsMissingIsStillDrawnBesideItsIcon 12 +EPDFWidgetDrawingEmbedderTest.AClearedIconThatWasTheAppearanceIsNotWrittenInto 70 +EPDFWidgetDrawingEmbedderTest.AClearedPictureIsInNoFullSave 22 EPDFWidgetDrawingEmbedderTest.ADrawingWithAMatrixIsPlacedByItsBox 14 EPDFWidgetDrawingEmbedderTest.AFittedCaptionTakesAThirdOfTheButton 15 -EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 19 +EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 14 EPDFWidgetDrawingEmbedderTest.AForeignIconOnlyButtonSurvivesARedraw 5 EPDFWidgetDrawingEmbedderTest.AFormWithoutPicturesListsNoDrawing 16 EPDFWidgetDrawingEmbedderTest.AMarkIsPlacedAsAPageIsFitted 196 EPDFWidgetDrawingEmbedderTest.AMarkSavedBeforeThisChangeIsADrawing 13 EPDFWidgetDrawingEmbedderTest.APushButtonWithoutAnAppearanceIsNotDrawn 3 +EPDFWidgetDrawingEmbedderTest.ARedrawnButtonCopiesNoOldAppearanceIntoALayer 3 EPDFWidgetDrawingEmbedderTest.ARefitKeepsTheDrawingShared 16 EPDFWidgetDrawingEmbedderTest.AReopenedFilePlacesItsDrawingAgain 14 EPDFWidgetDrawingEmbedderTest.AReplacedOrClearedIconIsInNoSavedFile 15 EPDFWidgetDrawingEmbedderTest.ASavedFileHoldsTheIconInBothPlaces 11 +EPDFWidgetDrawingEmbedderTest.AWidgetKeepsAnAPItSharesWhenTheOtherIsDrawnAgain 69 +EPDFWidgetDrawingEmbedderTest.AWidgetKeepsAnMKItSharesWhenTheOtherIsWritten 73 EPDFWidgetDrawingEmbedderTest.AnAppearanceThatIsTheIconIsNotWrittenInto 42 EPDFWidgetDrawingEmbedderTest.AnIconBesideACaptionIsFittedAsTheIconFitSays 107 +EPDFWidgetDrawingEmbedderTest.AnIconEntryThatNamesNoFormIsNoIcon 11 EPDFWidgetDrawingEmbedderTest.AnIconIsFittedAsTheIconFitSays 122 EPDFWidgetDrawingEmbedderTest.AnIconIsFittedInsideTheBorder 42 EPDFWidgetDrawingEmbedderTest.AnIconOnlyButtonCanBeDrawnInMemory 3 From 7f419f8550b57ff08d64bafeb30d1364b2afbbab Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Tue, 24 Nov 2026 18:19:49 +0200 Subject: [PATCH 4/5] Set an /MK colour as a new array and write nothing for no change A second review of this pull request found the two colour setters apart from the other /MK setters. EPDFAnnot_SetMKColor emptied and refilled the array its key named. That array may be an object of its own, which another /MK names too: through one /MK two widgets share, where the copy a widget gets keeps the reference, or from an /MK each. Setting one widget's border green then turned the other's green as well. The setter puts a new array of three numbers under the key now, and the array the entry named is left as it was. A colour call that changed nothing still wrote. Setting the colour a widget already has gave it a copy of a shared /MK, and both setters took the annotation for writing before they looked, which copies it into a layer. Both look through the read-only dictionary first now, as the text, text position, rotation and icon setters do: setting the colour the entry already is, and removing one that is not there, write nothing. The entry is that colour when it is three numbers that read as it; four numbers are another colour space's and are set anew. --- docs/embedpdf/fork-ledger.md | 6 +- fpdfsdk/epdf_annot.cpp | 58 +++++--- fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 148 +++++++++++++++++++ public/epdf_annot.h | 8 +- testing/embedpdf_layer_mode_writers.txt | 2 + 5 files changed, 199 insertions(+), 23 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 21b8d409ca..9bdbd5d0db 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -356,8 +356,10 @@ a new stream on every redraw, as a check box's are: the stream it had may be ano a picture the button showed, so it is never written into, and the icon its resources named goes with it. Before a form drawer replaces an appearance, a widget whose `/AP` is an indirect object gets a copy of its own (`OwnAppearanceDict`), and `epdf_annot.cpp` does the same for `/MK` before -a setter writes it (`OwnMKForWriting`): another widget may name the same dictionary. An icon is a -form XObject everywhere (`ButtonIconOf`, `MKIconForm`). +a setter writes it (`OwnMKForWriting`): another widget may name the same dictionary. A colour is +set as a new array, never written into the one the entry named, which may be an object another +`/MK` names too. An `/MK` setter that changes nothing writes nothing. An icon is a form XObject +everywhere (`ButtonIconOf`, `MKIconForm`). **Guarded by.** `check-fork-tags.py` fails when upstream's files differ from upstream (`fork-pristine.txt`); `upstream-preview.py` lists new upstream callers of `CPDF_GenerateAP::`, and upstream's commits to its generator. `epdf_widget_drawing_embeddertest.cpp` for a button's icon; diff --git a/fpdfsdk/epdf_annot.cpp b/fpdfsdk/epdf_annot.cpp index 7947cff327..d67eb326dd 100644 --- a/fpdfsdk/epdf_annot.cpp +++ b/fpdfsdk/epdf_annot.cpp @@ -4435,29 +4435,45 @@ static ByteString GetMKColorKey(EPDF_MK_COLORTYPE type) { return "BC"; } +// A component of a /MK colour as the API reads it, 0 to 255. +static unsigned int MKColorComponent(const CPDF_Array* color, size_t index) { + return static_cast(color->GetFloatAt(index) * 255.f + 0.5f); +} + FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKColor(FPDF_ANNOTATION annot, EPDF_MK_COLORTYPE type, unsigned int R, unsigned int G, unsigned int B) { ScopedFPDFAnnotationView annot_view(annot); - RetainPtr pAnnotDict = - GetMutableAnnotDictFromFPDFAnnotation(annot); - if (!pAnnotDict || R > 255 || G > 255 || B > 255) { + const CPDF_Dictionary* annot_dict = GetAnnotDictFromFPDFAnnotation(annot); + if (!annot_dict || R > 255 || G > 255 || B > 255) { return false; } - RetainPtr pMK = - OwnMKForWriting(annot, pAnnotDict.Get(), /*create=*/true); - ByteString key = GetMKColorKey(type); + // Nothing to change: no write. The entry is that colour already when it is + // three numbers that read as it. + const ByteString key = GetMKColorKey(type); + RetainPtr mk = annot_dict->GetDictFor("MK"); + RetainPtr color = + mk ? mk->GetArrayFor(key.AsStringView()) : nullptr; + if (color && color->size() == 3 && MKColorComponent(color.Get(), 0) == R && + MKColorComponent(color.Get(), 1) == G && + MKColorComponent(color.Get(), 2) == B) { + return true; + } - RetainPtr pColor = pMK->GetMutableArrayFor(key.AsStringView()); - if (pColor) { - pColor->Clear(); - } else { - pColor = pMK->SetNewFor(key); + RetainPtr pAnnotDict = + GetMutableAnnotDictFromFPDFAnnotation(annot); + if (!pAnnotDict) { + return false; } + // A new array under the key. The one the entry named is not written into: + // it may be an object of its own, which another /MK names too. + RetainPtr pColor = + OwnMKForWriting(annot, pAnnotDict.Get(), /*create=*/true) + ->SetNewFor(key); pColor->AppendNew(R / 255.f); pColor->AppendNew(G / 255.f); pColor->AppendNew(B / 255.f); @@ -4491,9 +4507,9 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_GetMKColor(FPDF_ANNOTATION annot, return false; } - *R = static_cast(pColor->GetFloatAt(0) * 255.f + 0.5f); - *G = static_cast(pColor->GetFloatAt(1) * 255.f + 0.5f); - *B = static_cast(pColor->GetFloatAt(2) * 255.f + 0.5f); + *R = MKColorComponent(pColor.Get(), 0); + *G = MKColorComponent(pColor.Get(), 1); + *B = MKColorComponent(pColor.Get(), 2); return true; } @@ -4501,19 +4517,23 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_GetMKColor(FPDF_ANNOTATION annot, FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_ClearMKColor(FPDF_ANNOTATION annot, EPDF_MK_COLORTYPE type) { ScopedFPDFAnnotationView annot_view(annot); - RetainPtr pAnnotDict = - GetMutableAnnotDictFromFPDFAnnotation(annot); - if (!pAnnotDict) { + const CPDF_Dictionary* annot_dict = GetAnnotDictFromFPDFAnnotation(annot); + if (!annot_dict) { return false; } // Nothing to remove: no write. - ByteString key = GetMKColorKey(type); - RetainPtr mk = pAnnotDict->GetDictFor("MK"); + const ByteString key = GetMKColorKey(type); + RetainPtr mk = annot_dict->GetDictFor("MK"); if (!mk || !mk->KeyExist(key.AsStringView())) { return true; } + RetainPtr pAnnotDict = + GetMutableAnnotDictFromFPDFAnnotation(annot); + if (!pAnnotDict) { + return false; + } RetainPtr pMK = OwnMKForWriting(annot, pAnnotDict.Get(), /*create=*/false); if (pMK) { diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp index 43d9dce1ac..7bb4b468da 100644 --- a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -3478,6 +3478,154 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, } } +// A /MK colour may be an array that is an object of its own, which another +// /MK names too: through one /MK that two widgets share, or from an /MK +// each. A colour one widget sets goes into a new array of its own, and the +// other widget keeps its colour. (The setter wrote into the array it found.) +TEST_F(EPDFWidgetDrawingEmbedderTest, AColourArrayOfItsOwnIsNotWrittenInto) { + int page_index = 0; + for (bool one_mk : {true, false}) { + SCOPED_TRACE(one_mk ? "one /MK for both widgets" : "an /MK each"); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation first = NewButton({10, 60, 130, 10}, 2.0f, page.get()); + ScopedFPDFAnnotation second = + NewButton({140, 60, 260, 10}, 2.0f, page.get()); + ASSERT_TRUE(first); + ASSERT_TRUE(second); + RetainPtr red = pdf()->NewIndirect(); + red->AppendNew(1); + red->AppendNew(0); + red->AppendNew(0); + if (one_mk) { + RetainPtr shared = pdf()->NewIndirect(); + shared->SetNewFor("BC", pdf(), red->GetObjNum()); + for (FPDF_ANNOTATION widget : {first.get(), second.get()}) { + MutableDictOf(widget)->SetNewFor("MK", pdf(), + shared->GetObjNum()); + } + } else { + for (FPDF_ANNOTATION widget : {first.get(), second.get()}) { + MutableDictOf(widget) + ->SetNewFor("MK") + ->SetNewFor("BC", pdf(), red->GetObjNum()); + } + } + const std::string red_before = ObjectGraph(pdf())[red->GetObjNum()]; + + ASSERT_TRUE(EPDFAnnot_SetMKColor(first.get(), EPDF_MK_COLOR_BC, 0, 255, 0)); + + // The array is as it was, and the second widget is red, drawn again too. + EXPECT_EQ(red_before, ObjectGraph(pdf())[red->GetObjNum()]); + unsigned int r = 9; + unsigned int g = 9; + unsigned int b = 9; + ASSERT_TRUE( + EPDFAnnot_GetMKColor(second.get(), EPDF_MK_COLOR_BC, &r, &g, &b)); + EXPECT_EQ(255u, r); + EXPECT_EQ(0u, g); + EXPECT_EQ(0u, b); + ASSERT_TRUE(EPDFAnnot_GenerateFormFieldAP(second.get())); + EXPECT_NE( + std::string::npos, + StreamBytes(AppearanceOf(second.get()).Get()).find("1 0 0 RG")); + + // The first is green, in an array of its own. + ASSERT_TRUE( + EPDFAnnot_GetMKColor(first.get(), EPDF_MK_COLOR_BC, &r, &g, &b)); + EXPECT_EQ(0u, r); + EXPECT_EQ(255u, g); + EXPECT_EQ(0u, b); + RetainPtr own = + DictOf(first.get())->GetDictFor("MK"); + ASSERT_TRUE(own); + RetainPtr entry = own->GetObjectFor("BC"); + ASSERT_TRUE(entry); + EXPECT_FALSE(entry->IsReference()); + } +} + +// A colour call that changes nothing writes nothing, as the other /MK +// setters: the colour a widget already has, set again, and a colour it does +// not have, removed. A widget that names a shared /MK goes on naming it, and +// on a layer nothing is copied into the layer. A colour is the one asked for +// only as three numbers: four that begin with them are set anew. +TEST_F(EPDFWidgetDrawingEmbedderTest, + AColourCallThatChangesNothingWritesNothing) { + { + ScopedFPDFAnnotation first = NewButton({10, 60, 130, 10}); + ScopedFPDFAnnotation second = NewButton({140, 60, 260, 10}); + ASSERT_TRUE(first); + ASSERT_TRUE(second); + RetainPtr shared = pdf()->NewIndirect(); + RetainPtr red = shared->SetNewFor("BC"); + red->AppendNew(1); + red->AppendNew(0); + red->AppendNew(0); + for (FPDF_ANNOTATION widget : {first.get(), second.get()}) { + MutableDictOf(widget)->SetNewFor("MK", pdf(), + shared->GetObjNum()); + } + const std::string shared_before = ObjectGraph(pdf())[shared->GetObjNum()]; + + ASSERT_TRUE(EPDFAnnot_SetMKColor(first.get(), EPDF_MK_COLOR_BC, 255, 0, 0)); + EXPECT_TRUE(DictOf(first.get())->GetObjectFor("MK")->IsReference()); + ASSERT_TRUE(EPDFAnnot_ClearMKColor(first.get(), EPDF_MK_COLOR_BG)); + EXPECT_TRUE(DictOf(first.get())->GetObjectFor("MK")->IsReference()); + EXPECT_EQ(shared_before, ObjectGraph(pdf())[shared->GetObjNum()]); + + // Four numbers are another colour space's: red is set as three. + red->AppendNew(0); + ASSERT_TRUE(EPDFAnnot_SetMKColor(first.get(), EPDF_MK_COLOR_BC, 255, 0, 0)); + RetainPtr own = + DictOf(first.get())->GetDictFor("MK"); + ASSERT_TRUE(own); + EXPECT_FALSE(DictOf(first.get())->GetObjectFor("MK")->IsReference()); + RetainPtr set = own->GetArrayFor("BC"); + ASSERT_TRUE(set); + EXPECT_EQ(3u, set->size()); + EXPECT_EQ(4u, red->size()); + } + { + // A file's own widget, on a layer. Its third button is given a red + // border colour, in a text of the same length, so the offsets hold. + std::string bytes = ForeignButtonsPdf(); + const std::string was = + "/TP 2 /CA (Go) /IF << /SW /A /S /P /A [0.5 0.5] >>"; + const std::string now = + "/TP 2 /CA (Go) /BC [1 0 0] /IF << /SW /A /S /P >> "; + ASSERT_EQ(was.size(), now.size()); + const size_t at = bytes.find(was); + ASSERT_NE(std::string::npos, at); + bytes.replace(at, was.size(), now); + embedpdf_test::LayerFixture foreign; + ASSERT_TRUE(foreign.OpenBytes({bytes.begin(), bytes.end()})); + ScopedFPDFPage page(FPDF_LoadPage(foreign.layer, 0)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button(FPDFPage_GetAnnot(page.get(), 2)); + ASSERT_TRUE(button); + unsigned int r = 9; + unsigned int g = 9; + unsigned int b = 9; + ASSERT_TRUE( + EPDFAnnot_GetMKColor(button.get(), EPDF_MK_COLOR_BC, &r, &g, &b)); + ASSERT_EQ(255u, r); + ASSERT_EQ(0u, g); + ASSERT_EQ(0u, b); + ASSERT_FALSE( + EPDFAnnot_GetMKColor(button.get(), EPDF_MK_COLOR_BG, &r, &g, &b)); + ASSERT_EQ(0u, EPDFLayer_GetPromotedObjectCount(foreign.layer)); + + ASSERT_TRUE(EPDFLayer_BeginTransaction(foreign.layer)); + ASSERT_TRUE( + EPDFAnnot_SetMKColor(button.get(), EPDF_MK_COLOR_BC, 255, 0, 0)); + ASSERT_TRUE(EPDFAnnot_ClearMKColor(button.get(), EPDF_MK_COLOR_BG)); + ASSERT_TRUE(EPDFLayer_CommitTransaction(foreign.layer)); + + EXPECT_EQ(0u, EPDFLayer_GetPromotedObjectCount(foreign.layer)); + } +} + // Two widgets may name one /AP. A widget that is drawn again gets an /AP of // its own first, and the other widget keeps the shared one and every // appearance it names: it shows what it showed. diff --git a/public/epdf_annot.h b/public/epdf_annot.h index 9776fce088..1e66057b02 100644 --- a/public/epdf_annot.h +++ b/public/epdf_annot.h @@ -1561,7 +1561,10 @@ typedef enum { } EPDF_MK_COLORTYPE; // Experimental EmbedPDF Extension API. -// Set a color in the widget annotation's /MK dictionary. +// Set a color in the widget annotation's /MK dictionary: a new array of three +// numbers under its key. The array the entry named is not written into, since +// it may be an object of its own, which another /MK names too. Setting the +// color the entry already is (three numbers that read as it) writes nothing. // // Every function here that writes a /MK entry writes the annotation's own // /MK. One that is an indirect object, which another widget may name too, is @@ -1593,7 +1596,8 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_GetMKColor(FPDF_ANNOTATION annot, unsigned int* B); // Experimental EmbedPDF Extension API. -// Remove a color from the widget annotation's /MK dictionary. +// Remove a color from the widget annotation's /MK dictionary. Removing one +// that is not there writes nothing. // // annot - handle to an annotation. // type - EPDF_MK_COLOR_BC (border) or EPDF_MK_COLOR_BG (background). diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index e4270a313b..4accaef423 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -405,6 +405,8 @@ EPDFWidgetDrawingEmbedderTest.ACaptionTooLargeForTheButtonIsDrawnAlone 21 EPDFWidgetDrawingEmbedderTest.ACaptionWhoseFontIsMissingIsStillDrawnBesideItsIcon 12 EPDFWidgetDrawingEmbedderTest.AClearedIconThatWasTheAppearanceIsNotWrittenInto 70 EPDFWidgetDrawingEmbedderTest.AClearedPictureIsInNoFullSave 22 +EPDFWidgetDrawingEmbedderTest.AColourArrayOfItsOwnIsNotWrittenInto 24 +EPDFWidgetDrawingEmbedderTest.AColourCallThatChangesNothingWritesNothing 12 EPDFWidgetDrawingEmbedderTest.ADrawingWithAMatrixIsPlacedByItsBox 14 EPDFWidgetDrawingEmbedderTest.AFittedCaptionTakesAThirdOfTheButton 15 EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 14 From 464fdb1c7fcf58ed1600174e394c03f3380a80d4 Mon Sep 17 00:00:00 2001 From: Bob Singor Date: Tue, 24 Nov 2026 20:50:53 +0200 Subject: [PATCH 5/5] Skip an /MK write only when the stored entry is well formed A third review of this pull request found that the colour setter's no-change check took a malformed entry for the value asked for. With /BC [1 /None 0], setting red returned success and left /None in the file: the name reads as 0. Before the check, the setter always wrote the array anew. The same held for the other /MK setters that look before they write. A caption stored as a name read as its text and stayed a name. A text position or a rotation stored as a real read as the integer it is cut down to and stayed a real. A setter writes nothing now only when the entry is well formed and reads as the value: three numbers from 0 to 1 for a colour (IsMKColor), a string for a caption, an integer for a text position and a rotation. A malformed entry that reads the same is written anew. --- docs/embedpdf/fork-ledger.md | 4 +- fpdfsdk/epdf_annot.cpp | 48 +++++++-- fpdfsdk/epdf_widget_drawing_embeddertest.cpp | 102 +++++++++++++++++++ public/epdf_annot.h | 6 +- testing/embedpdf_layer_mode_writers.txt | 1 + 5 files changed, 151 insertions(+), 10 deletions(-) diff --git a/docs/embedpdf/fork-ledger.md b/docs/embedpdf/fork-ledger.md index 9bdbd5d0db..2d84d9dfab 100644 --- a/docs/embedpdf/fork-ledger.md +++ b/docs/embedpdf/fork-ledger.md @@ -358,7 +358,9 @@ with it. Before a form drawer replaces an appearance, a widget whose `/AP` is an gets a copy of its own (`OwnAppearanceDict`), and `epdf_annot.cpp` does the same for `/MK` before a setter writes it (`OwnMKForWriting`): another widget may name the same dictionary. A colour is set as a new array, never written into the one the entry named, which may be an object another -`/MK` names too. An `/MK` setter that changes nothing writes nothing. An icon is a form XObject +`/MK` names too. An `/MK` setter that changes nothing writes nothing, and it changes nothing only +when the entry is well formed and reads as the value (three numbers from 0 to 1, a string, an +integer): a malformed entry that reads the same is written anew. An icon is a form XObject everywhere (`ButtonIconOf`, `MKIconForm`). **Guarded by.** `check-fork-tags.py` fails when upstream's files differ from upstream (`fork-pristine.txt`); `upstream-preview.py` lists new upstream callers of `CPDF_GenerateAP::`, and diff --git a/fpdfsdk/epdf_annot.cpp b/fpdfsdk/epdf_annot.cpp index d67eb326dd..c82bade7d3 100644 --- a/fpdfsdk/epdf_annot.cpp +++ b/fpdfsdk/epdf_annot.cpp @@ -4440,6 +4440,29 @@ static unsigned int MKColorComponent(const CPDF_Array* color, size_t index) { return static_cast(color->GetFloatAt(index) * 255.f + 0.5f); } +// Whether |color| is the RGB colour |r| |g| |b| as a /MK stores one: three +// numbers from 0 to 1 that read as it. An entry that only reads as the +// colour (a name among its numbers, a number out of range, a fourth number) +// is not it, and a setter writes it anew. +static bool IsMKColor(const CPDF_Array* color, + unsigned int r, + unsigned int g, + unsigned int b) { + if (!color || color->size() != 3) { + return false; + } + auto is_component = [color](size_t index, unsigned int value) { + RetainPtr entry = color->GetDirectObjectAt(index); + if (!entry || !entry->IsNumber()) { + return false; + } + const float stored = entry->GetNumber(); + return stored >= 0.f && stored <= 1.f && + MKColorComponent(color, index) == value; + }; + return is_component(0, r) && is_component(1, g) && is_component(2, b); +} + FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKColor(FPDF_ANNOTATION annot, EPDF_MK_COLORTYPE type, unsigned int R, @@ -4451,15 +4474,12 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKColor(FPDF_ANNOTATION annot, return false; } - // Nothing to change: no write. The entry is that colour already when it is - // three numbers that read as it. + // Nothing to change: no write. const ByteString key = GetMKColorKey(type); RetainPtr mk = annot_dict->GetDictFor("MK"); RetainPtr color = mk ? mk->GetArrayFor(key.AsStringView()) : nullptr; - if (color && color->size() == 3 && MKColorComponent(color.Get(), 0) == R && - MKColorComponent(color.Get(), 1) == G && - MKColorComponent(color.Get(), 2) == B) { + if (IsMKColor(color.Get(), R, G, B)) { return true; } @@ -4579,11 +4599,17 @@ FPDF_BOOL SetMKNumber(FPDF_ANNOTATION annot, if (FPDFAnnot_GetSubtype(annot) != FPDF_ANNOT_WIDGET) { return false; } + // The entry is that number already when it is an integer equal to it. One + // that only reads as it (a real that is cut down to it) is written anew. RetainPtr mk = WidgetMKFromFPDFAnnotation(annot); const bool present = mk && mk->KeyExist(key); + RetainPtr stored = + present ? mk->GetDirectObjectFor(key) : nullptr; + const CPDF_Number* number = stored ? stored->AsNumber() : nullptr; const bool unchanged = value == absent_value ? !present - : present && mk->GetIntegerFor(key) == value; + : number && number->IsInteger() && + number->GetInteger() == value; if (unchanged) { return true; } @@ -4616,10 +4642,16 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKText(FPDF_ANNOTATION annot, if (text) { next = UNSAFE_BUFFERS(WideStringFromFPDFWideString(text)); } - // Nothing to change: no write. + // Nothing to change: no write. The entry is that text already when it is a + // string that reads as it. One that only reads as it (a name) is written + // anew. RetainPtr mk = WidgetMKFromFPDFAnnotation(annot); const bool present = mk && mk->KeyExist(key); - if (next ? present && mk->GetUnicodeTextFor(key) == *next : !present) { + RetainPtr stored = + present ? mk->GetDirectObjectFor(key) : nullptr; + const bool is_that_text = next && stored && stored->IsString() && + stored->GetUnicodeText() == *next; + if (next ? is_that_text : !present) { return true; } RetainPtr dict = diff --git a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp index 7bb4b468da..70c02e46ac 100644 --- a/fpdfsdk/epdf_widget_drawing_embeddertest.cpp +++ b/fpdfsdk/epdf_widget_drawing_embeddertest.cpp @@ -3626,6 +3626,108 @@ TEST_F(EPDFWidgetDrawingEmbedderTest, } } +// A setter writes nothing only for an entry that is well formed and reads as +// the value. A malformed one that reads the same is written anew: a colour +// with a name among its numbers or a number out of range, a caption stored +// as a name, a text position or a rotation stored as a real that is cut down +// to it. (The no-change check read each as the value and left it.) +TEST_F(EPDFWidgetDrawingEmbedderTest, + AMalformedEntryThatReadsAsTheValueIsWrittenAnew) { + struct Case { + const char* name; + const char* key; + // Stores the malformed entry in the widget's /MK. + std::function store; + // Asks for the value the entry reads as. + std::function set; + // Whether the entry is as the setter writes that value. + std::function well_formed; + }; + auto set_red = [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKColor(widget, EPDF_MK_COLOR_BC, 255, 0, 0); + }; + auto is_red = [](const CPDF_Object* entry) { + const CPDF_Array* array = entry->AsArray(); + if (!array || array->size() != 3) { + return false; + } + for (size_t i = 0; i < 3; ++i) { + RetainPtr number = array->GetDirectObjectAt(i); + if (!number || !number->IsNumber() || + number->GetNumber() != (i == 0 ? 1.0f : 0.0f)) { + return false; + } + } + return true; + }; + auto is_integer = [](int value) { + return [value](const CPDF_Object* entry) { + const CPDF_Number* number = entry->AsNumber(); + return number && number->IsInteger() && number->GetInteger() == value; + }; + }; + const Case kCases[] = { + {"a colour with a name among its numbers", "BC", + [](CPDF_Dictionary* mk) { + RetainPtr color = mk->SetNewFor("BC"); + color->AppendNew(1); + color->AppendNew("None"); + color->AppendNew(0); + }, + set_red, is_red}, + {"a colour with numbers out of range", "BC", + [](CPDF_Dictionary* mk) { + RetainPtr color = mk->SetNewFor("BC"); + color->AppendNew(1.001f); + color->AppendNew(-0.001f); + color->AppendNew(0); + }, + set_red, is_red}, + {"a caption stored as a name", "CA", + [](CPDF_Dictionary* mk) { mk->SetNewFor("CA", "Go"); }, + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKText(widget, EPDF_MK_TEXT_CA, + GetFPDFWideString(L"Go").get()); + }, + [](const CPDF_Object* entry) { + return entry->IsString() && entry->GetUnicodeText() == L"Go"; + }}, + {"a text position stored as a real", "TP", + [](CPDF_Dictionary* mk) { mk->SetNewFor("TP", 2.7f); }, + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKTextPosition(widget, 2); + }, + is_integer(2)}, + {"a rotation stored as a real", "R", + [](CPDF_Dictionary* mk) { mk->SetNewFor("R", 90.5f); }, + [](FPDF_ANNOTATION widget) { + return !!EPDFAnnot_SetMKRotation(widget, 90); + }, + is_integer(90)}, + }; + int page_index = 0; + for (const Case& c : kCases) { + SCOPED_TRACE(c.name); + ScopedFPDFPage page(FPDFPage_New(doc_.get(), ++page_index, 400, 400)); + ASSERT_TRUE(page); + ScopedFPDFAnnotation button = NewButton({10, 60, 130, 10}, 2.0f, page.get()); + ASSERT_TRUE(button); + c.store(MutableDictOf(button.get())->GetOrCreateDictFor("MK").Get()); + auto entry = [&]() { + RetainPtr mk = + DictOf(button.get())->GetDictFor("MK"); + return mk ? mk->GetDirectObjectFor(c.key) : nullptr; + }; + ASSERT_TRUE(entry()); + ASSERT_FALSE(c.well_formed(entry().Get())); + + ASSERT_TRUE(c.set(button.get())); + + ASSERT_TRUE(entry()); + EXPECT_TRUE(c.well_formed(entry().Get())); + } +} + // Two widgets may name one /AP. A widget that is drawn again gets an /AP of // its own first, and the other widget keeps the shared one and every // appearance it names: it shows what it showed. diff --git a/public/epdf_annot.h b/public/epdf_annot.h index 1e66057b02..d9d50b55c6 100644 --- a/public/epdf_annot.h +++ b/public/epdf_annot.h @@ -1564,7 +1564,9 @@ typedef enum { // Set a color in the widget annotation's /MK dictionary: a new array of three // numbers under its key. The array the entry named is not written into, since // it may be an object of its own, which another /MK names too. Setting the -// color the entry already is (three numbers that read as it) writes nothing. +// color the entry already is writes nothing: three numbers from 0 to 1 that +// read as it. An entry that only reads as it, with a name among its numbers +// say, is written anew. // // Every function here that writes a /MK entry writes the annotation's own // /MK. One that is an indirect object, which another widget may name too, is @@ -1615,6 +1617,8 @@ EPDFAnnot_ClearMKColor(FPDF_ANNOTATION annot, EPDF_MK_COLORTYPE type); // Set a widget's /MK caption |which| (EPDF_MK_TEXT_*) to |text|, or remove // it when |text| is NULL. The appearance is not regenerated // (EPDFAnnot_GenerateFormFieldAP()). Returns false for another subtype. +// Setting the caption the entry already is, a string that reads as it, +// writes nothing; an entry that only reads as it (a name) is written anew. FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV EPDFAnnot_SetMKText(FPDF_ANNOTATION annot, int which, FPDF_WIDESTRING text); diff --git a/testing/embedpdf_layer_mode_writers.txt b/testing/embedpdf_layer_mode_writers.txt index 4accaef423..1a51e0a949 100644 --- a/testing/embedpdf_layer_mode_writers.txt +++ b/testing/embedpdf_layer_mode_writers.txt @@ -412,6 +412,7 @@ EPDFWidgetDrawingEmbedderTest.AFittedCaptionTakesAThirdOfTheButton 15 EPDFWidgetDrawingEmbedderTest.AForeignButtonKeepsItsIconInEveryLayout 14 EPDFWidgetDrawingEmbedderTest.AForeignIconOnlyButtonSurvivesARedraw 5 EPDFWidgetDrawingEmbedderTest.AFormWithoutPicturesListsNoDrawing 16 +EPDFWidgetDrawingEmbedderTest.AMalformedEntryThatReadsAsTheValueIsWrittenAnew 25 EPDFWidgetDrawingEmbedderTest.AMarkIsPlacedAsAPageIsFitted 196 EPDFWidgetDrawingEmbedderTest.AMarkSavedBeforeThisChangeIsADrawing 13 EPDFWidgetDrawingEmbedderTest.APushButtonWithoutAnAppearanceIsNotDrawn 3