5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2011 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
public sealed class Context: XwtObject, IDisposable
34
static IContextBackendHandler handler;
37
double globalAlpha = 1;
41
handler = WidgetRegistry.CreateSharedBackend<IContextBackendHandler> (typeof(Context));
44
protected override IBackendHandler BackendHandler {
50
internal Context (object backend): base (backend)
55
/// Makes a copy of the current state of the Context and saves it on an internal stack of saved states.
56
/// When Restore() is called, it will be restored to the saved state.
57
/// Multiple calls to Save() and Restore() can be nested;
58
/// each call to Restore() restores the state from the matching paired save().
62
handler.Save (Backend);
65
public void Restore ()
67
handler.Restore (Backend);
70
public double GlobalAlpha {
71
get { return globalAlpha; }
74
handler.SetGlobalAlpha (Backend, globalAlpha);
78
public void SetColor (Color color)
80
handler.SetColor (Backend, color);
84
/// Adds a circular arc of the given radius to the current path.
85
/// The arc is centered at (xc, yc),
86
/// begins at angle1 and proceeds in the direction
87
/// of increasing angles to end at angle2.
88
/// If angle2 is less than angle1,
89
/// it will be progressively increased by 1 degree until it is greater than angle1.
90
/// If there is a current point, an initial line segment will be added to the path
91
/// to connect the current point to the beginning of the arc.
92
/// If this initial line is undesired,
93
/// it can be avoided by calling NewPath() before calling Arc().
101
/// <param name='radius'>
104
/// <param name='angle1'>
105
/// Angle1 in degrees
107
/// <param name='angle2'>
108
/// Angle2 in degrees
110
public void Arc (double xc, double yc, double radius, double angle1, double angle2)
112
handler.Arc (Backend, xc, yc, radius, angle1, angle2);
116
/// Adds a circular arc of the given radius to the current path.
117
/// The arc is centered at (xc, yc),
118
/// begins at angle1 and proceeds in the direction
119
/// of decreasing angles to end at angle2.
120
/// If angle2 is greater than angle1 it will be progressively decreased
121
/// by 1 degree until it is less than angle1.
122
/// If there is a current point, an initial line segment will be added to the path
123
/// to connect the current point to the beginning of the arc.
124
/// If this initial line is undesired,
125
/// it can be avoided by calling NewPath() before calling ArcNegative().
127
/// <param name='xc'>
130
/// <param name='yc'>
133
/// <param name='radius'>
136
/// <param name='angle1'>
137
/// Angle1 in degrees
139
/// <param name='angle2'>
140
/// Angle2 in degrees
142
public void ArcNegative (double xc, double yc, double radius, double angle1, double angle2)
144
handler.ArcNegative (Backend, xc, yc, radius, angle1, angle2);
148
/// Establishes a new clip region by intersecting the current clip region with the current Path
149
/// as it would be filled by fill() and according to the current fill rule.
150
/// After clip(), the current path will be cleared from the Context.
151
/// The current clip region affects all drawing operations by effectively masking out any changes to the surface
152
/// that are outside the current clip region.
153
/// Calling clip() can only make the clip region smaller, never larger.
154
/// But the current clip is part of the graphics state,
155
/// so a temporary restriction of the clip region can be achieved by calling clip() within a save()/restore() pair.
156
/// The only other means of increasing the size of the clip region is reset_clip().
160
handler.Clip (Backend);
163
public void ClipPreserve ()
165
handler.ClipPreserve (Backend);
168
public void ClosePath ()
170
handler.ClosePath (Backend);
173
public void CurveTo (Point p1, Point p2, Point p3)
175
CurveTo (p1.X, p1.Y, p2.X, p2.Y, p3.X, p3.Y);
179
/// Adds a cubic Bezier spline to the path from the current point to position (x3, y3) in user-space coordinates,
180
/// using (x1, y1) and (x2, y2) as the control points.
182
/// <param name='x1'>
185
/// <param name='y1'>
188
/// <param name='x2'>
191
/// <param name='y2'>
194
/// <param name='x3'>
197
/// <param name='y3'>
200
public void CurveTo (double x1, double y1, double x2, double y2, double x3, double y3)
202
handler.CurveTo (Backend, x1, y1, x2, y2, x3, y3);
207
handler.Fill (Backend);
210
public void FillPreserve ()
212
handler.FillPreserve (Backend);
215
public void LineTo (Point p)
220
public void LineTo (double x, double y)
222
handler.LineTo (Backend, x, y);
225
public void MoveTo (Point p)
231
/// If the current subpath is not empty, begin a new subpath.
232
/// After this call the current point will be (x, y).
240
public void MoveTo (double x, double y)
242
handler.MoveTo (Backend, x, y);
245
public void NewPath ()
247
handler.NewPath (Backend);
250
public void Rectangle (Rectangle rectangle)
252
Rectangle (rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height);
255
public void Rectangle (Point p, double width, double height)
257
Rectangle (p.X, p.Y, width, height);
260
public void Rectangle (double x, double y, double width, double height)
262
handler.Rectangle (Backend, x, y, width, height);
265
public void RelCurveTo (Distance d1, Distance d2, Distance d3)
267
RelCurveTo (d1.Dx, d1.Dy, d2.Dx, d2.Dy, d3.Dx, d3.Dy);
271
/// Relative-coordinate version of curve_to().
272
/// All offsets are relative to the current point.
273
/// Adds a cubic Bezier spline to the path from the current point to a point offset
274
/// from the current point by (dx3, dy3), using points offset by (dx1, dy1) and (dx2, dy2)
275
/// as the control points. After this call the current point will be offset by (dx3, dy3).
276
/// Given a current point of (x, y), RelCurveTo(dx1, dy1, dx2, dy2, dx3, dy3)
277
/// is logically equivalent to CurveTo(x + dx1, y + dy1, x + dx2, y + dy2, x + dx3, y + dy3).
279
/// <param name='dx1'>
282
/// <param name='dy1'>
285
/// <param name='dx2'>
288
/// <param name='dy2'>
291
/// <param name='dx3'>
294
/// <param name='dy3'>
297
public void RelCurveTo (double dx1, double dy1, double dx2, double dy2, double dx3, double dy3)
299
handler.RelCurveTo (Backend, dx1, dy1, dx2, dy2, dx3, dy3);
302
public void RelLineTo (Distance d)
304
RelLineTo (d.Dx, d.Dy);
308
/// Adds a line to the path from the current point to a point that
309
/// is offset from the current point by (dx, dy) in user space.
310
/// After this call the current point will be offset by (dx, dy).
311
/// Given a current point of (x, y),
312
/// RelLineTo(dx, dy) is logically equivalent to LineTo(x + dx, y + dy).
314
/// <param name='dx'>
317
/// <param name='dy'>
320
public void RelLineTo (double dx, double dy)
322
handler.RelLineTo (Backend, dx, dy);
326
/// If the current subpath is not empty, begin a new subpath.
327
/// After this call the current point will offset by (x, y).
328
/// Given a current point of (x, y),
329
/// RelMoveTo(dx, dy) is logically equivalent to MoveTo(x + dx, y + dy).
334
public void RelMoveTo (Distance d)
336
RelMoveTo (d.Dx, d.Dy);
339
public void RelMoveTo (double dx, double dy)
341
handler.RelMoveTo (Backend, dx, dy);
344
public void Stroke ()
346
handler.Stroke (Backend);
349
public void StrokePreserve ()
351
handler.StrokePreserve (Backend);
354
public void SetLineWidth (double width)
356
handler.SetLineWidth (Backend, width);
359
public void DrawTextLayout (TextLayout layout, Point location)
361
handler.DrawTextLayout (Backend, layout, location.X, location.Y);
364
public void DrawTextLayout (TextLayout layout, double x, double y)
366
handler.DrawTextLayout (Backend, layout, x, y);
369
public void DrawImage (Image img, Point location)
371
handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, 1);
374
public void DrawImage (Image img, double x, double y)
376
handler.DrawImage (Backend, GetBackend (img), x, y, 1);
379
public void DrawImage (Image img, Point location, double alpha)
381
handler.DrawImage (Backend, GetBackend (img), location.X, location.Y, alpha);
384
public void DrawImage (Image img, double x, double y, double alpha)
386
handler.DrawImage (Backend, GetBackend (img), x, y, alpha);
389
public void DrawImage (Image img, Rectangle rect)
391
handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, 1);
394
public void DrawImage (Image img, double x, double y, double width, double height)
396
handler.DrawImage (Backend, GetBackend (img), x, y, width, height, 1);
399
public void DrawImage (Image img, Rectangle rect, double alpha)
401
handler.DrawImage (Backend, GetBackend (img), rect.X, rect.Y, rect.Width, rect.Height, alpha);
404
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect)
406
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, 1);
409
public void DrawImage (Image img, Rectangle srcRect, Rectangle destRect, double alpha)
411
handler.DrawImage (Backend, GetBackend (img), srcRect, destRect, alpha);
415
/// Applies a rotation transformation
417
/// <param name='angle'>
421
/// Modifies the current transformation matrix (CTM) by rotating the user-space axes by angle degrees.
422
/// The rotation of the axes takes places after any existing transformation of user space.
423
/// The rotation direction for positive angles is from the positive X axis toward the positive Y axis.
426
public void Rotate (double angle)
428
handler.Rotate (Backend, angle);
431
public void Scale (double scaleX, double scaleY)
433
handler.Scale (Backend, scaleX, scaleY);
436
public void Translate (double tx, double ty)
438
handler.Translate (Backend, tx, ty);
441
public void Translate (Point p)
443
handler.Translate (Backend, p.X, p.Y);
447
/// Transforms the point (x, y) by the current transformation matrix (CTM)
449
public void TransformPoint (ref double x, ref double y)
451
handler.TransformPoint (Backend, ref x, ref y);
455
/// Transforms the distance (dx, dy) by the scale and rotation elements (only) of the CTM
457
public void TransformDistance (ref double dx, ref double dy)
459
handler.TransformDistance (Backend, ref dx, ref dy);
463
/// Transforms the array of points by the current transformation matrix (CTM)
465
public void TransformPoints (Point[] points)
467
handler.TransformPoints (Backend, points);
471
/// Transforms the array of distances by the scale and rotation elements (only) of the CTM
473
public void TransformDistances (Distance[] vectors)
475
handler.TransformDistances (Backend, vectors);
478
public void Dispose ()
480
handler.Dispose (Backend);
483
public Pattern Pattern {
484
get { return pattern; }
487
handler.SetPattern (Backend, GetBackend (value));
495
handler.SetFont (Backend, value);
500
/// Sets the dash pattern to be used by stroke().
501
/// A dash pattern is specified by dashes, an array of positive values.
502
/// Each value provides the user-space length of altenate "on" and "off" portions of the stroke.
503
/// The offset specifies an offset into the pattern at which the stroke begins.
504
/// If dashes is empty dashing is disabled. If the size of dashes is 1,
505
/// a symmetric pattern is assumed with alternating on and off portions of the size specified by the single value in dashes.
506
/// It is invalid for any value in dashes to be negative, or for all values to be 0.
507
/// If this is the case, an exception will be thrown
509
/// <param name='offset'>
512
/// <param name='pattern'>
515
public void SetLineDash (double offset, params double[] pattern)
517
handler.SetLineDash (Backend, offset, pattern);