DrawText() public method

public DrawText ( IntPtr buffer, int length, SKPath path, float hOffset, float vOffset, SKPaint paint ) : void
buffer System.IntPtr
length int
path SKPath
hOffset float
vOffset float
paint SKPaint
return void
		public static void UnicodeText (SKCanvas canvas, int width, int height)
		{
			// stops at 0x530 on Android
			string text = "\u03A3 and \u0750";

			using (var paint = new SKPaint ())
			using (var tf = SKTypeface.FromFamilyName ("Tahoma")) {
				canvas.DrawColor (SKColors.White);

				paint.IsAntialias = true;
				paint.TextSize = 60;
				paint.Typeface = tf;
				canvas.DrawText (text, 50, 100, paint);
			}


			using (var paint = new SKPaint ())
			using (var tf = SKTypeface.FromFamilyName ("Times New Roman")) {
				paint.Color = XamDkBlue;

				paint.IsAntialias = true;
				paint.TextSize = 60;
				paint.Typeface = tf;
				canvas.DrawText (text, 50, 200, paint);
			}
		}
Beispiel #2
1
		public static void MeasureTextSample (SKCanvas canvas, int width, int height)
		{
			canvas.DrawColor (SKColors.White);

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x42, 0x81, 0xA4);
				paint.TextEncoding = SKTextEncoding.Utf32;

				canvas.DrawText ("Skia (UTF-32)", 0, 64.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-32)", ref bounds);
				bounds.Top += 64.0f;
				bounds.Bottom += 64.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.TextEncoding = SKTextEncoding.Utf16;

				canvas.DrawText ("Skia (UTF-16)", 0, 144.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-16)", ref bounds);
				bounds.Top += 144.0f;
				bounds.Bottom += 144.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0xE6, 0xB8, 0x9C);
				paint.TextEncoding = SKTextEncoding.Utf8;

				canvas.DrawText ("Skia (UTF-8)", 0, 224.0f, paint);

				var bounds = new SKRect();
				paint.MeasureText ("Skia (UTF-8)", ref bounds);
				bounds.Top += 224.0f;
				bounds.Bottom += 224.0f;

				paint.IsStroke = true;
				paint.Color = SKColors.Red;

				canvas.DrawRect (bounds, paint);
			}
		}
Beispiel #3
0
        public void Paint(SkiaSharp.SKCanvas canvas, double x, double y)
        {
            var paint = new SKPaint
            {
                Color       = SKColors.Black,
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                TextAlign   = SKTextAlign.Center,
                TextSize    = 24
            };

            canvas.DrawText(this.Text, (float)x, (float)y, paint);
        }
Beispiel #4
0
        private static SKBitmap CreateLabelAsBitmap(LabelStyle style, string text, SKPaint paint)
        {
            var rect = new SKRect();
            paint.MeasureText(text, ref rect);

            var backRect = new SKRect(0, 0, rect.Width + 6, rect.Height + 6);

            var bitmap = new SKBitmap((int)backRect.Width, (int)backRect.Height);

            using (var target = new SKCanvas(bitmap))
            {
                target.Clear();

                DrawBackground(style, backRect, target);
                target.DrawText(text, -rect.Left + 3, -rect.Top +3, paint);
                return bitmap;
            }
        }
Beispiel #5
0
        private static void DrawLabel(SKCanvas target, float x, float y, LabelStyle style, string text)
        {
            var paint = CreatePaint(style);

            var rect = new SKRect();

            paint.MeasureText(text, ref rect);

            var align = CalcAlignFactor(style.VerticalAlignment);

            rect.Offset(
                x - rect.Width*0.5f + (float) style.Offset.X,
                y + rect.Height*0.5f + (float) style.Offset.Y);

            var backRect = rect;
            backRect.Inflate(3, 3);
            DrawBackground(style, backRect, target);

            target.DrawText(text, rect.Left, rect.Bottom, paint);
        }
