0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-09 16:48:16 -05:00

Playing with image export and svg generation

This commit is contained in:
Alejandro Alonso 2024-10-10 14:06:16 +02:00
parent 445691430b
commit dfe5d861f2
5 changed files with 96 additions and 62 deletions

View file

@ -17,6 +17,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "base64"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8"
[[package]]
name = "bindgen"
version = "0.69.4"
@ -370,6 +376,7 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
name = "render"
version = "0.1.0"
dependencies = [
"base64",
"gl",
"skia-safe",
]

View file

@ -16,6 +16,7 @@ path = "src/main.rs"
[dependencies]
gl = "0.14.0"
skia-safe = "0.78.2"
base64 = "0.13"
[profile.release]
opt-level = "s"

View file

@ -2,7 +2,7 @@ use std::boxed::Box;
use skia_safe::{
gpu::{self, gl::FramebufferInfo, DirectContext},
textlayout::{FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle, TypefaceFontProvider},
FontMgr, Paint, Path, PaintStyle
FontMgr, Paint, Path, PaintStyle, EncodedImageFormat, Data, Canvas, SurfaceProps
};
use skia_safe as skia;
@ -29,7 +29,7 @@ pub struct State {
gpu_state: GpuState,
surface: skia::Surface,
typeface_font_provider: TypefaceFontProvider,
default_font: skia_safe::Font,
default_font: skia_safe::Font
}
impl State {
@ -199,8 +199,11 @@ pub unsafe extern "C" fn scale(state: *mut State, sx: f32, sy: f32) {
#[no_mangle]
pub unsafe extern "C" fn reset_canvas(state: *mut State) {
(*state).surface.canvas().clear(skia_safe::Color::TRANSPARENT);
(*state).surface.canvas().reset_matrix();
println!("reset_canvas");
let state = unsafe { state.as_mut() }.expect("got an invalid state pointer");
state.surface.canvas().clear(skia_safe::Color::TRANSPARENT);
state.surface.canvas().reset_matrix();
flush(state);
}
#[no_mangle]
@ -223,11 +226,19 @@ pub unsafe extern "C" fn draw_shapes(state: *mut State, ptr: *mut Rect, len: usi
path_paint.set_stroke_width(1.0);
path_paint.set_style(PaintStyle::Stroke);
let svg_canvas = skia_safe::svg::Canvas::new(skia_safe::Rect::from_size((10000, 10000)), None);
for rect in buf.iter() {
let r = skia::Rect::new(rect.left, rect.top, rect.right, rect.bottom);
let color = skia::Color::from_argb((rect.a * 255.0) as u8, rect.r as u8, rect.g as u8, rect.b as u8);
render_rect(&mut state.surface, r, color);
let mut paint = skia::Paint::default();
paint.set_style(skia::PaintStyle::Fill);
paint.set_color(color);
paint.set_anti_alias(true);
svg_canvas.draw_rect(r, &paint);
text_paint.set_color(color);
state.surface.canvas().draw_str("SKIA TEXT", (rect.left, rect.top), &state.default_font, &text_paint);
@ -250,6 +261,19 @@ pub unsafe extern "C" fn draw_shapes(state: *mut State, ptr: *mut Rect, len: usi
paragraph.layout(256.0);
paragraph.paint(state.surface.canvas(), (rect.left, rect.top));
}
// base64 image of the canvas
// let image = state.surface.image_snapshot();
// let mut context = state.surface.direct_context();
// let encoded_image = image.encode(context.as_mut(), EncodedImageFormat::PNG, None).unwrap();
// let base64_image = base64::encode(&encoded_image.as_bytes());
// println!("data:image/png;base64,{}", base64_image);
// SVG representation
// let svg_data = svg_canvas.end();
// let svg = String::from_utf8_lossy(svg_data.as_bytes());
// println!("svg: {}", svg);
flush(state);
std::mem::forget(buf);
}

View file

@ -40,7 +40,9 @@
;; - 4 F32 for points coordinates
;; - 4 F32 for color
;; rect-size (* 8 4)
rect-size (* 8 4)]
rect-size (* 8 4)
;; kk (data gpu-state)
]
(when shapes-ptr
(free_rects shapes-ptr shapes-size))

View file

@ -8972,6 +8972,17 @@ var dynCall_iiiiij = Module['dynCall_iiiiij'] = createExportWrapper('dynCall_iii
var dynCall_iiiiijj = Module['dynCall_iiiiijj'] = createExportWrapper('dynCall_iiiiijj', 9);
var dynCall_iiiiiijj = Module['dynCall_iiiiiijj'] = createExportWrapper('dynCall_iiiiiijj', 10);
function invoke_iiii(index,a1,a2,a3) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_ii(index,a1) {
var sp = stackSave();
try {
@ -8983,17 +8994,6 @@ function invoke_ii(index,a1) {
}
}
function invoke_viii(index,a1,a2,a3) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_vi(index,a1) {
var sp = stackSave();
try {
@ -9005,28 +9005,6 @@ function invoke_vi(index,a1) {
}
}
function invoke_iii(index,a1,a2) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_vifi(index,a1,a2,a3) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_vii(index,a1,a2) {
var sp = stackSave();
try {
@ -9049,6 +9027,39 @@ function invoke_viiii(index,a1,a2,a3,a4) {
}
}
function invoke_viii(index,a1,a2,a3) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_iii(index,a1,a2) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_vifi(index,a1,a2,a3) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiiii(index,a1,a2,a3,a4,a5) {
var sp = stackSave();
try {
@ -9082,10 +9093,21 @@ function invoke_iiiii(index,a1,a2,a3,a4) {
}
}
function invoke_iiii(index,a1,a2,a3) {
function invoke_viffi(index,a1,a2,a3,a4) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3);
getWasmTableEntry(index)(a1,a2,a3,a4);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiiiffii(index,a1,a2,a3,a4,a5,a6,a7,a8) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3,a4,a5,a6,a7,a8);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
@ -9203,28 +9225,6 @@ function invoke_viiff(index,a1,a2,a3,a4) {
}
}
function invoke_viffi(index,a1,a2,a3,a4) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3,a4);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiiiffii(index,a1,a2,a3,a4,a5,a6,a7,a8) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3,a4,a5,a6,a7,a8);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiiiii(index,a1,a2,a3,a4,a5,a6) {
var sp = stackSave();
try {