Skip to content

Commit

Permalink
[29_6] add pdf format and conversion mechanism betwen tm and pdf
Browse files Browse the repository at this point in the history
## Why you open this Pull Request?
Previously, there were many drawbacks to opening PDFs through
truncation, such as not being able to save properly, and detaching
attachments may overwrite other files. These problems can be perfectly
solved by adding pdf format and its conversion mechanism

## What work have you done in the current Pull Request?
add pdf format and conversion mechanism betwen tm and pdf
tangdouer1005 authored Sep 25, 2023
1 parent 2d529ca commit 28a3463
Showing 16 changed files with 526 additions and 147 deletions.
6 changes: 1 addition & 5 deletions TeXmacs/progs/texmacs/texmacs/tm-files.scm
Original file line number Diff line number Diff line change
@@ -151,7 +151,7 @@
(learn-interactive 'recent-buffer (list (cons "0" (url->unix name)))))

(define (has-faithful-format? name)
(in? (url-suffix name) '("tm" "ts" "tp" "stm" "tmml" "scm" "")))
(in? (url-suffix name) '("tm" "ts" "tp" "stm" "tmml" "scm" "pdf" "")))

(define (save-buffer-post name opts)
;;(display* "save-buffer-post " name "\n")
@@ -512,10 +512,6 @@
(if (current-buffer)
(set! name (url-relative (current-buffer) name))
(set! name (url-append (url-pwd) 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))))
(load-buffer-check-autosave name opts))

(tm-define (load-buffer name . opts)
23 changes: 23 additions & 0 deletions TeXmacs/tests/29_4_2multiple-files/main_convert_path.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<TeXmacs|2.1.2>

<style|myslides>

<\body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.

\;

<image|logo.pdf|0.3par|||>
</body>

<\initial>
<\collection>
<associate|page-medium|paper>
</collection>
</initial>
Binary file added TeXmacs/tests/images/29_5_1.pdf
Binary file not shown.
12 changes: 12 additions & 0 deletions TeXmacs/tests/images/29_5_1.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<TeXmacs|2.1.2>

<style|<tuple|generic|british>>

<\initial>
<\collection>
<associate|global-author|>
<associate|global-subject|>
<associate|global-title|29_1_1.tm>
<associate|page-medium|paper>
</collection>
</initial>
Binary file added TeXmacs/tests/images/29_5_2.pdf
Binary file not shown.
26 changes: 26 additions & 0 deletions TeXmacs/tests/images/29_5_2.tm
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<TeXmacs|2.1.2>

<style|tsts/myslides>

<\body>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
commodo consequat. Duis aute irure dolor in reprehenderit in voluptate
velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat
cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id
est laborum.

\;

<image|p/logo.pdf|0.3par|||>
</body>

<\initial>
<\collection>
<associate|global-author|>
<associate|global-subject|>
<associate|global-title|main.tm>
<associate|page-medium|paper>
</collection>
</initial>
63 changes: 63 additions & 0 deletions plugins/pdf/progs/init-pdf.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; MODULE : init-pdf.scm
;; DESCRIPTION : setup pdf converters
;; COPYRIGHT : (C) 2023 tangdouer
;;
;; This software falls under the GNU general public license version 3 or later.
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(texmacs-module (pdf-format))


(define (texmacs->pdf x . opts)
(let* ((tem-dir (url-temp-dir))
(tem-pdf (string->url (string-append (url->string tem-dir) "/tem.pdf")))
(tem-tm (string->url (string-append (url->string tem-dir) "/tem.tm")))
(tem-tm-out (open-output-file (url->string tem-tm)))
(url-list (pdf-get-linked-file-paths x buffer-get)))
(display* tem-dir "\n")
(display (serialize-texmacs (pdf-replace-linked-path x buffer-get)) tem-tm-out)
(close-output-port tem-tm-out)

(let* ((cur (current-buffer))
(buf (buffer-new)))
(buffer-set-master buf cur)
(switch-to-buffer buf)
(load-buffer tem-tm)
(set-drd cur)
(dynamic-make-slides)
(print-to-file tem-pdf)
(switch-to-buffer cur)
(buffer-close buf))
;;attachment-list need be generated
(if (pdf-make-attachments tem-pdf url-list tem-pdf)
(display* "pdf-make-attachments return true\n")
(display* "pdf-make-attachments return false\n"))
(string-load tem-pdf)))

(define (pdf->texmacs x . opts)
(let* ((tem-dir (url-temp-dir))
(tem-pdf (string->url (string-append (url->string tem-dir) "/tem.pdf")))
(tem-tm (string->url (string-append (url->string tem-dir) "/tem.tm")))
(tem-pdf-out (open-output-file (url->string tem-pdf))))
(display* tem-dir "\n")
(display x tem-pdf-out)
(close-output-port tem-pdf-out)
(if (extract-attachments tem-pdf)
(noop)
(display* "extract-attachments return false\n"))
(set! tem-tm (url-relative tem-tm (pdf-get-attached-main-tm tem-pdf)))

(if (url-exists? tem-tm)
(display* tem-tm " exist\n")
(display* tem-tm " not exist\n"))
(pdf-replace-linked-path (tree-import tem-tm (url-format tem-tm)) tem-pdf)))

(converter texmacs-tree pdf-document
(:function texmacs->pdf))

(converter pdf-document texmacs-tree
(:function pdf->texmacs))
109 changes: 7 additions & 102 deletions src/Edit/Editor/edit_main.cpp
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@

#ifdef USE_PLUGIN_PDF
#include "Pdf/pdf_hummus_make_attachment.hpp"
#include "Pdf/pdf_hummus_extract_attachment.hpp"
#endif

#ifdef EXPERIMENTAL
@@ -320,41 +321,6 @@ edit_main_rep::print_doc (url name, bool conform, int first, int last) {
}
#endif
}
static hashset<string> internal_styles;

static void
declare_style (url u) {
if (is_or (u)) {
declare_style (u[1]);
declare_style (u[2]);
}
else if (is_concat (u)) {
string dir= upcase_first (as_string (u[1]));
if (dir == "CVS" || dir == ".svn");
else declare_style (u[2]);
}
else if (is_atomic (u)) {
string s= as_string (u);
if (ends (s, ".ts") && !starts (s, "source")) {
internal_styles->insert (s(0,N(s)-3));
if (starts (s, "old-"))
internal_styles->insert (s(4,N(s)-3));
if (starts (s, "old2-"))
internal_styles->insert (s(5,N(s)-3));
}
}
}

static bool
is_internal_style (string style) {
return true;

if (N (internal_styles) == 0) {
url sty_u= descendance ("$TEXMACS_STYLE_ROOT");
declare_style (sty_u);
}
return internal_styles->contains (style);
}

void
edit_main_rep::print_to_file (url name, string first, string last) {
@@ -394,78 +360,17 @@ edit_main_rep::attach_doc_to_exported_pdf (url pdf_name) {
set_buffer_tree (new_u, new_t);
new_t= get_buffer_tree (new_u);

// dfs search all style and link
list<tree> st (new_t);
list<url> tm_and_linked_file (new_u);
while (N (st) != 0) {
auto la= last_item (st);
st = suppress_last (st);
for (int i= 0; i < arity (la); i++) {
if (is_compound (la[i])) {
string label= get_label (la[i]);
if (label == "image" || label == "include") {
if (is_atomic (la[i][0])) {
url pre_url= url (la[i][0]->label);
if (!exists (pre_url)) {
pre_url= head (cur_u) * pre_url;
if (!exists (pre_url)) {
debug_convert << pre_url << "do not exist" << LF;
}
}
tm_and_linked_file= tm_and_linked_file * pre_url;
string name = as_string (tail (pre_url));
la[i][0]->label = string (name);
}
}
else if (label == "style") {
if (is_func (la[i][0], TUPLE)) {
for (int j= 0; j < N (la[i][0]); j++) {
string style_name= get_label (la[i][0][j]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= head (cur_u) * style_url;
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
}
}
tm_and_linked_file= tm_and_linked_file * style_url;
string name = basename (style_url);
la[i][0][j]->label= name;
}
}
}
else {
if (!is_atomic (la[i][0])) {
debug_convert << get_label (la[i][0]) << "is not atomic tree" << LF;
}
string style_name= get_label (la[i][0]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= head (cur_u) * style_url;
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
}
}
tm_and_linked_file= tm_and_linked_file * style_url;
string name= basename (style_url);
la[i][0]->label= name;
}
}
}
else st= st * la[i];
}
}
}
array<url> tm_and_linked_file = get_linked_file_paths(new_t, cur_u);
new_t = replace_with_relative_path(new_t, cur_u);
array<url> attachments;
attachments = append(tm_and_linked_file, attachments);
attachments = append(new_u, attachments);

set_buffer_tree (new_u, new_t);
buffer_save (new_u);
new_t= get_buffer_tree (new_u);

if (!pdf_hummus_make_attachments (pdf_name, tm_and_linked_file, pdf_name)) {
if (!pdf_hummus_make_attachments (pdf_name, attachments, pdf_name)) {
debug_convert << "fail : pdf_hummus_make_attachments" << LF;
return false;
}
208 changes: 201 additions & 7 deletions src/Plugins/Pdf/pdf_hummus_extract_attachment.cpp
Original file line number Diff line number Diff line change
@@ -7,9 +7,14 @@
* It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
#include "PDFWriter/PDFWriter.h"
#include "PDFWriter/SafeBufferMacrosDefs.h"
#include "PDFWriter/Trace.h"
#include "pdf_hummus_extract_attachment.hpp"
#include "analyze.hpp"
#include "file.hpp"
#include "new_buffer.hpp"
#include "sys_utils.hpp"
#include "tm_debug.hpp"
#include "tm_url.hpp"
#include "tree_helper.hpp"

#include "PDFWriter/InputFileStream.h"
#include "PDFWriter/OutputBufferedStream.h"
@@ -20,11 +25,11 @@
#include "PDFWriter/PDFObjectCast.h"
#include "PDFWriter/PDFParser.h"
#include "PDFWriter/PDFStreamInput.h"
#include "PDFWriter/PDFWriter.h"
#include "PDFWriter/SafeBufferMacrosDefs.h"
#include "PDFWriter/Trace.h"
using namespace PDFHummus;
#include <utility>
using namespace IOBasicTypes;
#include "pdf_hummus_extract_attachment.hpp"
#include "tm_debug.hpp"

bool
extract_attachments_from_pdf (url pdf_path, list<url>& names) {
@@ -155,5 +160,194 @@ extract_attachments_from_pdf (url pdf_path, list<url>& names) {
bool
scm_extract_attachments (url pdf_path) {
list<url> attachments_paths;
return extract_attachments_from_pdf (pdf_path, attachments_paths);
bool ret= extract_attachments_from_pdf (pdf_path, attachments_paths);
return ret;
}
static hashset<string> internal_styles;

static void
declare_style (url u) {
if (is_or (u)) {
declare_style (u[1]);
declare_style (u[2]);
}
else if (is_concat (u)) {
string dir= upcase_first (as_string (u[1]));
if (dir == "CVS" || dir == ".svn")
;
else declare_style (u[2]);
}
else if (is_atomic (u)) {
string s= as_string (u);
if (ends (s, ".ts") && !starts (s, "source")) {
internal_styles->insert (s (0, N (s) - 3));
if (starts (s, "old-")) internal_styles->insert (s (4, N (s) - 3));
if (starts (s, "old2-")) internal_styles->insert (s (5, N (s) - 3));
}
}
}

static bool
is_internal_style (string style) {
if (N (internal_styles) == 0) {
url sty_u= descendance ("$TEXMACS_PATH/styles");
declare_style (sty_u);
sty_u= descendance ("$TEXMACS_PATH/packages");
declare_style (sty_u);
}
return internal_styles->contains (style);
}

array<url>
get_linked_file_paths (tree t, url path) {
list<tree> st (t);
array<url> tm_and_linked_file;
while (N (st) != 0) {
auto la= last_item (st);
st = suppress_last (st);
for (int i= 0; i < arity (la); i++) {
if (is_compound (la[i])) {
string label= get_label (la[i]);
if (label == "image" || label == "include") {
if (is_atomic (la[i][0])) {
url pre_url= url (get_label (la[i][0]));
if (!exists (pre_url)) {
pre_url= relative (path, pre_url);
if (!exists (pre_url)) {
debug_convert << pre_url << "do not exist" << LF;
}
}
tm_and_linked_file= append (pre_url, tm_and_linked_file);
}
}
else if (label == "style") {
if (get_label (la[i][0]) == "tuple") {
for (int j= 0; j < N (la[i][0]); j++) {
string style_name= get_label (la[i][0][j]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= relative (path, style_url);
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
continue;
}
}
tm_and_linked_file= append (style_url, tm_and_linked_file);
}
}
}
else {
if (!is_atomic (la[i][0])) {
debug_convert << get_label (la[i][0]) << "is not atomic tree"
<< LF;
}
string style_name= get_label (la[i][0]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= relative (path, style_url);
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
continue;
}
}
tm_and_linked_file= append (style_url, tm_and_linked_file);
}
}
}
else st= st * la[i];
}
}
}

return tm_and_linked_file;
}

