From 5970a47ac3cb1c0256d73f509ac73fa24aa4be42 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Thu, 11 Jan 2018 22:40:59 +0000 Subject: Add FPDFAnnot_SetAP to public API. Change-Id: I6de3e4e158a8b0276775c0915cbe53417135eec3 Reviewed-on: https://pdfium-review.googlesource.com/22570 Reviewed-by: Lei Zhang Commit-Queue: Henrique Nakashima --- fpdfsdk/fpdfannot.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'fpdfsdk/fpdfannot.cpp') diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index e40e21981e..9b0cb39574 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -747,6 +747,51 @@ FPDFAnnot_GetStringValue(FPDF_ANNOTATION annot, buffer, buflen); } +FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV +FPDFAnnot_SetAP(FPDF_ANNOTATION annot, + FPDF_ANNOT_APPEARANCEMODE appearanceMode, + FPDF_WIDESTRING value) { + if (appearanceMode < 0 || appearanceMode >= FPDF_ANNOT_APPEARANCEMODE_COUNT) + return false; + + if (!annot) + return false; + + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); + if (!pAnnotDict) + return false; + + constexpr const char* modeKeyForMode[] = {"N", "R", "D"}; + static_assert(FX_ArraySize(modeKeyForMode) == FPDF_ANNOT_APPEARANCEMODE_COUNT, + "length of modeKeyForMode should be equal to " + "FPDF_ANNOT_APPEARANCEMODE_COUNT"); + const char* modeKey = modeKeyForMode[appearanceMode]; + + CPDF_Dictionary* pApDict = pAnnotDict->GetDictFor("AP"); + + // If value is null, we're in remove mode. Otherwise, we're in add/update + // mode. + if (value) { + if (!pApDict) + pApDict = pAnnotDict->SetNewFor("AP"); + + ByteString newValue = CFXByteStringFromFPDFWideString(value); + auto pNewApStream = pdfium::MakeUnique(); + pNewApStream->SetData(newValue.raw_str(), newValue.GetLength()); + pApDict->SetFor(modeKey, std::move(pNewApStream)); + } else { + if (pApDict) { + if (appearanceMode == FPDF_ANNOT_APPEARANCEMODE_NORMAL) + pAnnotDict->RemoveFor("AP"); + else + pApDict->RemoveFor(modeKey); + } + } + + return true; +} + FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAnnot_GetAP(FPDF_ANNOTATION annot, FPDF_ANNOT_APPEARANCEMODE appearanceMode, -- cgit v1.2.3