Skip to content
Permalink
796c20d445
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
66 lines (61 sloc) 1.77 KB
(ns ulysses.lib.packer
(:require [clojure.walk :refer [keywordize-keys]]
[ulysses.utils :refer [map-subels ptr]]
[ulysses.extra :refer [Packer textDimensions]]))
(defn fit
[w h blocks]
(let [p (new Packer w h)]
(->> blocks
(clj->js)
(.fit p)
(js->clj)
(map
(fn [block]
(update block "fit" dissoc
"used" "right" "down" "w" "h"))))))
(defn text-dimensions
[text font-size]
(println text)
(-> text
(textDimensions font-size)
(js->clj)
(keywordize-keys)))
(def text-dimensions-memoized
(memoize text-dimensions))
(defn fit-words
[w h words]
[:div
{:style {:position :relative :width w :height h}}
(->> words
(map
(fn [{:keys [keyword weight]}]
(let [font-size (-> weight (* 80) (str "px"))]
(-> keyword
(text-dimensions-memoized font-size)
; padding
(update :w
(partial + 8))
; margin
(update :w
(partial + 4))
(update :h
(partial + 4))
; style
(assoc :font-size font-size)
(assoc :weight weight)))))
(doall)
(fit w h)
(map-subels
(fn [{:strs [w h fit name font-size weight]}]
[:a.keyword
{:style
{:fontSize font-size
:width (- w 4)
:height (- h 4)
:left (get fit "x")
:top (get fit "y")}
:class
(str "weight-" (Math/round (* 100 weight)))}
name])))])
(def fit-words-memoized
(memoize fit-words))