0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-04-05 19:41:27 -05:00

Path test

This commit is contained in:
Alejandro Alonso 2024-10-10 09:09:53 +02:00
parent cf8b62f1a8
commit 43903014c6
3 changed files with 61 additions and 86 deletions

View file

@ -1,13 +1,11 @@
use std::boxed::Box;
use skia_safe::{
gpu::{self, gl::FramebufferInfo, DirectContext},
// Color, Paint, PaintStyle, Rect, Surface,
textlayout::{FontCollection, ParagraphBuilder, ParagraphStyle, TextStyle, TypefaceFontProvider},
FontMgr, Paint,
FontMgr, Paint, Path, PaintStyle
};
static ROBOTO_REGULAR: &[u8] = include_bytes!("RobotoMono-Regular.ttf");
static LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur at leo at nulla tincidunt placerat. Proin eget purus augue. Quisque et est ullamcorper, pellentesque felis nec, pulvinar massa. Aliquam imperdiet, nulla ut dictum euismod, purus dui pulvinar risus, eu suscipit elit neque ac est. Nullam eleifend justo quis placerat ultricies. Vestibulum ut elementum velit. Praesent et dolor sit amet purus bibendum mattis. Aliquam erat volutpat.";
use skia_safe as skia;
@ -195,8 +193,6 @@ pub unsafe extern "C" fn draw_shapes(state: *mut State, ptr: *mut Rect, len: usi
// create a `Vec<Rect>` from the pointer to the linear memory and length
let buf = Vec::<Rect>::from_raw_parts(ptr, len, len);
let text = "SKIA TEXT";
// skia_safe::Font::default() is empty, let's use something better
let font_mgr = skia_safe::FontMgr::new();
let typeface = font_mgr
@ -205,45 +201,54 @@ pub unsafe extern "C" fn draw_shapes(state: *mut State, ptr: *mut Rect, len: usi
let default_font = skia_safe::Font::new(typeface, 10.0);
let mut text_paint = skia::Paint::default();
text_paint.set_color(skia_safe::Color::BLACK);
text_paint.set_anti_alias(true);
text_paint.set_style(skia_safe::paint::Style::StrokeAndFill);
text_paint.set_stroke_width(1.0);
let mut path_paint = skia::Paint::default();
path_paint.set_color(skia_safe::Color::BLACK);
path_paint.set_anti_alias(true);
path_paint.set_stroke_width(1.0);
path_paint.set_style(PaintStyle::Stroke);
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);
text_paint.set_color(color);
state.surface.canvas().draw_str(text, (rect.left, rect.top), &default_font, &text_paint);
state.surface.canvas().draw_str("SKIA TEXT", (rect.left, rect.top), &default_font, &text_paint);
// https://github.com/rust-skia/rust-skia/blob/02c89a87649af8d2870fb631aae4a5e171887367/skia-org/src/skparagraph_example.rs#L18
const TYPEFACE_ALIAS: &str = "ubuntu-regular";
let typeface_font_provider = {
let mut typeface_font_provider = TypefaceFontProvider::new();
// We need a system font manager to be able to load typefaces.
let font_mgr = FontMgr::new();
let typeface = font_mgr
.new_from_data(ROBOTO_REGULAR, None)
.expect("Failed to load ROBOTO font");
typeface_font_provider.register_typeface(typeface, TYPEFACE_ALIAS);
typeface_font_provider
};
let mut path = Path::new();
path.move_to((rect.left, rect.top));
path.line_to((rect.right, rect.bottom));
state.surface.canvas().draw_path(&path, &path_paint);
// // https://github.com/rust-skia/rust-skia/blob/02c89a87649af8d2870fb631aae4a5e171887367/skia-org/src/skparagraph_example.rs#L18
// const TYPEFACE_ALIAS: &str = "ubuntu-regular";
// let typeface_font_provider = {
// let mut typeface_font_provider = TypefaceFontProvider::new();
// // We need a system font manager to be able to load typefaces.
// let font_mgr = FontMgr::new();
// let typeface = font_mgr
// .new_from_data(ROBOTO_REGULAR, None)
// .expect("Failed to load ROBOTO font");
// typeface_font_provider.register_typeface(typeface, TYPEFACE_ALIAS);
// typeface_font_provider
// };
let mut font_collection = FontCollection::new();
font_collection.set_default_font_manager(Some(typeface_font_provider.into()), None);
let paragraph_style = ParagraphStyle::new();
let mut paragraph_builder = ParagraphBuilder::new(&paragraph_style, font_collection);
let mut ts = TextStyle::new();
ts.set_foreground_paint(&Paint::default())
.set_font_families(&[TYPEFACE_ALIAS]);
paragraph_builder.push_style(&ts);
paragraph_builder.add_text(LOREM_IPSUM);
let mut paragraph = paragraph_builder.build();
paragraph.layout(256.0);
paragraph.paint(state.surface.canvas(), (rect.left, rect.top));
// let mut font_collection = FontCollection::new();
// font_collection.set_default_font_manager(Some(typeface_font_provider.into()), None);
// let paragraph_style = ParagraphStyle::new();
// let mut paragraph_builder = ParagraphBuilder::new(&paragraph_style, font_collection);
// let mut ts = TextStyle::new();
// ts.set_foreground_paint(&Paint::default())
// .set_font_families(&[TYPEFACE_ALIAS]);
// paragraph_builder.push_style(&ts);
// paragraph_builder.add_text("Other skia text");
// let mut paragraph = paragraph_builder.build();
// paragraph.layout(256.0);
// paragraph.paint(state.surface.canvas(), (rect.left, rect.top));
}
flush(state);
std::mem::forget(buf);

