Ejemplo n.º 1
0
        public void Show(AnimationType animationType = AnimationType.Bottom)
        {
            if (controller is InteractivePopNavigationController interactiveController)
            {
                interactiveController.IsPushingViewController = true;
            }

            controller.View.AddSubview(popup);

            switch (animationType)
            {
            case AnimationType.Bottom:
                dialog.Transform = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, UIScreen.MainScreen.Bounds.Bottom);

                UIView.Animate(0.3, () =>
                {
                    popup.BackgroundColor = UIColor.Black.ColorWithAlpha(0.5f);
                    dialog.Transform      = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, targetY - 10);
                }, () =>
                {
                    UIView.Animate(0.1, () =>
                    {
                        dialog.Transform = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, targetY);
                    });
                });
                break;
            }
        }
        UIImage ForceImagePortrait(UIImage image)
        {
            const float M_PI = 3.14159265359f;

            CGAffineTransform rectTransform = CGAffineTransform.MakeIdentity();

            switch (image.Orientation)
            {
            case UIImageOrientation.Left:
                rectTransform = CGAffineTransform.Translate(CGAffineTransform.MakeRotation(M_PI * 2), 0, -image.Size.Height);
                break;

            case UIImageOrientation.Right:
                rectTransform = CGAffineTransform.Translate(CGAffineTransform.MakeRotation(-(M_PI * 2)), -image.Size.Width, 0);
                break;

            case UIImageOrientation.Down:
                rectTransform = CGAffineTransform.Translate(CGAffineTransform.MakeRotation(-M_PI), -image.Size.Width,
                                                            -image.Size.Height);
                break;
            }
            ;
            rectTransform = CGAffineTransform.Scale(rectTransform, image.CurrentScale, image.CurrentScale);

            image.CGImage.WithImageInRect(CGAffineTransform.CGRectApplyAffineTransform(
                                              new RectangleF(0, 0, image.Size.Width, image.Size.Height), rectTransform));

            var portraitImage = UIImage.FromImage(image.CGImage, image.CurrentScale, image.Orientation);

            return(portraitImage);
        }
Ejemplo n.º 3
0
        public void ZoomTo(nfloat newScale, CGPoint newAnchor)
        {
            // Figure out what the final scale will be after this transform.
            var targetScale = cumulativeScale * newScale;

            // If it's less than 1, we don't allow that at this point, so we constrain it to 1.0.
            if (targetScale < 1.0f)
            {
                newScale = 1.0f / cumulativeScale;
            }

            // Scale and translate the last transform.
            lastTransform.Scale(newScale, newScale);
            lastTransform.Translate(-GetZoomOffsetX(newScale, newAnchor), -GetZoomOffsetY(newScale, newAnchor));

            // Update the cumulative scale.
            cumulativeScale *= newScale;

            // Constrain the plot to the bounds of the chart using the new cumulative scale.
            lastTransform = ConstrainTransformToBounds(lastTransform, cumulativeScale);

            // Update the ploat transform.
            plotTransform = lastTransform;
            UpdatePlot();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates native transform which applies multiple transformations in this order:
        /// Scale(ScaleX, ScaleY )
        /// Skew(SkewX, SkewY)
        /// Rotate(Rotation)
        /// Translate(TranslateX, TranslateY)
        /// https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.compositetransform.aspx
        /// </summary>
        /// <returns></returns>
        internal override CGAffineTransform ToNativeTransform(CGSize size, bool withCenter = true)
        {
            var pivotX = withCenter ? CenterX : 0;
            var pivotY = withCenter ? CenterY : 0;

            var transform = CGAffineTransform.MakeIdentity();

            //Perform transformations about centre
            transform = CGAffineTransform.Translate(transform, (float)(pivotX), (float)(pivotY));

            //Apply transformations in order
            transform = CGAffineTransform.Scale(transform, (float)ScaleX, (float)ScaleY);

            //TODO: implement skew (see eg http://stackoverflow.com/questions/6203738/iphone-skew-a-calayer)
            if ((SkewX != 0 || SkewY != 0) && this.Log().IsEnabled(LogLevel.Warning))
            {
                this.Log().Warn("Skew is not enabled for CompositeTransform.");
            }

            transform = CGAffineTransform.Rotate(transform, (nfloat)ToRadians(Rotation));

            transform = CGAffineTransform.Translate(transform, (float)TranslateX, (float)TranslateY);

            //Unapply centering
            transform = CGAffineTransform.Translate(transform, (float)(-pivotX), (float)(-pivotY));

            return(transform);
        }
        private void UpdateFollowers(nfloat delta)
        {
            foreach (var follower in Followers)
            {
                if (follower is UITabBar tabBar)
                {
                    tabBar.Translucent = true;
                    var frame = tabBar.Frame;
                    frame.Y     += (nfloat)(delta * 1.5);
                    tabBar.Frame = frame;

                    if (sourceTabBar != null)
                    {
                        // Set the bar to its original state if it's in its original position
                        if (sourceTabBar.Frame.Y == tabBar.Frame.Y)
                        {
                            tabBar.Translucent = sourceTabBar.Translucent;
                        }
                    }
                }
                else
                {
                    follower.Transform = CGAffineTransform.Translate(follower.Transform, 0, -delta);
                }
            }
        }
Ejemplo n.º 6
0
        public void animateToChapter(int chapter, bool animated)
        {
            var offsetChange = listVector [chapter].Frame.X;

            if (chapter < SelectedChapter)
            {
                for (int i = 0; i < NumberOfItems; i++)
                {
                    var newFrame = listVector [i].Frame;
                    newFrame.X -= offsetChange;
                    UIView.Animate(animated ? 0.5f : 0, () => {
                        //listVector [i].Frame = newFrame;
                        listVector[i].Transform = CGAffineTransform.Translate(listVector[i].Transform, -offsetChange, 0);
                    });
                }
            }
            else
            {
                for (int i = NumberOfItems - 1; i >= 0; i--)
                {
                    var newFrame = listVector [i].Frame;
                    newFrame.X -= offsetChange;
                    UIView.Animate(animated ? 0.5f : 0, () => {
                        //listVector [i].Frame = newFrame;
                        listVector[i].Transform = CGAffineTransform.Translate(listVector[i].Transform, -offsetChange, 0);
                        //listVector[i].Transform = CGAffineTransform.MakeIdentity();
                    });
                }
            }
            SelectedChapter = chapter;

            ContentSize = new CGSize(listVector[chapter].Frame.Width, ContentSize.Height);
            SetContentOffset(new CGPoint(listVector [chapter].Frame.X, listVector [chapter].Frame.Y), false);
        }
        // Draws our animation path on the background image, just to show it
        protected void DrawPathAsBackground()
        {
            // create our offscreen bitmap context
            // size
            SizeF bitmapSize = new SizeF(View.Frame.Size);

            using (CGBitmapContext context = new CGBitmapContext(
                       IntPtr.Zero,
                       (int)bitmapSize.Width, (int)bitmapSize.Height, 8,
                       (int)(4 * bitmapSize.Width), CGColorSpace.CreateDeviceRGB(),
                       CGImageAlphaInfo.PremultipliedFirst)) {
                // convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1, -1);
                // move the y axis up
                affineTransform.Translate(0, View.Frame.Height);
                context.ConcatCTM(affineTransform);

                // actually draw the path
                context.AddPath(animationPath);
                context.SetStrokeColor(UIColor.LightGray.CGColor);
                context.SetLineWidth(3);
                context.StrokePath();

                // set what we've drawn as the backgound image
                backgroundImage.Image = UIImage.FromImage(context.ToImage());
            }
        }
Ejemplo n.º 8
0
        public void Translate()
        {
            var transform = CGAffineTransform.MakeIdentity();

            transform.Translate(1, -1);              // MatrixOrder.Append by default

            Assert.AreEqual((nfloat)1, transform.xx, "xx");
            Assert.AreEqual((nfloat)0, transform.yx, "yx");
            Assert.AreEqual((nfloat)0, transform.xy, "xy");
            Assert.AreEqual((nfloat)1, transform.yy, "yy");
            Assert.AreEqual((nfloat)1, transform.x0, "x0");
            Assert.AreEqual((nfloat)(-1), transform.y0, "y0");

            transform = new CGAffineTransform(1, 2, 3, 4, 5, 6);
            transform.Translate(2, -3);

            Assert.AreEqual((nfloat)1, transform.xx, "xx");
            Assert.AreEqual((nfloat)2, transform.yx, "yx");
            Assert.AreEqual((nfloat)3, transform.xy, "xy");
            Assert.AreEqual((nfloat)4, transform.yy, "yy");
            Assert.AreEqual((nfloat)7, transform.x0, "x0");
            Assert.AreEqual((nfloat)3, transform.y0, "y0");

            transform = new CGAffineTransform(1, 2, 3, 4, 5, 6);
            transform.Translate(2, -3, MatrixOrder.Prepend);

            Assert.AreEqual((nfloat)1, transform.xx, "xx");
            Assert.AreEqual((nfloat)2, transform.yx, "yx");
            Assert.AreEqual((nfloat)3, transform.xy, "xy");
            Assert.AreEqual((nfloat)4, transform.yy, "yy");
            Assert.AreEqual((nfloat)(-2), transform.x0, "x0");
            Assert.AreEqual((nfloat)(-2), transform.y0, "y0");
        }
Ejemplo n.º 9
0
        // rect changes depending on if the whole view is being redrawn, or just a section
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            // get a reference to the context
            using (CGContext context = UIGraphics.GetCurrentContext()) {
                // convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1, -1);
                // move the y axis up
                affineTransform.Translate(0, Frame.Height);
                context.ConcatCTM(affineTransform);

                // draw our coordinates for reference
                DrawCoordinateSpace(context);

                // draw our flag
                DrawFlag(context);

                // add a label
                DrawCenteredTextAtPoint(context, 384, 700, "Stars and Stripes", 60);
            }
        }
