diff --git a/TeXmacs/progs/texmacs/texmacs/tm-files.scm b/TeXmacs/progs/texmacs/texmacs/tm-files.scm index 49a577f32e..e208cdeb3d 100644 --- a/TeXmacs/progs/texmacs/texmacs/tm-files.scm +++ b/TeXmacs/progs/texmacs/texmacs/tm-files.scm @@ -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)))) diff --git a/src/Plugins/Pdf/pdf_hummus_get_attachment.cpp b/src/Plugins/Pdf/pdf_hummus_extract_attachment.cpp similarity index 71% rename from src/Plugins/Pdf/pdf_hummus_get_attachment.cpp rename to src/Plugins/Pdf/pdf_hummus_extract_attachment.cpp index b2d20496be..80b01baab6 100644 --- a/src/Plugins/Pdf/pdf_hummus_get_attachment.cpp +++ b/src/Plugins/Pdf/pdf_hummus_extract_attachment.cpp @@ -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. @@ -23,17 +23,19 @@ using namespace PDFHummus; #include 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& names) { +extract_attachments_from_pdf (url pdf_path, list& 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 ()); @@ -41,59 +43,59 @@ get_tm_attachments_in_pdf (url pdf_path, array& names) { 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 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 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 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 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 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 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 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; } @@ -102,7 +104,7 @@ get_tm_attachments_in_pdf (url pdf_path, array& 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; } @@ -113,7 +115,8 @@ get_tm_attachments_in_pdf (url pdf_path, array& 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 ()); @@ -121,17 +124,18 @@ get_tm_attachments_in_pdf (url pdf_path, array& names) { (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); @@ -140,23 +144,7 @@ get_tm_attachments_in_pdf (url pdf_path, array& names) { } bool -get_tm_attachment_in_pdf (url pdf_path, url& name) { - array 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 attachments_paths; - return get_tm_attachments_in_pdf (pdf_path, attachments_paths); +scm_extract_attachments (url pdf_path) { + list attachments_paths; + return extract_attachments_from_pdf (pdf_path, attachments_paths); } diff --git a/src/Plugins/Pdf/pdf_hummus_get_attachment.hpp b/src/Plugins/Pdf/pdf_hummus_extract_attachment.hpp similarity index 53% rename from src/Plugins/Pdf/pdf_hummus_get_attachment.hpp rename to src/Plugins/Pdf/pdf_hummus_extract_attachment.hpp index efdbe8673e..5649030512 100644 --- a/src/Plugins/Pdf/pdf_hummus_get_attachment.hpp +++ b/src/Plugins/Pdf/pdf_hummus_extract_attachment.hpp @@ -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. @@ -16,10 +16,23 @@ #include "tm_ostream.hpp" #include "url.hpp" -bool get_tm_attachments_in_pdf (url pdf_path, array& 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& 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 diff --git a/src/Plugins/Pdf/pdf_hummus_make_attachment.cpp b/src/Plugins/Pdf/pdf_hummus_make_attachment.cpp index 36bbb23979..b7614f7e9c 100644 --- a/src/Plugins/Pdf/pdf_hummus_make_attachment.cpp +++ b/src/Plugins/Pdf/pdf_hummus_make_attachment.cpp @@ -8,6 +8,7 @@ * in the root directory or . ******************************************************************************/ #include "pdf_hummus_make_attachment.hpp" +#include "tm_debug.hpp" #include "PDFWriter/DictionaryContext.h" #include "PDFWriter/ObjectsContext.h" @@ -69,7 +70,7 @@ pdf_hummus_make_attachments (url pdf_path, list 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); @@ -80,11 +81,13 @@ pdf_hummus_make_attachments (url pdf_path, list 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 (); @@ -99,11 +102,13 @@ pdf_hummus_make_attachments (url pdf_path, list 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; } } @@ -111,22 +116,15 @@ pdf_hummus_make_attachments (url pdf_path, list attachment_paths, } 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 (attachment_path), - out_path); - else return false; -} PDFAttachment::PDFAttachment (void) { file_content= NULL; lenth = 0; @@ -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; } diff --git a/src/Plugins/Pdf/pdf_hummus_make_attachment.hpp b/src/Plugins/Pdf/pdf_hummus_make_attachment.hpp index 25d7392693..3049d9de8f 100644 --- a/src/Plugins/Pdf/pdf_hummus_make_attachment.hpp +++ b/src/Plugins/Pdf/pdf_hummus_make_attachment.hpp @@ -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 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 diff --git a/src/Scheme/Plugins/glue_pdf.lua b/src/Scheme/Plugins/glue_pdf.lua index 15bc84cb89..34978e8ff9 100644 --- a/src/Scheme/Plugins/glue_pdf.lua +++ b/src/Scheme/Plugins/glue_pdf.lua @@ -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" diff --git a/src/Scheme/Plugins/init_glue_plugins.cpp b/src/Scheme/Plugins/init_glue_plugins.cpp index 38c5a08b15..b4cb888674 100644 --- a/src/Scheme/Plugins/init_glue_plugins.cpp +++ b/src/Scheme/Plugins/init_glue_plugins.cpp @@ -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 diff --git a/tests/Plugins/Pdf/pdf_make_attachment_test.cpp b/tests/Plugins/Pdf/pdf_make_attachment_test.cpp index 495b85d9ae..80c9aa4782 100644 --- a/tests/Plugins/Pdf/pdf_make_attachment_test.cpp +++ b/tests/Plugins/Pdf/pdf_make_attachment_test.cpp @@ -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 ("$TEXMACS_PATH/tests/29_1_1.tm")), url ("$TEXMACS_PATH/tests/images/29_1_1_attach.pdf")); QVERIFY (attach_judge);