/// Same as draw_buffer, with only the curr_item's full text void ViewDrawingArea::render_full_article() { // Dimensions of drawing area Gtk::Allocation allocation = get_allocation(); const int height = allocation.get_height(); const int width = allocation.get_width(); Cairo::RefPtr<Cairo::Context> cr = _pixmap->create_cairo_context(); cr->reset_clip(); cr->rectangle (0.0, 0.0, width, height); cr->clip(); cr->set_source_rgb (1.0, 1.0, 1.0); cr->paint(); cr->set_source_rgb (0.0, 0.0, 0.0); Item *item = AppContext::get().get_curr_item(); item->make_display_unit(); ItemDisplayUnit *du = item->get_display_unit(); du->render (cr, 0, -_vadj->get_value()); cr->show_page(); double h = du->get_height(); if (h > height) _vadj->set_upper (h - height); else _vadj->set_upper (0); _vadj->set_page_size (height); _vadj->set_step_increment (height * 1.0/16.0); _vadj->set_page_increment (height * 15.0/16.0); _vadj->changed(); }
int drawCairo(const string& fname, const valarray<double>& Xin, const valarray<double>& Yin, const Hull& hull) { #ifdef CAIRO_HAS_SVG_SURFACE unsigned n=Xin.size(); assert(Yin.size()==n); // normalise coords to range 0-1 valarray<double> X=Xin, Y=Yin; X-=X.min(); Y-=Y.min(); X/=X.max(); Y/=Y.max(); Cairo::RefPtr<Cairo::SvgSurface> surface = Cairo::SvgSurface::create(fname, width+2*border, height+2*border); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); cr->save(); // save the state of the context cr->set_source_rgba(0.0, 0.0, 0.0, 0.7); // draw a circle at each coordinate for(unsigned i=0;i<n;i++) { dot(cr,xcoord(X[i]),ycoord(Y[i])); } cr->set_source_rgba(0.0, 0.0, 0.0, 0.3); cr->move_to(xcoord(X[hull[0]]),ycoord(Y[hull[0]])); for(unsigned i=1;i<hull.size();i++) { cr->line_to(xcoord(X[hull[i]]),ycoord(Y[hull[i]])); } cr->line_to(xcoord(X[hull[0]]),ycoord(Y[hull[0]])); cr->stroke(); cr->set_source_rgba(0.0, 0.0, 0.0, 1.); for(vector<unsigned>::const_iterator i=hull.begin();i!=hull.end();++i) { unsigned j=*i; stringstream ss; ss<<j; printf("p[%d]=(%f,%f)\n",j,X[j],Y[j]); cr->move_to(xcoord(X[j]),ycoord(Y[j])); cr->show_text(ss.str()); cr->stroke(); } cr->restore(); cr->show_page(); cout << "Wrote SVG file \"" << fname << "\"" << endl; return 0; #else cout << "You must compile cairo with SVG support for this example to work." << endl; return 1; #endif }
void ImageWidget::SaveSvg(const std::string &filename) { unsigned width = get_width(), height = get_height(); Cairo::RefPtr<Cairo::SvgSurface> surface = Cairo::SvgSurface::create(filename, width, height); Cairo::RefPtr<Cairo::Context> cairo = Cairo::Context::create(surface); if(HasImage()) { AOLogger::Debug << "Saving SVG of " << get_width() << " x " << get_height() << "\n"; update(cairo, width, height); } cairo->show_page(); surface->finish(); }
void ImageWidget::SavePdf(const std::string &filename) { unsigned width = get_width(), height = get_height(); Cairo::RefPtr<Cairo::PdfSurface> surface = Cairo::PdfSurface::create(filename, width, height); Cairo::RefPtr<Cairo::Context> cairo = Cairo::Context::create(surface); if(HasImage()) { AOLogger::Debug << "Saving PDF of " <<get_width() << " x " << get_height() << "\n"; update(cairo, width, height); } cairo->show_page(); // We finish the surface. This might be required, because some of the subclasses store the cairo context. In that // case, it won't be written. surface->finish(); }
int main() { #ifdef CAIRO_HAS_PDF_SURFACE std::string filename = "image.pdf"; int width = 600; int height = 400; Cairo::RefPtr<Cairo::PdfSurface> surface = Cairo::PdfSurface::create( filename, width, height); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); cr->save(); // save the state of the context cr->set_source_rgb(0.86, 0.85, 0.47); cr->paint(); // fill image with the color cr->restore(); // color is back to black now cr->save(); // draw a border around the image cr->set_line_width(20.0); // make the line wider cr->rectangle(0.0, 0.0, cairo_image_surface_get_width(surface->cobj()), height); cr->stroke(); cr->set_source_rgba(0.0, 0.0, 0.0, 0.7); // draw a circle in the center of the image cr->arc(width / 2.0, height / 2.0, height / 4.0, 0.0, 2.0 * M_PI); cr->stroke(); // draw a diagonal line cr->move_to(width / 4.0, height / 4.0); cr->line_to(width * 3.0 / 4.0, height * 3.0 / 4.0); cr->stroke(); cr->restore(); cr->show_page(); std::cout << "Wrote PDF file \"" << filename << "\"" << std::endl; return 0; #else std::cout << "You must compile cairo with PDF support for this example to work." << std::endl; return 1; #endif }
void guiRenderer2D::takeScreenshot(const string& filename) { // PDF document size in points int pdfWidth = 500; int pdfHeight = 500; // Save window size int tmpWidgetWidth = m_widgetWidth; int tmpWidgetHeight = m_widgetHeight; rescaleSensorLayout(pdfWidth, pdfHeight); Cairo::RefPtr<Cairo::PdfSurface> surface = Cairo::PdfSurface::create(filename, pdfWidth, pdfHeight); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); drawMatrices(cr, pdfWidth, pdfHeight, true); cr->show_page(); // Restore window size m_widgetWidth = tmpWidgetWidth; m_widgetHeight = tmpWidgetHeight; rescaleSensorLayout(m_widgetWidth, m_widgetWidth); }
bool ViewDrawingArea::on_configure_event (GdkEventConfigure *event) { #ifdef DEBUG std::cerr << "on_configure_event()" << std::endl << std::flush; #endif if (_pixmap) _pixmap.clear(); const Gtk::Allocation& all = get_allocation(); _pixmap = Gdk::Pixmap::create (get_window(), all.get_width(), all.get_height()); if (AppContext::get().get_feed() == NULL) { Cairo::RefPtr<Cairo::Context> cr = _pixmap->create_cairo_context(); cr->set_source_rgb (1.0, 1.0, 1.0); cr->paint(); cr->show_page(); } else { const AppContext& ac = AppContext::get(); if (ac.get_display_type() == FULL) ac.get_curr_item()->reset_display_unit(); else { Feed *feed = ac.get_feed(); const item_list_t items = feed->get_items(); for_each (items.begin(), items.end(), std::mem_fun (&Item::reset_display_unit)); } } draw_buffer(); return true; }
void ViewDrawingArea::draw_buffer() { #ifdef DEBUG std::cerr << "VA->val:"<<_vadj->get_value() <<" VA->upper:"<<_vadj->get_upper() << std::endl << std::flush; #endif Feed *feed = AppContext::get().get_feed(); if (feed == NULL) return; if (AppContext::get().get_display_type() == FULL) { render_full_article(); return; } // Dimensions of drawing area Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); // First determine start item Cairo::RefPtr<Cairo::Context> cr = _pixmap->create_cairo_context(); #ifdef DEBUG /// This code was meant to investigate the 500ms delay with pango /// at start of rendering. if (!_layout_prepared) { Glib::Timer sw; sw.start(); Glib::RefPtr<Pango::Layout> pl = Pango::Layout::create (cr); const char *fonts[] = {"Sans 10", "Sans 14"}; for (unsigned i = 0; i < sizeof(fonts)/sizeof(const char*); ++i) { Pango::FontDescription fdesc (fonts[i]); pl->set_font_description (fdesc); pl->set_width (10 * Pango::SCALE); // force line break pl->set_markup ("<b>Show Us Your Freaky Geek Costumes</b>\nHitting the streets in a scary, tech-themed outfit this Halloween? We want to see it. Find out how your Sergey Brin costume (or is it David Duchovny?) could be featured on Wired News. test\n test"); Pango::Rectangle irect, lrect; pl->get_extents (irect, lrect); } _layout_prepared = true; sw.stop(); unsigned long l; sw.elapsed(l); std::cerr << "Time spent rendering: " << l << " us" << std::endl << std::flush; } #endif const item_list_t items = feed->get_items(); item_list_t::const_iterator start_item; const int n_items = items.size(); double y = 0.0; if (n_items == 0) start_item = items.end(); else { // Set start_item and h int start = static_cast<int> (_vadj->get_value()); double h = 0.0; if (_vadj->get_value() > 0 && _vadj->get_value() == _vadj->get_upper()) { // This will give a blank page when pulling the handle // full down. While unusual, it might make sense with // an ever-growing newslist. y = 0.0; start_item = items.end(); } else { start_item = items.begin(); for (int i = 0; i < start; ++i) ++start_item; if (start < 0) start = 0; double prop = _vadj->get_value() - start; if (prop > 0.0) { (*start_item)->make_display_unit(); ItemDisplayUnit *du = (*start_item)->get_display_unit(); du->layout (cr); h = du->get_height(); } y = - h * prop; #ifdef DEBUG std::cerr << "prop:"<<prop <<" y:"<<y << std::endl << std::flush; #endif } } // clip to the area indicated by the expose event so that we only redraw // the portion of the window that needs to be redrawn cr->reset_clip(); cr->rectangle (0.0, 0.0, width, height); cr->clip(); // Black on white cr->set_source_rgb (1.0, 1.0, 1.0); cr->paint(); cr->set_source_rgb (0.0, 0.0, 0.0); // Render text item_list_t::const_iterator it = start_item; _top_item = *start_item; _topitem_y = y; int count = 0; for (; it != items.end() && y < height; ++it, ++count) { (*it)->make_display_unit(); ItemDisplayUnit *du = (*it)->get_display_unit(); du->render (cr, 0, y); y += du->get_height(); } cr->show_page(); // Scrollbar settings can now be specified _vadj->set_upper (n_items); //_vadj->set_page_size (count - 1 + ); _vadj->set_step_increment (1); _vadj->set_page_increment (count); _vadj->changed(); }
void OutputFile::generate() { unsigned E=es.size(); bool cleanupRoutes=false; if(routes==NULL) { cleanupRoutes=true; routes = new vector<straightener::Route*>(E); for(unsigned i=0;i<E;i++) { straightener::Route* r=new straightener::Route(2); r->xs[0]=rs[es[i].first]->getCentreX(); r->ys[0]=rs[es[i].first]->getCentreY(); r->xs[1]=rs[es[i].second]->getCentreX(); r->ys[1]=rs[es[i].second]->getCentreY(); (*routes)[i]=r; } } #if defined (CAIRO_HAS_SVG_SURFACE) && defined (CAIRO_HAS_PDF_SURFACE) double width,height,r=2; if(rects) r=rs[0]->width()/2; double xmin=DBL_MAX, ymin=xmin; double xmax=-DBL_MAX, ymax=xmax; for (unsigned i=0;i<rs.size();i++) { double x=rs[i]->getCentreX(), y=rs[i]->getCentreY(); xmin=min(xmin,x); ymin=min(ymin,y); xmax=max(xmax,x); ymax=max(ymax,y); } xmax+=2*r; ymax+=2*r; xmin-=2*r; ymin-=2*r; width=xmax-xmin; height=ymax-ymin; Cairo::RefPtr<Cairo::Context> cr; openCairo(cr,width,height); /* set background colour cr->save(); // save the state of the context cr->set_source_rgb(0.86, 0.85, 0.47); cr->paint(); // fill image with the color cr->restore(); // color is back to black now */ cr->set_line_width(1.); cr->set_font_size(8); cr->save(); if(rc) for(Clusters::const_iterator c=rc->clusters.begin();c!=rc->clusters.end();c++) { draw_cluster_boundary(cr,**c,xmin,ymin); } if(curvedEdges) draw_curved_edges(cr,es,xmin,ymin); else draw_edges(cr,*routes,xmin,ymin); Cairo::TextExtents te; for (unsigned i=0;i<rs.size();i++) { if(!rects) { double x=rs[i]->getCentreX()-xmin, y=rs[i]->getCentreY()-ymin; cr->arc(x,y,r, 0.0, 2.0 * M_PI); cr->fill(); } else { double x=rs[i]->getMinX()-xmin+0.5, y=rs[i]->getMinY()-ymin+0.5; std::string str; if(labels.size()==rs.size()) { str=labels[i]; } else { std::stringstream s; s<<i; str=s.str(); } cr->get_text_extents(str,te); /* double llx = x-te.width/2.-1; double lly = y-te.height/2.-1; cr->rectangle(llx,lly,te.width+2,te.height+2); */ cr->rectangle(x,y, rs[i]->width()-1,rs[i]->height()-1); cr->stroke_preserve(); cr->save(); cr->set_source_rgba(245./255., 233./255., 177./255., 0.6); cr->fill(); cr->restore(); if(labels.size()==rs.size()) { cr->move_to(x-te.x_bearing+te.width/2.,y-te.y_bearing+te.height/2.); cr->show_text(str); } cr->stroke(); } } cr->show_page(); std::cout << "Wrote file \"" << fname << "\"" << std::endl; #else std::cout << "WARNING: cola::OutputFile::generate(): No SVG file produced." << std::endl << " You must have cairomm (and cairo with SVG support) " << "this to work." << std::endl; #endif if(cleanupRoutes) { for(unsigned i=0;i<E;i++) { delete (*routes)[i]; } delete routes; } }