mirror of https://github.com/qpdf/qpdf.git
Reimplement QPDF_Stream::getStreamJSON in terms of writeStreamJSON
writeStreamJSON is a temporary implementation minimally adapted from getStreamJSON.
This commit is contained in:
parent
9589fad1e5
commit
920e929864
|
@ -190,22 +190,44 @@ QPDF_Stream::getStreamJSON(
|
||||||
qpdf_stream_decode_level_e decode_level,
|
qpdf_stream_decode_level_e decode_level,
|
||||||
Pipeline* p,
|
Pipeline* p,
|
||||||
std::string const& data_filename)
|
std::string const& data_filename)
|
||||||
|
{
|
||||||
|
Pl_Buffer pb{"streamjson"};
|
||||||
|
JSON::Writer jw{&pb, 0};
|
||||||
|
decode_level =
|
||||||
|
writeStreamJSON(json_version, jw, json_data, decode_level, p, data_filename, true);
|
||||||
|
pb.finish();
|
||||||
|
auto result = JSON::parse(pb.getString());
|
||||||
|
if (json_data == qpdf_sj_inline) {
|
||||||
|
result.addDictionaryMember("data", JSON::makeBlob(StreamBlobProvider(this, decode_level)));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
qpdf_stream_decode_level_e
|
||||||
|
QPDF_Stream::writeStreamJSON(
|
||||||
|
int json_version,
|
||||||
|
JSON::Writer& jw,
|
||||||
|
qpdf_json_stream_data_e json_data,
|
||||||
|
qpdf_stream_decode_level_e decode_level,
|
||||||
|
Pipeline* p,
|
||||||
|
std::string const& data_filename,
|
||||||
|
bool no_data_key)
|
||||||
{
|
{
|
||||||
switch (json_data) {
|
switch (json_data) {
|
||||||
case qpdf_sj_none:
|
case qpdf_sj_none:
|
||||||
case qpdf_sj_inline:
|
case qpdf_sj_inline:
|
||||||
if (p != nullptr) {
|
if (p != nullptr) {
|
||||||
throw std::logic_error("QPDF_Stream::getStreamJSON: pipeline should only be supplied "
|
throw std::logic_error("QPDF_Stream::writeStreamJSON: pipeline should only be supplied "
|
||||||
"when json_data is file");
|
"when json_data is file");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case qpdf_sj_file:
|
case qpdf_sj_file:
|
||||||
if (p == nullptr) {
|
if (p == nullptr) {
|
||||||
throw std::logic_error(
|
throw std::logic_error(
|
||||||
"QPDF_Stream::getStreamJSON: pipeline must be supplied when json_data is file");
|
"QPDF_Stream::writeStreamJSON: pipeline must be supplied when json_data is file");
|
||||||
}
|
}
|
||||||
if (data_filename.empty()) {
|
if (data_filename.empty()) {
|
||||||
throw std::logic_error("QPDF_Stream::getStreamJSON: data_filename must be supplied "
|
throw std::logic_error("QPDF_Stream::writeStreamJSON: data_filename must be supplied "
|
||||||
"when json_data is file");
|
"when json_data is file");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -234,12 +256,13 @@ QPDF_Stream::getStreamJSON(
|
||||||
decode_level = qpdf_dl_none;
|
decode_level = qpdf_dl_none;
|
||||||
buf_pl.getString(); // reset buf_pl
|
buf_pl.getString(); // reset buf_pl
|
||||||
} else {
|
} else {
|
||||||
if (json_data == qpdf_sj_file) {
|
buf_pl_ready = true;
|
||||||
buf_pl_ready = true;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!buf_pl_ready) {
|
||||||
|
throw std::logic_error("QPDF_Stream: failed to get stream data");
|
||||||
|
}
|
||||||
// We can use unsafeShallowCopy because we are only touching top-level keys.
|
// We can use unsafeShallowCopy because we are only touching top-level keys.
|
||||||
dict = this->stream_dict.unsafeShallowCopy();
|
dict = this->stream_dict.unsafeShallowCopy();
|
||||||
dict.removeKey("/Length");
|
dict.removeKey("/Length");
|
||||||
|
@ -249,19 +272,20 @@ QPDF_Stream::getStreamJSON(
|
||||||
}
|
}
|
||||||
if (json_data == qpdf_sj_file) {
|
if (json_data == qpdf_sj_file) {
|
||||||
result.addDictionaryMember("datafile", JSON::makeString(data_filename));
|
result.addDictionaryMember("datafile", JSON::makeString(data_filename));
|
||||||
if (!buf_pl_ready) {
|
|
||||||
throw std::logic_error("QPDF_Stream: failed to get stream data in json file mode");
|
|
||||||
}
|
|
||||||
p->writeString(buf_pl.getString());
|
p->writeString(buf_pl.getString());
|
||||||
} else if (json_data == qpdf_sj_inline) {
|
} else if (json_data == qpdf_sj_inline) {
|
||||||
result.addDictionaryMember(
|
if (!no_data_key) {
|
||||||
"data", JSON::makeBlob(StreamBlobProvider(this, decode_level)));
|
result.addDictionaryMember(
|
||||||
|
"data", JSON::makeBlob(StreamBlobProvider(this, decode_level)));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw std::logic_error("QPDF_Stream: unexpected value of json_data");
|
throw std::logic_error("QPDF_Stream: unexpected value of json_data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.addDictionaryMember("dict", dict.getJSON(json_version));
|
result.addDictionaryMember("dict", dict.getJSON(json_version));
|
||||||
return result;
|
jw << std::move(result);
|
||||||
|
return decode_level;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -64,6 +64,14 @@ class QPDF_Stream: public QPDFValue
|
||||||
qpdf_stream_decode_level_e decode_level,
|
qpdf_stream_decode_level_e decode_level,
|
||||||
Pipeline* p,
|
Pipeline* p,
|
||||||
std::string const& data_filename);
|
std::string const& data_filename);
|
||||||
|
qpdf_stream_decode_level_e writeStreamJSON(
|
||||||
|
int json_version,
|
||||||
|
JSON::Writer& jw,
|
||||||
|
qpdf_json_stream_data_e json_data,
|
||||||
|
qpdf_stream_decode_level_e decode_level,
|
||||||
|
Pipeline* p,
|
||||||
|
std::string const& data_filename,
|
||||||
|
bool no_data_key = false);
|
||||||
|
|
||||||
void replaceDict(QPDFObjectHandle const& new_dict);
|
void replaceDict(QPDFObjectHandle const& new_dict);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue