From f00b898dd13b85e510188714f6b9d9a349d23ed2 Mon Sep 17 00:00:00 2001 From: Bastien Nocera Date: Mon, 10 Jul 2006 21:04:30 +0000 Subject: New backend to support impress slides. Fixes bug #30867. 2006-07-11 Bastien Nocera * Makefile.am: * backend/Makefile.am: * backend/ev-document-factory.c: (ev_document_factory_add_filters): * backend/ev-document-factory.h: * configure.ac: * impress/.cvsignore: * impress/Makefile.am: * impress/common.h: * impress/document.c: (_imp_load_xml), (imp_open), (imp_nr_pages), (imp_get_page), (imp_next_page), (imp_prev_page), (imp_get_page_no), (imp_get_page_name), (imp_get_xml), (imp_close): * impress/f_oasis.c: (render_object), (render_page), (get_geometry), (_imp_oasis_load): * impress/f_oo13.c: (render_object), (render_page), (get_geometry), (_imp_oo13_load): * impress/iksemel.c: (iks_malloc), (iks_free), (iks_set_mem_funcs), (iks_strdup), (iks_strcat), (iks_strcmp), (iks_strcasecmp), (iks_strncmp), (iks_strncasecmp), (iks_strlen), (iks_escape), (iks_unescape), (find_space), (iks_stack_new), (iks_stack_alloc), (iks_stack_strdup), (iks_stack_strcat), (iks_stack_stat), (iks_stack_delete), (iks_sax_new), (iks_sax_extend), (iks_parser_stack), (iks_user_data), (iks_nr_bytes), (iks_nr_lines), (stack_init), (stack_expand), (sax_core), (iks_parse), (iks_parser_reset), (iks_parser_delete), (iks_new), (iks_new_within), (iks_insert), (iks_insert_cdata), (iks_insert_attrib), (iks_insert_node), (iks_hide), (iks_delete), (iks_next), (iks_next_tag), (iks_prev), (iks_prev_tag), (iks_parent), (iks_root), (iks_child), (iks_first_tag), (iks_attrib), (iks_find), (iks_find_cdata), (iks_find_attrib), (iks_find_with_attrib), (iks_stack), (iks_type), (iks_name), (iks_cdata), (iks_cdata_size), (iks_has_children), (iks_has_attribs), (escape_size), (my_strcat), (escape), (iks_string), (iks_copy_within), (iks_copy), (tagHook), (cdataHook), (deleteHook), (iks_dom_new), (iks_set_size_hint), (iks_tree), (iks_load), (iks_save): * impress/iksemel.h: * impress/imposter.h: * impress/impress-document.c: (G_DEFINE_TYPE_WITH_CODE), (imp_render_draw_bezier_real), (imp_render_get_size), (imp_render_set_fg_color), (imp_render_draw_line), (imp_render_draw_rect), (imp_render_draw_polygon), (imp_render_draw_arc), (imp_render_draw_bezier), (imp_render_open_image), (imp_render_get_image_size), (imp_render_scale_image), (imp_render_draw_image), (imp_render_close_image), (imp_render_markup), (imp_render_get_text_size), (imp_render_draw_text), (impress_document_load), (impress_document_save), (impress_document_get_n_pages), (impress_document_get_page_size), (imp_render_get_from_drawable), (impress_document_render_pixbuf), (impress_document_finalize), (impress_document_class_init), (impress_document_can_get_text), (impress_document_get_info), (impress_document_document_iface_init), (impress_document_thumbnails_get_thumbnail), (impress_document_thumbnails_get_dimensions), (impress_document_document_thumbnails_iface_init), (impress_document_init): * impress/impress-document.h: * impress/internal.h: * impress/r_back.c: (_imp_fill_back): * impress/r_draw.c: (_imp_draw_rect), (_imp_draw_line_end), (_imp_draw_image), (_imp_tile_image): * impress/r_geometry.c: (r_parse_color), (r_get_color), (fg_color), (r_get_x), (r_get_y), (r_get_angle), (r_get_viewbox), (r_polygon), (r_polyline): * impress/r_gradient.c: (poly_rotate), (r_draw_gradient_simple), (r_draw_gradient_complex), (r_draw_gradient): * impress/r_style.c: (get_style), (r_get_style), (get_style_x), (r_get_bullet): * impress/r_text.c: (add_line), (add_span), (calc_sizes), (calc_pos), (_imp_draw_layout), (text_span), (text_p), (text_list), (r_text): * impress/render.c: (imp_create_context), (imp_context_set_page), (imp_context_set_step), (imp_render), (imp_delete_context): * impress/render.h: * impress/zip.c: (zip_error), (find_cd), (get_long), (get_word), (list_files), (zip_open), (zip_close), (find_file), (seek_file), (zip_load_xml), (zip_get_size), (zip_load): * impress/zip.h: * shell/ev-utils.c: New backend to support impress slides. Fixes bug #30867. --- (limited to 'impress/impress-document.c') diff --git a/impress/impress-document.c b/impress/impress-document.c new file mode 100644 index 0000000..3ea993d --- /dev/null +++ b/impress/impress-document.c @@ -0,0 +1,521 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */ +/* + * Copyright (C) 2005, Jonathan Blandford + * Copyright (C) 2005, Bastien Nocera + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include +#include +#include "imposter.h" +#include "impress-document.h" +#include "ev-document-thumbnails.h" +#include "ev-document-misc.h" + +struct _ImpressDocumentClass +{ + GObjectClass parent_class; +}; + +struct _ImpressDocument +{ + GObject parent_instance; + + ImpDoc *imp; + ImpRenderCtx *ctx; + + GMutex *mutex; + GdkPixmap *pixmap; + GdkGC *gc; + PangoContext *pango_ctx; + + /* Only used while rendering inside the mainloop */ + int pagenum; + GdkPixbuf *pixbuf; + GCond *cond; +}; + +#define PAGE_WIDTH 1024 +#define PAGE_HEIGHT 768 + +typedef struct _ImpressDocumentClass ImpressDocumentClass; + +static void impress_document_document_iface_init (EvDocumentIface *iface); +static void impress_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface); + +G_DEFINE_TYPE_WITH_CODE (ImpressDocument, impress_document, G_TYPE_OBJECT, + { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT, + impress_document_document_iface_init); + G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS, + impress_document_document_thumbnails_iface_init); + }); + +/* Renderer */ +static void +imp_render_draw_bezier_real (GdkDrawable *d, GdkGC *gc, int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) +{ + int x, y, nx, ny; + int ax, bx, cx, ay, by, cy; + double t, t2, t3; + + x = x0; + y = y0; + + cx = 3 * (x1 - x0); + bx = 3 * (x2 - x1) - cx; + ax = x3 - x0 - cx - bx; + cy = 3 * (y1 - y0); + by = 3 * (y2 - y1) - cy; + ay = y3 - y0 - cy - by; + + for (t = 0; t < 1; t += 0.01) { + t2 = t * t; + t3 = t2 * t; + nx = ax * t3 + bx * t2 + cx * t + x0; + ny = ay * t3 + by * t2 + cy * t + y0; + gdk_draw_line (d, gc, x, y, nx, ny); + x = nx; + y = ny; + } +} + +static void +imp_render_get_size(void *drw_data, int *w, int *h) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + gdk_drawable_get_size(impress_document->pixmap, w, h); +} + +static void +imp_render_set_fg_color(void *drw_data, ImpColor *color) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + GdkColor c; + + c.red = color->red; + c.green = color->green; + c.blue = color->blue; + gdk_gc_set_rgb_fg_color(impress_document->gc, &c); +} + +static void +imp_render_draw_line(void *drw_data, int x1, int y1, int x2, int y2) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + gdk_draw_line(impress_document->pixmap, impress_document->gc, x1, y1, x2, y2); +} + +static void +imp_render_draw_rect(void *drw_data, int fill, int x, int y, int w, int h) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + gdk_draw_rectangle(impress_document->pixmap, impress_document->gc, fill, x, y, w, h); +} + +static void +imp_render_draw_polygon(void *drw_data, int fill, ImpPoint *pts, int nr_pts) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + gdk_draw_polygon(impress_document->pixmap, impress_document->gc, fill, (GdkPoint *)pts, nr_pts); +} + +static void +imp_render_draw_arc(void *drw_data, int fill, int x, int y, int w, int h, int sa, int ea) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + gdk_draw_arc(impress_document->pixmap, impress_document->gc, fill, x, y, w, h, sa * 64, ea * 64); +} + +static void +imp_render_draw_bezier(void *drw_data, int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + + imp_render_draw_bezier_real (impress_document->pixmap, impress_document->gc, x0, y0, x1, y1, x2, y2, x3, y3); +} + +static void * +imp_render_open_image(void *drw_data, const unsigned char *pix, size_t size) +{ + GdkPixbufLoader *gpl; + GdkPixbuf *pb; + + gpl = gdk_pixbuf_loader_new(); + gdk_pixbuf_loader_write(gpl, pix, size, NULL); + gdk_pixbuf_loader_close(gpl, NULL); + pb = gdk_pixbuf_loader_get_pixbuf(gpl); + return pb; +} + +static void +imp_render_get_image_size(void *drw_data, void *img_data, int *w, int *h) +{ + GdkPixbuf *pb = (GdkPixbuf *) img_data; + + *w = gdk_pixbuf_get_width(pb); + *h = gdk_pixbuf_get_height(pb); +} + +static void * +imp_render_scale_image(void *drw_data, void *img_data, int w, int h) +{ + GdkPixbuf *pb = (GdkPixbuf *) img_data; + + return gdk_pixbuf_scale_simple(pb, w, h, GDK_INTERP_BILINEAR); +} + +static void +imp_render_draw_image(void *drw_data, void *img_data, int x, int y, int w, int h) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + GdkPixbuf *pb = (GdkPixbuf *) img_data; + + gdk_draw_pixbuf(impress_document->pixmap, impress_document->gc, pb, 0, 0, x, y, w, h, GDK_RGB_DITHER_NONE, 0, 0); +} + +static void +imp_render_close_image(void *drw_data, void *img_data) +{ + GdkPixbuf *pb = (GdkPixbuf *) img_data; + + g_object_unref(G_OBJECT(pb)); +} + +static char * +imp_render_markup(const char *text, size_t len, int styles, int size) +{ + double scr_mm, scr_px, dpi; + char *esc; + char *ret; + int sz; + + scr_mm = gdk_screen_get_height_mm(gdk_screen_get_default()); + scr_px = gdk_screen_get_height(gdk_screen_get_default()); + dpi = (scr_px / scr_mm) * 25.4; + sz = (int) ((double) size * 72.0 * PANGO_SCALE / dpi); + esc = g_markup_escape_text(text, len); + ret = g_strdup_printf("%s", sz, esc); + g_free(esc); + return ret; +} + +static void +imp_render_get_text_size(void *drw_data, const char *text, size_t len, int size, int styles, int *w, int *h) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + PangoLayout *lay; + int pw, ph; + char *m; + + g_return_if_fail (impress_document->pango_ctx != NULL); + + lay = pango_layout_new(impress_document->pango_ctx); + m = imp_render_markup(text, len, styles, size); + pango_layout_set_markup(lay, m, strlen(m)); + pango_layout_get_size(lay, &pw, &ph); + g_object_unref(lay); + g_free(m); + *w = pw / PANGO_SCALE; + *h = ph / PANGO_SCALE; +} + +static void +imp_render_draw_text(void *drw_data, int x, int y, const char *text, size_t len, int size, int styles) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (drw_data); + PangoLayout *lay; + char *m; + + g_return_if_fail (impress_document->pango_ctx != NULL); + + lay = pango_layout_new(impress_document->pango_ctx); + m = imp_render_markup(text, len, styles, size); + pango_layout_set_markup(lay, m, strlen(m)); + gdk_draw_layout(impress_document->pixmap, impress_document->gc, x, y, lay); + g_object_unref(lay); + g_free(m); +} + +static const ImpDrawer imp_render_functions = { + imp_render_get_size, + imp_render_set_fg_color, + imp_render_draw_line, + imp_render_draw_rect, + imp_render_draw_polygon, + imp_render_draw_arc, + imp_render_draw_bezier, + imp_render_open_image, + imp_render_get_image_size, + imp_render_scale_image, + imp_render_draw_image, + imp_render_close_image, + imp_render_get_text_size, + imp_render_draw_text +}; + +/* Document interface */ +static gboolean +impress_document_load (EvDocument *document, + const char *uri, + GError **error) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (document); + gchar *filename; + ImpDoc *imp; + int err; + + /* FIXME: Could we actually load uris ? */ + filename = g_filename_from_uri (uri, NULL, error); + if (!filename) + { + //FIXME + //g_error_set (); + return FALSE; + } + + imp = imp_open (filename, &err); + + if (!imp) + { + //FIXME translate the err, set error + g_free (filename); + return FALSE; + } + impress_document->imp = imp; + + return TRUE; +} + +static gboolean +impress_document_save (EvDocument *document, + const char *uri, + GError **error) +{ + return FALSE; +} + +static int +impress_document_get_n_pages (EvDocument *document) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (document); + + g_return_val_if_fail (IMPRESS_IS_DOCUMENT (document), 0); + g_return_val_if_fail (impress_document->imp != NULL, 0); + + return imp_nr_pages (impress_document->imp); +} + +static void +impress_document_get_page_size (EvDocument *document, + int page, + double *width, + double *height) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (document); + + g_return_if_fail (IMPRESS_IS_DOCUMENT (document)); + g_return_if_fail (impress_document->imp != NULL); + + //FIXME + *width = PAGE_WIDTH; + *height = PAGE_HEIGHT; +} + +static gboolean +imp_render_get_from_drawable (ImpressDocument *impress_document) +{ + ImpPage *page; + + page = imp_get_page (impress_document->imp, impress_document->pagenum); + + g_return_val_if_fail (page != NULL, FALSE); + + imp_context_set_page (impress_document->ctx, page); + imp_render (impress_document->ctx, impress_document); + + impress_document->pixbuf = gdk_pixbuf_get_from_drawable (NULL, + GDK_DRAWABLE (impress_document->pixmap), + NULL, + 0, 0, + 0, 0, + PAGE_WIDTH, PAGE_HEIGHT); + g_cond_broadcast (impress_document->cond); + return FALSE; +} + +static GdkPixbuf * +impress_document_render_pixbuf (EvDocument *document, + EvRenderContext *rc) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (document); + GdkPixbuf *scaled_pixbuf; + + g_return_val_if_fail (IMPRESS_IS_DOCUMENT (document), 0); + g_return_val_if_fail (impress_document->imp != NULL, 0); + + impress_document->pagenum = rc->page; + + g_mutex_lock (impress_document->mutex); + impress_document->cond = g_cond_new (); + + g_idle_add ((GSourceFunc) imp_render_get_from_drawable, impress_document); + + g_cond_wait (impress_document->cond, impress_document->mutex); + g_cond_free (impress_document->cond); + g_mutex_unlock (impress_document->mutex); + + scaled_pixbuf = gdk_pixbuf_scale_simple (impress_document->pixbuf, + PAGE_WIDTH * rc->scale, + PAGE_HEIGHT * rc->scale, + GDK_INTERP_BILINEAR); + gdk_pixbuf_unref (impress_document->pixbuf); + impress_document->pixbuf = NULL; + + return scaled_pixbuf; +} + +static void +impress_document_finalize (GObject *object) +{ + ImpressDocument *impress_document = IMPRESS_DOCUMENT (object); + + g_mutex_free (impress_document->mutex); + + imp_close (impress_document->imp); + imp_delete_context (impress_document->ctx); + g_free (impress_document->pango_ctx); + g_object_unref (G_OBJECT (impress_document->pixmap)); + g_object_unref (impress_document->gc); + + G_OBJECT_CLASS (impress_document_parent_class)->finalize (object); +} + +static void +impress_document_class_init (ImpressDocumentClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->finalize = impress_document_finalize; +} + +static gboolean +impress_document_can_get_text (EvDocument *document) +{ + return FALSE; +} + +static EvDocumentInfo * +impress_document_get_info (EvDocument *document) +{ + EvDocumentInfo *info; + + info = g_new0 (EvDocumentInfo, 1); + info->fields_mask = 0; + + return info; +} + +static void +impress_document_document_iface_init (EvDocumentIface *iface) +{ + iface->load = impress_document_load; + iface->save = impress_document_save; + iface->can_get_text = impress_document_can_get_text; + iface->get_n_pages = impress_document_get_n_pages; + iface->get_page_size = impress_document_get_page_size; + iface->render_pixbuf = impress_document_render_pixbuf; + iface->get_info = impress_document_get_info; +} + +static GdkPixbuf * +impress_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document, + gint page, + gint rotation, + gint size, + gboolean border) +{ + GdkPixbuf *pixbuf = NULL; + gdouble w, h; + EvRenderContext *rc; + + impress_document_get_page_size (EV_DOCUMENT (document), + page, + &w, &h); + + rc = ev_render_context_new (rotation, page, size/w); + pixbuf = impress_document_render_pixbuf (EV_DOCUMENT (document), rc); + g_object_unref (G_OBJECT (rc)); + + if (border) + { + GdkPixbuf *tmp_pixbuf = pixbuf; + pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf); + g_object_unref (tmp_pixbuf); + } + + return pixbuf; +} + +static void +impress_document_thumbnails_get_dimensions (EvDocumentThumbnails *document, + gint page, + gint suggested_width, + gint *width, + gint *height) +{ + gdouble page_ratio; + gdouble w, h; + + impress_document_get_page_size (EV_DOCUMENT (document), + page, + &w, &h); + g_return_if_fail (w > 0); + page_ratio = h/w; + *width = suggested_width; + *height = (gint) (suggested_width * page_ratio); +} + +static void +impress_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface) +{ + iface->get_thumbnail = impress_document_thumbnails_get_thumbnail; + iface->get_dimensions = impress_document_thumbnails_get_dimensions; +} + +static void +impress_document_init (ImpressDocument *impress_document) +{ + GdkWindow *window; + + impress_document->mutex = g_mutex_new (); + impress_document->ctx = imp_create_context(&imp_render_functions); + + window = gdk_screen_get_root_window (gdk_screen_get_default ()); + + impress_document->pixmap = gdk_pixmap_new (window, + PAGE_WIDTH, PAGE_HEIGHT, -1); + impress_document->gc = gdk_gc_new (impress_document->pixmap); + impress_document->pango_ctx = gdk_pango_context_get (); +} + +/* + * vim: sw=2 ts=8 cindent noai bs=2 + */ -- cgit v0.9.1