0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-22 14:39:45 -05:00

Refactoring texts

This commit is contained in:
Alejandro Alonso 2024-10-10 09:35:50 +02:00
parent 43903014c6
commit 88722bcf4f
3 changed files with 98 additions and 69 deletions

View file

@ -5,10 +5,11 @@ use skia_safe::{
FontMgr, Paint, Path, PaintStyle
};
static ROBOTO_REGULAR: &[u8] = include_bytes!("RobotoMono-Regular.ttf");
use skia_safe as skia;
static ROBOTO_REGULAR: &[u8] = include_bytes!("RobotoMono-Regular.ttf");
static TYPEFACE_ALIAS: &str = "roboto-regular";
extern "C" {
pub fn emscripten_GetProcAddress(
name: *const ::std::os::raw::c_char,
@ -27,11 +28,13 @@ struct GpuState {
pub struct State {
gpu_state: GpuState,
surface: skia::Surface,
typeface_font_provider: TypefaceFontProvider,
default_font: skia_safe::Font,
}
impl State {
fn new(gpu_state: GpuState, surface: skia::Surface) -> Self {
State { gpu_state, surface }
fn new(gpu_state: GpuState, surface: skia::Surface, typeface_font_provider: TypefaceFontProvider, default_font: skia_safe::Font) -> Self {
State { gpu_state, surface, typeface_font_provider, default_font }
}
fn set_surface(&mut self, surface: skia::Surface) {
@ -98,7 +101,22 @@ fn render_rect(surface: &mut skia::Surface, rect: skia::Rect, color: skia::Color
pub extern "C" fn init(width: i32, height: i32) -> Box<State> {
let mut gpu_state = create_gpu_state();
let surface = create_surface(&mut gpu_state, width, height);
let state = State::new(gpu_state, surface);
// skia_safe::Font::default() is empty, let's use something better
let font_mgr = skia_safe::FontMgr::new();
let typeface = font_mgr
.new_from_data(ROBOTO_REGULAR, None)
.expect("Failed to load ROBOTO font");
let default_font = skia_safe::Font::new(typeface.clone(), 4.0);
let typeface_font_provider = {
let mut typeface_font_provider = TypefaceFontProvider::new();
// We need a system font manager to be able to load typefaces.
typeface_font_provider.register_typeface(typeface, TYPEFACE_ALIAS);
typeface_font_provider
};
let state = State::new(gpu_state, surface, typeface_font_provider, default_font);
Box::new(state)
}
@ -181,6 +199,7 @@ 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();
}
@ -193,13 +212,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);
// skia_safe::Font::default() is empty, let's use something better
let font_mgr = skia_safe::FontMgr::new();
let typeface = font_mgr
.new_from_data(ROBOTO_REGULAR, None)
.expect("Failed to load ROBOTO font");
let default_font = skia_safe::Font::new(typeface, 10.0);
let mut text_paint = skia::Paint::default();
text_paint.set_anti_alias(true);
text_paint.set_style(skia_safe::paint::Style::StrokeAndFill);
@ -217,38 +229,26 @@ pub unsafe extern "C" fn draw_shapes(state: *mut State, ptr: *mut Rect, len: usi
render_rect(&mut state.surface, r, color);
text_paint.set_color(color);
state.surface.canvas().draw_str("SKIA TEXT", (rect.left, rect.top), &default_font, &text_paint);
state.surface.canvas().draw_str("SKIA TEXT", (rect.left, rect.top), &state.default_font, &text_paint);
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("Other skia text");
// let mut paragraph = paragraph_builder.build();
// paragraph.layout(256.0);
// paragraph.paint(state.surface.canvas(), (rect.left, rect.top));
// https://github.com/rust-skia/rust-skia/blob/02c89a87649af8d2870fb631aae4a5e171887367/skia-org/src/skparagraph_example.rs#L18
let mut font_collection = FontCollection::new();
font_collection.set_default_font_manager(Some(state.typeface_font_provider.clone().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

@ -39,7 +39,6 @@
(defn draw-canvas
[vbox zoom base-objects]
(js/console.log "draw-canvas")
(cond
;; CPP
(contains? cf/flags :render-v2-cpp)
@ -50,7 +49,6 @@
(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,6 +8876,8 @@ var wasmImports = {
/** @export */
invoke_vi,
/** @export */
invoke_vif,
/** @export */
invoke_viffff,
/** @export */
invoke_viffi,
@ -8886,6 +8888,8 @@ var wasmImports = {
/** @export */
invoke_viif,
/** @export */
invoke_viiff,
/** @export */
invoke_viii,
/** @export */
invoke_viiii,
@ -8942,6 +8946,12 @@ 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);
@ -8949,7 +8959,6 @@ 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);
@ -9051,21 +9060,10 @@ function invoke_viiiii(index,a1,a2,a3,a4,a5) {
}
}
function invoke_iiii(index,a1,a2,a3) {
function invoke_v(index) {
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_iiiiiii(index,a1,a2,a3,a4,a5,a6) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3,a4,a5,a6);
getWasmTableEntry(index)();
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
@ -9084,10 +9082,10 @@ function invoke_iiiii(index,a1,a2,a3,a4) {
}
}
function invoke_viffff(index,a1,a2,a3,a4,a5) {
function invoke_iiii(index,a1,a2,a3) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3,a4,a5);
return getWasmTableEntry(index)(a1,a2,a3);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
@ -9095,10 +9093,10 @@ function invoke_viffff(index,a1,a2,a3,a4,a5) {
}
}
function invoke_iiff(index,a1,a2,a3) {
function invoke_iiiiiii(index,a1,a2,a3,a4,a5,a6) {
var sp = stackSave();
try {
return getWasmTableEntry(index)(a1,a2,a3);
return getWasmTableEntry(index)(a1,a2,a3,a4,a5,a6);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
@ -9139,6 +9137,28 @@ function invoke_viif(index,a1,a2,a3) {
}
}
function invoke_viffff(index,a1,a2,a3,a4,a5) {
var sp = stackSave();
try {
getWasmTableEntry(index)(a1,a2,a3,a4,a5);
} catch(e) {
stackRestore(sp);
if (!(e instanceof EmscriptenEH)) throw e;
_setThrew(1, 0);
}
}
function invoke_iiff(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_iif(index,a1,a2) {
var sp = stackSave();
try {
@ -9161,6 +9181,28 @@ 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 {
@ -9194,17 +9236,6 @@ 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 {