Skip to content

Commit

Permalink
fix: create pattern from Canvas (#1013)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Mar 4, 2025
1 parent d369c55 commit d1137a5
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
18 changes: 18 additions & 0 deletions __test__/regression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,21 @@ test('draw-avif-image', async (t) => {
ctx.drawImage(image, 0, 0)
await snapshotImage(t, { ctx, canvas })
})

// https://github.com/Brooooooklyn/canvas/issues/1010
test('canvas-pattern-1010', async (t) => {
const canvas = createCanvas(512, 512)
const tmpCanvas = createCanvas(512, 512)
const ctx = canvas.getContext('2d')
const tmpCtx = tmpCanvas.getContext('2d')
const image = await loadImage(join(__dirname, 'javascript.png'))
tmpCtx.drawImage(image, 0, 0)
const pattern = ctx.createPattern(image, 'repeat')
const pattern2 = ctx.createPattern(tmpCanvas, 'repeat')
ctx.fillStyle = pattern
ctx.fillRect(0, 0, 512 / 2, 512)

ctx.fillStyle = pattern2
ctx.fillRect(512 / 2, 0, 512 / 2, 512)
await snapshotImage(t, { ctx, canvas })
})
Binary file added __test__/snapshots/canvas-pattern-1010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 3 additions & 12 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ export interface DOMMatrix extends DOMMatrixReadOnly {
toJSON(): { [K in OmitNeverOfMatrix]: DOMMatrix[K] }
}

type OmitMatrixMethod = {
[K in keyof DOMMatrix]: DOMMatrix[K] extends (...args: any[]) => any ? never : K
}
type OmitMatrixMethod = { [K in keyof DOMMatrix]: DOMMatrix[K] extends (...args: any[]) => any ? never : K }

type OmitNeverOfMatrix = OmitMatrixMethod[keyof OmitMatrixMethod]

Expand Down Expand Up @@ -278,7 +276,7 @@ export interface SKRSContext2D
dh: number,
): void
createPattern(
image: Image | ImageData,
image: Image | ImageData | Canvas | SvgCanvas,
repeat: 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat' | null,
): CanvasPattern
getContextAttributes(): { alpha: boolean; desynchronized: boolean }
Expand Down Expand Up @@ -393,14 +391,7 @@ export function createCanvas(width: number, height: number): Canvas
export function createCanvas(width: number, height: number, svgExportFlag: SvgExportFlag): SvgCanvas

interface IGlobalFonts {
readonly families: {
family: string
styles: {
weight: number
width: string
style: string
}[]
}[]
readonly families: { family: string; styles: { weight: number; width: string; style: string }[] }[]
// return true if succeeded
register(font: Buffer, nameAlias?: string): boolean
// absolute path
Expand Down
15 changes: 14 additions & 1 deletion skia-c/skia_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,7 @@ extern "C"
}

skiac_shader *skiac_bitmap_get_shader(
bool is_canvas,
skiac_bitmap *c_bitmap,
int repeat_x,
int repeat_y,
Expand All @@ -1652,7 +1653,19 @@ extern "C"
skiac_transform c_ts)
{
const auto ts = conv_from_transform(c_ts);
auto bitmap = reinterpret_cast<SkBitmap *>(c_bitmap);
SkBitmap *bitmap;
if (is_canvas) {
auto surface = reinterpret_cast<SkSurface *>(c_bitmap);
auto bm = new SkBitmap();
bm->allocPixels(surface->imageInfo());
if (surface->readPixels(*bm, 0, 0)) {
bitmap = bm;
} else {
return nullptr;
}
} else {
bitmap = reinterpret_cast<SkBitmap *>(c_bitmap);
}
auto shader = bitmap->makeShader((SkTileMode)repeat_x, (SkTileMode)repeat_y, SkSamplingOptions({B, C}), &ts).release();
if (shader)
{
Expand Down
1 change: 1 addition & 0 deletions skia-c/skia_c.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ extern "C"
size_t skiac_bitmap_get_width(skiac_bitmap *c_bitmap);
size_t skiac_bitmap_get_height(skiac_bitmap *c_bitmap);
skiac_shader *skiac_bitmap_get_shader(
bool is_canvas,
skiac_bitmap *c_bitmap,
int repeat_x,
int repeat_y,
Expand Down
4 changes: 4 additions & 0 deletions src/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ impl CanvasPattern {
repetition: Option<String>,
) -> Result<Self> {
let mut inner_bitmap = None;
let mut is_canvas = false;
let bitmap = match input {
Either4::A(image) => image
.bitmap
Expand All @@ -80,12 +81,14 @@ impl CanvasPattern {
let canvas_bitmap = canvas.ctx.context.surface.get_bitmap();
let ptr = canvas_bitmap.0.bitmap;
inner_bitmap = Some(canvas_bitmap);
is_canvas = true;
ptr
}
Either4::D(svg_canvas) => {
let canvas_bitmap = svg_canvas.ctx.context.surface.get_bitmap();
let ptr = canvas_bitmap.0.bitmap;
inner_bitmap = Some(canvas_bitmap);
is_canvas = true;
ptr
}
};
Expand All @@ -110,6 +113,7 @@ impl CanvasPattern {
bitmap,
repeat_x,
repeat_y,
is_canvas,
}),
bitmap: inner_bitmap,
})
Expand Down
15 changes: 13 additions & 2 deletions src/sk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ pub mod ffi {
pub fn skiac_bitmap_get_height(c_bitmap: *mut skiac_bitmap) -> usize;

pub fn skiac_bitmap_get_shader(
is_canvas: bool,
c_bitmap: *mut skiac_bitmap,
repeat_x: i32,
repeat_y: i32,
Expand Down Expand Up @@ -2973,6 +2974,7 @@ impl Shader {
}

pub fn from_bitmap(
is_canvas: bool,
bitmap: *mut ffi::skiac_bitmap,
repeat_x: TileMode,
repeat_y: TileMode,
Expand All @@ -2981,8 +2983,15 @@ impl Shader {
ts: Transform,
) -> Option<Shader> {
unsafe {
let shader_ptr =
ffi::skiac_bitmap_get_shader(bitmap, repeat_x as i32, repeat_y as i32, b, c, ts.into());
let shader_ptr = ffi::skiac_bitmap_get_shader(
is_canvas,
bitmap,
repeat_x as i32,
repeat_y as i32,
b,
c,
ts.into(),
);
Shader::from_ptr(shader_ptr)
}
}
Expand Down Expand Up @@ -3581,11 +3590,13 @@ pub struct ImagePattern {
pub(crate) repeat_x: TileMode,
pub(crate) repeat_y: TileMode,
pub(crate) transform: Transform,
pub(crate) is_canvas: bool,
}

impl ImagePattern {
pub(crate) fn get_shader(&self) -> Option<Shader> {
Shader::from_bitmap(
self.is_canvas,
self.bitmap,
self.repeat_x,
self.repeat_y,
Expand Down

0 comments on commit d1137a5

Please sign in to comment.