Skip to content

Commit

Permalink
[29_1] adjust pdf attachment interface
Browse files Browse the repository at this point in the history
  • Loading branch information
tangdouer1005 authored Sep 9, 2023
1 parent 18dde97 commit 6849454
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 68 deletions.
2 changes: 1 addition & 1 deletion TeXmacs/progs/texmacs/texmacs/tm-files.scm
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@
(if (current-buffer)
(set! name (url-relative (current-buffer) name))
(set! name (url-append (url-pwd) name))))
(if (and (string=? (url-format name) "pdf") (get-attachments name))
(if (and (string=? (url-format name) "pdf") (extract-attachments name))
(let* ((tm-name (url-glue (url-relative name (url-basename name)) ".tm")))
(if(url-exists? tm-name)
(set! name tm-name))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
* MODULE : pdf_hummus_get_attachment.cpp
* DESCRIPTION: Interface for getting attachment file in pdf
* MODULE : pdf_hummus_extract_attachment.cpp
* DESCRIPTION: Interface for extract attachment file in pdf
* COPYRIGHT : (C) 2023 Tangdouer
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
Expand All @@ -23,77 +23,79 @@
using namespace PDFHummus;
#include <utility>
using namespace IOBasicTypes;
#include "pdf_hummus_get_attachment.hpp"
#include "pdf_hummus_extract_attachment.hpp"
#include "tm_debug.hpp"

bool
get_tm_attachments_in_pdf (url pdf_path, array<url>& names) {
extract_attachments_from_pdf (url pdf_path, list<url>& names) {
EStatusCode status= PDFHummus::eSuccess;
InputFile pdfFile;
PDFParser parser;
do {
status= pdfFile.OpenFile (as_charp (as_string (pdf_path)));
if (status != PDFHummus::eSuccess) {
cout << "fail to open " << as_string (pdf_path) << LF;
if (DEBUG_CONVERT)
debug_convert << "fail to open " << as_string (pdf_path) << LF;
break;
}
parser.StartPDFParsing (pdfFile.GetInputStream ());
PDFObjectCastPtr<PDFDictionary> catalog (
parser.QueryDictionaryObject (parser.GetTrailer (), "Root"));
// return 0;
if (!catalog) {
cout << "Can't find catalog. fail\n";
if (DEBUG_CONVERT) debug_convert << "Can't find catalog. fail\n";
status= PDFHummus::eFailure;
break;
}

PDFObjectCastPtr<PDFDictionary> d_1 (catalog->QueryDirectObject ("Names"));
if (!d_1) {
cout << "Can't find d1. fail\n";
if (DEBUG_CONVERT) debug_convert << "Can't find d1. fail\n";
status= PDFHummus::eFailure;
break;
}
PDFObjectCastPtr<PDFDictionary> d_2 (
d_1->QueryDirectObject ("EmbeddedFiles"));
if (!d_2) {
cout << "Can't find d2. fail\n";
if (DEBUG_CONVERT) debug_convert << "Can't find d2. fail\n";
status= PDFHummus::eFailure;
break;
}

PDFObjectCastPtr<PDFArray> arr (d_2->QueryDirectObject ("Names"));
if (!arr) {
cout << "Can't find arr. fail\n";
if (DEBUG_CONVERT) debug_convert << "Can't find arr. fail\n";
status= PDFHummus::eFailure;
break;
}
unsigned long n= arr->GetLength ();
if (n & 1) {
cout << "n is wrong\n";
if (DEBUG_CONVERT) debug_convert << "n is wrong\n";
break;
}
for (unsigned long i= 0; i < n; i+= 2) {
PDFObjectCastPtr<PDFLiteralString> name (arr->QueryObject (i));
if (!name) {
cout << "Can't find name\n";
if (DEBUG_CONVERT) debug_convert << "Can't find name\n";
status= PDFHummus::eFailure;
break;
}
PDFObjectCastPtr<PDFDictionary> arr_d1 (arr->QueryObject (i + 1));
if (!arr_d1) {
cout << "Can't find arr_d1\n";
if (DEBUG_CONVERT) debug_convert << "Can't find arr_d1\n";
status= PDFHummus::eFailure;
break;
}
PDFObjectCastPtr<PDFDictionary> arr_d2 (arr_d1->QueryDirectObject ("EF"));
if (!arr_d2) {
cout << "Can't find arr_d2\n";
if (DEBUG_CONVERT) debug_convert << "Can't find arr_d2\n";
status= PDFHummus::eFailure;
break;
}
PDFObjectCastPtr<PDFStreamInput> stream (
parser.QueryDictionaryObject (arr_d2.GetPtr (), "F"));
if (!stream) {
cout << "Can't find stream\n";
if (DEBUG_CONVERT) debug_convert << "Can't find stream\n";
status= PDFHummus::eFailure;
break;
}
Expand All @@ -102,7 +104,7 @@ get_tm_attachments_in_pdf (url pdf_path, array<url>& names) {
IByteReader* streamReader=
parser.CreateInputStreamReader (stream.GetPtr ());
if (!streamReader) {
cout << "Can't find streamReader\n";
if (DEBUG_CONVERT) debug_convert << "Can't find streamReader\n";
status= PDFHummus::eFailure;
break;
}
Expand All @@ -113,25 +115,27 @@ get_tm_attachments_in_pdf (url pdf_path, array<url>& names) {
status= attachment_file.OpenFile (
std::string (as_charp (as_string (attachment_path))));
if (status != PDFHummus::eSuccess) {
cout << "fail to open " << as_string (attachment_path) << LF;
if (DEBUG_CONVERT)
debug_convert << "fail to open " << as_string (attachment_path) << LF;
break;
}
pdfFile.GetInputStream ()->SetPosition (stream->GetStreamContentStart ());
OutputStreamTraits copy_help (
(IByteWriter*) attachment_file.GetOutputStream ());
status= copy_help.CopyToOutputStream (streamReader);
if (status != PDFHummus::eSuccess) {
cout << "Can't CopyToOutputStream\n";
if (DEBUG_CONVERT) debug_convert << "Can't CopyToOutputStream\n";
break;
}
status= attachment_file.CloseFile ();
if (status != PDFHummus::eSuccess) {
cout << "fail to close " << as_string (attachment_path) << LF;
if (DEBUG_CONVERT)
debug_convert << "fail to close " << as_string (attachment_path)
<< LF;
break;
}

names= append (attachment_path, names);
cout << "\n\n" << names << LF;
names= names * attachment_path;
delete streamReader;
}
} while (0);
Expand All @@ -140,23 +144,7 @@ get_tm_attachments_in_pdf (url pdf_path, array<url>& names) {
}

bool
get_tm_attachment_in_pdf (url pdf_path, url& name) {
array<url> names;
if (get_tm_attachments_in_pdf (pdf_path, names)) {
if (N (names) != 1) {
cout << "TODO: many attachment" << LF;
return false;
}
name= names[0];
return true;
}
else {
return false;
}
}

bool
scm_get_attachments (url pdf_path) {
array<url> attachments_paths;
return get_tm_attachments_in_pdf (pdf_path, attachments_paths);
scm_extract_attachments (url pdf_path) {
list<url> attachments_paths;
return extract_attachments_from_pdf (pdf_path, attachments_paths);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
* MODULE : pdf_hummus_get_attachment.cpp
* DESCRIPTION: Interface for getting attachment file in pdf
* MODULE : pdf_hummus_extract_attachment.cpp
* DESCRIPTION: Interface for extract attachment file in pdf
* COPYRIGHT : (C) 2023 Tangdouer
*******************************************************************************
* This software falls under the GNU general public license version 3 or later.
Expand All @@ -16,10 +16,23 @@
#include "tm_ostream.hpp"
#include "url.hpp"

bool get_tm_attachments_in_pdf (url pdf_path, array<url>& names);
/**
bool get_tm_attachment_in_pdf (url pdf_path, url& name);
Extracts attachments from a PDF file.
@param pdf_path The path of the PDF file.
@param names A reference to a list that will store the paths of extracted
attachments.
@return Returns true if the extraction is successful, false otherwise.
*/
bool extract_attachments_from_pdf (url pdf_path, list<url>& names);

bool scm_get_attachments (url pdf_path);
/**
Extracts attachments from a PDF file in a simplified way for SCM glue
operations.
@param pdf_path The path of the PDF file.
@return Returns true if the extraction is successful, false otherwise.
*/
bool scm_extract_attachments (url pdf_path);

#endif // ifdef PDF_HUMMUS_MAKE_ATTACHMENT_H
28 changes: 13 additions & 15 deletions src/Plugins/Pdf/pdf_hummus_make_attachment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
#include "pdf_hummus_make_attachment.hpp"
#include "tm_debug.hpp"

#include "PDFWriter/DictionaryContext.h"
#include "PDFWriter/ObjectsContext.h"
Expand Down Expand Up @@ -69,7 +70,7 @@ pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
as_charp (as_string (out_path)));

if (status != eSuccess) {
cout << "start fail\n";
if (DEBUG_CONVERT) debug_convert << "start fail\n";
break;
}
PDFAttachmentWriter attachmentWriter (&pdfWriter);
Expand All @@ -80,11 +81,13 @@ pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
status= tm_file_stream.Open (as_charp (attachment_path));

if (status != PDFHummus::eSuccess) {
cout << "failed to open " << attachment_path << "\n";
if (DEBUG_CONVERT)
debug_convert << "failed to open " << attachment_path << "\n";
continue;
}
else {
cout << "success to open " << attachment_path << "\n";
if (DEBUG_CONVERT)
debug_convert << "success to open " << attachment_path << "\n";
}

LongFilePositionType file_size= tm_file_stream.GetFileSize ();
Expand All @@ -99,34 +102,29 @@ pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
status= attachmentWriter.AttachToAllPage (aAttachment);
// return status;
if (status != eSuccess) {
cout << "fail to attach " << attachment_path << "\n";
if (DEBUG_CONVERT)
debug_convert << "fail to attach " << attachment_path << "\n";
continue;
}
else {
cout << "success to attach " << attachment_path << "\n";
if (DEBUG_CONVERT)
debug_convert << "success to attach " << attachment_path << "\n";
continue;
}
}
status= pdfWriter.EndPDF ();
} while (false);

if (eSuccess == status) {
cout << "Succeeded in creating PDF file\n";
if (DEBUG_CONVERT) debug_convert << "Succeeded in creating PDF file\n";
return true;
}
else {
cout << "Failed in creating PDF file\n";
if (DEBUG_CONVERT) debug_convert << "Failed in creating PDF file\n";
return false;
}
}

bool
pdf_hummus_make_attachment (url pdf_path, url attachment_path, url out_path) {
if ((suffix (pdf_path) == "pdf"))
return pdf_hummus_make_attachments (pdf_path, list<url> (attachment_path),
out_path);
else return false;
}
PDFAttachment::PDFAttachment (void) {
file_content= NULL;
lenth = 0;
Expand All @@ -136,7 +134,7 @@ PDFAttachment::PDFAttachment (string attachment_path) {
InputFileStream tm_file_stream;
EStatusCode status= tm_file_stream.Open (as_charp (attachment_path));
if (status != PDFHummus::eSuccess) {
cout << "failed to open tm\n";
if (DEBUG_CONVERT) debug_convert << "failed to open tm\n";
return;
}

Expand Down
12 changes: 9 additions & 3 deletions src/Plugins/Pdf/pdf_hummus_make_attachment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
#include "tm_ostream.hpp"
#include "url.hpp"

/**
Embeds attachments into a PDF file.
@param pdf_path The path of the PDF file where attachments need to be embedded.
@param attachment_path A list that specifies the paths of attachments to be
embedded.
@param out_path The path of the new PDF file with the embedded attachments.
@return Returns true if the embedding is successful, false otherwise.
*/
bool pdf_hummus_make_attachments (url pdf_path, list<url> attachment_path,
url out_path);

bool pdf_hummus_make_attachment (url pdf_path, url attachment_path,
url out_path);

#endif // ifdef PDF_HUMMUS_MAKE_ATTACHMENT_H
4 changes: 2 additions & 2 deletions src/Scheme/Plugins/glue_pdf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function main()
ret_type = "string"
},
{
scm_name = "get-attachments",
cpp_name = "scm_get_attachments",
scm_name = "extract-attachments",
cpp_name = "scm_extract_attachments",
ret_type = "bool",
arg_list = {
"url"
Expand Down
2 changes: 1 addition & 1 deletion src/Scheme/Plugins/init_glue_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pdfhummus_version () {

#include "glue_ghostscript.cpp"

#include "Pdf/pdf_hummus_get_attachment.hpp"
#include "Pdf/pdf_hummus_extract_attachment.hpp"
#include "glue_pdf.cpp"

void
Expand Down
4 changes: 2 additions & 2 deletions tests/Plugins/Pdf/pdf_make_attachment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ private slots:
void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_attachment () {

bool attach_judge= pdf_hummus_make_attachment (
bool attach_judge= pdf_hummus_make_attachments (
url ("$TEXMACS_PATH/tests/images/29_1_1.pdf"),
url ("$TEXMACS_PATH/tests/29_1_1.tm"),
list<url> (url ("$TEXMACS_PATH/tests/29_1_1.tm")),
url ("$TEXMACS_PATH/tests/images/29_1_1_attach.pdf"));
QVERIFY (attach_judge);

Expand Down

0 comments on commit 6849454

Please sign in to comment.