Mercurial > hg > orthanc-stone
annotate Framework/Wrappers/CairoContext.cpp @ 1310:9bea7e15b519 broker
- first pass at changes to cope with the refactoring of the loading system
- global loader-related data accessible through ILoadersContext::ILock
- many changes in legacy loaders (CT, RTSTRUCT, DOSE) + loader cache
- NOT FINISHED! there are shared_from_this calls in ctors! this will crash!
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 09 Mar 2020 14:53:22 +0100 |
parents | 2d8ab34c8c91 |
children | 30deba7bc8e2 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
807
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "CairoContext.h" | |
23 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
25 #include <Core/OrthancException.h> |
0 | 26 |
339 | 27 |
0 | 28 namespace OrthancStone |
29 { | |
339 | 30 CairoContext::CairoContext(CairoSurface& surface) : |
31 width_(surface.GetWidth()), | |
32 height_(surface.GetHeight()) | |
0 | 33 { |
34 context_ = cairo_create(surface.GetObject()); | |
35 if (!context_) | |
36 { | |
37 LOG(ERROR) << "Cannot create Cairo drawing context"; | |
38 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
39 } | |
40 } | |
41 | |
42 | |
43 CairoContext::~CairoContext() | |
44 { | |
45 if (context_ != NULL) | |
46 { | |
47 cairo_destroy(context_); | |
48 context_ = NULL; | |
49 } | |
50 } | |
51 | |
52 | |
53 void CairoContext::SetSourceColor(uint8_t red, | |
54 uint8_t green, | |
55 uint8_t blue) | |
56 { | |
57 cairo_set_source_rgb(context_, | |
58 static_cast<float>(red) / 255.0f, | |
59 static_cast<float>(green) / 255.0f, | |
60 static_cast<float>(blue) / 255.0f); | |
61 } | |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
62 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
63 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
64 class CairoContext::AlphaSurface : public boost::noncopyable |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
65 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
66 private: |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
67 cairo_surface_t *surface_; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
68 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
69 public: |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
70 AlphaSurface(unsigned int width, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
71 unsigned int height) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
72 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
73 surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
74 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
75 if (!surface_) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
76 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
77 // Should never occur |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
79 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
80 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
81 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
82 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
83 LOG(ERROR) << "Cannot create a Cairo surface"; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
84 cairo_surface_destroy(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
85 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
86 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
87 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
88 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
89 ~AlphaSurface() |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
90 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
91 cairo_surface_destroy(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
92 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
93 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
94 void GetAccessor(Orthanc::ImageAccessor& target) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
95 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
96 target.AssignWritable(Orthanc::PixelFormat_Grayscale8, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
97 cairo_image_surface_get_width(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
98 cairo_image_surface_get_height(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
99 cairo_image_surface_get_stride(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
100 cairo_image_surface_get_data(surface_)); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
101 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
102 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
103 void Blit(cairo_t* cr, |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
104 double x, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
105 double y) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
106 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
107 cairo_surface_mark_dirty(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
108 cairo_mask_surface(cr, surface_, x, y); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
109 cairo_fill(cr); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
110 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
111 }; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
112 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
113 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
114 void CairoContext::DrawText(const Orthanc::Font& font, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
115 const std::string& text, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
116 double x, |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
117 double y, |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
118 BitmapAnchor anchor) |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
119 { |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
120 // Render a bitmap containing the text |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
121 unsigned int width, height; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
122 font.ComputeTextExtent(width, height, text); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
123 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
124 AlphaSurface surface(width, height); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
125 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
126 Orthanc::ImageAccessor accessor; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
127 surface.GetAccessor(accessor); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
128 font.Draw(accessor, text, 0, 0, 255); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
129 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
130 // Correct the text location given the anchor location |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
131 double deltaX, deltaY; |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
132 ComputeAnchorTranslation(deltaX, deltaY, anchor, width, height); |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
133 |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
134 // Cancel zoom/rotation before blitting the text onto the surface |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
135 double pixelX = x; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
136 double pixelY = y; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
137 cairo_user_to_device(context_, &pixelX, &pixelY); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
138 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
139 cairo_save(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
140 cairo_identity_matrix(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
141 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
142 // Blit the text bitmap |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
143 surface.Blit(context_, pixelX + deltaX, pixelY + deltaY); |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
144 cairo_restore(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
145 } |
0 | 146 } |