mirror of
https://github.com/penpot/penpot.git
synced 2025-07-19 20:07:22 +02:00
✨ Replace duplicate fn get-point with segment->point
This commit is contained in:
parent
f3c3f3e2d8
commit
1d0020f6e6
7 changed files with 46 additions and 49 deletions
|
@ -14,12 +14,21 @@
|
|||
[app.common.geom.point :as gpt]
|
||||
[app.common.math :as mth]
|
||||
[app.common.svg :as csvg]
|
||||
[app.common.types.path.segment :as path.segm]
|
||||
[app.common.types.path.helpers :as path.helpers]
|
||||
[app.common.types.path.segment :as path.segment]
|
||||
[cuerdas.core :as str]))
|
||||
|
||||
(def commands-regex #"(?i)[mzlhvcsqta][^mzlhvcsqta]*")
|
||||
(def regex #"[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?")
|
||||
|
||||
(defn- get-point
|
||||
"Get a point for a segment"
|
||||
[prev-pos {:keys [relative params] :as segment}]
|
||||
(let [{:keys [x y] :or {x (:x prev-pos) y (:y prev-pos)}} params]
|
||||
(if relative
|
||||
(-> prev-pos (update :x + x) (update :y + y))
|
||||
(path.helpers/segment->point segment))))
|
||||
|
||||
(defn extract-params
|
||||
[data pattern]
|
||||
(loop [result []
|
||||
|
@ -184,7 +193,7 @@
|
|||
|
||||
(defn smooth->curve
|
||||
[{:keys [params]} pos handler]
|
||||
(let [{c1x :x c1y :y} (path.segm/calculate-opposite-handler pos handler)]
|
||||
(let [{c1x :x c1y :y} (path.segment/calculate-opposite-handler pos handler)]
|
||||
{:c1x c1x
|
||||
:c1y c1y
|
||||
:c2x (:cx params)
|
||||
|
@ -412,7 +421,7 @@
|
|||
|
||||
(= :smooth-quadratic-bezier-curve-to (:command command))
|
||||
(-> (assoc :command :curve-to)
|
||||
(update :params merge (quadratic->curve prev-pos (gpt/point params) (path.segm/calculate-opposite-handler prev-pos prev-qc)))))
|
||||
(update :params merge (quadratic->curve prev-pos (gpt/point params) (path.segment/calculate-opposite-handler prev-pos prev-qc)))))
|
||||
|
||||
result (if (= :elliptical-arc (:command command))
|
||||
(into result (arc->beziers prev-pos command))
|
||||
|
@ -435,13 +444,13 @@
|
|||
(gpt/point (get-in orig-command [:params :cx]) (get-in orig-command [:params :cy]))
|
||||
|
||||
:smooth-quadratic-bezier-curve-to
|
||||
(path.segm/calculate-opposite-handler prev-pos prev-qc)
|
||||
(path.segment/calculate-opposite-handler prev-pos prev-qc)
|
||||
|
||||
(gpt/point (get-in orig-command [:params :x]) (get-in orig-command [:params :y])))
|
||||
|
||||
next-pos (if (= :close-path (:command command))
|
||||
prev-start
|
||||
(path.segm/get-point prev-pos command))
|
||||
(get-point prev-pos command))
|
||||
|
||||
next-start (if (= :move-to (:command command)) next-pos prev-start)]
|
||||
|
||||
|
|
|
@ -18,19 +18,6 @@
|
|||
|
||||
#?(:clj (set! *warn-on-reflection* true))
|
||||
|
||||
(defn get-point
|
||||
"Get a point for a segment"
|
||||
([prev-pos {:keys [relative params] :as segment}]
|
||||
(let [{:keys [x y] :or {x (:x prev-pos) y (:y prev-pos)}} params]
|
||||
(if relative
|
||||
(-> prev-pos (update :x + x) (update :y + y))
|
||||
(get-point segment))))
|
||||
|
||||
([segment]
|
||||
(when segment
|
||||
(let [{:keys [x y]} (:params segment)]
|
||||
(gpt/point x y)))))
|
||||
|
||||
(defn update-handler
|
||||
[command prefix point]
|
||||
(let [[cox coy] (if (= prefix :c1) [:c1x :c1y] [:c2x :c2y])]
|
||||
|
@ -57,8 +44,8 @@
|
|||
(d/enumerate)
|
||||
(mapcat (fn [[index [cur-cmd pre-cmd]]]
|
||||
(if (and pre-cmd (= :curve-to (:command cur-cmd)))
|
||||
(let [cur-pos (get-point cur-cmd)
|
||||
pre-pos (get-point pre-cmd)]
|
||||
(let [cur-pos (helpers/segment->point cur-cmd)
|
||||
pre-pos (helpers/segment->point pre-cmd)]
|
||||
(-> [[pre-pos [index :c1]]
|
||||
[cur-pos [index :c2]]]))
|
||||
[])))
|
||||
|
@ -69,7 +56,7 @@
|
|||
(defn point-indices
|
||||
[content point]
|
||||
(->> (d/enumerate content)
|
||||
(filter (fn [[_ cmd]] (= point (get-point cmd))))
|
||||
(filter (fn [[_ cmd]] (= point (helpers/segment->point cmd))))
|
||||
(mapv (fn [[index _]] index))))
|
||||
|
||||
(defn handler-indices
|
||||
|
@ -79,8 +66,8 @@
|
|||
(d/enumerate)
|
||||
(mapcat (fn [[index [cur-cmd pre-cmd]]]
|
||||
(if (and (some? pre-cmd) (= :curve-to (:command cur-cmd)))
|
||||
(let [cur-pos (get-point cur-cmd)
|
||||
pre-pos (get-point pre-cmd)]
|
||||
(let [cur-pos (helpers/segment->point cur-cmd)
|
||||
pre-pos (helpers/segment->point pre-cmd)]
|
||||
(cond-> []
|
||||
(= pre-pos point) (conj [index :c1])
|
||||
(= cur-pos point) (conj [index :c2])))
|
||||
|
@ -91,8 +78,8 @@
|
|||
[content index prefix]
|
||||
|
||||
(let [point (if (= prefix :c2)
|
||||
(get-point (nth content index))
|
||||
(get-point (nth content (dec index))))
|
||||
(helpers/segment->point (nth content index))
|
||||
(helpers/segment->point (nth content (dec index))))
|
||||
|
||||
point->handlers (content->handlers content)
|
||||
|
||||
|
@ -113,7 +100,7 @@
|
|||
"Returns the commands involving a point with its indices"
|
||||
[content point]
|
||||
(->> (d/enumerate content)
|
||||
(filterv (fn [[_ cmd]] (= (get-point cmd) point)))))
|
||||
(filterv (fn [[_ cmd]] (= (helpers/segment->point cmd) point)))))
|
||||
|
||||
;; FIXME: candidate to be optimized with native data type operation
|
||||
(defn handler->point
|
||||
|
@ -135,8 +122,8 @@
|
|||
(defn handler->node
|
||||
[content index prefix]
|
||||
(if (= prefix :c1)
|
||||
(get-point (nth content (dec index)))
|
||||
(get-point (nth content index))))
|
||||
(helpers/segment->point (nth content (dec index)))
|
||||
(helpers/segment->point (nth content index))))
|
||||
|
||||
(defn calculate-opposite-handler
|
||||
"Given a point and its handler, gives the symmetric handler"
|
||||
|
@ -370,8 +357,8 @@
|
|||
process-command
|
||||
(fn [content [index [command prev]]]
|
||||
|
||||
(let [cur-point (get-point command)
|
||||
pre-point (get-point prev)
|
||||
(let [cur-point (helpers/segment->point command)
|
||||
pre-point (helpers/segment->point prev)
|
||||
handler-c1 (get-handler command :c1)
|
||||
handler-c2 (get-handler command :c2)]
|
||||
(if (and (= :curve-to (:command command))
|
||||
|
@ -403,7 +390,7 @@
|
|||
(defn- line->curve
|
||||
[from-p segment]
|
||||
|
||||
(let [to-p (get-point segment)
|
||||
(let [to-p (helpers/segment->point segment)
|
||||
|
||||
v (gpt/to-vec from-p to-p)
|
||||
d (gpt/distance from-p to-p)
|
||||
|
@ -459,10 +446,10 @@
|
|||
{:index index
|
||||
:prev-i (when (some? prev) prev-i)
|
||||
:prev-c prev
|
||||
:prev-p (get-point prev)
|
||||
:prev-p (helpers/segment->point prev)
|
||||
:next-i (when (some? next) next-i)
|
||||
:next-c next
|
||||
:next-p (get-point next)
|
||||
:next-p (helpers/segment->point next)
|
||||
:segment segment}))
|
||||
indices)
|
||||
|
||||
|
@ -541,7 +528,7 @@
|
|||
;; Close-path makes a segment from the last point to the initial path point
|
||||
cur-point (if close-path?
|
||||
start-point
|
||||
(get-point cur-cmd))
|
||||
(helpers/segment->point cur-cmd))
|
||||
|
||||
;; If there is a move-to we don't have a segment
|
||||
prev-point (if move-to?
|
||||
|
@ -646,10 +633,10 @@
|
|||
|
||||
subpath (peek result)
|
||||
|
||||
point (get-point cur-cmd)
|
||||
point (helpers/segment->point cur-cmd)
|
||||
|
||||
old-prev-point (get-point prev-cmd)
|
||||
new-prev-point (get-point (peek subpath))
|
||||
old-prev-point (helpers/segment->point prev-cmd)
|
||||
new-prev-point (helpers/segment->point (peek subpath))
|
||||
|
||||
remove? (contains? points point)
|
||||
|
||||
|
@ -724,8 +711,8 @@
|
|||
(flatten)
|
||||
(into []))
|
||||
|
||||
(let [prev-point (get-point prev-cmd)
|
||||
cur-point (get-point cur-cmd)
|
||||
(let [prev-point (helpers/segment->point prev-cmd)
|
||||
cur-point (helpers/segment->point cur-cmd)
|
||||
|
||||
cur-cmd (cond-> cur-cmd
|
||||
(and (contains? points prev-point)
|
||||
|
@ -803,7 +790,7 @@
|
|||
[content point->merge-point]
|
||||
(let [replace-command
|
||||
(fn [cmd]
|
||||
(let [point (get-point cmd)]
|
||||
(let [point (helpers/segment->point cmd)]
|
||||
(if (contains? point->merge-point point)
|
||||
(let [merge-point (get point->merge-point point)]
|
||||
(-> cmd (update :params assoc :x (:x merge-point) :y (:y merge-point))))
|
||||
|
|
|
@ -216,7 +216,7 @@
|
|||
(butlast content)
|
||||
content)
|
||||
command (first content)
|
||||
to-p (path.helpers/command->point command)
|
||||
to-p (path.helpers/segment->point command)
|
||||
|
||||
[from-p move-p command-pts]
|
||||
(case (:command command)
|
||||
|
@ -224,8 +224,8 @@
|
|||
:close-path [move-p move-p (when move-p [move-p])]
|
||||
:line-to [to-p move-p (when (and from-p to-p) [from-p to-p])]
|
||||
:curve-to [to-p move-p
|
||||
(let [c1 (path.helpers/command->point command :c1)
|
||||
c2 (path.helpers/command->point command :c2)
|
||||
(let [c1 (path.helpers/segment->point command :c1)
|
||||
c2 (path.helpers/segment->point command :c2)
|
||||
curve [from-p to-p c1 c2]]
|
||||
(when (and from-p to-p c1 c2)
|
||||
(into [from-p to-p]
|
||||
|
|
|
@ -76,7 +76,7 @@
|
|||
|
||||
index (or index (count content))
|
||||
prefix (or prefix :c1)
|
||||
position (or position (path.segment/get-point (nth content (dec index))))
|
||||
position (or position (path.helpers/segment->point (nth content (dec index))))
|
||||
|
||||
old-handler (path.segment/handler->point content index prefix)
|
||||
|
||||
|
|
|
@ -260,7 +260,7 @@
|
|||
content (st/get-path state :content)
|
||||
points (path.segment/get-points content)
|
||||
|
||||
point (-> content (nth (if (= prefix :c1) (dec index) index)) (path.segment/get-point))
|
||||
point (-> content (nth (if (= prefix :c1) (dec index) index)) (path.helpers/segment->point))
|
||||
handler (-> content (nth index) (path.segment/get-handler prefix))
|
||||
|
||||
[op-idx op-prefix] (path.segment/opposite-index content index prefix)
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
[app.common.data.macros :as dm]
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.types.path :as path]
|
||||
[app.common.types.path.helpers :as path.helpers]
|
||||
[app.common.types.path.segment :as path.segment]
|
||||
[app.main.data.workspace.path :as drp]
|
||||
[app.main.snap :as snap]
|
||||
|
@ -299,7 +300,7 @@
|
|||
(mf/with-memo [content-points]
|
||||
(into #{} content-points))
|
||||
|
||||
last-p (->> content last path.segment/get-point)
|
||||
last-p (->> content last path.helpers/segment->point)
|
||||
|
||||
handlers
|
||||
(mf/with-memo [content]
|
||||
|
@ -422,7 +423,7 @@
|
|||
(let [[snap-selected snap-points]
|
||||
(cond
|
||||
(some? drag-handler) [#{drag-handler} points]
|
||||
(some? preview) [#{(path.segment/get-point preview)} points]
|
||||
(some? preview) [#{(path.helpers/segment->point preview)} points]
|
||||
(some? moving-handler) [#{moving-handler} points]
|
||||
:else
|
||||
[(->> selected-points (map base->point) (into #{}))
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
WARNING: Pending to be removed from codebase once completly unused"
|
||||
(:require
|
||||
[app.common.geom.point :as gpt]
|
||||
[app.common.types.path.segment :as path.segm]
|
||||
[app.common.types.path.helpers :refer [segment->point]]
|
||||
[app.util.array :as arr]))
|
||||
|
||||
(defn pt=
|
||||
|
@ -122,7 +122,7 @@
|
|||
(try
|
||||
(let [result (make-array (count content))]
|
||||
(reduce (fn [last-move current]
|
||||
(let [point (path.segm/get-point current)
|
||||
(let [point (segment->point current)
|
||||
current-move? (= :move-to (:command current))
|
||||
last-move (if current-move? point last-move)]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue