Example #1
0
static VALUE
rg_size(VALUE self)
{
    double width, height;
    poppler_page_get_size(SELF(self), &width, &height);
    return rb_ary_new3(2, rb_float_new(width), rb_float_new(height));
}
Example #2
0
static void zoom_rect(int x1, int y1, int x2, int y2) {
	cairo_t *ctx;
	cairo_surface_t *buf, *t;
	t = cairo_xlib_surface_create(dpy, wshow, vis, sw, sh);
	buf = cairo_surface_create_similar(t, CAIRO_CONTENT_COLOR, sw, sh);
	ctx = cairo_create(buf);
	cairo_surface_destroy(t);
	PopplerDocument *pdf;
	pdf = poppler_document_new_from_file(show->uri, NULL, NULL);
	PopplerPage *page = poppler_document_get_page(pdf, show->cur);
	double pdfw, pdfh;
	poppler_page_get_size(page, &pdfw, &pdfh);
	cairo_set_source_rgba(ctx, 1, 1, 1, 1);
	cairo_paint(ctx);
	double scx = show->w / pdfw, scy = show->h / pdfh;
	double dx = 0.0, dy = 0.0;
	if (conf.lock_aspect) {
		if (scx > scy) dx = (show->w - pdfw * (scx=scy)) / 2.0;
		else dy = (show->h - pdfh * (scy=scx)) / 2.0;
	}
	cairo_scale(ctx, scx * show->w /(double) (x2-x1),
			scy * show->h /(double) (y2-y1));
	cairo_translate(ctx, (dx - x1)/scx, (dy - y1)/scy);
	poppler_page_render(page, ctx);
	cairo_set_source_surface(show->target[0].ctx, buf, 0, 0);
	int i;
	for (i = conf.fade; i; i--) {
		cairo_paint_with_alpha(show->target[0].ctx, 1/(float)i);
		XFlush(dpy);
		usleep(5000);
	}
	cairo_destroy(ctx);
	cairo_surface_destroy(buf);
}
Example #3
0
static cairo_surface_t *
pgd_find_render_page (PgdFindDemo *demo)
{
        cairo_t *cr;
        PopplerPage *page;
        gdouble width, height;
        cairo_surface_t *surface = NULL;

        page = poppler_document_get_page (demo->doc, demo->selected_page);
        if (!page)
                return NULL;

        poppler_page_get_size (page, &width, &height);
        gtk_widget_set_size_request (demo->darea, width, height);

        surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
                                              width, height);
        cr = cairo_create (surface);

        cairo_save (cr);
        cairo_set_source_rgb (cr, 1, 1, 1);
        cairo_rectangle (cr, 0, 0, width, height);
        cairo_fill (cr);
        cairo_restore (cr);

        cairo_save (cr);
        poppler_page_render (page, cr);
        cairo_restore (cr);

        cairo_destroy (cr);
        g_object_unref (page);

        return surface;
}
Example #4
0
void write_mapping(int page_num, PopplerPage *page) {
	double w, h;
	poppler_page_get_size (page, &w, &h);

    GList *mapping = poppler_page_get_link_mapping (page);
    gint n_links = g_list_length (mapping);
    GList *l;
    
    printf("PAGE: %d\n", page_num);
    for (l = mapping; l; l = g_list_next (l)) {
    	PopplerLinkMapping *lmapping = (PopplerLinkMapping*)l->data;
	   	switch (lmapping->action->type) {
	    	case POPPLER_ACTION_URI: {
	    		PopplerActionUri *auri = (PopplerActionUri*)lmapping->action;
	    		double x1 = lmapping->area.x1 / w;
	    		double y1 = 1.0 - (lmapping->area.y1 / h);
	    		double x2 = lmapping->area.x2 / w;
	    		double y2 = 1.0 - (lmapping->area.y2 / h);
	    		printf("LINK: %f %f %f %f URI: %s\n",
	    			x1, y2, x2 - x1, y1 - y2,
					auri->uri);
	    	}
	        	break;
    	}
    }
    poppler_page_free_link_mapping (mapping);
    g_object_unref (page);
}
        virtual bool page(int pageNum)
        {
            if (!_doc) return false;

            if (pageNum<0 || pageNum>=getNumOfPages()) return false;

            PopplerPage* page = poppler_document_get_page(_doc, pageNum);

            if(!page) return false;

            _pageNum = pageNum;

            double w = 0.0f;
            double h = 0.0f;

            poppler_page_get_size(page, &w, &h);

            _cairoImage->create((unsigned int)(w*2.0),(unsigned int)(h*2.0));

            osg::clearImageToColor(this, _backgroundColor);

	    cairo_save(_cairoImage->getContext());

            cairo_scale(_cairoImage->getContext(), double(s())/w, double(t())/h);
            poppler_page_render(page, _cairoImage->getContext());
	    cairo_restore(_cairoImage->getContext());

	    g_object_unref (page);

            dirty();

            return true;
    }
