X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;f=ps%2Fps-document.c;h=a155207b1b58f85a6424817cb808ffc9143fbb8e;hb=d395ea58afa6744be92e33193881a1d4e6aeefed;hp=d7995f7688505d1b7c003dd5c16ef6e2e2d3e29b;hpb=25fddef525d3be8aa9b2825a3d8fcd29ff8c2c69;p=evince.git diff --git a/ps/ps-document.c b/ps/ps-document.c index d7995f76..a155207b 100644 --- a/ps/ps-document.c +++ b/ps/ps-document.c @@ -46,6 +46,7 @@ #include "ps-document.h" #include "ev-debug.h" #include "gsdefaults.h" +#include "ev-ps-exporter.h" #ifdef HAVE_LOCALE_H # include @@ -67,11 +68,6 @@ GCond* pixbuf_cond = NULL; GMutex* pixbuf_mutex = NULL; GdkPixbuf *current_pixbuf = NULL; -enum { - PROP_0, - PROP_TITLE -}; - /* structure to describe section of file to send to ghostscript */ struct record_list { FILE *fp; @@ -101,6 +97,7 @@ static void input(gpointer data, gint source, GdkInputCondition condition); static void stop_interpreter(PSDocument * gs); static gint start_interpreter(PSDocument * gs); static void ps_document_document_iface_init (EvDocumentIface *iface); +static void ps_document_ps_exporter_iface_init (EvPSExporterIface *iface); static gboolean ps_document_widget_event (GtkWidget *widget, GdkEvent *event, gpointer data); static GObjectClass *parent_class = NULL; @@ -142,45 +139,13 @@ ps_document_init (PSDocument *gs) gs->gs_status = _("No document loaded."); + gs->ps_export_pagelist = NULL; + gs->ps_export_filename = NULL; + pixbuf_cond = g_cond_new (); pixbuf_mutex = g_mutex_new (); } -static void -ps_document_set_property (GObject *object, - guint prop_id, - const GValue *value, - GParamSpec *pspec) -{ - switch (prop_id) - - { - case PROP_TITLE: - /* read only */ - break; - } -} - -static void -ps_document_get_property (GObject *object, - guint prop_id, - GValue *value, - GParamSpec *pspec) -{ - PSDocument *ps = PS_DOCUMENT (object); - - switch (prop_id) - { - case PROP_TITLE: - if (ps->doc) { - g_value_set_string (value, ps->doc->title); - } else { - g_value_set_string (value, NULL); - } - break; - } -} - static void ps_document_class_init(PSDocumentClass *klass) { @@ -191,15 +156,11 @@ ps_document_class_init(PSDocumentClass *klass) gs_class = klass; object_class->finalize = ps_document_finalize; - object_class->get_property = ps_document_get_property; - object_class->set_property = ps_document_set_property; klass->gs_atom = gdk_atom_intern ("GHOSTVIEW", FALSE); klass->next_atom = gdk_atom_intern ("NEXT", FALSE); klass->page_atom = gdk_atom_intern ("PAGE", FALSE); klass->string_atom = gdk_atom_intern ("STRING", FALSE); - - g_object_class_override_property (object_class, PROP_TITLE, "title"); } static void @@ -372,8 +333,6 @@ setup_pixmap (PSDocument *gs, int page, double scale) pixmap_width = floor (width * scale); pixmap_height = floor (height * scale); - g_print ("%f %f %f %d %d\n", width, height, scale, pixmap_width, pixmap_height); - if(gs->bpixmap) { int w, h; @@ -400,43 +359,99 @@ setup_pixmap (PSDocument *gs, int page, double scale) } } +#define DEFAULT_PAGE_SIZE 1 + static void get_page_box (PSDocument *gs, int page, int *urx, int *ury, int *llx, int *lly) { - GtkGSPaperSize *paper_sizes = gtk_gs_defaults_get_paper_sizes (); + gint new_llx = 0; + gint new_lly = 0; + gint new_urx = 0; + gint new_ury = 0; + GtkGSPaperSize *papersizes = gtk_gs_defaults_get_paper_sizes(); + int new_pagesize = -1; + + g_return_if_fail(PS_IS_DOCUMENT(gs)); + + if(new_pagesize == -1) { + new_pagesize = DEFAULT_PAGE_SIZE; + if(gs->doc) { + /* If we have a document: + We use -- the page size (if specified) + or the doc. size (if specified) + or the page bbox (if specified) + or the bounding box + */ + if((page >= 0) && (gs->doc->numpages > page) && + (gs->doc->pages) && (gs->doc->pages[page].size)) { + new_pagesize = gs->doc->pages[page].size - gs->doc->size; + } + else if(gs->doc->default_page_size != NULL) { + new_pagesize = gs->doc->default_page_size - gs->doc->size; + } + else if((page >= 0) && + (gs->doc->numpages > page) && + (gs->doc->pages) && + (gs->doc->pages[page].boundingbox[URX] > + gs->doc->pages[page].boundingbox[LLX]) && + (gs->doc->pages[page].boundingbox[URY] > + gs->doc->pages[page].boundingbox[LLY])) { + new_pagesize = -1; + } + else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) && + (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) { + new_pagesize = -1; + } + } + } - g_return_if_fail (PS_IS_DOCUMENT (gs)); - g_return_if_fail (gs->doc != NULL); - g_return_if_fail (page >= 0); - - if (gs->doc->pages && gs->doc->pages[page].size) { - int page_size; - - page_size = gs->doc->pages[page].size - gs->doc->size; - *llx = *lly = 0; - *urx = gs->doc->size[page_size].width; - *ury = gs->doc->size[page_size].height; - } else if (gs->doc->pages && - (gs->doc->pages[page].boundingbox[URX] > - gs->doc->pages[page].boundingbox[LLX]) && - (gs->doc->pages[page].boundingbox[URY] > - gs->doc->pages[page].boundingbox[LLY])) { - *llx = gs->doc->pages[page].boundingbox[LLX]; - *lly = gs->doc->pages[page].boundingbox[LLY]; - *urx = gs->doc->pages[page].boundingbox[URX]; - *ury = gs->doc->pages[page].boundingbox[URY]; - } else if ((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) && - (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) { - *llx = gs->doc->boundingbox[LLX]; - *lly = gs->doc->boundingbox[LLY]; - *urx = gs->doc->boundingbox[URX]; - *ury = gs->doc->boundingbox[URY]; - } else { - /* Fallback to A4 */ - *llx = *lly = 0; - *urx = paper_sizes[12].width; - *ury = paper_sizes[12].height; - } + /* Compute bounding box */ + if(gs->doc && (gs->doc->epsf || new_pagesize == -1)) { /* epsf or bbox */ + if((page >= 0) && + (gs->doc->pages) && + (gs->doc->pages[page].boundingbox[URX] > + gs->doc->pages[page].boundingbox[LLX]) + && (gs->doc->pages[page].boundingbox[URY] > + gs->doc->pages[page].boundingbox[LLY])) { + /* use page bbox */ + new_llx = gs->doc->pages[page].boundingbox[LLX]; + new_lly = gs->doc->pages[page].boundingbox[LLY]; + new_urx = gs->doc->pages[page].boundingbox[URX]; + new_ury = gs->doc->pages[page].boundingbox[URY]; + } + else if((gs->doc->boundingbox[URX] > gs->doc->boundingbox[LLX]) && + (gs->doc->boundingbox[URY] > gs->doc->boundingbox[LLY])) { + /* use doc bbox */ + new_llx = gs->doc->boundingbox[LLX]; + new_lly = gs->doc->boundingbox[LLY]; + new_urx = gs->doc->boundingbox[URX]; + new_ury = gs->doc->boundingbox[URY]; + } + } + else { + if(new_pagesize < 0) + new_pagesize = DEFAULT_PAGE_SIZE; + new_llx = new_lly = 0; + if(gs->doc && gs->doc->size && + (new_pagesize < gs->doc->numsizes)) { + new_urx = gs->doc->size[new_pagesize].width; + new_ury = gs->doc->size[new_pagesize].height; + } + else { + new_urx = papersizes[new_pagesize].width; + new_ury = papersizes[new_pagesize].height; + } + } + + if(new_urx <= new_llx) + new_urx = papersizes[12].width; + if(new_ury <= new_lly) + new_ury = papersizes[12].height; + + *urx = new_urx; + *ury = new_ury; + *llx = new_llx; + *lly = new_lly; } static int @@ -449,6 +464,12 @@ get_page_orientation (PSDocument *gs, int page) if (gs->structured_doc) { orientation = gs->doc->pages[page].orientation; } + if (orientation == GTK_GS_ORIENTATION_NONE) { + orientation = gs->doc->default_page_orientation; + } + if (orientation == GTK_GS_ORIENTATION_NONE) { + orientation = gs->doc->orientation; + } if (orientation == GTK_GS_ORIENTATION_NONE) { orientation = GTK_GS_ORIENTATION_PORTRAIT; } @@ -975,12 +996,22 @@ ps_document_get_type(void) NULL }; + static const GInterfaceInfo ps_exporter_info = + { + (GInterfaceInitFunc) ps_document_ps_exporter_iface_init, + NULL, + NULL + }; + gs_type = g_type_register_static(G_TYPE_OBJECT, "PSDocument", &gs_info, 0); g_type_add_interface_static (gs_type, EV_TYPE_DOCUMENT, &document_info); + g_type_add_interface_static (gs_type, + EV_TYPE_PS_EXPORTER, + &ps_exporter_info); } return gs_type; @@ -1047,8 +1078,6 @@ document_load(PSDocument * gs, const gchar * fname) /* we grab the vital statistics!!! */ gs->doc = psscan(gs->gs_psfile, TRUE, filename); - g_object_notify (G_OBJECT (gs), "title"); - if(gs->doc == NULL) { /* File does not seem to be a Postscript one */ gchar buf[1024]; @@ -1170,13 +1199,59 @@ ps_document_load (EvDocument *document, return result; } +static gboolean +save_page_list (PSDocument *document, int *page_list, const char *filename) +{ + gboolean result = TRUE; + GtkGSDocSink *sink = gtk_gs_doc_sink_new (); + FILE *f; + gchar *buf; + + pscopydoc (sink, document->gs_filename, document->doc, page_list); + + buf = gtk_gs_doc_sink_get_buffer (sink); + + f = fopen (filename, "w"); + if (f) { + fputs (buf, f); + fclose (f); + } else { + result = FALSE; + } + + g_free (buf); + gtk_gs_doc_sink_free (sink); + g_free (sink); + + return result; +} + static gboolean ps_document_save (EvDocument *document, const char *uri, GError **error) { - g_warning ("ps_document_save not implemented"); /* FIXME */ - return TRUE; + PSDocument *ps = PS_DOCUMENT (document); + int *page_list; + gboolean result; + int i; + char *filename; + + filename = g_filename_from_uri (uri, NULL, error); + if (!filename) + return FALSE; + + page_list = g_new0 (int, ps->doc->numpages); + for (i = 0; i < ps->doc->numpages; i++) { + page_list[i] = 1; + } + + result = save_page_list (ps, page_list, filename); + + g_free (page_list); + g_free (filename); + + return result; } static int @@ -1232,11 +1307,10 @@ ps_document_get_page_size (EvDocument *document, } } -static char * -ps_document_get_text (EvDocument *document, int page, EvRectangle *rect) +static gboolean +ps_document_can_get_text (EvDocument *document) { - g_warning ("ps_document_get_text not implemented"); /* FIXME ? */ - return NULL; + return FALSE; } static gboolean @@ -1289,13 +1363,69 @@ ps_document_render_pixbuf (EvDocument *document, int page, double scale) return pixbuf; } +static EvDocumentInfo * +ps_document_get_info (EvDocument *document) +{ + EvDocumentInfo *info; + PSDocument *ps = PS_DOCUMENT (document); + + info = g_new0 (EvDocumentInfo, 1); + info->fields_mask = EV_DOCUMENT_INFO_TITLE; + info->title = ps->doc->title; + + return info; +} + static void ps_document_document_iface_init (EvDocumentIface *iface) { iface->load = ps_document_load; iface->save = ps_document_save; - iface->get_text = ps_document_get_text; + iface->can_get_text = ps_document_can_get_text; iface->get_n_pages = ps_document_get_n_pages; iface->get_page_size = ps_document_get_page_size; iface->render_pixbuf = ps_document_render_pixbuf; + iface->get_info = ps_document_get_info; +} + +static void +ps_document_ps_export_begin (EvPSExporter *exporter, const char *filename, + int first_page, int last_page) +{ + PSDocument *document = PS_DOCUMENT (exporter); + + g_free (document->ps_export_pagelist); + + document->ps_export_pagelist = g_new0 (int, document->doc->numpages); + document->ps_export_filename = g_strdup (filename); +} + +static void +ps_document_ps_export_do_page (EvPSExporter *exporter, int page) +{ + PSDocument *document = PS_DOCUMENT (exporter); + + document->ps_export_pagelist[page] = 1; +} + +static void +ps_document_ps_export_end (EvPSExporter *exporter) +{ + PSDocument *document = PS_DOCUMENT (exporter); + + save_page_list (document, document->ps_export_pagelist, + document->ps_export_filename); + + g_free (document->ps_export_pagelist); + g_free (document->ps_export_filename); + document->ps_export_pagelist = NULL; + document->ps_export_filename = NULL; +} + +static void +ps_document_ps_exporter_iface_init (EvPSExporterIface *iface) +{ + iface->begin = ps_document_ps_export_begin; + iface->do_page = ps_document_ps_export_do_page; + iface->end = ps_document_ps_export_end; }