2018-06-19 13:26:41 +00:00
|
|
|
#include <qpdf/QPDFFormFieldObjectHelper.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/Pl_QPDFTokenizer.hh>
|
|
|
|
#include <qpdf/QIntC.hh>
|
2018-06-21 19:00:00 +00:00
|
|
|
#include <qpdf/QPDFAcroFormDocumentHelper.hh>
|
2019-01-04 00:09:00 +00:00
|
|
|
#include <qpdf/QPDFAnnotationObjectHelper.hh>
|
2022-04-02 21:14:10 +00:00
|
|
|
#include <qpdf/QTC.hh>
|
2019-01-04 00:09:00 +00:00
|
|
|
#include <qpdf/QUtil.hh>
|
|
|
|
#include <stdlib.h>
|
2018-06-19 13:26:41 +00:00
|
|
|
|
|
|
|
QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper(QPDFObjectHandle oh) :
|
|
|
|
QPDFObjectHelper(oh),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFormFieldObjectHelper::QPDFFormFieldObjectHelper() :
|
|
|
|
QPDFObjectHelper(QPDFObjectHandle::newNull()),
|
|
|
|
m(new Members())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isNull()
|
|
|
|
{
|
|
|
|
return this->oh.isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFFormFieldObjectHelper
|
|
|
|
QPDFFormFieldObjectHelper::getParent()
|
|
|
|
{
|
|
|
|
return this->oh.getKey("/Parent"); // may be null
|
|
|
|
}
|
|
|
|
|
2021-02-22 22:44:13 +00:00
|
|
|
QPDFFormFieldObjectHelper
|
|
|
|
QPDFFormFieldObjectHelper::getTopLevelField(bool* is_different)
|
|
|
|
{
|
|
|
|
auto top_field = this->oh;
|
|
|
|
std::set<QPDFObjGen> seen;
|
|
|
|
while (top_field.isDictionary() &&
|
2022-04-02 21:14:10 +00:00
|
|
|
(!top_field.getKey("/Parent").isNull())) {
|
2021-02-22 22:44:13 +00:00
|
|
|
top_field = top_field.getKey("/Parent");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (is_different) {
|
2021-02-22 22:44:13 +00:00
|
|
|
*is_different = true;
|
|
|
|
}
|
|
|
|
auto og = top_field.getObjGen();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (seen.count(og)) {
|
2021-02-22 22:44:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
seen.insert(og);
|
|
|
|
}
|
|
|
|
return QPDFFormFieldObjectHelper(top_field);
|
|
|
|
}
|
|
|
|
|
2021-02-26 19:47:52 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getFieldFromAcroForm(std::string const& name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle result = QPDFObjectHandle::newNull();
|
|
|
|
// Fields are supposed to be indirect, so this should work.
|
|
|
|
QPDF* q = this->oh.getOwningQPDF();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!q) {
|
2021-02-26 19:47:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
auto acroform = q->getRoot().getKey("/AcroForm");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!acroform.isDictionary()) {
|
2021-02-26 19:47:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return acroform.getKey(name);
|
|
|
|
}
|
|
|
|
|
2018-06-19 13:26:41 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getInheritableFieldValue(std::string const& name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle node = this->oh;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!node.isDictionary()) {
|
2019-06-21 21:37:33 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
|
|
|
}
|
2018-06-19 13:26:41 +00:00
|
|
|
QPDFObjectHandle result(node.getKey(name));
|
|
|
|
std::set<QPDFObjGen> seen;
|
2022-04-02 21:14:10 +00:00
|
|
|
while (result.isNull() && node.hasKey("/Parent")) {
|
2018-06-19 13:26:41 +00:00
|
|
|
seen.insert(node.getObjGen());
|
|
|
|
node = node.getKey("/Parent");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (seen.count(node.getObjGen())) {
|
2018-06-19 13:26:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = node.getKey(name);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!result.isNull()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf", "QPDFFormFieldObjectHelper non-trivial inheritance");
|
2018-06-19 13:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getInheritableFieldValueAsString(
|
|
|
|
std::string const& name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle fv = getInheritableFieldValue(name);
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fv.isString()) {
|
2018-06-19 13:26:41 +00:00
|
|
|
result = fv.getUTF8Value();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getInheritableFieldValueAsName(
|
|
|
|
std::string const& name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle fv = getInheritableFieldValue(name);
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fv.isName()) {
|
2018-06-19 13:26:41 +00:00
|
|
|
result = fv.getName();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getFieldType()
|
|
|
|
{
|
|
|
|
return getInheritableFieldValueAsName("/FT");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getFullyQualifiedName()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
QPDFObjectHandle node = this->oh;
|
|
|
|
std::set<QPDFObjGen> seen;
|
2022-04-02 21:14:10 +00:00
|
|
|
while ((!node.isNull()) && (seen.count(node.getObjGen()) == 0)) {
|
|
|
|
if (node.getKey("/T").isString()) {
|
|
|
|
if (!result.empty()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFFormFieldObjectHelper non-trivial qualified name");
|
2018-06-19 13:26:41 +00:00
|
|
|
result = "." + result;
|
|
|
|
}
|
|
|
|
result = node.getKey("/T").getUTF8Value() + result;
|
|
|
|
}
|
|
|
|
seen.insert(node.getObjGen());
|
|
|
|
node = node.getKey("/Parent");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getPartialName()
|
|
|
|
{
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->oh.getKey("/T").isString()) {
|
2018-06-19 13:26:41 +00:00
|
|
|
result = this->oh.getKey("/T").getUTF8Value();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getAlternativeName()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->oh.getKey("/TU").isString()) {
|
2018-06-19 13:26:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper TU present");
|
|
|
|
return this->oh.getKey("/TU").getUTF8Value();
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper TU absent");
|
|
|
|
return getFullyQualifiedName();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getMappingName()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->oh.getKey("/TM").isString()) {
|
2018-06-19 13:26:41 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper TM present");
|
|
|
|
return this->oh.getKey("/TM").getUTF8Value();
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper TM absent");
|
|
|
|
return getAlternativeName();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getValue()
|
|
|
|
{
|
|
|
|
return getInheritableFieldValue("/V");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getValueAsString()
|
|
|
|
{
|
|
|
|
return getInheritableFieldValueAsString("/V");
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getDefaultValue()
|
|
|
|
{
|
|
|
|
return getInheritableFieldValue("/DV");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getDefaultValueAsString()
|
|
|
|
{
|
|
|
|
return getInheritableFieldValueAsString("/DV");
|
|
|
|
}
|
|
|
|
|
2021-02-26 19:47:52 +00:00
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getDefaultResources()
|
|
|
|
{
|
|
|
|
return getFieldFromAcroForm("/DR");
|
|
|
|
}
|
|
|
|
|
2018-06-19 13:26:41 +00:00
|
|
|
std::string
|
|
|
|
QPDFFormFieldObjectHelper::getDefaultAppearance()
|
|
|
|
{
|
2021-02-26 19:47:52 +00:00
|
|
|
auto value = getInheritableFieldValue("/DA");
|
|
|
|
bool looked_in_acroform = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!value.isString()) {
|
2021-02-26 19:47:52 +00:00
|
|
|
value = getFieldFromAcroForm("/DA");
|
|
|
|
looked_in_acroform = true;
|
|
|
|
}
|
|
|
|
std::string result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (value.isString()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFFormFieldObjectHelper DA present",
|
|
|
|
looked_in_acroform ? 0 : 1);
|
2021-02-26 19:47:52 +00:00
|
|
|
result = value.getUTF8Value();
|
|
|
|
}
|
|
|
|
return result;
|
2018-06-19 13:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
QPDFFormFieldObjectHelper::getQuadding()
|
|
|
|
{
|
|
|
|
QPDFObjectHandle fv = getInheritableFieldValue("/Q");
|
2021-02-26 19:47:52 +00:00
|
|
|
bool looked_in_acroform = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!fv.isInteger()) {
|
2021-02-26 19:47:52 +00:00
|
|
|
fv = getFieldFromAcroForm("/Q");
|
|
|
|
looked_in_acroform = true;
|
|
|
|
}
|
|
|
|
int result = 0;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (fv.isInteger()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFFormFieldObjectHelper Q present",
|
|
|
|
looked_in_acroform ? 0 : 1);
|
2019-06-21 03:35:23 +00:00
|
|
|
result = QIntC::to_int(fv.getIntValue());
|
2018-06-19 13:26:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2018-06-21 19:00:00 +00:00
|
|
|
|
2019-01-02 02:27:18 +00:00
|
|
|
int
|
|
|
|
QPDFFormFieldObjectHelper::getFlags()
|
|
|
|
{
|
|
|
|
QPDFObjectHandle f = getInheritableFieldValue("/Ff");
|
2019-06-21 03:35:23 +00:00
|
|
|
return f.isInteger() ? f.getIntValueAsInt() : 0;
|
2019-01-02 02:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isText()
|
|
|
|
{
|
|
|
|
return (getFieldType() == "/Tx");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isCheckbox()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return (
|
|
|
|
(getFieldType() == "/Btn") &&
|
|
|
|
((getFlags() & (ff_btn_radio | ff_btn_pushbutton)) == 0));
|
2019-01-02 02:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isRadioButton()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return (
|
|
|
|
(getFieldType() == "/Btn") &&
|
|
|
|
((getFlags() & ff_btn_radio) == ff_btn_radio));
|
2019-01-02 02:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isPushbutton()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
return (
|
|
|
|
(getFieldType() == "/Btn") &&
|
|
|
|
((getFlags() & ff_btn_pushbutton) == ff_btn_pushbutton));
|
2019-01-02 02:27:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
QPDFFormFieldObjectHelper::isChoice()
|
|
|
|
{
|
|
|
|
return (getFieldType() == "/Ch");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string>
|
|
|
|
QPDFFormFieldObjectHelper::getChoices()
|
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!isChoice()) {
|
2019-01-02 02:27:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle opt = getInheritableFieldValue("/Opt");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (opt.isArray()) {
|
2019-06-21 03:35:23 +00:00
|
|
|
int n = opt.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < n; ++i) {
|
2019-01-02 02:27:18 +00:00
|
|
|
QPDFObjectHandle item = opt.getArrayItem(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (item.isString()) {
|
2019-01-02 02:27:18 +00:00
|
|
|
result.push_back(item.getUTF8Value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-06-21 19:00:00 +00:00
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::setFieldAttribute(
|
|
|
|
std::string const& key, QPDFObjectHandle value)
|
|
|
|
{
|
|
|
|
this->oh.replaceKey(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::setFieldAttribute(
|
|
|
|
std::string const& key, std::string const& utf8_value)
|
|
|
|
{
|
|
|
|
this->oh.replaceKey(key, QPDFObjectHandle::newUnicodeString(utf8_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFFormFieldObjectHelper::setV(QPDFObjectHandle value, bool need_appearances)
|
2018-06-21 19:00:00 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (getFieldType() == "/Btn") {
|
|
|
|
if (isCheckbox()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
bool okay = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (value.isName()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
std::string name = value.getName();
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((name == "/Yes") || (name == "/Off")) {
|
2019-01-03 22:00:56 +00:00
|
|
|
okay = true;
|
|
|
|
setCheckBoxValue((name == "/Yes"));
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!okay) {
|
2019-01-03 22:00:56 +00:00
|
|
|
this->oh.warnIfPossible(
|
|
|
|
"ignoring attempt to set a checkbox field to a"
|
|
|
|
" value of other than /Yes or /Off");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (isRadioButton()) {
|
|
|
|
if (value.isName()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
setRadioButtonValue(value);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-03 22:00:56 +00:00
|
|
|
this->oh.warnIfPossible(
|
|
|
|
"ignoring attempt to set a radio button field to"
|
|
|
|
" an object that is not a name");
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (isPushbutton()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
this->oh.warnIfPossible(
|
|
|
|
"ignoring attempt set the value of a pushbutton field");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (value.isString()) {
|
2019-01-04 00:09:00 +00:00
|
|
|
setFieldAttribute(
|
|
|
|
"/V", QPDFObjectHandle::newUnicodeString(value.getUTF8Value()));
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-04 00:09:00 +00:00
|
|
|
setFieldAttribute("/V", value);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (need_appearances) {
|
2018-06-21 19:00:00 +00:00
|
|
|
QPDF* qpdf = this->oh.getOwningQPDF();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!qpdf) {
|
2018-06-21 19:00:00 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"QPDFFormFieldObjectHelper::setV called with"
|
|
|
|
" need_appearances = true on an object that is"
|
|
|
|
" not associated with an owning QPDF");
|
|
|
|
}
|
|
|
|
QPDFAcroFormDocumentHelper(*qpdf).setNeedAppearances(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::setV(
|
|
|
|
std::string const& utf8_value, bool need_appearances)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
setV(QPDFObjectHandle::newUnicodeString(utf8_value), need_appearances);
|
2018-06-21 19:00:00 +00:00
|
|
|
}
|
2019-01-03 22:00:56 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::setRadioButtonValue(QPDFObjectHandle name)
|
|
|
|
{
|
|
|
|
// Set the value of a radio button field. This has the following
|
|
|
|
// specific behavior:
|
|
|
|
// * If this is a radio button field that has a parent that is
|
|
|
|
// also a radio button field and has no explicit /V, call itself
|
|
|
|
// on the parent
|
2019-01-05 01:10:34 +00:00
|
|
|
// * If this is a radio button field with children, set /V to the
|
2019-01-03 22:00:56 +00:00
|
|
|
// given value. Then, for each child, if the child has the
|
|
|
|
// specified value as one of its keys in the /N subdictionary of
|
|
|
|
// its /AP (i.e. its normal appearance stream dictionary), set
|
|
|
|
// /AS to name; otherwise, if /Off is a member, set /AS to /Off.
|
|
|
|
// Note that we never turn on /NeedAppearances when setting a
|
|
|
|
// radio button field.
|
|
|
|
QPDFObjectHandle parent = this->oh.getKey("/Parent");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (parent.isDictionary() && parent.getKey("/Parent").isNull()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QPDFFormFieldObjectHelper ph(parent);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (ph.isRadioButton()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
// This is most likely one of the individual buttons. Try
|
|
|
|
// calling on the parent.
|
2022-04-02 21:14:10 +00:00
|
|
|
QTC::TC(
|
|
|
|
"qpdf", "QPDFFormFieldObjectHelper set parent radio button");
|
2019-01-03 22:00:56 +00:00
|
|
|
ph.setRadioButtonValue(name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle kids = this->oh.getKey("/Kids");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!(isRadioButton() && parent.isNull() && kids.isArray())) {
|
2019-01-03 22:00:56 +00:00
|
|
|
this->oh.warnIfPossible("don't know how to set the value"
|
|
|
|
" of this field as a radio button");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setFieldAttribute("/V", name);
|
|
|
|
int nkids = kids.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < nkids; ++i) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QPDFObjectHandle kid = kids.getArrayItem(i);
|
|
|
|
QPDFObjectHandle AP = kid.getKey("/AP");
|
|
|
|
QPDFObjectHandle annot;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (AP.isNull()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
// The widget may be below. If there is more than one,
|
|
|
|
// just find the first one.
|
|
|
|
QPDFObjectHandle grandkids = kid.getKey("/Kids");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (grandkids.isArray()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
int ngrandkids = grandkids.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int j = 0; j < ngrandkids; ++j) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QPDFObjectHandle grandkid = grandkids.getArrayItem(j);
|
|
|
|
AP = grandkid.getKey("/AP");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!AP.isNull()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFFormFieldObjectHelper radio button grandkid");
|
2019-01-03 22:00:56 +00:00
|
|
|
annot = grandkid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-03 22:00:56 +00:00
|
|
|
annot = kid;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!annot.isInitialized()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QTC::TC("qpdf", "QPDFObjectHandle broken radio button");
|
|
|
|
this->oh.warnIfPossible(
|
|
|
|
"unable to set the value of this radio button");
|
|
|
|
continue;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (AP.isDictionary() && AP.getKey("/N").isDictionary() &&
|
|
|
|
AP.getKey("/N").hasKey(name.getName())) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper turn on radio button");
|
|
|
|
annot.replaceKey("/AS", name);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-03 22:00:56 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper turn off radio button");
|
|
|
|
annot.replaceKey("/AS", QPDFObjectHandle::newName("/Off"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::setCheckBoxValue(bool value)
|
|
|
|
{
|
|
|
|
// Set /AS to /Yes or /Off in addition to setting /V.
|
2022-04-02 21:14:10 +00:00
|
|
|
QPDFObjectHandle name = QPDFObjectHandle::newName(value ? "/Yes" : "/Off");
|
2019-01-03 22:00:56 +00:00
|
|
|
setFieldAttribute("/V", name);
|
|
|
|
QPDFObjectHandle AP = this->oh.getKey("/AP");
|
|
|
|
QPDFObjectHandle annot;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (AP.isNull()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
// The widget may be below. If there is more than one, just
|
|
|
|
// find the first one.
|
|
|
|
QPDFObjectHandle kids = this->oh.getKey("/Kids");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (kids.isArray()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
int nkids = kids.getArrayNItems();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (int i = 0; i < nkids; ++i) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QPDFObjectHandle kid = kids.getArrayItem(i);
|
|
|
|
AP = kid.getKey("/AP");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!AP.isNull()) {
|
|
|
|
QTC::TC(
|
|
|
|
"qpdf",
|
|
|
|
"QPDFFormFieldObjectHelper checkbox kid widget");
|
2019-01-03 22:00:56 +00:00
|
|
|
annot = kid;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-03 22:00:56 +00:00
|
|
|
annot = this->oh;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!annot.isInitialized()) {
|
2019-01-03 22:00:56 +00:00
|
|
|
QTC::TC("qpdf", "QPDFObjectHandle broken checkbox");
|
2022-04-02 21:14:10 +00:00
|
|
|
this->oh.warnIfPossible("unable to set the value of this checkbox");
|
2019-01-03 22:00:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper set checkbox AS");
|
|
|
|
annot.replaceKey("/AS", name);
|
|
|
|
}
|
2019-01-04 00:09:00 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::generateAppearance(QPDFAnnotationObjectHelper& aoh)
|
|
|
|
{
|
|
|
|
std::string ft = getFieldType();
|
|
|
|
// Ignore field types we don't know how to generate appearances
|
|
|
|
// for. Button fields don't really need them -- see code in
|
|
|
|
// QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded.
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((ft == "/Tx") || (ft == "/Ch")) {
|
2019-01-04 00:09:00 +00:00
|
|
|
generateTextAppearance(aoh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
class ValueSetter: public QPDFObjectHandle::TokenFilter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ValueSetter(
|
|
|
|
std::string const& DA,
|
|
|
|
std::string const& V,
|
|
|
|
std::vector<std::string> const& opt,
|
|
|
|
double tf,
|
|
|
|
QPDFObjectHandle::Rectangle const& bbox);
|
|
|
|
virtual ~ValueSetter() = default;
|
|
|
|
virtual void handleToken(QPDFTokenizer::Token const&);
|
|
|
|
virtual void handleEOF();
|
|
|
|
void writeAppearance();
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string DA;
|
|
|
|
std::string V;
|
|
|
|
std::vector<std::string> opt;
|
|
|
|
double tf;
|
|
|
|
QPDFObjectHandle::Rectangle bbox;
|
|
|
|
enum { st_top, st_bmc, st_emc, st_end } state;
|
|
|
|
bool replaced;
|
|
|
|
};
|
|
|
|
} // namespace
|
2019-01-04 00:09:00 +00:00
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
ValueSetter::ValueSetter(
|
|
|
|
std::string const& DA,
|
|
|
|
std::string const& V,
|
|
|
|
std::vector<std::string> const& opt,
|
|
|
|
double tf,
|
|
|
|
QPDFObjectHandle::Rectangle const& bbox) :
|
2019-01-04 00:09:00 +00:00
|
|
|
DA(DA),
|
|
|
|
V(V),
|
|
|
|
opt(opt),
|
|
|
|
tf(tf),
|
|
|
|
bbox(bbox),
|
2019-01-21 02:52:44 +00:00
|
|
|
state(st_top),
|
|
|
|
replaced(false)
|
2019-01-04 00:09:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ValueSetter::handleToken(QPDFTokenizer::Token const& token)
|
|
|
|
{
|
|
|
|
QPDFTokenizer::token_type_e ttype = token.getType();
|
|
|
|
std::string value = token.getValue();
|
|
|
|
bool do_replace = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (state) {
|
|
|
|
case st_top:
|
|
|
|
writeToken(token);
|
|
|
|
if ((ttype == QPDFTokenizer::tt_word) && (value == "BMC")) {
|
|
|
|
state = st_bmc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case st_bmc:
|
2019-01-04 00:09:00 +00:00
|
|
|
if ((ttype == QPDFTokenizer::tt_space) ||
|
2022-04-02 21:14:10 +00:00
|
|
|
(ttype == QPDFTokenizer::tt_comment)) {
|
2019-01-04 00:09:00 +00:00
|
|
|
writeToken(token);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-04 00:09:00 +00:00
|
|
|
state = st_emc;
|
|
|
|
}
|
|
|
|
// fall through to emc
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case st_emc:
|
|
|
|
if ((ttype == QPDFTokenizer::tt_word) && (value == "EMC")) {
|
2019-01-04 00:09:00 +00:00
|
|
|
do_replace = true;
|
|
|
|
state = st_end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case st_end:
|
2019-01-04 00:09:00 +00:00
|
|
|
writeToken(token);
|
|
|
|
break;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (do_replace) {
|
2019-01-04 00:09:00 +00:00
|
|
|
writeAppearance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 02:52:44 +00:00
|
|
|
void
|
|
|
|
ValueSetter::handleEOF()
|
2019-01-04 00:09:00 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!this->replaced) {
|
2019-01-21 02:52:44 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper replaced BMC at EOF");
|
|
|
|
write("/Tx BMC\n");
|
|
|
|
writeAppearance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ValueSetter::writeAppearance()
|
|
|
|
{
|
|
|
|
this->replaced = true;
|
|
|
|
|
2019-01-04 00:09:00 +00:00
|
|
|
// This code does not take quadding into consideration because
|
|
|
|
// doing so requires font metric information, which we don't
|
|
|
|
// have in many cases.
|
|
|
|
|
|
|
|
double tfh = 1.2 * tf;
|
|
|
|
int dx = 1;
|
|
|
|
|
|
|
|
// Write one or more lines, centered vertically, possibly with
|
|
|
|
// one row highlighted.
|
|
|
|
|
|
|
|
size_t max_rows = static_cast<size_t>((bbox.ury - bbox.lly) / tfh);
|
|
|
|
bool highlight = false;
|
|
|
|
size_t highlight_idx = 0;
|
|
|
|
|
|
|
|
std::vector<std::string> lines;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (opt.empty() || (max_rows < 2)) {
|
2019-01-04 00:09:00 +00:00
|
|
|
lines.push_back(V);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-04 00:09:00 +00:00
|
|
|
// Figure out what rows to write
|
|
|
|
size_t nopt = opt.size();
|
|
|
|
size_t found_idx = 0;
|
|
|
|
bool found = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
for (found_idx = 0; found_idx < nopt; ++found_idx) {
|
|
|
|
if (opt.at(found_idx) == V) {
|
2019-01-04 00:09:00 +00:00
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (found) {
|
2019-01-04 00:09:00 +00:00
|
|
|
// Try to make the found item the second one, but
|
|
|
|
// adjust for under/overflow.
|
2019-06-21 03:35:23 +00:00
|
|
|
int wanted_first = QIntC::to_int(found_idx) - 1;
|
|
|
|
int wanted_last = QIntC::to_int(found_idx + max_rows) - 2;
|
2019-01-04 00:09:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list found");
|
2022-04-02 21:14:10 +00:00
|
|
|
while (wanted_first < 0) {
|
2019-01-04 00:09:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list first too low");
|
|
|
|
++wanted_first;
|
|
|
|
++wanted_last;
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
while (wanted_last >= QIntC::to_int(nopt)) {
|
2019-01-04 00:09:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list last too high");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (wanted_first > 0) {
|
2019-01-04 00:09:00 +00:00
|
|
|
--wanted_first;
|
|
|
|
}
|
|
|
|
--wanted_last;
|
|
|
|
}
|
|
|
|
highlight = true;
|
2019-06-21 03:35:23 +00:00
|
|
|
highlight_idx = found_idx - QIntC::to_size(wanted_first);
|
|
|
|
for (size_t i = QIntC::to_size(wanted_first);
|
2022-04-02 21:14:10 +00:00
|
|
|
i <= QIntC::to_size(wanted_last);
|
|
|
|
++i) {
|
2019-01-04 00:09:00 +00:00
|
|
|
lines.push_back(opt.at(i));
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-01-04 00:09:00 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper list not found");
|
|
|
|
// include our value and the first n-1 rows
|
|
|
|
highlight_idx = 0;
|
|
|
|
highlight = true;
|
|
|
|
lines.push_back(V);
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; ((i < nopt) && (i < (max_rows - 1))); ++i) {
|
2019-01-04 00:09:00 +00:00
|
|
|
lines.push_back(opt.at(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the lines centered vertically, highlighting if needed
|
|
|
|
size_t nlines = lines.size();
|
2022-04-02 21:14:10 +00:00
|
|
|
double dy = bbox.ury -
|
|
|
|
((bbox.ury - bbox.lly - (static_cast<double>(nlines) * tfh)) / 2.0);
|
|
|
|
if (highlight) {
|
|
|
|
write(
|
|
|
|
"q\n0.85 0.85 0.85 rg\n" + QUtil::double_to_string(bbox.llx) + " " +
|
|
|
|
QUtil::double_to_string(
|
|
|
|
bbox.lly + dy -
|
|
|
|
(tfh * (static_cast<double>(highlight_idx + 1)))) +
|
|
|
|
" " + QUtil::double_to_string(bbox.urx - bbox.llx) + " " +
|
|
|
|
QUtil::double_to_string(tfh) + " re f\nQ\n");
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|
2019-01-21 03:39:25 +00:00
|
|
|
dy -= tf;
|
|
|
|
write("q\nBT\n" + DA + "\n");
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < nlines; ++i) {
|
2019-01-21 03:39:25 +00:00
|
|
|
// We could adjust Tm to translate to the beginning the first
|
|
|
|
// line, set TL to tfh, and use T* for each subsequent line,
|
|
|
|
// but doing this would require extracting any Tm from DA,
|
|
|
|
// which doesn't seem really worth the effort.
|
2022-04-02 21:14:10 +00:00
|
|
|
if (i == 0) {
|
|
|
|
write(
|
|
|
|
QUtil::double_to_string(bbox.llx + static_cast<double>(dx)) +
|
|
|
|
" " +
|
|
|
|
QUtil::double_to_string(bbox.lly + static_cast<double>(dy)) +
|
|
|
|
" Td\n");
|
|
|
|
} else {
|
2019-01-21 03:39:25 +00:00
|
|
|
write("0 " + QUtil::double_to_string(-tfh) + " Td\n");
|
|
|
|
}
|
|
|
|
write(QPDFObjectHandle::newString(lines.at(i)).unparse() + " Tj\n");
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|
2019-01-21 03:39:25 +00:00
|
|
|
write("ET\nQ\nEMC");
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 17:21:57 +00:00
|
|
|
namespace
|
2019-01-04 00:09:00 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
class TfFinder: public QPDFObjectHandle::TokenFilter
|
2019-01-04 00:09:00 +00:00
|
|
|
{
|
2022-04-16 17:21:57 +00:00
|
|
|
public:
|
|
|
|
TfFinder();
|
|
|
|
virtual ~TfFinder()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
virtual void handleToken(QPDFTokenizer::Token const&);
|
|
|
|
double getTf();
|
|
|
|
std::string getFontName();
|
|
|
|
std::string getDA();
|
|
|
|
|
|
|
|
private:
|
|
|
|
double tf;
|
|
|
|
int tf_idx;
|
|
|
|
std::string font_name;
|
|
|
|
double last_num;
|
|
|
|
int last_num_idx;
|
|
|
|
std::string last_name;
|
|
|
|
std::vector<std::string> DA;
|
|
|
|
};
|
|
|
|
} // namespace
|
2019-01-04 00:09:00 +00:00
|
|
|
|
|
|
|
TfFinder::TfFinder() :
|
|
|
|
tf(11.0),
|
2022-02-18 18:43:26 +00:00
|
|
|
tf_idx(-1),
|
2019-01-21 03:48:24 +00:00
|
|
|
last_num(0.0),
|
2022-02-18 18:43:26 +00:00
|
|
|
last_num_idx(-1)
|
2019-01-04 00:09:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TfFinder::handleToken(QPDFTokenizer::Token const& token)
|
|
|
|
{
|
|
|
|
QPDFTokenizer::token_type_e ttype = token.getType();
|
|
|
|
std::string value = token.getValue();
|
2019-01-21 03:48:24 +00:00
|
|
|
DA.push_back(token.getRawValue());
|
2022-04-02 21:14:10 +00:00
|
|
|
switch (ttype) {
|
|
|
|
case QPDFTokenizer::tt_integer:
|
|
|
|
case QPDFTokenizer::tt_real:
|
2019-01-04 00:09:00 +00:00
|
|
|
last_num = strtod(value.c_str(), 0);
|
2022-02-18 18:43:26 +00:00
|
|
|
last_num_idx = QIntC::to_int(DA.size() - 1);
|
2019-01-04 00:09:00 +00:00
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDFTokenizer::tt_name:
|
2019-01-05 18:42:31 +00:00
|
|
|
last_name = value;
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
case QPDFTokenizer::tt_word:
|
|
|
|
if (value == "Tf") {
|
|
|
|
if ((last_num > 1.0) && (last_num < 1000.0)) {
|
2022-02-18 18:43:26 +00:00
|
|
|
// These ranges are arbitrary but keep us from doing
|
|
|
|
// insane things or suffering from over/underflow
|
|
|
|
tf = last_num;
|
|
|
|
}
|
|
|
|
tf_idx = last_num_idx;
|
|
|
|
font_name = last_name;
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
default:
|
2019-01-04 00:09:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
double
|
|
|
|
TfFinder::getTf()
|
|
|
|
{
|
|
|
|
return this->tf;
|
|
|
|
}
|
|
|
|
|
2019-01-21 03:48:24 +00:00
|
|
|
std::string
|
|
|
|
TfFinder::getDA()
|
|
|
|
{
|
|
|
|
std::string result;
|
|
|
|
size_t n = this->DA.size();
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < n; ++i) {
|
2019-01-21 03:48:24 +00:00
|
|
|
std::string cur = this->DA.at(i);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (QIntC::to_int(i) == tf_idx) {
|
2019-01-21 03:48:24 +00:00
|
|
|
double delta = strtod(cur.c_str(), 0) - this->tf;
|
2022-04-02 21:14:10 +00:00
|
|
|
if ((delta > 0.001) || (delta < -0.001)) {
|
2019-01-21 03:48:24 +00:00
|
|
|
// tf doesn't match the font size passed to Tf, so
|
|
|
|
// substitute.
|
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper fallback Tf");
|
|
|
|
cur = QUtil::double_to_string(tf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result += cur;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-05 18:42:31 +00:00
|
|
|
std::string
|
|
|
|
TfFinder::getFontName()
|
|
|
|
{
|
|
|
|
return this->font_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QPDFObjectHandle
|
|
|
|
QPDFFormFieldObjectHelper::getFontFromResource(
|
|
|
|
QPDFObjectHandle resources, std::string const& name)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle result;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (resources.isDictionary() && resources.getKey("/Font").isDictionary() &&
|
|
|
|
resources.getKey("/Font").hasKey(name)) {
|
2019-01-05 18:42:31 +00:00
|
|
|
result = resources.getKey("/Font").getKey(name);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-01-04 00:09:00 +00:00
|
|
|
void
|
|
|
|
QPDFFormFieldObjectHelper::generateTextAppearance(
|
|
|
|
QPDFAnnotationObjectHelper& aoh)
|
|
|
|
{
|
|
|
|
QPDFObjectHandle AS = aoh.getAppearanceStream("/N");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (AS.isNull()) {
|
2019-01-21 02:52:44 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper create AS from scratch");
|
|
|
|
QPDFObjectHandle::Rectangle rect = aoh.getRect();
|
|
|
|
QPDFObjectHandle::Rectangle bbox(
|
|
|
|
0, 0, rect.urx - rect.llx, rect.ury - rect.lly);
|
|
|
|
QPDFObjectHandle dict = QPDFObjectHandle::parse(
|
|
|
|
"<< /Resources << /ProcSet [ /PDF /Text ] >>"
|
|
|
|
" /Type /XObject /Subtype /Form >>");
|
|
|
|
dict.replaceKey("/BBox", QPDFObjectHandle::newFromRectangle(bbox));
|
|
|
|
AS = QPDFObjectHandle::newStream(
|
|
|
|
this->oh.getOwningQPDF(), "/Tx BMC\nEMC\n");
|
|
|
|
AS.replaceDict(dict);
|
|
|
|
QPDFObjectHandle AP = aoh.getAppearanceDictionary();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (AP.isNull()) {
|
2019-01-21 02:52:44 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper create AP from scratch");
|
|
|
|
aoh.getObjectHandle().replaceKey(
|
|
|
|
"/AP", QPDFObjectHandle::newDictionary());
|
|
|
|
AP = aoh.getAppearanceDictionary();
|
|
|
|
}
|
|
|
|
AP.replaceKey("/N", AS);
|
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!AS.isStream()) {
|
2019-01-04 00:09:00 +00:00
|
|
|
aoh.getObjectHandle().warnIfPossible(
|
|
|
|
"unable to get normal appearance stream for update");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle bbox_obj = AS.getDict().getKey("/BBox");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!bbox_obj.isRectangle()) {
|
2019-01-04 00:09:00 +00:00
|
|
|
aoh.getObjectHandle().warnIfPossible(
|
|
|
|
"unable to get appearance stream bounding box");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPDFObjectHandle::Rectangle bbox = bbox_obj.getArrayAsRectangle();
|
|
|
|
std::string DA = getDefaultAppearance();
|
2019-01-05 18:42:31 +00:00
|
|
|
std::string V = getValueAsString();
|
|
|
|
std::vector<std::string> opt;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (isChoice() && ((getFlags() & ff_ch_combo) == 0)) {
|
2019-01-05 18:42:31 +00:00
|
|
|
opt = getChoices();
|
|
|
|
}
|
2019-01-04 00:09:00 +00:00
|
|
|
|
|
|
|
TfFinder tff;
|
|
|
|
Pl_QPDFTokenizer tok("tf", &tff);
|
|
|
|
tok.write(QUtil::unsigned_char_pointer(DA.c_str()), DA.length());
|
|
|
|
tok.finish();
|
|
|
|
double tf = tff.getTf();
|
2019-01-21 03:48:24 +00:00
|
|
|
DA = tff.getDA();
|
2019-01-05 18:42:31 +00:00
|
|
|
|
|
|
|
std::string (*encoder)(std::string const&, char) = &QUtil::utf8_to_ascii;
|
|
|
|
std::string font_name = tff.getFontName();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!font_name.empty()) {
|
2019-01-05 18:42:31 +00:00
|
|
|
// See if the font is encoded with something we know about.
|
|
|
|
QPDFObjectHandle resources = AS.getDict().getKey("/Resources");
|
|
|
|
QPDFObjectHandle font = getFontFromResource(resources, font_name);
|
2021-03-03 21:57:37 +00:00
|
|
|
bool found_font_in_dr = false;
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!font.isInitialized()) {
|
2021-02-26 19:47:52 +00:00
|
|
|
QPDFObjectHandle dr = getDefaultResources();
|
2019-01-05 18:42:31 +00:00
|
|
|
font = getFontFromResource(dr, font_name);
|
2021-03-03 21:57:37 +00:00
|
|
|
found_font_in_dr = (font.isInitialized() && font.isDictionary());
|
2019-01-05 18:42:31 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (found_font_in_dr && resources.isDictionary()) {
|
2021-03-03 21:57:37 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper get font from /DR");
|
2022-04-02 21:14:10 +00:00
|
|
|
if (resources.isIndirect()) {
|
2021-03-03 21:57:37 +00:00
|
|
|
resources = resources.getOwningQPDF()->makeIndirectObject(
|
|
|
|
resources.shallowCopy());
|
|
|
|
AS.getDict().replaceKey("/Resources", resources);
|
|
|
|
}
|
|
|
|
// Use mergeResources to force /Font to be local
|
2022-02-05 14:18:58 +00:00
|
|
|
resources.mergeResources("<< /Font << >> >>"_qpdf);
|
2021-03-03 21:57:37 +00:00
|
|
|
resources.getKey("/Font").replaceKey(font_name, font);
|
|
|
|
}
|
|
|
|
|
2022-04-02 21:14:10 +00:00
|
|
|
if (font.isInitialized() && font.isDictionary() &&
|
|
|
|
font.getKey("/Encoding").isName()) {
|
2019-01-05 18:42:31 +00:00
|
|
|
std::string encoding = font.getKey("/Encoding").getName();
|
2022-04-02 21:14:10 +00:00
|
|
|
if (encoding == "/WinAnsiEncoding") {
|
2019-01-05 18:42:31 +00:00
|
|
|
QTC::TC("qpdf", "QPDFFormFieldObjectHelper WinAnsi");
|
|
|
|
encoder = &QUtil::utf8_to_win_ansi;
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (encoding == "/MacRomanEncoding") {
|
2019-01-05 18:42:31 +00:00
|
|
|
encoder = &QUtil::utf8_to_mac_roman;
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|
2019-01-05 18:42:31 +00:00
|
|
|
|
|
|
|
V = (*encoder)(V, '?');
|
2022-04-02 21:14:10 +00:00
|
|
|
for (size_t i = 0; i < opt.size(); ++i) {
|
2019-01-05 18:42:31 +00:00
|
|
|
opt.at(i) = (*encoder)(opt.at(i), '?');
|
|
|
|
}
|
|
|
|
|
2022-04-03 20:10:27 +00:00
|
|
|
AS.addTokenFilter(
|
|
|
|
// line-break
|
2022-04-09 18:35:56 +00:00
|
|
|
std::shared_ptr<QPDFObjectHandle::TokenFilter>(
|
2022-04-03 20:10:27 +00:00
|
|
|
new ValueSetter(DA, V, opt, tf, bbox)));
|
2019-01-04 00:09:00 +00:00
|
|
|
}
|