Ejemplo n.º 10
0
        public void StaticTranslate()
        {
            var origin     = CGAffineTransform.MakeIdentity();
            var transformM = CGAffineTransform.Translate(origin, 1, -1);
            var transformN = CGAffineTransformTranslate(origin, 1, -1);

            Assert.AreEqual((nfloat)1, transformM.xx, "xx");
            Assert.AreEqual((nfloat)0, transformM.yx, "yx");
            Assert.AreEqual((nfloat)0, transformM.xy, "xy");
            Assert.AreEqual((nfloat)1, transformM.yy, "yy");
            Assert.AreEqual((nfloat)1, transformM.x0, "x0");
            Assert.AreEqual((nfloat)(-1), transformM.y0, "y0");
            Assert.IsTrue(transformN == transformM);

            origin     = new CGAffineTransform(1, 2, 3, 4, 5, 6);
            transformM = CGAffineTransform.Translate(origin, 2, -3);
            transformN = CGAffineTransformTranslate(origin, 2, -3);

            Assert.AreEqual((nfloat)1, transformM.xx, "xx");
            Assert.AreEqual((nfloat)2, transformM.yx, "yx");
            Assert.AreEqual((nfloat)3, transformM.xy, "xy");
            Assert.AreEqual((nfloat)4, transformM.yy, "yy");
            Assert.AreEqual((nfloat)(-2), transformM.x0, "x0");
            Assert.AreEqual((nfloat)(-2), transformM.y0, "y0");
            Assert.IsTrue(transformN == transformM);
        }
        public override void TraitCollectionDidChange(UITraitCollection previousTraitCollection)
        {
            if (previousTraitCollection == null)
            {
                previousTraitCollection = new UITraitCollection();
            }

            base.TraitCollectionDidChange(previousTraitCollection);

            if (TraitCollection.VerticalSizeClass == UIUserInterfaceSizeClass.Compact)
            {
                CGAffineTransform t = CGAffineTransform.MakeIdentity();
                t.Translate(this.Frame.Width / 2, 0);
                t.Scale(0.5f, 0.5f);

                this.Transform = t;
            }
            else
            {
                CGAffineTransform t = CGAffineTransform.MakeIdentity();
                t.Translate(0, 0);
                t.Scale(1f, 1f);

                this.Transform = t;
            }
        }
Ejemplo n.º 12
0
        //========================================================================

        #endregion
        //========================================================================

        /// <summary>
        /// rect changes depending on if the whole view is being redrawn, or just a section
        /// </summary>
        /// <param name="rect">
        /// A <see cref="RectangleF"/>
        /// </param>
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            using (CGContext context = UIGraphics.GetCurrentContext())
            {
                CGAffineTransform affineTransform = context.GetCTM();
                //affineTransform.Scale (1, -1);
                affineTransform.Translate(1, -1);
                context.ConcatCTM(affineTransform);

                //---- fill the background with white
                // set fill color
                UIColor.White.SetFill();
                //context.SetRGBFillColor (1, 1, 1, 1f);
                // paint
                context.FillRect(rect);

                PointF[] myStarPoints = { new PointF(5f,                 5f)
                                          ,                 new PointF(10f,   15f), new PointF(10f, 15f)
                                          ,                 new PointF(15f,    5f), new PointF(15f, 5f)
                                          ,                 new PointF(12f,    5f), new PointF(15f, 5f)
                                          ,                 new PointF(2.5f,  11f), new PointF(2.5f, 11f)
                                          ,                 new PointF(16.5f, 11f),
                                          new PointF(16.5f,                 11f)
                                          ,                 new PointF(5f, 5f) };

                //---- create the layer
                using (CGLayer starLayer = CGLayer.Create(context, rect.Size))
                {
                    //---- set fill to blue
                    starLayer.Context.SetRGBFillColor(0f, 0f, 1f, 1f);
                    starLayer.Context.AddLines(myStarPoints);
                    starLayer.Context.FillPath();

                    //---- draw the layer onto our screen
                    float starYPos = 5;
                    float starXPos = 5;

                    for (int row = 0; row < 50; row++)
                    {
                        //---- reset the x position for each row
                        starXPos = 5;
                        //----
                        for (int col = 0; col < 30; col++)
                        {
                            context.DrawLayer(starLayer, new PointF(starXPos, starYPos));
                            starXPos += 20;
                        }
                        starYPos += 20;
                    }
                }
            }
        }