Example #6
0
int convertPage(PopplerPage *page, const char* svgFilename)
{
	// Poppler stuff 
	double width, height;

	// Cairo stuff
	cairo_surface_t *surface;
	cairo_t *drawcontext;

	if (page == NULL) {
		fprintf(stderr, "Page does not exist\n");
		return -1;
	}
	poppler_page_get_size (page, &width, &height);

	// Open the SVG file
	surface = cairo_svg_surface_create(svgFilename, width, height);
	drawcontext = cairo_create(surface);

	// Render the PDF file into the SVG file
	poppler_page_render_for_printing(page, drawcontext);
	cairo_show_page(drawcontext);

	// Close the SVG file
	cairo_destroy(drawcontext);
	cairo_surface_destroy(surface);

	// Close the PDF file
	g_object_unref(page);
	
	return 0;     
}
Example #7
0
ClutterActor *
ckd_slide_new_for_poppler_page (PopplerPage *page, gdouble scale)
{
        CkdSlide *self = g_object_new (CKD_TYPE_SLIDE, NULL);
        CkdSlidePriv *priv = CKD_SLIDE_GET_PRIVATE (self);
        ClutterContent *canvas;
        gdouble w, h;
        
        priv->content = clutter_actor_new ();
        canvas = clutter_canvas_new ();

        poppler_page_get_size (page, &w, &h);
        
        clutter_canvas_set_size (CLUTTER_CANVAS(canvas), w * scale, h * scale);
        clutter_actor_set_content (priv->content, canvas);
        clutter_actor_set_content_scaling_filters (priv->content,
                                                   CLUTTER_SCALING_FILTER_TRILINEAR,
                                                   CLUTTER_SCALING_FILTER_LINEAR);
        g_object_unref (canvas);

        clutter_actor_set_size (priv->content, w * scale, h * scale);
        clutter_actor_add_child (CLUTTER_ACTOR(self), priv->content);

        struct CkdDrawData data = {page, scale};
        g_signal_connect (canvas, "draw", G_CALLBACK (draw_page), &data);
        
        clutter_content_invalidate (canvas);

        
        return (ClutterActor *)self;
}
Example #8
0
static void ease_pdf_actor_draw_page (EasePdfActor* self) {
#line 415 "ease-pdf-actor.c"
	PopplerPage* page;
	double width;
	double height;
	cairo_t* cr;
	GError * _inner_error_ = NULL;
#line 58 "ease-pdf-actor.vala"
	g_return_if_fail (self != NULL);
#line 61 "ease-pdf-actor.vala"
	page = _g_object_ref0 (poppler_document_get_page (self->priv->doc, self->priv->current_page));
#line 62 "ease-pdf-actor.vala"
	width = (double) 0;
#line 62 "ease-pdf-actor.vala"
	height = (double) 0;
#line 63 "ease-pdf-actor.vala"
	poppler_page_get_size (page, &width, &height);
#line 66 "ease-pdf-actor.vala"
	if (self->priv->texture == NULL) {
#line 433 "ease-pdf-actor.c"
		ClutterCairoTexture* _tmp0_;
		ClutterActor* _tmp1_;
#line 68 "ease-pdf-actor.vala"
		self->priv->texture = (_tmp0_ = g_object_ref_sink ((ClutterCairoTexture*) clutter_cairo_texture_new ((guint) ((gint) width), (guint) ((gint) height))), _g_object_unref0 (self->priv->texture), _tmp0_);
#line 69 "ease-pdf-actor.vala"
		clutter_container_add_actor ((ClutterContainer*) (_tmp1_ = ((EaseActor*) self)->contents, CLUTTER_IS_GROUP (_tmp1_) ? ((ClutterGroup*) _tmp1_) : NULL), (ClutterActor*) self->priv->texture);
#line 71 "ease-pdf-actor.vala"
		clutter_actor_set_width ((ClutterActor*) self->priv->texture, clutter_actor_get_width (((EaseActor*) self)->contents));
#line 72 "ease-pdf-actor.vala"
		clutter_actor_set_height ((ClutterActor*) self->priv->texture, clutter_actor_get_height (((EaseActor*) self)->contents));
#line 74 "ease-pdf-actor.vala"
		g_signal_connect_object ((GObject*) ((EaseActor*) self)->contents, "notify::width", (GCallback) __lambda53__g_object_notify, self, 0);
#line 78 "ease-pdf-actor.vala"
		g_signal_connect_object ((GObject*) ((EaseActor*) self)->contents, "notify::height", (GCallback) __lambda54__g_object_notify, self, 0);
#line 448 "ease-pdf-actor.c"
	} else {
#line 86 "ease-pdf-actor.vala"
		clutter_cairo_texture_set_surface_size (self->priv->texture, (guint) ((gint) width), (guint) ((gint) height));
#line 452 "ease-pdf-actor.c"
	}
#line 90 "ease-pdf-actor.vala"
	clutter_cairo_texture_clear (self->priv->texture);
#line 91 "ease-pdf-actor.vala"
	cr = clutter_cairo_texture_create (self->priv->texture);
#line 92 "ease-pdf-actor.vala"
	ease_background_cairo_render (self->priv->pdf_element->background, cr, (gint) width, (gint) height, ease_document_get_path (ease_slide_get_parent (ease_element_get_parent (((EaseActor*) self)->element))), &_inner_error_);
#line 460 "ease-pdf-actor.c"
	if (_inner_error_ != NULL) {
		_cairo_destroy0 (cr);
		_g_object_unref0 (page);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
#line 94 "ease-pdf-actor.vala"
	poppler_page_render (page, cr);
#line 470 "ease-pdf-actor.c"
	_cairo_destroy0 (cr);
	_g_object_unref0 (page);
}
Example #9
0
PageInfo *analyse_page (PopplerDocument *doc, guint page_num)
{
  PopplerPage *page;
  PageInfo *info;

  GdkPixbuf *image;
  double width_points, height_points;
  int width, height;

  gboolean *white_rows, *white_cols;

  page = poppler_document_get_page (doc, page_num);
  if (!page) {
    g_error ("Couldn't open page %d of document", page_num);
  }

  /* There are 72 points in an inch. So width and height should be
   * multiplied by settings.dpi / 72.0 */

  poppler_page_get_size (page, &width_points, &height_points);
  width = (int) ((width_points * settings.dpi / 72.0) + 0.5);
  height = (int) ((height_points * settings.dpi / 72.0) + 0.5);

  image = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
  if (!image) {
    g_error ("Couldn't create an image (size %d x %d) for page %d",
             width, height, page_num);
  }

  poppler_page_render_to_pixbuf (page, 0, 0, width, height,
                                 settings.dpi / 72.0, 0, image);
  g_object_unref (page);

  find_white (image, &white_rows, &white_cols);
  g_object_unref (image);

  guint firstrow, lastrow, hunkscount;
  HunkData* hunks = find_hunks (white_rows, height,
                                &firstrow, &lastrow, &hunkscount);

  info = g_new (PageInfo, 1);
  info->bbox.x = first_zero (white_cols, width);
  info->bbox.width = last_zero (white_cols, width) - info->bbox.x;
  if (info->bbox.width <= 0) {
    g_error ("Empty page (%d)? Couldn't find a nonwhite column.",
             page_num);
  }

  info->bbox.y = firstrow;
  info->bbox.height = lastrow - firstrow;

  info->num_hunks = hunkscount;
  info->hunks = hunks;

  g_free (white_rows);
  g_free (white_cols);

  return info;
}
Example #10
0
/* adapted from pdf2png.c */
static const char *
_poppler_render_page (const char *filename,
		      const char *page_label,
		      cairo_surface_t **surface_out)
{
    PopplerDocument *document;
    PopplerPage *page;
    double width, height;
    GError *error = NULL;
    gchar *absolute, *uri;
    cairo_surface_t *surface;
    cairo_t *cr;
    cairo_status_t status;

    if (g_path_is_absolute (filename)) {
	absolute = g_strdup (filename);
    } else {
	gchar *dir = g_get_current_dir ();
	absolute = g_build_filename (dir, filename, (gchar *) 0);
	g_free (dir);
    }

    uri = g_filename_to_uri (absolute, NULL, &error);
    g_free (absolute);
    if (uri == NULL)
	return error->message; /* XXX g_error_free (error) */

    document = poppler_document_new_from_file (uri, NULL, &error);
    g_free (uri);
    if (document == NULL)
	return error->message; /* XXX g_error_free (error) */

    page = poppler_document_get_page_by_label (document, page_label);
    g_object_unref (document);
    if (page == NULL)
	return "page not found";

    poppler_page_get_size (page, &width, &height);

    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
    cr = cairo_create (surface);

    cairo_set_source_rgb (cr, 1., 1., 1.);
    cairo_paint (cr);

    poppler_page_render (page, cr);
    g_object_unref (page);

    status = cairo_status (cr);
    cairo_destroy (cr);

    if (status) {
	cairo_surface_destroy (surface);
	return  cairo_status_to_string (status);
    }

    *surface_out = surface;
    return NULL;
}
Example #11
0
static gboolean
render_page(GtkWidget *widget)
{
  /* GtkDrawingAreaにPDFを描画 */

  static cairo_surface_t *surface = NULL;
  cairo_t *cr;
  double width,height;
  PopplerPage *page;
  GdkWindow *window;

  if (doc == NULL) {
    return FALSE;
  }

  if (surface != NULL) {
    cairo_surface_destroy(surface);
    surface = NULL;
  }

  /* PDFの1ページ目を表示する。第2引数を変えればそのページを表示 */
  page = poppler_document_get_page(doc,0);

  /* PDFページのサイズを取得 */
  poppler_page_get_size(page, &width, &height);

  /* DrawingAreaをPDFページのサイズに設定=等倍 */
  gtk_widget_set_size_request(widget,(int)width,(int)height);

  /* cairo surface作成 */
  surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
    width,height);
  cr = cairo_create(surface);
  cairo_save(cr);
  /* スケールを等倍 */
  cairo_scale(cr,1.0,1.0);
  /* surfaceにページを描画 */
  poppler_page_render(page,cr);
  cairo_restore(cr);

  cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
  cairo_set_source_rgb (cr, 1., 1., 1.);
  cairo_paint (cr);
  cairo_destroy (cr);

  /* DrawingAreaのGdkWindowを取得 */
  window = gtk_widget_get_window(widget);
  if (window != NULL) {
    /* GdkWindowにsurfaceを設定 */
    cr = gdk_cairo_create(window);
    cairo_set_source_surface(cr,surface,0,0);
    cairo_paint(cr);
    cairo_destroy(cr);
  }

  g_object_unref(page);
}
Example #12
0
File: doc.c Project: arsane/vipdf
void *
xv_doc_get_paint (struct xv_doc_t *doc, unsigned int n,
                  double scale,
                  PgdRenderMode mode,
                  double * width, double *height)
{
    struct xv_page_t *page;

    if (n >= doc->n_pages) {
        return NULL;
    }

    if (!doc->pages[n]) {
        doc->pages[n] = g_malloc0(sizeof(struct xv_page_t));
        doc->pages[n]->page = poppler_document_get_page(doc->doc, n);
        if (!doc->pages[n]->page) {
            printf("Could not open page %d of document\n", n);
            return NULL;
        }
        poppler_page_get_size(doc->pages[n]->page,
                              &doc->pages[n]->width,
                              &doc->pages[n]->height);
    }

    page = doc->pages[n];

    /* auto scale */
    if (scale == 0) {
        double a, b;
        a = *width  / page->width;
        b = *height / page->height;
        scale = a < b? a : b;
    }

    /* A4 paper size */
    if (scale == -1) {
        GdkScreen * def = gdk_screen_get_default();
        gdouble res;
        g_assert (def != NULL);
        res = gdk_screen_get_resolution (def);
        scale = 8.3 * res / page->width;
    }

    /* rendering */
    if (mode == PGD_RENDER_CAIRO) {
        xv_doc_get_surface (page, mode, scale);
    } else {
        xv_doc_get_gdkbuf (page, mode, scale);
    }

    *width = doc->pages[n]->width * scale;
    *height = doc->pages[n]->height * scale;

    return page->pixbuf;
}
Example #13
0
static VALUE
rg_get_text(int argc, VALUE *argv, VALUE self)
{
    gchar *text;
    PopplerSelectionStyle style = POPPLER_SELECTION_GLYPH;
    VALUE rb_text, arg1, arg2, rb_rect;
    PopplerPage *page;

    rb_scan_args(argc, argv, "02", &arg1, &arg2);

    page = SELF(self);
    rb_rect = Qnil;
    if (!NIL_P(arg1)) {
        if (RTEST(rb_obj_is_kind_of(arg1, cRectangle))) {
            rb_rect = arg1;
        } else {
            rb_raise(rb_eArgError, "wrong first arrument. selection rectangle is expected.");
        }

        if (!NIL_P(arg2)) {
            style = RVAL2POPPLERSELECTIONSTYLE(arg2);
        }
    }

    if (NIL_P(rb_rect)) {
#if POPPLER_CHECK_VERSION(0, 15, 0)
        text = poppler_page_get_text(page);
#else
        PopplerRectangle rect;
        double width, height;

        rect.x1 = 0;
        rect.y1 = 0;
        poppler_page_get_size(page, &width, &height);
        rect.x2 = width;
        rect.y2 = height;
        text = poppler_page_get_text(page,
                                     style,
                                     &rect);
#endif
    } else {
        PopplerRectangle *rect;

        rect = RVAL2POPPLERRECTANGLE(rb_rect);
#if POPPLER_CHECK_VERSION(0, 15, 0)
        text = poppler_page_get_selected_text(page, style, rect);
#else
        text = poppler_page_get_text(page, style, rect);
#endif
    }

    rb_text = CSTR2RVAL(text);
    g_free(text);
    return rb_text;
}
Example #14
0
File: view.c Project: N8Fear/adwc
static void
redraw_handler(struct widget *widget, void *data)
{
	struct view *view = data;

	struct rectangle allocation;
	cairo_surface_t *surface;
	cairo_t *cr;
	PopplerPage *page;
	double width, height, doc_aspect, window_aspect, scale;

	widget_get_allocation(view->widget, &allocation);

	surface = window_get_surface(view->window);

	cr = cairo_create(surface);
	cairo_rectangle(cr, allocation.x, allocation.y,
			 allocation.width, allocation.height);
	cairo_clip(cr);

	cairo_set_source_rgba(cr, 0, 0, 0, 0.8);
	cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
	cairo_paint(cr);

        if(!view->document) {
                cairo_destroy(cr);
                cairo_surface_destroy(surface);
                window_flush(view->window);
                return;
        }

	page = poppler_document_get_page(view->document, view->page);
	poppler_page_get_size(page, &width, &height);
	doc_aspect = width / height;
	window_aspect = (double) allocation.width / allocation.height;
	if (doc_aspect < window_aspect)
		scale = allocation.height / height;
	else
		scale = allocation.width / width;
	cairo_translate(cr, allocation.x, allocation.y);
	cairo_scale(cr, scale, scale);
	cairo_translate(cr,
			(allocation.width - width * scale) / 2 / scale,
			(allocation.height - height * scale) / 2 / scale);
	cairo_rectangle(cr, 0, 0, width, height);
	cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
	cairo_set_source_rgb(cr, 1, 1, 1);
	cairo_fill(cr);
	poppler_page_render(page, cr);
	cairo_destroy(cr);
	cairo_surface_destroy(surface);
	g_object_unref(G_OBJECT(page));
}
Example #15
0
static void
pgd_selections_update_selection_region (PgdSelectionsDemo *demo)
{
	PopplerRectangle area = { 0, 0, 0, 0 };

	if (demo->selection_region)
		cairo_region_destroy (demo->selection_region);

	poppler_page_get_size (demo->page, &area.x2, &area.y2);
	demo->selection_region = poppler_page_get_selected_region (demo->page,
                                                                   1.0,
                                                                   POPPLER_SELECTION_GLYPH,
                                                                   &area);
}
Example #16
0
void DocumentView::drawTexImage(cairo_t* cr, TexImage* texImage)
{
	XOJ_CHECK_TYPE(DocumentView);

	cairo_matrix_t defaultMatrix = { 0 };
	cairo_get_matrix(cr, &defaultMatrix);

	PopplerDocument* pdf = texImage->getPdf();
	cairo_surface_t* img = texImage->getImage();

	if (pdf != nullptr)
	{
		if (poppler_document_get_n_pages(pdf) < 1)
		{
			g_warning("Got latex PDf without pages!: %s", texImage->getText().c_str());
			return;
		}

		PopplerPage* page = poppler_document_get_page(pdf, 0);

		double pageWidth = 0;
		double pageHeight = 0;
		poppler_page_get_size(page, &pageWidth, &pageHeight);

		double xFactor = texImage->getElementWidth() / pageWidth;
		double yFactor = texImage->getElementHeight() / pageHeight;

		cairo_translate(cr, texImage->getX(), texImage->getY());
		cairo_scale(cr, xFactor, yFactor);
		poppler_page_render(page, cr);
	}
	else if (img != nullptr)
	{
		int width = cairo_image_surface_get_width(img);
		int height = cairo_image_surface_get_height(img);

		cairo_set_operator(cr, CAIRO_OPERATOR_OVER);

		double xFactor = texImage->getElementWidth() / width;
		double yFactor = texImage->getElementHeight() / height;

		cairo_scale(cr, xFactor, yFactor);

		cairo_set_source_surface(cr, img, texImage->getX() / xFactor, texImage->getY() / yFactor);
		cairo_paint(cr);
	}

	cairo_set_matrix(cr, &defaultMatrix);
}
static GdkPixbuf *
get_thumbnail (PopplerDocument *doc,
               gint             page_num,
               gint             preferred_size)
{
    PopplerPage *page;
    GdkPixbuf   *pixbuf;

    page = poppler_document_get_page (doc, page_num);

    if (! page)
        return NULL;

    /* XXX: Remove conditional when we depend on poppler 0.8.0, but also
     * add configure check to make sure POPPLER_WITH_GDK is enabled!
     */
#ifdef POPPLER_WITH_GDK
    pixbuf = poppler_page_get_thumbnail_pixbuf (page);
#else
    pixbuf = poppler_page_get_thumbnail (page);
#endif


    if (! pixbuf)
    {
        gdouble width;
        gdouble height;
        gdouble scale;

        poppler_page_get_size (page, &width, &height);

        scale = (gdouble) preferred_size / MAX (width, height);

        width  *= scale;
        height *= scale;

        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
                                 width, height);

        poppler_page_render_to_pixbuf (page,
                                       0, 0, width, height, scale, 0, pixbuf);
    }

    g_object_unref (page);

    return pixbuf;
}
Example #18
0
static void
pgd_selections_render (GtkButton         *button,
		       PgdSelectionsDemo *demo)
{
	gdouble  page_width, page_height;
	cairo_t *cr;

	if (!demo->page)
		demo->page = poppler_document_get_page (demo->doc, demo->page_index);

	if (!demo->page)
		return;

	pgd_selections_clear_selections (demo);
	pgd_selections_update_selection_region (demo);
        gtk_widget_set_sensitive (demo->copy_button, FALSE);

	if (demo->surface)
		cairo_surface_destroy (demo->surface);
	demo->surface = NULL;

	poppler_page_get_size (demo->page, &page_width, &page_height);
	page_width *= demo->scale;
	page_height *= demo->scale;

	demo->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
						    page_width, page_height);
	cr = cairo_create (demo->surface);

	cairo_save (cr);

	if (demo->scale != 1.0)
		cairo_scale (cr, demo->scale, demo->scale);

	poppler_page_render (demo->page, cr);
	cairo_restore (cr);

	cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
	cairo_set_source_rgb (cr, 1., 1., 1.);
	cairo_paint (cr);

	cairo_destroy (cr);

	gtk_widget_set_size_request (demo->darea, page_width, page_height);
	gtk_widget_queue_draw (demo->darea);
}
Example #19
0
static gboolean
pgd_selections_render_selections (PgdSelectionsDemo *demo)
{
	PopplerRectangle doc_area;
	gdouble page_width, page_height;
	cairo_t *cr;

	if (!demo->page || demo->start.x == -1) {
		demo->selections_idle = 0;

		return FALSE;
	}

	poppler_page_get_size (demo->page, &page_width, &page_height);
	page_width *= demo->scale;
	page_height *= demo->scale;

	doc_area.x1 = demo->start.x / demo->scale;
	doc_area.y1 = demo->start.y / demo->scale;
	doc_area.x2 = demo->stop.x / demo->scale;
	doc_area.y2 = demo->stop.y / demo->scale;

	if (demo->selection_surface)
		cairo_surface_destroy (demo->selection_surface);
	demo->selection_surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
							      page_width, page_height);
	cr = cairo_create (demo->selection_surface);
	if (demo->scale != 1.0)
		cairo_scale (cr, demo->scale, demo->scale);
	poppler_page_render_selection (demo->page, cr,
				       &doc_area, &demo->doc_area,
				       demo->style,
				       &demo->glyph_color,
				       &demo->background_color);
	cairo_destroy (cr);

	demo->doc_area = doc_area;
	gtk_widget_queue_draw (demo->darea);

	demo->selections_idle = 0;

	return FALSE;
}
Example #20
0
void render_page(page_t *page_meta, PopplerPage *page) {;
	cairo_t *cairoctx;
	cairo_surface_t *surface;
	double width, height;
	char outfilename[NAME_MAX];

	poppler_page_get_size(page, &width, &height);
	snprintf(outfilename, sizeof outfilename - 1, "img-%d.svg", page_meta->pagenum);
	surface = cairo_svg_surface_create(outfilename,
		width, height);
	cairoctx = cairo_create(surface);
	if (cairoctx == NULL)
		ERROR("Cannot create a Cairo buffer");
	poppler_page_render(page, cairoctx);
	cairo_surface_show_page(surface);

	cairo_surface_destroy(surface);
	cairo_destroy(cairoctx);
}
Example #21
0
static void
pgd_text_get_text (GtkWidget   *button,
		   PgdTextDemo *demo)
{
	PopplerPage     *page;
	PopplerRectangle rect;
	gdouble          width, height;
	gchar           *text;
	GTimer          *timer;

	page = poppler_document_get_page (demo->doc, demo->page);
	if (!page)
		return;

	poppler_page_get_size (page, &width, &height);
	rect.x1 = rect.y1 = 0;
	rect.x2 = width;
	rect.y2 = height;

	timer = g_timer_new ();
	text = poppler_page_get_text (page, POPPLER_SELECTION_GLYPH, &rect);
	g_timer_stop (timer);

	if (text) {
		gchar *str;

		str = g_strdup_printf ("<i>got text in %.4f seconds</i>",
				       g_timer_elapsed (timer, NULL));
		gtk_label_set_markup (GTK_LABEL (demo->timer_label), str);
		g_free (str);
	} else {
		gtk_label_set_markup (GTK_LABEL (demo->timer_label), "<i>No text found</i>");
	}

	g_timer_destroy (timer);
	g_object_unref (page);

	if (text) {
		gtk_text_buffer_set_text (demo->buffer, text, strlen (text));
		g_free (text);
	}
}
Example #22
0
static gboolean
pgd_annots_drawing_area_motion_notify (GtkWidget      *area,
                                       GdkEventMotion *event,
                                       PgdAnnotsDemo  *demo)
{
    PopplerRectangle rect;
    PopplerPoint start, end;
    gdouble width, height;

    if (!demo->page || demo->mode != MODE_DRAWING || demo->start.x == -1)
        return FALSE;

    demo->stop.x = event->x;
    demo->stop.y = event->y;

    poppler_page_get_size (demo->page, &width, &height);

    /* Keep the drawing within the page */
    demo->stop.x = CLAMP (demo->stop.x, 0, width);
    demo->stop.y = CLAMP (demo->stop.y, 0, height);

    rect.x1 = start.x = demo->start.x;
    rect.y1 = start.y = height - demo->start.y;
    rect.x2 = end.x = demo->stop.x;
    rect.y2 = end.y = height - demo->stop.y;

    poppler_annot_set_rectangle (demo->active_annot, &rect);

    if (demo->annot_type == POPPLER_ANNOT_LINE)
        poppler_annot_line_set_vertices (POPPLER_ANNOT_LINE (demo->active_annot),
                                         &start, &end);

    if (POPPLER_IS_ANNOT_TEXT_MARKUP (demo->active_annot))
        pgd_annots_update_selected_text (demo);

    pgd_annot_view_set_annot (demo, demo->active_annot);
    pgd_annots_viewer_queue_redraw (demo);

    return TRUE;
}
Example #23
0
File: doc.c Project: arsane/vipdf
/* operations for doc */
struct xv_doc_t * xv_doc_new(const char * file)
{
    GError *err = NULL;
    struct xv_doc_t *xv_doc;

