void ActivityDrawingArea::drawAxis() { // draw a reference axis and pod info Glib::RefPtr < Gdk::Window > window = get_window(); Cairo::RefPtr < Cairo::Context > cr = window->create_cairo_context(); Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); //draw axis cr->save(); cr->set_line_width(2.0); this->setSourceRGB(cr, currentColourScheme.getAxisColour()); cr->set_font_size(12); cr->move_to(0, height / 2); cr->line_to(width, height / 2); cr->move_to(190, 25 + height / 2); cr->show_text("20"); cr->move_to(390, 25 + height / 2); cr->show_text("40"); cr->move_to(590, 25 + height / 2); cr->show_text("60"); cr->move_to(790, 25 + height / 2); cr->show_text("80"); cr->stroke(); cr->restore(); }
void Format7DrawingArea::DrawImageDimensionsText( Cairo::RefPtr<Cairo::Context> refCairo, unsigned int left, unsigned int top, unsigned int width, unsigned int height ) { refCairo->save(); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 10 ); // Set draw color to black refCairo->set_source_rgb(0.0, 0.0, 0.0); // Get width / height of widget int widgetWidth = 0; int widgetHeight = 0; get_window()->get_size( widgetWidth, widgetHeight ); // Create text for image offset char imageOffsets[128]; sprintf( imageOffsets, "Start: (%d,%d) End: (%d,%d)", left, top, left + width, top + height ); Cairo::TextExtents offsetExtents; refCairo->get_text_extents(imageOffsets, offsetExtents); // Draw the offset text refCairo->move_to( (widgetWidth/2) - (offsetExtents.width/2), (widgetHeight/2) - offsetExtents.height - (offsetExtents.height/2)); refCairo->show_text( imageOffsets ); // Create text for image dimensions char imageDimensions[128]; sprintf( imageDimensions, "Dimensions: %d x %d", width, height); Cairo::TextExtents dimensionsExtents; refCairo->get_text_extents(imageDimensions, dimensionsExtents); // Draw the dimensions text refCairo->move_to( (widgetWidth/2) - (dimensionsExtents.width/2), (widgetHeight/2) + dimensionsExtents.height + (dimensionsExtents.height/2)); refCairo->show_text( imageDimensions ); refCairo->restore(); }
void Operation::draw_title(const Glib::RefPtr<Gtk::PrintContext>& print) { double pos_x, pos_y; Cairo::RefPtr<Cairo::Context> cairo = print->get_cairo_context(); DICOM::SummaryInfo* info = const_cast<DICOM::SummaryInfo*>(&dicom_info_); DICOM::StudyInfo* study = info->get_study_info(); // Show Institution Name cairo->set_font_size(title_font_size_); Cairo::TextExtents cur_ext, prev_ext; cairo->get_text_extents( study->get_institution_name(), cur_ext); pos_x = (width_ - cur_ext.width) / 2.; pos_y = cur_ext.height + OFFSET; cairo->move_to( pos_x, pos_y); cairo->show_text(study->get_institution_name()); // Show Institution Address prev_ext = cur_ext; cairo->set_font_size(subtitle_font_size_); cairo->get_text_extents( study->get_institution_address(), cur_ext); pos_x = -(prev_ext.width + prev_ext.x_bearing + cur_ext.width + cur_ext.x_bearing) / 2.; pos_y = cur_ext.height + OFFSET; cairo->rel_move_to( pos_x, pos_y); cairo->show_text(study->get_institution_address()); // Show Study Description prev_ext = cur_ext; cairo->set_font_size(title_font_size_); cairo->get_text_extents( study->get_description(), cur_ext); pos_x = -(prev_ext.width + prev_ext.x_bearing + cur_ext.width + cur_ext.x_bearing) / 2.; pos_y = cur_ext.height + OFFSET; cairo->rel_move_to( pos_x, pos_y); cairo->show_text(study->get_description()); pos_x = -(width_ + cur_ext.width) / 2.; pos_y = cur_ext.height + OFFSET; cairo->rel_move_to( pos_x, pos_y); cairo->stroke_preserve(); }
bool ButtonWidget::on_expose_event(GdkEventExpose* event) { Gtk::DrawingArea::on_expose_event(event); Glib::RefPtr<Gdk::Window> window = get_window(); if(window) { Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); int w = get_allocation().get_width() - 10; int h = get_allocation().get_height() - 10; cr->set_source_rgb(0.0, 0.0, 0.0); cr->set_line_width(1.0); cr->translate(5, 5); cr->rectangle(0, 0, w, h); if (down) cr->fill_preserve(); cr->stroke(); if (down) cr->set_source_rgb(1.0, 1.0, 1.0); // FIXME: There are better ways to center text if (name.size() == 2) cr->move_to(w/2-6, h/2+3); else cr->move_to(w/2-4, h/2+3); cr->show_text(name); } return true; }
void Format7DrawingArea::DrawDisabledText(Cairo::RefPtr<Cairo::Context> refCairo) { refCairo->save(); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 10 ); // Set draw color to black refCairo->set_source_rgb(0.0, 0.0, 0.0); // Print current cursor position char cursorPosition[128]; sprintf( cursorPosition, "Custom Image is not supported by this camera."); // Get width / height of widget int width; int height; get_window()->get_size( width, height ); Cairo::TextExtents textExtents; refCairo->get_text_extents( cursorPosition, textExtents ); refCairo->move_to( (width/2) - (textExtents.width/2), (height/2) + textExtents.height + (textExtents.height/2)); refCairo->show_text( cursorPosition ); refCairo->restore(); }
int main(int, char**) { Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create( Cairo::FORMAT_ARGB32, WIDTH, HEIGHT); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); // fill background in white cr->set_source_rgb(1.0, 1.0, 1.0); cr->paint(); // draw a little dot at the point where text will be drawn cr->arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2 * M_PI); cr->set_source_rgba(0.0, 1.0, 0.0, 0.5); cr->fill(); // draw the text cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y); cr->set_source_rgb(0.8, 0.2, 0.2); Cairo::RefPtr<Cairo::ToyFontFace> font = Cairo::ToyFontFace::create( "Bitstream Charter", Cairo::FONT_SLANT_ITALIC, Cairo::FONT_WEIGHT_BOLD); cr->set_font_face(font); cr->set_font_size(FONT_SIZE); cr->show_text("cairomm!"); surface->write_to_png("toy-text.png"); return 0; }
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 ICLayerLineString::draw(Cairo::RefPtr<Cairo::Context> cr, double scale, std::set<int> select, bool DisplayID, double Alpha) { std::map<int, ICLayerObject*>::iterator it; for (it = m_ICLayerObject.begin(); it != m_ICLayerObject.end(); it++) { if ((*it).second->selfIdExisting()) { bool isSelect = false; if (!select.empty()) { std::set<int>::iterator it2; it2 = select.find((*it).first); if (it2 != select.end() && (*it2) == (*it).first) { drawLine(cr, (*it).second->getOGRGeometryObject(), scale, true); isSelect = true; } else drawLine(cr, (*it).second->getOGRGeometryObject(), scale, false); } else drawLine(cr, (*it).second->getOGRGeometryObject(), scale, false); if (DisplayID) { Cairo::TextExtents extents; std::stringstream str; str << (*it).first; std::string text = str.str(); cr->select_font_face("Bitstream Vera Sans, Arial", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_size(12 / scale); Cairo::FontOptions font_options; font_options.set_hint_style(Cairo::HINT_STYLE_NONE); font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF); font_options.set_antialias(Cairo::ANTIALIAS_GRAY); cr->set_font_options(font_options); cr->save(); cr->get_text_extents(text, extents); cr->move_to((*it).second->getCentroid().first, (*it).second->getCentroid().second); cr->scale(1, -1); if (isSelect) cr->set_source_rgba(0, 0, 0, Alpha); cr->show_text(text); cr->stroke(); cr->restore(); } } } }
int main(int, char**) { Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create( Cairo::FORMAT_ARGB32, WIDTH, HEIGHT); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); // fill background in white cr->set_source_rgb(1.0, 1.0, 1.0); cr->paint(); // draw a little dot at the point where text will be drawn cr->arc(TEXT_ORIGIN_X, TEXT_ORIGIN_Y, FONT_SIZE / 4.0, 0, 2 * M_PI); cr->set_source_rgba(0.0, 1.0, 0.0, 0.5); cr->fill(); // draw the text cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y); cr->set_source_rgb(0.8, 0.2, 0.2); Cairo::RefPtr<BoxFontFace> font = BoxFontFace::create(); cr->set_font_face(font); cr->set_font_size(FONT_SIZE); cr->show_text("cairomm!"); // Now show it with the toy text API to // demonstrate how the glyphs match up cr->move_to(TEXT_ORIGIN_X, TEXT_ORIGIN_Y); cr->set_source_rgba(0.2, 0.2, 0.2, 0.3); Cairo::RefPtr<Cairo::ToyFontFace> toy_font = Cairo::ToyFontFace::create( "Bitstream Charter", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD); cr->set_font_face(toy_font); cr->set_font_size(FONT_SIZE); cr->show_text("cairomm!"); const char* filename = "user-font.png"; try { surface->write_to_png(filename); std::cout << "Wrote Image " << filename << std::endl; return 0; } catch (const std::exception& e) { std::cout << "** Unable to write Image " << filename << std::endl; return 1; } }
void NodeActivityDrawingArea::drawText() { ActivityDrawingArea::drawText(); // This is where we draw on the window Glib::RefPtr < Gdk::Window > window = get_window(); Cairo::RefPtr < Cairo::Context > cr = window->create_cairo_context(); Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); cr->save(); cr->set_line_width(2.0); cr->set_source_rgb(1, 1, 1); cr->set_font_size(12); { cr->move_to(width / 5, 20); std::stringstream ss; ss << "ID"; if (this->isPrimaryInput == true) { ss << " (PIN)"; } if (this->isPrimaryOutput == true) { ss << " (POUT)"; } ss<< ": " <<node->getUUIDString(); cr->show_text(ss.str()); } { std::stringstream ss; cr->move_to(width / 5, 40); int impulse_count = node->getImpulses().getSize(); int inputs_count = node->getConnector().getInputs().size(); int outputs_count = node->getConnector().getOutputs().size(); double threshold = node->getActivityThreshold(); ss << "Impulses: " << impulse_count; ss << " Inputs: " << inputs_count; ss << " Outputs: " << outputs_count; ss << " Threshold: " << threshold; cr->show_text(ss.str()); } cr->restore(); }
void cairo::draw_centered_text( const Cairo::RefPtr<Cairo::Context>& cr, const std::string& utf8, const cairo_coord_t& pos ) { cr->save(); Cairo::TextExtents texts; cr->get_text_extents( utf8, texts ); cr->move_to( pos[0] - texts.width/2. - texts.x_bearing , pos[1] ); cr->show_text( utf8 ); cr->restore(); }
void ConnectionActivityDrawingArea::drawText() { ActivityDrawingArea::drawText(); // This is where we draw on the window Glib::RefPtr < Gdk::Window > window = get_window(); Cairo::RefPtr < Cairo::Context > cr = window->create_cairo_context(); Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); cr->save(); cr->set_line_width(2.0); cr->set_source_rgb(1, 1, 1); cr->set_font_size(12); { cr->move_to(width / 5, 20); std::stringstream ss; ss << "ID"; if (this->isPrimaryInput == true) { ss << " (PIN)"; } if (this->isPrimaryOutput == true) { ss << " (POUT)"; } ss << ": " << connection->getUUIDString(); cr->show_text(ss.str()); } { std::stringstream ss; cr->move_to(width / 5, 40); ss << "Impulses: " << connection->getImpulses().getSize(); // <<" compression: "<<fibril->getMutator().getCompression() // <<" delay: " <<fibril->getMutator().getDelay(); cr->show_text(ss.str()); } cr->restore(); }
bool GMasterReturn::on_expose_event(GdkEventExpose* event) { // This is where we draw on the window Glib::RefPtr<Gdk::Window> window = get_window(); if(window) // Only run if Window does exist { // clip to the area indicated by the expose event so that we only redraw // the portion of the window that needs to be redrawn Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->clip(); cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->set_source_rgb(0.1 , 0.1 , 0.1 ); cr->fill(); Dial(cr, true, 35 , 0, value, DIAL_MODE_SEND ); // fader // Dial text "A" cr->select_font_face ("Impact" , Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD); cr->set_font_size ( 20 ); cr->move_to ( 10, 28); switch ( ID ) { case 0: setColour(cr, COLOUR_BLUE_1 ); cr->show_text ( "A" ); break; case 1: setColour(cr, COLOUR_BLUE_1 ); cr->show_text ( "B" ); break; case 2: setColour(cr, COLOUR_BLUE_1 ); cr->show_text ( "C" ); break; } } return true; }
void ConnectionActivityDrawingArea::drawAxis() { // draw a reference axis and pod info Glib::RefPtr < Gdk::Window > window = get_window(); Cairo::RefPtr < Cairo::Context > cr = window->create_cairo_context(); Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height(); double max_delay = connection->getActivityTimer()->getStartingDelay(); //draw axis cr->save(); cr->set_line_width(2.0); this->setSourceRGB(cr, currentColourScheme.getAxisColour()); cr->set_font_size(12); cr->move_to(0, height / 2); cr->line_to(width, height / 2); { std::stringstream ss_temp; ss_temp << max_delay / 2; cr->move_to(-40 + width / 2, 25 + height / 2); cr->show_text(ss_temp.str()); } { std::stringstream ss_temp; ss_temp << max_delay; cr->move_to(width - 40, 25 + height / 2); cr->show_text(ss_temp.str()); } cr->stroke(); cr->restore(); }
/* Draw the word cairo at NUM_TEXT different angles */ void draw(Cairo::RefPtr<Cairo::Context> cr, int width, int height) { int i, x_off, y_off; Cairo::TextExtents extents; std::string text("cairo"); cr->select_font_face("Bitstream Vera Sans", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_size(TEXT_SIZE); Cairo::FontOptions font_options; font_options.set_hint_style(Cairo::HINT_STYLE_NONE); font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF); font_options.set_antialias(Cairo::ANTIALIAS_GRAY); cr->set_font_options(font_options); cr->set_source_rgb(0.0, 0.0, 0.0); cr->translate(width / 2.0, height / 2.0); cr->get_text_extents(text, extents); if (NUM_TEXT == 1) { x_off = y_off = 0; } else { y_off = (int) - floor(0.5 + extents.height / 2.0); x_off = (int) floor(0.5 + (extents.height + 1.0) / (2.0 * tan (M_PI / NUM_TEXT))); } for (i=0; i < NUM_TEXT; i++) { cr->save(); cr->rotate(2 * M_PI * i / NUM_TEXT); cr->set_line_width(1.0); cr->set_source_rgb(1, 0, 0); cr->stroke(); cr->move_to(x_off - extents.x_bearing, y_off - extents.y_bearing); cr->set_source_rgb(0, 0, 0); cr->show_text("Five Lights!"); cr->restore(); } }
void renderLabels(Cairo::RefPtr<Cairo::Context> cr, std::vector<std::pair<string, FloatPoint> >& toPlace) { cr->save(); cr->set_source_rgba(0.0, 0.0, 0.0, 0.5); Cairo::RefPtr<Cairo::ToyFontFace> font = Cairo::ToyFontFace::create(DEFAULT_FONT, Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_face(font); cr->set_font_size(120.0); cr->set_line_width(2.0); Cairo::TextExtents textSize; std::list<shared_ptr<Label> > labels; int i = 0; std::vector<shared_ptr<Style>> styles; for (auto& pair : toPlace) { string& text = pair.first; cr->get_text_extents(text, textSize); shared_ptr<Style> s = boost::make_shared<Style>(); s->text = text; styles.push_back(s); FloatPoint center = pair.second + FloatPoint(textSize.width/2.0, textSize.height/2.0); FloatRect owner = FloatRect(center.x, center.y, center.x, center.y); FloatPoint origin = pair.second - FloatPoint(textSize.x_bearing, textSize.y_bearing); shared_ptr<Label> l = boost::make_shared<Label>(FloatRect(pair.second, textSize.width, textSize.height), owner, s->text, s.get(), origin); cr->rectangle(l->box.minX, l->box.minY, l->box.getWidth(), l->box.getHeight()); cr->stroke(); labels.push_back(l); } std::vector<shared_ptr<Label> > placed; placeLabels(labels, placed); for (auto& l: placed) { cr->set_source_rgba(0.0, 0.0, 0.0, 1.0); cr->move_to(l->box.minX, l->box.maxY); cr->show_text(l->style->text.str()); cr->fill(); cr->set_source_rgba(1.0, 0.0, 0.0, 0.5); cr->rectangle(l->box.minX, l->box.minY, l->box.getWidth(), l->box.getHeight()); cr->fill(); } cr->restore(); }
void ConsoleWindow::draw(Cairo::RefPtr<Cairo::Context> ctx) { ctx->save(); ctx->set_source_rgba(0, 0, 0, 0.8); ctx->paint(); ctx->set_source_rgba(1, 1, 1, 0.3); ctx->move_to(0, height - 0.5); ctx->line_to(width, height - 0.5); ctx->set_line_width(1); ctx->stroke(); ctx->restore(); ctx->move_to(10, 16); ctx->set_source_rgb(1, 1, 1); ctx->select_font_face("Menlo", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); ctx->set_font_size(13); ctx->show_text(input); }
void TitleBar( Cairo::RefPtr<Cairo::Context> cr, float x, float y, float xS, float yS, std::string name, bool active) { if ( active ) setColour(cr, COLOUR_GREEN_1 ); else setColour(cr, COLOUR_GREY_1 ); cr->rectangle(x,y, xS, 15); cr->set_line_width(2.8); cr->fill(); cr->select_font_face ("Impact" , Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_size ( 13 ); cr->move_to ( x + 8, y + 13 ); cr->set_source_rgb( 0 / 255.f, 0/255.f , 0/255.f ); cr->show_text ( name ); //std::cout << "LupppWidget::TitleBar() called!" << std::endl; }
void Format7DrawingArea::DrawCurrentCursorPositionText( Cairo::RefPtr<Cairo::Context> refCairo, unsigned int currX, unsigned int currY ) { refCairo->save(); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 10 ); // Set draw color to black refCairo->set_source_rgb(0.0, 0.0, 0.0); // Print current cursor position char cursorPosition[128]; sprintf( cursorPosition, "Cursor: (%d, %d)", currX, currY ); // Get width / height of widget int width; int height; get_window()->get_size( width, height ); Cairo::TextExtents textExtents; refCairo->get_text_extents( cursorPosition, textExtents ); refCairo->move_to( (width / 2) - (textExtents.width / 2), (height * 0.9) - (textExtents.height / 2) ); refCairo->show_text( cursorPosition ); refCairo->restore(); }
void HistogramDrawingArea::DrawHistogramGridLabels( Cairo::RefPtr<Cairo::Context> refCairo ) { // Reserve the outside 10% float paddedLeft, paddedTop, paddedWidth, paddedHeight; GetPaddedSize( paddedLeft, paddedTop, paddedWidth, paddedHeight ); refCairo->save(); // Set draw color refCairo->set_source_rgb(0, 0, 0); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 10 ); const int k_numPartitions = 8; // Render the x-axis labels for ( int i=0; i <= k_numPartitions; i++ ) { const float fraction = i / static_cast<float>(k_numPartitions); unsigned int maxNumPixelValues = 0; for ( int j=0; j < ImageStatistics::NUM_STATISTICS_CHANNELS; j++ ) { unsigned int numPixelValues = 0; m_histogramStats.GetNumPixelValues( static_cast<ImageStatistics::StatisticsChannel>(j), &numPixelValues ); maxNumPixelValues = std::max( numPixelValues, maxNumPixelValues ); } char caption[32]; float pixelValue; if ( maxNumPixelValues == 0 ) { pixelValue = 256 * fraction; } else { pixelValue = maxNumPixelValues * fraction; } sprintf( caption, "%.0f", pixelValue ); const float xOffset = paddedLeft + (paddedWidth * fraction); const float yOffset = paddedTop + paddedHeight + (paddedTop/2); //height - paddedTop; Cairo::TextExtents textExtents; refCairo->get_text_extents( caption, textExtents ); refCairo->move_to( xOffset - (textExtents.width / 2), yOffset + (textExtents.height / 2) ); refCairo->show_text( caption ); } // Render the y-axis labels for ( int i=1; i < k_numPartitions; i++ ) { if ( i % 2 != 0 ) { continue; } const float fraction = i / static_cast<float>(k_numPartitions); float scaleRatio = 100 / static_cast<float>(m_maxVal); float yOffset = paddedTop + paddedHeight - (paddedHeight * fraction * scaleRatio); char caption[32]; sprintf( caption, "%u%%", (i*100)/k_numPartitions ); if ( yOffset > paddedTop ) { Cairo::TextExtents textExtents; refCairo->get_text_extents( caption, textExtents ); refCairo->move_to( (paddedLeft / 2) - (textExtents.width / 2), yOffset + (textExtents.height / 2) ); refCairo->show_text( caption ); } } refCairo->restore(); }
Cairo::RefPtr<Cairo::ImageSurface> TextSurface::create_cairo_surface(const std::string& text, const TextProperties& text_props, Cairo::TextExtents& out_text_extents, Cairo::FontExtents& out_font_extents) { { // get TextExtents and FontExtents Cairo::RefPtr<Cairo::ImageSurface> tmp_surface = Cairo::ImageSurface::create(Cairo::FORMAT_RGB24, 0, 0); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(tmp_surface); cr->set_font_size(text_props.get_font_size()); cr->select_font_face(text_props.get_font(), Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->get_text_extents(text, out_text_extents); cr->get_font_extents(out_font_extents); } Cairo::RefPtr<Cairo::ImageSurface> surface = Cairo::ImageSurface::create(Cairo::FORMAT_ARGB32, static_cast<int>(out_text_extents.width + text_props.get_line_width()), static_cast<int>(out_text_extents.height + text_props.get_line_width())); Cairo::RefPtr<Cairo::Context> cr = Cairo::Context::create(surface); // set the font cr->set_font_size(text_props.get_font_size()); cr->select_font_face(text_props.get_font(), Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); if (text_props.get_line_width() != 0) { // create path cr->move_to(-out_text_extents.x_bearing + text_props.get_line_width()/2.0, -out_text_extents.y_bearing + text_props.get_line_width()/2.0); cr->text_path(text); // paint cr->set_line_width(text_props.get_line_width()); cr->set_line_join(Cairo::LINE_JOIN_ROUND); cr->set_source_rgb(0.0, 0.0, 0.0); cr->stroke(); } // print text cr->move_to(-out_text_extents.x_bearing + text_props.get_line_width()/2.0, -out_text_extents.y_bearing + text_props.get_line_width()/2.0); cr->set_source_rgb(1.0, 1.0, 0.0); double y = -out_text_extents.y_bearing - out_font_extents.ascent; // toying around with color gradients if (false) { Cairo::RefPtr<Cairo::LinearGradient> gradient = Cairo::LinearGradient::create(0, y, 0, y + out_font_extents.ascent + out_font_extents.descent); gradient->add_color_stop_rgb(0.0, 1.0, 1.0, 0.0); gradient->add_color_stop_rgb(0.5, 1.0, 1.0, 1.0); gradient->add_color_stop_rgb(0.5, 0.4, 0.4, 0.2); gradient->add_color_stop_rgb(1.0, 1.0, 1.0, 0.0); cr->set_source(gradient); } cr->show_text(text); return surface; }
void NodeSurface::DrawNodeText( Cairo::RefPtr<Cairo::Context> refCairo, TopologyNode* pNode, int x, int y ) { refCairo->save(); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 24 ); // Set draw color to black refCairo->set_source_rgb(0.0, 0.0, 0.0); char idCaption[128]; TopologyNode::NodeType nodeType = pNode->GetNodeType(); switch (nodeType) { case TopologyNode::COMPUTER: sprintf( idCaption, "PC" ); break; case TopologyNode::BUS: { refCairo->set_font_size( 18 ); InterfaceType interfaceType = pNode->GetInterfaceType(); switch (interfaceType) { case INTERFACE_IEEE1394: sprintf( idCaption, "1394 Bus" ); break; case INTERFACE_USB2: sprintf( idCaption, "USB 2.0 Bus" ); break; case INTERFACE_USB3: sprintf( idCaption, "USB 3.0 Bus" ); break; case INTERFACE_GIGE: sprintf( idCaption, "GigE Bus" ); break; default: sprintf( idCaption, "Bus" ); break; } } break; case TopologyNode::CAMERA: sprintf( idCaption, "ID: %d", pNode->GetDeviceId() ); break; case TopologyNode::NODE: PGRGuid blankGuid; if(pNode->GetGuid() == blankGuid) sprintf( idCaption, "Node" ); else sprintf( idCaption, "Node*" ); break; } Cairo::TextExtents textExtents; refCairo->get_text_extents( idCaption, textExtents ); refCairo->move_to( x - (textExtents.width / 2) , y + (textExtents.height / 2) ); refCairo->show_text( idCaption ); refCairo->restore(); }
void guiRenderer2D::drawMatrices(const Cairo::RefPtr<Cairo::Context>& cr, int width, int height, bool screenshot) { cr->scale(m_scaleFactor, m_scaleFactor); // Scale sensor to fit the active window cr->translate(m_offsetX, m_offsetY); // Center figure on drawable/surface cr->set_line_width(0.25); for(uint m = 0; m < m_frameManager->getNumMatrices(); m++) { matrixInfo &matrix = m_frameManager->getMatrixInfo(m); // TSFrame* tsFrame = m_frameManager->getCurrentFrame(); TSFrame* tsFrame = m_frameManager->getCurrentFilteredFrame(); for(uint y = 0; y < matrix.cells_y; y++) { for(uint x = 0; x < matrix.cells_x; x++) { bool maskedStatic = m_frameManager->getStaticMask(m, x, y); bool maskedDynamic = m_frameManager->getDynamicMask(m, x, y); uint cellID = matrix.texel_offset + y * matrix.cells_x + x; float value = tsFrame->cells[cellID]; if(maskedStatic) { RGB color = determineColor(value); // Draw sensor cell rectangle cr->rectangle(m_rectangleTopLeftX[cellID], m_rectangleTopLeftY[cellID], m_rectangleWidth[cellID], m_rectangleHeight[cellID]); cr->set_source_rgb(0.0, 0.0, 0.0); cr->stroke_preserve(); // Cell outline if(maskedDynamic) { if(value > 0.0) { cr->set_source_rgb(color.r, color.g, color.b); // Active cells } else { cr->set_source_rgb(1.0, 1.0, 1.0); // Inactive cells } } else { cr->set_source_rgb(0.8, 0.8, 0.8); // Disabled cells } cr->fill(); } // Highlight selected cells if(m_frameManager->isSelected(cellID)) { cr->rectangle(m_rectangleTopLeftX[cellID], m_rectangleTopLeftY[cellID], m_rectangleWidth[cellID], m_rectangleHeight[cellID]); cr->set_source_rgba(0.0, 1.0, 0.0, 0.5); // Fill active cells cr->fill(); } if(screenshot) { if(maskedStatic) { // Print values Cairo::RefPtr<Cairo::ToyFontFace> font = Cairo::ToyFontFace::create("LMSans10", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_face(font); cr->set_font_size(matrix.texel_width/3); std::ostringstream ss; ss << value; std::string valueStr = ss.str(); Cairo::TextExtents te; cr->get_text_extents(valueStr, te); cr->move_to(m_matrixCellCenterX[cellID]-te.width/2, m_matrixCellCenterY[cellID]+te.height/2); cr->set_source_rgb(0.0, 0.0, 0.0); cr->show_text(valueStr); } } } } if(!screenshot) { { // Print Matrix IDs Cairo::RefPtr<Cairo::ToyFontFace> font = Cairo::ToyFontFace::create("LMSans10", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_face(font); cr->set_font_size(matrix.cells_x*matrix.texel_width); std::ostringstream ss; ss << m; std::string idString = ss.str(); Cairo::TextExtents te; cr->get_text_extents(idString, te); cr->move_to(m_newCenterX[m]-te.width/2, m_newCenterY[m]+te.height/2); cr->set_source_rgba(0.3, 0.3, 0.3, 0.3); cr->show_text(idString); } } } }
/** Expose event handler. * @param event event info structure. * @return signal return value */ bool LaserDrawingArea::on_expose_event(GdkEventExpose* event) #endif { // This is where we draw on the window Glib::RefPtr<Gdk::Window> window = get_window(); if(window) { Gtk::Allocation allocation = get_allocation(); if(__first_draw) { __first_draw = false; const int width = allocation.get_width(); const int height = allocation.get_height(); // coordinates for the center of the window __xc = width / 2; __yc = height / 2; } #if GTK_VERSION_LT(3,0) Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); #endif cr->set_line_width(1.0); cr->set_source_rgb(1, 1, 1); #if GTK_VERSION_LT(3,0) // 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->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->fill_preserve(); cr->clip(); #else cr->paint(); #endif cr->set_source_rgb(0, 0, 0); //cr->set_source_rgba(0,0,0,1); // __last_xc += __translation_x; // __last_yc += __translation_y; cr->translate(__xc, __yc); cr->save(); if (! __connected) { Cairo::TextExtents te; std::string t = "Not connected to BlackBoard"; cr->set_source_rgb(1, 0, 0); cr->set_font_size(20); cr->get_text_extents(t, te); cr->move_to(- te.width / 2, -te.height / 2); cr->show_text(t); } else if ( __laser_ifs.empty() ) { Cairo::TextExtents te; std::string t = "No interface opened"; cr->set_source_rgb(1, 0, 0); cr->set_font_size(20); cr->get_text_extents(t, te); cr->move_to(- te.width / 2, -te.height / 2); cr->show_text(t); } else if (! all_laser_ifs_have_writer() ) { Cairo::TextExtents te; std::string t = "No writer for "; for (std::list<InterfaceColorPair>::const_iterator it = __laser_ifs.begin(); it != __laser_ifs.end(); ++it) { fawkes::Interface* itf = it->first; if (!itf->has_writer()) { t += itf->uid(); t += ' '; } } cr->set_source_rgb(1, 0, 0); cr->set_font_size(20); cr->get_text_extents(t, te); cr->move_to(- te.width / 2, -te.height / 2); cr->show_text(t); } else { if (! __break_drawing) { for (std::list<InterfaceColorPair>::const_iterator it = __laser_ifs.begin(); it != __laser_ifs.end(); ++it) { fawkes::Interface* laser_if = it->first; laser_if->read(); } } for (std::list<InterfaceColorPair>::const_iterator it = __laser_ifs.begin(); it != __laser_ifs.end(); ++it) { const fawkes::Interface* laser_if = it->first; const Color& color = it->second; cr->save(); cr->set_source_rgb(color.r, color.g, color.b); draw_beams(laser_if, window, cr); cr->restore(); } if (__robot_drawer) __robot_drawer->draw_robot(window, cr); for (std::list<InterfaceColorPair>::const_iterator it = __laser_ifs.begin(); it != __laser_ifs.end(); ++it) { const fawkes::Interface* laser_if = it->first; const Color& color = it->second; cr->save(); cr->set_source_rgb(color.r, color.g, color.b); draw_segments(laser_if, window, cr); cr->restore(); } draw_persons_legs(window, cr); if(__switch_if != NULL && __switch_if->has_writer()){ SwitchInterface::EnableSwitchMessage *esm = new SwitchInterface::EnableSwitchMessage(); __switch_if->msgq_enqueue(esm); } } cr->restore(); cr->save(); cr->rotate(0.5 * M_PI + __rotation); cr->scale(-__zoom_factor, __zoom_factor); cr->set_line_width(1. / __zoom_factor); if (__visdisp_if) { __visdisp->process_messages(); __visdisp->draw(cr); } const float radius = 0.01; if (__line_if) { __line_if->read(); if (__line_if->has_writer() && __line_if->is_valid() && __line_if->is_visible()) { cr->set_source_rgb(1, 0, 0); /* std::vector<double> dashes(1); dashes[0] = 0.1; cr->set_dash(dashes, 0); */ cr->rectangle(__line_if->world_x() - radius * 0.5, __line_if->world_y() - radius * 0.5, radius, radius); cr->rectangle(__line_if->relative_x() - radius * 0.5, __line_if->relative_y() - radius * 0.5, radius, radius); cr->fill_preserve(); cr->stroke(); cr->move_to(__line_if->world_x(), __line_if->world_y()); cr->line_to(__line_if->relative_x(), __line_if->relative_y()); cr->stroke(); } } cr->restore(); } return true; }
/** Draw person legs. * Draws the legs of persons * @param window Gdk window * @param cr Cairo context to draw to. It is assumed that possible transformations * have been setup before. */ void LaserDrawingArea::draw_persons_legs(Glib::RefPtr<Gdk::Window> &window, const Cairo::RefPtr<Cairo::Context> &cr) { std::list<ObjectPositionInterface*>::iterator objpos_if_itt;; cr->save(); if (__l_objpos_if_persons) { cr->set_source_rgb(0,0,1); for( objpos_if_itt = __l_objpos_if_persons->begin(); objpos_if_itt != __l_objpos_if_persons->end() && (*objpos_if_itt)->has_writer(); objpos_if_itt++ ) { if(!__break_drawing) (*objpos_if_itt)->read(); if ((*objpos_if_itt)->is_valid()){ std::pair<float,float> pos = transform_coords_from_fawkes((*objpos_if_itt)->relative_x(), (*objpos_if_itt)->relative_y()); float x=pos.first; float y=pos.second; cr->move_to(x, y); // cr->arc(x, y, std::max((*objpos_if_itt)->extent_x(),(*objpos_if_itt)->extent_y()), 0, 2*M_PI); cr->arc(x, y, 0.2, 0, 2*M_PI); } } cr->stroke(); } if (__l_objpos_if_legs) { cr->set_source_rgb(0,1,0); for( objpos_if_itt = __l_objpos_if_legs->begin(); objpos_if_itt != __l_objpos_if_legs->end() && (*objpos_if_itt)->has_writer() ; objpos_if_itt++ ) { if(!__break_drawing) (*objpos_if_itt)->read(); if ((*objpos_if_itt)->is_valid()){ std::pair<float,float> pos = transform_coords_from_fawkes((*objpos_if_itt)->relative_x(), (*objpos_if_itt)->relative_y()); float x=pos.first; float y=pos.second; cr->move_to(x, y); cr->arc(x, y, 0.1, 0, 2*M_PI); } } cr->stroke(); } if (__l_objpos_if_misc) { cr->set_source_rgb(0,1,1); for( objpos_if_itt = __l_objpos_if_misc->begin(); objpos_if_itt != __l_objpos_if_misc->end() && (*objpos_if_itt)->has_writer() ; objpos_if_itt++ ) { if(!__break_drawing) (*objpos_if_itt)->read(); if ((*objpos_if_itt)->is_valid()){ // switch( (*objpos_if_itt)->object_type() ){ // case ObjectPositionInterface::TYPE_BALL: //TYPE_OPPONENT if((*objpos_if_itt)->object_type()==ObjectPositionInterface::TYPE_BALL){ std::pair<float,float> pos = transform_coords_from_fawkes((*objpos_if_itt)->relative_x(), (*objpos_if_itt)->relative_y()); float x=pos.first; float y=pos.second; pos = transform_coords_from_fawkes((*objpos_if_itt)->world_x(), (*objpos_if_itt)->world_y()); float begin_x=pos.first; float begin_y=pos.second; pos = transform_coords_from_fawkes((*objpos_if_itt)->world_x_velocity(), (*objpos_if_itt)->world_y_velocity()); float end_x= pos.first; float end_y= pos.first; float angle1=atan2(begin_y- y, begin_x - x); float angle2=atan2(end_y- y, end_x - x); float radius=(*objpos_if_itt)->relative_x_velocity(); float probability = (*objpos_if_itt)->relative_z_velocity(); cr->move_to(begin_x, begin_y); cr->arc(x, y, radius, angle2, angle1); // Cairo::TextExtents te; std::string t = StringConversions::to_string(probability); t.erase(5); // cr->set_source_rgb(0,1 ,1); cr->set_font_size(0.08); // cr->get_text_extents(t, te); // cr->move_to(- te.width / 2, -te.height / 2); cr->move_to(begin_x, begin_y); cr->show_text(t); // cr->set_source_rgb(0,0,1); // break; // case ObjectPositionInterface::TYPE_LINE: }else if((*objpos_if_itt)->object_type()==ObjectPositionInterface::TYPE_LINE){ std::pair<float,float> pos = transform_coords_from_fawkes((*objpos_if_itt)->world_x(), (*objpos_if_itt)->world_y()); float begin_x=pos.first; float begin_y=pos.second; pos = transform_coords_from_fawkes((*objpos_if_itt)->world_x_velocity(), (*objpos_if_itt)->world_y_velocity()); float end_x= pos.first; float end_y= pos.first; cr->move_to(begin_x, begin_y); cr->line_to(end_x, end_y); //break; } } } // cr->fill_preserve(); cr->stroke(); } cr->set_source_rgb(1,0,1); float r,g,b; r=g=b=0.0; int color_it=0; float delta = 0.25; if (__l_track_if) { std::list<Position2DTrackInterface*>::iterator track_if_itt;; const float radius (0.1); float* x_positions1; float* y_positions1; int* timestamps1; float* x_positions2 = NULL; float* y_positions2 = NULL; unsigned int track_length1 = 0; unsigned int track_length2 = 0; int* timestamps2 = NULL; unsigned int id; cr->set_font_size(0.03); #ifdef LASERGUI_DEBUG_PRINT_TRACKS printf("\n\n################################\n"); #endif for( track_if_itt = __l_track_if->begin(); track_if_itt != __l_track_if->end() && (*track_if_itt)->has_writer();) { bool b_compound_track(false); if(!__break_drawing) (*track_if_itt)->read(); if ((*track_if_itt)->is_valid()){ x_positions1=(*track_if_itt)->track_x_positions(); y_positions1=(*track_if_itt)->track_y_positions(); timestamps1=(*track_if_itt)->track_timestamps(); track_length1 = (*track_if_itt)->length(); id = (*track_if_itt)->track_id(); ++track_if_itt; if( track_if_itt != __l_track_if->end() && (*track_if_itt)->has_writer()){ if(!__break_drawing) (*track_if_itt)->read(); if( (*track_if_itt)->is_valid() && (*track_if_itt)->track_id()==id ){ b_compound_track = true; x_positions2=(*track_if_itt)->track_x_positions(); y_positions2=(*track_if_itt)->track_y_positions(); timestamps2=(*track_if_itt)->track_timestamps(); track_length2 = (*track_if_itt)->length(); ++track_if_itt; } } #ifdef LASERGUI_DEBUG_PRINT_TRACKS printf("\n trackid %d\n", id); #endif unsigned int i(0); unsigned int j(0); float x = x_positions1[i]; float y = y_positions1[i]; if(b_compound_track){ while(j+1 < track_length2 && timestamps2[j] < timestamps1[i]){ ++j; } if(timestamps2[j] == timestamps1[i]){ x += x_positions2[i]; x /= 2; y += y_positions2[i]; y /=2; } } std::pair<float,float> pos = transform_coords_from_fawkes(x,y); cr->move_to(pos.first,pos.second); for (; i < track_length1; ++i){ x = x_positions1[i]; y = y_positions1[i]; if(b_compound_track){ while(j+1 < track_length2 && timestamps2[j] < timestamps1[i]){ ++j; } if(timestamps2[j] == timestamps1[i]){ x += x_positions2[i]; x /= 2; y += y_positions2[i]; y /=2; } } std::pair<float,float> pos = transform_coords_from_fawkes(x,y); //cr->move_to(pos.first - radius, pos.second); // cr->arc(pos.first, pos.second, radius, 0, 2*M_PI); cr->line_to(pos.first, pos.second); // cr->rectangle(x_positions[i], y_positions[i], 4 / __zoom_factor, 4 / __zoom_factor); // std::string t = StringConversions::toString(id) + "-" + StringConversions::toString(timestamps[i]); std::string t = StringConversions::to_string(timestamps1[i]); // cr->move_to(begin_x, begin_y); cr->show_text(t); cr->move_to(pos.first, pos.second); #ifdef LASERGUI_DEBUG_PRINT_TRACKS printf("( %f,%f,[%d] )", pos.first, pos.second, timestamps1[i] ); #endif } // chose color if (div(color_it,3).rem == 0) r+= delta; if (div(color_it,3).rem == 1) g+= delta; if (div(color_it,3).rem == 2) b+= delta; cr->set_source_rgb(r,g,b); color_it++; cr->stroke(); i = std::max(0, (int) track_length1 - CFG_PRINT_NR_TRACKELEMENTS); j = 0; for (; i < track_length1; ++i){ x = x_positions1[i]; y = y_positions1[i]; if(b_compound_track){ while(j+1 < track_length2 && timestamps2[j] < timestamps1[i]){ ++j; } } std::pair<float,float> pos = transform_coords_from_fawkes(x_positions1[i],y_positions1[i]); cr->move_to(pos.first - radius, pos.second); cr->arc(pos.first, pos.second, radius, 0, 2*M_PI); if(b_compound_track && timestamps2[j] == timestamps1[i]){ cr->move_to(pos.first, pos.second); std::pair<float,float> pos = transform_coords_from_fawkes(x_positions2[j],y_positions2[j]); cr->line_to(pos.first, pos.second); cr->move_to(pos.first - radius, pos.second); cr->arc(pos.first, pos.second, radius, 0, 2*M_PI); } } cr->set_source_rgb(0,0,1); cr->stroke(); } else{ break; } } } /* DRAW TARGET */ if(__target_if && __target_if->has_writer()){ __target_if->read(); if(__target_if->is_valid()){ cr->set_source_rgb(1,0,0); std::pair<float,float> pos = transform_coords_from_fawkes(__target_if->relative_x(), __target_if->relative_y()); float x=pos.first; float y=pos.second; float radius = 0.1; cr->move_to(x, y); cr->arc(x, y, radius, 0, 2*M_PI); cr->move_to(x - radius, y); cr->line_to(x + radius, y); cr->move_to(x, y - radius ); cr->line_to(x, y + radius); cr->stroke(); } } /* float r,g,b; r=g=b=0.0; float delta = 0.2; for (int i = 0; i< 15 ; i++){ if (div(i,3).rem == 0) r+= delta; if (div(i,3).rem == 1) g+= delta; if (div(i,3).rem == 2) b+= delta; // printf("i %d rem %d| r %f, g %f, b %f\n", i, div(i,3).rem,r,g,b); cr->move_to(0, (i+1)*0.125); cr->set_source_rgb(r,g,b); cr->rectangle(0, (i+1)*0.125, 0.1 , 0.1 ); cr->fill_preserve(); cr->stroke(); } */ // cr->stroke(); cr->restore(); }
bool CalibrationArea::on_expose_event(GdkEventExpose *event) { // check that screensize did not change if (display_width != get_width() || display_height != get_height()) { set_display_size(get_width(), get_height()); } Glib::RefPtr<Gdk::Window> window = get_window(); if (window) { Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); cr->save(); cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->clip(); // Print the text cr->set_font_size(font_size); double text_height = -1; double text_width = -1; Cairo::TextExtents extent; for (int i = 0; i != help_lines; i++) { cr->get_text_extents(help_text[i], extent); text_width = std::max(text_width, extent.width); text_height = std::max(text_height, extent.height); } text_height += 2; double x = (display_width - text_width) / 2; double y = (display_height - text_height) / 2 - 60; cr->set_line_width(2); cr->rectangle(x - 10, y - (help_lines*text_height) - 10, text_width + 20, (help_lines*text_height) + 20); // Print help lines y -= 3; for (int i = help_lines-1; i != -1; i--) { cr->get_text_extents(help_text[i], extent); cr->move_to(x + (text_width-extent.width)/2, y); cr->show_text(help_text[i]); y -= text_height; } cr->stroke(); // Draw the points for (int i = 0; i <= calibrator->get_numclicks(); i++) { // set color: already clicked or not if (i < calibrator->get_numclicks()) cr->set_source_rgb(1.0, 1.0, 1.0); else cr->set_source_rgb(0.8, 0.0, 0.0); cr->set_line_width(1); cr->move_to(X[i] - cross_lines, Y[i]); cr->rel_line_to(cross_lines*2, 0); cr->move_to(X[i], Y[i] - cross_lines); cr->rel_line_to(0, cross_lines*2); cr->stroke(); cr->arc(X[i], Y[i], cross_circle, 0.0, 2.0 * M_PI); cr->stroke(); } // Draw the clock background cr->arc(display_width/2, display_height/2, clock_radius/2, 0.0, 2.0 * M_PI); cr->set_source_rgb(0.5, 0.5, 0.5); cr->fill_preserve(); cr->stroke(); cr->set_line_width(clock_line_width); cr->arc(display_width/2, display_height/2, (clock_radius - clock_line_width)/2, 3/2.0*M_PI, (3/2.0*M_PI) + ((double)time_elapsed/(double)max_time) * 2*M_PI); cr->set_source_rgb(0.0, 0.0, 0.0); cr->stroke(); // Draw the message (if any) if (message != NULL) { // Frame the message cr->set_font_size(font_size); Cairo::TextExtents extent; cr->get_text_extents(this->message, extent); text_width = extent.width; text_height = extent.height; x = (display_width - text_width) / 2; y = (display_height - text_height + clock_radius) / 2 + 60; cr->set_line_width(2); cr->rectangle(x - 10, y - text_height - 10, text_width + 20, text_height + 25); // Print the message cr->move_to(x, y); cr->show_text(this->message); cr->stroke(); } cr->restore(); } return true; }
bool knob::on_expose_event(GdkEventExpose* event) { // This is where we draw on the window Glib::RefPtr<Gdk::Window> window = get_window(); if(window) { Gtk::Allocation allocation = get_allocation(); const int width = allocation.get_width(); const int height = allocation.get_height() - (allocation.get_height()/3); const int height_offset = allocation.get_height()/6; // coordinates for the center of the window int xc, yc; xc = width / 2; yc = height / 2; Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context(); cr->set_line_width(6.0); // 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->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); cr->clip(); // background grad Cairo::RefPtr<Cairo::LinearGradient> back_grad = Cairo::LinearGradient::create( 0,0,0,allocation.get_height() ); switch (pos_mode) { case 0: back_grad->add_color_stop_rgba(0,top_colour.get_red_p(),top_colour.get_green_p(),top_colour.get_blue_p(),1); back_grad->add_color_stop_rgba(1,bottom_colour.get_red_p(),bottom_colour.get_green_p(),bottom_colour.get_blue_p(),1); break; case 1: back_grad->add_color_stop_rgba(0,top_colour.get_red_p(),top_colour.get_green_p(),top_colour.get_blue_p(),1); back_grad->add_color_stop_rgba(1, (bottom_colour.get_red_p() + top_colour.get_red_p())/2, (bottom_colour.get_green_p() + top_colour.get_green_p())/2, (bottom_colour.get_blue_p() + top_colour.get_blue_p())/2, 1); break; case 2: back_grad->add_color_stop_rgba(0, (bottom_colour.get_red_p() + top_colour.get_red_p())/2, (bottom_colour.get_green_p() + top_colour.get_green_p())/2, (bottom_colour.get_blue_p() + top_colour.get_blue_p())/2, 1); back_grad->add_color_stop_rgba(1,bottom_colour.get_red_p(),bottom_colour.get_green_p(),bottom_colour.get_blue_p(),1); break; } // fill background if (rounded<5) { rounded_rectangle(cr,rounded, grad_top_colour, grad_bottom_colour, event->area.x, event->area.y, event->area.width, event->area.height,pos_mode, top_colour.to_string(),bottom_colour.to_string() ); } else { cr->rectangle(event->area.x, event->area.y, event->area.width, event->area.height); } cr->set_source(back_grad); cr->fill(); // ------------------------------------------------------------------ float cos_x = (allocation.get_width()/5) * (cos(((((1-knob_value)*0.75)-0.3)*2) * M_PI)); float sin_y = (allocation.get_width()/5) * (sin(((((1-knob_value)*0.75)-0.3)*2) * M_PI)); Cairo::RefPtr<Cairo::RadialGradient> grad1 = Cairo::RadialGradient::create( (allocation.get_width()/2) + sin_y, (allocation.get_height()/2) + cos_x, 0, (allocation.get_width()/2) , (allocation.get_height()/2) ,(allocation.get_width()/2.5)); grad1->add_color_stop_rgba(0,0.4,0.4,0.4,1); grad1->add_color_stop_rgba(0.5,0.2,0.2,0.2,1); grad1->add_color_stop_rgba(0.8,0.17,0.17,0.17,1); grad1->add_color_stop_rgba(1.0,0.0,0.0,0.0,1); cos_x = (allocation.get_width()/5) * (cos((((knob_value*0.75)-0.61)*2) * M_PI)); sin_y = (allocation.get_width()/5) * (sin((((knob_value*0.75)-0.61)*2) * M_PI)); cr->set_source(grad1); cr->arc(allocation.get_width()/2, allocation.get_height()/2, (allocation.get_width()/2.5), 0.0, 2 * M_PI); cr->fill(); cr->set_source_rgb(0.0, 0.0, 0.0); cr->arc((allocation.get_width()/2) +cos_x, (allocation.get_height()/2)+sin_y, (allocation.get_width()/16), 0.0, 2 * M_PI); cr->fill(); // draw text label cr->select_font_face("Bitstream Vera Sans", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_NORMAL); cr->set_font_size(width/4.5); cr->set_source_rgba(0.9,0.9,0.9,0.8); Cairo::FontOptions font_options; font_options.set_hint_style(Cairo::HINT_STYLE_NONE); font_options.set_hint_metrics(Cairo::HINT_METRICS_OFF); font_options.set_antialias(Cairo::ANTIALIAS_GRAY); int x_font_centre = (width/2) - ((width/5) * (label.length()/3.5)); cr->set_font_options(font_options); cr->move_to(x_font_centre,height/3.5); cr->show_text(label); cr->move_to(x_font_centre,allocation.get_height() - (height_offset/3) ); ostringstream slider_value; slider_value.str(""); if (invert) { slider_value << max - value; } else { slider_value << value; } slider_value.str(slider_value.str().substr(0,5)); x_font_centre = (width/2) - ((width/5) * (slider_value.str().length()/3.5)); cr->move_to(x_font_centre,allocation.get_height() - (height_offset/1.5) ); cr->show_text(slider_value.str()); } return true; }
void HistogramDrawingArea::DrawRowColGridLabels( Cairo::RefPtr<Cairo::Context> refCairo, unsigned int numPixelValues, unsigned int numValues ) { // Reserve the outside 10% float paddedLeft, paddedTop, paddedWidth, paddedHeight; GetPaddedSize( paddedLeft, paddedTop, paddedWidth, paddedHeight ); refCairo->save(); // Set draw color refCairo->set_source_rgb(0, 0, 0); // Set the font parameters refCairo->select_font_face( "monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD ); refCairo->set_font_size( 10 ); const int k_numPartitions = 8; for ( int i=0; i <= k_numPartitions; i++ ) { const float fraction = i / static_cast<float>(k_numPartitions); char caption[32]; float dimensionValue = numValues * fraction; sprintf( caption, "%.0f", dimensionValue ); const float xOffset = paddedLeft + (paddedWidth * fraction); const float yOffset = paddedTop + paddedHeight + (paddedTop/2); Cairo::TextExtents textExtents; refCairo->get_text_extents( caption, textExtents ); refCairo->move_to( xOffset - (textExtents.width / 2), yOffset + (textExtents.height / 2) ); refCairo->show_text( caption ); } for ( int i=1; i <= k_numPartitions; i++ ) { const float fraction = i / static_cast<float>(k_numPartitions); char caption[32]; float pixelValue = numPixelValues * fraction; sprintf( caption, "%.0f", pixelValue ); float yOffset = paddedTop + paddedHeight - (paddedHeight * fraction); Cairo::TextExtents textExtents; refCairo->get_text_extents( caption, textExtents ); refCairo->move_to( (paddedLeft/2) - (textExtents.width / 2), yOffset + (textExtents.height / 2) ); refCairo->show_text( caption ); } refCairo->restore(); }
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; } }
bool PhotoPreview::on_draw(const Cairo::RefPtr<Cairo::Context>& cr) { if (!Image) return false; int Width,Height; Gtk::Allocation Allocation = get_allocation(); Width=Allocation.get_width(); Height=Allocation.get_height(); if (Width<=1 || Height<=1) return false; /* Scale the photo */ Glib::RefPtr<Gdk::Pixbuf> Scaled; /* Preserve aspect ratio */ double ImageAspectRatio,PreviewAspectRatio; PreviewAspectRatio=(float)Width/Height; ImageAspectRatio=(float)Image->get_width()/Image->get_height(); if (ImageAspectRatio<PreviewAspectRatio) { Width=Height*ImageAspectRatio; } else { Height=Width/ImageAspectRatio; } Scaled=Image->scale_simple( Width, Height, Gdk::INTERP_BILINEAR); if (Cfg->GetFlipPreview()) Scaled=Scaled->flip(); Gdk::Cairo::set_source_pixbuf(cr, Scaled, (Allocation.get_width()/2)-(Scaled->get_width()/2), (Allocation.get_height()/2)-(Scaled->get_height()/2) ); cr->paint(); double TimeLeft=Util::TimeDiff(&PhotoTime,NULL); double MinDim=get_width(); if (get_height()<MinDim) MinDim=get_height(); if (Overlay.length()==0 && TimeLeft>0) { double TotalTime=Cfg->GetCountdownCount()*(Cfg->GetCountdownTimeout()/1000.0); unsigned int Whole=floor(Cfg->GetCountdownCount()*TimeLeft/TotalTime); float Frac=(Cfg->GetCountdownCount()*TimeLeft/TotalTime)-Whole; Cairo::RefPtr<Cairo::Context> Context = get_window()->create_cairo_context(); Context->set_source_rgba(1.0, 1.0, 1.0,0.5); Context->translate(get_width()/2.0,get_height()/2.0); /* Draw the fractional portion of the countdown */ Context->save(); Context->rotate(M_PI/2.0); Context->arc(0.0,0.0,MinDim/4.0,0,Frac*2*M_PI); Context->fill(); Context->restore(); /* Draw the Whole portion of the countdown */ Context->save(); Context->set_font_size(MinDim/2.0); char StrBuf[1024]; snprintf(StrBuf,sizeof(StrBuf),"%d",Whole+1); Cairo::TextExtents Extents; Context->get_text_extents(StrBuf,Extents); Context->translate(-Extents.width/2.0,Extents.height/2.0); Context->show_text(StrBuf); Context->stroke(); Context->restore(); } if (Overlay.length()) { Cairo::RefPtr<Cairo::Context> Context = get_window()->create_cairo_context(); Context->set_source_rgba(1.0, 1.0, 1.0,0.5); Context->translate(get_width()/2.0,get_height()/2.0); Context->save(); Context->set_font_size(MinDim/2.0); Cairo::TextExtents Extents; Context->get_text_extents(Overlay,Extents); Context->translate(-Extents.width/2.0,Extents.height/2.0); Context->show_text(Overlay); Context->stroke(); Context->restore(); } return true; }