Ejemplo n.º 13
0
        internal override CGAffineTransform ToNativeTransform(CGSize size, bool withCenter = true)
        {
            var pivotX = withCenter ? CenterX : 0;
            var pivotY = withCenter ? CenterY : 0;

            CGAffineTransform transform = CGAffineTransform.MakeTranslation((nfloat)(pivotX), (nfloat)(pivotY));

            transform = CGAffineTransform.Rotate(transform, (nfloat)ToRadians(Angle));
            transform = CGAffineTransform.Translate(transform, -(nfloat)pivotX, -(nfloat)pivotY);
            return(transform);
        }
Ejemplo n.º 14
0
        public override void DrawInContext(CGContext context)
        {
            base.DrawInContext(context);
            nfloat size = NMath.Min(Bounds.Size.Width, Bounds.Size.Height);

            CGAffineTransform transform = AffineTransform;

            nfloat xTranslate = 0;
            nfloat yTranslate = 0;

            if (Bounds.Size.Width < Bounds.Size.Height)
            {
                yTranslate = (Bounds.Size.Height - size) / 2f;
            }
            else
            {
                xTranslate = (Bounds.Size.Width - size) / 2f;
            }

            transform.Translate(xTranslate, yTranslate);

            nfloat strokeWidth   = StrokeFactor * size;
            nfloat checkBoxInset = InsetFactor * size;

            // Create the outer border for the check box.
            nfloat outerDimension = size - 2 * checkBoxInset;
            var    checkBoxRect   = new CGRect(checkBoxInset, checkBoxInset, outerDimension, outerDimension);

            checkBoxRect = transform.TransformRect(checkBoxRect);

            // Make the desired width of the outer box.
            context.SetLineWidth(strokeWidth);

            // Set the tint color of the outer box.
            context.SetStrokeColor(TintColor);

            // Draw the outer box.
            context.StrokeRect(checkBoxRect);

            // Draw the inner box if it's checked.
            if (Checked)
            {
                nfloat markInset = MarkInsetFactor * size;

                nfloat markDimension = size - 2 * markInset;
                var    markRect      = new CGRect(markInset, markInset, markDimension, markDimension);
                markRect = transform.TransformRect(markRect);

                context.SetFillColor(TintColor);
                context.FillRect(markRect);
            }
        }
Ejemplo n.º 15
0
 public void Hide()
 {
     UIView.Animate(0.3, () =>
     {
         popup.BackgroundColor = UIColor.Black.ColorWithAlpha(0.0f);
         dialog.Transform      = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, UIScreen.MainScreen.Bounds.Bottom);
     }, () =>
     {
         if (controller is InteractivePopNavigationController interactiveController)
         {
             interactiveController.IsPushingViewController = false;
         }
         popup.RemoveFromSuperview();
     });
 }
 public ViewController(IntPtr handle) : base(handle)
 {
     MaskLayer            = new CAShapeLayer();
     CurrentOrientation   = UIDeviceOrientation.Portrait;
     captureSession       = new AVCaptureSession();
     VideoDataOutput      = new AVCaptureVideoDataOutput();
     VideoDataOutputQueue = new DispatchQueue("VideoDataOutputQueue");
     RegionOfInterest     = new CGRect(0, 0, 1, 1);
     TextOrientation      = ImageIO.CGImagePropertyOrientation.Up;
     UIRotationTransform  = CGAffineTransform.MakeIdentity();
     BottomToTopTransform = CGAffineTransform.MakeScale(1, -1);
     BottomToTopTransform = CGAffineTransform.Translate(BottomToTopTransform, 0, -1);
     ROIToGlobalTransform = CGAffineTransform.MakeIdentity();
     gestureRecognizer    = new UITapGestureRecognizer((gesture) => {
         HandleTap();
     });
 }
Ejemplo n.º 17
0
        internal override CGAffineTransform ToNativeTransform(CGSize size, bool withCenter = true)
        {
            var pivotX = withCenter ? CenterX : 0;
            var pivotY = withCenter ? CenterY : 0;

            var transform = CGAffineTransform.MakeIdentity();

            //Perform transformations about centre
            transform = CGAffineTransform.Translate(transform, (nfloat)pivotX, (nfloat)pivotY);

            //Apply transformations in order
            transform = CGAffineTransform.Scale(transform, (nfloat)ScaleX, (nfloat)ScaleY);

            //Unapply centering
            transform = CGAffineTransform.Translate(transform, -(nfloat)pivotX, -(nfloat)pivotY);

            return(transform);
        }
        private void UpdateSquareTransform(CGPoint translation, nfloat rotation, nfloat scale)
        {
            var transform = CGAffineTransform.MakeIdentity();

            Console.WriteLine(lastScale);

            // Include previous translation, rotation and scale
            translation.X += lastTranslation.X;
            translation.Y += lastTranslation.Y;
            rotation      += lastRotation;
            scale         *= lastScale;

            // Combine translation, rotation and scale
            transform = CGAffineTransform.Translate(transform, translation.X, translation.Y);
            transform = CGAffineTransform.Rotate(transform, rotation);
            transform = CGAffineTransform.Scale(transform, scale, scale);

            square.Transform = transform;
        }
Ejemplo n.º 19
0
        // rect changes depending on if the whole view is being redrawn, or just a section
        public override void Draw(RectangleF rect)
        {
            Console.WriteLine("Draw() Called");
            base.Draw(rect);

            // get a reference to the context
            using (CGContext context = UIGraphics.GetCurrentContext()) {
                // convert to View space
                CGAffineTransform affineTransform = CGAffineTransform.MakeIdentity();
                // invert the y axis
                affineTransform.Scale(1, -1);
                // move the y axis up
                affineTransform.Translate(0, Frame.Height);
                context.ConcatCTM(affineTransform);

                // draw some stars
                DrawStars(context);
            }
        }
