mirror of
https://github.com/qpdf/qpdf.git
synced 2025-01-22 22:58:33 +00:00
Remove some if statements and simplify some boolean expressions
Use QPDFObjectHandle::isNameAndEquals, isDictionaryOfType and isStreamOfType.
This commit is contained in:
parent
710d2e54f0
commit
07db3200cb
@ -290,15 +290,14 @@ static void check(char const* filename,
|
||||
QPDFObjectHandle color_space = image_dict.getKey("/ColorSpace");
|
||||
QPDFObjectHandle filter = image_dict.getKey("/Filter");
|
||||
bool this_errors = false;
|
||||
if (! (filter.isName() && (filter.getName() == desired_filter)))
|
||||
if (! filter.isNameAndEquals(desired_filter))
|
||||
{
|
||||
this_errors = errors = true;
|
||||
std::cout << "page " << pageno << ": expected filter "
|
||||
<< desired_filter << "; actual filter = "
|
||||
<< filter.unparse() << std::endl;
|
||||
}
|
||||
if (! (color_space.isName() &&
|
||||
(color_space.getName() == desired_color_space)))
|
||||
if (! color_space.isNameAndEquals(desired_color_space))
|
||||
{
|
||||
this_errors = errors = true;
|
||||
std::cout << "page " << pageno << ": expected color space "
|
||||
|
@ -167,9 +167,8 @@ int main(int argc, char* argv[])
|
||||
// keys to determine the image type.
|
||||
if (image.pipeStreamData(0, qpdf_ef_compress,
|
||||
qpdf_dl_all) &&
|
||||
color_space.isName() &&
|
||||
color_space.isNameAndEquals("/DeviceGray") &&
|
||||
bits_per_component.isInteger() &&
|
||||
(color_space.getName() == "/DeviceGray") &&
|
||||
(bits_per_component.getIntValue() == 8))
|
||||
{
|
||||
inv->registerImage(image, p);
|
||||
|
@ -1100,10 +1100,7 @@ QPDF::read_xrefStream(qpdf_offset_t xref_offset)
|
||||
{
|
||||
// ignore -- report error below
|
||||
}
|
||||
if (xref_obj.isInitialized() &&
|
||||
xref_obj.isStream() &&
|
||||
xref_obj.getDict().getKey("/Type").isName() &&
|
||||
xref_obj.getDict().getKey("/Type").getName() == "/XRef")
|
||||
if (xref_obj.isStreamOfType("/XRef"))
|
||||
{
|
||||
QTC::TC("qpdf", "QPDF found xref stream");
|
||||
found = true;
|
||||
@ -2202,8 +2199,7 @@ QPDF::resolveObjectsInStream(int obj_stream_number)
|
||||
this->m->obj_cache[stream_og].end_after_space;
|
||||
|
||||
QPDFObjectHandle dict = obj_stream.getDict();
|
||||
if (! (dict.getKey("/Type").isName() &&
|
||||
dict.getKey("/Type").getName() == "/ObjStm"))
|
||||
if (! dict.isDictionaryOfType("/ObjStm"))
|
||||
{
|
||||
QTC::TC("qpdf", "QPDF ERR object stream with wrong type");
|
||||
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
||||
@ -2849,13 +2845,10 @@ QPDF::getCompressibleObjGens()
|
||||
{
|
||||
QTC::TC("qpdf", "QPDF exclude encryption dictionary");
|
||||
}
|
||||
else if ((! obj.isStream()) &&
|
||||
(! (obj.isDictionary() &&
|
||||
else if (! (obj.isStream() ||
|
||||
(obj.isDictionaryOfType("/Sig") &&
|
||||
obj.hasKey("/ByteRange") &&
|
||||
obj.hasKey("/Contents") &&
|
||||
obj.hasKey("/Type") &&
|
||||
obj.getKey("/Type").isName() &&
|
||||
obj.getKey("/Type").getName() == "/Sig")))
|
||||
obj.hasKey("/Contents"))))
|
||||
{
|
||||
result.push_back(og);
|
||||
}
|
||||
|
@ -280,9 +280,7 @@ QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h)
|
||||
{
|
||||
QPDFObjectHandle oh = h.getObjectHandle();
|
||||
QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull());
|
||||
if (! (oh.isDictionary() &&
|
||||
oh.getKey("/Subtype").isName() &&
|
||||
(oh.getKey("/Subtype").getName() == "/Widget")))
|
||||
if (! oh.isDictionaryOfType("", "/Widget"))
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
@ -15,8 +15,7 @@ QPDFFileSpecObjectHelper::QPDFFileSpecObjectHelper(
|
||||
oh.warnIfPossible("Embedded file object is not a dictionary");
|
||||
return;
|
||||
}
|
||||
auto type = oh.getKey("/Type");
|
||||
if (! (type.isName() && (type.getName() == "/Filespec")))
|
||||
if (! oh.isDictionaryOfType("/Filespec"))
|
||||
{
|
||||
oh.warnIfPossible("Embedded file object's type is not /Filespec");
|
||||
}
|
||||
|
@ -1055,23 +1055,9 @@ QPDFObjectHandle::getDictAsMap()
|
||||
bool
|
||||
QPDFObjectHandle::isOrHasName(std::string const& value)
|
||||
{
|
||||
if (isName() && (getName() == value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (isArray())
|
||||
{
|
||||
int n = getArrayNItems();
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
QPDFObjectHandle item = getArrayItem(0);
|
||||
if (item.isName() && (item.getName() == value))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return isNameAndEquals(value) ||
|
||||
(isArray() && (getArrayNItems() > 0) &&
|
||||
getArrayItem(0).isNameAndEquals(value));
|
||||
}
|
||||
|
||||
void
|
||||
@ -2520,8 +2506,7 @@ QPDFObjectHandle::parseInternal(PointerHolder<InputSource> input,
|
||||
}
|
||||
if (!contents_string.empty() &&
|
||||
dict.count("/Type") &&
|
||||
dict["/Type"].isName() &&
|
||||
dict["/Type"].getName() == "/Sig" &&
|
||||
dict["/Type"].isNameAndEquals("/Sig") &&
|
||||
dict.count("/ByteRange") &&
|
||||
dict.count("/Contents") &&
|
||||
dict["/Contents"].isString())
|
||||
@ -3237,7 +3222,7 @@ QPDFObjectHandle::isPageObject()
|
||||
if (this->hasKey("/Type"))
|
||||
{
|
||||
QPDFObjectHandle type = this->getKey("/Type");
|
||||
if (type.isName() && (type.getName() == "/Page"))
|
||||
if (type.isNameAndEquals("/Page"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@ -3263,39 +3248,22 @@ QPDFObjectHandle::isPagesObject()
|
||||
}
|
||||
// getAllPages repairs /Type when traversing the page tree.
|
||||
getOwningQPDF()->getAllPages();
|
||||
return (this->isDictionary() &&
|
||||
this->hasKey("/Type") &&
|
||||
this->getKey("/Type").isName() &&
|
||||
this->getKey("/Type").getName() == "/Pages");
|
||||
return isDictionaryOfType("/Pages");
|
||||
}
|
||||
|
||||
bool
|
||||
QPDFObjectHandle::isFormXObject()
|
||||
{
|
||||
if (! this->isStream())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QPDFObjectHandle dict = this->getDict();
|
||||
return (dict.getKey("/Type").isName() &&
|
||||
("/XObject" == dict.getKey("/Type").getName()) &&
|
||||
dict.getKey("/Subtype").isName() &&
|
||||
("/Form" == dict.getKey("/Subtype").getName()));
|
||||
return isStreamOfType("/XObject", "/Form");
|
||||
}
|
||||
|
||||
bool
|
||||
QPDFObjectHandle::isImage(bool exclude_imagemask)
|
||||
{
|
||||
if (! this->isStream())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
QPDFObjectHandle dict = this->getDict();
|
||||
return (dict.hasKey("/Subtype") &&
|
||||
(dict.getKey("/Subtype").getName() == "/Image") &&
|
||||
return (isStreamOfType("", "/Image") &&
|
||||
((! exclude_imagemask) ||
|
||||
(! (dict.getKey("/ImageMask").isBool() &&
|
||||
dict.getKey("/ImageMask").getBoolValue()))));
|
||||
(! (getDict().getKey("/ImageMask").isBool() &&
|
||||
getDict().getKey("/ImageMask").getBoolValue()))));
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -558,10 +558,7 @@ QPDFPageObjectHelper::getAnnotations(std::string const& only_subtype)
|
||||
for (int i = 0; i < nannots; ++i)
|
||||
{
|
||||
QPDFObjectHandle annot = annots.getArrayItem(i);
|
||||
if (only_subtype.empty() ||
|
||||
(annot.isDictionary() &&
|
||||
annot.getKey("/Subtype").isName() &&
|
||||
(only_subtype == annot.getKey("/Subtype").getName())))
|
||||
if (annot.isDictionaryOfType("", only_subtype))
|
||||
{
|
||||
result.push_back(QPDFAnnotationObjectHelper(annot));
|
||||
}
|
||||
|
@ -1241,9 +1241,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object)
|
||||
" another file.");
|
||||
}
|
||||
|
||||
if (this->m->qdf_mode &&
|
||||
object.isStream() && object.getDict().getKey("/Type").isName() &&
|
||||
(object.getDict().getKey("/Type").getName() == "/XRef"))
|
||||
if (this->m->qdf_mode && object.isStreamOfType("/XRef"))
|
||||
{
|
||||
// As a special case, do not output any extraneous XRef
|
||||
// streams in QDF mode. Doing so will confuse fix-qdf,
|
||||
@ -1474,8 +1472,7 @@ QPDFWriter::willFilterStream(QPDFObjectHandle stream,
|
||||
QPDFObjGen old_og = stream.getObjGen();
|
||||
QPDFObjectHandle stream_dict = stream.getDict();
|
||||
|
||||
if (stream_dict.getKey("/Type").isName() &&
|
||||
(stream_dict.getKey("/Type").getName() == "/Metadata"))
|
||||
if (stream_dict.isDictionaryOfType("/Metadata"))
|
||||
{
|
||||
is_metadata = true;
|
||||
}
|
||||
@ -1691,11 +1688,8 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
||||
QTC::TC("qpdf", "QPDFWriter preserve Extensions");
|
||||
QPDFObjectHandle adbe = extensions.getKey("/ADBE");
|
||||
if (adbe.isDictionary() &&
|
||||
adbe.hasKey("/BaseVersion") &&
|
||||
adbe.getKey("/BaseVersion").isName() &&
|
||||
(adbe.getKey("/BaseVersion").getName() ==
|
||||
"/" + this->m->final_pdf_version) &&
|
||||
adbe.hasKey("/ExtensionLevel") &&
|
||||
adbe.getKey("/BaseVersion").isNameAndEquals(
|
||||
"/" + this->m->final_pdf_version) &&
|
||||
adbe.getKey("/ExtensionLevel").isInteger() &&
|
||||
(adbe.getKey("/ExtensionLevel").getIntValue() ==
|
||||
this->m->final_extension_level))
|
||||
@ -1764,7 +1758,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
||||
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
||||
{
|
||||
QPDFObjectHandle item = filter.getArrayItem(i);
|
||||
if (item.isName() && item.getName() == "/Crypt")
|
||||
if (item.isNameAndEquals("/Crypt"))
|
||||
{
|
||||
idx = i;
|
||||
break;
|
||||
@ -1802,9 +1796,7 @@ QPDFWriter::unparseObject(QPDFObjectHandle object, int level,
|
||||
writeString(QPDF_Name::normalizeName(key));
|
||||
writeString(" ");
|
||||
if (key == "/Contents" &&
|
||||
object.hasKey("/Type") &&
|
||||
object.getKey("/Type").isName() &&
|
||||
object.getKey("/Type").getName() == "/Sig" &&
|
||||
object.isDictionaryOfType("/Sig") &&
|
||||
object.hasKey("/ByteRange"))
|
||||
{
|
||||
QTC::TC("qpdf", "QPDFWriter no encryption sig contents");
|
||||
|
@ -35,10 +35,8 @@ class SF_Crypt: public QPDFStreamFilter
|
||||
for (auto const& key: decode_parms.getKeys())
|
||||
{
|
||||
if (((key == "/Type") || (key == "/Name")) &&
|
||||
(decode_parms.getKey("/Type").isNull() ||
|
||||
(decode_parms.getKey("/Type").isName() &&
|
||||
(decode_parms.getKey("/Type").getName() ==
|
||||
"/CryptFilterDecodeParms"))))
|
||||
((! decode_parms.hasKey("/Type")) ||
|
||||
decode_parms.isDictionaryOfType("/CryptFilterDecodeParms")))
|
||||
{
|
||||
// we handle this in decryptStream
|
||||
}
|
||||
|
@ -1262,9 +1262,7 @@ QPDF::decryptStream(PointerHolder<EncryptionParameters> encp,
|
||||
{
|
||||
QPDFObjectHandle decode_parms =
|
||||
stream_dict.getKey("/DecodeParms");
|
||||
if (decode_parms.getKey("/Type").isName() &&
|
||||
(decode_parms.getKey("/Type").getName() ==
|
||||
"/CryptFilterDecodeParms"))
|
||||
if (decode_parms.isDictionaryOfType("/CryptFilterDecodeParms"))
|
||||
{
|
||||
QTC::TC("qpdf", "QPDF_encryption stream crypt filter");
|
||||
method = interpretCF(encp, decode_parms.getKey("/Name"));
|
||||
@ -1280,8 +1278,7 @@ QPDF::decryptStream(PointerHolder<EncryptionParameters> encp,
|
||||
{
|
||||
for (int i = 0; i < filter.getArrayNItems(); ++i)
|
||||
{
|
||||
if (filter.getArrayItem(i).isName() &&
|
||||
(filter.getArrayItem(i).getName() == "/Crypt"))
|
||||
if (filter.getArrayItem(i).isNameAndEquals("/Crypt"))
|
||||
{
|
||||
QPDFObjectHandle crypt_params =
|
||||
decode.getArrayItem(i);
|
||||
|
@ -382,17 +382,13 @@ QPDF::updateObjectMapsInternal(
|
||||
|
||||
bool is_page_node = false;
|
||||
|
||||
if (oh.isDictionary() && oh.hasKey("/Type"))
|
||||
if (oh.isDictionaryOfType("/Page"))
|
||||
{
|
||||
std::string type = oh.getKey("/Type").getName();
|
||||
if (type == "/Page")
|
||||
{
|
||||
is_page_node = true;
|
||||
if (! top)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
is_page_node = true;
|
||||
if (! top)
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (oh.isIndirect())
|
||||
|
@ -139,8 +139,7 @@ QPDF::getAllPagesInternal(QPDFObjectHandle cur_node,
|
||||
result.push_back(cur_node);
|
||||
}
|
||||
|
||||
QPDFObjectHandle type_key = cur_node.getKey("/Type");
|
||||
if (! (type_key.isName() && (type_key.getName() == wanted_type)))
|
||||
if (! cur_node.isDictionaryOfType(wanted_type))
|
||||
{
|
||||
warn(QPDFExc(qpdf_e_damaged_pdf, this->m->file->getName(),
|
||||
"page tree node",
|
||||
|
Loading…
x
Reference in New Issue
Block a user