Merge pull request #6278 from penpot/elenatorro-send-leaf-attrs-uint-arr

🔧 Parse text leaves all at once
This commit is contained in:
Elena Torró 2025-04-21 14:27:26 +02:00 committed by GitHub
commit c7b062f483
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 457 additions and 117 deletions

View file

@ -18,6 +18,7 @@
[app.main.refs :as refs]
[app.main.render :as render]
[app.render-wasm.api.fonts :as f]
[app.render-wasm.api.texts :as t]
[app.render-wasm.deserializers :as dr]
[app.render-wasm.helpers :as h]
[app.render-wasm.mem :as mem]
@ -30,7 +31,6 @@
[app.util.http :as http]
[app.util.webapi :as wapi]
[beicon.v2.core :as rx]
[cuerdas.core :as str]
[promesa.core :as p]
[rumext.v2 :as mf]))
@ -328,8 +328,6 @@
(h/call wasm/internal-module "stringToUTF8" content offset size)
(h/call wasm/internal-module "_set_shape_svg_raw_content")))
(defn set-shape-blend-mode
[blend-mode]
;; These values correspond to skia::BlendMode representation
@ -597,47 +595,19 @@
(h/call wasm/internal-module "_add_shape_shadow" rgba blur spread x y (sr/translate-shadow-style style) hidden)
(recur (inc index)))))))
(defn utf8->buffer [text]
(let [encoder (js/TextEncoder.)]
(.encode encoder text)))
(defn- add-text-leaf [leaf]
(let [text (dm/get-prop leaf :text)]
(when (and text (not (str/blank? text)))
(let [font-id (f/serialize-font-id (dm/get-prop leaf :font-id))
font-style (f/serialize-font-style (dm/get-prop leaf :font-style))
font-weight (f/serialize-font-weight (dm/get-prop leaf :font-weight))
font-size (js/Number (dm/get-prop leaf :font-size))
buffer (utf8->buffer text)
size (.-byteLength buffer)
offset (mem/alloc-bytes size)
heap (mem/get-heap-u8)
mem (js/Uint8Array. (.-buffer heap) offset size)]
(.set mem buffer)
(h/call wasm/internal-module "_add_text_leaf"
(aget font-id 0)
(aget font-id 1)
(aget font-id 2)
(aget font-id 3)
font-weight font-style font-size)))))
(defn set-shape-text-content [content]
(defn set-shape-text-content
[content]
(h/call wasm/internal-module "_clear_shape_text")
(let [paragraph-set (first (dm/get-prop content :children))
paragraphs (dm/get-prop paragraph-set :children)
fonts (fonts/get-content-fonts content)
total-paragraphs (count paragraphs)]
fonts (fonts/get-content-fonts content)]
(loop [index 0]
(when (< index total-paragraphs)
(when (< index (count paragraphs))
(let [paragraph (nth paragraphs index)
leaves (dm/get-prop paragraph :children)]
(when (seq leaves)
(h/call wasm/internal-module "_add_text_paragraph")
(loop [leaf-index 0]
(when (< leaf-index (count leaves))
(add-text-leaf (nth leaves leaf-index))
(recur (inc leaf-index))))))
(recur (inc index))))
(t/write-shape-text leaves paragraph)
(recur (inc index))))))
(f/store-fonts fonts)))
(defn set-view-box

View file

@ -175,3 +175,4 @@
:style style
:weight weight}]
(store-font-id font-data asset-id))) fonts))

View file