Ejemplo n.º 20
0
        private void SetAnimation(AnimationType animationType)
        {
            switch (animationType)
            {
            case AnimationType.Bottom:
                dialog.Transform = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, UIScreen.MainScreen.Bounds.Bottom);

                UIView.Animate(0.3, () =>
                {
                    popup.BackgroundColor = UIColor.Black.ColorWithAlpha(0.5f);
                    dialog.Transform      = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, targetY - 10);
                }, () =>
                {
                    UIView.Animate(0.1, () =>
                    {
                        dialog.Transform = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), 0, targetY);
                    });
                });
                break;
            }
        }
Ejemplo n.º 21
0
        void modifiedGesture(UIGestureRecognizer gesture)
        {
            if (gesture.GetType() == typeof(UIPinchGestureRecognizer))
            {
                var pinch = (UIPinchGestureRecognizer)gesture;
                scaleTransform = CGAffineTransform.Scale(CGAffineTransform.MakeIdentity(), pinch.Scale, pinch.Scale);
            }
            else if (gesture.GetType() == typeof(UIRotationGestureRecognizer))
            {
                var rotate = (UIRotationGestureRecognizer)gesture;
                rotateTransform = CGAffineTransform.Rotate(CGAffineTransform.MakeIdentity(), rotate.Rotation);
            }
            else if (gesture.GetType() == typeof(UIPanGestureRecognizer))
            {
                var     pan         = (UIPanGestureRecognizer)gesture;
                CGPoint translation = pan.TranslationInView(Superview);
                panTransform = CGAffineTransform.Translate(CGAffineTransform.MakeIdentity(), translation.X, translation.Y);
            }

            Transform = CGAffineTransform.Multiply(CGAffineTransform.Multiply(scaleTransform, rotateTransform), panTransform);
        }
Ejemplo n.º 22
0
        CGAffineTransform GetImageTransformAboutCenter(float angleDegrees, CGSize imageSize)
        {
            // Create a tranform that will rotate our image about its center
            CGAffineTransform transform = CGAffineTransform.MakeIdentity( );

            // setup our transform. Translate it by the image's half width/height so it rotates about its center.
            transform.Translate(-imageSize.Width / 2, -imageSize.Height / 2);
            transform.Rotate(angleDegrees * Rock.Mobile.Math.Util.DegToRad);

            // now we need to concat on a post-transform that will put the image's pivot back at the top left.
            // get the image's dimensions transformed
            CGRect transformedImageRect = transform.TransformRect(new CGRect(0, 0, imageSize.Width, imageSize.Height));

            // our post transform simply translates the image back
            CGAffineTransform postTransform = CGAffineTransform.MakeIdentity( );

            postTransform.Translate(transformedImageRect.Width / 2, transformedImageRect.Height / 2);

            // now multiply the transform and postTranform and we have our final transform to use
            return(CGAffineTransform.Multiply(transform, postTransform));
        }
Ejemplo n.º 23
0
        private byte[] RotateImage(UIImage image)
        {
            UIImage imageToReturn = null;

            if (image.Size.Height > image.Size.Width)
            {
                imageToReturn = image;
            }
            else
            {
                CGAffineTransform transform = CGAffineTransform.MakeIdentity();
                transform.Rotate(-(float)Math.PI / 2);
                transform.Translate(0, image.Size.Width);
                //now draw image
                using (var context = new CGBitmapContext(IntPtr.Zero,
                                                         (int)image.Size.Height,
                                                         (int)image.Size.Width,
                                                         image.CGImage.BitsPerComponent,
                                                         image.CGImage.BytesPerRow,
                                                         image.CGImage.ColorSpace,
                                                         image.CGImage.BitmapInfo))
                {
                    context.ConcatCTM(transform);
                    context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);

                    using (var imageRef = context.ToImage())
                    {
                        imageToReturn = new UIImage(imageRef);
                    }
                }
            }

            using (NSData imageData = imageToReturn.AsPNG())
            {
                Byte[] byteArray = new Byte[imageData.Length];
                System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length));
                return(byteArray);
            }
        }
Ejemplo n.º 24
0
        public Matrix(RectangleF rect, PointF[] plgpts)
        {
            if (plgpts == null)
            {
                throw new ArgumentNullException("plgpts");
            }
            if (plgpts.Length != 3)
            {
                throw new ArgumentException("plgpts");
            }

            PointF p0 = plgpts [0];
            PointF p1 = plgpts [1];
            PointF p2 = plgpts [2];

            float m11 = (p1.X - p0.X) / rect.Width;
            float m12 = (p1.Y - p0.Y) / rect.Width;
            float m21 = (p2.X - p0.X) / rect.Height;
            float m22 = (p2.X - p0.X) / rect.Height;

            transform = new CGAffineTransform(m11, m12, m21, m22, p0.X, p0.Y);
            transform.Translate(rect.Width, rect.Height);
        }
Ejemplo n.º 25
0
        public void Translate()
        {
            var transform = CGAffineTransform.MakeIdentity();

            transform.Translate(1, -1);

            Assert.AreEqual((nfloat)1, transform.xx, "xx");
            Assert.AreEqual((nfloat)0, transform.yx, "yx");
            Assert.AreEqual((nfloat)0, transform.xy, "xy");
            Assert.AreEqual((nfloat)1, transform.yy, "yy");
            Assert.AreEqual((nfloat)1, transform.x0, "x0");
            Assert.AreEqual((nfloat)(-1), transform.y0, "y0");

            transform = new CGAffineTransform(1, 2, 3, 4, 5, 6);
            transform.Translate(2, -3);

            Assert.AreEqual((nfloat)1, transform.xx, "xx");
            Assert.AreEqual((nfloat)2, transform.yx, "yx");
            Assert.AreEqual((nfloat)3, transform.xy, "xy");
            Assert.AreEqual((nfloat)4, transform.yy, "yy");
            Assert.AreEqual((nfloat)7, transform.x0, "x0");
            Assert.AreEqual((nfloat)3, transform.y0, "y0");
        }
        private void AnimateOpening(double duration = 0)
        {
            if (_isOpened)
            {
                return;
            }
            _isOpened = true;

            if (_wasResized || _hintLabel.Frame.Height == 0)
            {
                _hintLabel.Superview.SetNeedsLayout();
                _hintLabel.Superview.LayoutIfNeeded();
                _wasResized = false;
            }

            AnimateNotify(
                duration,
                () =>
            {
                var initialTransform    = _hintLabel.Transform;
                var scaledTransform     = CGAffineTransform.Scale(initialTransform, HintLabelScale, HintLabelScale);
                var translatedTransform = CGAffineTransform.Translate(
                    scaledTransform,
                    -((Frame.Width - _hintLabel.Frame.Width * HintLabelScale) / 2 / HintLabelScale),
                    -(((Frame.Height * HintLabelOffsetMultiplier - _hintLabel.Frame.Height * HintLabelScale) / 2 +
                       _hintLabel.Frame.Height * HintLabelScale) / HintLabelScale));
                _hintLabel.Transform = translatedTransform;
            },
                completed =>
            {
                if (!completed)
                {
                    return;
                }
                BackgroundColor = _backgroundColor;
            });
        }
