Permalink
Cannot retrieve contributors at this time
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?
ulysses-front/src/cljs/ulysses/lib/packer.cljs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
66 lines (61 sloc)
1.77 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |