Skip to content

Commit 91ac582

Browse files
committedMar 21, 2025
feat: shaders (incomplete)
1 parent 7b9fe20 commit 91ac582

15 files changed

+791
-133
lines changed
 

‎NAMESPACE

+8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ S3method("$",FillType__bundle)
66
S3method("$",Join__bundle)
77
S3method("$",PointMode__bundle)
88
S3method("$",Style__bundle)
9+
S3method("$",TileMode__bundle)
910
S3method("$<-",savvy_skiagd__sealed)
1011
S3method("[[",BlendMode__bundle)
1112
S3method("[[",Cap__bundle)
1213
S3method("[[",FillType__bundle)
1314
S3method("[[",Join__bundle)
1415
S3method("[[",PointMode__bundle)
1516
S3method("[[",Style__bundle)
17+
S3method("[[",TileMode__bundle)
1618
S3method("[[<-",savvy_skiagd__sealed)
1719
S3method(print,BlendMode)
1820
S3method(print,BlendMode__bundle)
@@ -27,15 +29,21 @@ S3method(print,PathEffect)
2729
S3method(print,PathEffect__bundle)
2830
S3method(print,PointMode)
2931
S3method(print,PointMode__bundle)
32+
S3method(print,Shader)
33+
S3method(print,Shader__bundle)
3034
S3method(print,Style)
3135
S3method(print,Style__bundle)
36+
S3method(print,TileMode)
37+
S3method(print,TileMode__bundle)
3238
export(BlendMode)
3339
export(Cap)
3440
export(FillType)
3541
export(Join)
3642
export(PathEffect)
3743
export(PointMode)
44+
export(Shader)
3845
export(Style)
46+
export(TileMode)
3947
export(add_circle)
4048
export(add_diff_rect)
4149
export(add_line)

‎R/000-wrappers.R

+155-2
Original file line numberDiff line numberDiff line change
@@ -579,19 +579,21 @@ class(`Join`) <- c("Join__bundle", "savvy_skiagd__sealed")
579579
#' * miter: Stroke miter.
580580
#' * blend_mode: BlendMode.
581581
#' * path_effect: PathEffect.
582+
#' * shader: Shader.
582583
#'
583584
#' @noRd
584585
`PaintAttrs` <- new.env(parent = emptyenv())
585586

586587
### associated functions for PaintAttrs
587588

588-
`PaintAttrs`$`set_attrs` <- function(`color`, `style`, `join`, `cap`, `width`, `miter`, `blend_mode`, `path_effect`) {
589+
`PaintAttrs`$`set_attrs` <- function(`color`, `style`, `join`, `cap`, `width`, `miter`, `blend_mode`, `path_effect`, `shader`) {
589590
`style` <- .savvy_extract_ptr(`style`, "Style")
590591
`join` <- .savvy_extract_ptr(`join`, "Join")
591592
`cap` <- .savvy_extract_ptr(`cap`, "Cap")
592593
`blend_mode` <- .savvy_extract_ptr(`blend_mode`, "BlendMode")
593594
`path_effect` <- .savvy_extract_ptr(`path_effect`, "PathEffect")
594-
.savvy_wrap_PaintAttrs(.Call(savvy_PaintAttrs_set_attrs__impl, `color`, `style`, `join`, `cap`, `width`, `miter`, `blend_mode`, `path_effect`))
595+
`shader` <- .savvy_extract_ptr(`shader`, "Shader")
596+
.savvy_wrap_PaintAttrs(.Call(savvy_PaintAttrs_set_attrs__impl, `color`, `style`, `join`, `cap`, `width`, `miter`, `blend_mode`, `path_effect`, `shader`))
595597
}
596598

597599

@@ -762,6 +764,91 @@ class(`PointMode`) <- c("PointMode__bundle", "savvy_skiagd__sealed")
762764
cat('PointMode\n')
763765
}
764766