Ejemplo n.º 27
0
        public static Stream RotateImage(UIImage image)
        {
            UIImage imageToReturn = null;

            if (image.Orientation == UIImageOrientation.Up)
            {
                imageToReturn = image;
            }
            else
            {
                CGAffineTransform transform = CGAffineTransform.MakeIdentity();

                switch (image.Orientation)
                {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform.Rotate((float)Math.PI);
                    transform.Translate(image.Size.Width, image.Size.Height);
                    break;

                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform.Rotate((float)Math.PI / 2);
                    transform.Translate(image.Size.Width, 0);
                    break;

                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform.Rotate(-(float)Math.PI / 2);
                    transform.Translate(0, image.Size.Height);
                    break;

                case UIImageOrientation.Up:
                case UIImageOrientation.UpMirrored:
                    break;
                }

                switch (image.Orientation)
                {
                case UIImageOrientation.UpMirrored:
                case UIImageOrientation.DownMirrored:
                    transform.Translate(image.Size.Width, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.RightMirrored:
                    transform.Translate(image.Size.Height, 0);
                    transform.Scale(-1, 1);
                    break;

                case UIImageOrientation.Up:
                case UIImageOrientation.Down:
                case UIImageOrientation.Left:
                case UIImageOrientation.Right:
                    break;
                }

                using (var context = new CGBitmapContext(IntPtr.Zero,
                                                         (int)image.Size.Width,
                                                         (int)image.Size.Height,
                                                         image.CGImage.BitsPerComponent,
                                                         image.CGImage.BytesPerRow,
                                                         image.CGImage.ColorSpace,
                                                         image.CGImage.BitmapInfo))
                {
                    context.ConcatCTM(transform);
                    switch (image.Orientation)
                    {
                    case UIImageOrientation.Left:
                    case UIImageOrientation.LeftMirrored:
                    case UIImageOrientation.Right:
                    case UIImageOrientation.RightMirrored:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Height, (float)image.Size.Width)), image.CGImage);
                        break;

                    default:
                        context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage);
                        break;
                    }

                    using (var imageRef = context.ToImage())
                    {
                        imageToReturn = new UIImage(imageRef);
                    }
                }
            }

            var finalQuality = 1.0f;
            var imageData    = image.AsJPEG(finalQuality);

            //continue to move down quality , rare instances
            while (imageData == null && finalQuality > 0)
            {
                finalQuality -= 0.05f;
                imageData     = image.AsJPEG(finalQuality);
            }

            if (imageData == null)
            {
                throw new NullReferenceException("Unable to convert image to jpeg, please ensure file exists or lower quality level");
            }

            var stream = imageData.AsStream();

            imageData.Dispose();
            return(stream);
        }
Ejemplo n.º 28
0
 public void TranslateTransform(float offsetX, float offsetY)
 {
     Control.TranslateCTM(offsetX, offsetY);
     currentTransform.Translate(offsetX, offsetY);
 }
        internal void NativeDrawString(string s, Font font, Color brush, RectangleF layoutRectangle, StringFormat stringFormat)
        {
            if (font == null)
                throw new ArgumentNullException ("font");

            if (s == null || s.Length == 0)
                return;

            var attributedString = buildAttributedString(s, font, brush);

            // Work out the geometry
            RectangleF insetBounds = layoutRectangle;
            bool layoutAvailable = true;

            if (insetBounds.Size == SizeF.Empty)
            {
                insetBounds.Size = new SizeF (8388608, 8388608);
                layoutAvailable = false;
            }

            PointF textPosition = new PointF(insetBounds.X,
                                             insetBounds.Y);

            float boundsWidth = insetBounds.Width;

            // Calculate the lines
            int start = 0;
            int length = (int)attributedString.Length;

            var typesetter = new CTTypesetter(attributedString);

            float baselineOffset = 0;

            // First we need to calculate the offset for Vertical Alignment if we
            // are using anything but Top
            if (layoutAvailable && stringFormat.LineAlignment != StringAlignment.Near) {
                while (start < length) {
                    int count = (int)typesetter.SuggestLineBreak (start, boundsWidth);
                    var line = typesetter.GetLine (new NSRange(start, count));

                    // Create and initialize some values from the bounds.
                    nfloat ascent;
                    nfloat descent;
                    nfloat leading;
                    line.GetTypographicBounds (out ascent, out descent, out leading);
                    baselineOffset += (float)Math.Ceiling (ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior
                    line.Dispose ();
                    start += count;
                }
            }

            start = 0;

            while (start < length && textPosition.Y < insetBounds.Bottom)
            {

                // Now we ask the typesetter to break off a line for us.
                // This also will take into account line feeds embedded in the text.
                //  Example: "This is text \n with a line feed embedded inside it"
                int count = (int)typesetter.SuggestLineBreak(start, boundsWidth);
                var line = typesetter.GetLine(new NSRange(start, count));

                // Create and initialize some values from the bounds.
                nfloat ascent;
                nfloat descent;
                nfloat leading;
                double lineWidth = line.GetTypographicBounds(out ascent, out descent, out leading);

                if (!layoutAvailable)
                {
                    insetBounds.Width = (float)lineWidth;
                    insetBounds.Height = (float)(ascent + descent + leading);
                }

                // Calculate the string format if need be
                var penFlushness = 0.0f;

                if (stringFormat.Alignment == StringAlignment.Far)
                    penFlushness = (float)line.GetPenOffsetForFlush(1.0f, insetBounds.Width);
                else if (stringFormat.Alignment == StringAlignment.Center)
                    penFlushness = (float)line.GetPenOffsetForFlush(0.5f, insetBounds.Width);

                // initialize our Text Matrix or we could get trash in here
                var textMatrix = new CGAffineTransform (
                                     1, 0, 0, -1, 0, ascent);

                if (stringFormat.LineAlignment == StringAlignment.Near)
                    textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y); //insetBounds.Height - textPosition.Y -(float)Math.Floor(ascent - 1));
                if (stringFormat.LineAlignment == StringAlignment.Center)
                    textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y + ((insetBounds.Height / 2) - (baselineOffset / 2)) );  // -(float)Math.Floor(ascent)
                if (stringFormat.LineAlignment == StringAlignment.Far)
                    textMatrix.Translate(penFlushness + textPosition.X,  textPosition.Y + ((insetBounds.Height) - (baselineOffset)));

                var glyphRuns = line.GetGlyphRuns ();

                for (int glyphRunIndex = 0; glyphRunIndex < glyphRuns.Length; glyphRunIndex++)
                {

                    var glyphRun = glyphRuns [glyphRunIndex];
                    var glyphs = glyphRun.GetGlyphs ();
                    var glyphPositions = glyphRun.GetPositions ();
                    //var textMatrix = glyphRun.TextMatrix;

                    // Create and initialize some values from the bounds.
                    float glyphAscent;
                    float glyphDescent;
                    float glyphLeading;

                    var elementPoints = new PointF [3];

                    for (int glyphIndex = 0; glyphIndex < glyphs.Length; glyphIndex++)
                    {
                        if (glyphIndex > 0)
                        {
                            textMatrix.x0 += glyphPositions [glyphIndex].X - glyphPositions[glyphIndex - 1].X;
                            textMatrix.y0 += glyphPositions [glyphIndex].Y - glyphPositions[glyphIndex - 1].Y;
                        }

                        var glyphPath = font.nativeFont.GetPathForGlyph (glyphs [glyphIndex]);

                        // glyphPath = null if it is a white space character
                        if (glyphPath != null) {

                            glyphPath.Apply (
                                delegate (CGPathElement pathElement) {

                                    elementPoints[0] = textMatrix.TransformPoint(pathElement.Point1).ToPointF ();
                                    elementPoints[1] = textMatrix.TransformPoint(pathElement.Point2).ToPointF ();
                                        elementPoints[2] = textMatrix.TransformPoint(pathElement.Point3).ToPointF ();
                                //Console.WriteLine ("Applying {0} - {1}, {2}, {3}", pathElement.Type, elementPoints[0], elementPoints[1], elementPoints[2]);

                                        // now add position offsets
                                        switch(pathElement.Type)
                                        {
                                        case CGPathElementType.MoveToPoint:
                                            start_new_fig = true;
                                            Append(elementPoints[0].X, elementPoints[0].Y,PathPointType.Line,true);
                                            break;
                                        case CGPathElementType.AddLineToPoint:
                                            var lastPoint = points[points.Count - 1];
                                            AppendPoint(lastPoint, PathPointType.Line, false);
                                            AppendPoint(elementPoints[0], PathPointType.Line, false);
                                            break;
                                        case CGPathElementType.AddCurveToPoint:
                                        case CGPathElementType.AddQuadCurveToPoint:
                                            //  This is the only thing I can think of right now for the fonts that
                                            //  I have tested.  See the description of the quadraticToCubic method for
                                            //  more information

                                            // Get the last point
                                            var pt1 = points[points.Count - 1];
                                            var pt2 = PointF.Empty;
                                            var pt3 = PointF.Empty;
                                            var pt4 = elementPoints[1];
                                            GeomUtilities.QuadraticToCubic(pt1, elementPoints[0], elementPoints[1], out pt2, out pt3);
                                            Append (pt1.X, pt1.Y, PathPointType.Line, true);
                                            AppendBezier (pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y);
                                            break;
                                        case CGPathElementType.CloseSubpath:
                                            CloseFigure();
                                            break;
                                        }

                                }

                            );
                        }

                    }
                }

                // Move the index beyond the line break.
                start += count;
                textPosition.Y += (float)Math.Ceiling(ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior
                line.Dispose();

            }
        }
        public void DrawString(string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format = null)
        {
            if (font == null)
                throw new ArgumentNullException ("font");
            if (brush == null)
                throw new ArgumentNullException ("brush");
            if (s == null || s.Length == 0)
                return;

            if (format == null)
            {
                format = StringFormat.GenericDefault;
            }

            // TODO: Take into consideration units

            // Not sure we need the Save and Restore around this yet.
            context.SaveState();

            // TextMatrix is not part of the Graphics State and Restore
            var saveMatrix = context.TextMatrix;
            bool layoutAvailable = true;

            //			context.SelectFont ( font.nativeFont.PostScriptName,
            //			                    font.SizeInPoints,
            //			                    CGTextEncoding.MacRoman);
            //
            //			context.SetCharacterSpacing(1);
            //			context.SetTextDrawingMode(CGTextDrawingMode.Fill); // 5
            //
            //			// Setup both the stroke and the fill ?
            //			brush.Setup(this, true);
            //			brush.Setup(this, false);
            //
            //			var textMatrix = font.nativeFont.Matrix;
            //
            //			textMatrix.Scale(1,-1);
            //			context.TextMatrix = textMatrix;
            //
            //			context.ShowTextAtPoint(layoutRectangle.X,
            //			                        layoutRectangle.Y + font.nativeFont.CapHeightMetric, s);
            //
            //
            // First we call the brush with a fill of false so the brush can setup the stroke color
            // that the text will be using.
            // For LinearGradientBrush this will setup a TransparentLayer so the gradient can
            // be filled at the end.  See comments.
            brush.Setup(this, false); // Stroke

            // I think we only Fill the text with no Stroke surrounding
            context.SetTextDrawingMode(CGTextDrawingMode.Fill);

            var attributedString = buildAttributedString(s, font, format, lastBrushColor);

            // Work out the geometry
            RectangleF insetBounds = layoutRectangle;
            if (insetBounds.Size == SizeF.Empty)
            {
                insetBounds.Width = boundingBox.Width;
                insetBounds.Height = boundingBox.Height;
                layoutAvailable = false;

                if (format.LineAlignment != StringAlignment.Near)
                {
                    insetBounds.Size = MeasureString (s, font);
                }

            }

            PointF textPosition = new PointF(insetBounds.X,
                                             insetBounds.Y);

            float boundsWidth = insetBounds.Width;

            // Calculate the lines
            int start = 0;
            int length = (int)attributedString.Length;
            float baselineOffset = 0;

            var typesetter = new CTTypesetter(attributedString);

            // First we need to calculate the offset for Vertical Alignment if we
            // are using anything but Top
            if (layoutAvailable && format.LineAlignment != StringAlignment.Near) {
                while (start < length) {
                    int count = (int)typesetter.SuggestLineBreak (start, boundsWidth);
                    var line = typesetter.GetLine (new NSRange(start, count));

                    // Create and initialize some values from the bounds.
                    nfloat ascent;
                    nfloat descent;
                    nfloat leading;
                    line.GetTypographicBounds (out ascent, out descent, out leading);
                    baselineOffset += (float)Math.Ceiling (ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior
                    line.Dispose ();
                    start += count;
                }

                start = 0;
            }

            // If we are drawing vertial direction then we need to rotate our context transform by 90 degrees
            if ((format.FormatFlags & StringFormatFlags.DirectionVertical) == StringFormatFlags.DirectionVertical)
            {
                //textMatrix.Rotate (ConversionHelpers.DegreesToRadians (90));
                var verticalOffset = 0.0f;
                while (start < length) {
                    int count = (int)typesetter.SuggestLineBreak (start, boundsWidth);
                    var line = typesetter.GetLine (new NSRange(start, count));

                    // Create and initialize some values from the bounds.
                    nfloat ascent;
                    nfloat descent;
                    nfloat leading;
                    line.GetTypographicBounds (out ascent, out descent, out leading);
                    verticalOffset += (float)Math.Ceiling (ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior
                    line.Dispose ();
                    start += count;
                }
                context.TranslateCTM (layoutRectangle.X, layoutRectangle.Y);
                context.RotateCTM (ConversionHelpers.DegreesToRadians (90));
                context.TranslateCTM (-layoutRectangle.X, -layoutRectangle.Y);
                context.TranslateCTM (0, -verticalOffset);
                start = 0;
            }

            start = 0;
            while (start < length && textPosition.Y < insetBounds.Bottom)
            {

                // Now we ask the typesetter to break off a line for us.
                // This also will take into account line feeds embedded in the text.
                //  Example: "This is text \n with a line feed embedded inside it"
                int count = (int)typesetter.SuggestLineBreak(start, boundsWidth);
                var line = typesetter.GetLine(new NSRange(start, count));

                // Create and initialize some values from the bounds.
                nfloat ascent;
                nfloat descent;
                nfloat leading;
                double lineWidth = line.GetTypographicBounds(out ascent, out descent, out leading);

                // Calculate the string format if need be
                var penFlushness = 0.0f;
                if (format != null)
                {
                    if (layoutAvailable)
                    {
                        if (format.Alignment == StringAlignment.Far)
                            penFlushness = (float)line.GetPenOffsetForFlush(1.0f, boundsWidth);
                        else if (format.Alignment == StringAlignment.Center)
                            penFlushness = (float)line.GetPenOffsetForFlush(0.5f, boundsWidth);
                    }
                    else
                    {
                        // We were only passed in a point so we need to format based
                        // on the point.
                        if (format.Alignment == StringAlignment.Far)
                            penFlushness -= (float)lineWidth;
                        else if (format.Alignment == StringAlignment.Center)
                            penFlushness -= (float)lineWidth / 2.0f;

                    }

                }

                // initialize our Text Matrix or we could get trash in here
                var textMatrix = new CGAffineTransform (
                    1, 0, 0, -1, 0, ascent);

                if (format.LineAlignment == StringAlignment.Near)
                    textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y);
                if (format.LineAlignment == StringAlignment.Center)
                {
                    if (layoutAvailable)
                        textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y + ((insetBounds.Height / 2) - (baselineOffset / 2)));
                    else
                        textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y - ((insetBounds.Height / 2) - (baselineOffset / 2)));
                }

                if (format.LineAlignment == StringAlignment.Far)
                {
                    if (layoutAvailable)
                        textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y + ((insetBounds.Height) - (baselineOffset)));
                    else
                        textMatrix.Translate (penFlushness + textPosition.X, textPosition.Y - ((insetBounds.Height) - (baselineOffset)));
                }

                context.TextMatrix = textMatrix;

                // and draw the line
                line.Draw(context);

                // Move the index beyond the line break.
                start += count;
                textPosition.Y += (float)Math.Ceiling(ascent + descent + leading + 1); // +1 matches best to CTFramesetter's behavior
                line.Dispose();

            }

            // Now we call the brush with a fill of true so the brush can do the fill if need be
            // For LinearGradientBrush this will draw the Gradient and end the TransparentLayer.
            // See comments.
            brush.Setup(this, true); // Fill

            context.TextMatrix = saveMatrix;
            context.RestoreState();
        }
        internal void NativeDrawString(string s, Font font, Color brush, RectangleF layoutRectangle, StringFormat stringFormat)
        {
            if (font == null)
            {
                throw new ArgumentNullException("font");
            }

            if (s == null || s.Length == 0)
            {
                return;
            }

            var attributedString = buildAttributedString(s, font, brush);

            // Work out the geometry
            RectangleF insetBounds     = layoutRectangle;
            bool       layoutAvailable = true;

            if (insetBounds.Size == SizeF.Empty)
            {
                insetBounds.Size = new SizeF(8388608, 8388608);
                layoutAvailable  = false;
            }

            PointF textPosition = new PointF(insetBounds.X,
                                             insetBounds.Y);

            float boundsWidth = insetBounds.Width;

            // Calculate the lines
            int start  = 0;
            int length = (int)attributedString.Length;

            var typesetter = new CTTypesetter(attributedString);

            float baselineOffset = 0;

            // First we need to calculate the offset for Vertical Alignment if we
            // are using anything but Top
            if (layoutAvailable && stringFormat.LineAlignment != StringAlignment.Near)
            {
                while (start < length)
                {
                    int count = (int)typesetter.SuggestLineBreak(start, boundsWidth);
                    var line  = typesetter.GetLine(new NSRange(start, count));

                    // Create and initialize some values from the bounds.
                    nfloat ascent;
                    nfloat descent;
                    nfloat leading;
                    line.GetTypographicBounds(out ascent, out descent, out leading);
                    baselineOffset += (float)Math.Ceiling(ascent + descent + leading + 1);                      // +1 matches best to CTFramesetter's behavior
                    line.Dispose();
                    start += count;
                }
            }

            start = 0;

            while (start < length && textPosition.Y < insetBounds.Bottom)
            {
                // Now we ask the typesetter to break off a line for us.
                // This also will take into account line feeds embedded in the text.
                //  Example: "This is text \n with a line feed embedded inside it"
                int count = (int)typesetter.SuggestLineBreak(start, boundsWidth);
                var line  = typesetter.GetLine(new NSRange(start, count));

                // Create and initialize some values from the bounds.
                nfloat ascent;
                nfloat descent;
                nfloat leading;
                double lineWidth = line.GetTypographicBounds(out ascent, out descent, out leading);

                if (!layoutAvailable)
                {
                    insetBounds.Width  = (float)lineWidth;
                    insetBounds.Height = (float)(ascent + descent + leading);
                }

                // Calculate the string format if need be
                var penFlushness = 0.0f;

                if (stringFormat.Alignment == StringAlignment.Far)
                {
                    penFlushness = (float)line.GetPenOffsetForFlush(1.0f, insetBounds.Width);
                }
                else if (stringFormat.Alignment == StringAlignment.Center)
                {
                    penFlushness = (float)line.GetPenOffsetForFlush(0.5f, insetBounds.Width);
                }

                // initialize our Text Matrix or we could get trash in here
                var textMatrix = new CGAffineTransform(
                    1, 0, 0, -1, 0, ascent);

                if (stringFormat.LineAlignment == StringAlignment.Near)
                {
                    textMatrix.Translate(penFlushness + textPosition.X, textPosition.Y);                      //insetBounds.Height - textPosition.Y -(float)Math.Floor(ascent - 1));
                }
                if (stringFormat.LineAlignment == StringAlignment.Center)
                {
                    textMatrix.Translate(penFlushness + textPosition.X, textPosition.Y + ((insetBounds.Height / 2) - (baselineOffset / 2)));                        // -(float)Math.Floor(ascent)
                }
                if (stringFormat.LineAlignment == StringAlignment.Far)
                {
                    textMatrix.Translate(penFlushness + textPosition.X, textPosition.Y + ((insetBounds.Height) - (baselineOffset)));
                }

                var glyphRuns = line.GetGlyphRuns();

                for (int glyphRunIndex = 0; glyphRunIndex < glyphRuns.Length; glyphRunIndex++)
                {
                    var glyphRun       = glyphRuns [glyphRunIndex];
                    var glyphs         = glyphRun.GetGlyphs();
                    var glyphPositions = glyphRun.GetPositions();
                    //var textMatrix = glyphRun.TextMatrix;

                    // Create and initialize some values from the bounds.
                    float glyphAscent;
                    float glyphDescent;
                    float glyphLeading;

                    var elementPoints = new PointF [3];

                    for (int glyphIndex = 0; glyphIndex < glyphs.Length; glyphIndex++)
                    {
                        if (glyphIndex > 0)
                        {
                            textMatrix.x0 += glyphPositions [glyphIndex].X - glyphPositions[glyphIndex - 1].X;
                            textMatrix.y0 += glyphPositions [glyphIndex].Y - glyphPositions[glyphIndex - 1].Y;
                        }

                        var glyphPath = font.nativeFont.GetPathForGlyph(glyphs [glyphIndex]);

                        // glyphPath = null if it is a white space character
                        if (glyphPath != null)
                        {
                            glyphPath.Apply(
                                delegate(CGPathElement pathElement) {
                                elementPoints[0] = textMatrix.TransformPoint(pathElement.Point1).ToPointF();
                                elementPoints[1] = textMatrix.TransformPoint(pathElement.Point2).ToPointF();
                                elementPoints[2] = textMatrix.TransformPoint(pathElement.Point3).ToPointF();
                                //Console.WriteLine ("Applying {0} - {1}, {2}, {3}", pathElement.Type, elementPoints[0], elementPoints[1], elementPoints[2]);


                                // now add position offsets
                                switch (pathElement.Type)
                                {
                                case CGPathElementType.MoveToPoint:
                                    start_new_fig = true;
                                    Append(elementPoints[0].X, elementPoints[0].Y, PathPointType.Line, true);
                                    break;

                                case CGPathElementType.AddLineToPoint:
                                    var lastPoint = points[points.Count - 1];
                                    AppendPoint(lastPoint, PathPointType.Line, false);
                                    AppendPoint(elementPoints[0], PathPointType.Line, false);
                                    break;

                                case CGPathElementType.AddCurveToPoint:
                                case CGPathElementType.AddQuadCurveToPoint:
                                    //  This is the only thing I can think of right now for the fonts that
                                    //  I have tested.  See the description of the quadraticToCubic method for
                                    //  more information

                                    // Get the last point
                                    var pt1 = points[points.Count - 1];
                                    var pt2 = PointF.Empty;
                                    var pt3 = PointF.Empty;
                                    var pt4 = elementPoints[1];
                                    GeomUtilities.QuadraticToCubic(pt1, elementPoints[0], elementPoints[1], out pt2, out pt3);
                                    Append(pt1.X, pt1.Y, PathPointType.Line, true);
                                    AppendBezier(pt2.X, pt2.Y, pt3.X, pt3.Y, pt4.X, pt4.Y);
                                    break;

                                case CGPathElementType.CloseSubpath:
                                    CloseFigure();
                                    break;
                                }
                            }

                                );
                        }
                    }
                }

                // Move the index beyond the line break.
                start          += count;
                textPosition.Y += (float)Math.Ceiling(ascent + descent + leading + 1);                 // +1 matches best to CTFramesetter's behavior
                line.Dispose();
            }
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Gets the rotate image.
        /// </summary>
        /// <param name="imagePath">The image path.</param>
        /// <returns>UIImage.</returns>
        private static UIImage GetRotateImage(String imagePath)
        {
            UIImage image  = UIImage.FromFile(imagePath);
            CGImage imgRef = image.CGImage;

            float width  = imgRef.Width;
            float height = imgRef.Height;

            CGAffineTransform  transform = CGAffineTransform.MakeIdentity();
            RectangleF         bounds    = new RectangleF(0, 0, 200, 200);
            SizeF              imageSize = new SizeF(200, 200);
            float              boundHeight;
            UIImageOrientation orient = image.Orientation;

            switch (orient)
            {
            case UIImageOrientation.Up:
                transform = CGAffineTransform.MakeIdentity();
                break;

            case UIImageOrientation.UpMirrored:
                transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0.0f);
                transform.Scale(-1.0f, 1.0f);
                break;

            case UIImageOrientation.Down:
                transform.Rotate((float)Math.PI);
                transform.Translate(imageSize.Width, imageSize.Height);
                break;

            case UIImageOrientation.DownMirrored:
                transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Height);
                transform.Scale(1.0f, -1.0f);
                break;

            case UIImageOrientation.LeftMirrored:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform.Scale(-1.0f, 1.0f);
                transform.Rotate((float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Left:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeRotation((float)Math.PI / 2.0f);
                transform.Translate(imageSize.Height, 0.0f);
                break;

            case UIImageOrientation.RightMirrored:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                transform.Scale(-1.0f, 1.0f);
                transform.Rotate(3.0f * (float)Math.PI / 2.0f);
                break;

            case UIImageOrientation.Right:
                boundHeight   = bounds.Size.Height;
                bounds.Height = bounds.Size.Width;
                bounds.Width  = boundHeight;
                transform     = CGAffineTransform.MakeRotation(-(float)Math.PI / 2.0f);
                transform.Translate(0.0f, imageSize.Width);
                break;
            }

            UIGraphics.BeginImageContext(bounds.Size);

            CGContext context = UIGraphics.GetCurrentContext();

            context.ConcatCTM(transform);

            context = UIGraphics.GetCurrentContext();
            context.DrawImage(new RectangleF(0, 0, width, height), imgRef);
            UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();

            UIGraphics.EndImageContext();

            return(imageCopy);
        }
Ejemplo n.º 33
0
        private CGAffineTransform NodeToParentTransform()
        {
            if (_isTransformDirty) {
                _transform = CGAffineTransform.MakeIdentity();

                if (!IsRelativeAnchorPoint) {
                    _transform.Translate(TransformAnchor.X, TransformAnchor.Y);
                }

                _transform.Translate((int)Position.X, (int)Position.Y);
                _transform.Rotate(Rotation.ToRadians());
                _transform.Scale(ScaleX, ScaleY);
                _transform.Translate(-TransformAnchor.X, -TransformAnchor.Y);

                _isTransformDirty = false;
            }

            return _transform;
        }