@ -0,0 +1,105 @@
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) KALEIDOS INC
(ns app.render-wasm.api.texts
(:require
[app.render-wasm.api.fonts :as f]
[app.render-wasm.helpers :as h]
[app.render-wasm.mem :as mem]
[app.render-wasm.serializers :as sr]
[app.render-wasm.wasm :as wasm]))
(defn utf8->buffer [text]
(let [encoder (js/TextEncoder.)]
(.encode encoder text)))
(defn write-shape-text
;; buffer has the following format:
;; [<num-leaves> <paragraph_attributes> <leaves_attributes> <text>]
[leaves paragraph]
(let [num-leaves (count leaves)
paragraph-attr-size 48
leaf-attr-size 52
metadata-size (+ 1 paragraph-attr-size (* num-leaves leaf-attr-size))
text (apply str (map :text leaves))
text-buffer (utf8->buffer text)
text-size (.-byteLength text-buffer)
buffer (js/ArrayBuffer. (+ metadata-size text-size))
dview (js/DataView. buffer)]
(.setUint32 dview 0 num-leaves)
;; Serialize paragraph attributes
(let [text-align (sr/serialize-text-align (:text-align paragraph))
text-direction (sr/serialize-text-direction (:text-direction paragraph))
text-decoration (sr/serialize-text-decoration (:text-decoration paragraph))
text-transform (sr/serialize-text-transform (:text-transform paragraph))
line-height (:line-height paragraph)
letter-spacing (:letter-spacing paragraph)
typography-ref-file (sr/serialize-uuid (:typography-ref-file paragraph))
typography-ref-id (sr/serialize-uuid (:typography-ref-id paragraph))]
(.setUint8 dview 4 text-align)
(.setUint8 dview 5 text-direction)
(.setUint8 dview 6 text-decoration)
(.setUint8 dview 7 text-transform)
(.setFloat32 dview 8 line-height)
(.setFloat32 dview 12 letter-spacing)
(.setUint32 dview 16 (aget typography-ref-file 0))
(.setUint32 dview 20 (aget typography-ref-file 1))
(.setUint32 dview 24 (aget typography-ref-file 2))
(.setInt32 dview 28 (aget typography-ref-file 3))
(.setUint32 dview 32 (aget typography-ref-id 0))
(.setUint32 dview 36 (aget typography-ref-id 1))
(.setUint32 dview 40 (aget typography-ref-id 2))
(.setInt32 dview 44 (aget typography-ref-id 3)))
;; Serialize leaves attributes
(loop [index 0 offset (+ 1 paragraph-attr-size)]
(when (< index num-leaves)
(let [leaf (nth leaves index)
font-style (f/serialize-font-style (:font-style leaf))
font-size (:font-size leaf)
font-weight (:font-weight leaf)
font-id (f/serialize-font-id (:font-id leaf))
font-family (hash (:font-family leaf))
font-variant-id (sr/serialize-uuid (:font-variant-id leaf))
text-length (count (:text leaf))]
(.setUint8 dview offset font-style)
(.setFloat32 dview (+ offset 4) font-size)
(.setUint32 dview (+ offset 8) font-weight)
(.setUint32 dview (+ offset 12) (aget font-id 0))
(.setUint32 dview (+ offset 16) (aget font-id 1))
(.setUint32 dview (+ offset 20) (aget font-id 2))
(.setInt32 dview (+ offset 24) (aget font-id 3))
(.setInt32 dview (+ offset 28) font-family)
(.setUint32 dview (+ offset 32) (aget font-variant-id 0))
(.setUint32 dview (+ offset 36) (aget font-variant-id 1))
(.setUint32 dview (+ offset 40) (aget font-variant-id 2))
(.setInt32 dview (+ offset 44) (aget font-variant-id 3))
(.setInt32 dview (+ offset 48) text-length)
(recur (inc index) (+ offset leaf-attr-size)))))
;; Add text content to buffer
(let [text-offset metadata-size
buffer-u8 (js/Uint8Array. buffer)]
(.set buffer-u8 (js/Uint8Array. text-buffer) text-offset))
;; Allocate memory and set buffer
(let [total-size (.-byteLength buffer)
metadata-offset (mem/alloc-bytes total-size)
heap (mem/get-heap-u8)]
(.set heap (js/Uint8Array. buffer) metadata-offset)))
(h/call wasm/internal-module "_set_shape_text_content"))

View file

@ -44,6 +44,13 @@
(aset u32-arr 3 (aget buffer 3))
(js/Uint8Array. (.-buffer u32-arr))))
(defn serialize-uuid
[id]
(if (nil? id)
[uuid/zero]
(let [as-uuid (uuid/uuid id)]
(uuid/get-u32 as-uuid))))
(defn heapu32-set-u32
[value heap offset]
(aset heap offset value))
@ -266,9 +273,28 @@
:inner-shadow 1
0))
(defn translate-structure-modifier-type
[type]
(case type
:remove-children 1
:add-children 2))
(defn- serialize-enum
[value enum-map]
(get enum-map value 0))
(defn serialize-text-align
[text-align]
(serialize-enum text-align {"left" 0 "center" 1 "right" 2 "justify" 3}))
(defn serialize-text-transform
[text-transform]
(serialize-enum text-transform {"none" 0 "uppercase" 1 "lowercase" 2 "capitalize" 3}))
(defn serialize-text-decoration
[text-decoration]
(serialize-enum text-decoration {"none" 0 "underline" 1 "line-through" 2 "overline" 3}))
(defn serialize-text-direction
[text-direction]
(serialize-enum text-direction {"ltr" 0 "rtl" 1}))