Ghostscript pdf export without ps2pdf
[texmacs.git] / src / src / Edit / Editor / edit_main.cpp
blob4d71f7077515f58ad67b4701358665487ad35ecd
2 /******************************************************************************
3 * MODULE : editor.cpp
4 * DESCRIPTION: routines for the editor
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
12 #include "edit_main.hpp"
13 #include "tm_buffer.hpp"
14 #include "file.hpp"
15 #include "sys_utils.hpp"
16 #include "printer.hpp"
17 #include "convert.hpp"
18 #include "connect.hpp"
19 #include "typesetter.hpp"
20 #include "drd_std.hpp"
21 #include "message.hpp"
22 #include <setjmp.h>
23 #ifdef EXPERIMENTAL
24 #include "../../Style/Memorizer/clean_copy.hpp"
25 #endif
27 #ifdef USE_GS
28 #include "Ghostscript/gs_utilities.hpp"
29 #endif
31 /******************************************************************************
32 * Constructors and destructor
33 ******************************************************************************/
35 editor_rep::editor_rep ():
36 simple_widget_rep (),
37 drd (buf->abbr, std_drd), et (the_et), rp (buf->rp) {}
39 editor_rep::editor_rep (server_rep* sv2, tm_buffer buf2):
40 simple_widget_rep (),
41 sv (sv2), buf (buf2), drd (buf->abbr, std_drd),
42 et (the_et), rp (buf2->rp) {}
44 edit_main_rep::edit_main_rep (server_rep* sv, tm_buffer buf):
45 editor_rep (sv, buf), props (UNKNOWN), ed_obs (edit_observer (this))
47 #ifdef EXPERIMENTAL
48 cct= copy (subtree (et, rp));
49 copy_ip (subtree (et, rp), cct);
50 #endif
51 attach_observer (subtree (et, rp), ed_obs);
52 notify_change (THE_TREE);
53 tp= correct_cursor (et, rp * 0);
56 edit_main_rep::~edit_main_rep () {
57 detach_observer (subtree (et, rp), ed_obs);
58 #ifdef EXPERIMENTAL
59 mem= memorizer ();
60 #endif
63 editor
64 new_editor (server_rep* sv, tm_buffer buf) {
65 return tm_new<edit_main_rep> (sv, buf);
68 /******************************************************************************
69 * Properties
70 ******************************************************************************/
72 void
73 edit_main_rep::set_property (scheme_tree what, scheme_tree val) {
74 props (what)= val;
77 void
78 edit_main_rep::set_bool_property (string what, bool val) {
79 props (what)= (val? string ("true"): string ("false"));
82 void
83 edit_main_rep::set_int_property (string what, int val) {
84 props (what)= as_tree (val);
87 void
88 edit_main_rep::set_string_property (string what, string val) {
89 props (what)= val;
92 scheme_tree
93 edit_main_rep::get_property (scheme_tree what) {
94 return props [what];
97 bool
98 edit_main_rep::get_bool_property (string what) {
99 return as_bool (props [what]);
103 edit_main_rep::get_int_property (string what) {
104 return as_int (props [what]);
107 string
108 edit_main_rep::get_string_property (string what) {
109 return as_string (props [what]);
112 /******************************************************************************
113 * Global routines
114 ******************************************************************************/
116 void
117 edit_main_rep::clear_buffer () {
118 assign (rp, tree (DOCUMENT, tree ("")));
121 void
122 edit_main_rep::new_window () {
125 void
126 edit_main_rep::clone_window () {
129 void
130 edit_main_rep::tex_buffer () {
134 edit_main_rep::get_name () {
135 return buf->name;
138 void
139 edit_main_rep::focus_on_this_editor () {
140 sv->focus_on_editor (this);
143 void
144 edit_main_rep::notify_page_change () {
145 if (is_attached (this)) send_invalidate_all (this);
148 /******************************************************************************
149 * Printing
150 ******************************************************************************/
152 string printing_dpi ("600");
153 string printing_cmd ("lpr");
154 string printing_on ("a4");
156 void
157 edit_main_rep::print (url name, bool conform, int first, int last) {
158 bool pdf= (suffix (name) == "pdf");
159 url orig= resolve (name, "");
160 #ifdef USE_GS
161 if (pdf) name= url_temp (".ps");
162 #endif
164 string medium = env->get_string (PAGE_MEDIUM);
165 if (conform && (medium != "paper")) conform= false;
166 // FIXME: better command for conform printing
168 // Set environment variables for printing
170 typeset_prepare ();
171 env->write (DPI, printing_dpi);
172 env->write (PAGE_SHOW_HF, "true");
173 env->write (PAGE_SCREEN_MARGIN, "false");
174 if (!conform) {
175 env->write (PAGE_MEDIUM, "paper");
176 env->write (PAGE_PRINTED, "true");
179 // Typeset pages for printing
181 box the_box= typeset_as_document (env, subtree (et, rp), reverse (rp));
183 // Determine parameters for printer
185 string page_type = env->get_string (PAGE_TYPE);
186 double w = env->page_width;
187 double h = env->page_height;
188 double cm = env->as_length (string ("1cm"));
189 bool landsc = env->page_landscape;
190 int dpi = as_int (printing_dpi);
191 int start = max (0, first-1);
192 int end = min (N(the_box[0]), last);
193 int pages = end-start;
194 if (conform) {
195 page_type= "user";
196 SI bw= the_box[0][0]->w();
197 SI bh= the_box[0][0]->h();
198 string bws= as_string (bw) * "tmpt";
199 string bhs= as_string (bh) * "tmpt";
200 w= env->as_length (bws);
201 h= env->as_length (bhs);
204 // Print pages
206 int i;
207 renderer ren=
208 printer (name, dpi, pages, page_type, landsc, w/cm, h/cm);
209 for (i=start; i<end; i++) {
210 tree bg= env->read (BG_COLOR);
211 ren->set_background_pattern (bg);
212 if (bg != "white")
213 ren->clear_pattern (0, (SI) -h, (SI) w, 0);
215 rectangles rs;
216 the_box[0]->sx(i)= 0;
217 the_box[0]->sy(i)= 0;
218 the_box[0][i]->redraw (ren, path (0), rs);
219 if (i<end-1) ren->next_page ();
221 tm_delete (ren);
223 #ifdef USE_GS
224 if (pdf) {
225 gs_to_pdf (name, orig);
226 ::remove (name);
228 #endif
231 void
232 edit_main_rep::print_to_file (url name, string first, string last) {
233 print (name, false, as_int (first), as_int (last));
236 void
237 edit_main_rep::print_buffer (string first, string last) {
238 url temp= url_temp (".ps");
239 print (temp, false, as_int (first), as_int (last));
240 system (printing_cmd, temp);
241 ::remove (temp);
244 void
245 edit_main_rep::export_ps (url name, string first, string last) {
246 print (name, true, as_int (first), as_int (last));
249 array<int>
250 edit_main_rep::print_snippet (url name, tree t) {
251 bool ps= suffix (name) == "ps" || suffix (name) == "eps";
252 typeset_prepare ();
253 int dpi= as_int (printing_dpi);
254 //if (!ps) t= tree (WITH, MAGNIFICATION, "2", PAGE_WIDTH, "40cm", t);
255 if (!ps) t= tree (WITH, MAGNIFICATION, "1.6", PAGE_WIDTH, "40cm", t);
256 box b= typeset_as_box (env, t, path ());
257 if (b->x4 - b->x3 >= 5*PIXEL && b->y4 - b->y3 >= 5*PIXEL) {
258 if (ps) make_eps (name, b, dpi);
259 else {
260 url temp= url_temp ("eps");
261 make_eps (temp, b, dpi);
262 system ("convert", temp, name);
263 ::remove (temp);
266 array<int> a;
267 a << b->x3 << b->y3 << b->x4 << b->y4;
268 return a;
271 /******************************************************************************
272 * Evaluation of expressions
273 ******************************************************************************/
275 void
276 edit_main_rep::footer_eval (string s) {
277 // s= unslash (s); // FIXME: dirty fix; should not be necessary
278 string r= object_to_string (eval (s));
279 set_message (r, "evaluate expression");
282 tree
283 edit_main_rep::the_line () {
284 path p= search_parent_upwards (DOCUMENT);
285 return copy (subtree (et, p));
288 tree
289 edit_main_rep::the_root () {
290 return et;
293 tree
294 edit_main_rep::the_buffer () {
295 return subtree (et, rp);
298 tree
299 edit_main_rep::the_subtree (path p) {
300 return subtree (et, p);
303 path
304 edit_main_rep::the_buffer_path () {
305 return copy (rp);
308 path
309 edit_main_rep::the_path () {
310 return copy (tp);
313 /******************************************************************************
314 * Miscellaneous
315 ******************************************************************************/
317 void
318 edit_main_rep::show_tree () {
319 stretched_print (et, true);
320 // cout << et << "\n";
323 void
324 edit_main_rep::show_env () {
325 cout << env << "\n";
328 void
329 edit_main_rep::show_path () {
330 cout << tp << "\n";
333 void
334 edit_main_rep::show_cursor () {
335 cout << "Principal cursor: "
336 << cu->ox << ", " << cu->oy << " [" << cu->delta << "]\n";
337 cout << "Ghost cursor : "
338 << mv->ox << ", " << mv->oy << " [" << mv->delta << "]\n";
341 void
342 edit_main_rep::show_selection () {
343 selection sel; selection_get (sel);
344 cout << "physical selection: " << start_p << " --- " << end_p << "\n";
345 cout << "logical selection: " << sel->start << " --- " << sel->end << "\n";
348 void
349 edit_main_rep::show_meminfo () {
350 mem_info ();
353 void
354 edit_main_rep::edit_special () {
357 #ifdef UNCOMMENTED
358 void test_commute ();
359 void test_invert ();
360 #endif
362 void
363 edit_main_rep::edit_test () {
364 cout << "Test !\n";
365 #ifdef UNCOMMENTED
366 test_commute();
367 test_invert();
368 #endif