tree
replace_with_relative_path (tree t, url path) {
// dfs search all style and link
list<tree> st (t);
list<url> tm_and_linked_file;
// url cur_u = get_current_buffer ();
while (N (st) != 0) {
auto la= last_item (st);
st = suppress_last (st);
for (int i= 0; i < arity (la); i++) {
if (is_compound (la[i])) {
string label= get_label (la[i]);
if (label == "image" || label == "include") {
if (is_atomic (la[i][0])) {
url pre_url= url (get_label (la[i][0]));
if (!exists (pre_url)) {
pre_url= relative (path, pre_url);
if (!exists (pre_url)) {
debug_convert << pre_url << "do not exist" << LF;
}
}
string name= as_string (tail (pre_url));
if (path != url ()) {
name= as_string (relative (path, name));
}
la[i][0]->label= string (name);
}
}
else if (label == "style") {
if (is_tuple (la[i][0])) {
for (int j= 0; j < N (la[i][0]); j++) {
string style_name= get_label (la[i][0][j]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= relative (path, style_url);
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
}
}
string name= basename (style_url);
if (path != url ()) {
name= as_string (relative (path, name));
}
la[i][0][j]->label= name;
}
}
}
else {
if (!is_atomic (la[i][0])) {
debug_convert << get_label (la[i][0]) << "is not atomic tree"
<< LF;
}
string style_name= get_label (la[i][0]);
if (!is_internal_style (style_name)) {
url style_url= url (style_name);
style_url = glue (style_url, ".ts");
if (!exists (style_url)) {
style_url= relative (path, style_url);
if (!exists (style_url)) {
debug_convert << style_url << "do not exist" << LF;
}
}
string name= basename (style_url);
if (path != url ()) {
name= as_string (relative (path, name));
}
la[i][0]->label= name;
}
}
}
else st= st * la[i];
}
}
}
return t;
}