    xv_doc = g_malloc0(sizeof(struct xv_doc_t));
    xv_doc->doc = poppler_document_new_from_file(file, NULL, &err);

    if (!xv_doc->doc) {
        printf("%s\n", err->message);
        g_error_free(err);
        g_free(xv_doc);
        return NULL;
    }

    xv_doc->n_pages = poppler_document_get_n_pages(xv_doc->doc);

    /* initialize the pages */
    xv_doc->pages = g_malloc0(xv_doc->n_pages * sizeof(struct xv_page_t *));

    /* initial the first page */
    xv_doc->pages[0] = g_malloc0(sizeof(struct xv_page_t));
    xv_doc->pages[0]-> page = poppler_document_get_page(xv_doc->doc, 0);

    if (!xv_doc->pages[0]->page) {
        printf("Could not open page %d of document\n", 0);
        g_object_unref(xv_doc->doc);
        g_free(xv_doc->pages[0]);
        g_free(xv_doc->pages);
        g_free(xv_doc);
        return NULL;
    }

    poppler_page_get_size(xv_doc->pages[0]->page,
                          &xv_doc->pages[0]->width,
                          &xv_doc->pages[0]->height);

    return xv_doc;
}
Example #24
0
Window *render_create_sorter(Window win) {
	int i;
	PopplerPage *page;
	double pdfw, pdfh, scale;
	cairo_surface_t *pix_s;
	cairo_t *pix_c;

	XSetWindowAttributes wa;
	wa.backing_store = Always;
	wa.event_mask = ButtonPress | ExposureMask | PointerMotionMask;
	Window *wins = malloc((_pdf[0].npage + 1) * sizeof(Window));
	Pixmap pix;
	int ww, wh;
	for (i = 0; i < _pdf[0].npage; ++i) {
		page = poppler_document_get_page(_pdf[0].doc, i);
		poppler_page_get_size(page, &pdfw, &pdfh);
		scale = get_d(presH) / (6 * pdfh);
		ww = scale * pdfw; wh = scale * pdfh;
		wins[i] = XCreateWindow(dpy, win, 0, 0, ww, wh, 1, DefaultDepth(dpy,scr),
				InputOutput, DefaultVisual(dpy,scr), CWBackingStore | CWEventMask, &wa);
		XDefineCursor(dpy, wins[i], XCreateFontCursor(dpy, 68));
		pix = XCreatePixmap(dpy, wins[i], ww, wh, DefaultDepth(dpy,scr));
		pix_s = cairo_xlib_surface_create(dpy, pix, DefaultVisual(dpy,scr), ww, wh);
		pix_c = cairo_create(pix_s);
		cairo_surface_destroy(pix_s);
		cairo_set_source_rgb(pix_c, 1, 1, 1);
		cairo_paint(pix_c);
		cairo_scale(pix_c, scale, scale);
		poppler_page_render(page, pix_c);
		cairo_destroy(pix_c);
		XSetWindowBackgroundPixmap(dpy, wins[i], pix);
		XFreePixmap(dpy, pix);
		g_object_unref(page);
	}
	wins[_pdf[0].npage] = None;
	return wins;
}
Example #25
0
static void
pgd_render_slice_selector_setup (PgdRenderDemo *demo)
{
	PopplerPage *page;
	gdouble      width, height;

	page = poppler_document_get_page (demo->doc, demo->page);
	if (!page)
		return;

	poppler_page_get_size (page, &width, &height);
	
	gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->slice_x), 0, width);
	gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->slice_y), 0, height);
	gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->slice_w), 0, width);
	gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->slice_h), 0, height);

	gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->slice_x), 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->slice_y), 0);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->slice_w), width);
	gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->slice_h), height);

	g_object_unref (page);
}
Example #26
0
static void
pgd_text_area_selector_setup (PgdTextDemo *demo)
{
        PopplerPage *page;
        gdouble      width, height;

        page = poppler_document_get_page (demo->doc, demo->page);
        if (!page)
                return;

        poppler_page_get_size (page, &width, &height);

        gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->area_x1), -10, width - 10);
        gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->area_y1), -10, height - 10);
        gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->area_x2), 0, width + 10);
        gtk_spin_button_set_range (GTK_SPIN_BUTTON (demo->area_y2), 0, height + 10);

        gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->area_x1), 0);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->area_y1), 0);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->area_x2), width);
        gtk_spin_button_set_value (GTK_SPIN_BUTTON (demo->area_y2), height);

        g_object_unref (page);
}
Example #27
0
static gboolean
pgd_find_find_text (PgdFindDemo *demo)
{
	PopplerPage  *page;
	GList        *matches;
	GTimer       *timer;
        GtkTreeModel *model;

	page = poppler_document_get_page (demo->doc, demo->page_index);
	if (!page) {
		demo->page_index++;
		return demo->page_index < demo->n_pages;
	}

        model = gtk_tree_view_get_model (GTK_TREE_VIEW (demo->treeview));
	timer = g_timer_new ();
	matches = poppler_page_find_text_with_options (page, gtk_entry_get_text (GTK_ENTRY (demo->entry)), demo->options);
	g_timer_stop (timer);
	if (matches) {
		GtkTreeIter iter;
		gchar      *str;
		GList      *l;
                gdouble     height;
		gint        n_match = 0;

		str = g_strdup_printf ("%d matches found on page %d in %.4f seconds",
				       g_list_length (matches), demo->page_index + 1,
				       g_timer_elapsed (timer, NULL));
		
		gtk_tree_store_append (GTK_TREE_STORE (model), &iter, NULL);
		gtk_tree_store_set (GTK_TREE_STORE (model), &iter,
				    TITLE_COLUMN, str,
				    VISIBLE_COLUMN, FALSE,
                                    PAGE_COLUMN, demo->page_index,
				    -1);
		g_free (str);

                poppler_page_get_size (page, NULL, &height);

		for (l = matches; l && l->data; l = g_list_next (l)) {
			PopplerRectangle *rect = (PopplerRectangle *)l->data;
			GtkTreeIter       iter_child;
			gchar            *x1, *y1, *x2, *y2;
                        gdouble           tmp;

			str = g_strdup_printf ("Match %d", ++n_match);
			x1 = g_strdup_printf ("%.2f", rect->x1);
			y1 = g_strdup_printf ("%.2f", rect->y1);
			x2 = g_strdup_printf ("%.2f", rect->x2);
			y2 = g_strdup_printf ("%.2f", rect->y2);

                        tmp = rect->y1;
                        rect->y1 = height - rect->y2;
                        rect->y2 = height - tmp;

			gtk_tree_store_append (GTK_TREE_STORE (model), &iter_child, &iter);
			gtk_tree_store_set (GTK_TREE_STORE (model), &iter_child,
					    TITLE_COLUMN, str,
					    X1_COLUMN, x1,
					    Y1_COLUMN, y1,
					    X2_COLUMN, x2,
					    Y2_COLUMN, y2,
					    VISIBLE_COLUMN, TRUE,
                                            PAGE_COLUMN, demo->page_index,
                                            PAGE_RECT, rect,
					    -1);
			g_free (str);
			g_free (x1);
			g_free (y1);
			g_free (x2);
			g_free (y2);
                        g_object_weak_ref (G_OBJECT (model),
                                           (GWeakNotify)poppler_rectangle_free,
                                           rect);
		}
		g_list_free (matches);
	}

	g_timer_destroy (timer);
	g_object_unref (page);

	demo->page_index++;
	pgd_find_update_progress (demo, demo->page_index);

	return demo->page_index < demo->n_pages;
}
Example #28
0
static GdkPixbuf * getRenderedPixbuf(struct viewport *pp, int mypage_i)
{
	int myfitmode = -1;
	double pw = 0, ph = 0;
	double w = 0, h = 0;
	double page_ratio = 1, screen_ratio = 1, scale = 1;
	GdkPixbuf *targetBuf = NULL;
	PopplerPage *page = NULL;

	GList *it = NULL;
	struct cacheItem *ci = NULL;
	gboolean found = FALSE;

	/* Limit boundaries of mypage_i -- just to be sure. */
	if (mypage_i < 0 || mypage_i >= doc_n_pages)
		return NULL;

	/* Get this page and its ratio. That function should never return
	 * NULL because we already took care of it. Catch it nontheless. */
	page = poppler_document_get_page(doc, mypage_i);
	if (page == NULL)
		return NULL;

	poppler_page_get_size(page, &pw, &ph);
	page_ratio = pw / ph;
	screen_ratio = (double)pp->width / (double)pp->height;

	/* select fit mode */
	if (runpref.fit_mode == FIT_PAGE)
	{
		/* that's it: compare screen and page ratio. this
		 * will cover all 4 cases that could happen. */
		if (screen_ratio > page_ratio)
			myfitmode = FIT_HEIGHT;
		else
			myfitmode = FIT_WIDTH;
	}
	else
		myfitmode = runpref.fit_mode;

	switch (myfitmode)
	{
		case FIT_HEIGHT:
			h = pp->height;
			w = h * page_ratio;
			scale = h / ph;
			break;

		case FIT_WIDTH:
			w = pp->width;
			h = w / page_ratio;
			scale = w / pw;
			break;
	}

	/* check if already in cache. */
	it = cache;
	found = FALSE;
	while (it)
	{
		ci = (struct cacheItem *)(it->data);

		if (ci->slidenum == mypage_i && ci->w == w && ci->h == h
				&& ci->scale == scale)
		{
			/* cache hit. */
			found = TRUE;
			targetBuf = ci->pixbuf;

			/* we need to increase this item's "score", that is marking
			 * it as a "recent" item. we do so by placing it at the end
			 * of the list. */
			cache = g_list_remove(cache, ci);
			cache = g_list_append(cache, ci);

			/* now quit the loop. */
			break;
		}

		it = g_list_next(it);
	}

	if (!found)
	{
		/* cache miss, render to a pixbuf. */
		targetBuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, w, h);
		g_assert(targetBuf);
		poppler_page_render_to_pixbuf(page, 0, 0, w, h, scale, 0,
				targetBuf);

		/* check if cache full. if so, kill the oldest item. */
		if (g_list_length(cache) + 1 > runpref.cache_max)
		{
			it = g_list_first(cache);
			if (it == NULL)
			{
				fprintf(stderr, "[Cache] No first item in list."
						" cache_max too small?\n");
			}
			else
			{
				/* unref pixbuf. */
				ci = (struct cacheItem *)(it->data);
				if (ci->pixbuf != NULL)
					g_object_unref(ci->pixbuf);

				/* free memory alloc'd for the struct. */
				free(ci);

				/* remove the pointer which is now invalid from the
				 * list.
				 */
				cache = g_list_remove(cache, ci);
			}
		}

		/* add new item to cache. */
		ci = (struct cacheItem *)malloc(sizeof(struct cacheItem));
		ci->slidenum = mypage_i;
		ci->w = w;
		ci->h = h;
		ci->scale = scale;
		ci->pixbuf = targetBuf;
		cache = g_list_append(cache, ci);
	}

	/* cleanup */
	g_object_unref(G_OBJECT(page));

	return targetBuf;
}
Example #29
0
int main (int argc, char *argv[])
{
    PopplerDocument *document;
    PopplerPage *page;
    double width, height;
    const char *filename = argv[1];
    const char *output_filename = argv[2];
    const char *page_label = argv[3];
    gchar *absolute, *uri;
    cairo_surface_t *surface;
    cairo_t *cr;
    cairo_status_t status;
    GError *error = NULL;

    if (argc != 4)
	FAIL ("usage: pdf2png input_file.pdf output_file.png page");

    g_type_init ();

    if (g_path_is_absolute(filename)) {
	absolute = g_strdup (filename);
    } else {
	gchar *dir = g_get_current_dir ();
	absolute = g_build_filename (dir, filename, (gchar *) 0);
	g_free (dir);
    }

    uri = g_filename_to_uri (absolute, NULL, &error);
    g_free (absolute);
    if (uri == NULL)
	FAIL (error->message);

    document = poppler_document_new_from_file (uri, NULL, &error);
    if (document == NULL)
	FAIL (error->message);

    page = poppler_document_get_page_by_label (document, page_label);
    if (page == NULL)
	FAIL ("page not found");

    poppler_page_get_size (page, &width, &height);

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
    cr = cairo_create (surface);
    cairo_surface_destroy (surface);

    poppler_page_render (page, cr);
    g_object_unref (page);

    cairo_set_operator (cr, CAIRO_OPERATOR_DEST_OVER);
    cairo_set_source_rgb (cr, 1., 1., 1.);
    cairo_paint (cr);

    status = cairo_surface_write_to_png (cairo_get_target (cr),
					 output_filename);
    cairo_destroy (cr);

    if (status)
	FAIL (cairo_status_to_string (status));

    return 0;
}
Example #30
0
static gboolean onCanvasDraw(GtkWidget *widget, cairo_t *cr,
		struct viewport *pp)
{
	(void)widget;