View file

@ -50,6 +50,7 @@
(render-v2-rs/draw-canvas vbox zoom base-objects)))
(defn set-objects [vbox zoom base-objects]
(js/console.log "set-objects")
(cond
;; Rust
(contains? cf/flags :render-v2-rs)

View file

@ -8876,8 +8876,6 @@ var wasmImports = {
/** @export */
invoke_vi,
/** @export */
invoke_vif,
/** @export */
invoke_viffff,
/** @export */
invoke_viffi,
@ -8888,8 +8886,6 @@ var wasmImports = {
/** @export */
invoke_viif,
/** @export */
invoke_viiff,
/** @export */
invoke_viii,
/** @export */
invoke_viiii,
@ -8946,12 +8942,6 @@ var ___cxa_increment_exception_refcount = createExportWrapper('__cxa_increment_e
var ___get_exception_message = createExportWrapper('__get_exception_message', 3);
var ___cxa_can_catch = createExportWrapper('__cxa_can_catch', 3);
var ___cxa_get_exception_ptr = createExportWrapper('__cxa_get_exception_ptr', 1);
var dynCall_iiiji = Module['dynCall_iiiji'] = createExportWrapper('dynCall_iiiji', 6);
var dynCall_ji = Module['dynCall_ji'] = createExportWrapper('dynCall_ji', 2);
var dynCall_iiji = Module['dynCall_iiji'] = createExportWrapper('dynCall_iiji', 5);
var dynCall_iijjiii = Module['dynCall_iijjiii'] = createExportWrapper('dynCall_iijjiii', 9);
var dynCall_iij = Module['dynCall_iij'] = createExportWrapper('dynCall_iij', 4);
var dynCall_vijjjii = Module['dynCall_vijjjii'] = createExportWrapper('dynCall_vijjjii', 10);
var dynCall_viji = Module['dynCall_viji'] = createExportWrapper('dynCall_viji', 5);
var dynCall_vijiii = Module['dynCall_vijiii'] = createExportWrapper('dynCall_vijiii', 7);
var dynCall_viiiiij = Module['dynCall_viiiiij'] = createExportWrapper('dynCall_viiiiij', 8);
@ -8959,6 +8949,7 @@ var dynCall_jii = Module['dynCall_jii'] = createExportWrapper('dynCall_jii', 3);
var dynCall_vij = Module['dynCall_vij'] = createExportWrapper('dynCall_vij', 4);
var dynCall_jiiiiii = Module['dynCall_jiiiiii'] = createExportWrapper('dynCall_jiiiiii', 7);
var dynCall_jiiiiji = Module['dynCall_jiiiiji'] = createExportWrapper('dynCall_jiiiiji', 8);
var dynCall_ji = Module['dynCall_ji'] = createExportWrapper('dynCall_ji', 2);
var dynCall_iijj = Module['dynCall_iijj'] = createExportWrapper('dynCall_iijj', 6);
var dynCall_iiij = Module['dynCall_iiij'] = createExportWrapper('dynCall_iiij', 5);
var dynCall_viij = Module['dynCall_viij'] = createExportWrapper('dynCall_viij', 5);
@ -9060,28 +9051,6 @@ function invoke_viiiii(index,a1,a2,a3,a4,a5) {
}
}
function invoke_v(index) {
var sp = stackSave();
try {
getWasmTableEntry(index)();
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_iiiii(index,a1,a2,a3,a4) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3,a4);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_iiii(index,a1,a2,a3) {
var sp = stackSave();
try {
@ -9104,6 +9073,17 @@ function invoke_iiiiiii(index,a1,a2,a3,a4,a5,a6) {
}
}
function invoke_iiiii(index,a1,a2,a3,a4) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3,a4);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viffff(index,a1,a2,a3,a4,a5) {
var sp = stackSave();
try {
@ -9181,28 +9161,6 @@ function invoke_iiiiffii(index,a1,a2,a3,a4,a5,a6,a7) {
}
}
function invoke_vif(index,a1,a2) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiff(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_viffi(index,a1,a2,a3,a4) {
var sp = stackSave();
try {
@ -9236,6 +9194,17 @@ function invoke_viiiiii(index,a1,a2,a3,a4,a5,a6) {
}
}
function invoke_v(index) {
var sp = stackSave();
try {
getWasmTableEntry(index)();
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_viiiiiiiii(index,a1,a2,a3,a4,a5,a6,a7,a8,a9) {
var sp = stackSave();
try {