767+
### wrapper functions for Shader
768+
769+
`Shader_get_label` <- function(self) {
770+
function() {
771+
.Call(savvy_Shader_get_label__impl, `self`)
772+
}
773+
}
774+
775+
`.savvy_wrap_Shader` <- function(ptr) {
776+
e <- new.env(parent = emptyenv())
777+
e$.ptr <- ptr
778+
e$`get_label` <- `Shader_get_label`(ptr)
779+
780+
class(e) <- c("Shader", "savvy_skiagd__sealed")
781+
e
782+
}
783+
784+
785+
#' Shader
786+
#'
787+
#' @seealso
788+
#' * [Gradients | React Native Skia](https://shopify.github.io/react-native-skia/docs/shaders/gradients)
789+
#' * [Perlin Noise Shaders | React Native Skia](https://shopify.github.io/react-native-skia/docs/shaders/perlin-noise)
790+
#' * [Blending and Colors | React Native Skia](https://shopify.github.io/react-native-skia/docs/shaders/colors)
791+
#' @export
792+
`Shader` <- new.env(parent = emptyenv())
793+
794+
### associated functions for Shader
795+
796+
`Shader`$`blend` <- function(`mode`, `dst`, `src`) {
797+
`mode` <- .savvy_extract_ptr(`mode`, "BlendMode")
798+
`dst` <- .savvy_extract_ptr(`dst`, "Shader")
799+
`src` <- .savvy_extract_ptr(`src`, "Shader")
800+
.savvy_wrap_Shader(.Call(savvy_Shader_blend__impl, `mode`, `dst`, `src`))
801+
}
802+
803+
`Shader`$`color` <- function(`color`) {
804+
.savvy_wrap_Shader(.Call(savvy_Shader_color__impl, `color`))
805+
}
806+
807+
`Shader`$`conical_gradient` <- function(`start`, `end`, `radii`, `from`, `to`, `mode`, `flags`, `mat`) {
808+
`mode` <- .savvy_extract_ptr(`mode`, "TileMode")
809+
.savvy_wrap_Shader(.Call(savvy_Shader_conical_gradient__impl, `start`, `end`, `radii`, `from`, `to`, `mode`, `flags`, `mat`))
810+
}
811+
812+
`Shader`$`fractal_noise` <- function(`freq`, `octaves`, `seed`, `tile_size`) {
813+
.savvy_wrap_Shader(.Call(savvy_Shader_fractal_noise__impl, `freq`, `octaves`, `seed`, `tile_size`))
814+
}
815+
816+
`Shader`$`from_png` <- function(`png_bytes`, `mode`, `mat`) {
817+
`mode` <- .savvy_extract_ptr(`mode`, "TileMode")
818+
.savvy_wrap_Shader(.Call(savvy_Shader_from_png__impl, `png_bytes`, `mode`, `mat`))
819+
}
820+
821+
`Shader`$`linear_gradient` <- function(`start`, `end`, `from`, `to`, `mode`, `flags`, `mat`) {
822+
`mode` <- .savvy_extract_ptr(`mode`, "TileMode")
823+
.savvy_wrap_Shader(.Call(savvy_Shader_linear_gradient__impl, `start`, `end`, `from`, `to`, `mode`, `flags`, `mat`))
824+
}
825+
826+
`Shader`$`no_shader` <- function() {
827+
.savvy_wrap_Shader(.Call(savvy_Shader_no_shader__impl))
828+
}
829+
830+
`Shader`$`radial_gradient` <- function(`center`, `radius`, `from`, `to`, `mode`, `flags`, `mat`) {
831+
`mode` <- .savvy_extract_ptr(`mode`, "TileMode")
832+
.savvy_wrap_Shader(.Call(savvy_Shader_radial_gradient__impl, `center`, `radius`, `from`, `to`, `mode`, `flags`, `mat`))
833+
}
834+
835+
`Shader`$`sweep_gradient` <- function(`center`, `start`, `end`, `from`, `to`, `mode`, `flags`, `mat`) {
836+
`mode` <- .savvy_extract_ptr(`mode`, "TileMode")
837+
.savvy_wrap_Shader(.Call(savvy_Shader_sweep_gradient__impl, `center`, `start`, `end`, `from`, `to`, `mode`, `flags`, `mat`))
838+
}
839+
840+
`Shader`$`turbulence` <- function(`freq`, `octaves`, `seed`, `tile_size`) {
841+
.savvy_wrap_Shader(.Call(savvy_Shader_turbulence__impl, `freq`, `octaves`, `seed`, `tile_size`))
842+
}
843+
844+
845+
class(`Shader`) <- c("Shader__bundle", "savvy_skiagd__sealed")
846+
847+
#' @export
848+
`print.Shader__bundle` <- function(x, ...) {
849+
cat('Shader\n')
850+
}
851+
765852
### wrapper functions for Style
766853

