mirror of
https://github.com/penpot/penpot.git
synced 2025-12-12 06:24:17 +01:00
✨ Limit the amount of text fills passed to wasm
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
|
|
||||||
(def ^:const PARAGRAPH-ATTR-U8-SIZE 44)
|
(def ^:const PARAGRAPH-ATTR-U8-SIZE 44)
|
||||||
(def ^:const LEAF-ATTR-U8-SIZE 60)
|
(def ^:const LEAF-ATTR-U8-SIZE 60)
|
||||||
|
(def ^:const MAX-TEXT-FILLS types.fills.impl/MAX-FILLS)
|
||||||
|
|
||||||
(defn- encode-text
|
(defn- encode-text
|
||||||
"Into an UTF8 buffer. Returns an ArrayBuffer instance"
|
"Into an UTF8 buffer. Returns an ArrayBuffer instance"
|
||||||
@@ -26,7 +27,7 @@
|
|||||||
|
|
||||||
(defn- write-leaf-fills
|
(defn- write-leaf-fills
|
||||||
[offset dview fills]
|
[offset dview fills]
|
||||||
(reduce (fn [offset fill]
|
(let [new-ofset (reduce (fn [offset fill]
|
||||||
(let [opacity (get fill :fill-opacity 1.0)
|
(let [opacity (get fill :fill-opacity 1.0)
|
||||||
color (get fill :fill-color)
|
color (get fill :fill-color)
|
||||||
gradient (get fill :fill-color-gradient)
|
gradient (get fill :fill-color-gradient)
|
||||||
@@ -43,11 +44,10 @@
|
|||||||
(types.fills.impl/write-image-fill offset dview opacity image))))
|
(types.fills.impl/write-image-fill offset dview opacity image))))
|
||||||
|
|
||||||
offset
|
offset
|
||||||
fills))
|
fills)
|
||||||
|
padding-fills (max 0 (- MAX-TEXT-FILLS (count fills)))]
|
||||||
|
(+ new-ofset (* padding-fills types.fills.impl/FILL-U8-SIZE))))
|
||||||
|
|
||||||
(defn- get-total-fills
|
|
||||||
[leaves]
|
|
||||||
(reduce #(+ %1 (count (:fills %2))) 0 leaves))
|
|
||||||
|
|
||||||
(defn- write-paragraph
|
(defn- write-paragraph
|
||||||
[offset dview paragraph]
|
[offset dview paragraph]
|
||||||
@@ -86,8 +86,7 @@
|
|||||||
|
|
||||||
text-buffer (encode-text (get leaf :text))
|
text-buffer (encode-text (get leaf :text))
|
||||||
text-length (mem/size text-buffer)
|
text-length (mem/size text-buffer)
|
||||||
fills (get leaf :fills)
|
fills (take MAX-TEXT-FILLS (get leaf :fills))
|
||||||
total-fills (count fills)
|
|
||||||
|
|
||||||
font-variant-id
|
font-variant-id
|
||||||
(get leaf :font-variant-id)
|
(get leaf :font-variant-id)
|
||||||
@@ -127,7 +126,7 @@
|
|||||||
(mem/write-uuid dview (d/nilv font-variant-id uuid/zero))
|
(mem/write-uuid dview (d/nilv font-variant-id uuid/zero))
|
||||||
|
|
||||||
(mem/write-i32 dview text-length)
|
(mem/write-i32 dview text-length)
|
||||||
(mem/write-i32 dview total-fills)
|
(mem/write-i32 dview (count fills))
|
||||||
(mem/assert-written offset LEAF-ATTR-U8-SIZE)
|
(mem/assert-written offset LEAF-ATTR-U8-SIZE)
|
||||||
|
|
||||||
(write-leaf-fills dview fills))))
|
(write-leaf-fills dview fills))))
|
||||||
@@ -139,11 +138,9 @@
|
|||||||
;; [<num-leaves> <paragraph_attributes> <leaves_attributes> <text>]
|
;; [<num-leaves> <paragraph_attributes> <leaves_attributes> <text>]
|
||||||
[leaves paragraph text]
|
[leaves paragraph text]
|
||||||
(let [num-leaves (count leaves)
|
(let [num-leaves (count leaves)
|
||||||
fills-size (* types.fills.impl/FILL-U8-SIZE
|
fills-size (* types.fills.impl/FILL-U8-SIZE MAX-TEXT-FILLS)
|
||||||
(get-total-fills leaves))
|
|
||||||
metadata-size (+ PARAGRAPH-ATTR-U8-SIZE
|
metadata-size (+ PARAGRAPH-ATTR-U8-SIZE
|
||||||
(* num-leaves LEAF-ATTR-U8-SIZE)
|
(* num-leaves (+ LEAF-ATTR-U8-SIZE fills-size)))
|
||||||
fills-size)
|
|
||||||
|
|
||||||
text-buffer (encode-text text)
|
text-buffer (encode-text text)
|
||||||
text-size (mem/size text-buffer)
|
text-size (mem/size text-buffer)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use macros::ToJs;
|
use macros::ToJs;
|
||||||
|
|
||||||
use super::fonts::RawFontStyle;
|
use super::{fills::RawFillData, fonts::RawFontStyle};
|
||||||
use crate::math::{Matrix, Point};
|
use crate::math::{Matrix, Point};
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::shapes::{
|
use crate::shapes::{
|
||||||
@@ -9,10 +9,11 @@ use crate::shapes::{
|
|||||||
use crate::utils::{uuid_from_u32, uuid_from_u32_quartet};
|
use crate::utils::{uuid_from_u32, uuid_from_u32_quartet};
|
||||||
use crate::{with_current_shape, with_current_shape_mut, with_state_mut, STATE};
|
use crate::{with_current_shape, with_current_shape_mut, with_state_mut, STATE};
|
||||||
|
|
||||||
const RAW_LEAF_DATA_SIZE: usize = std::mem::size_of::<RawTextLeafAttrs>();
|
const RAW_LEAF_DATA_SIZE: usize = std::mem::size_of::<RawTextLeaf>();
|
||||||
pub const RAW_LEAF_FILLS_SIZE: usize = 160;
|
|
||||||
const RAW_PARAGRAPH_DATA_SIZE: usize = std::mem::size_of::<RawParagraphData>();
|
const RAW_PARAGRAPH_DATA_SIZE: usize = std::mem::size_of::<RawParagraphData>();
|
||||||
|
|
||||||
|
const MAX_TEXT_FILLS: usize = 8;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
#[derive(Debug, PartialEq, Clone, Copy, ToJs)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum RawTextAlign {
|
pub enum RawTextAlign {
|
||||||
@@ -121,10 +122,9 @@ impl TryFrom<&[u8]> for RawParagraphData {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: Merge this struct with RawTextLeaf once we cap the amount of fills a text shape has
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub struct RawTextLeafAttrs {
|
pub struct RawTextLeaf {
|
||||||
font_style: RawFontStyle,
|
font_style: RawFontStyle,
|
||||||
text_decoration: RawTextDecoration,
|
text_decoration: RawTextDecoration,
|
||||||
text_transform: RawTextTransform,
|
text_transform: RawTextTransform,
|
||||||
@@ -136,55 +136,24 @@ pub struct RawTextLeafAttrs {
|
|||||||
font_family: [u8; 4],
|
font_family: [u8; 4],
|
||||||
font_variant_id: [u32; 4], // TODO: maybe add RawUUID type
|
font_variant_id: [u32; 4], // TODO: maybe add RawUUID type
|
||||||
text_length: u32,
|
text_length: u32,
|
||||||
fill_count: u32, // FIXME: we should cap the amount of fills a text shape has
|
fill_count: u32,
|
||||||
|
fills: [RawFillData; MAX_TEXT_FILLS],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<[u8; RAW_LEAF_DATA_SIZE]> for RawTextLeafAttrs {
|
impl From<[u8; RAW_LEAF_DATA_SIZE]> for RawTextLeaf {
|
||||||
fn from(bytes: [u8; RAW_LEAF_DATA_SIZE]) -> Self {
|
fn from(bytes: [u8; RAW_LEAF_DATA_SIZE]) -> Self {
|
||||||
unsafe { std::mem::transmute(bytes) }
|
unsafe { std::mem::transmute(bytes) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<&[u8]> for RawTextLeafAttrs {
|
impl TryFrom<&[u8]> for RawTextLeaf {
|
||||||
type Error = String;
|
type Error = String;
|
||||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||||
let data: [u8; RAW_LEAF_DATA_SIZE] = bytes
|
let data: [u8; RAW_LEAF_DATA_SIZE] = bytes
|
||||||
.get(0..RAW_LEAF_DATA_SIZE)
|
.get(0..RAW_LEAF_DATA_SIZE)
|
||||||
.and_then(|slice| slice.try_into().ok())
|
.and_then(|slice| slice.try_into().ok())
|
||||||
.ok_or("Invalid text leaf data".to_string())?;
|
.ok_or("Invalid text leaf data".to_string())?;
|
||||||
Ok(RawTextLeafAttrs::from(data))
|
Ok(RawTextLeaf::from(data))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub struct RawTextLeaf {
|
|
||||||
attrs: RawTextLeafAttrs,
|
|
||||||
raw_fills: Vec<u8>, // FIXME: remove this once we cap the amount of fills a text shape has
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<&[u8]> for RawTextLeaf {
|
|
||||||
// TODO: use a proper error type
|
|
||||||
type Error = String;
|
|
||||||
|
|
||||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
|
||||||
let raw_attrs: RawTextLeafAttrs = RawTextLeafAttrs::try_from(bytes)?;
|
|
||||||
let total_fills = raw_attrs.fill_count as usize;
|
|
||||||
|
|
||||||
// Use checked_mul to prevent overflow
|
|
||||||
let fills_size = total_fills
|
|
||||||
.checked_mul(RAW_LEAF_FILLS_SIZE)
|
|
||||||
.ok_or("Overflow occurred while calculating fills size")?;
|
|
||||||
|
|
||||||
let fills_start = RAW_LEAF_DATA_SIZE;
|
|
||||||
let fills_end = fills_start + fills_size;
|
|
||||||
let raw_fills = &bytes[fills_start..fills_end];
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
attrs: raw_attrs,
|
|
||||||
raw_fills: raw_fills.to_vec(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,23 +162,28 @@ impl From<RawTextLeaf> for shapes::TextLeaf {
|
|||||||
let text = String::default();
|
let text = String::default();
|
||||||
|
|
||||||
let font_family = shapes::FontFamily::new(
|
let font_family = shapes::FontFamily::new(
|
||||||
uuid_from_u32(value.attrs.font_id),
|
uuid_from_u32(value.font_id),
|
||||||
value.attrs.font_weight as u32,
|
value.font_weight as u32,
|
||||||
value.attrs.font_style.into(),
|
value.font_style.into(),
|
||||||
);
|
);
|
||||||
let fills =
|
|
||||||
super::fills::parse_fills_from_bytes(&value.raw_fills, value.attrs.fill_count as usize);
|
let fills = value
|
||||||
|
.fills
|
||||||
|
.into_iter()
|
||||||
|
.take(value.fill_count as usize)
|
||||||
|
.map(|fill| fill.into())
|
||||||
|
.collect();
|
||||||
|
|
||||||
Self::new(
|
Self::new(
|
||||||
text,
|
text,
|
||||||
font_family,
|
font_family,
|
||||||
value.attrs.font_size,
|
value.font_size,
|
||||||
value.attrs.letter_spacing,
|
value.letter_spacing,
|
||||||
value.attrs.text_decoration.into(),
|
value.text_decoration.into(),
|
||||||
value.attrs.text_transform.into(),
|
value.text_transform.into(),
|
||||||
value.attrs.text_direction.into(),
|
value.text_direction.into(),
|
||||||
value.attrs.font_weight,
|
value.font_weight,
|
||||||
uuid_from_u32(value.attrs.font_variant_id),
|
uuid_from_u32(value.font_variant_id),
|
||||||
fills,
|
fills,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -233,11 +207,8 @@ impl TryFrom<&Vec<u8>> for RawParagraph {
|
|||||||
let mut raw_text_leaves: Vec<RawTextLeaf> = Vec::new();
|
let mut raw_text_leaves: Vec<RawTextLeaf> = Vec::new();
|
||||||
|
|
||||||
for _ in 0..attrs.leaf_count {
|
for _ in 0..attrs.leaf_count {
|
||||||
let text_leaf = RawTextLeaf::try_from(&bytes[offset..])?;
|
let text_leaf = RawTextLeaf::try_from(&bytes[offset..(offset + RAW_LEAF_DATA_SIZE)])?;
|
||||||
let leaf_size =
|
offset += RAW_LEAF_DATA_SIZE;
|
||||||
RAW_LEAF_DATA_SIZE + (text_leaf.attrs.fill_count as usize * RAW_LEAF_FILLS_SIZE);
|
|
||||||
|
|
||||||
offset += leaf_size;
|
|
||||||
raw_text_leaves.push(text_leaf);
|
raw_text_leaves.push(text_leaf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +231,7 @@ impl From<RawParagraph> for shapes::Paragraph {
|
|||||||
|
|
||||||
let mut offset = 0;
|
let mut offset = 0;
|
||||||
for raw_leaf in value.leaves.into_iter() {
|
for raw_leaf in value.leaves.into_iter() {
|
||||||
let delta = raw_leaf.attrs.text_length as usize;
|
let delta = raw_leaf.text_length as usize;
|
||||||
let text_buffer = &value.text_buffer[offset..offset + delta];
|
let text_buffer = &value.text_buffer[offset..offset + delta];
|
||||||
|
|
||||||
let mut leaf = shapes::TextLeaf::from(raw_leaf);
|
let mut leaf = shapes::TextLeaf::from(raw_leaf);
|
||||||
|
|||||||
Reference in New Issue
Block a user