From 5c0bb83f84d048cf7abb0222b3f248b4946f039c Mon Sep 17 00:00:00 2001 From: Bryan Phelps Date: Thu, 13 May 2021 18:58:53 -0700 Subject: [PATCH] Add blur filter --- packages/reason-skia/examples/skia-cli/SkiaCli.re | 15 +++++++++++++-- packages/reason-skia/src/Skia.re | 14 ++++++++++++++ packages/reason-skia/src/Skia.rei | 5 +++++ .../src/wrapped/bindings/SkiaWrappedBindings.re | 12 ++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/reason-skia/examples/skia-cli/SkiaCli.re b/packages/reason-skia/examples/skia-cli/SkiaCli.re index d437d9435..9bc9f5ec9 100644 --- a/packages/reason-skia/examples/skia-cli/SkiaCli.re +++ b/packages/reason-skia/examples/skia-cli/SkiaCli.re @@ -80,7 +80,7 @@ let draw = canvas => { let fill3 = Paint.make(); Paint.setColor(fill3, Color.makeArgb(0xFFl, 0xFFl, 0xFFl, 0xFFl)); - Paint.setTextSize(fill3, 30.); + Paint.setTextSize(fill3, 20.); let nonExistentTypeface = Typeface.makeFromFile("non-existent-font.ttf", 0); assert(nonExistentTypeface == None); @@ -97,7 +97,18 @@ let draw = canvas => { print_endline(__LOC__ ++ ": we will set."); Paint.setTypeface(fill3, typeFace); print_endline(__LOC__ ++ ": setTypeface is OK."); - Canvas.drawText(canvas, "Hello, world!", 30., 30., fill3); + Paint.setImageFilter( + fill3, + Some(ImageFilter.Blur.make(3., 3., None, None)), + ); + let spreadX = 3.0; + let spreadY = 3.0; + Canvas.drawText(canvas, "Hello, world!", 150. -. spreadX, 50., fill3); + Canvas.drawText(canvas, "Hello, world!", 150. +. spreadX, 50., fill3); + Canvas.drawText(canvas, "Hello, world!", 150., 50. -. spreadY, fill3); + Canvas.drawText(canvas, "Hello, world!", 150., 50. +. spreadY, fill3); + Paint.setImageFilter(fill3, None); + Canvas.drawText(canvas, "Hello, world!", 150., 50., fill3); let metrics = FontMetrics.make(); let _ret: float = Paint.getFontMetrics(fill3, metrics, 1.0); diff --git a/packages/reason-skia/src/Skia.re b/packages/reason-skia/src/Skia.re index 276319078..608a54787 100644 --- a/packages/reason-skia/src/Skia.re +++ b/packages/reason-skia/src/Skia.re @@ -121,6 +121,20 @@ module ImageFilter = { }; }; + module Blur = { + let make = (~sigmaX, ~sigmaY, inputOption, cropRectOption) => { + let blurFilter = + SkiaWrapped.ImageFilter.Blur.allocate( + sigmaX, + sigmaY, + inputOption, + cropRectOption, + ); + Gc.finalise(SkiaWrapped.ImageFilter.delete, blurFilter); + blurFilter; + }; + }; + let makeDropShadow = DropShadow.make; }; diff --git a/packages/reason-skia/src/Skia.rei b/packages/reason-skia/src/Skia.rei index 37f7be127..71674596e 100644 --- a/packages/reason-skia/src/Skia.rei +++ b/packages/reason-skia/src/Skia.rei @@ -110,6 +110,11 @@ module ImageFilter: { t; }; + module Blur: { + let make: + (~sigmaX: float, ~sigmaY: float, option(t), option(CropRect.t)) => t; + }; + let makeDropShadow: ( float, diff --git a/packages/reason-skia/src/wrapped/bindings/SkiaWrappedBindings.re b/packages/reason-skia/src/wrapped/bindings/SkiaWrappedBindings.re index ceb04d4be..d7aff73ee 100644 --- a/packages/reason-skia/src/wrapped/bindings/SkiaWrappedBindings.re +++ b/packages/reason-skia/src/wrapped/bindings/SkiaWrappedBindings.re @@ -230,6 +230,18 @@ module M = (F: FOREIGN) => { @-> returning(t), ); }; + + module Blur = { + let allocate = + foreign( + "sk_imagefilter_new_blur", + float + @-> float + @-> ptr_opt(SkiaTypes.ImageFilter.t) + @-> ptr_opt(SkiaTypes.ImageFilter.CropRect.t) + @-> returning(t), + ); + }; }; module Point = {