Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 209 additions & 28 deletions core/fpdfdoc/cpdf_appearancegenerator_forms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#include <algorithm>
#include <array>
#include <cmath>
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <utility>
#include <vector>

#include "constants/annotation_common.h"
#include "constants/appearance.h"
Expand Down Expand Up @@ -540,6 +542,194 @@ RetainPtr<CPDF_Dictionary> OwnAppearanceDict(CPDF_Document* doc,
return annot_dict->GetOrCreateDictFor(pdfium::annotation::kAP);
}

// A widget's new normal appearance: the stream |objnum|, for the state
// |state| (/Off, /Yes) or for a widget without states when |state| is empty.
struct NormalAppearance {
ByteString state;
uint32_t objnum;
};

// The stream a normal appearance (an /AP's /N) holds for |state|: the stream
// it is for a widget without states, else the one under the state's name.
RetainPtr<const CPDF_Stream> NormalStreamFor(const CPDF_Object* normal,
const ByteString& state) {
if (!normal) {
return nullptr;
}
if (state.IsEmpty()) {
return pdfium::WrapRetain(normal->AsStream());
}
const CPDF_Dictionary* states =
normal->IsStream() ? nullptr : normal->AsDictionary();
return states ? states->GetStreamFor(state.AsStringView()) : nullptr;
}

// A stream that a new normal appearance replaces: the state it was for (empty
// for a widget without states), its number, and the number of the stream
// that stands for it now.
struct ReplacedStream {
ByteString state;
uint32_t replaced;
uint32_t objnum;
};

// The stream that stands for the one a reference under /R or /D names, or 0
// when it names none that was replaced. Two states may have shared one
// stream: a reference under the state |state| follows that state's.
uint32_t StreamStandingFor(const CPDF_Object* named,
const ByteString& state,
const std::vector<ReplacedStream>& replaced) {
const CPDF_Reference* reference = named ? named->AsReference() : nullptr;
if (!reference) {
return 0;
}
uint32_t first = 0;
for (const ReplacedStream& stream : replaced) {
if (stream.replaced != reference->GetRefObjNum()) {
continue;
}
if (stream.state == state) {
return stream.objnum;
}
if (first == 0) {
first = stream.objnum;
}
}
return first;
}

// Makes every reference under |key| (/R or /D) of a widget's own /AP that
// named a replaced stream name the one that stands for it: the entry itself,
// or a state of it. Each of /N, /R and /D is one stream or a dictionary of
// states, whatever the others are. A dictionary of states may be an indirect
// object, which another widget may name too, so the widget gets a copy of it
// first.
void FollowReplacedStreams(CPDF_Document* doc,
CPDF_Dictionary* ap,
const ByteString& key,
const std::vector<ReplacedStream>& replaced) {
RetainPtr<const CPDF_Object> entry = ap->GetObjectFor(key.AsStringView());
if (!entry) {
return;
}
if (const uint32_t stream =
StreamStandingFor(entry.Get(), ByteString(), replaced)) {
ap->SetNewFor<CPDF_Reference>(key, doc, stream);
return;
}
RetainPtr<const CPDF_Object> held =
ap->GetDirectObjectFor(key.AsStringView());
const CPDF_Dictionary* states =
held && !held->IsStream() ? held->AsDictionary() : nullptr;
if (!states) {
return;
}
std::map<ByteString, uint32_t> follows;
{
CPDF_DictionaryLocker locker(states);
for (const auto& [state, named] : locker) {
if (const uint32_t stream =
StreamStandingFor(named.Get(), state, replaced)) {
follows[state] = stream;
}
}
}
if (follows.empty()) {
return;
}
if (entry->IsReference()) {
ap->SetFor(key, states->CloneForHolder(doc));
}
RetainPtr<CPDF_Dictionary> own = ap->GetMutableDictFor(key.AsStringView());
for (const auto& [state, stream] : follows) {
own->SetNewFor<CPDF_Reference>(state, doc, stream);
}
}

// Gives the new stream |stream| what the stream it replaces says of any
// drawing in it, as against what it drew: the layer it shows in (/OC), how
// it is composited (/Group), and the colour spaces its device colours are
// read in (the defaults under /Resources /ColorSpace). Nothing else is
// taken: the rest of a stream's dictionary and resources is about the old
// drawing, and would keep what that drawing named in the file.
void CarryHowADrawingShows(CPDF_Document* doc,
const CPDF_Stream* replaced,
CPDF_Stream* stream) {
RetainPtr<const CPDF_Dictionary> from = replaced->GetDict();
RetainPtr<CPDF_Dictionary> to = stream->GetMutableDict();
for (const char* key : {"OC", "Group"}) {
if (RetainPtr<const CPDF_Object> said = from->GetObjectFor(key)) {
to->SetFor(key, said->CloneForHolder(doc));
}
}
RetainPtr<const CPDF_Dictionary> resources = from->GetDictFor("Resources");
RetainPtr<const CPDF_Dictionary> spaces =
resources ? resources->GetDictFor("ColorSpace") : nullptr;
if (!spaces) {
return;
}
for (const char* key : {"DefaultGray", "DefaultRGB", "DefaultCMYK"}) {
if (RetainPtr<const CPDF_Object> space = spaces->GetObjectFor(key)) {
to->GetOrCreateDictFor("Resources")
->GetOrCreateDictFor("ColorSpace")
->SetFor(key, space->CloneForHolder(doc));
}
}
}