767854

@@ -838,3 +925,69 @@ class(`Style`) <- c("Style__bundle", "savvy_skiagd__sealed")
838925
cat('Style\n')
839926
}
840927

928+
### wrapper functions for TileMode
929+
930+
931+
`.savvy_wrap_TileMode` <- function(ptr) {
932+
e <- new.env(parent = emptyenv())
933+
e$.ptr <- ptr
934+
935+
936+
class(e) <- c("TileMode", "savvy_skiagd__sealed")
937+
e
938+
}
939+
940+
941+
#' TileMode (0-3)
942+
#'
943+
#' @export
944+
`TileMode` <- new.env(parent = emptyenv())
945+
`TileMode`$`Clamp` <- .savvy_wrap_TileMode(0L)
946+
`TileMode`$`Repeat` <- .savvy_wrap_TileMode(1L)
947+
`TileMode`$`Mirror` <- .savvy_wrap_TileMode(2L)
948+
`TileMode`$`Decal` <- .savvy_wrap_TileMode(3L)
949+
950+
#' @export
951+
`$.TileMode__bundle` <- function(x, name) {
952+
if (!name %in% c("Clamp", "Repeat", "Mirror", "Decal")) {
953+
stop(paste0("Unknown variant: ", name), call. = FALSE)
954+
}
955+
956+
NextMethod()
957+
}
958+
959+
#' @export
960+
`[[.TileMode__bundle` <- function(x, i) {
961+
if (is.numeric(i)) {
962+
stop("TileMode cannot be subset by index", call. = FALSE)
963+
}
964+
965+
if (!i %in% c("Clamp", "Repeat", "Mirror", "Decal")) {
966+
stop(paste0("Unknown variant: ", i), call. = FALSE)
967+
}
968+
969+
NextMethod()
970+
}
971+
972+
#' @export
973+
`print.TileMode` <- function(x, ...) {
974+
idx <- x$.ptr + 1L
975+
label <- c("Clamp", "Repeat", "Mirror", "Decal")[idx]
976+
if (is.na(label)) {
977+
stop("Unexpected value for TileMode", call. = TRUE)
978+
}
979+
cat("TileMode::", label, "\n", sep = "")
980+
}
981+
982+
983+
### associated functions for TileMode
984+
985+
986+
987+
class(`TileMode`) <- c("TileMode__bundle", "savvy_skiagd__sealed")
988+
989+
#' @export
990+
`print.TileMode__bundle` <- function(x, ...) {
991+
cat('TileMode\n')
992+
}
993+

‎R/aaa.R

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ print.PathEffect <- function(x, ...) {
1212
cat("PathEffect::", x$get_label(), "\n", sep = "")
1313
}
1414

15+
#' @export
16+
print.Shader <- function(x, ...) {
17+
cat("Shader::", x$get_label(), "\n", sep = "")
18+
}
19+
1520
#' Applying affine transformations to a picture
1621
#'
1722
#' @description

‎R/paint.R

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#' * `miter`: Numeric scalar (stroke miter)
2121
#' * `blend_mode`: See [BlendMode].
2222
#' * `path_effect`: See [PathEffect].
23+
#' * `shader`: See [Shader].
2324
#' * `point_mode`: [PointMode] for [add_point()].
2425
#' * `fill_type`: [FillType] for [add_path()].
2526
#' * `transform`: Numerics of length 9. See [transform-matrix] for affine transformations.
@@ -94,6 +95,7 @@ default_attrs <- function() {
9495
miter = props[["linemitre"]],
9596
blend_mode = env_get(BlendMode, "Src"),
9697
path_effect = PathEffect$no_effect(),
98+
shader = Shader$no_shader(),
9799
point_mode = env_get(PointMode, "Points"),
98100
fill_type = env_get(FillType, "Winding"),
99101
transform = sk_matrix_default()
@@ -109,6 +111,7 @@ as_paint_attrs <- function(p) {
109111
p[["width"]],
110112
p[["miter"]],
111113
p[["blend_mode"]],
112-
p[["path_effect"]]
114+
p[["path_effect"]],
115+
p[["shader"]]
113116
)
114117
}

‎man/Shader.Rd

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/TileMode.Rd

+16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/paint.Rd

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)