Exemplo n.º 1
0
bool ImageHandler::insertImage(GFile * file, double x, double y) {
	XOJ_CHECK_TYPE(ImageHandler);

	GError * err = NULL;
	GFileInputStream * in = g_file_read(file, NULL, &err);

	g_object_unref(file);

	GdkPixbuf * pixbuf = NULL;

	if (!err) {
		pixbuf = gdk_pixbuf_new_from_stream(G_INPUT_STREAM(in), NULL, &err);
		g_input_stream_close(G_INPUT_STREAM(in), NULL, NULL);
	} else {
		GtkWidget * dialog = gtk_message_dialog_new((GtkWindow*) *control->getWindow(), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK,
				_("This image could not be loaded. Error message: %s"), err->message);
		gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(this->control->getWindow()->getWindow()));
		gtk_dialog_run(GTK_DIALOG(dialog));
		gtk_widget_destroy(dialog);
		g_error_free(err);
		return false;
	}

	Image * img = new Image();
	img->setX(x);
	img->setY(y);
	img->setImage(pixbuf);

	int width = gdk_pixbuf_get_width(pixbuf);
	int height = gdk_pixbuf_get_height(pixbuf);
	gdk_pixbuf_unref(pixbuf);

	double zoom = 1;

	PageRef page = view->getPage();

	if (x + width > page.getWidth() || y + height > page.getHeight()) {
		double maxZoomX = (page.getWidth() - x) / width;
		double maxZoomY = (page.getHeight() - y) / height;

		if (maxZoomX < maxZoomY) {
			zoom = maxZoomX;
		} else {
			zoom = maxZoomY;
		}
	}

	img->setWidth(width * zoom);
	img->setHeight(height * zoom);

	page.getSelectedLayer()->addElement(img);

	InsertUndoAction * insertUndo = new InsertUndoAction(page, page.getSelectedLayer(), img, view);
	control->getUndoRedoHandler()->addUndoAction(insertUndo);

	view->rerenderElement(img);

	return true;
}
Exemplo n.º 2
0
bool PdfExport::writePagesindex()
{
    XOJ_CHECK_TYPE(PdfExport);

    this->xref->setXref(1, this->writer->getDataCount());
    //Pages root
    this->writer->write("1 0 obj\n");
    this->writer->write("<</Type /Pages\n");
    this->writer->write("/Kids [");

    int pageCount = 0;
    for (GList* l = this->pageIds; l != NULL; l = l->next)
    {
        int id = *((int*) l->data);
        this->writer->writef("%i 0 R ", id);
        pageCount++;
    }
    this->writer->write("]\n");
    this->writer->writef("/Count %i\n", pageCount);

    PageRef page = doc->getPage(0);

    this->writer->writef("/MediaBox [0 0 %.2F %.2F]\n", page->getWidth(),
                         page->getHeight());
    this->writer->write(">>\n");
    this->writer->write("endobj\n");

    return true;
}
Exemplo n.º 3
0
/**
 * Drawing first step
 * @param page The page to draw
 * @param cr Draw to thgis context
 * @param dontRenderEditingStroke false to draw currently drawing stroke
 */
