Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 387f8ab

Browse files
committed
[[ Graphics ]] Transform
This patch adds a 'Transform' property to MCGContext. The transform property changes the transform which has been applied since the last save - the effective transform being the concatenation of the transform at the last save with the transform property.
1 parent 6b9163b commit 387f8ab

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

libgraphics/include/graphics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,9 @@ void MCGContextRestore(MCGContextRef context);
905905
void MCGContextSetFlatness(MCGContextRef context, MCGFloat flatness);
906906
void MCGContextSetShouldAntialias(MCGContextRef context, bool should_antialias);
907907

908+
// Transform attribute
909+
void MCGContextSetTransform(MCGContextRef context, MCGAffineTransform p_transform);
910+
908911
// Layer attributes and manipulation - bitmap effect options would be added here also.
909912
void MCGContextSetOpacity(MCGContextRef context, MCGFloat opacity);
910913
void MCGContextSetBlendMode(MCGContextRef context, MCGBlendMode mode);

libgraphics/src/context.cpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ static bool MCGContextStateCreate(MCGContextStateRef& r_state)
108108

109109
if (t_success)
110110
{
111+
t_state -> base_transform = MCGAffineTransformMakeIdentity();
112+
t_state -> transform = MCGAffineTransformMakeIdentity();
113+
111114
t_state -> opacity = 1.0f;
112115
t_state -> blend_mode = kMCGBlendModeSourceOver;
113116
t_state -> flatness = 0.0f;
@@ -127,7 +130,6 @@ static bool MCGContextStateCreate(MCGContextStateRef& r_state)
127130
t_state -> stroke_attr . dashes = NULL;
128131
t_state -> stroke_style = kMCGPaintStyleOpaque;
129132

130-
131133
t_state -> is_layer_begin_pt = false;
132134
t_state -> parent = NULL;
133135
}
@@ -140,7 +142,6 @@ static bool MCGContextStateCreate(MCGContextStateRef& r_state)
140142
return t_success;
141143
}
142144

143-
144145
static bool MCGContextStateCopy(MCGContextStateRef p_state, MCGContextStateRef& r_new_state)
145146
{
146147
bool t_success;
@@ -153,6 +154,9 @@ static bool MCGContextStateCopy(MCGContextStateRef p_state, MCGContextStateRef&
153154

154155
if (t_success)
155156
{
157+
t_state->base_transform = MCGAffineTransformMakeIdentity();
158+
t_state->transform = MCGAffineTransformMakeIdentity();
159+
156160
t_state -> opacity = p_state -> opacity;
157161
t_state -> blend_mode = p_state -> blend_mode;
158162
t_state -> flatness = p_state -> flatness;
@@ -300,6 +304,7 @@ static bool MCGContextPushState(MCGContextRef self)
300304
if (t_success)
301305
{
302306
t_state -> parent = self -> state;
307+
t_state->base_transform = MCGContextGetDeviceTransform(self);
303308
self -> state = t_state;
304309
}
305310

@@ -366,8 +371,6 @@ static bool MCGContextCreateUnbound(MCGContextRef& r_context)
366371

367372
if (t_success)
368373
{
369-
//t_context -> width = p_bitmap . width();
370-
//t_context -> height = p_bitmap . height();
371374
t_context->references = 1;
372375
t_context->path = NULL;
373376
t_context->is_valid = true;
@@ -575,7 +578,7 @@ void MCGContextSave(MCGContextRef self)
575578
if (t_success)
576579
{
577580
// we use skia to manage the clip and matrix between states, everything else is held in the state directly
578-
self -> layer -> canvas -> save();
581+
self -> layer -> canvas -> save();
579582
t_success = MCGContextPushState(self);
580583
}
581584

@@ -1722,22 +1725,30 @@ void MCGContextSetStrokePaintStyle(MCGContextRef self, MCGPaintStyle p_paint_sty
17221725

17231726
inline static bool MCGContextSetCTM(MCGContextRef self, MCGAffineTransform p_transform)
17241727
{
1728+
self->state->transform = p_transform;
1729+
17251730
SkMatrix t_matrix;
1726-
MCGAffineTransformToSkMatrix(p_transform, t_matrix);
1731+
MCGAffineTransformToSkMatrix(MCGAffineTransformConcat(self->state->base_transform, p_transform), t_matrix);
17271732
self -> layer -> canvas -> setMatrix(t_matrix);
1728-
1733+
17291734
// no need to transform the clip at this point as this is handled internally by skia
17301735
return true;
17311736
}
17321737

1738+
void MCGContextSetTransform(MCGContextRef self, MCGAffineTransform p_transform)
1739+
{
1740+
if (!MCGContextIsValid(self))
1741+
return;
1742+
1743+
MCGContextSetCTM(self, p_transform);
1744+
}
1745+
17331746
void MCGContextConcatCTM(MCGContextRef self, MCGAffineTransform p_transform)
17341747
{
17351748
if (!MCGContextIsValid(self))
17361749
return;
17371750

1738-
SkMatrix t_matrix;
1739-
MCGAffineTransformToSkMatrix(p_transform, t_matrix);
1740-
self -> layer -> canvas -> concat(t_matrix);
1751+
MCGContextSetCTM(self, MCGAffineTransformConcat(self->state->transform, p_transform));
17411752
}
17421753

17431754
void MCGContextRotateCTM(MCGContextRef self, MCGFloat p_angle)

libgraphics/src/graphics-internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ typedef struct __MCGContextState *MCGContextStateRef;
262262

263263
struct __MCGContextState
264264
{
265+
MCGAffineTransform base_transform;
266+
MCGAffineTransform transform;
267+
265268
MCGFloat opacity;
266269
MCGBlendMode blend_mode;
267270
MCGFloat flatness;

0 commit comments

Comments
 (0)