// Names a widget's new normal appearance, in an /AP of its own
// (OwnAppearanceDict()). The streams are new and the ones they replace are
// read only, so what the old appearance said beyond its drawing is said
// again here:
// - how any drawing in it shows: a new stream takes the layer, the group
// and the default colour spaces of the stream it replaces, the one of its
// state (CarryHowADrawingShows());
// - which other appearances are the same one: a reference under /R or /D
// that named a replaced stream names the new one
// (FollowReplacedStreams()). A rollover or pressed appearance made apart
// stays as it is.
void NameNormalAppearance(CPDF_Document* doc,
CPDF_Dictionary* annot_dict,
const std::vector<NormalAppearance>& appearances) {
RetainPtr<const CPDF_Dictionary> old_ap =
annot_dict->GetDictFor(pdfium::annotation::kAP);
RetainPtr<const CPDF_Object> old_normal =
old_ap ? old_ap->GetDirectObjectFor("N") : nullptr;

std::vector<ReplacedStream> replaced;
for (const NormalAppearance& appearance : appearances) {
RetainPtr<const CPDF_Stream> old_stream =
NormalStreamFor(old_normal.Get(), appearance.state);
RetainPtr<CPDF_Stream> stream =
old_stream ? ToStream(doc->GetMutableIndirectObject(appearance.objnum))
: nullptr;
if (!stream) {
continue;
}
CarryHowADrawingShows(doc, old_stream.Get(), stream.Get());
if (old_stream->GetObjNum() != 0) {
replaced.push_back(
{appearance.state, old_stream->GetObjNum(), appearance.objnum});
}
}

RetainPtr<CPDF_Dictionary> ap = OwnAppearanceDict(doc, annot_dict);
if (!replaced.empty()) {
for (const char* key : {"R", "D"}) {
FollowReplacedStreams(doc, ap.Get(), key, replaced);
}
}

if (appearances.size() == 1 && appearances.front().state.IsEmpty()) {
ap->SetNewFor<CPDF_Reference>("N", doc, appearances.front().objnum);
return;
}
RetainPtr<CPDF_Dictionary> states = ap->SetNewFor<CPDF_Dictionary>("N");
for (const NormalAppearance& appearance : appearances) {
states->SetNewFor<CPDF_Reference>(appearance.state, doc, appearance.objnum);
}
}

// 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.
Expand Down Expand Up @@ -1057,8 +1247,8 @@ void GenerateIconButtonAP(APGenerationTarget* target,
}
const uint32_t normal_obj_num = CreateFormXObjectStream(
doc, content, dims.bbox, dims.matrix, std::move(resources));
OwnAppearanceDict(doc, target->persistent_annot_dict)
->SetNewFor<CPDF_Reference>("N", doc, normal_obj_num);
NameNormalAppearance(doc, target->persistent_annot_dict,
{{ByteString(), normal_obj_num}});
}