url
get_main_tm (url pdf_path) {
list<url> attachments_paths;
bool ret= extract_attachments_from_pdf (pdf_path, attachments_paths);
return attachments_paths[0];
}
49 changes: 46 additions & 3 deletions src/Plugins/Pdf/pdf_hummus_extract_attachment.hpp
Original file line number Diff line number Diff line change
@@ -14,11 +14,12 @@
#include "list.hpp"
#include "string.hpp"
#include "tm_ostream.hpp"
#include "tree.hpp"
#include "url.hpp"

/**
Extracts attachments from a PDF file.
@brief 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.
@@ -28,11 +29,53 @@ bool extract_attachments_from_pdf (url pdf_path, list<url>& names);

/**
Extracts attachments from a PDF file in a simplified way for SCM glue
operations.
@brief Extracts attachments from a PDF file in a simplified way for SCM
glueoperations.
@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);

/**
@brief Retrieves the absolute paths of linked files in the texmacs tree based on
the specified directory path.
@param t The texmacs tree to be traversed.
@param path The directory path where the tree's TM file is located.
@return An array of URLs representing the absolute paths of linked files.
@note
This function performs a depth-first traversal to search for the URLs of all
linked files in the given t tree. It generates the absolute paths of these
linked files based on the specified path where the tree's TM file is located.
*/
array<url> get_linked_file_paths (tree t, url path);

