Skip to content

Commit 00ef270

Browse files
committed
Update CairoHelper
* #25 * NatronGitHub/Natron#970
1 parent 61543e4 commit 00ef270

File tree

2 files changed

+88
-16
lines changed

2 files changed

+88
-16
lines changed

Helpers/CairoHelper.cpp

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,70 @@
2020

2121
#include <cmath>
2222

23-
void CairoHelper::applyRotate(cairo_t *cr,
24-
double rotate,
25-
XY origin)
23+
void
24+
CairoHelper::applyFlip(cairo_t *cr,
25+
const int &height)
2626
{
27-
if (!cr || rotate == 0.) { return; }
27+
if (!cr || height < 1) { return; }
28+
cairo_scale(cr, 1.0f, -1.0f);
29+
cairo_translate(cr, 0.0f, -height);
30+
}
31+
32+
void
33+
CairoHelper::applyPosition(cairo_t *cr,
34+
const _XY &position)
35+
{
36+
if (!cr) { return; }
37+
cairo_move_to(cr, position.x, position.y);
38+
}
39+
40+
void
41+
CairoHelper::applyScale(cairo_t *cr,
42+
const _XY &scale)
43+
{
44+
if (!cr || (scale.x == 0. && scale.y == 0.)) { return; }
45+
cairo_scale(cr, scale.x, scale.y);
46+
}
2847

48+
void
49+
CairoHelper::applySkew(cairo_t *cr,
50+
const _XY &skew,
51+
const _XY &origin)
52+
{
53+
if (!cr || (skew.x == 0. && skew.y == 0.)) { return; }
54+
double x = skew.x;
55+
double y = skew.y;
56+
if (x != 0.) { x = -x; }
57+
if (y != 0.) { y = -y; }
58+
cairo_matrix_t matrix = {
59+
1.0, y,
60+
x , 1.0,
61+
0.0, 0.0
62+
};
2963
cairo_translate(cr, origin.x, origin.y);
30-
cairo_rotate(cr, -rotate * (M_PI / 180.0));
64+
cairo_transform(cr, &matrix);
3165
cairo_translate(cr, -origin.x, -origin.y);
3266
}
3367

34-
void CairoHelper::applyFlip(cairo_t *cr,
35-
int height)
68+
void
69+
CairoHelper::applyRotate(cairo_t *cr,
70+
const double &rotate,
71+
const _XY &origin)
3672
{
37-
if (!cr || height < 1) { return; }
73+
if (!cr || rotate == 0.) { return; }
74+
cairo_translate(cr, origin.x, origin.y);
75+
cairo_rotate(cr, -rotate * (M_PI / 180.0));
76+
cairo_translate(cr, -origin.x, -origin.y);
77+
}
3878

39-
cairo_scale(cr, 1.0f, -1.0f);
40-
cairo_translate(cr, 0.0f, -height);
79+
void
80+
CairoHelper::applyTransform(cairo_t *cr,
81+
const _Transform &transform)
82+
{
83+
if (!cr) { return; }
84+
if (transform.flip) { applyFlip(cr, transform.height); }
85+
if (transform.position) { applyPosition(cr, transform.origin); }
86+
applyScale(cr, transform.scale);
87+
applySkew(cr, transform.skew, transform.origin);
88+
applyRotate(cr, transform.rotate, transform.origin);
4189
}

Helpers/CairoHelper.h

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,42 @@
2424
class CairoHelper
2525
{
2626
public:
27-
struct XY
27+
struct _XY
2828
{
2929
double x;
3030
double y;
3131
};
32-
/** @brief apply rotate */
33-
static void applyRotate(cairo_t *cr,
34-
double rotate,
35-
XY origin);
32+
struct _Transform
33+
{
34+
_XY origin;
35+
_XY scale;
36+
_XY skew;
37+
int width;
38+
int height;
39+
double rotate;
40+
bool position;
41+
bool flip;
42+
};
3643
/** @brief apply flip */
3744
static void applyFlip(cairo_t *cr,
38-
int height);
45+
const int &height);
46+
/** @brief apply position */
47+
static void applyPosition(cairo_t *cr,
48+
const _XY &position);
49+
/** @brief apply scale */
50+
static void applyScale(cairo_t *cr,
51+
const _XY &scale);
52+
/** @brief apply skew */
53+
static void applySkew(cairo_t *cr,
54+
const _XY &skew,
55+
const _XY &origin);
56+
/** @brief apply rotate */
57+
static void applyRotate(cairo_t *cr,
58+
const double &rotate,
59+
const _XY &origin);
60+
/** @brief apply transform */
61+
static void applyTransform(cairo_t *cr,
62+
const _Transform &transform);
3963
};
4064

4165
#endif // CAIROHELPER_H

0 commit comments

Comments
 (0)