	int mypage_i, myfitmode = -1;
	gdouble w = 0, h = 0;
	gchar *title = NULL;
	gdouble popwidth, popheight;
	gdouble tx, ty;
	gdouble screen_ratio, page_ratio, scale;
	PopplerPage *page = NULL;

	/* no valid target size? */
	if (pp->width <= 0 || pp->height <= 0)
		return TRUE;

	/* decide which page to render - if any */
	mypage_i = pagenumForPort(pp);

	if (mypage_i < 0 || mypage_i >= doc_n_pages)
	{
		/* We don't do any drawing and set the frame's title to "X".
		 * Thus, we'll end up with an "empty" frame. */
		if (pp->frame != NULL)
			gtk_frame_set_label(GTK_FRAME(pp->frame), "X");

		return TRUE;
	}
	else
	{
		/* update frame title */
		if (pp->frame != NULL)
		{
			title = g_strdup_printf("Slide %d / %d", mypage_i + 1,
					doc_n_pages);
			gtk_frame_set_label(GTK_FRAME(pp->frame), title);
			g_free(title);
		}
	}

	/* if note-control is active, print current page number if on
	 * "main" frame. (don't do this on the beamer because it could be
	 * locked.)
	 * this allows you to attach any kind of other program or script
	 * which can show notes for a specific slide. simply pipe the
	 * output of pdfpres to your other tool.
	 */
	if (pp->offset == 0 && !pp->isBeamer)
	{
		printNote(doc_page + 1);
		if (runpref.do_notectrl)
		{
			printf("%d\n", doc_page + 1);
			fflush(stdout);
		}
	}