/**
@brief Replaces the relative paths of linked files in the texmacs tree with the
absolute path relative to the specified directory.
@param t The texmacs tree to be traversed.
@param path The target directory path.
@return The tree after the replacements.
@note
This function searches for the URLs of all linked files in the given t tree
using a depth-first traversal. It replaces these URLs by converting them to be
relative to the path directory.
*/
tree replace_with_relative_path (tree t, url path);

/**
@brief Retrieves the main TM (TeXmacs) file embedded in a PDF, based on the
provided PDF path.
@param pdf_path The path to the PDF file containing the embedded TM documents.
@return The URL of the main TM file embedded in the PDF.
@note
This function retrieves the main TM (TeXmacs) file from a PDF file that contains
embedded TM documents. The first file in the array of embedded files is
considered as the main TM file, due to the mechanism of embedding TM documents
in Mogan.
*/
url get_main_tm (url pdf_path);

#endif // ifdef PDF_HUMMUS_MAKE_ATTACHMENT_H
59 changes: 40 additions & 19 deletions src/Plugins/Pdf/pdf_hummus_make_attachment.cpp
Original file line number Diff line number Diff line change
@@ -8,23 +8,21 @@
* in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
******************************************************************************/
#include "pdf_hummus_make_attachment.hpp"
#include "file.hpp"
#include "tm_debug.hpp"