void DocumentView::initDrawing(PageRef page, cairo_t* cr, bool dontRenderEditingStroke)
{
	XOJ_CHECK_TYPE(DocumentView);

	this->cr = cr;
	this->page = page;
	this->width = page->getWidth();
	this->height = page->getHeight();
	this->dontRenderEditingStroke = dontRenderEditingStroke;
}
Exemplo n.º 4
0
bool PdfExport::writePage(int pageNr)
{
    XOJ_CHECK_TYPE(PdfExport);

    PageRef page = doc->getPage(pageNr);

    if (!page)
    {
        return false;
    }

    int* pageId = (int*) g_malloc(sizeof(int));
    *pageId = this->writer->getObjectId();
    this->pageIds = g_list_append(this->pageIds, pageId);

    this->writer->writeObj();
    this->writer->write("<</Type /Page\n");
    this->writer->write("/Parent 1 0 R\n");

    this->writer->writef("/MediaBox [0 0 %.2F %.2F]\n", page->getWidth(),
                         page->getHeight());
    this->writer->write("/Resources 2 0 R\n");
    //	if (isset($this->PageLinks[$n])) {
    //		//Links
    //		$annots = '/Annots [';
    //foreach	($this->PageLinks[$n] as $pl)
    //	{
    //		$rect=sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
    //		$annots.='<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
    //		if(is_string($pl[4]))
    //		$annots.='/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
    //		else
    //		{
    //			$l=$this->links[$pl[4]];
    //			$h=isset($this->PageSizes[$l[0]]) ? $this->PageSizes[$l[0]][1] : $hPt;
    //			$annots.=sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',1+2*$l[0],$h-$l[1]*$this->k);
    //		}
    //	}
    //	$this->_out($annots.']');
    //}
    this->writer->writef("/Contents %i 0 R>>\n", this->writer->getObjectId());
    this->writer->write("endobj\n");
    //Page content
    this->writer->writeObj();

    this->writer->startStream();

    addPopplerDocument(doc->getPdfDocument());
    currentPdfDoc = doc->getPdfDocument();

    if (page->getBackgroundType() == BACKGROUND_TYPE_PDF)
    {
        XojPopplerPage* pdf = doc->getPdfPage(page->getPdfPageNr());
        if (!addPopplerPage(pdf, currentPdfDoc))
        {
            return false;
        }
    }

    currentPdfDoc = cPdf.getDocument();
    if (!addPopplerPage(cPdf.getPage(pageNr), currentPdfDoc))
    {
        return false;
    }

    this->writer->endStream();

    this->writer->write("endobj\n");

    return true;
}
Exemplo n.º 5
0
void PdfBookmarks::writeOutlines(Document * doc, PdfWriter * writer, int * outlineRoot, GList * pageIds) {
	XOJ_CHECK_TYPE(PdfBookmarks);

	GtkTreeModel * model = doc->getContentsModel();
	if (!model) {
		return;
	}

	GList * bookmarkList = exportBookmarksFromTreeModel(model, doc);
	int bookmarksLenght = g_list_length(bookmarkList);

	if (bookmarksLenght == 0) {
		return;
	}

	Bookmark ** bookmarks = new Bookmark *[bookmarksLenght];

	int maxLevel = 0;
	int i = 0;
	for (GList * l = bookmarkList; l != NULL; l = l->next) {
		Bookmark * b = (Bookmark *) l->data;
		if (maxLevel < b->level) {
			maxLevel = b->level;
		}
		bookmarks[i++] = b;
	}

	int * levels = new int[maxLevel + 1];
	for (int u = 0; u < maxLevel + 1; u++) {
		levels[u] = 0;
	}

	int level = 0;
	for (i = 0; i < bookmarksLenght; i++) {
		Bookmark * b = bookmarks[i];
		if (b->level > 0) {
			int parent = levels[b->level - 1];
			//Set parent and last pointers
			b->parent = parent;
			bookmarks[parent]->last = i;
			if (b->level > level) {
				//Level increasing: set first pointer
				bookmarks[parent]->first = i;
			}
		} else {
			bookmarks[i]->parent = bookmarksLenght;
		}

		if (b->level <= level && i > 0) {
			//Set prev and next pointers
			int prev = levels[b->level];
			bookmarks[prev]->next = i;
			bookmarks[i]->prev = prev;
		}
		levels[b->level] = i;
		level = b->level;
	}

	int n = writer->getObjectId();

	for (i = 0; i < bookmarksLenght; i++) {
		Bookmark * b = bookmarks[i];
		writer->writeObj();

		writer->write("<<\n/Title ");
		writer->writeTxt(b->name.c_str());
		writer->write("\n");

		writer->write("/Parent ");
		writer->write(n + b->parent);
		writer->write(" 0 R\n");

		if (b->prev != -1) {
			writer->write("/Prev ");
			writer->write(n + b->prev);
			writer->write(" 0 R\n");
		}

		if (b->next != -1) {
			writer->write("/Next ");
			writer->write(n + b->next);
			writer->write(" 0 R\n");
		}

		if (b->first != -1) {
			writer->write("/First ");
			writer->write(n + b->first);
			writer->write(" 0 R\n");
		}

		if (b->last != -1) {
			writer->write("/Last ");
			writer->write(n + b->last);
			writer->write(" 0 R\n");
		}

		PageRef p = doc->getPage(b->page);
		float top = 0;
		if (p.isValid()) {
			top = (p.getHeight() - b->top) * 72;
		}

		//Outline items
		char buffer[256];

		int pObjId = 0;
		int * pObjIdPtr = (int *)g_list_nth_data(pageIds, b->page);
		if (pObjIdPtr) {
			pObjId = *pObjIdPtr;
		}

		sprintf(buffer, "/Dest [%d 0 R /XYZ 0 %.2f null]\n", pObjId, top /*($this->h-$o['y'])*$this->k*/);

		writer->write(buffer);
		writer->write("/Count 0\n>>\n");
		writer->write("endobj\n");
	}

	//Outline root
	writer->writeObj();
	*outlineRoot = writer->getObjectId() - 1;
	writer->write("<<\n/Type /Outlines /First ");
	writer->write(n);
	writer->write(" 0 R\n");
	writer->write("/Last ");
	writer->write(n + levels[0]);
	writer->write(" 0 R\n>>\n");
	writer->write("endobj\n");

	for (i = 0; i < bookmarksLenght; i++) {
		delete bookmarks[i];
	}

	delete[] bookmarks;
	delete[] levels;

	return;
}