bool GenerateFormAPToTarget(APGenerationTarget* target,
Expand Down Expand Up @@ -1209,25 +1399,17 @@ bool GenerateFormAPToTarget(APGenerationTarget* target,
if (!resource_font_dict) {
return false; // registered /DA font without a resource: no appearance
}
// 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
// A field'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 (a
// push 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<CPDF_Stream> normal_stream;
if (type != CPDF_AppearanceGenerator::kPushButton) {
RetainPtr<CPDF_Dictionary> ap_dict =
annot_dict->GetMutableDictFor(pdfium::annotation::kAP);
normal_stream = ap_dict ? ap_dict->GetMutableStreamFor("N") : nullptr;
}
if (!normal_stream) {
normal_stream =
doc->NewIndirect<CPDF_Stream>(pdfium::MakeRetain<CPDF_Dictionary>());
OwnAppearanceDict(doc, annot_dict)
->SetNewFor<CPDF_Reference>("N", doc, normal_stream->GetObjNum());
}
// shows, which would keep that picture in the file. It also stays as it
// reads: a capture taken before the write (public/epdf_capture.h) puts the
// widget back with the appearance it named, and an import never replaces a
// stream the layer holds. The old stream is not opened for writing, so
// nothing of it is copied into a layer.
RetainPtr<CPDF_Stream> normal_stream =
doc->NewIndirect<CPDF_Stream>(pdfium::MakeRetain<CPDF_Dictionary>());
normal_stream->SetDataFromStringstreamAndRemoveFilter(&app_stream);
RetainPtr<CPDF_Dictionary> stream_dict = normal_stream->GetMutableDict();
// A form XObject, said so: a stream made here has no type yet, and
Expand All @@ -1242,6 +1424,8 @@ bool GenerateFormAPToTarget(APGenerationTarget* target,
if (drawn_icon != 0) {
NameButtonIcon(resources.Get(), doc, drawn_icon);
}
NameNormalAppearance(doc, annot_dict,
{{ByteString(), normal_stream->GetObjNum()}});
return true;
}

Expand Down Expand Up @@ -1414,8 +1598,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);
OwnAppearanceDict(doc, annot_dict)
->SetNewFor<CPDF_Reference>("N", doc, normal_obj_num);
NameNormalAppearance(doc, annot_dict, {{ByteString(), normal_obj_num}});
}

// static
Expand Down Expand Up @@ -1466,9 +1649,8 @@ void CPDF_AppearanceGenerator::GenerateCheckboxFormAP(
on_state = "Yes";
}

RetainPtr<CPDF_Dictionary> n_dict = ap_dict->SetNewFor<CPDF_Dictionary>("N");
n_dict->SetNewFor<CPDF_Reference>("Off", doc, off_obj_num);
n_dict->SetNewFor<CPDF_Reference>(on_state, doc, yes_obj_num);
NameNormalAppearance(doc, annot_dict,
{{"Off", off_obj_num}, {on_state, yes_obj_num}});

if (!annot_dict->KeyExist("AS")) {
annot_dict->SetNewFor<CPDF_Name>("AS", "Off");
Expand Down Expand Up @@ -1580,9 +1762,8 @@ void CPDF_AppearanceGenerator::GenerateRadioButtonFormAP(
}
}

RetainPtr<CPDF_Dictionary> n_dict = ap_dict->SetNewFor<CPDF_Dictionary>("N");
n_dict->SetNewFor<CPDF_Reference>("Off", doc, off_obj_num);
n_dict->SetNewFor<CPDF_Reference>(on_state, doc, yes_obj_num);
NameNormalAppearance(doc, annot_dict,
{{"Off", off_obj_num}, {on_state, yes_obj_num}});

if (!annot_dict->KeyExist("AS")) {
annot_dict->SetNewFor<CPDF_Name>("AS", "Off");
Expand Down
30 changes: 20 additions & 10 deletions docs/embedpdf/fork-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,16 +363,26 @@ 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. 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` and
`/BS` before a setter writes one (`OwnMKForWriting` and `BorderStyleDictForWrite`, both through
`OwnDictEntry`): another annotation may name the same dictionary. A `/DR` font that an appearance
written into the file names, when the file holds it inline (a dictionary inside `/DR`), gets
its object number in `CPDF_AnnotFontMap::PublishFontResources`. A failed appearance leaves it
inline. On success the form and appearance share the font so Acrobat displays the field. A colour is
middle, and reads an icon narrower than one unit as one unit wide. A field's appearance is a new
stream on every redraw, whatever kind the field is: the stream it had may be another widget's or
a picture a push button showed, so it is never written into, and the icon its resources named
goes with it. It also stays as it reads, so a capture taken before the redraw
(`public/epdf_capture.h`) brings the widget back with the appearance it named: an import never
replaces a stream the layer holds. What the old appearance said beyond its drawing is said of the
new one (`NameNormalAppearance`). A new stream takes what the stream it replaces says of any drawing
in it: its layer (`/OC`), its transparency group (`/Group`) and the default colour spaces in its
resources. It takes nothing that is about the old drawing: the rest of that stream's dictionary and
resources is left, so what it named is in no file saved in full afterwards. And a reference under
`/R` or `/D` that named a replaced stream names the new one, whether that entry is one stream or a
dictionary of states, and whatever `/N` is. A rollover or pressed appearance made apart stays.
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` and `/BS` before a setter writes one (`OwnMKForWriting`
and `BorderStyleDictForWrite`, both through `OwnDictEntry`): another annotation may name the same
dictionary. A `/DR` font that an appearance written into the file names, when the file holds it
inline (a dictionary inside `/DR`), gets its object number in
`CPDF_AnnotFontMap::PublishFontResources`. A failed appearance leaves it inline. On success the
form and appearance share the font so Acrobat displays the field. 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, 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
Expand Down
Loading