#include "PDFWriter/DictionaryContext.h"
#include "PDFWriter/InputFileStream.h"
#include "PDFWriter/ObjectsContext.h"
#include "PDFWriter/OutputBufferedStream.h"
#include "PDFWriter/OutputFileStream.h"
#include "PDFWriter/PDFRectangle.h"
#include "PDFWriter/PDFStream.h"
#include "PDFWriter/PDFWriter.h"
#include "PDFWriter/SafeBufferMacrosDefs.h"
#include "PDFWriter/Trace.h"

#include "PDFWriter/InputFileStream.h"
#include "PDFWriter/OutputBufferedStream.h"
#include "PDFWriter/OutputFileStream.h"
#include "file.hpp"
#include "tm_debug.hpp"
using namespace PDFHummus;

using namespace IOBasicTypes;

class PDFAttachment {
public:
PDFAttachment (void);
@@ -57,12 +55,13 @@ class PDFAttachmentWriter : public DocumentContextExtenderAdapter {
private:
PDFWriter* mPDFWriter;
PDFAttachmentToObjectIDTypeMap mAttachment;
void ListenOnCatalogWrite ();
EStatusCodeAndObjectIDType WriteAttachment (PDFAttachment* inAttachment);
void CleanupAttachment ();

void ListenOnCatalogWrite ();
EStatusCodeAndObjectIDType WriteAttachment (PDFAttachment* inAttachment);
void CleanupAttachment ();
};
bool
pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
pdf_hummus_make_attachments (url pdf_path, array<url> attachment_paths,
url out_path) {
if (N (attachment_paths) == 0) {
if (DEBUG_CONVERT) debug_convert << "N (attachment_paths) is 0\n";
@@ -79,11 +78,6 @@ pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
if (DEBUG_CONVERT) debug_convert << pdf_path << " is not regular\n";
return false;
}
if (is_regular (out_path)) {
if (DEBUG_CONVERT)
debug_convert << out_path << " has existed, causing coverage\n";
return false;
}

PDFWriter pdfWriter;
EStatusCode status;
@@ -97,7 +91,6 @@ pdf_hummus_make_attachments (url pdf_path, list<url> attachment_paths,
break;
}
PDFAttachmentWriter attachmentWriter (&pdfWriter);

for (int i= 0; i < (int) N (attachment_paths); i++) {
string attachment_path= as_string (attachment_paths[i]);
InputFileStream tm_file_stream;
@@ -216,10 +209,38 @@ PDFAttachmentWriter::OnCatalogWrite (
dictionaryContext_0->WriteKey ("Names");
inPDFWriterObjectContext->StartArray ();

iterator<PDFAttachment*> it= iterate (mAttachment);
iterator<PDFAttachment*> it_0 = iterate (mAttachment);
ObjectIDType the_main_tm_id= 999999999;
PDFAttachment* the_main_tm = NULL;
while (it_0->busy ()) {
PDFAttachment* cur_attachment= it_0->next ();
if (the_main_tm_id > mAttachment (cur_attachment)) {
the_main_tm = cur_attachment;
the_main_tm_id= mAttachment (cur_attachment);
}
}
if (the_main_tm != NULL) {
inPDFWriterObjectContext->WriteLiteralString (as_charp (the_main_tm->name));
DictionaryContext* dictionaryContext_2=
inPDFWriterObjectContext->StartDictionary ();
dictionaryContext_2->WriteKey ("EF");
DictionaryContext* dictionaryContext_3=
inPDFWriterObjectContext->StartDictionary ();
dictionaryContext_3->WriteKey ("F");
inPDFWriterObjectContext->WriteIndirectObjectReference (
mAttachment[the_main_tm]);
inPDFWriterObjectContext->EndDictionary (dictionaryContext_3);
dictionaryContext_2->WriteKey ("F");
dictionaryContext_2->WriteLiteralStringValue (as_charp (the_main_tm->name));
dictionaryContext_2->WriteKey ("Type");
dictionaryContext_2->WriteNameValue ("F");
inPDFWriterObjectContext->EndDictionary (dictionaryContext_2);
}

iterator<PDFAttachment*> it= iterate (mAttachment);
while (it->busy ()) {
PDFAttachment* cur_attachment= it->next ();
if (cur_attachment == the_main_tm) continue;
inPDFWriterObjectContext->WriteLiteralString (
as_charp (cur_attachment->name));
DictionaryContext* dictionaryContext_2=
2 changes: 1 addition & 1 deletion src/Plugins/Pdf/pdf_hummus_make_attachment.hpp
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ 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,
bool pdf_hummus_make_attachments (url pdf_path, array<url> attachment_path,
url out_path);

#endif // ifdef PDF_HUMMUS_MAKE_ATTACHMENT_H
5 changes: 5 additions & 0 deletions src/Scheme/L3/glue_lolly.lua
Original file line number Diff line number Diff line change
@@ -853,6 +853,11 @@ function main()
cpp_name = "url_temp",
ret_type = "url"
},
{
scm_name = "url-temp-dir",
cpp_name = "url_temp_dir",
ret_type = "url"
},
{
scm_name = "system-move",
cpp_name = "move",
36 changes: 36 additions & 0 deletions src/Scheme/Plugins/glue_pdf.lua
Original file line number Diff line number Diff line change
@@ -33,6 +33,42 @@ function main()
"url"
}
},
{
scm_name = "pdf-make-attachments",
cpp_name = "pdf_hummus_make_attachments",
ret_type = "bool",
arg_list = {
"url",
"array_url",
"url"
}
},
{
scm_name = "pdf-get-linked-file-paths",
cpp_name = "get_linked_file_paths",
ret_type = "array_url",
arg_list = {
"tree",
"url"
}
},
{
scm_name = "pdf-replace-linked-path",
cpp_name = "replace_with_relative_path",
ret_type = "tree",
arg_list = {
"tree",
"url"
}
},
{
scm_name = "pdf-get-attached-main-tm",
cpp_name = "get_main_tm",
ret_type = "url",
arg_list = {
"url"
}
},
}
}
end
1 change: 1 addition & 0 deletions src/Scheme/Plugins/init_glue_plugins.cpp
Original file line number Diff line number Diff line change
@@ -71,6 +71,7 @@ pdfhummus_version () {

#ifdef USE_PLUGIN_PDF
#include "Pdf/pdf_hummus_extract_attachment.hpp"
#include "Pdf/pdf_hummus_make_attachment.hpp"
#include "glue_pdf.cpp"
#endif

74 changes: 64 additions & 10 deletions tests/Plugins/Pdf/pdf_make_attachment_test.cpp
Original file line number Diff line number Diff line change
@@ -10,28 +10,34 @@

#include "Pdf/pdf_hummus_extract_attachment.hpp"
#include "Pdf/pdf_hummus_make_attachment.hpp"
#include "base.hpp"
#include "convert.hpp"
#include "file.hpp"
#include "sys_utils.hpp"
#include "tm_file.hpp"
#include "tree_helper.hpp"
#include <QtTest/QtTest>

class TestHummusPdfMakeAttachment : public QObject {
Q_OBJECT

private slots:
void init () { lolly::init_tbox (); }
void init () { init_lolly (); }
void test_pdf_hummus_make_single_attachment ();
void test_pdf_hummus_make_multiple_attachments ();
void test_pdf_hummus_make_zero_attachment ();
void test_pdf_hummus_make_attachment_for_wrong_pdf ();
void test_pdf_hummus_make_attachment_for_no_pdf ();
void test_get_linked_file_paths ();
void test_replace_with_relative_path ();
void test_get_main_tm ();
};
void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_single_attachment () {

array<url> attachmen_0;
attachmen_0 << url ("$TEXMACS_PATH/tests/29_1_1.tm");
bool attach_judge= pdf_hummus_make_attachments (
url ("$TEXMACS_PATH/tests/images/29_1_1.pdf"),
list<url> (url ("$TEXMACS_PATH/tests/29_1_1.tm")),
url ("$TEXMACS_PATH/tests/images/29_1_1.pdf"), attachmen_0,
url ("$TEXMACS_PATH/tests/images/29_1_1_attach.pdf"));
QVERIFY (attach_judge);
bool out_pdf_judge=
@@ -58,7 +64,7 @@ TestHummusPdfMakeAttachment::test_pdf_hummus_make_single_attachment () {
void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_multiple_attachments () {

auto multiple_tm= list<url> (
auto multiple_tm= array<url> (
url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main.tm"),
url ("$TEXMACS_PATH/tests/29_4_2multiple-files/tsts/myslides.ts"),
url ("$TEXMACS_PATH/tests/29_4_2multiple-files/p/logo.pdf"));
@@ -107,7 +113,7 @@ TestHummusPdfMakeAttachment::test_pdf_hummus_make_multiple_attachments () {
void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_zero_attachment () {
bool attach_judge= pdf_hummus_make_attachments (
url ("$TEXMACS_PATH/tests/images/29_4_3.pdf"), list<url> (),
url ("$TEXMACS_PATH/tests/images/29_4_3.pdf"), array<url> (),
url ("$TEXMACS_PATH/tests/images/29_4_3_attach.pdf"));
QVERIFY (!attach_judge);
bool out_pdf_judge=
@@ -123,9 +129,10 @@ TestHummusPdfMakeAttachment::test_pdf_hummus_make_zero_attachment () {

void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_attachment_for_wrong_pdf () {
array<url> attachment_0;
attachment_0 << url ("$TEXMACS_PATH/tests/29_1_1.tm");
bool attach_judge= pdf_hummus_make_attachments (
url ("$TEXMACS_PATH/tests/images/29_4_4.pdf"),
list<url> (url ("$TEXMACS_PATH/tests/29_1_1.tm")),
url ("$TEXMACS_PATH/tests/images/29_4_4.pdf"), attachment_0,
url ("$TEXMACS_PATH/tests/images/29_4_4_attach.pdf"));
QVERIFY (!attach_judge);
bool out_pdf_judge=
@@ -136,14 +143,61 @@ TestHummusPdfMakeAttachment::test_pdf_hummus_make_attachment_for_wrong_pdf () {
void
TestHummusPdfMakeAttachment::test_pdf_hummus_make_attachment_for_no_pdf () {
QVERIFY (!is_regular (url ("$TEXMACS_PATH/tests/images/29_4_5.pdf")));
array<url> attachment_0;
attachment_0 << url ("$TEXMACS_PATH/tests/29_1_1.tm");
bool attach_judge= pdf_hummus_make_attachments (
url ("$TEXMACS_PATH/tests/images/29_4_5.pdf"),
list<url> (url ("$TEXMACS_PATH/tests/29_1_1.tm")),
url ("$TEXMACS_PATH/tests/images/29_4_5.pdf"), attachment_0,
url ("$TEXMACS_PATH/tests/images/29_4_5_attach.pdf"));
QVERIFY (!attach_judge);
bool out_pdf_judge=
is_regular (url ("$TEXMACS_PATH/tests/images/29_4_5_attach.pdf"));
QVERIFY (!out_pdf_judge);
}

void
TestHummusPdfMakeAttachment::test_get_linked_file_paths () {
array<url> attachment_0;
attachment_0 << url ("$TEXMACS_PATH/tests/29_1_1.tm");
string texmacs_doc_1= string_load (url ("$TEXMACS_PATH/tests/29_1_1.tm"));
tree texmacs_tree_1= texmacs_to_tree (texmacs_doc_1);
array<url> linked_0 = get_linked_file_paths (
texmacs_tree_1, url ("$TEXMACS_PATH/tests/29_1_1.tm"));
QVERIFY (N (linked_0) == 0);

string texmacs_doc_2=
string_load (url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main.tm"));
tree texmacs_tree_2= texmacs_to_tree (texmacs_doc_2);
array<url> linked = get_linked_file_paths (
texmacs_tree_2, url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main.tm"));
QVERIFY (N (linked) == 2);
QVERIFY (is_regular (linked[0]));
QVERIFY (tail (linked[0]) == url ("logo.pdf"));
QVERIFY (is_regular (linked[1]));
QVERIFY (tail (linked[1]) == url ("myslides.ts"));
}
void
TestHummusPdfMakeAttachment::test_replace_with_relative_path () {
string texmacs_doc_1=
string_load (url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main.tm"));
tree texmacs_tree_1= texmacs_to_tree (texmacs_doc_1);

string texmacs_doc_2= string_load (
url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main_convert_path.tm"));
tree texmacs_tree_2= texmacs_to_tree (texmacs_doc_2);
tree after_1 = replace_with_relative_path (
texmacs_tree_1, url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main.tm"));
tree after_2= replace_with_relative_path (
texmacs_tree_2,
url ("$TEXMACS_PATH/tests/29_4_2multiple-files/main_convert_path.tm"));
QVERIFY (after_2 == after_1);
}
void
TestHummusPdfMakeAttachment::test_get_main_tm () {
QVERIFY (get_main_tm (url ("$TEXMACS_PATH/tests/images/29_5_1.pdf")) ==
url ("$TEXMACS_PATH/tests/images/29_5_1.tm"));
QVERIFY (get_main_tm (url ("$TEXMACS_PATH/tests/images/29_5_2.pdf")) ==
url ("$TEXMACS_PATH/tests/images/29_5_2.tm"));
}

QTEST_MAIN (TestHummusPdfMakeAttachment)
#include "pdf_make_attachment_test.moc"

0 comments on commit 28a3463

Please sign in to comment.