	/* Get the page and it's size from the document. */
	page = poppler_document_get_page(doc, mypage_i);
	poppler_page_get_size(page, &popwidth, &popheight);
	
	/* Set page number */
	if (pp->isBeamer)
	{
	    gtk_label_set_text(GTK_LABEL(curPageLabel), 
	        g_strdup_printf("%s/%d", poppler_page_get_label(page), doc_last_page));
	}

	/* Select fit mode. */
	page_ratio = popwidth / popheight;
	screen_ratio = (double)pp->width / (double)pp->height;

	if (runpref.fit_mode == FIT_PAGE)
	{
		/* That's it: Compare screen and page ratio. This
		 * will cover all 4 cases that could happen. */
		if (screen_ratio > page_ratio)
			myfitmode = FIT_HEIGHT;
		else
			myfitmode = FIT_WIDTH;
	}
	else
		myfitmode = runpref.fit_mode;

	switch (myfitmode)
	{
		case FIT_HEIGHT:
			/* Fit size. */
			h = pp->height;
			w = h * page_ratio;
			scale = h / popheight;
			/* Center page. */
			tx = (pp->width - popwidth * scale) * 0.5;
			ty = 0;
			break;

		case FIT_WIDTH:
			w = pp->width;
			h = w / page_ratio;
			scale = w / popwidth;
			tx = 0;
			ty = (pp->height - popheight * scale) * 0.5;
			break;
	}

	/* A black background on beamer frame. Push and pop cairo contexts, so we have a
	 * clean state afterwards. */
	if (pp->isBeamer) {
	    cairo_save(cr);
	    cairo_set_source_rgb(cr, 0, 0, 0);
	    cairo_rectangle(cr, 0, 0, pp->width, pp->height);
	    cairo_fill(cr);
	    cairo_restore(cr);
	    
	    /* center page on beamer */
	    cairo_translate(cr, tx, ty);
	} else {
	    cairo_translate(cr, tx, 0);
	}

	/* Render the page */
	cairo_scale(cr, scale, scale);
	poppler_page_render(page, cr);

	/* We no longer need that page. */
	g_object_unref(G_OBJECT(page));

	/* Nobody else draws to this widget. */
	return TRUE;
}