mirror of
https://github.com/penpot/penpot.git
synced 2025-06-03 08:31:39 +02:00
✨ Text grow width/height
This commit is contained in:
parent
6b300d516b
commit
568af52ebc
13 changed files with 381 additions and 106 deletions
|
@ -11,6 +11,7 @@
|
||||||
[app.common.data :as d]
|
[app.common.data :as d]
|
||||||
[app.common.data.macros :as dm]
|
[app.common.data.macros :as dm]
|
||||||
[app.common.files.helpers :as cfh]
|
[app.common.files.helpers :as cfh]
|
||||||
|
[app.common.geom.matrix :as gmt]
|
||||||
[app.common.geom.point :as gpt]
|
[app.common.geom.point :as gpt]
|
||||||
[app.common.geom.shapes :as gsh]
|
[app.common.geom.shapes :as gsh]
|
||||||
[app.common.math :as mth]
|
[app.common.math :as mth]
|
||||||
|
@ -28,6 +29,7 @@
|
||||||
[app.main.features :as features]
|
[app.main.features :as features]
|
||||||
[app.main.fonts :as fonts]
|
[app.main.fonts :as fonts]
|
||||||
[app.main.router :as rt]
|
[app.main.router :as rt]
|
||||||
|
[app.render-wasm.api :as wasm.api]
|
||||||
[app.util.text-editor :as ted]
|
[app.util.text-editor :as ted]
|
||||||
[app.util.text.content.styles :as styles]
|
[app.util.text.content.styles :as styles]
|
||||||
[app.util.timers :as ts]
|
[app.util.timers :as ts]
|
||||||
|
@ -47,6 +49,45 @@
|
||||||
(declare v2-update-text-shape-content)
|
(declare v2-update-text-shape-content)
|
||||||
(declare v2-update-text-editor-styles)
|
(declare v2-update-text-editor-styles)
|
||||||
|
|
||||||
|
(defn resize-wasm-text-modifiers
|
||||||
|
([shape]
|
||||||
|
(resize-wasm-text-modifiers shape (:content shape)))
|
||||||
|
|
||||||
|
([{:keys [id points selrect] :as shape} content]
|
||||||
|
(wasm.api/use-shape id)
|
||||||
|
(wasm.api/set-shape-text-content content)
|
||||||
|
(let [dimension (wasm.api/text-dimensions)
|
||||||
|
resize-v
|
||||||
|
(gpt/point
|
||||||
|
(/ (:width dimension) (-> selrect :width))
|
||||||
|
(/ (:height dimension) (-> selrect :height)))
|
||||||
|
|
||||||
|
origin (first points)]
|
||||||
|
{id
|
||||||
|
{:modifiers
|
||||||
|
(ctm/resize-modifiers
|
||||||
|
resize-v
|
||||||
|
origin
|
||||||
|
(:transform shape (gmt/matrix))
|
||||||
|
(:transform-inverse shape (gmt/matrix)))}})))
|
||||||
|
|
||||||
|
(defn resize-wasm-text
|
||||||
|
[id]
|
||||||
|
(ptk/reify ::resize-wasm-text
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ state _]
|
||||||
|
(let [objects (dsh/lookup-page-objects state)
|
||||||
|
shape (get objects id)]
|
||||||
|
(rx/of (dwm/apply-wasm-modifiers (resize-wasm-text-modifiers shape)))))))
|
||||||
|
|
||||||
|
(defn resize-wasm-text-all
|
||||||
|
[ids]
|
||||||
|
(ptk/reify ::resize-wasm-text-all
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ _ _]
|
||||||
|
(->> (rx/from ids)
|
||||||
|
(rx/map resize-wasm-text)))))
|
||||||
|
|
||||||
;; -- Editor
|
;; -- Editor
|
||||||
|
|
||||||
(defn update-editor
|
(defn update-editor
|
||||||
|
@ -98,28 +139,50 @@
|
||||||
|
|
||||||
new-shape? (nil? (:content shape))]
|
new-shape? (nil? (:content shape))]
|
||||||
(if (ted/content-has-text? content)
|
(if (ted/content-has-text? content)
|
||||||
(let [content (d/merge (ted/export-content content)
|
(if (features/active-feature? state "render-wasm/v1")
|
||||||
(dissoc (:content shape) :children))
|
(let [content (d/merge (ted/export-content content)
|
||||||
modifiers (get-in state [:workspace-text-modifier id])]
|
(dissoc (:content shape) :children))]
|
||||||
(rx/merge
|
(rx/merge
|
||||||
(rx/of (update-editor-state shape nil))
|
(rx/of (update-editor-state shape nil))
|
||||||
(when (and (not= content (:content shape))
|
(when (and (not= content (:content shape))
|
||||||
(some? (:current-page-id state))
|
(some? (:current-page-id state))
|
||||||
(some? shape))
|
(some? shape))
|
||||||
(rx/of
|
(rx/of
|
||||||
(dwsh/update-shapes
|
(dwsh/update-shapes
|
||||||
[id]
|
[id]
|
||||||
(fn [shape]
|
(fn [shape]
|
||||||
(let [{:keys [width height position-data]} modifiers]
|
|
||||||
(-> shape
|
(-> shape
|
||||||
(assoc :content content)
|
(assoc :content content)
|
||||||
(cond-> position-data
|
|
||||||
(assoc :position-data position-data))
|
|
||||||
(cond-> (and update-name? (some? name))
|
(cond-> (and update-name? (some? name))
|
||||||
(assoc :name name))
|
(assoc :name name))))
|
||||||
(cond-> (or (some? width) (some? height))
|
{:undo-group (when new-shape? id)})
|
||||||
(gsh/transform-shape (ctm/change-size shape width height))))))
|
|
||||||
{:undo-group (when new-shape? id)})))))
|
(dwm/apply-wasm-modifiers
|
||||||
|
(resize-wasm-text-modifiers shape content)
|
||||||
|
{:undo-group (when new-shape? id)})))))
|
||||||
|
|
||||||
|
(let [content (d/merge (ted/export-content content)
|
||||||
|
(dissoc (:content shape) :children))
|
||||||
|
modifiers (get-in state [:workspace-text-modifier id])]
|
||||||
|
(rx/merge
|
||||||
|
(rx/of (update-editor-state shape nil))
|
||||||
|
(when (and (not= content (:content shape))
|
||||||
|
(some? (:current-page-id state))
|
||||||
|
(some? shape))
|
||||||
|
(rx/of
|
||||||
|
(dwsh/update-shapes
|
||||||
|
[id]
|
||||||
|
(fn [shape]
|
||||||
|
(let [{:keys [width height position-data]} modifiers]
|
||||||
|
(-> shape
|
||||||
|
(assoc :content content)
|
||||||
|
(cond-> position-data
|
||||||
|
(assoc :position-data position-data))
|
||||||
|
(cond-> (and update-name? (some? name))
|
||||||
|
(assoc :name name))
|
||||||
|
(cond-> (or (some? width) (some? height))
|
||||||
|
(gsh/transform-shape (ctm/change-size shape width height))))))
|
||||||
|
{:undo-group (when new-shape? id)}))))))
|
||||||
|
|
||||||
(when (some? id)
|
(when (some? id)
|
||||||
(rx/of (dws/deselect-shape id)
|
(rx/of (dws/deselect-shape id)
|
||||||
|
@ -733,7 +796,14 @@
|
||||||
(rx/empty)))
|
(rx/empty)))
|
||||||
|
|
||||||
(when (features/active-feature? state "text-editor/v2")
|
(when (features/active-feature? state "text-editor/v2")
|
||||||
(rx/of (v2-update-text-editor-styles id attrs)))))))
|
(rx/of (v2-update-text-editor-styles id attrs)))
|
||||||
|
|
||||||
|
(when (features/active-feature? state "render-wasm/v1")
|
||||||
|
;; This delay is to give time for the font to be correctly rendered
|
||||||
|
;; in wasm.
|
||||||
|
(cond->> (rx/of (resize-wasm-text id))
|
||||||
|
(contains? attrs :font-id)
|
||||||
|
(rx/delay 200)))))))
|
||||||
|
|
||||||
ptk/EffectEvent
|
ptk/EffectEvent
|
||||||
(effect [_ state _]
|
(effect [_ state _]
|
||||||
|
@ -852,34 +922,54 @@
|
||||||
(ptk/reify ::v2-update-text-shape-position-data
|
(ptk/reify ::v2-update-text-shape-position-data
|
||||||
ptk/UpdateEvent
|
ptk/UpdateEvent
|
||||||
(update [_ state]
|
(update [_ state]
|
||||||
(let []
|
(update-in state [:workspace-text-modifier shape-id] {:position-data position-data}))))
|
||||||
(update-in state [:workspace-text-modifier shape-id] {:position-data position-data})))))
|
|
||||||
|
|
||||||
(defn v2-update-text-shape-content
|
(defn v2-update-text-shape-content
|
||||||
([id content]
|
[id content & {:keys [update-name? name finalize?]
|
||||||
(v2-update-text-shape-content id content false nil))
|
:or {update-name? false name nil finalize? false}}]
|
||||||
([id content update-name?]
|
(ptk/reify ::v2-update-text-shape-content
|
||||||
(v2-update-text-shape-content id content update-name? nil))
|
ptk/WatchEvent
|
||||||
([id content update-name? name]
|
(watch [_ state _]
|
||||||
(ptk/reify ::v2-update-text-shape-content
|
(if (features/active-feature? state "render-wasm/v1")
|
||||||
ptk/WatchEvent
|
(let [objects (dsh/lookup-page-objects state)
|
||||||
(watch [_ state _]
|
shape (get objects id)
|
||||||
(let [objects (dsh/lookup-page-objects state)
|
new-shape? (nil? (:content shape))]
|
||||||
shape (get objects id)
|
(rx/of
|
||||||
modifiers (get-in state [:workspace-text-modifier id])
|
(dwsh/update-shapes
|
||||||
new-shape? (nil? (:content shape))]
|
[id]
|
||||||
(rx/of
|
(fn [shape]
|
||||||
(dwsh/update-shapes
|
(let [new-shape (-> shape
|
||||||
[id]
|
(assoc :content content)
|
||||||
(fn [shape]
|
(cond-> (and update-name? (some? name))
|
||||||
(let [{:keys [width height position-data]} modifiers]
|
(assoc :name name)))]
|
||||||
(let [new-shape (-> shape
|
new-shape))
|
||||||
(assoc :content content)
|
{:undo-group (when new-shape? id)})
|
||||||
(cond-> position-data
|
|
||||||
(assoc :position-data position-data))
|
(if finalize?
|
||||||
(cond-> (and update-name? (some? name))
|
(dwm/apply-wasm-modifiers
|
||||||
(assoc :name name))
|
(resize-wasm-text-modifiers shape content)
|
||||||
(cond-> (or (some? width) (some? height))
|
{:undo-group (when new-shape? id)})
|
||||||
(gsh/transform-shape (ctm/change-size shape width height))))]
|
|
||||||
new-shape)))
|
(dwm/set-wasm-modifiers
|
||||||
{:undo-group (when new-shape? id)})))))))
|
(resize-wasm-text-modifiers shape content)
|
||||||
|
{:undo-group (when new-shape? id)}))))
|
||||||
|
|
||||||
|
(let [objects (dsh/lookup-page-objects state)
|
||||||
|
shape (get objects id)
|
||||||
|
modifiers (get-in state [:workspace-text-modifier id])
|
||||||
|
new-shape? (nil? (:content shape))]
|
||||||
|
(rx/of
|
||||||
|
(dwsh/update-shapes
|
||||||
|
[id]
|
||||||
|
(fn [shape]
|
||||||
|
(let [{:keys [width height position-data]} modifiers]
|
||||||
|
(let [new-shape (-> shape
|
||||||
|
(assoc :content content)
|
||||||
|
(cond-> position-data
|
||||||
|
(assoc :position-data position-data))
|
||||||
|
(cond-> (and update-name? (some? name))
|
||||||
|
(assoc :name name))
|
||||||
|
(cond-> (or (some? width) (some? height))
|
||||||
|
(gsh/transform-shape (ctm/change-size shape width height))))]
|
||||||
|
new-shape)))
|
||||||
|
{:undo-group (when new-shape? id)})))))))
|
||||||
|
|
|
@ -1169,3 +1169,31 @@
|
||||||
(if (features/active-feature? state "render-wasm/v1")
|
(if (features/active-feature? state "render-wasm/v1")
|
||||||
(rx/of (dwm/apply-wasm-modifiers modifiers {:undo-group undo-group}))
|
(rx/of (dwm/apply-wasm-modifiers modifiers {:undo-group undo-group}))
|
||||||
(rx/of (dwm/apply-modifiers {:modifiers modifiers :undo-group undo-group})))))))
|
(rx/of (dwm/apply-modifiers {:modifiers modifiers :undo-group undo-group})))))))
|
||||||
|
|
||||||
|
(defn resize-text-editor
|
||||||
|
[id {:keys [width height]}]
|
||||||
|
(ptk/reify ::resize-text-editor
|
||||||
|
ptk/WatchEvent
|
||||||
|
(watch [_ state _]
|
||||||
|
(let [objects (dsh/lookup-page-objects state)
|
||||||
|
shape (get objects id)
|
||||||
|
|
||||||
|
resize-v
|
||||||
|
(gpt/point
|
||||||
|
(/ width (-> shape :selrect :width))
|
||||||
|
(/ height (-> shape :selrect :height)))
|
||||||
|
|
||||||
|
origin
|
||||||
|
(first (:points shape))
|
||||||
|
|
||||||
|
modifiers
|
||||||
|
{id
|
||||||
|
{:modifiers
|
||||||
|
(ctm/resize-modifiers
|
||||||
|
resize-v
|
||||||
|
origin
|
||||||
|
(:transform shape (gmt/matrix))
|
||||||
|
(:transform-inverse shape (gmt/matrix)))}}]
|
||||||
|
|
||||||
|
(.log js/console (clj->js modifiers))
|
||||||
|
(rx/of (dwm/set-wasm-modifiers modifiers))))))
|
||||||
|
|
|
@ -8,35 +8,52 @@
|
||||||
(:require
|
(:require
|
||||||
[app.common.geom.shapes :as gsh]
|
[app.common.geom.shapes :as gsh]
|
||||||
[app.main.data.workspace.texts :as dwt]
|
[app.main.data.workspace.texts :as dwt]
|
||||||
|
[app.main.features :as features]
|
||||||
[app.main.refs :as refs]
|
[app.main.refs :as refs]
|
||||||
|
[app.main.store :as st]
|
||||||
|
[app.render-wasm.api :as wasm.api]
|
||||||
[rumext.v2 :as mf]))
|
[rumext.v2 :as mf]))
|
||||||
|
|
||||||
(mf/defc text-edition-outline
|
(mf/defc text-edition-outline
|
||||||
[{:keys [shape zoom modifiers]}]
|
[{:keys [shape zoom modifiers]}]
|
||||||
(let [modifiers (get-in modifiers [(:id shape) :modifiers])
|
(if (features/active-feature? @st/state "render-wasm/v1")
|
||||||
|
(let [transform (gsh/transform-str shape)
|
||||||
|
{:keys [id x y]} shape
|
||||||
|
{:keys [width height]} (wasm.api/text-dimensions id)]
|
||||||
|
[:rect.main.viewport-selrect
|
||||||
|
{:x x
|
||||||
|
:y y
|
||||||
|
:width width
|
||||||
|
:height height
|
||||||
|
:transform transform
|
||||||
|
:style {:stroke "var(--color-accent-tertiary)"
|
||||||
|
:stroke-width (/ 1 zoom)
|
||||||
|
:fill "none"}}])
|
||||||
|
|
||||||
text-modifier-ref
|
(let [modifiers (get-in modifiers [(:id shape) :modifiers])
|
||||||
(mf/use-memo (mf/deps (:id shape)) #(refs/workspace-text-modifier-by-id (:id shape)))
|
|
||||||
|
|
||||||
text-modifier
|
text-modifier-ref
|
||||||
(mf/deref text-modifier-ref)
|
(mf/use-memo (mf/deps (:id shape)) #(refs/workspace-text-modifier-by-id (:id shape)))
|
||||||
|
|
||||||
shape (cond-> shape
|
text-modifier
|
||||||
(some? modifiers)
|
(mf/deref text-modifier-ref)
|
||||||
(gsh/transform-shape modifiers)
|
|
||||||
|
|
||||||
(some? text-modifier)
|
shape (cond-> shape
|
||||||
(dwt/apply-text-modifier text-modifier))
|
(some? modifiers)
|
||||||
|
(gsh/transform-shape modifiers)
|
||||||
|
|
||||||
transform (gsh/transform-str shape)
|
(some? text-modifier)
|
||||||
{:keys [x y width height]} shape]
|
(dwt/apply-text-modifier text-modifier))
|
||||||
|
|
||||||
[:rect.main.viewport-selrect
|
transform (gsh/transform-str shape)
|
||||||
{:x x
|
{:keys [x y width height]} shape]
|
||||||
:y y
|
|
||||||
:width width
|
[:rect.main.viewport-selrect
|
||||||
:height height
|
{:x x
|
||||||
:transform transform
|
:y y
|
||||||
:style {:stroke "var(--color-accent-tertiary)"
|
:width width
|
||||||
:stroke-width (/ 1 zoom)
|
:height height
|
||||||
:fill "none"}}]))
|
:transform transform
|
||||||
|
:style {:stroke "var(--color-accent-tertiary)"
|
||||||
|
:stroke-width (/ 1 zoom)
|
||||||
|
:fill "none"}}])))
|
||||||
|
|
|
@ -69,7 +69,10 @@
|
||||||
on-blur
|
on-blur
|
||||||
(fn []
|
(fn []
|
||||||
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
||||||
(st/emit! (dwt/v2-update-text-shape-content shape-id content update-name? (gen-name instance))))
|
(st/emit! (dwt/v2-update-text-shape-content shape-id content
|
||||||
|
:update-name? update-name?
|
||||||
|
:name (gen-name instance)
|
||||||
|
:finalize? true)))
|
||||||
|
|
||||||
(let [container-node (mf/ref-val container-ref)]
|
(let [container-node (mf/ref-val container-ref)]
|
||||||
(dom/set-style! container-node "opacity" 0)))
|
(dom/set-style! container-node "opacity" 0)))
|
||||||
|
@ -87,7 +90,7 @@
|
||||||
on-needs-layout
|
on-needs-layout
|
||||||
(fn []
|
(fn []
|
||||||
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
||||||
(st/emit! (dwt/v2-update-text-shape-content shape-id content true)))
|
(st/emit! (dwt/v2-update-text-shape-content shape-id content :update-name? true)))
|
||||||
;; FIXME: We need to find a better way to trigger layout changes.
|
;; FIXME: We need to find a better way to trigger layout changes.
|
||||||
#_(st/emit!
|
#_(st/emit!
|
||||||
(dwt/v2-update-text-shape-position-data shape-id [])))
|
(dwt/v2-update-text-shape-position-data shape-id [])))
|
||||||
|
@ -95,7 +98,7 @@
|
||||||
on-change
|
on-change
|
||||||
(fn []
|
(fn []
|
||||||
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
(when-let [content (content/dom->cljs (dwt/get-editor-root instance))]
|
||||||
(st/emit! (dwt/v2-update-text-shape-content shape-id content true))))]
|
(st/emit! (dwt/v2-update-text-shape-content shape-id content :update-name? true))))]
|
||||||
|
|
||||||
(.addEventListener ^js global/document "keyup" on-key-up)
|
(.addEventListener ^js global/document "keyup" on-key-up)
|
||||||
(.addEventListener ^js instance "blur" on-blur)
|
(.addEventListener ^js instance "blur" on-blur)
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
[app.main.data.workspace.shortcuts :as sc]
|
[app.main.data.workspace.shortcuts :as sc]
|
||||||
[app.main.data.workspace.texts :as dwt]
|
[app.main.data.workspace.texts :as dwt]
|
||||||
[app.main.data.workspace.undo :as dwu]
|
[app.main.data.workspace.undo :as dwu]
|
||||||
|
[app.main.features :as features]
|
||||||
[app.main.refs :as refs]
|
[app.main.refs :as refs]
|
||||||
[app.main.store :as st]
|
[app.main.store :as st]
|
||||||
[app.main.ui.components.radio-buttons :refer [radio-button radio-buttons]]
|
[app.main.ui.components.radio-buttons :refer [radio-button radio-buttons]]
|
||||||
|
@ -130,6 +131,9 @@
|
||||||
(st/emit!
|
(st/emit!
|
||||||
(dwu/start-undo-transaction uid)
|
(dwu/start-undo-transaction uid)
|
||||||
(dwsh/update-shapes ids #(assoc % :grow-type grow-type)))
|
(dwsh/update-shapes ids #(assoc % :grow-type grow-type)))
|
||||||
|
|
||||||
|
(when (features/active-feature? @st/state "render-wasm/v1")
|
||||||
|
(st/emit! (dwt/resize-wasm-text-all ids)))
|
||||||
;; We asynchronously commit so every sychronous event is resolved first and inside the transaction
|
;; We asynchronously commit so every sychronous event is resolved first and inside the transaction
|
||||||
(ts/schedule #(st/emit! (dwu/commit-undo-transaction uid))))
|
(ts/schedule #(st/emit! (dwu/commit-undo-transaction uid))))
|
||||||
(when (some? on-blur) (on-blur))))]
|
(when (some? on-blur) (on-blur))))]
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
[app.common.types.shape-tree :as ctt]
|
[app.common.types.shape-tree :as ctt]
|
||||||
[app.common.types.shape.layout :as ctl]
|
[app.common.types.shape.layout :as ctl]
|
||||||
[app.main.data.workspace.modifiers :as dwm]
|
[app.main.data.workspace.modifiers :as dwm]
|
||||||
|
[app.main.data.workspace.transforms :as dwt]
|
||||||
[app.main.features :as features]
|
[app.main.features :as features]
|
||||||
[app.main.refs :as refs]
|
[app.main.refs :as refs]
|
||||||
[app.main.store :as st]
|
[app.main.store :as st]
|
||||||
|
@ -305,9 +306,12 @@
|
||||||
(let [content (-> active-editor-state
|
(let [content (-> active-editor-state
|
||||||
(ted/get-editor-current-content)
|
(ted/get-editor-current-content)
|
||||||
(ted/export-content))]
|
(ted/export-content))]
|
||||||
|
(wasm.api/use-shape edition)
|
||||||
(wasm.api/set-shape-text-content content)
|
(wasm.api/set-shape-text-content content)
|
||||||
(wasm.api/clear-drawing-cache)
|
(let [dimension (wasm.api/text-dimensions)]
|
||||||
(wasm.api/request-render "content")))))
|
(st/emit! (dwt/resize-text-editor edition dimension))
|
||||||
|
(wasm.api/clear-drawing-cache)
|
||||||
|
(wasm.api/request-render "content"))))))
|
||||||
|
|
||||||
(mf/with-effect [vport]
|
(mf/with-effect [vport]
|
||||||
(when @canvas-init?
|
(when @canvas-init?
|
||||||
|
|
|
@ -582,6 +582,8 @@
|
||||||
(h/call wasm/internal-module "_add_shape_shadow" rgba blur spread x y (sr/translate-shadow-style style) hidden)
|
(h/call wasm/internal-module "_add_shape_shadow" rgba blur spread x y (sr/translate-shadow-style style) hidden)
|
||||||
(recur (inc index)))))))
|
(recur (inc index)))))))
|
||||||
|
|
||||||
|
(declare propagate-apply)
|
||||||
|
|
||||||
(defn set-shape-text-content
|
(defn set-shape-text-content
|
||||||
[content]
|
[content]
|
||||||
(h/call wasm/internal-module "_clear_shape_text")
|
(h/call wasm/internal-module "_clear_shape_text")
|
||||||
|
@ -604,6 +606,22 @@
|
||||||
fonts)]
|
fonts)]
|
||||||
(f/store-fonts fonts))))
|
(f/store-fonts fonts))))
|
||||||
|
|
||||||
|
(defn set-shape-grow-type
|
||||||
|
[grow-type]
|
||||||
|
(h/call wasm/internal-module "_set_shape_grow_type" (sr/translate-grow-type grow-type)))
|
||||||
|
|
||||||
|
(defn text-dimensions
|
||||||
|
([id]
|
||||||
|
(use-shape id)
|
||||||
|
(text-dimensions))
|
||||||
|
([]
|
||||||
|
(let [offset (h/call wasm/internal-module "_get_text_dimensions")
|
||||||
|
heapf32 (mem/get-heap-f32)
|
||||||
|
width (aget heapf32 (mem/ptr8->ptr32 offset))
|
||||||
|
height (aget heapf32 (mem/ptr8->ptr32 (+ offset 4)))]
|
||||||
|
(h/call wasm/internal-module "_free_bytes")
|
||||||
|
{:width width :height height})))
|
||||||
|
|
||||||
(defn set-view-box
|
(defn set-view-box
|
||||||
[zoom vbox]
|
[zoom vbox]
|
||||||
(h/call wasm/internal-module "_set_view" zoom (- (:x vbox)) (- (:y vbox)))
|
(h/call wasm/internal-module "_set_view" zoom (- (:x vbox)) (- (:y vbox)))
|
||||||
|
@ -639,6 +657,7 @@
|
||||||
opacity (dm/get-prop shape :opacity)
|
opacity (dm/get-prop shape :opacity)
|
||||||
hidden (dm/get-prop shape :hidden)
|
hidden (dm/get-prop shape :hidden)
|
||||||
content (dm/get-prop shape :content)
|
content (dm/get-prop shape :content)
|
||||||
|
grow-type (dm/get-prop shape :grow-type)
|
||||||
blur (dm/get-prop shape :blur)
|
blur (dm/get-prop shape :blur)
|
||||||
corners (when (some? (dm/get-prop shape :r1))
|
corners (when (some? (dm/get-prop shape :r1))
|
||||||
[(dm/get-prop shape :r1)
|
[(dm/get-prop shape :r1)
|
||||||
|
@ -677,7 +696,8 @@
|
||||||
(when (some? shadows) (set-shape-shadows shadows))
|
(when (some? shadows) (set-shape-shadows shadows))
|
||||||
(when (and (= type :text) (some? content))
|
(when (and (= type :text) (some? content))
|
||||||
(set-shape-text-content content))
|
(set-shape-text-content content))
|
||||||
|
(when (= type :text)
|
||||||
|
(set-shape-grow-type grow-type))
|
||||||
(when (or (ctl/any-layout? shape)
|
(when (or (ctl/any-layout? shape)
|
||||||
(ctl/any-layout-immediate-child? objects shape))
|
(ctl/any-layout-immediate-child? objects shape))
|
||||||
(set-layout-child shape))
|
(set-layout-child shape))
|
||||||
|
|
|
@ -283,6 +283,13 @@
|
||||||
:remove-children 1
|
:remove-children 1
|
||||||
:add-children 2))
|
:add-children 2))
|
||||||
|
|
||||||
|
(defn translate-grow-type
|
||||||
|
[grow-type]
|
||||||
|
(case grow-type
|
||||||
|
:auto-width 1
|
||||||
|
:auto-height 2
|
||||||
|
0))
|
||||||
|
|
||||||
(defn- serialize-enum
|
(defn- serialize-enum
|
||||||
[value enum-map]
|
[value enum-map]
|
||||||
(get enum-map value 0))
|
(get enum-map value 0))
|
||||||
|
|
|
@ -152,6 +152,9 @@
|
||||||
(= (:type shape) :text)
|
(= (:type shape) :text)
|
||||||
(into [] (api/set-shape-text-content v)))
|
(into [] (api/set-shape-text-content v)))
|
||||||
|
|
||||||
|
:grow-type
|
||||||
|
(api/set-shape-grow-type v)
|
||||||
|
|
||||||
(:layout-item-margin
|
(:layout-item-margin
|
||||||
:layout-item-margin-type
|
:layout-item-margin-type
|
||||||
:layout-item-h-sizing
|
:layout-item-h-sizing
|
||||||
|
|
|
@ -356,6 +356,7 @@ impl RenderState {
|
||||||
s.canvas().concat(&matrix);
|
s.canvas().concat(&matrix);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let text_content = text_content.new_bounds(shape.selrect());
|
||||||
let paragraphs = text_content.get_skia_paragraphs(&self.fonts.font_collection());
|
let paragraphs = text_content.get_skia_paragraphs(&self.fonts.font_collection());
|
||||||
|
|
||||||
shadows::render_text_drop_shadows(self, &shape, ¶graphs, antialias);
|
shadows::render_text_drop_shadows(self, &shape, ¶graphs, antialias);
|
||||||
|
|
|
@ -711,9 +711,9 @@ impl Shape {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_text(&mut self) {
|
pub fn clear_text(&mut self) {
|
||||||
match self.shape_type {
|
match &self.shape_type {
|
||||||
Type::Text(_) => {
|
Type::Text(old_text_content) => {
|
||||||
let new_text_content = TextContent::new(self.selrect);
|
let new_text_content = TextContent::new(self.selrect, old_text_content.grow_type());
|
||||||
self.shape_type = Type::Text(new_text_content);
|
self.shape_type = Type::Text(new_text_content);
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
@ -12,19 +12,57 @@ use super::FontFamily;
|
||||||
use crate::utils::uuid_from_u32;
|
use crate::utils::uuid_from_u32;
|
||||||
use crate::Uuid;
|
use crate::Uuid;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||||
|
pub enum GrowType {
|
||||||
|
Fixed,
|
||||||
|
AutoWidth,
|
||||||
|
AutoHeight,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GrowType {
|
||||||
|
pub fn from(grow_type: u8) -> Self {
|
||||||
|
match grow_type {
|
||||||
|
0 => Self::Fixed,
|
||||||
|
1 => Self::AutoWidth,
|
||||||
|
2 => Self::AutoHeight,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct TextContent {
|
pub struct TextContent {
|
||||||
paragraphs: Vec<Paragraph>,
|
paragraphs: Vec<Paragraph>,
|
||||||
bounds: Rect,
|
bounds: Rect,
|
||||||
|
grow_type: GrowType,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_paragraphs_width(width: f32, paragraphs: &mut Vec<Vec<skia::textlayout::Paragraph>>) {
|
||||||
|
for group in paragraphs {
|
||||||
|
for paragraph in group {
|
||||||
|
paragraph.layout(width)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextContent {
|
impl TextContent {
|
||||||
pub fn new(bounds: Rect) -> Self {
|
pub fn new(bounds: Rect, grow_type: GrowType) -> Self {
|
||||||
let mut res = Self::default();
|
let mut res = Self::default();
|
||||||
res.bounds = bounds;
|
res.bounds = bounds;
|
||||||
|
res.grow_type = grow_type;
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_bounds(&self, bounds: Rect) -> Self {
|
||||||
|
let paragraphs = self.paragraphs.clone();
|
||||||
|
let grow_type = self.grow_type;
|
||||||
|
Self {
|
||||||
|
paragraphs,
|
||||||
|
bounds,
|
||||||
|
grow_type,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_xywh(&mut self, x: f32, y: f32, w: f32, h: f32) {
|
pub fn set_xywh(&mut self, x: f32, y: f32, w: f32, h: f32) {
|
||||||
self.bounds = Rect::from_xywh(x, y, w, h);
|
self.bounds = Rect::from_xywh(x, y, w, h);
|
||||||
}
|
}
|
||||||
|
@ -97,22 +135,25 @@ impl TextContent {
|
||||||
paragraph_group
|
paragraph_group
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn collect_paragraphs(
|
||||||
|
&self,
|
||||||
|
mut paragraphs: Vec<Vec<skia::textlayout::Paragraph>>,
|
||||||
|
) -> Vec<Vec<skia::textlayout::Paragraph>> {
|
||||||
|
if self.grow_type() == GrowType::AutoWidth {
|
||||||
|
set_paragraphs_width(f32::MAX, &mut paragraphs);
|
||||||
|
let max_width = auto_width(¶graphs).ceil();
|
||||||
|
set_paragraphs_width(max_width, &mut paragraphs);
|
||||||
|
} else {
|
||||||
|
set_paragraphs_width(self.width(), &mut paragraphs);
|
||||||
|
}
|
||||||
|
paragraphs
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_skia_paragraphs(
|
pub fn get_skia_paragraphs(
|
||||||
&self,
|
&self,
|
||||||
fonts: &FontCollection,
|
fonts: &FontCollection,
|
||||||
) -> Vec<Vec<skia::textlayout::Paragraph>> {
|
) -> Vec<Vec<skia::textlayout::Paragraph>> {
|
||||||
self.to_paragraphs(fonts)
|
self.collect_paragraphs(self.to_paragraphs(fonts))
|
||||||
.into_iter()
|
|
||||||
.map(|group| {
|
|
||||||
group
|
|
||||||
.into_iter()
|
|
||||||
.map(|mut paragraph| {
|
|
||||||
paragraph.layout(self.width());
|
|
||||||
paragraph
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_skia_stroke_paragraphs(
|
pub fn get_skia_stroke_paragraphs(
|
||||||
|
@ -120,18 +161,15 @@ impl TextContent {
|
||||||
fonts: &FontCollection,
|
fonts: &FontCollection,
|
||||||
paints: &Vec<Paint>,
|
paints: &Vec<Paint>,
|
||||||
) -> Vec<Vec<skia::textlayout::Paragraph>> {
|
) -> Vec<Vec<skia::textlayout::Paragraph>> {
|
||||||
self.to_stroke_paragraphs(fonts, paints)
|
self.collect_paragraphs(self.to_stroke_paragraphs(fonts, paints))
|
||||||
.into_iter()
|
}
|
||||||
.map(|group| {
|
|
||||||
group
|
pub fn grow_type(&self) -> GrowType {
|
||||||
.into_iter()
|
self.grow_type
|
||||||
.map(|mut paragraph| {
|
}
|
||||||
paragraph.layout(self.width());
|
|
||||||
paragraph
|
pub fn set_grow_type(&mut self, grow_type: GrowType) {
|
||||||
})
|
self.grow_type = grow_type;
|
||||||
.collect()
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,6 +178,7 @@ impl Default for TextContent {
|
||||||
Self {
|
Self {
|
||||||
paragraphs: vec![],
|
paragraphs: vec![],
|
||||||
bounds: Rect::default(),
|
bounds: Rect::default(),
|
||||||
|
grow_type: GrowType::Fixed,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -495,3 +534,16 @@ impl From<&Vec<u8>> for RawTextData {
|
||||||
Self { paragraph }
|
Self { paragraph }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn auto_width(paragraphs: &Vec<Vec<skia::textlayout::Paragraph>>) -> f32 {
|
||||||
|
paragraphs.iter().flatten().fold(0.0, |auto_width, p| {
|
||||||
|
f32::max(p.max_intrinsic_width(), auto_width)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn auto_height(paragraphs: &Vec<Vec<skia::textlayout::Paragraph>>) -> f32 {
|
||||||
|
paragraphs
|
||||||
|
.iter()
|
||||||
|
.flatten()
|
||||||
|
.fold(0.0, |auto_height, p| auto_height + p.height())
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use crate::mem;
|
use crate::mem;
|
||||||
use crate::shapes::RawTextData;
|
use crate::shapes::{auto_height, auto_width, GrowType, RawTextData, Type};
|
||||||
use crate::with_current_shape;
|
|
||||||
use crate::STATE;
|
use crate::STATE;
|
||||||
|
use crate::{with_current_shape, with_state};
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn clear_shape_text() {
|
pub extern "C" fn clear_shape_text() {
|
||||||
|
@ -22,3 +23,48 @@ pub extern "C" fn set_shape_text_content() {
|
||||||
|
|
||||||
mem::free_bytes();
|
mem::free_bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn set_shape_grow_type(grow_type: u8) {
|
||||||
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
if let Type::Text(text_content) = &mut shape.shape_type {
|
||||||
|
text_content.set_grow_type(GrowType::from(grow_type));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn get_text_dimensions() -> *mut u8 {
|
||||||
|
let font_col;
|
||||||
|
with_state!(state, {
|
||||||
|
font_col = state.render_state.fonts.font_collection();
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut width = 0.01;
|
||||||
|
let mut height = 0.01;
|
||||||
|
with_current_shape!(state, |shape: &mut Shape| {
|
||||||
|
width = shape.selrect.width();
|
||||||
|
height = shape.selrect.height();
|
||||||
|
|
||||||
|
if let Type::Text(content) = &shape.shape_type {
|
||||||
|
match content.grow_type() {
|
||||||
|
GrowType::AutoWidth => {
|
||||||
|
let paragraphs = content.get_skia_paragraphs(font_col);
|
||||||
|
width = auto_width(¶graphs);
|
||||||
|
height = auto_height(¶graphs);
|
||||||
|
}
|
||||||
|
GrowType::AutoHeight => {
|
||||||
|
let paragraphs = content.get_skia_paragraphs(font_col);
|
||||||
|
height = auto_height(¶graphs);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut bytes = Vec::<u8>::with_capacity(8);
|
||||||
|
bytes.resize(8, 0);
|
||||||
|
bytes[0..4].clone_from_slice(&width.to_le_bytes());
|
||||||
|
bytes[4..8].clone_from_slice(&height.to_le_bytes());
|
||||||
|
mem::write_bytes(bytes)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue