Skip to content
Permalink
4966fdb459
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
55 lines (52 sloc) 1.59 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
(sort-by :h)
(reverse)
(clj->js)
(.fit p)
(js->clj)
(keywordize-keys)
(map
(fn [block]
(update block :fit dissoc
:used :right :down :w :h))))))
(defn text-dimensions
[text options]
(-> text
(textDimensions (clj->js options))
(js->clj)
(keywordize-keys)))
(defn fit-words
[w h words]
(let [style-base {:fontFamily "Source Sans Pro" :fontWeight "100"}]
[:div
{:style {:position :relative :width w :height h}}
(->> words
(map
(fn [{:keys [name weight]}]
(let [style (assoc style-base :fontSize (-> weight (* 10) (Math/log) (* 35) (str "px")))]
(-> name
(text-dimensions style)
(update :w
(partial + 8))
(assoc :style style)
(assoc :weight weight)))))
(fit w h)
(map-subels
(fn [{:keys [w h fit name style weight]}]
[:a.keyword
{:style
(assoc style
:width w
:height h
:left (:x fit)
:top (:y fit))
:class
(str "weight-" (Math/round (* 100 weight)))}
name])))]))