1 #include "export_pdf.hpp"
2 #include "canvas_pdf.hpp"
3 #include <podofo/podofo.h>
4 #include "util/util.hpp"
5 #include "schematic/schematic.hpp"
6 #include "export_pdf_util.hpp"
7 
8 namespace horizon {
9 
cb_nop(std::string,double)10 static void cb_nop(std::string, double)
11 {
12 }
13 
export_pdf(const class Schematic & sch,const class PDFExportSettings & settings,std::function<void (std::string,double)> cb)14 void export_pdf(const class Schematic &sch, const class PDFExportSettings &settings,
15                 std::function<void(std::string, double)> cb)
16 {
17     if (!cb)
18         cb = &cb_nop;
19     cb("Initializing", 0);
20 
21     PoDoFo::PdfStreamedDocument document(settings.output_filename.c_str());
22     PoDoFo::PdfPainterMM painter;
23     auto info = document.GetInfo();
24     info->SetCreator("horizon EDA");
25     info->SetProducer("horizon EDA");
26     if (sch.block->project_meta.count("author")) {
27         info->SetAuthor(sch.block->project_meta.at("author"));
28     }
29     std::string title = "Schematic";
30     if (sch.block->project_meta.count("project_title")) {
31         title = sch.block->project_meta.at("project_title");
32     }
33     info->SetTitle(title);
34 
35     std::vector<const Sheet *> sheets;
36     for (const auto &it : sch.sheets) {
37         sheets.push_back(&it.second);
38     }
39     std::sort(sheets.begin(), sheets.end(), [](auto a, auto b) { return a->index < b->index; });
40 
41 
42     auto font = document.CreateFont("Helvetica");
43 
44 #if PODOFO_VERSION_MAJOR != 0 || PODOFO_VERSION_MINOR != 9 || PODOFO_VERSION_PATCH != 5
45     auto outlines = document.GetOutlines();
46     auto proot = outlines->CreateRoot(title);
47 
48     bool first_outline_item = true;
49 #endif
50 
51     CanvasPDF ca(painter, *font, settings);
52     ca.use_layer_colors = false;
53     for (const auto sheet : sheets) {
54         cb("Exporting sheet " + format_m_of_n(sheet->index, sheets.size()), ((double)sheet->index) / sheets.size());
55 
56         auto page = document.CreatePage(PoDoFo::PdfRect(0, 0, to_pt(sheet->frame.width), to_pt(sheet->frame.height)));
57         painter.SetPage(page);
58         painter.SetLineCapStyle(PoDoFo::ePdfLineCapStyle_Round);
59         painter.SetFont(font);
60         painter.SetColor(0, 0, 0);
61         painter.SetTextRenderingMode(PoDoFo::ePdfTextRenderingMode_Invisible);
62 
63         for (const auto &[uu, pic] : sheet->pictures) {
64             if (!pic.on_top)
65                 render_picture(document, painter, pic);
66         }
67 
68         ca.update(*sheet);
69 
70         for (const auto &[uu, pic] : sheet->pictures) {
71             if (pic.on_top)
72                 render_picture(document, painter, pic);
73         }
74 
75         painter.FinishPage();
76 
77 
78 #if PODOFO_VERSION_MAJOR != 0 || PODOFO_VERSION_MINOR != 9 || PODOFO_VERSION_PATCH != 5
79         if (first_outline_item)
80             proot->CreateChild(sheet->name, PoDoFo::PdfDestination(page));
81         else
82             proot->Last()->CreateNext(sheet->name, PoDoFo::PdfDestination(page));
83         first_outline_item = false;
84 #endif
85     }
86     document.Close();
87 }
88 } // namespace horizon
89