comparison 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
comparison
equal deleted inserted replaced
364:aad2f9293089 365:ef31240a73f6
57 cairo_set_source_rgb(context_, 57 cairo_set_source_rgb(context_,
58 static_cast<float>(red) / 255.0f, 58 static_cast<float>(red) / 255.0f,
59 static_cast<float>(green) / 255.0f, 59 static_cast<float>(green) / 255.0f,
60 static_cast<float>(blue) / 255.0f); 60 static_cast<float>(blue) / 255.0f);
61 } 61 }
62
63
64 class CairoContext::AlphaSurface : public boost::noncopyable
65 {
66 private:
67 cairo_surface_t *surface_;
68
69 public:
70 AlphaSurface(unsigned int width,
71 unsigned int height)
72 {
73 surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height);
74
75 if (!surface_)
76 {
77 // Should never occur
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
79 }
80
81 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS)
82 {
83 LOG(ERROR) << "Cannot create a Cairo surface";
84 cairo_surface_destroy(surface_);
85 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
86 }
87 }
88
89 ~AlphaSurface()
90 {
91 cairo_surface_destroy(surface_);
92 }
93
94 void GetAccessor(Orthanc::ImageAccessor& target)
95 {
96 target.AssignWritable(Orthanc::PixelFormat_Grayscale8,
97 cairo_image_surface_get_width(surface_),
98 cairo_image_surface_get_height(surface_),
99 cairo_image_surface_get_stride(surface_),
100 cairo_image_surface_get_data(surface_));
101 }
102
103 void Fill(cairo_t* cr,
104 double x,
105 double y)
106 {
107 cairo_surface_mark_dirty(surface_);
108 cairo_mask_surface(cr, surface_, x, y);
109 cairo_fill(cr);
110 }
111 };
112
113
114 void CairoContext::DrawText(const Orthanc::Font& font,
115 const std::string& text,
116 double x,
117 double y)
118 {
119 unsigned int width, height;
120 font.ComputeTextExtent(width, height, text);
121
122 AlphaSurface surface(width, height);
123
124 Orthanc::ImageAccessor accessor;
125 surface.GetAccessor(accessor);
126 font.Draw(accessor, text, 0, 0, 255);
127
128 double pixelX = x;
129 double pixelY = y;
130 cairo_user_to_device(context_, &pixelX, &pixelY);
131
132 cairo_save(context_);
133 cairo_identity_matrix(context_);
134
135 surface.Fill(context_, pixelX, pixelY);
136
137 cairo_restore(context_);
138 }
62 } 139 }