Beispiel #6
0
        public static void Xfermode(SKCanvas canvas, int width, int height)
        {
            var modes = Enum.GetValues(typeof(SKXferMode)).Cast <SKXferMode> ().ToArray();

            var cols = width < height ? 3 : 5;
            var rows = (modes.Length - 1) / cols + 1;

            var w         = (float)width / cols;
            var h         = (float)height / rows;
            var rect      = SKRect.Create(w, h);
            var srcPoints = new[] {
                new SKPoint(0.0f, 0.0f),
                new SKPoint(w, 0.0f)
            };
            var srcColors = new [] {
                SKColors.Magenta.WithAlpha(0),
                SKColors.Magenta
            };
            var dstPoints = new [] {
                new SKPoint(0.0f, 0.0f),
                new SKPoint(0.0f, h)
            };
            var dstColors = new [] {
                SKColors.Cyan.WithAlpha(0),
                SKColors.Cyan
            };

            using (var text = new SKPaint())
                using (var stroke = new SKPaint())
                    using (var src = new SKPaint())
                        using (var dst = new SKPaint())
                            using (var srcShader = SKShader.CreateLinearGradient(srcPoints [0], srcPoints [1], srcColors, null, SKShaderTileMode.Clamp))
                                using (var dstShader = SKShader.CreateLinearGradient(dstPoints [0], dstPoints [1], dstColors, null, SKShaderTileMode.Clamp)) {
                                    text.TextSize    = 12.0f;
                                    text.IsAntialias = true;
                                    text.TextAlign   = SKTextAlign.Center;
                                    stroke.IsStroke  = true;
                                    src.Shader       = srcShader;
                                    dst.Shader       = dstShader;

                                    canvas.Clear(SKColors.White);

                                    for (var i = 0; i < modes.Length; ++i)
                                    {
                                        using (new SKAutoCanvasRestore(canvas, true)) {
                                            canvas.Translate(w * (i / rows), h * (i % rows));

                                            canvas.ClipRect(rect);
                                            canvas.DrawColor(SKColors.LightGray);

                                            canvas.SaveLayer(null);
                                            canvas.Clear(SKColors.Transparent);
                                            canvas.DrawPaint(dst);

                                            src.XferMode = modes [i];
                                            canvas.DrawPaint(src);
                                            canvas.DrawRect(rect, stroke);

                                            var desc = modes [i].ToString();
                                            canvas.DrawText(desc, w / 2f, h / 2f, text);
                                        }
                                    }
                                }
        }
		public static void EmbossMaskFilter(SKCanvas canvas, int width, int height)
		{
			canvas.DrawColor(SKColors.White);

			SKPoint3 direction = new SKPoint3(1.0f, 1.0f, 1.0f);

			using (var paint = new SKPaint())
			using (var filter = SKMaskFilter.CreateEmboss(2.0f, direction, 0.3f, 0.1f))
			{
				paint.IsAntialias = true;
				paint.TextSize = 120;
				paint.TextAlign = SKTextAlign.Center;
				paint.MaskFilter = filter;

				canvas.DrawText("Skia", width / 2f, height / 2f, paint);
			}
		}
		public static void BlurMaskFilter(SKCanvas canvas, int width, int height)
		{
			canvas.DrawColor(SKColors.White);

			using (var paint = new SKPaint())
			using (var filter = SKMaskFilter.CreateBlur(SKBlurStyle.Normal, 5.0f))
			{
				paint.IsAntialias = true;
				paint.TextSize = 120;
				paint.TextAlign = SKTextAlign.Center;
				paint.MaskFilter = filter;

				canvas.DrawText("Skia", width / 2f, height / 2f, paint);
			}
		}
		public static void BitmapDecoder(SKCanvas canvas, int width, int height)
		{
			var assembly = typeof(Demos).GetTypeInfo().Assembly;
			var imageName = assembly.GetName().Name + ".color-wheel.png";

			canvas.Clear(SKColors.White);

			// load the embedded resource stream
			using (var resource = assembly.GetManifestResourceStream(imageName))
			using (var stream = new SKManagedStream(resource))
			using (var decoder = new SKImageDecoder(stream))
			using (var paint = new SKPaint())
			using (var tf = SKTypeface.FromFamilyName("Arial"))
			{
				paint.IsAntialias = true;
				paint.TextSize = 14;
				paint.Typeface = tf;
				paint.Color = SKColors.Black;

				// read / set decoder settings
				decoder.DitherImage = true;
				decoder.PreferQualityOverSpeed = true;
				decoder.SampleSize = 2;

				// decode the image
				using (var bitmap = new SKBitmap())
				{
					var result = decoder.Decode(stream, bitmap);
					if (result != SKImageDecoderResult.Failure)
					{
						var info = bitmap.Info;
						var x = 25;
						var y = 25;

						canvas.DrawBitmap(bitmap, x, y);
						x += info.Width + 25;
						y += 14;

						canvas.DrawText(string.Format("Result: {0}", result), x, y, paint);
						y += 20;

						canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint);
						y += 20;

						canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint);
					}
				}
			}
		}
