mirror of
https://github.com/penpot/penpot.git
synced 2026-02-24 18:56:28 -05:00
Compare commits
2 Commits
alotor-exp
...
superalex-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbb5c91acd | ||
|
|
5eba070a2c |
@@ -58,8 +58,7 @@
|
||||
:share-id share-id
|
||||
:object-id (mapv :id objects)
|
||||
:route "objects"
|
||||
:skip-children skip-children
|
||||
:wasm "true"}
|
||||
:skip-children skip-children}
|
||||
uri (-> (cf/get :public-uri)
|
||||
(assoc :path "/render.html")
|
||||
(assoc :query (u/map->query-string params)))]
|
||||
|
||||
@@ -45,9 +45,7 @@
|
||||
[app.main.ui.shapes.svg-raw :as svg-raw]
|
||||
[app.main.ui.shapes.text :as text]
|
||||
[app.main.ui.shapes.text.fontfaces :as ff]
|
||||
[app.render-wasm.api :as wasm.api]
|
||||
[app.util.dom :as dom]
|
||||
[app.util.globals :as g]
|
||||
[app.util.http :as http]
|
||||
[app.util.strings :as ust]
|
||||
[app.util.thumbnails :as th]
|
||||
@@ -55,7 +53,6 @@
|
||||
[beicon.v2.core :as rx]
|
||||
[clojure.set :as set]
|
||||
[cuerdas.core :as str]
|
||||
[promesa.core :as p]
|
||||
[rumext.v2 :as mf]))
|
||||
|
||||
(def ^:const viewbox-decimal-precision 3)
|
||||
@@ -174,8 +171,6 @@
|
||||
;; Don't wrap svg elements inside a <g> otherwise some can break
|
||||
[:> svg-raw-wrapper {:shape shape :frame frame}]))))))
|
||||
|
||||
(set! wasm.api/shape-wrapper-factory shape-wrapper-factory)
|
||||
|
||||
(defn format-viewbox
|
||||
"Format a viewbox given a rectangle"
|
||||
[{:keys [x y width height] :or {x 0 y 0 width 100 height 100}}]
|
||||
@@ -485,48 +480,6 @@
|
||||
[:& ff/fontfaces-style {:fonts fonts}]
|
||||
[:& shape-wrapper {:shape object}]]]]))
|
||||
|
||||
(mf/defc object-wasm
|
||||
{::mf/wrap [mf/memo]}
|
||||
[{:keys [objects object-id embed skip-children]
|
||||
:or {embed false}
|
||||
:as props}]
|
||||
(let [object (get objects object-id)
|
||||
object (cond-> object
|
||||
(:hide-fill-on-export object)
|
||||
(assoc :fills [])
|
||||
|
||||
skip-children
|
||||
(assoc :shapes []))
|
||||
|
||||
{:keys [width height] :as bounds}
|
||||
(gsb/get-object-bounds objects object {:ignore-margin? false})
|
||||
|
||||
vbox (format-viewbox bounds)
|
||||
zoom 1
|
||||
canvas-ref (mf/use-ref nil)]
|
||||
|
||||
(mf/use-effect
|
||||
(fn []
|
||||
(let [canvas (mf/ref-val canvas-ref)]
|
||||
(->> @wasm.api/module
|
||||
(p/fmap
|
||||
(fn [ready?]
|
||||
(when ready?
|
||||
(try
|
||||
(when (wasm.api/init-canvas-context canvas)
|
||||
(wasm.api/initialize-viewport
|
||||
objects zoom vbox "transparent"
|
||||
(fn []
|
||||
(wasm.api/render-sync-shape object-id)
|
||||
(dom/set-attribute! canvas "id" (dm/str "screenshot-" object-id)))))
|
||||
(catch :default e
|
||||
(js/console.error "Error initializing canvas context:" e)
|
||||
false)))))))))
|
||||
[:canvas {:ref canvas-ref
|
||||
:width width
|
||||
:height height
|
||||
:style {:background "red"}}]))
|
||||
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
;; SPRITES (DEBUG)
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
[app.main.data.workspace.media :as dwm]
|
||||
[app.main.data.workspace.path :as dwdp]
|
||||
[app.main.data.workspace.specialized-panel :as-alias dwsp]
|
||||
[app.main.data.workspace.texts :as dwt]
|
||||
[app.main.features :as features]
|
||||
[app.main.refs :as refs]
|
||||
[app.main.store :as st]
|
||||
@@ -49,42 +50,41 @@
|
||||
(mf/deps id blocked hidden type selected edition drawing-tool text-editing?
|
||||
node-editing? grid-editing? drawing-path? create-comment? @z? @space?
|
||||
panning read-only?)
|
||||
(fn [event]
|
||||
(fn [bevent]
|
||||
;; We need to handle editor related stuff here because
|
||||
;; handling on editor dom node does not works properly.
|
||||
(let [target (dom/get-target event)
|
||||
(let [target (dom/get-target bevent)
|
||||
editor (txu/closest-text-editor-content target)]
|
||||
;; Capture mouse pointer to detect the movements even if cursor
|
||||
;; leaves the viewport or the browser itself
|
||||
;; https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture
|
||||
(if editor
|
||||
(.setPointerCapture editor (.-pointerId event))
|
||||
(.setPointerCapture target (.-pointerId event))))
|
||||
(.setPointerCapture editor (.-pointerId bevent))
|
||||
(.setPointerCapture target (.-pointerId bevent))))
|
||||
|
||||
(when (or (dom/class? (dom/get-target event) "viewport-controls")
|
||||
(dom/class? (dom/get-target event) "viewport-selrect")
|
||||
(dom/child? (dom/get-target event) (dom/query ".grid-layout-editor")))
|
||||
(when (or (dom/class? (dom/get-target bevent) "viewport-controls")
|
||||
(dom/class? (dom/get-target bevent) "viewport-selrect")
|
||||
(dom/child? (dom/get-target bevent) (dom/query ".grid-layout-editor")))
|
||||
|
||||
(dom/stop-propagation event)
|
||||
(dom/stop-propagation bevent)
|
||||
|
||||
(when-not @z?
|
||||
(let [native-event (dom/event->native-event event)
|
||||
ctrl? (kbd/ctrl? native-event)
|
||||
meta? (kbd/meta? native-event)
|
||||
shift? (kbd/shift? native-event)
|
||||
alt? (kbd/alt? native-event)
|
||||
mod? (kbd/mod? native-event)
|
||||
off-pt (dom/get-offset-position native-event)
|
||||
(let [event (dom/event->native-event bevent)
|
||||
ctrl? (kbd/ctrl? event)
|
||||
meta? (kbd/meta? event)
|
||||
shift? (kbd/shift? event)
|
||||
alt? (kbd/alt? event)
|
||||
mod? (kbd/mod? event)
|
||||
|
||||
left-click? (and (not panning) (dom/left-mouse? event))
|
||||
middle-click? (and (not panning) (dom/middle-mouse? event))]
|
||||
left-click? (and (not panning) (dom/left-mouse? bevent))
|
||||
middle-click? (and (not panning) (dom/middle-mouse? bevent))]
|
||||
|
||||
(cond
|
||||
(or middle-click? (and left-click? @space?))
|
||||
(do
|
||||
(dom/prevent-default event)
|
||||
(dom/prevent-default bevent)
|
||||
(if mod?
|
||||
(let [raw-pt (dom/get-client-position native-event)
|
||||
(let [raw-pt (dom/get-client-position event)
|
||||
pt (uwvv/point->viewport raw-pt)]
|
||||
(st/emit! (dw/start-zooming pt)))
|
||||
(st/emit! (dw/start-panning))))
|
||||
@@ -94,23 +94,18 @@
|
||||
(st/emit! (mse/->MouseEvent :down ctrl? shift? alt? meta?)
|
||||
::dwsp/interrupt)
|
||||
|
||||
(when (wasm.api/text-editor-is-active?)
|
||||
(wasm.api/text-editor-pointer-down (.-x off-pt) (.-y off-pt)))
|
||||
|
||||
(when (and (not= edition id) (or text-editing? grid-editing?))
|
||||
(st/emit! (dw/clear-edition-mode))
|
||||
;; FIXME: I think this is not completely correct because this
|
||||
;; is going to happen even when clicking or selecting text.
|
||||
;; Sync and stop WASM text editor when exiting edit mode
|
||||
#_(when (and text-editing?
|
||||
(features/active-feature? @st/state "render-wasm/v1")
|
||||
wasm.wasm/context-initialized?)
|
||||
(when-let [{:keys [shape-id content]} (wasm.api/text-editor-sync-content)]
|
||||
(st/emit! (dwt/v2-update-text-shape-content
|
||||
shape-id content
|
||||
:update-name? true
|
||||
:finalize? true)))
|
||||
(wasm.api/text-editor-stop)))
|
||||
(when (and text-editing?
|
||||
(features/active-feature? @st/state "render-wasm/v1")
|
||||
wasm.wasm/context-initialized?)
|
||||
(when-let [{:keys [shape-id content]} (wasm.api/text-editor-sync-content)]
|
||||
(st/emit! (dwt/v2-update-text-shape-content
|
||||
shape-id content
|
||||
:update-name? true
|
||||
:finalize? true)))
|
||||
(wasm.api/text-editor-stop)))
|
||||
|
||||
(when (and (not text-editing?)
|
||||
(not blocked)
|
||||
@@ -192,14 +187,10 @@
|
||||
alt? (kbd/alt? event)
|
||||
meta? (kbd/meta? event)
|
||||
hovering? (some? @hover)
|
||||
native-event (dom/event->native-event event)
|
||||
off-pt (dom/get-offset-position native-event)
|
||||
raw-pt (dom/get-client-position event)
|
||||
pt (uwvv/point->viewport raw-pt)]
|
||||
(st/emit! (mse/->MouseEvent :click ctrl? shift? alt? meta?))
|
||||
|
||||
;; FIXME: Maybe we can transform this into a cond instead
|
||||
;; of multiple (when)s.
|
||||
(when (and hovering?
|
||||
(not @space?)
|
||||
(not edition)
|
||||
@@ -207,8 +198,6 @@
|
||||
(not drawing-tool))
|
||||
(st/emit! (dw/select-shape (:id @hover) shift?)))
|
||||
|
||||
;; FIXME: Maybe we can move into a function of the kind
|
||||
;; "text-editor-on-click"
|
||||
;; If clicking on a text shape and wasm render is enabled, forward cursor position
|
||||
(when (and hovering?
|
||||
(not @space?)
|
||||
@@ -219,7 +208,9 @@
|
||||
(when (and (= :text (:type hover-shape))
|
||||
(features/active-feature? @st/state "text-editor-wasm/v1")
|
||||
wasm.wasm/context-initialized?)
|
||||
(wasm.api/text-editor-set-cursor-from-point (.-x off-pt) (.-y off-pt)))))
|
||||
(let [raw-pt (dom/get-client-position event)]
|
||||
;; FIXME
|
||||
(wasm.api/text-editor-set-cursor-from-point (.-x raw-pt) (.-y raw-pt))))))
|
||||
|
||||
(when (and @z?
|
||||
(not @space?)
|
||||
@@ -270,12 +261,6 @@
|
||||
wasm.wasm/context-initialized?)
|
||||
(wasm.api/text-editor-start id)))
|
||||
|
||||
(and editable? (= id edition) (not read-only?)
|
||||
(= type :text)
|
||||
(features/active-feature? @st/state "text-editor-wasm/v1")
|
||||
wasm.wasm/context-initialized?)
|
||||
(wasm.api/text-editor-select-all)
|
||||
|
||||
(some? selected-shape)
|
||||
(do
|
||||
(reset! hover selected-shape)
|
||||
@@ -325,24 +310,20 @@
|
||||
;; Release pointer on mouse up
|
||||
(.releasePointerCapture target (.-pointerId event)))
|
||||
|
||||
(let [native-event (dom/event->native-event event)
|
||||
off-pt (dom/get-offset-position native-event)
|
||||
ctrl? (kbd/ctrl? native-event)
|
||||
shift? (kbd/shift? native-event)
|
||||
alt? (kbd/alt? native-event)
|
||||
meta? (kbd/meta? native-event)
|
||||
(let [event (dom/event->native-event event)
|
||||
ctrl? (kbd/ctrl? event)
|
||||
shift? (kbd/shift? event)
|
||||
alt? (kbd/alt? event)
|
||||
meta? (kbd/meta? event)
|
||||
|
||||
left-click? (= 1 (.-which native-event))
|
||||
middle-click? (= 2 (.-which native-event))]
|
||||
left-click? (= 1 (.-which event))
|
||||
middle-click? (= 2 (.-which event))]
|
||||
|
||||
(when left-click?
|
||||
(st/emit! (mse/->MouseEvent :up ctrl? shift? alt? meta?))
|
||||
|
||||
(when (wasm.api/text-editor-is-active?)
|
||||
(wasm.api/text-editor-pointer-up (.-x off-pt) (.-y off-pt))))
|
||||
(st/emit! (mse/->MouseEvent :up ctrl? shift? alt? meta?)))
|
||||
|
||||
(when middle-click?
|
||||
(dom/prevent-default native-event)
|
||||
(dom/prevent-default event)
|
||||
|
||||
;; We store this so in Firefox the middle button won't do a paste of the content
|
||||
(mf/set-ref-val! disable-paste-ref true)
|
||||
@@ -400,9 +381,7 @@
|
||||
(let [last-position (mf/use-var nil)]
|
||||
(mf/use-fn
|
||||
(fn [event]
|
||||
(let [native-event (unchecked-get event "nativeEvent")
|
||||
off-pt (dom/get-offset-position native-event)
|
||||
raw-pt (dom/get-client-position event)
|
||||
(let [raw-pt (dom/get-client-position event)
|
||||
pt (uwvv/point->viewport raw-pt)
|
||||
|
||||
;; We calculate the delta because Safari's MouseEvent.movementX/Y drop
|
||||
@@ -411,12 +390,6 @@
|
||||
(gpt/subtract raw-pt @last-position)
|
||||
(gpt/point 0 0))]
|
||||
|
||||
;; IMPORTANT! This function, right now it's called on EVERY pointermove. I think
|
||||
;; in the future (when we handle the UI in the render) should be better to
|
||||
;; have a "wasm.api/pointer-move" function that works as an entry point for
|
||||
;; all the pointer-move events.
|
||||
(wasm.api/text-editor-pointer-move (.-x off-pt) (.-y off-pt))
|
||||
|
||||
(rx/push! move-stream pt)
|
||||
(reset! last-position raw-pt)
|
||||
(st/emit! (mse/->PointerEvent :delta delta
|
||||
|
||||
@@ -63,7 +63,7 @@
|
||||
|
||||
(mf/defc object-svg
|
||||
{::mf/wrap-props false}
|
||||
[{:keys [object-id embed skip-children wasm]}]
|
||||
[{:keys [object-id embed skip-children]}]
|
||||
(let [objects (mf/deref ref:objects)]
|
||||
|
||||
;; Set the globa CSS to assign the page size, needed for PDF
|
||||
@@ -77,41 +77,26 @@
|
||||
(mth/ceil height) "px")}))))
|
||||
|
||||
(when objects
|
||||
(if wasm
|
||||
[:& render/object-wasm
|
||||
{:objects objects
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]
|
||||
|
||||
[:& (mf/provider ctx/is-render?) {:value true}
|
||||
[:& render/object-svg
|
||||
{:objects objects
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]]))))
|
||||
[:& (mf/provider ctx/is-render?) {:value true}
|
||||
[:& render/object-svg
|
||||
{:objects objects
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]])))
|
||||
|
||||
(mf/defc objects-svg
|
||||
{::mf/wrap-props false}
|
||||
[{:keys [object-ids embed skip-children wasm]}]
|
||||
[{:keys [object-ids embed skip-children]}]
|
||||
(when-let [objects (mf/deref ref:objects)]
|
||||
(for [object-id object-ids]
|
||||
(let [objects (render/adapt-objects-for-shape objects object-id)]
|
||||
(if wasm
|
||||
[:& render/object-wasm
|
||||
{:objects objects
|
||||
:key (str object-id)
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]
|
||||
|
||||
[:& (mf/provider ctx/is-render?) {:value true}
|
||||
[:& render/object-svg
|
||||
{:objects objects
|
||||
:key (str object-id)
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]])))))
|
||||
[:& (mf/provider ctx/is-render?) {:value true}
|
||||
[:& render/object-svg
|
||||
{:objects objects
|
||||
:key (str object-id)
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children}]]))))
|
||||
|
||||
(defn- fetch-objects-bundle
|
||||
[& {:keys [file-id page-id share-id object-id] :as options}]
|
||||
@@ -151,7 +136,7 @@
|
||||
(defn- render-objects
|
||||
[params]
|
||||
(try
|
||||
(let [{:keys [file-id page-id embed share-id object-id skip-children wasm] :as params}
|
||||
(let [{:keys [file-id page-id embed share-id object-id skip-children] :as params}
|
||||
(coerce-render-objects-params params)]
|
||||
(st/emit! (fetch-objects-bundle :file-id file-id :page-id page-id :share-id share-id :object-id object-id))
|
||||
(if (uuid? object-id)
|
||||
@@ -162,8 +147,7 @@
|
||||
:share-id share-id
|
||||
:object-id object-id
|
||||
:embed embed
|
||||
:skip-children skip-children
|
||||
:wasm wasm}])
|
||||
:skip-children skip-children}])
|
||||
|
||||
(mf/html
|
||||
[:& objects-svg
|
||||
@@ -172,8 +156,7 @@
|
||||
:share-id share-id
|
||||
:object-ids (into #{} object-id)
|
||||
:embed embed
|
||||
:skip-children skip-children
|
||||
:wasm wasm}])))
|
||||
:skip-children skip-children}])))
|
||||
(catch :default cause
|
||||
(when-let [explain (-> cause ex-data ::sm/explain)]
|
||||
(js/console.log "Unexpected error")
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
[app.common.uuid :as uuid]
|
||||
[app.config :as cf]
|
||||
[app.main.refs :as refs]
|
||||
[app.main.render :as render]
|
||||
[app.main.store :as st]
|
||||
[app.main.ui.shapes.text]
|
||||
[app.main.worker :as mw]
|
||||
@@ -86,11 +87,7 @@
|
||||
(def text-editor-start text-editor/text-editor-start)
|
||||
(def text-editor-stop text-editor/text-editor-stop)
|
||||
(def text-editor-set-cursor-from-point text-editor/text-editor-set-cursor-from-point)
|
||||
(def text-editor-pointer-down text-editor/text-editor-pointer-down)
|
||||
(def text-editor-pointer-move text-editor/text-editor-pointer-move)
|
||||
(def text-editor-pointer-up text-editor/text-editor-pointer-up)
|
||||
(def text-editor-is-active? text-editor/text-editor-is-active?)
|
||||
(def text-editor-select-all text-editor/text-editor-select-all)
|
||||
(def text-editor-sync-content text-editor/text-editor-sync-content)
|
||||
|
||||
(def dpr
|
||||
@@ -99,9 +96,6 @@
|
||||
(def noop-fn
|
||||
(constantly nil))
|
||||
|
||||
;;
|
||||
(def shape-wrapper-factory nil)
|
||||
|
||||
(defn- yield-to-browser
|
||||
"Returns a promise that resolves after yielding to the browser's event loop.
|
||||
Uses requestAnimationFrame for smooth visual updates during loading."
|
||||
@@ -117,7 +111,7 @@
|
||||
(let [objects (mf/deref refs/workspace-page-objects)
|
||||
shape-wrapper
|
||||
(mf/with-memo [shape]
|
||||
(shape-wrapper-factory objects))]
|
||||
(render/shape-wrapper-factory objects))]
|
||||
|
||||
[:svg {:version "1.1"
|
||||
:xmlns "http://www.w3.org/2000/svg"
|
||||
@@ -269,6 +263,22 @@
|
||||
[attrs]
|
||||
(text-editor/apply-style-to-selection attrs use-shape set-shape-text-content))
|
||||
|
||||
(defn update-text-rect!
|
||||
[id]
|
||||
(when wasm/context-initialized?
|
||||
(mw/emit!
|
||||
{:cmd :index/update-text-rect
|
||||
:page-id (:current-page-id @st/state)
|
||||
:shape-id id
|
||||
:dimensions (get-text-dimensions id)})))
|
||||
|
||||
(defn- ensure-text-content
|
||||
"Guarantee that the shape always sends a valid text tree to WASM. When the
|
||||
content is nil (freshly created text) we fall back to
|
||||
tc/default-text-content so the renderer receives typography information."
|
||||
[content]
|
||||
(or content (tc/v2-default-text-content)))
|
||||
|
||||
(defn set-parent-id
|
||||
[id]
|
||||
(let [buffer (uuid/get-u32 id)]
|
||||
@@ -986,81 +996,65 @@
|
||||
(render-finish)
|
||||
(perf/end-measure "set-view-box::zoom")))))
|
||||
|
||||
(defn update-text-rect!
|
||||
[id]
|
||||
(when wasm/context-initialized?
|
||||
(mw/emit!
|
||||
{:cmd :index/update-text-rect
|
||||
:page-id (:current-page-id @st/state)
|
||||
:shape-id id
|
||||
:dimensions (get-text-dimensions id)})))
|
||||
|
||||
(defn- ensure-text-content
|
||||
"Guarantee that the shape always sends a valid text tree to WASM. When the
|
||||
content is nil (freshly created text) we fall back to
|
||||
tc/default-text-content so the renderer receives typography information."
|
||||
[content]
|
||||
(or content (tc/v2-default-text-content)))
|
||||
|
||||
(defn set-object
|
||||
[shape]
|
||||
(perf/begin-measure "set-object")
|
||||
(when shape
|
||||
(let [shape (svg-filters/apply-svg-derived shape)
|
||||
id (dm/get-prop shape :id)
|
||||
type (dm/get-prop shape :type)
|
||||
(let [shape (svg-filters/apply-svg-derived shape)
|
||||
id (dm/get-prop shape :id)
|
||||
type (dm/get-prop shape :type)
|
||||
|
||||
masked (get shape :masked-group)
|
||||
masked (get shape :masked-group)
|
||||
|
||||
fills (get shape :fills)
|
||||
strokes (if (= type :group)
|
||||
[] (get shape :strokes))
|
||||
children (get shape :shapes)
|
||||
content (let [content (get shape :content)]
|
||||
(if (= type :text)
|
||||
(ensure-text-content content)
|
||||
content))
|
||||
bool-type (get shape :bool-type)
|
||||
grow-type (get shape :grow-type)
|
||||
blur (get shape :blur)
|
||||
svg-attrs (get shape :svg-attrs)
|
||||
shadows (get shape :shadow)]
|
||||
fills (get shape :fills)
|
||||
strokes (if (= type :group)
|
||||
[] (get shape :strokes))
|
||||
children (get shape :shapes)
|
||||
content (let [content (get shape :content)]
|
||||
(if (= type :text)
|
||||
(ensure-text-content content)
|
||||
content))
|
||||
bool-type (get shape :bool-type)
|
||||
grow-type (get shape :grow-type)
|
||||
blur (get shape :blur)
|
||||
svg-attrs (get shape :svg-attrs)
|
||||
shadows (get shape :shadow)]
|
||||
|
||||
(shapes/set-shape-base-props shape)
|
||||
(shapes/set-shape-base-props shape)
|
||||
|
||||
;; Remaining properties that need separate calls (variable-length or conditional)
|
||||
(set-shape-children children)
|
||||
(set-shape-blur blur)
|
||||
(when (= type :group)
|
||||
(set-masked (boolean masked)))
|
||||
(when (= type :bool)
|
||||
(set-shape-bool-type bool-type))
|
||||
(when (and (some? content)
|
||||
(or (= type :path)
|
||||
(= type :bool)))
|
||||
(set-shape-path-content content))
|
||||
(when (some? svg-attrs)
|
||||
(set-shape-svg-attrs svg-attrs))
|
||||
(when (and (some? content) (= type :svg-raw))
|
||||
(set-shape-svg-raw-content (get-static-markup shape)))
|
||||
(set-shape-shadows shadows)
|
||||
(when (= type :text)
|
||||
(set-shape-grow-type grow-type))
|
||||
;; Remaining properties that need separate calls (variable-length or conditional)
|
||||
(set-shape-children children)
|
||||
(set-shape-blur blur)
|
||||
(when (= type :group)
|
||||
(set-masked (boolean masked)))
|
||||
(when (= type :bool)
|
||||
(set-shape-bool-type bool-type))
|
||||
(when (and (some? content)
|
||||
(or (= type :path)
|
||||
(= type :bool)))
|
||||
(set-shape-path-content content))
|
||||
(when (some? svg-attrs)
|
||||
(set-shape-svg-attrs svg-attrs))
|
||||
(when (and (some? content) (= type :svg-raw))
|
||||
(set-shape-svg-raw-content (get-static-markup shape)))
|
||||
(set-shape-shadows shadows)
|
||||
(when (= type :text)
|
||||
(set-shape-grow-type grow-type))
|
||||
|
||||
(set-shape-layout shape)
|
||||
(set-layout-data shape)
|
||||
(let [pending_thumbnails (into [] (concat
|
||||
(set-shape-text-content id content)
|
||||
(set-shape-text-images id content true)
|
||||
(set-shape-fills id fills true)
|
||||
(set-shape-strokes id strokes true)))
|
||||
pending_full (into [] (concat
|
||||
(set-shape-text-images id content false)
|
||||
(set-shape-fills id fills false)
|
||||
(set-shape-strokes id strokes false)))]
|
||||
(perf/end-measure "set-object")
|
||||
{:thumbnails pending_thumbnails
|
||||
:full pending_full}))))
|
||||
(set-shape-layout shape)
|
||||
(set-layout-data shape)
|
||||
|
||||
(let [pending_thumbnails (into [] (concat
|
||||
(set-shape-text-content id content)
|
||||
(set-shape-text-images id content true)
|
||||
(set-shape-fills id fills true)
|
||||
(set-shape-strokes id strokes true)))
|
||||
pending_full (into [] (concat
|
||||
(set-shape-text-images id content false)
|
||||
(set-shape-fills id fills false)
|
||||
(set-shape-strokes id strokes false)))]
|
||||
(perf/end-measure "set-object")
|
||||
{:thumbnails pending_thumbnails
|
||||
:full pending_full})))
|
||||
|
||||
(defn update-text-layouts
|
||||
[shapes]
|
||||
@@ -1658,33 +1652,6 @@
|
||||
(let [controls-to-blur (dom/query-all (dom/get-element "viewport-controls") ".blurrable")]
|
||||
(run! #(dom/set-style! % "filter" "blur(4px)") controls-to-blur)))
|
||||
|
||||
(defn render-shape-pixels
|
||||
[shape-id scale]
|
||||
(let [buffer (uuid/get-u32 shape-id)
|
||||
|
||||
offset
|
||||
(h/call wasm/internal-module "_render_shape_pixels"
|
||||
(aget buffer 0)
|
||||
(aget buffer 1)
|
||||
(aget buffer 2)
|
||||
(aget buffer 3)
|
||||
scale)
|
||||
|
||||
offset-32
|
||||
(mem/->offset-32 offset)
|
||||
|
||||
heap (mem/get-heap-u8)
|
||||
heapu32 (mem/get-heap-u32)
|
||||
|
||||
length (aget heapu32 (mem/->offset-32 offset))
|
||||
width (aget heapu32 (+ (mem/->offset-32 offset) 1))
|
||||
height (aget heapu32 (+ (mem/->offset-32 offset) 2))
|
||||
|
||||
result (dr/read-image-bytes heap (+ offset 12) length)]
|
||||
|
||||
(mem/free)
|
||||
|
||||
result))
|
||||
|
||||
(defn init-wasm-module
|
||||
[module]
|
||||
|
||||
@@ -45,10 +45,6 @@
|
||||
:center (gpt/point cx cy)
|
||||
:transform (gmt/matrix a b c d e f)}))
|
||||
|
||||
(defn read-image-bytes
|
||||
[heap offset length]
|
||||
(.slice ^js heap offset (+ offset length)))
|
||||
|
||||
(defn read-position-data-entry
|
||||
[heapu32 heapf32 offset]
|
||||
(let [paragraph (aget heapu32 (+ offset 0))
|
||||
|
||||
@@ -27,21 +27,6 @@
|
||||
(when wasm/context-initialized?
|
||||
(h/call wasm/internal-module "_text_editor_set_cursor_from_point" x y)))
|
||||
|
||||
(defn text-editor-pointer-down
|
||||
[x y]
|
||||
(when wasm/context-initialized?
|
||||
(h/call wasm/internal-module "_text_editor_pointer_down" x y)))
|
||||
|
||||
(defn text-editor-pointer-move
|
||||
[x y]
|
||||
(when wasm/context-initialized?
|
||||
(h/call wasm/internal-module "_text_editor_pointer_move" x y)))
|
||||
|
||||
(defn text-editor-pointer-up
|
||||
[x y]
|
||||
(when wasm/context-initialized?
|
||||
(h/call wasm/internal-module "_text_editor_pointer_up" x y)))
|
||||
|
||||
(defn text-editor-update-blink
|
||||
[timestamp-ms]
|
||||
(when wasm/context-initialized?
|
||||
@@ -98,12 +83,9 @@
|
||||
(h/call wasm/internal-module "_text_editor_stop")))
|
||||
|
||||
(defn text-editor-is-active?
|
||||
([id]
|
||||
(when wasm/context-initialized?
|
||||
(not (zero? (h/call wasm/internal-module "_text_editor_is_active_with_id" id)))))
|
||||
([]
|
||||
(when wasm/context-initialized?
|
||||
(not (zero? (h/call wasm/internal-module "_text_editor_is_active"))))))
|
||||
[]
|
||||
(when wasm/context-initialized?
|
||||
(not (zero? (h/call wasm/internal-module "_text_editor_is_active")))))
|
||||
|
||||
(defn text-editor-export-content
|
||||
[]
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
[rumext.v2 :as mf])
|
||||
(:import goog.events.EventType))
|
||||
|
||||
(def caret-blink-interval-ms 250)
|
||||
|
||||
(defn- sync-wasm-text-editor-content!
|
||||
"Sync WASM text editor content back to the shape via the standard
|
||||
commit pipeline. Called after every text-modifying input."
|
||||
@@ -54,18 +56,17 @@
|
||||
(.focus node))
|
||||
js/undefined))
|
||||
|
||||
;; Animation loop for cursor blink
|
||||
(mf/use-effect
|
||||
(fn []
|
||||
(let [raf-id (atom nil)
|
||||
animate (fn animate []
|
||||
(when (text-editor/text-editor-is-active?)
|
||||
(wasm.api/request-render "cursor-blink")
|
||||
(reset! raf-id (js/requestAnimationFrame animate))))]
|
||||
(animate)
|
||||
(let [timeout-id (atom nil)
|
||||
schedule-blink (fn schedule-blink []
|
||||
(when (text-editor/text-editor-is-active?)
|
||||
(wasm.api/request-render "cursor-blink"))
|
||||
(reset! timeout-id (js/setTimeout schedule-blink caret-blink-interval-ms)))]
|
||||
(schedule-blink)
|
||||
(fn []
|
||||
(when @raf-id
|
||||
(js/cancelAnimationFrame @raf-id))))))
|
||||
(when @timeout-id
|
||||
(js/clearTimeout @timeout-id))))))
|
||||
|
||||
;; Document-level keydown handler for control keys
|
||||
(mf/use-effect
|
||||
|
||||
@@ -6,8 +6,6 @@
|
||||
|
||||
(ns debug
|
||||
(:require
|
||||
[app.render-wasm.wasm :as wasm]
|
||||
[app.render-wasm.api :as wasm.api]
|
||||
[app.common.data :as d]
|
||||
[app.common.data.macros :as dm]
|
||||
[app.common.exceptions :as ex]
|
||||
@@ -460,24 +458,7 @@
|
||||
[]
|
||||
(.log js/console (clj->js @http/network-averages)))
|
||||
|
||||
|
||||
(defn print-last-exception
|
||||
[]
|
||||
(some-> errors/last-exception ex/print-throwable))
|
||||
|
||||
(defn ^:export export-image
|
||||
[]
|
||||
|
||||
(let [objects (dsh/lookup-page-objects @st/state)
|
||||
shape-id (->> (get-selected @st/state) first)
|
||||
bytes (wasm.api/render-shape-pixels shape-id 1.0)
|
||||
|
||||
blob (js/Blob. #js [bytes] #js {:type "image/png"})
|
||||
url (.createObjectURL js/URL blob)
|
||||
|
||||
a (.createElement js/document "a")]
|
||||
(set! (.-href a) url)
|
||||
(set! (.-download a) "export.png")
|
||||
(.click a)
|
||||
(.revokeObjectURL js/URL url)
|
||||
|
||||
nil))
|
||||
|
||||
@@ -76,4 +76,3 @@ export function getFills(fillStyle) {
|
||||
const [color, opacity] = getColor(fillStyle);
|
||||
return `[["^ ","~:fill-color","${color}","~:fill-opacity",${opacity}]]`;
|
||||
}
|
||||
|
||||
|
||||
@@ -162,15 +162,12 @@ class TextEditorPlayground {
|
||||
}
|
||||
|
||||
this.#module.call("use_shape", ...textShape.id);
|
||||
// FIXME: This function doesn't exists anymore.
|
||||
/*
|
||||
const caretPosition = this.#module.call(
|
||||
"get_caret_position_at",
|
||||
e.offsetX,
|
||||
e.offsetY,
|
||||
);
|
||||
console.log("caretPosition", caretPosition);
|
||||
*/
|
||||
};
|
||||
|
||||
#onResize = (_entries) => {
|
||||
|
||||
86
render-wasm/Cargo.lock
generated
86
render-wasm/Cargo.lock
generated
@@ -202,9 +202,9 @@ checksum = "d2fabcfbdc87f4758337ca535fb41a6d701b65693ce38287d856d1674551ec9b"
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.0"
|
||||
version = "0.16.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb"
|
||||
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
@@ -214,9 +214,9 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "indexmap"
|
||||
version = "2.7.1"
|
||||
version = "2.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652"
|
||||
checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017"
|
||||
dependencies = [
|
||||
"equivalent",
|
||||
"hashbrown",
|
||||
@@ -253,12 +253,6 @@ version = "3.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2db585e1d738fc771bf08a151420d3ed193d9d895a36df7f6f8a9456b911ddc"
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.161"
|
||||
@@ -468,18 +462,27 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.210"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8e3592472072e6e22e0a54d5904d9febf8508f65fb8552499a1abc7d1078c3a"
|
||||
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.210"
|
||||
version = "1.0.228"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f"
|
||||
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@@ -500,11 +503,11 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "0.6.8"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
|
||||
checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -515,9 +518,9 @@ checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "skia-bindings"
|
||||
version = "0.87.0"
|
||||
version = "0.93.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "704242769235d2ffe66a2a0a3002661262fc4af08d32807c362d7b0160ee703c"
|
||||
checksum = "2359f7e30c9da3f322f8ca3d4ec0abbc12a40035ce758309db0cdab07b5d4476"
|
||||
dependencies = [
|
||||
"bindgen",
|
||||
"cc",
|
||||
@@ -532,13 +535,12 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "skia-safe"
|
||||
version = "0.87.0"
|
||||
version = "0.93.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f7d94f3e7537c71ad4cf132eb26e3be8c8a886ed3649c4525c089041fc312b2"
|
||||
checksum = "7f9e837ea9d531c9efee8f980bfcdb7226b21db0285b0c3171d8be745829f940"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"bitflags",
|
||||
"lazy_static",
|
||||
"percent-encoding",
|
||||
"skia-bindings",
|
||||
"skia-svg-macros",
|
||||
@@ -579,38 +581,43 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.8.19"
|
||||
version = "1.0.3+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
|
||||
checksum = "c7614eaf19ad818347db24addfa201729cf2a9b6fdfd9eb0ab870fcacc606c0c"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"indexmap",
|
||||
"serde_core",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_edit",
|
||||
"toml_parser",
|
||||
"toml_writer",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.8"
|
||||
version = "1.0.0+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
|
||||
checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.22"
|
||||
name = "toml_parser"
|
||||
version = "1.0.9+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ae48d6208a266e853d946088ed816055e556cc6028c5e8e2b84d9fa5dd7c7f5"
|
||||
checksum = "702d4415e08923e7e1ef96cd5727c0dfed80b4d2fa25db9647fe5eb6f7c5a4c4"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"winnow",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_writer"
|
||||
version = "1.0.6+spec-1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.13"
|
||||
@@ -775,12 +782,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.6.20"
|
||||
version = "0.7.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36c1fec1a2bb5866f07c25f68c26e565c4c200aebb96d7e55710c19d3e8ac49b"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829"
|
||||
|
||||
[[package]]
|
||||
name = "xattr"
|
||||
|
||||
@@ -25,7 +25,7 @@ gl = "0.14.0"
|
||||
glam = "0.24.2"
|
||||
indexmap = "2.7.1"
|
||||
macros = { path = "macros" }
|
||||
skia-safe = { version = "0.87.0", default-features = false, features = [
|
||||
skia-safe = { version = "0.93.1", default-features = false, features = [
|
||||
"gl",
|
||||
"svg",
|
||||
"textlayout",
|
||||
|
||||
@@ -10,7 +10,7 @@ fi
|
||||
|
||||
export BUILD_NAME="${BUILD_NAME:-render-wasm}"
|
||||
export CARGO_BUILD_TARGET=${CARGO_BUILD_TARGET:-"wasm32-unknown-emscripten"};
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.87.0/skia-binaries-e551f334ad5cbdf43abf-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.93.1/skia-binaries-319323662b1685a112f5-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
|
||||
# 256 MB of initial heap to perform less
|
||||
# initial calls to memory grow.
|
||||
|
||||
@@ -11,7 +11,7 @@ fi
|
||||
. ./_build_env
|
||||
|
||||
export CARGO_BUILD_TARGET=${CARGO_BUILD_TARGET:-"wasm32-unknown-emscripten"};
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.87.0/skia-binaries-e551f334ad5cbdf43abf-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.93.1/skia-binaries-319323662b1685a112f5-wasm32-unknown-emscripten-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
|
||||
|
||||
ALLOWED_RULES="-D static_mut_refs"
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
onlyBuiltDependencies:
|
||||
- esbuild
|
||||
@@ -742,24 +742,6 @@ pub extern "C" fn end_temp_objects() {
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn render_shape_pixels(a: u32, b: u32, c: u32, d: u32, scale: f32) -> *mut u8 {
|
||||
let id = uuid_from_u32_quartet(a, b, c, d);
|
||||
|
||||
with_state_mut!(state, {
|
||||
let (data, width, height) = state.render_shape_pixels(&id, scale, performance::get_time())
|
||||
.expect("Cannot render into texture");
|
||||
|
||||
let len = data.len() as u32;
|
||||
let mut buf = Vec::with_capacity(4 + data.len());
|
||||
buf.extend_from_slice(&len.to_le_bytes());
|
||||
buf.extend_from_slice(&width.to_le_bytes());
|
||||
buf.extend_from_slice(&height.to_le_bytes());
|
||||
buf.extend_from_slice(&data);
|
||||
mem::write_bytes(buf)
|
||||
})
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
init_gl!();
|
||||
|
||||
@@ -356,7 +356,7 @@ impl Bounds {
|
||||
}
|
||||
|
||||
pub fn from_rect(r: &Rect) -> Self {
|
||||
let [nw, ne, se, sw] = r.to_quad();
|
||||
let [nw, ne, se, sw] = r.to_quad(None);
|
||||
Self::new(nw, ne, se, sw)
|
||||
}
|
||||
|
||||
|
||||
@@ -477,30 +477,32 @@ pub fn debug_render_bool_paths(
|
||||
paint.set_alpha_f(1.0);
|
||||
paint.set_style(skia::PaintStyle::Stroke);
|
||||
|
||||
let mut path = skia::Path::default();
|
||||
path.move_to((b.1.start.x as f32, b.1.start.y as f32));
|
||||
|
||||
match b.1.handles {
|
||||
BezierHandles::Linear => {
|
||||
path.line_to((b.1.end.x as f32, b.1.end.y as f32));
|
||||
let path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.move_to((b.1.start.x as f32, b.1.start.y as f32));
|
||||
match b.1.handles {
|
||||
BezierHandles::Linear => {
|
||||
pb.line_to((b.1.end.x as f32, b.1.end.y as f32));
|
||||
}
|
||||
BezierHandles::Quadratic { handle } => {
|
||||
pb.quad_to(
|
||||
(handle.x as f32, handle.y as f32),
|
||||
(b.1.end.x as f32, b.1.end.y as f32),
|
||||
);
|
||||
}
|
||||
BezierHandles::Cubic {
|
||||
handle_start,
|
||||
handle_end,
|
||||
} => {
|
||||
pb.cubic_to(
|
||||
(handle_start.x as f32, handle_start.y as f32),
|
||||
(handle_end.x as f32, handle_end.y as f32),
|
||||
(b.1.end.x as f32, b.1.end.y as f32),
|
||||
);
|
||||
}
|
||||
}
|
||||
BezierHandles::Quadratic { handle } => {
|
||||
path.quad_to(
|
||||
(handle.x as f32, handle.y as f32),
|
||||
(b.1.end.x as f32, b.1.end.y as f32),
|
||||
);
|
||||
}
|
||||
BezierHandles::Cubic {
|
||||
handle_start,
|
||||
handle_end,
|
||||
} => {
|
||||
path.cubic_to(
|
||||
(handle_start.x as f32, handle_start.y as f32),
|
||||
(handle_end.x as f32, handle_end.y as f32),
|
||||
(b.1.end.x as f32, b.1.end.y as f32),
|
||||
);
|
||||
}
|
||||
}
|
||||
pb.detach()
|
||||
};
|
||||
canvas.draw_path(&path, &paint);
|
||||
|
||||
let mut v1 = b.1.normal(TValue::Parametric(1.0));
|
||||
|
||||
@@ -18,7 +18,6 @@ use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use gpu_state::GpuState;
|
||||
|
||||
use options::RenderOptions;
|
||||
pub use surfaces::{SurfaceId, Surfaces};
|
||||
|
||||
@@ -44,7 +43,6 @@ const BLUR_DOWNSCALE_THRESHOLD: f32 = 8.0;
|
||||
|
||||
type ClipStack = Vec<(Rect, Option<Corners>, Matrix)>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct NodeRenderState {
|
||||
pub id: Uuid,
|
||||
// We use this bool to keep that we've traversed all the children inside this node.
|
||||
@@ -539,7 +537,7 @@ impl RenderState {
|
||||
);
|
||||
}
|
||||
|
||||
pub fn apply_drawing_to_render_canvas(&mut self, shape: Option<&Shape>, target: SurfaceId) {
|
||||
pub fn apply_drawing_to_render_canvas(&mut self, shape: Option<&Shape>) {
|
||||
performance::begin_measure!("apply_drawing_to_render_canvas");
|
||||
|
||||
let paint = skia::Paint::default();
|
||||
@@ -547,12 +545,12 @@ impl RenderState {
|
||||
// Only draw surfaces that have content (dirty flag optimization)
|
||||
if self.surfaces.is_dirty(SurfaceId::TextDropShadows) {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::TextDropShadows, target, Some(&paint));
|
||||
.draw_into(SurfaceId::TextDropShadows, SurfaceId::Current, Some(&paint));
|
||||
}
|
||||
|
||||
if self.surfaces.is_dirty(SurfaceId::Fills) {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::Fills, target, Some(&paint));
|
||||
.draw_into(SurfaceId::Fills, SurfaceId::Current, Some(&paint));
|
||||
}
|
||||
|
||||
let mut render_overlay_below_strokes = false;
|
||||
@@ -562,17 +560,17 @@ impl RenderState {
|
||||
|
||||
if render_overlay_below_strokes && self.surfaces.is_dirty(SurfaceId::InnerShadows) {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::InnerShadows, target, Some(&paint));
|
||||
.draw_into(SurfaceId::InnerShadows, SurfaceId::Current, Some(&paint));
|
||||
}
|
||||
|
||||
if self.surfaces.is_dirty(SurfaceId::Strokes) {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::Strokes, target, Some(&paint));
|
||||
.draw_into(SurfaceId::Strokes, SurfaceId::Current, Some(&paint));
|
||||
}
|
||||
|
||||
if !render_overlay_below_strokes && self.surfaces.is_dirty(SurfaceId::InnerShadows) {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::InnerShadows, target, Some(&paint));
|
||||
.draw_into(SurfaceId::InnerShadows, SurfaceId::Current, Some(&paint));
|
||||
}
|
||||
|
||||
// Build mask of dirty surfaces that need clearing
|
||||
@@ -645,7 +643,6 @@ impl RenderState {
|
||||
offset: Option<(f32, f32)>,
|
||||
parent_shadows: Option<Vec<skia_safe::Paint>>,
|
||||
spread: Option<f32>,
|
||||
target_surface: SurfaceId,
|
||||
) {
|
||||
let surface_ids = fills_surface_id as u32
|
||||
| strokes_surface_id as u32
|
||||
@@ -689,16 +686,15 @@ impl RenderState {
|
||||
&& !(shape.fills.is_empty() && has_nested_fills)
|
||||
&& !shape
|
||||
.svg_attrs
|
||||
.as_ref().is_some_and(|attrs| attrs.fill_none)
|
||||
&& target_surface != SurfaceId::Export;
|
||||
.as_ref()
|
||||
.is_some_and(|attrs| attrs.fill_none);
|
||||
|
||||
if can_render_directly {
|
||||
let scale = self.get_scale();
|
||||
let translation = self
|
||||
.surfaces
|
||||
.get_render_context_translation(self.render_area, scale);
|
||||
|
||||
self.surfaces.apply_mut(target_surface as u32, |s| {
|
||||
self.surfaces.apply_mut(SurfaceId::Current as u32, |s| {
|
||||
let canvas = s.canvas();
|
||||
canvas.save();
|
||||
canvas.scale((scale, scale));
|
||||
@@ -710,7 +706,7 @@ impl RenderState {
|
||||
shape,
|
||||
&shape.fills,
|
||||
antialias,
|
||||
target_surface,
|
||||
SurfaceId::Current,
|
||||
None,
|
||||
);
|
||||
|
||||
@@ -720,12 +716,12 @@ impl RenderState {
|
||||
self,
|
||||
shape,
|
||||
&visible_strokes,
|
||||
Some(target_surface),
|
||||
Some(SurfaceId::Current),
|
||||
antialias,
|
||||
spread,
|
||||
);
|
||||
|
||||
self.surfaces.apply_mut(target_surface as u32, |s| {
|
||||
self.surfaces.apply_mut(SurfaceId::Current as u32, |s| {
|
||||
s.canvas().restore();
|
||||
});
|
||||
|
||||
@@ -1138,7 +1134,7 @@ impl RenderState {
|
||||
}
|
||||
|
||||
if apply_to_current_surface {
|
||||
self.apply_drawing_to_render_canvas(Some(&shape), target_surface);
|
||||
self.apply_drawing_to_render_canvas(Some(&shape));
|
||||
}
|
||||
|
||||
// Only restore if we saved (optimization for simple shapes)
|
||||
@@ -1300,7 +1296,7 @@ impl RenderState {
|
||||
self.current_tile = None;
|
||||
self.render_in_progress = true;
|
||||
|
||||
self.apply_drawing_to_render_canvas(None, SurfaceId::Current);
|
||||
self.apply_drawing_to_render_canvas(None);
|
||||
|
||||
if sync_render {
|
||||
self.render_shape_tree_sync(base_object, tree, timestamp)?;
|
||||
@@ -1351,51 +1347,6 @@ impl RenderState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render_shape_pixels(
|
||||
&mut self,
|
||||
id: &Uuid,
|
||||
tree: ShapesPoolRef,
|
||||
scale: f32,
|
||||
timestamp: i32,
|
||||
) -> Result<(Vec<u8>, i32, i32), String> {
|
||||
let target_surface = SurfaceId::Export;
|
||||
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.clear(skia::Color::TRANSPARENT);
|
||||
|
||||
if tree.len() != 0 {
|
||||
let shape = tree.get(id).unwrap();
|
||||
let mut extrect = shape.extrect(tree, scale);
|
||||
let margins = self.surfaces.margins;
|
||||
extrect.offset((margins.width as f32 / scale, margins.height as f32 / scale));
|
||||
|
||||
self.surfaces.resize_export_surface(scale, extrect);
|
||||
self.surfaces.update_render_context(extrect, scale);
|
||||
|
||||
self.pending_nodes.push(NodeRenderState {
|
||||
id: *id,
|
||||
visited_children: false,
|
||||
clip_bounds: None,
|
||||
visited_mask: false,
|
||||
mask: false,
|
||||
});
|
||||
self.render_shape_tree_partial_uncached(tree, timestamp, false, true)?;
|
||||
}
|
||||
|
||||
self.surfaces.flush_and_submit(&mut self.gpu_state, target_surface);
|
||||
|
||||
let image = self.surfaces.snapshot(target_surface);
|
||||
let data = image.encode(
|
||||
&mut self.gpu_state.context,
|
||||
skia::EncodedImageFormat::PNG,
|
||||
100
|
||||
).expect("PNG encode failed");
|
||||
let skia::ISize { width, height } = image.dimensions();
|
||||
|
||||
Ok((data.as_bytes().to_vec(), width, height))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn should_stop_rendering(&self, iteration: i32, timestamp: i32) -> bool {
|
||||
iteration % NODE_BATCH_THRESHOLD == 0
|
||||
@@ -1403,7 +1354,7 @@ impl RenderState {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn render_shape_enter(&mut self, element: &Shape, mask: bool, target_surface: SurfaceId) {
|
||||
pub fn render_shape_enter(&mut self, element: &Shape, mask: bool) {
|
||||
// Masked groups needs two rendering passes, the first one rendering
|
||||
// the content and the second one rendering the mask so we need to do
|
||||
// an extra save_layer to keep all the masked group separate from
|
||||
@@ -1418,7 +1369,7 @@ impl RenderState {
|
||||
let paint = skia::Paint::default();
|
||||
let layer_rec = skia::canvas::SaveLayerRec::default().paint(&paint);
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.canvas(SurfaceId::Current)
|
||||
.save_layer(&layer_rec);
|
||||
}
|
||||
}
|
||||
@@ -1435,7 +1386,7 @@ impl RenderState {
|
||||
mask_paint.set_blend_mode(skia::BlendMode::DstIn);
|
||||
let mask_rec = skia::canvas::SaveLayerRec::default().paint(&mask_paint);
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.canvas(SurfaceId::Current)
|
||||
.save_layer(&mask_rec);
|
||||
}
|
||||
|
||||
@@ -1464,7 +1415,7 @@ impl RenderState {
|
||||
|
||||
let layer_rec = skia::canvas::SaveLayerRec::default().paint(&paint);
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.canvas(SurfaceId::Current)
|
||||
.save_layer(&layer_rec);
|
||||
}
|
||||
|
||||
@@ -1477,7 +1428,6 @@ impl RenderState {
|
||||
element: &Shape,
|
||||
visited_mask: bool,
|
||||
clip_bounds: Option<ClipStack>,
|
||||
target_surface: SurfaceId
|
||||
) {
|
||||
if visited_mask {
|
||||
// Because masked groups needs two rendering passes (first drawing
|
||||
@@ -1485,7 +1435,7 @@ impl RenderState {
|
||||
// extra restore.
|
||||
if let Type::Group(group) = element.shape_type {
|
||||
if group.masked {
|
||||
self.surfaces.canvas(target_surface).restore();
|
||||
self.surfaces.canvas(SurfaceId::Current).restore();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -1547,7 +1497,6 @@ impl RenderState {
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
target_surface,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1556,7 +1505,7 @@ impl RenderState {
|
||||
let needs_layer = element.needs_layer();
|
||||
|
||||
if needs_layer {
|
||||
self.surfaces.canvas(target_surface).restore();
|
||||
self.surfaces.canvas(SurfaceId::Current).restore();
|
||||
}
|
||||
|
||||
self.focus_mode.exit(&element.id);
|
||||
@@ -1638,7 +1587,6 @@ impl RenderState {
|
||||
scale: f32,
|
||||
translation: (f32, f32),
|
||||
extra_layer_blur: Option<Blur>,
|
||||
target_surface: SurfaceId
|
||||
) {
|
||||
let mut transformed_shadow: Cow<Shadow> = Cow::Borrowed(shadow);
|
||||
transformed_shadow.to_mut().offset = (0.0, 0.0);
|
||||
@@ -1722,7 +1670,6 @@ impl RenderState {
|
||||
Some(shadow.offset),
|
||||
None,
|
||||
Some(shadow.spread),
|
||||
target_surface,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1769,7 +1716,6 @@ impl RenderState {
|
||||
Some(shadow.offset), // Offset is geometric
|
||||
None,
|
||||
Some(shadow.spread), // Spread is geometric
|
||||
target_surface,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1811,7 +1757,6 @@ impl RenderState {
|
||||
Some(shadow.offset), // Offset is geometric
|
||||
None,
|
||||
Some(shadow.spread), // Spread is geometric
|
||||
target_surface,
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1866,7 +1811,6 @@ impl RenderState {
|
||||
scale: f32,
|
||||
translation: (f32, f32),
|
||||
node_render_state: &NodeRenderState,
|
||||
target_surface: SurfaceId
|
||||
) {
|
||||
let element_extrect = extrect.get_or_insert_with(|| element.extrect(tree, scale));
|
||||
let inherited_layer_blur = match element.shape_type {
|
||||
@@ -1889,7 +1833,6 @@ impl RenderState {
|
||||
scale,
|
||||
translation,
|
||||
None,
|
||||
target_surface,
|
||||
);
|
||||
|
||||
if !matches!(element.shape_type, Type::Bool(_)) {
|
||||
@@ -1919,7 +1862,6 @@ impl RenderState {
|
||||
scale,
|
||||
translation,
|
||||
inherited_layer_blur,
|
||||
target_surface,
|
||||
);
|
||||
} else {
|
||||
let paint = skia::Paint::default();
|
||||
@@ -1956,7 +1898,6 @@ impl RenderState {
|
||||
None,
|
||||
Some(vec![new_shadow_paint.clone()]),
|
||||
None,
|
||||
target_surface,
|
||||
);
|
||||
});
|
||||
self.surfaces.canvas(SurfaceId::DropShadows).restore();
|
||||
@@ -1975,7 +1916,7 @@ impl RenderState {
|
||||
|
||||
if let Some(clips) = clip_bounds.as_ref() {
|
||||
let antialias = element.should_use_antialias(scale);
|
||||
self.surfaces.canvas(target_surface).save();
|
||||
self.surfaces.canvas(SurfaceId::Current).save();
|
||||
for (bounds, corners, transform) in clips.iter() {
|
||||
let mut total_matrix = Matrix::new_identity();
|
||||
total_matrix.pre_scale((scale, scale), None);
|
||||
@@ -1983,18 +1924,18 @@ impl RenderState {
|
||||
total_matrix.pre_concat(transform);
|
||||
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.canvas(SurfaceId::Current)
|
||||
.concat(&total_matrix);
|
||||
|
||||
if let Some(corners) = corners {
|
||||
let rrect = RRect::new_rect_radii(*bounds, corners);
|
||||
self.surfaces.canvas(target_surface).clip_rrect(
|
||||
self.surfaces.canvas(SurfaceId::Current).clip_rrect(
|
||||
rrect,
|
||||
skia::ClipOp::Intersect,
|
||||
antialias,
|
||||
);
|
||||
} else {
|
||||
self.surfaces.canvas(target_surface).clip_rect(
|
||||
self.surfaces.canvas(SurfaceId::Current).clip_rect(
|
||||
*bounds,
|
||||
skia::ClipOp::Intersect,
|
||||
antialias,
|
||||
@@ -2002,15 +1943,15 @@ impl RenderState {
|
||||
}
|
||||
|
||||
self.surfaces
|
||||
.canvas(target_surface)
|
||||
.canvas(SurfaceId::Current)
|
||||
.concat(&total_matrix.invert().unwrap_or_default());
|
||||
}
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::DropShadows, target_surface, None);
|
||||
self.surfaces.canvas(target_surface).restore();
|
||||
.draw_into(SurfaceId::DropShadows, SurfaceId::Current, None);
|
||||
self.surfaces.canvas(SurfaceId::Current).restore();
|
||||
} else {
|
||||
self.surfaces
|
||||
.draw_into(SurfaceId::DropShadows, target_surface, None);
|
||||
.draw_into(SurfaceId::DropShadows, SurfaceId::Current, None);
|
||||
}
|
||||
self.surfaces
|
||||
.canvas(SurfaceId::DropShadows)
|
||||
@@ -2022,16 +1963,10 @@ impl RenderState {
|
||||
tree: ShapesPoolRef,
|
||||
timestamp: i32,
|
||||
allow_stop: bool,
|
||||
export: bool,
|
||||
) -> Result<(bool, bool), String> {
|
||||
let mut iteration = 0;
|
||||
let mut is_empty = true;
|
||||
|
||||
let mut target_surface = SurfaceId::Current;
|
||||
if export {
|
||||
target_surface = SurfaceId::Export;
|
||||
}
|
||||
|
||||
while let Some(node_render_state) = self.pending_nodes.pop() {
|
||||
let node_id = node_render_state.id;
|
||||
let visited_children = node_render_state.visited_children;
|
||||
@@ -2057,7 +1992,7 @@ impl RenderState {
|
||||
if visited_children {
|
||||
// Skip render_shape_exit for flattened containers
|
||||
if !element.can_flatten() {
|
||||
self.render_shape_exit(element, visited_mask, clip_bounds, target_surface);
|
||||
self.render_shape_exit(element, visited_mask, clip_bounds);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -2080,14 +2015,11 @@ impl RenderState {
|
||||
|
||||
let has_effects = transformed_element.has_effects_that_extend_bounds();
|
||||
|
||||
let is_visible = export || if is_container || has_effects {
|
||||
let is_visible = if is_container || has_effects {
|
||||
let element_extrect =
|
||||
extrect.get_or_insert_with(|| transformed_element.extrect(tree, scale));
|
||||
element_extrect.intersects(self.render_area)
|
||||
} else if !has_effects {
|
||||
// Simple shape: selrect check is sufficient, skip expensive extrect
|
||||
let selrect = transformed_element.selrect();
|
||||
selrect.intersects(self.render_area)
|
||||
&& !transformed_element.visually_insignificant(scale, tree)
|
||||
} else {
|
||||
let selrect = transformed_element.selrect();
|
||||
selrect.intersects(self.render_area)
|
||||
@@ -2126,7 +2058,6 @@ impl RenderState {
|
||||
let translation = self
|
||||
.surfaces
|
||||
.get_render_context_translation(self.render_area, scale);
|
||||
|
||||
self.render_element_drop_shadows_and_composite(
|
||||
element,
|
||||
tree,
|
||||
@@ -2135,11 +2066,10 @@ impl RenderState {
|
||||
scale,
|
||||
translation,
|
||||
&node_render_state,
|
||||
target_surface,
|
||||
);
|
||||
}
|
||||
|
||||
self.render_shape_enter(element, mask, target_surface);
|
||||
self.render_shape_enter(element, mask);
|
||||
}
|
||||
|
||||
if !node_render_state.is_root() && self.focus_mode.is_active() {
|
||||
@@ -2166,7 +2096,6 @@ impl RenderState {
|
||||
scale,
|
||||
translation,
|
||||
&node_render_state,
|
||||
target_surface
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2181,14 +2110,13 @@ impl RenderState {
|
||||
None,
|
||||
None,
|
||||
None,
|
||||
target_surface,
|
||||
);
|
||||
|
||||
self.surfaces
|
||||
.canvas(SurfaceId::DropShadows)
|
||||
.clear(skia::Color::TRANSPARENT);
|
||||
} else if visited_children {
|
||||
self.apply_drawing_to_render_canvas(Some(element), target_surface);
|
||||
self.apply_drawing_to_render_canvas(Some(element));
|
||||
}
|
||||
|
||||
// Skip nested state updates for flattened containers
|
||||
@@ -2318,7 +2246,7 @@ impl RenderState {
|
||||
let tile_is_visible = self.tile_viewbox.is_visible(¤t_tile);
|
||||
let can_stop = allow_stop && !tile_is_visible;
|
||||
let (is_empty, early_return) =
|
||||
self.render_shape_tree_partial_uncached(tree, timestamp, can_stop, false)?;
|
||||
self.render_shape_tree_partial_uncached(tree, timestamp, can_stop)?;
|
||||
|
||||
if early_return {
|
||||
return Ok(());
|
||||
|
||||
@@ -51,15 +51,18 @@ fn draw_image_fill(
|
||||
canvas.clip_rect(container, skia::ClipOp::Intersect, antialias);
|
||||
}
|
||||
Type::Circle => {
|
||||
let mut oval_path = skia::Path::new();
|
||||
oval_path.add_oval(container, None);
|
||||
let oval_path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.add_oval(container, None, None);
|
||||
pb.detach()
|
||||
};
|
||||
canvas.clip_path(&oval_path, skia::ClipOp::Intersect, antialias);
|
||||
}
|
||||
shape_type @ (Type::Path(_) | Type::Bool(_)) => {
|
||||
if let Some(path) = shape_type.path() {
|
||||
if let Some(path_transform) = path_transform {
|
||||
canvas.clip_path(
|
||||
path.to_skia_path().transform(&path_transform),
|
||||
&path.to_skia_path().make_transform(&path_transform),
|
||||
skia::ClipOp::Intersect,
|
||||
antialias,
|
||||
);
|
||||
|
||||
@@ -104,38 +104,4 @@ impl GpuState {
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn create_surface_from_texture(
|
||||
&mut self,
|
||||
width: i32,
|
||||
height: i32,
|
||||
texture_id: u32
|
||||
) -> skia::Surface {
|
||||
let texture_info = TextureInfo {
|
||||
target: gl::TEXTURE_2D,
|
||||
id: texture_id,
|
||||
format: gl::RGBA8,
|
||||
protected: skia::gpu::Protected::No,
|
||||
};
|
||||
|
||||
let backend_texture = unsafe{
|
||||
gpu::backend_textures::make_gl(
|
||||
(width, height),
|
||||
gpu::Mipmapped::No,
|
||||
texture_info,
|
||||
String::from("export_texture"))
|
||||
};
|
||||
|
||||
gpu::surfaces::wrap_backend_texture(
|
||||
&mut self.context,
|
||||
&backend_texture,
|
||||
gpu::SurfaceOrigin::BottomLeft,
|
||||
None,
|
||||
skia::ColorType::RGBA8888,
|
||||
None,
|
||||
None,
|
||||
).unwrap()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,7 +24,11 @@ pub fn render_overlay(zoom: f32, canvas: &skia::Canvas, shape: &Shape, shapes: S
|
||||
cell.anchor + hv + vv,
|
||||
cell.anchor + vv,
|
||||
];
|
||||
let polygon = skia::Path::polygon(&points, true, None, None);
|
||||
let polygon = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.add_polygon(&points, true);
|
||||
pb.detach()
|
||||
};
|
||||
canvas.draw_path(&polygon, &paint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,8 +83,11 @@ fn draw_stroke_on_circle(
|
||||
if let Some(clip_op) = stroke.clip_op() {
|
||||
let layer_rec = skia::canvas::SaveLayerRec::default().paint(&paint);
|
||||
canvas.save_layer(&layer_rec);
|
||||
let mut clip_path = skia::Path::new();
|
||||
clip_path.add_oval(rect, None);
|
||||
let clip_path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.add_oval(rect, None, None);
|
||||
pb.detach()
|
||||
};
|
||||
canvas.clip_path(&clip_path, clip_op, antialias);
|
||||
canvas.draw_oval(stroke_rect, &paint);
|
||||
canvas.restore();
|
||||
@@ -153,8 +156,9 @@ fn draw_stroke_on_path(
|
||||
blur: Option<&ImageFilter>,
|
||||
antialias: bool,
|
||||
) {
|
||||
let mut skia_path = path.to_skia_path();
|
||||
skia_path.transform(path_transform.unwrap_or(&Matrix::default()));
|
||||
let skia_path = path
|
||||
.to_skia_path()
|
||||
.make_transform(path_transform.unwrap_or(&Matrix::default()));
|
||||
|
||||
let is_open = path.is_open();
|
||||
|
||||
@@ -174,15 +178,7 @@ fn draw_stroke_on_path(
|
||||
}
|
||||
}
|
||||
|
||||
handle_stroke_caps(
|
||||
&mut skia_path,
|
||||
stroke,
|
||||
canvas,
|
||||
is_open,
|
||||
paint,
|
||||
blur,
|
||||
antialias,
|
||||
);
|
||||
handle_stroke_caps(&skia_path, stroke, canvas, is_open, paint, blur, antialias);
|
||||
}
|
||||
|
||||
fn handle_stroke_cap(
|
||||
@@ -224,7 +220,7 @@ fn handle_stroke_cap(
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn handle_stroke_caps(
|
||||
path: &mut skia::Path,
|
||||
path: &skia::Path,
|
||||
stroke: &Stroke,
|
||||
canvas: &skia::Canvas,
|
||||
is_open: bool,
|
||||
@@ -232,8 +228,7 @@ fn handle_stroke_caps(
|
||||
blur: Option<&ImageFilter>,
|
||||
_antialias: bool,
|
||||
) {
|
||||
let mut points = vec![Point::default(); path.count_points()];
|
||||
path.get_points(&mut points);
|
||||
let mut points = path.points().to_vec();
|
||||
// Curves can have duplicated points, so let's remove consecutive duplicated points
|
||||
points.dedup();
|
||||
let c_points = points.len();
|
||||
@@ -304,13 +299,16 @@ fn draw_square_cap(
|
||||
let mut transformed_points = points;
|
||||
matrix.map_points(&mut transformed_points, &points);
|
||||
|
||||
let mut path = skia::Path::new();
|
||||
path.move_to(Point::new(center.x, center.y));
|
||||
path.move_to(transformed_points[0]);
|
||||
path.line_to(transformed_points[1]);
|
||||
path.line_to(transformed_points[2]);
|
||||
path.line_to(transformed_points[3]);
|
||||
path.close();
|
||||
let path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.move_to(Point::new(center.x, center.y));
|
||||
pb.move_to(transformed_points[0]);
|
||||
pb.line_to(transformed_points[1]);
|
||||
pb.line_to(transformed_points[2]);
|
||||
pb.line_to(transformed_points[3]);
|
||||
pb.close();
|
||||
pb.detach()
|
||||
};
|
||||
canvas.draw_path(&path, paint);
|
||||
}
|
||||
|
||||
@@ -338,13 +336,15 @@ fn draw_arrow_cap(
|
||||
let mut transformed_points = points;
|
||||
matrix.map_points(&mut transformed_points, &points);
|
||||
|
||||
let mut path = skia::Path::new();
|
||||
path.move_to(transformed_points[1]);
|
||||
path.line_to(transformed_points[0]);
|
||||
path.line_to(transformed_points[2]);
|
||||
path.move_to(Point::new(center.x, center.y));
|
||||
path.line_to(transformed_points[0]);
|
||||
|
||||
let path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.move_to(transformed_points[1]);
|
||||
pb.line_to(transformed_points[0]);
|
||||
pb.line_to(transformed_points[2]);
|
||||
pb.move_to(Point::new(center.x, center.y));
|
||||
pb.line_to(transformed_points[0]);
|
||||
pb.detach()
|
||||
};
|
||||
canvas.draw_path(&path, paint);
|
||||
}
|
||||
|
||||
@@ -372,12 +372,14 @@ fn draw_triangle_cap(
|
||||
let mut transformed_points = points;
|
||||
matrix.map_points(&mut transformed_points, &points);
|
||||
|
||||
let mut path = skia::Path::new();
|
||||
path.move_to(transformed_points[0]);
|
||||
path.line_to(transformed_points[1]);
|
||||
path.line_to(transformed_points[2]);
|
||||
path.close();
|
||||
|
||||
let path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.move_to(transformed_points[0]);
|
||||
pb.line_to(transformed_points[1]);
|
||||
pb.line_to(transformed_points[2]);
|
||||
pb.close();
|
||||
pb.detach()
|
||||
};
|
||||
canvas.draw_path(&path, paint);
|
||||
}
|
||||
|
||||
@@ -441,8 +443,7 @@ fn draw_image_stroke_in_container(
|
||||
shape_type @ (Type::Path(_) | Type::Bool(_)) => {
|
||||
if let Some(p) = shape_type.path() {
|
||||
canvas.save();
|
||||
let mut path = p.to_skia_path();
|
||||
path.transform(&path_transform.unwrap());
|
||||
let path = p.to_skia_path().make_transform(&path_transform.unwrap());
|
||||
let stroke_kind = stroke.render_kind(p.is_open());
|
||||
match stroke_kind {
|
||||
StrokeKind::Inner => {
|
||||
@@ -464,7 +465,7 @@ fn draw_image_stroke_in_container(
|
||||
canvas.draw_path(&path, &thin_paint);
|
||||
}
|
||||
handle_stroke_caps(
|
||||
&mut path,
|
||||
&path,
|
||||
stroke,
|
||||
canvas,
|
||||
is_open,
|
||||
@@ -504,8 +505,7 @@ fn draw_image_stroke_in_container(
|
||||
// Clear outer stroke for paths if necessary. When adding an outer stroke we need to empty the stroke added too in the inner area.
|
||||
if let Type::Path(p) = &shape.shape_type {
|
||||
if stroke.render_kind(p.is_open()) == StrokeKind::Outer {
|
||||
let mut path = p.to_skia_path();
|
||||
path.transform(&path_transform.unwrap());
|
||||
let path = p.to_skia_path().make_transform(&path_transform.unwrap());
|
||||
let mut clear_paint = skia::Paint::default();
|
||||
clear_paint.set_blend_mode(skia::BlendMode::Clear);
|
||||
clear_paint.set_anti_alias(antialias);
|
||||
|
||||
@@ -17,18 +17,17 @@ const TILE_SIZE_MULTIPLIER: i32 = 2;
|
||||
#[repr(u32)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
pub enum SurfaceId {
|
||||
Target = 0b000_0000_0001,
|
||||
Filter = 0b000_0000_0010,
|
||||
Cache = 0b000_0000_0100,
|
||||
Current = 0b000_0000_1000,
|
||||
Fills = 0b000_0001_0000,
|
||||
Strokes = 0b000_0010_0000,
|
||||
DropShadows = 0b000_0100_0000,
|
||||
InnerShadows = 0b000_1000_0000,
|
||||
TextDropShadows = 0b001_0000_0000,
|
||||
Export = 0b010_0000_0000,
|
||||
UI = 0b100_0000_0000,
|
||||
Debug = 0b100_0000_0001,
|
||||
Target = 0b00_0000_0001,
|
||||
Filter = 0b00_0000_0010,
|
||||
Cache = 0b00_0000_0100,
|
||||
Current = 0b00_0000_1000,
|
||||
Fills = 0b00_0001_0000,
|
||||
Strokes = 0b00_0010_0000,
|
||||
DropShadows = 0b00_0100_0000,
|
||||
InnerShadows = 0b00_1000_0000,
|
||||
TextDropShadows = 0b01_0000_0000,
|
||||
UI = 0b10_0000_0000,
|
||||
Debug = 0b10_0000_0001,
|
||||
}
|
||||
|
||||
pub struct Surfaces {
|
||||
@@ -53,15 +52,11 @@ pub struct Surfaces {
|
||||
// for drawing debug info.
|
||||
debug: skia::Surface,
|
||||
// for drawing tiles.
|
||||
export: skia::Surface,
|
||||
|
||||
tiles: TileTextureCache,
|
||||
sampling_options: skia::SamplingOptions,
|
||||
pub margins: skia::ISize,
|
||||
margins: skia::ISize,
|
||||
// Tracks which surfaces have content (dirty flag bitmask)
|
||||
dirty_surfaces: u32,
|
||||
|
||||
extra_tile_dims: skia::ISize,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
@@ -82,7 +77,6 @@ impl Surfaces {
|
||||
let filter = gpu_state.create_surface_with_isize("filter".to_string(), extra_tile_dims);
|
||||
let cache = gpu_state.create_surface_with_dimensions("cache".to_string(), width, height);
|
||||
let current = gpu_state.create_surface_with_isize("current".to_string(), extra_tile_dims);
|
||||
|
||||
let drop_shadows =
|
||||
gpu_state.create_surface_with_isize("drop_shadows".to_string(), extra_tile_dims);
|
||||
let inner_shadows =
|
||||
@@ -93,13 +87,10 @@ impl Surfaces {
|
||||
gpu_state.create_surface_with_isize("shape_fills".to_string(), extra_tile_dims);
|
||||
let shape_strokes =
|
||||
gpu_state.create_surface_with_isize("shape_strokes".to_string(), extra_tile_dims);
|
||||
let export =
|
||||
gpu_state.create_surface_with_isize("export".to_string(), extra_tile_dims);
|
||||
|
||||
let ui = gpu_state.create_surface_with_dimensions("ui".to_string(), width, height);
|
||||
let debug = gpu_state.create_surface_with_dimensions("debug".to_string(), width, height);
|
||||
|
||||
|
||||
let tiles = TileTextureCache::new();
|
||||
Surfaces {
|
||||
target,
|
||||
@@ -113,12 +104,10 @@ impl Surfaces {
|
||||
shape_strokes,
|
||||
ui,
|
||||
debug,
|
||||
export,
|
||||
tiles,
|
||||
sampling_options,
|
||||
margins,
|
||||
dirty_surfaces: 0,
|
||||
extra_tile_dims
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,9 +259,6 @@ impl Surfaces {
|
||||
if ids & SurfaceId::Debug as u32 != 0 {
|
||||
f(self.get_mut(SurfaceId::Debug));
|
||||
}
|
||||
if ids & SurfaceId::Export as u32 != 0 {
|
||||
f(self.get_mut(SurfaceId::Export));
|
||||
}
|
||||
performance::begin_measure!("apply_mut::flags");
|
||||
}
|
||||
|
||||
@@ -298,7 +284,6 @@ impl Surfaces {
|
||||
| SurfaceId::InnerShadows as u32
|
||||
| SurfaceId::TextDropShadows as u32;
|
||||
|
||||
|
||||
// Clear surfaces before updating transformations to remove residual content
|
||||
self.apply_mut(surface_ids, |s| {
|
||||
s.canvas().clear(skia::Color::TRANSPARENT);
|
||||
@@ -320,7 +305,7 @@ impl Surfaces {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self, id: SurfaceId) -> &mut skia::Surface {
|
||||
fn get_mut(&mut self, id: SurfaceId) -> &mut skia::Surface {
|
||||
match id {
|
||||
SurfaceId::Target => &mut self.target,
|
||||
SurfaceId::Filter => &mut self.filter,
|
||||
@@ -333,7 +318,6 @@ impl Surfaces {
|
||||
SurfaceId::Strokes => &mut self.shape_strokes,
|
||||
SurfaceId::Debug => &mut self.debug,
|
||||
SurfaceId::UI => &mut self.ui,
|
||||
SurfaceId::Export => &mut self.export
|
||||
}
|
||||
}
|
||||
|
||||
@@ -350,7 +334,6 @@ impl Surfaces {
|
||||
SurfaceId::Strokes => &self.shape_strokes,
|
||||
SurfaceId::Debug => &self.debug,
|
||||
SurfaceId::UI => &self.ui,
|
||||
SurfaceId::Export => &self.export
|
||||
}
|
||||
}
|
||||
|
||||
@@ -437,14 +420,12 @@ impl Surfaces {
|
||||
self.canvas(SurfaceId::TextDropShadows).restore_to_count(1);
|
||||
self.canvas(SurfaceId::Strokes).restore_to_count(1);
|
||||
self.canvas(SurfaceId::Current).restore_to_count(1);
|
||||
self.canvas(SurfaceId::Export).restore_to_count(1);
|
||||
self.apply_mut(
|
||||
SurfaceId::Fills as u32
|
||||
| SurfaceId::Strokes as u32
|
||||
| SurfaceId::Current as u32
|
||||
| SurfaceId::InnerShadows as u32
|
||||
| SurfaceId::TextDropShadows as u32
|
||||
| SurfaceId::Export as u32,
|
||||
| SurfaceId::TextDropShadows as u32,
|
||||
|s| {
|
||||
s.canvas().clear(color).reset_matrix();
|
||||
},
|
||||
@@ -476,9 +457,9 @@ impl Surfaces {
|
||||
);
|
||||
|
||||
let snapshot = self.current.image_snapshot();
|
||||
let mut direct_context = self.current.direct_context();
|
||||
let props = skia::image::RequiredProperties::default();
|
||||
let tile_image_opt = snapshot
|
||||
.make_subset(direct_context.as_mut(), rect)
|
||||
.make_subset(None, rect, props)
|
||||
.or_else(|| self.current.image_snapshot_with_bounds(rect));
|
||||
|
||||
if let Some(tile_image) = tile_image_opt {
|
||||
@@ -567,32 +548,6 @@ impl Surfaces {
|
||||
pub fn gc(&mut self) {
|
||||
self.tiles.gc();
|
||||
}
|
||||
|
||||
pub fn resize_export_surface(&mut self, scale: f32, rect: skia::Rect) {
|
||||
let target_w = (scale * rect.width()).ceil() as i32;
|
||||
let target_h = (scale * rect.height()).ceil() as i32;
|
||||
|
||||
let max_w = i32::max(self.extra_tile_dims.width, target_w);
|
||||
let max_h = i32::max(self.extra_tile_dims.height, target_h);
|
||||
|
||||
if max_w > self.extra_tile_dims.width || max_h > self.extra_tile_dims.height {
|
||||
self.extra_tile_dims = skia::ISize::new(max_w, max_h);
|
||||
self.drop_shadows =
|
||||
self.drop_shadows.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
self.inner_shadows =
|
||||
self.inner_shadows.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
self.text_drop_shadows =
|
||||
self.text_drop_shadows.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
self.text_drop_shadows =
|
||||
self.text_drop_shadows.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
self.shape_strokes =
|
||||
self.shape_strokes.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
self.shape_fills =
|
||||
self.shape_strokes.new_surface_with_dimensions((max_w, max_h)).unwrap();
|
||||
}
|
||||
|
||||
self.export = self.export.new_surface_with_dimensions((target_w, target_h)).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
pub struct TileTextureCache {
|
||||
@@ -670,5 +625,5 @@ impl TileTextureCache {
|
||||
for k in self.grid.keys() {
|
||||
self.removed.insert(*k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,11 +45,7 @@ fn render_cursor(
|
||||
paint.set_color(editor_state.theme.cursor_color);
|
||||
paint.set_anti_alias(true);
|
||||
|
||||
let shape_matrix = shape.get_matrix();
|
||||
canvas.save();
|
||||
canvas.concat(&shape_matrix);
|
||||
canvas.draw_rect(rect, &paint);
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
fn render_selection(
|
||||
@@ -69,14 +65,9 @@ fn render_selection(
|
||||
paint.set_blend_mode(BlendMode::Multiply);
|
||||
paint.set_color(editor_state.theme.selection_color);
|
||||
paint.set_anti_alias(true);
|
||||
|
||||
let shape_matrix = shape.get_matrix();
|
||||
canvas.save();
|
||||
canvas.concat(&shape_matrix);
|
||||
for rect in rects {
|
||||
canvas.draw_rect(rect, &paint);
|
||||
}
|
||||
canvas.restore();
|
||||
}
|
||||
|
||||
fn vertical_align_offset(
|
||||
@@ -108,6 +99,8 @@ fn calculate_cursor_rect(
|
||||
return None;
|
||||
}
|
||||
|
||||
let selrect = shape.selrect();
|
||||
|
||||
let mut y_offset = vertical_align_offset(shape, &layout_paragraphs);
|
||||
for (idx, laid_out_para) in layout_paragraphs.iter().enumerate() {
|
||||
if idx == cursor.paragraph {
|
||||
@@ -164,8 +157,8 @@ fn calculate_cursor_rect(
|
||||
};
|
||||
|
||||
return Some(Rect::from_xywh(
|
||||
cursor_x,
|
||||
y_offset,
|
||||
selrect.x() + cursor_x,
|
||||
selrect.y() + y_offset,
|
||||
editor_state.theme.cursor_width,
|
||||
cursor_height,
|
||||
));
|
||||
@@ -189,6 +182,7 @@ fn calculate_selection_rects(
|
||||
let paragraphs = text_content.paragraphs();
|
||||
let layout_paragraphs: Vec<_> = text_content.layout.paragraphs.iter().flatten().collect();
|
||||
|
||||
let selrect = shape.selrect();
|
||||
let mut y_offset = vertical_align_offset(shape, &layout_paragraphs);
|
||||
|
||||
for (para_idx, laid_out_para) in layout_paragraphs.iter().enumerate() {
|
||||
@@ -231,8 +225,8 @@ fn calculate_selection_rects(
|
||||
for text_box in text_boxes {
|
||||
let r = text_box.rect;
|
||||
rects.push(Rect::from_xywh(
|
||||
r.left(),
|
||||
y_offset + r.top(),
|
||||
selrect.x() + r.left(),
|
||||
selrect.y() + y_offset + r.top(),
|
||||
r.width(),
|
||||
r.height(),
|
||||
));
|
||||
|
||||
@@ -258,18 +258,6 @@ pub fn all_with_ancestors(
|
||||
}
|
||||
|
||||
impl Shape {
|
||||
pub fn get_relative_point(
|
||||
point: &Point,
|
||||
view_matrix: &Matrix,
|
||||
shape_matrix: &Matrix,
|
||||
) -> Option<Point> {
|
||||
let inv_view_matrix = view_matrix.invert()?;
|
||||
let inv_shape_matrix = shape_matrix.invert()?;
|
||||
let transform_matrix: Matrix = Matrix::concat(&inv_shape_matrix, &inv_view_matrix);
|
||||
let shape_relative_point = transform_matrix.map_point(*point);
|
||||
Some(shape_relative_point)
|
||||
}
|
||||
|
||||
pub fn new(id: Uuid) -> Self {
|
||||
Self {
|
||||
id,
|
||||
@@ -1348,7 +1336,7 @@ impl Shape {
|
||||
if let Some(path) = self.shape_type.path() {
|
||||
let mut skia_path = path.to_skia_path();
|
||||
if let Some(path_transform) = self.to_path_transform() {
|
||||
skia_path.transform(&path_transform);
|
||||
skia_path = skia_path.make_transform(&path_transform);
|
||||
}
|
||||
if let Some(svg_attrs) = &self.svg_attrs {
|
||||
if svg_attrs.fill_rule == FillRule::Evenodd {
|
||||
|
||||
@@ -51,10 +51,10 @@ impl Gradient {
|
||||
rect.left + self.end.0 * rect.width(),
|
||||
rect.top + self.end.1 * rect.height(),
|
||||
);
|
||||
skia::shader::Shader::linear_gradient(
|
||||
skia::gradient_shader::linear(
|
||||
(start, end),
|
||||
self.colors.as_slice(),
|
||||
self.offsets.as_slice(),
|
||||
Some(self.offsets.as_slice()),
|
||||
skia::TileMode::Clamp,
|
||||
None,
|
||||
None,
|
||||
@@ -83,11 +83,11 @@ impl Gradient {
|
||||
transform.pre_scale((self.width * rect.width() / rect.height(), 1.), None);
|
||||
transform.pre_translate((-center.x, -center.y));
|
||||
|
||||
skia::shader::Shader::radial_gradient(
|
||||
skia::gradient_shader::radial(
|
||||
center,
|
||||
distance,
|
||||
self.colors.as_slice(),
|
||||
self.offsets.as_slice(),
|
||||
Some(self.offsets.as_slice()),
|
||||
skia::TileMode::Clamp,
|
||||
None,
|
||||
Some(&transform),
|
||||
|
||||
@@ -29,40 +29,28 @@ impl Default for Path {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_verb(v: u8) -> skia::path::Verb {
|
||||
match v {
|
||||
0 => skia::path::Verb::Move,
|
||||
1 => skia::path::Verb::Line,
|
||||
2 => skia::path::Verb::Quad,
|
||||
3 => skia::path::Verb::Conic,
|
||||
4 => skia::path::Verb::Cubic,
|
||||
5 => skia::path::Verb::Close,
|
||||
_ => skia::path::Verb::Done,
|
||||
}
|
||||
}
|
||||
|
||||
impl Path {
|
||||
pub fn new(segments: Vec<Segment>) -> Self {
|
||||
let mut skia_path = skia::Path::new();
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
let mut start = None;
|
||||
|
||||
for segment in segments.iter() {
|
||||
let destination = match *segment {
|
||||
Segment::MoveTo(xy) => {
|
||||
start = Some(xy);
|
||||
skia_path.move_to(xy);
|
||||
pb.move_to(xy);
|
||||
None
|
||||
}
|
||||
Segment::LineTo(xy) => {
|
||||
skia_path.line_to(xy);
|
||||
pb.line_to(xy);
|
||||
Some(xy)
|
||||
}
|
||||
Segment::CurveTo((c1, c2, xy)) => {
|
||||
skia_path.cubic_to(c1, c2, xy);
|
||||
pb.cubic_to(c1, c2, xy);
|
||||
Some(xy)
|
||||
}
|
||||
Segment::Close => {
|
||||
skia_path.close();
|
||||
pb.close();
|
||||
None
|
||||
}
|
||||
};
|
||||
@@ -71,11 +59,12 @@ impl Path {
|
||||
if math::is_close_to(destination.0, start.0)
|
||||
&& math::is_close_to(destination.1, start.1)
|
||||
{
|
||||
skia_path.close();
|
||||
pb.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let skia_path = pb.detach();
|
||||
let open = subpaths::is_open_path(&segments);
|
||||
|
||||
Self {
|
||||
@@ -86,38 +75,31 @@ impl Path {
|
||||
}
|
||||
|
||||
pub fn from_skia_path(path: skia::Path) -> Self {
|
||||
let nv = path.count_verbs();
|
||||
let mut verbs = vec![0; nv];
|
||||
path.get_verbs(&mut verbs);
|
||||
|
||||
let np = path.count_points();
|
||||
let mut points = Vec::with_capacity(np);
|
||||
points.resize(np, skia::Point::default());
|
||||
path.get_points(&mut points);
|
||||
let verbs = path.verbs();
|
||||
let points = path.points();
|
||||
|
||||
let mut segments = Vec::new();
|
||||
|
||||
let mut current_point = 0;
|
||||
for verb in verbs {
|
||||
let verb = to_verb(verb);
|
||||
match verb {
|
||||
skia::path::Verb::Move => {
|
||||
skia::PathVerb::Move => {
|
||||
let p = points[current_point];
|
||||
segments.push(Segment::MoveTo((p.x, p.y)));
|
||||
current_point += 1;
|
||||
}
|
||||
skia::path::Verb::Line => {
|
||||
skia::PathVerb::Line => {
|
||||
let p = points[current_point];
|
||||
segments.push(Segment::LineTo((p.x, p.y)));
|
||||
current_point += 1;
|
||||
}
|
||||
skia::path::Verb::Quad => {
|
||||
skia::PathVerb::Quad => {
|
||||
let p1 = points[current_point];
|
||||
let p2 = points[current_point + 1];
|
||||
segments.push(Segment::CurveTo(((p1.x, p1.y), (p1.x, p1.y), (p2.x, p2.y))));
|
||||
current_point += 2;
|
||||
}
|
||||
skia::path::Verb::Conic => {
|
||||
skia::PathVerb::Conic => {
|
||||
// TODO: There is no way currently to access the conic weight
|
||||
// to transform this correctly
|
||||
let p1 = points[current_point];
|
||||
@@ -125,17 +107,14 @@ impl Path {
|
||||
segments.push(Segment::CurveTo(((p1.x, p1.y), (p1.x, p1.y), (p2.x, p2.y))));
|
||||
current_point += 2;
|
||||
}
|
||||
skia::path::Verb::Cubic => {
|
||||
skia::PathVerb::Cubic => {
|
||||
let p1 = points[current_point];
|
||||
let p2 = points[current_point + 1];
|
||||
let p3 = points[current_point + 2];
|
||||
segments.push(Segment::CurveTo(((p1.x, p1.y), (p2.x, p2.y), (p3.x, p3.y))));
|
||||
current_point += 3;
|
||||
}
|
||||
skia::path::Verb::Close => {
|
||||
segments.push(Segment::Close);
|
||||
}
|
||||
skia::path::Verb::Done => {
|
||||
skia::PathVerb::Close => {
|
||||
segments.push(Segment::Close);
|
||||
}
|
||||
}
|
||||
@@ -184,7 +163,7 @@ impl Path {
|
||||
_ => {}
|
||||
});
|
||||
|
||||
self.skia_path.transform(mtx);
|
||||
self.skia_path = self.skia_path.make_transform(mtx);
|
||||
}
|
||||
|
||||
pub fn segments(&self) -> &Vec<Segment> {
|
||||
|
||||
@@ -225,13 +225,16 @@ impl Stroke {
|
||||
if self.style != StrokeStyle::Solid {
|
||||
let path_effect = match self.style {
|
||||
StrokeStyle::Dotted => {
|
||||
let mut circle_path = skia::Path::new();
|
||||
let width = match self.kind {
|
||||
StrokeKind::Inner => self.width,
|
||||
StrokeKind::Center => self.width / 2.0,
|
||||
StrokeKind::Outer => self.width,
|
||||
};
|
||||
circle_path.add_circle((0.0, 0.0), width, None);
|
||||
let circle_path = {
|
||||
let mut pb = skia::PathBuilder::new();
|
||||
pb.add_circle((0.0, 0.0), width, None);
|
||||
pb.detach()
|
||||
};
|
||||
let advance = self.width + 5.0;
|
||||
skia::PathEffect::path_1d(
|
||||
&circle_path,
|
||||
|
||||
@@ -112,15 +112,12 @@ impl TextContentSize {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct TextPositionWithAffinity {
|
||||
#[allow(dead_code)]
|
||||
pub position_with_affinity: PositionWithAffinity,
|
||||
pub paragraph: i32,
|
||||
#[allow(dead_code)]
|
||||
pub span: i32,
|
||||
#[allow(dead_code)]
|
||||
pub span_relative_offset: i32,
|
||||
pub offset: i32,
|
||||
}
|
||||
|
||||
@@ -129,14 +126,12 @@ impl TextPositionWithAffinity {
|
||||
position_with_affinity: PositionWithAffinity,
|
||||
paragraph: i32,
|
||||
span: i32,
|
||||
span_relative_offset: i32,
|
||||
offset: i32,
|
||||
) -> Self {
|
||||
Self {
|
||||
position_with_affinity,
|
||||
paragraph,
|
||||
span,
|
||||
span_relative_offset,
|
||||
offset,
|
||||
}
|
||||
}
|
||||
@@ -426,10 +421,7 @@ impl TextContent {
|
||||
self.bounds = Rect::from_ltrb(p1.x, p1.y, p2.x, p2.y);
|
||||
}
|
||||
|
||||
pub fn get_caret_position_from_shape_coords(
|
||||
&self,
|
||||
point: &Point,
|
||||
) -> Option<TextPositionWithAffinity> {
|
||||
pub fn get_caret_position_at(&self, point: &Point) -> Option<TextPositionWithAffinity> {
|
||||
let mut offset_y = 0.0;
|
||||
let layout_paragraphs = self.layout.paragraphs.iter().flatten();
|
||||
|
||||
@@ -495,7 +487,6 @@ impl TextContent {
|
||||
paragraph_index,
|
||||
span_index,
|
||||
span_offset,
|
||||
position_with_affinity.position,
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -518,23 +509,12 @@ impl TextContent {
|
||||
0, // paragraph 0
|
||||
0, // span 0
|
||||
0, // offset 0
|
||||
0,
|
||||
));
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_caret_position_from_screen_coords(
|
||||
&self,
|
||||
point: &Point,
|
||||
view_matrix: &Matrix,
|
||||
shape_matrix: &Matrix,
|
||||
) -> Option<TextPositionWithAffinity> {
|
||||
let shape_rel_point = Shape::get_relative_point(point, view_matrix, shape_matrix)?;
|
||||
self.get_caret_position_from_shape_coords(&shape_rel_point)
|
||||
}
|
||||
|
||||
/// Builds the ParagraphBuilders necessary to render
|
||||
/// this text.
|
||||
pub fn paragraph_builder_group_from_text(
|
||||
|
||||
@@ -101,7 +101,6 @@ impl TextPaths {
|
||||
if let Some((text_blob_path, text_blob_bounds)) =
|
||||
Self::get_text_blob_path(span_text, font, blob_offset_x, blob_offset_y)
|
||||
{
|
||||
let mut text_path = text_blob_path.clone();
|
||||
let text_width = font.measure_text(span_text, None).0;
|
||||
|
||||
let decoration = style_metric.text_style.decoration();
|
||||
@@ -111,16 +110,20 @@ impl TextPaths {
|
||||
let blob_top = blob_offset_y;
|
||||
let blob_height = text_blob_bounds.height();
|
||||
|
||||
if let Some(decoration_rect) = self.calculate_text_decoration_rect(
|
||||
decoration.ty,
|
||||
font_metrics,
|
||||
blob_left,
|
||||
blob_top,
|
||||
text_width,
|
||||
blob_height,
|
||||
) {
|
||||
text_path.add_rect(decoration_rect, None);
|
||||
}
|
||||
let text_path = {
|
||||
let mut pb = skia::PathBuilder::new_path(&text_blob_path);
|
||||
if let Some(decoration_rect) = self.calculate_text_decoration_rect(
|
||||
decoration.ty,
|
||||
font_metrics,
|
||||
blob_left,
|
||||
blob_top,
|
||||
text_width,
|
||||
blob_height,
|
||||
) {
|
||||
pb.add_rect(decoration_rect, None, None);
|
||||
}
|
||||
pb.detach()
|
||||
};
|
||||
|
||||
let mut paint = style_metric.text_style.foreground();
|
||||
paint.set_anti_alias(antialias);
|
||||
|
||||
@@ -99,11 +99,6 @@ impl State {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn render_shape_pixels(&mut self, id: &Uuid, scale: f32, timestamp: i32) -> Result<(Vec<u8>, i32, i32), String> {
|
||||
self.render_state
|
||||
.render_shape_pixels(id, &self.shapes, scale, timestamp)
|
||||
}
|
||||
|
||||
pub fn start_render_loop(&mut self, timestamp: i32) -> Result<(), String> {
|
||||
// If zoom changed, we MUST rebuild the tile index before using it.
|
||||
// Otherwise, the index will have tiles from the old zoom level, causing visible
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::shapes::{TextContent, TextPositionWithAffinity};
|
||||
use crate::shapes::TextPositionWithAffinity;
|
||||
use crate::uuid::Uuid;
|
||||
use skia_safe::{
|
||||
textlayout::{Affinity, PositionWithAffinity},
|
||||
Color,
|
||||
};
|
||||
use skia_safe::Color;
|
||||
|
||||
/// Cursor position within text content.
|
||||
/// Uses character offsets for precise positioning.
|
||||
@@ -125,9 +122,6 @@ pub struct TextEditorState {
|
||||
pub theme: TextEditorTheme,
|
||||
pub selection: TextSelection,
|
||||
pub is_active: bool,
|
||||
// This property indicates that we've started
|
||||
// selecting something with the pointer.
|
||||
pub is_pointer_selection_active: bool,
|
||||
pub active_shape_id: Option<Uuid>,
|
||||
pub cursor_visible: bool,
|
||||
pub last_blink_time: f64,
|
||||
@@ -144,7 +138,6 @@ impl TextEditorState {
|
||||
},
|
||||
selection: TextSelection::new(),
|
||||
is_active: false,
|
||||
is_pointer_selection_active: false,
|
||||
active_shape_id: None,
|
||||
cursor_visible: true,
|
||||
last_blink_time: 0.0,
|
||||
@@ -158,7 +151,6 @@ impl TextEditorState {
|
||||
self.cursor_visible = true;
|
||||
self.last_blink_time = 0.0;
|
||||
self.selection = TextSelection::new();
|
||||
self.is_pointer_selection_active = false;
|
||||
self.pending_events.clear();
|
||||
}
|
||||
|
||||
@@ -166,65 +158,7 @@ impl TextEditorState {
|
||||
self.is_active = false;
|
||||
self.active_shape_id = None;
|
||||
self.cursor_visible = false;
|
||||
self.is_pointer_selection_active = false;
|
||||
self.pending_events.clear();
|
||||
self.reset_blink();
|
||||
}
|
||||
|
||||
pub fn start_pointer_selection(&mut self) -> bool {
|
||||
if self.is_pointer_selection_active {
|
||||
return false;
|
||||
}
|
||||
self.is_pointer_selection_active = true;
|
||||
true
|
||||
}
|
||||
|
||||
pub fn stop_pointer_selection(&mut self) -> bool {
|
||||
if !self.is_pointer_selection_active {
|
||||
return false;
|
||||
}
|
||||
self.is_pointer_selection_active = false;
|
||||
true
|
||||
}
|
||||
|
||||
pub fn select_all(&mut self, content: &TextContent) -> bool {
|
||||
self.is_pointer_selection_active = false;
|
||||
self.set_caret_from_position(TextPositionWithAffinity::new(
|
||||
PositionWithAffinity {
|
||||
position: 0,
|
||||
affinity: Affinity::Downstream,
|
||||
},
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
));
|
||||
let num_paragraphs = (content.paragraphs().len() - 1) as i32;
|
||||
let Some(last_paragraph) = content.paragraphs().last() else {
|
||||
return false;
|
||||
};
|
||||
let num_spans = (last_paragraph.children().len() - 1) as i32;
|
||||
let Some(last_text_span) = last_paragraph.children().last() else {
|
||||
return false;
|
||||
};
|
||||
let mut offset = 0;
|
||||
for span in last_paragraph.children() {
|
||||
offset += span.text.len();
|
||||
}
|
||||
self.extend_selection_from_position(TextPositionWithAffinity::new(
|
||||
PositionWithAffinity {
|
||||
position: offset as i32,
|
||||
affinity: Affinity::Upstream,
|
||||
},
|
||||
num_paragraphs,
|
||||
num_spans,
|
||||
last_text_span.text.len() as i32,
|
||||
offset as i32,
|
||||
));
|
||||
self.reset_blink();
|
||||
self.push_event(crate::state::EditorEvent::SelectionChanged);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn set_caret_from_position(&mut self, position: TextPositionWithAffinity) {
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
use macros::ToJs;
|
||||
|
||||
use super::{fills::RawFillData, fonts::RawFontStyle};
|
||||
|
||||
use crate::math::{Matrix, Point};
|
||||
use crate::mem::{self, SerializableResult};
|
||||
use crate::shapes::{
|
||||
self, GrowType, Shape, TextAlign, TextDecoration, TextDirection, TextTransform, Type,
|
||||
};
|
||||
use crate::utils::{uuid_from_u32, uuid_from_u32_quartet};
|
||||
use crate::{with_current_shape, with_current_shape_mut, with_state, with_state_mut, STATE};
|
||||
use crate::{
|
||||
with_current_shape, with_current_shape_mut, with_state, with_state_mut,
|
||||
with_state_mut_current_shape, STATE,
|
||||
};
|
||||
|
||||
const RAW_SPAN_DATA_SIZE: usize = std::mem::size_of::<RawTextSpan>();
|
||||
const RAW_PARAGRAPH_DATA_SIZE: usize = std::mem::size_of::<RawParagraphData>();
|
||||
@@ -385,6 +388,32 @@ pub extern "C" fn update_shape_text_layout_for(a: u32, b: u32, c: u32, d: u32) {
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn get_caret_position_at(x: f32, y: f32) -> i32 {
|
||||
with_state_mut_current_shape!(state, |shape: &Shape| {
|
||||
if let Type::Text(text_content) = &shape.shape_type {
|
||||
let mut matrix = Matrix::new_identity();
|
||||
let shape_matrix = shape.get_concatenated_matrix(&state.shapes);
|
||||
let view_matrix = state.render_state.viewbox.get_matrix();
|
||||
if let Some(inv_view_matrix) = view_matrix.invert() {
|
||||
matrix.post_concat(&inv_view_matrix);
|
||||
matrix.post_concat(&shape_matrix);
|
||||
|
||||
let mapped_point = matrix.map_point(Point::new(x, y));
|
||||
|
||||
if let Some(position_with_affinity) =
|
||||
text_content.get_caret_position_at(&mapped_point)
|
||||
{
|
||||
return position_with_affinity.position_with_affinity.position;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
panic!("Trying to get caret position of a shape that it's not a text shape");
|
||||
}
|
||||
});
|
||||
-1
|
||||
}
|
||||
|
||||
const RAW_POSITION_DATA_SIZE: usize = size_of::<shapes::PositionData>();
|
||||
|
||||
impl From<[u8; RAW_POSITION_DATA_SIZE]> for shapes::PositionData {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
use macros::ToJs;
|
||||
|
||||
use crate::math::{Matrix, Point, Rect};
|
||||
use crate::mem;
|
||||
use crate::shapes::{Paragraph, Shape, TextContent, Type, VerticalAlign};
|
||||
@@ -5,7 +7,6 @@ use crate::state::{TextCursor, TextSelection};
|
||||
use crate::utils::uuid_from_u32_quartet;
|
||||
use crate::utils::uuid_to_u32_quartet;
|
||||
use crate::{with_state, with_state_mut, STATE};
|
||||
use macros::ToJs;
|
||||
|
||||
#[derive(PartialEq, ToJs)]
|
||||
#[repr(u8)]
|
||||
@@ -53,17 +54,6 @@ pub extern "C" fn text_editor_is_active() -> bool {
|
||||
with_state!(state, { state.text_editor_state.is_active })
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_is_active_with_id(a: u32, b: u32, c: u32, d: u32) -> bool {
|
||||
with_state!(state, {
|
||||
let shape_id = uuid_from_u32_quartet(a, b, c, d);
|
||||
let Some(active_shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return false;
|
||||
};
|
||||
state.text_editor_state.is_active && active_shape_id == shape_id
|
||||
})
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_get_active_shape_id(buffer_ptr: *mut u32) {
|
||||
with_state!(state, {
|
||||
@@ -80,25 +70,45 @@ pub extern "C" fn text_editor_get_active_shape_id(buffer_ptr: *mut u32) {
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_select_all() -> bool {
|
||||
pub extern "C" fn text_editor_select_all() {
|
||||
with_state_mut!(state, {
|
||||
if !state.text_editor_state.is_active {
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return false;
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return false;
|
||||
return;
|
||||
};
|
||||
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return false;
|
||||
return;
|
||||
};
|
||||
state.text_editor_state.select_all(text_content)
|
||||
})
|
||||
|
||||
let paragraphs = text_content.paragraphs();
|
||||
if paragraphs.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let last_para_idx = paragraphs.len() - 1;
|
||||
let last_para = ¶graphs[last_para_idx];
|
||||
let total_chars: usize = last_para
|
||||
.children()
|
||||
.iter()
|
||||
.map(|span| span.text.chars().count())
|
||||
.sum();
|
||||
|
||||
use crate::state::TextCursor;
|
||||
state.text_editor_state.selection.anchor = TextCursor::new(0, 0);
|
||||
state.text_editor_state.selection.focus = TextCursor::new(last_para_idx, total_chars);
|
||||
state.text_editor_state.reset_blink();
|
||||
state
|
||||
.text_editor_state
|
||||
.push_event(crate::state::EditorEvent::SelectionChanged);
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -110,105 +120,6 @@ pub extern "C" fn text_editor_poll_event() -> u8 {
|
||||
// SELECTION MANAGEMENT
|
||||
// ============================================================================
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_pointer_down(x: f32, y: f32) {
|
||||
with_state_mut!(state, {
|
||||
if !state.text_editor_state.is_active {
|
||||
return;
|
||||
}
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return;
|
||||
};
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return;
|
||||
};
|
||||
let point = Point::new(x, y);
|
||||
let view_matrix: Matrix = state.render_state.viewbox.get_matrix();
|
||||
let shape_matrix = shape.get_matrix();
|
||||
state.text_editor_state.start_pointer_selection();
|
||||
if let Some(position) =
|
||||
text_content.get_caret_position_from_screen_coords(&point, &view_matrix, &shape_matrix)
|
||||
{
|
||||
state.text_editor_state.set_caret_from_position(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_pointer_move(x: f32, y: f32) {
|
||||
with_state_mut!(state, {
|
||||
if !state.text_editor_state.is_active {
|
||||
return;
|
||||
}
|
||||
let view_matrix: Matrix = state.render_state.viewbox.get_matrix();
|
||||
let point = Point::new(x, y);
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return;
|
||||
};
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
let shape_matrix = shape.get_matrix();
|
||||
let Some(_shape_rel_point) = Shape::get_relative_point(&point, &view_matrix, &shape_matrix)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if !state.text_editor_state.is_pointer_selection_active {
|
||||
return;
|
||||
}
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return;
|
||||
};
|
||||
|
||||
if let Some(position) =
|
||||
text_content.get_caret_position_from_screen_coords(&point, &view_matrix, &shape_matrix)
|
||||
{
|
||||
state
|
||||
.text_editor_state
|
||||
.extend_selection_from_position(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_pointer_up(x: f32, y: f32) {
|
||||
with_state_mut!(state, {
|
||||
if !state.text_editor_state.is_active {
|
||||
return;
|
||||
}
|
||||
let view_matrix: Matrix = state.render_state.viewbox.get_matrix();
|
||||
let point = Point::new(x, y);
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return;
|
||||
};
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
let shape_matrix = shape.get_matrix();
|
||||
let Some(_shape_rel_point) = Shape::get_relative_point(&point, &view_matrix, &shape_matrix)
|
||||
else {
|
||||
return;
|
||||
};
|
||||
if !state.text_editor_state.is_pointer_selection_active {
|
||||
return;
|
||||
}
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
return;
|
||||
};
|
||||
if let Some(position) =
|
||||
text_content.get_caret_position_from_screen_coords(&point, &view_matrix, &shape_matrix)
|
||||
{
|
||||
state
|
||||
.text_editor_state
|
||||
.extend_selection_from_position(position);
|
||||
}
|
||||
state.text_editor_state.stop_pointer_selection();
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_set_cursor_from_point(x: f32, y: f32) {
|
||||
with_state_mut!(state, {
|
||||
@@ -216,26 +127,144 @@ pub extern "C" fn text_editor_set_cursor_from_point(x: f32, y: f32) {
|
||||
return;
|
||||
}
|
||||
|
||||
let view_matrix: Matrix = state.render_state.viewbox.get_matrix();
|
||||
let point = Point::new(x, y);
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return;
|
||||
};
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
|
||||
let (shape_matrix, view_matrix, selrect, vertical_align) = {
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
(
|
||||
shape.get_concatenated_matrix(&state.shapes),
|
||||
state.render_state.viewbox.get_matrix(),
|
||||
shape.selrect(),
|
||||
shape.vertical_align(),
|
||||
)
|
||||
};
|
||||
|
||||
let Some(inv_view_matrix) = view_matrix.invert() else {
|
||||
return;
|
||||
};
|
||||
let shape_matrix = shape.get_matrix();
|
||||
let Type::Text(text_content) = &shape.shape_type else {
|
||||
|
||||
let Some(inv_shape_matrix) = shape_matrix.invert() else {
|
||||
return;
|
||||
};
|
||||
if let Some(position) =
|
||||
text_content.get_caret_position_from_screen_coords(&point, &view_matrix, &shape_matrix)
|
||||
{
|
||||
|
||||
let mut matrix = Matrix::new_identity();
|
||||
matrix.post_concat(&inv_view_matrix);
|
||||
matrix.post_concat(&inv_shape_matrix);
|
||||
|
||||
let mapped_point = matrix.map_point(Point::new(x, y));
|
||||
|
||||
let Some(shape) = state.shapes.get_mut(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Type::Text(text_content) = &mut shape.shape_type else {
|
||||
return;
|
||||
};
|
||||
|
||||
if text_content.layout.paragraphs.is_empty() && !text_content.paragraphs().is_empty() {
|
||||
let bounds = text_content.bounds;
|
||||
text_content.update_layout(bounds);
|
||||
}
|
||||
|
||||
// Calculate vertical alignment offset (same as in render/text_editor.rs)
|
||||
let layout_paragraphs: Vec<_> = text_content.layout.paragraphs.iter().flatten().collect();
|
||||
let total_height: f32 = layout_paragraphs.iter().map(|p| p.height()).sum();
|
||||
let vertical_offset = match vertical_align {
|
||||
crate::shapes::VerticalAlign::Center => (selrect.height() - total_height) / 2.0,
|
||||
crate::shapes::VerticalAlign::Bottom => selrect.height() - total_height,
|
||||
_ => 0.0,
|
||||
};
|
||||
|
||||
// Adjust point: subtract selrect offset and vertical alignment
|
||||
// The text layout expects coordinates where (0, 0) is the top-left of the text content
|
||||
let adjusted_point = Point::new(
|
||||
mapped_point.x - selrect.x(),
|
||||
mapped_point.y - selrect.y() - vertical_offset,
|
||||
);
|
||||
|
||||
if let Some(position) = text_content.get_caret_position_at(&adjusted_point) {
|
||||
state.text_editor_state.set_caret_from_position(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn text_editor_extend_selection_to_point(x: f32, y: f32) {
|
||||
with_state_mut!(state, {
|
||||
if !state.text_editor_state.is_active {
|
||||
return;
|
||||
}
|
||||
|
||||
let Some(shape_id) = state.text_editor_state.active_shape_id else {
|
||||
return;
|
||||
};
|
||||
|
||||
let (shape_matrix, view_matrix, selrect, vertical_align) = {
|
||||
let Some(shape) = state.shapes.get(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
(
|
||||
shape.get_concatenated_matrix(&state.shapes),
|
||||
state.render_state.viewbox.get_matrix(),
|
||||
shape.selrect(),
|
||||
shape.vertical_align(),
|
||||
)
|
||||
};
|
||||
|
||||
let Some(inv_view_matrix) = view_matrix.invert() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(inv_shape_matrix) = shape_matrix.invert() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let mut matrix = Matrix::new_identity();
|
||||
matrix.post_concat(&inv_view_matrix);
|
||||
matrix.post_concat(&inv_shape_matrix);
|
||||
|
||||
let mapped_point = matrix.map_point(Point::new(x, y));
|
||||
|
||||
let Some(shape) = state.shapes.get_mut(&shape_id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let Type::Text(text_content) = &mut shape.shape_type else {
|
||||
return;
|
||||
};
|
||||
|
||||
if text_content.layout.paragraphs.is_empty() && !text_content.paragraphs().is_empty() {
|
||||
let bounds = text_content.bounds;
|
||||
text_content.update_layout(bounds);
|
||||
}
|
||||
|
||||
// Calculate vertical alignment offset (same as in render/text_editor.rs)
|
||||
let layout_paragraphs: Vec<_> = text_content.layout.paragraphs.iter().flatten().collect();
|
||||
let total_height: f32 = layout_paragraphs.iter().map(|p| p.height()).sum();
|
||||
let vertical_offset = match vertical_align {
|
||||
crate::shapes::VerticalAlign::Center => (selrect.height() - total_height) / 2.0,
|
||||
crate::shapes::VerticalAlign::Bottom => selrect.height() - total_height,
|
||||
_ => 0.0,
|
||||
};
|
||||
|
||||
// Adjust point: subtract selrect offset and vertical alignment
|
||||
let adjusted_point = Point::new(
|
||||
mapped_point.x - selrect.x(),
|
||||
mapped_point.y - selrect.y() - vertical_offset,
|
||||
);
|
||||
|
||||
if let Some(position) = text_content.get_caret_position_at(&adjusted_point) {
|
||||
state
|
||||
.text_editor_state
|
||||
.extend_selection_from_position(position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// TEXT OPERATIONS
|
||||
// ============================================================================
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
set -x
|
||||
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.87.0/skia-binaries-e551f334ad5cbdf43abf-x86_64-unknown-linux-gnu-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
export SKIA_BINARIES_URL=${SKIA_BINARIES_URL:-"https://github.com/penpot/skia-binaries/releases/download/0.93.1/skia-binaries-319323662b1685a112f5-x86_64-unknown-linux-gnu-gl-svg-textlayout-binary-cache-webp.tar.gz"}
|
||||
export CARGO_BUILD_TARGET=${CARGO_BUILD_TARGET:-"x86_64-unknown-linux-gnu"};
|
||||
|
||||
_SCRIPT_DIR=$(dirname $0);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env bash
|
||||
_SCRIPT_DIR=$(dirname $0);
|
||||
|
||||
export SKIA_BINARIES_URL="https://github.com/penpot/skia-binaries/releases/download/0.87.0/skia-binaries-e551f334ad5cbdf43abf-x86_64-unknown-linux-gnu-gl-svg-textlayout-binary-cache-webp.tar.gz"
|
||||
export SKIA_BINARIES_URL="https://github.com/penpot/skia-binaries/releases/download/0.93.1/skia-binaries-319323662b1685a112f5-x86_64-unknown-linux-gnu-gl-svg-textlayout-binary-cache-webp.tar.gz"
|
||||
|
||||
pushd $_SCRIPT_DIR;
|
||||
cargo watch -x "test --bin render_wasm -- --show-output"
|
||||
|
||||
Reference in New Issue
Block a user