Replace cmd name usage with segment name

For fix naming inconsistency
This commit is contained in:
Andrey Antukh 2025-04-09 08:56:39 +02:00
parent 1d0020f6e6
commit 3a22545158

View file

@ -42,10 +42,10 @@
[content] [content]
(->> (d/with-prev content) (->> (d/with-prev content)
(d/enumerate) (d/enumerate)
(mapcat (fn [[index [cur-cmd pre-cmd]]] (mapcat (fn [[index [cur-segment pre-segment]]]
(if (and pre-cmd (= :curve-to (:command cur-cmd))) (if (and pre-segment (= :curve-to (:command cur-segment)))
(let [cur-pos (helpers/segment->point cur-cmd) (let [cur-pos (helpers/segment->point cur-segment)
pre-pos (helpers/segment->point pre-cmd)] pre-pos (helpers/segment->point pre-segment)]
(-> [[pre-pos [index :c1]] (-> [[pre-pos [index :c1]]
[cur-pos [index :c2]]])) [cur-pos [index :c2]]]))
[]))) [])))
@ -56,7 +56,7 @@
(defn point-indices (defn point-indices
[content point] [content point]
(->> (d/enumerate content) (->> (d/enumerate content)
(filter (fn [[_ cmd]] (= point (helpers/segment->point cmd)))) (filter (fn [[_ segment]] (= point (helpers/segment->point segment))))
(mapv (fn [[index _]] index)))) (mapv (fn [[index _]] index))))
(defn handler-indices (defn handler-indices
@ -64,10 +64,10 @@
[content point] [content point]
(->> (d/with-prev content) (->> (d/with-prev content)
(d/enumerate) (d/enumerate)
(mapcat (fn [[index [cur-cmd pre-cmd]]] (mapcat (fn [[index [cur-segment pre-segment]]]
(if (and (some? pre-cmd) (= :curve-to (:command cur-cmd))) (if (and (some? pre-segment) (= :curve-to (:command cur-segment)))
(let [cur-pos (helpers/segment->point cur-cmd) (let [cur-pos (helpers/segment->point cur-segment)
pre-pos (helpers/segment->point pre-cmd)] pre-pos (helpers/segment->point pre-segment)]
(cond-> [] (cond-> []
(= pre-pos point) (conj [index :c1]) (= pre-pos point) (conj [index :c1])
(= cur-pos point) (conj [index :c2]))) (= cur-pos point) (conj [index :c2])))
@ -100,7 +100,7 @@
"Returns the commands involving a point with its indices" "Returns the commands involving a point with its indices"
[content point] [content point]
(->> (d/enumerate content) (->> (d/enumerate content)
(filterv (fn [[_ cmd]] (= (helpers/segment->point cmd) point))))) (filterv (fn [[_ segment]] (= (helpers/segment->point segment) point)))))
;; FIXME: candidate to be optimized with native data type operation ;; FIXME: candidate to be optimized with native data type operation
(defn handler->point (defn handler->point
@ -281,15 +281,15 @@
[shape position] [shape position]
(let [point+distance (let [point+distance
(fn [[cur-cmd prev-cmd]] (fn [[cur-segment prev-segment]]
(let [from-p (helpers/segment->point prev-cmd) (let [from-p (helpers/segment->point prev-segment)
to-p (helpers/segment->point cur-cmd) to-p (helpers/segment->point cur-segment)
h1 (gpt/point (get-in cur-cmd [:params :c1x]) h1 (gpt/point (get-in cur-segment [:params :c1x])
(get-in cur-cmd [:params :c1y])) (get-in cur-segment [:params :c1y]))
h2 (gpt/point (get-in cur-cmd [:params :c2x]) h2 (gpt/point (get-in cur-segment [:params :c2x])
(get-in cur-cmd [:params :c2y])) (get-in cur-segment [:params :c2y]))
point point
(case (:command cur-cmd) (case (:command cur-segment)
:line-to :line-to
(line-closest-point position from-p to-p) (line-closest-point position from-p to-p)
@ -318,15 +318,15 @@
[content position] [content position]
(let [point+distance (let [point+distance
(fn [[cur-cmd prev-cmd]] (fn [[cur-segment prev-segment]]
(let [from-p (helpers/segment->point prev-cmd) (let [from-p (helpers/segment->point prev-segment)
to-p (helpers/segment->point cur-cmd) to-p (helpers/segment->point cur-segment)
h1 (gpt/point (get-in cur-cmd [:params :c1x]) h1 (gpt/point (get-in cur-segment [:params :c1x])
(get-in cur-cmd [:params :c1y])) (get-in cur-segment [:params :c1y]))
h2 (gpt/point (get-in cur-cmd [:params :c2x]) h2 (gpt/point (get-in cur-segment [:params :c2x])
(get-in cur-cmd [:params :c2y])) (get-in cur-segment [:params :c2y]))
point point
(case (:command cur-cmd) (case (:command cur-segment)
:line-to :line-to
(line-closest-point position from-p to-p) (line-closest-point position from-p to-p)
@ -518,17 +518,17 @@
prev-point nil prev-point nil
start-point nil start-point nil
index 0 index 0
cur-cmd (first content) cur-segment (first content)
content (rest content)] content (rest content)]
(let [command (:command cur-cmd) (let [command (:command cur-segment)
close-path? (= command :close-path) close-path? (= command :close-path)
move-to? (= command :move-to) move-to? (= command :move-to)
;; Close-path makes a segment from the last point to the initial path point ;; Close-path makes a segment from the last point to the initial path point
cur-point (if close-path? cur-point (if close-path?
start-point start-point
(helpers/segment->point cur-cmd)) (helpers/segment->point cur-segment))
;; If there is a move-to we don't have a segment ;; If there is a move-to we don't have a segment
prev-point (if move-to? prev-point (if move-to?
@ -548,10 +548,10 @@
is-segment? is-segment?
(conj {:start prev-point (conj {:start prev-point
:end cur-point :end cur-point
:cmd cur-cmd :segment cur-segment
:index index}))] :index index}))]
(if (some? cur-cmd) (if (some? cur-segment)
(recur segments (recur segments
cur-point cur-point
start-point start-point
@ -566,22 +566,22 @@
[content points value] [content points value]
(let [split-command (let [split-command
(fn [{:keys [start end cmd index]}] (fn [{:keys [start end segment index]}]
(case (:command cmd) (case (:command segment)
:line-to [index (helpers/split-line-to start cmd value)] :line-to [index (helpers/split-line-to start segment value)]
:curve-to [index (helpers/split-curve-to start cmd value)] :curve-to [index (helpers/split-curve-to start segment value)]
:close-path [index [(helpers/make-line-to (gpt/lerp start end value)) cmd]] :close-path [index [(helpers/make-line-to (gpt/lerp start end value)) segment]]
nil)) nil))
cmd-changes segment-changes
(->> (get-segments content points) (->> (get-segments content points)
(into {} (comp (map split-command) (into {} (comp (map split-command)
(filter (comp not nil?))))) (filter (comp not nil?)))))
process-segments process-segments
(fn [[index command]] (fn [[index command]]
(if (contains? cmd-changes index) (if (contains? segment-changes index)
(get cmd-changes index) (get segment-changes index)
[command]))] [command]))]
(into [] (mapcat process-segments) (d/enumerate content)))) (into [] (mapcat process-segments) (d/enumerate content))))
@ -613,10 +613,10 @@
(loop [result [] (loop [result []
last-handler nil last-handler nil
[cur-cmd prev-cmd] (first content) [cur-segment prev-segment] (first content)
content (rest content)] content (rest content)]
(if (nil? cur-cmd) (if (nil? cur-segment)
;; The result with be an array of arrays were every entry is a subpath ;; The result with be an array of arrays were every entry is a subpath
(->> result (->> result
;; remove empty and only 1 node subpaths ;; remove empty and only 1 node subpaths
@ -625,17 +625,17 @@
(flatten) (flatten)
(into [])) (into []))
(let [move? (= :move-to (:command cur-cmd)) (let [move? (= :move-to (:command cur-segment))
curve? (= :curve-to (:command cur-cmd)) curve? (= :curve-to (:command cur-segment))
;; When the old command was a move we start a subpath ;; When the old command was a move we start a subpath
result (if move? (conj result []) result) result (if move? (conj result []) result)
subpath (peek result) subpath (peek result)
point (helpers/segment->point cur-cmd) point (helpers/segment->point cur-segment)
old-prev-point (helpers/segment->point prev-cmd) old-prev-point (helpers/segment->point prev-segment)
new-prev-point (helpers/segment->point (peek subpath)) new-prev-point (helpers/segment->point (peek subpath))
remove? (contains? points point) remove? (contains? points point)
@ -645,7 +645,7 @@
;; use it for the first handler of the regenerated path ;; use it for the first handler of the regenerated path
cur-handler (cond cur-handler (cond
(and (not last-handler) remove? curve?) (and (not last-handler) remove? curve?)
(select-keys (:params cur-cmd) [:c1x :c1y]) (select-keys (:params cur-segment) [:c1x :c1y])
(not remove?) (not remove?)
nil nil
@ -653,11 +653,11 @@
:else :else
last-handler) last-handler)
cur-cmd (cond-> cur-cmd cur-segment (cond-> cur-segment
;; If we're starting a subpath and it's not a move make it a move ;; If we're starting a subpath and it's not a move make it a move
(and (not move?) (empty? subpath)) (and (not move?) (empty? subpath))
(assoc :command :move-to (assoc :command :move-to
:params (select-keys (:params cur-cmd) [:x :y])) :params (select-keys (:params cur-segment) [:x :y]))
;; If have a curve the first handler will be relative to the previous ;; If have a curve the first handler will be relative to the previous
;; point. We change the handler to the new previous point ;; point. We change the handler to the new previous point
@ -668,7 +668,7 @@
result (cond-> result result (cond-> result
(not remove?) (not remove?)
(update head-idx conj cur-cmd))] (update head-idx conj cur-segment))]
(recur result (recur result
cur-handler cur-handler
(first content) (first content)
@ -702,32 +702,32 @@
(let [content (d/with-prev content)] (let [content (d/with-prev content)]
(loop [result [] (loop [result []
[cur-cmd prev-cmd] (first content) [cur-segment prev-segment] (first content)
content (rest content)] content (rest content)]
(if (nil? cur-cmd) (if (nil? cur-segment)
(->> result (->> result
(filter #(> (count %) 1)) (filter #(> (count %) 1))
(flatten) (flatten)
(into [])) (into []))
(let [prev-point (helpers/segment->point prev-cmd) (let [prev-point (helpers/segment->point prev-segment)
cur-point (helpers/segment->point cur-cmd) cur-point (helpers/segment->point cur-segment)
cur-cmd (cond-> cur-cmd cur-segment (cond-> cur-segment
(and (contains? points prev-point) (and (contains? points prev-point)
(contains? points cur-point)) (contains? points cur-point))
(assoc :command :move-to (assoc :command :move-to
:params (select-keys (:params cur-cmd) [:x :y]))) :params (select-keys (:params cur-segment) [:x :y])))
move? (= :move-to (:command cur-cmd)) move? (= :move-to (:command cur-segment))
result (if move? (conj result []) result) result (if move? (conj result []) result)
head-idx (dec (count result)) head-idx (dec (count result))
result (-> result result (-> result
(update head-idx conj cur-cmd))] (update head-idx conj cur-segment))]
(recur result (recur result
(first content) (first content)
(rest content))))))) (rest content)))))))
@ -749,6 +749,7 @@
(not= % other)))) (not= % other))))
(set/union target other))) (set/union target other)))
;; FIXME: revisit impl of this fn
(defn- group-segments [segments] (defn- group-segments [segments]
(loop [result [] (loop [result []
{point-a :start point-b :end :as segment} (first segments) {point-a :start point-b :end :as segment} (first segments)
@ -789,12 +790,12 @@
"Replaces the points in a path for its merge-point" "Replaces the points in a path for its merge-point"
[content point->merge-point] [content point->merge-point]
(let [replace-command (let [replace-command
(fn [cmd] (fn [segment]
(let [point (helpers/segment->point cmd)] (let [point (helpers/segment->point segment)]
(if (contains? point->merge-point point) (if (contains? point->merge-point point)
(let [merge-point (get point->merge-point point)] (let [merge-point (get point->merge-point point)]
(-> cmd (update :params assoc :x (:x merge-point) :y (:y merge-point)))) (-> segment (update :params assoc :x (:x merge-point) :y (:y merge-point))))
cmd)))] segment)))]
(->> content (->> content
(mapv replace-command)))) (mapv replace-command))))