Mercurial > hg > orthanc-stone
diff Framework/Viewport/CairoContext.cpp @ 365:ef31240a73f6 am-2
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 31 Oct 2018 18:10:29 +0100 |
parents | 5a7915b23138 |
children | a7de01c8fd29 |
line wrap: on
line diff
--- a/Framework/Viewport/CairoContext.cpp Wed Oct 31 11:06:25 2018 +0100 +++ b/Framework/Viewport/CairoContext.cpp Wed Oct 31 18:10:29 2018 +0100 @@ -59,4 +59,81 @@ static_cast<float>(green) / 255.0f, static_cast<float>(blue) / 255.0f); } + + + class CairoContext::AlphaSurface : public boost::noncopyable + { + private: + cairo_surface_t *surface_; + + public: + AlphaSurface(unsigned int width, + unsigned int height) + { + surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height); + + if (!surface_) + { + // Should never occur + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + + if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) + { + LOG(ERROR) << "Cannot create a Cairo surface"; + cairo_surface_destroy(surface_); + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); + } + } + + ~AlphaSurface() + { + cairo_surface_destroy(surface_); + } + + void GetAccessor(Orthanc::ImageAccessor& target) + { + target.AssignWritable(Orthanc::PixelFormat_Grayscale8, + cairo_image_surface_get_width(surface_), + cairo_image_surface_get_height(surface_), + cairo_image_surface_get_stride(surface_), + cairo_image_surface_get_data(surface_)); + } + + void Fill(cairo_t* cr, + double x, + double y) + { + cairo_surface_mark_dirty(surface_); + cairo_mask_surface(cr, surface_, x, y); + cairo_fill(cr); + } + }; + + + void CairoContext::DrawText(const Orthanc::Font& font, + const std::string& text, + double x, + double y) + { + unsigned int width, height; + font.ComputeTextExtent(width, height, text); + + AlphaSurface surface(width, height); + + Orthanc::ImageAccessor accessor; + surface.GetAccessor(accessor); + font.Draw(accessor, text, 0, 0, 255); + + double pixelX = x; + double pixelY = y; + cairo_user_to_device(context_, &pixelX, &pixelY); + + cairo_save(context_); + cairo_identity_matrix(context_); + + surface.Fill(context_, pixelX, pixelY); + + cairo_restore(context_); + } }