Beispiel #10
0
 public void DrawText(string text, float x, float y, PaintBrush paint)
 => _canvas.DrawText(text, x, y, paint.ToSkia());
		public static void CustomFonts (SKCanvas canvas, int width, int height)
		{
			string text = "\u03A3 and \u0750";

			using (var paint = new SKPaint ()) {
				canvas.Clear (SKColors.White);
				paint.IsAntialias = true;

				using (var tf = SKTypeface.FromFile (CustomFontPath)) {
					paint.Color = XamGreen;
					paint.TextSize = 40;
					paint.Typeface = tf;

					canvas.DrawText (text, 50, 50, paint);
				}

				using (var stream = new SKFileStream (CustomFontPath))
				using (var tf = SKTypeface.FromStream (stream)) {
					paint.Color = XamDkBlue;
					paint.TextSize = 40;
					paint.Typeface = tf;

					canvas.DrawText (text, 50, 100, paint);
				}

				var assembly = typeof(Demos).GetTypeInfo ().Assembly;
				var fontName = assembly.GetName ().Name + ".embedded-font.ttf";

				using (var resource = assembly.GetManifestResourceStream (fontName))
				using (var memory = new MemoryStream ()) {
					resource.CopyTo (memory);
					var bytes = memory.ToArray ();
					using (var stream = new SKMemoryStream (bytes))
					using (var tf = SKTypeface.FromStream (stream)) {
						paint.Color = XamLtBlue;
						paint.TextSize = 40;
						paint.Typeface = tf;

						canvas.DrawText (text, 50, 150, paint);
					}
				}

				using (var resource = assembly.GetManifestResourceStream (fontName))
				using (var stream = new SKManagedStream (resource))
				using (var tf = SKTypeface.FromStream (stream)) {
					paint.Color = XamPurple;
					paint.TextSize = 40;
					paint.Typeface = tf;

					canvas.DrawText (text, 50, 200, paint);
				}
			}
		}
		// https://fiddle.skia.org/c/3523e95a9f8b96228b6b4bdd5409cb94
		public static void TextSample (SKCanvas canvas, int width, int height)
		{
			canvas.DrawColor (SKColors.White);

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x42, 0x81, 0xA4);
				paint.IsStroke = false;

				canvas.DrawText ("Skia", width / 2f, 64.0f, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.IsStroke = true;
				paint.StrokeWidth = 3;
				paint.TextAlign = SKTextAlign.Center;

				canvas.DrawText ("Skia", width / 2f, 144.0f, paint);
			}

			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0xE6, 0xB8, 0x9C);
				paint.TextScaleX = 1.5f;
				paint.TextAlign = SKTextAlign.Right;

				canvas.DrawText ("Skia", width / 2f, 224.0f, paint);
			}
		}
Beispiel #13
0
		public static void CreatePdfSample (SKCanvas canvas, int width, int height)
		{
			canvas.Clear(SKColors.White);

			using (var paint = new SKPaint ()) {
				paint.TextSize = 24.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.StrokeWidth = 3;
				paint.TextAlign = SKTextAlign.Center;

				canvas.DrawText ("tap to open", width / 2f, 100f, paint);
			}

			using (var stream = new SKFileWStream (Path.Combine (WorkingDirectory, "document.pdf")))
			using (var document = SKDocument.CreatePdf (stream))
			using (var pdfCanvas = document.BeginPage (width, height))
			using (var paint = new SKPaint ()) {
				paint.TextSize = 64.0f;
				paint.IsAntialias = true;
				paint.Color = new SKColor (0x9C, 0xAF, 0xB7);
				paint.IsStroke = true;
				paint.StrokeWidth = 3;
				paint.TextAlign = SKTextAlign.Center;

				pdfCanvas.DrawText ("...PDF...", width / 2f, 100f, paint);
				document.EndPage ();
				document.Close ();
			}
		}
Beispiel #14
0
		public static void BitmapDecoder(SKCanvas canvas, int width, int height)
		{
			var assembly = typeof(Demos).GetTypeInfo().Assembly;
			var imageName = assembly.GetName().Name + ".color-wheel.png";

			canvas.Clear(SKColors.White);

			// load the embedded resource stream
			using (var resource = assembly.GetManifestResourceStream(imageName))
			using (var stream = new SKManagedStream(resource))
			using (var codec = SKCodec.Create(stream))
			using (var paint = new SKPaint())
			using (var tf = SKTypeface.FromFamilyName("Arial"))
			{
				var info = codec.Info;

				paint.IsAntialias = true;
				paint.TextSize = 14;
				paint.Typeface = tf;
				paint.Color = SKColors.Black;

				// decode the image
				using (var bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul))
				{
					IntPtr length;
					var result = codec.GetPixels(bitmap.Info, bitmap.GetPixels(out length));
					if (result == SKCodecResult.Success || result == SKCodecResult.IncompleteInput)
					{
						var x = 25;
						var y = 25;

						canvas.DrawBitmap(bitmap, x, y);
						x += bitmap.Info.Width + 25;
						y += 14;

						canvas.DrawText(string.Format("Result: {0}", result), x, y, paint);
						y += 20;

						canvas.DrawText(string.Format("Size: {0}px x {1}px", bitmap.Width, bitmap.Height), x, y, paint);
						y += 20;

						canvas.DrawText(string.Format("Pixels: {0} @ {1}b/px", bitmap.Pixels.Length, bitmap.BytesPerPixel), x, y, paint);
					}
				}
			}
		}
Beispiel #15
0
		public static void BitmapLattice (SKCanvas canvas, int width, int height)
		{
			canvas.Clear (SKColors.White);

			var assembly = typeof (Demos).GetTypeInfo ().Assembly;
			var imageName = assembly.GetName ().Name + ".nine-patch.png";

			// load the image from the embedded resource stream
			using (var resource = assembly.GetManifestResourceStream (imageName))
			using (var stream = new SKManagedStream (resource))
			using (var bitmap = SKBitmap.Decode (stream))
			{
				var patchCenter = new SKRectI (33, 33, 256 - 33, 256 - 33);

				// 2x3 for portrait, or 3x2 for landscape
				var land = width > height;
				var min = land ? Math.Min (width / 3f, height / 2f) : Math.Min (width / 2f, height / 3f);
				var wide = SKRect.Inflate (SKRect.Create (0, land ? min : (min * 2f), min * 2f, min), -6, -6);
				var tall = SKRect.Inflate (SKRect.Create (land ? (min * 2f) : min, 0, min, min * 2f), -6, -6);
				var square = SKRect.Inflate (SKRect.Create (0, 0, min, min), -6, -6);
				var text = SKRect.Create (land ? min : 0, land ? 0 : min, min, min / 5f);
				text.Offset (text.Width / 2f, text.Height * 1.5f);
				text.Right = text.Left;

				// draw the bitmaps
				canvas.DrawBitmapNinePatch (bitmap, patchCenter, square);
				canvas.DrawBitmapNinePatch (bitmap, patchCenter, tall);
				canvas.DrawBitmapNinePatch (bitmap, patchCenter, wide);

				// describe what we see
				using (var paint = new SKPaint ())
				{
					paint.TextAlign = SKTextAlign.Center;
					paint.TextSize = text.Height * 0.75f;

					canvas.DrawText ("The corners", text.Left, text.Top, paint);
					text.Offset (0, text.Height);
					canvas.DrawText ("should always", text.Left, text.Top, paint);
					text.Offset (0, text.Height);
					canvas.DrawText ("be square", text.Left, text.Top, paint);
				}
			}
		}
 public void DrawText(string text, float x, float y, PaintBrush paint)
 => _canvas.DrawText(text, x, y, GetSKPaint(paint));
        internal void Draw(DrawingContextImpl context,
                           SKCanvas canvas, SKPoint origin,
                           DrawingContextImpl.PaintWrapper foreground)
        {
            /* TODO: This originated from Native code, it might be useful for debugging character positions as
             * we improve the FormattedText support. Will need to port this to C# obviously. Rmove when
             * not needed anymore.

                SkPaint dpaint;
                ctx->Canvas->save();
                ctx->Canvas->translate(origin.fX, origin.fY);
                for (int c = 0; c < Lines.size(); c++)
                {
                    dpaint.setARGB(255, 0, 0, 0);
                    SkRect rc;
                    rc.fLeft = 0;
                    rc.fTop = Lines[c].Top;
                    rc.fRight = Lines[c].Width;
                    rc.fBottom = rc.fTop + LineOffset;
                    ctx->Canvas->drawRect(rc, dpaint);
                }
                for (int c = 0; c < Length; c++)
                {
                    dpaint.setARGB(255, c % 10 * 125 / 10 + 125, (c * 7) % 10 * 250 / 10, (c * 13) % 10 * 250 / 10);
                    dpaint.setStyle(SkPaint::kFill_Style);
                    ctx->Canvas->drawRect(Rects[c], dpaint);
                }
                ctx->Canvas->restore();
            */
            SKPaint paint = _paint;
            IDisposable currd = null;
            var currentWrapper = foreground;

            try
            {
                SKPaint currFGPaint = ApplyWrapperTo(ref foreground, ref currd, paint);
                bool hasCusomFGBrushes = _foregroundBrushes.Any();

                for (int c = 0; c < _skiaLines.Count; c++)
                {
                    AvaloniaFormattedTextLine line = _skiaLines[c];

                    float x = TransformX(origin.X, 0, paint.TextAlign);

                    if (!hasCusomFGBrushes)
                    {
                        var subString = _text.Substring(line.Start, line.Length);
                        canvas.DrawText(subString, x, origin.Y + line.Top + _lineOffset, paint);
                    }
                    else
                    {
                        float currX = x;
                        string subStr;
                        int len;

                        for (int i = line.Start; i < line.Start + line.Length;)
                        {
                            var fb = GetNextForegroundBrush(ref line, i, out len);

                            if (fb != null)
                            {
                                //TODO: figure out how to get the brush size
                                currentWrapper = context.CreatePaint(fb, new Size());
                            }
                            else
                            {
                                if (!currentWrapper.Equals(foreground)) currentWrapper.Dispose();
                                currentWrapper = foreground;
                            }

                            subStr = _text.Substring(i, len);

                            if (currFGPaint != currentWrapper.Paint)
                            {
                                currFGPaint = ApplyWrapperTo(ref currentWrapper, ref currd, paint);
                            }

                            canvas.DrawText(subStr, currX, origin.Y + line.Top + _lineOffset, paint);

                            i += len;
                            currX += paint.MeasureText(subStr);
                        }
                    }
                }
            }
            finally
            {
                if (!currentWrapper.Equals(foreground)) currentWrapper.Dispose();
                currd?.Dispose();
            }
        }
		public static void Xfermode (SKCanvas canvas, int width, int height)
		{
			var modes = Enum.GetValues (typeof(SKXferMode)).Cast<SKXferMode> ().ToArray ();

			var cols = width < height ? 3 : 5;
			var rows = (modes.Length - 1) / cols + 1;

			var w = (float)width / cols;
			var h = (float)height / rows;
			var rect = SKRect.Create (w, h);
			var srcPoints = new[] {
				new SKPoint (0.0f, 0.0f),
				new SKPoint (w, 0.0f)
			};
			var srcColors = new [] {
				SKColors.Magenta.WithAlpha (0),
				SKColors.Magenta
			};
			var dstPoints = new [] {
				new SKPoint (0.0f, 0.0f),
				new SKPoint (0.0f, h)
			};
			var dstColors = new [] {
				SKColors.Cyan.WithAlpha (0),
				SKColors.Cyan
			};

			using (var text = new SKPaint ())
			using (var stroke = new SKPaint ())
			using (var src = new SKPaint ())
			using (var dst = new SKPaint ())
			using (var srcShader = SKShader.CreateLinearGradient (srcPoints [0], srcPoints [1], srcColors, null, SKShaderTileMode.Clamp))
			using (var dstShader = SKShader.CreateLinearGradient (dstPoints [0], dstPoints [1], dstColors, null, SKShaderTileMode.Clamp)) {
				text.TextSize = 12.0f;
				text.IsAntialias = true;
				text.TextAlign = SKTextAlign.Center;
				stroke.IsStroke = true;
				src.Shader = srcShader;
				dst.Shader = dstShader;

				canvas.Clear (SKColors.White);

				for (var i = 0; i < modes.Length; ++i) {
					using (new SKAutoCanvasRestore (canvas, true)) {
						canvas.Translate (w * (i / rows), h * (i % rows));

						canvas.ClipRect (rect);
						canvas.DrawColor (SKColors.LightGray);

						canvas.SaveLayer (null);
						canvas.Clear (SKColors.Transparent);
						canvas.DrawPaint (dst);

						src.XferMode = modes [i];
						canvas.DrawPaint (src);
						canvas.DrawRect (rect, stroke);

						var desc = modes [i].ToString ();
						canvas.DrawText (desc, w / 2f, h / 2f, text);
					}
				}
			}
		}