Layout: pages and elements

A page namespace defines (page c a):

It returns hiccup (preferred) or maps:

(ns pages.p03
  (:require [doc.core :as doc] [lib.layout :as l]))

(defn page [c a]
  [:page {:bg :crema}
   [:text {:x 20 :y 24 :w 170 :style :h1} (c :p03/title)]
   [:photo {:x 20 :y 60 :w 170 :h 120 :src (a :img/quarry) :focus [0.5 0.3]}]
   [:box {:x 20 :y 190 :w 80}
    [:text {:style :body} (c :p03/body)]
    [:text {:mt 4 :style :cap} (c :p03/caption)]]
   (l/folio)])

Top-level elements are placed at :x :y (from the top left; :right instead of :x measures from the right edge). Inside a :box, children flow top to bottom (:mt = space above), unless they have :x/:y themselves (then they are placed inside the box).

[:page {:bg :color :w :h} ...]: :w/:h override the size for this page only.

Elements

Element Properties Notes
:text :style (a token style or a map), :override (map merged into the style), :w, :max-w, :align (:left :center :right), :nowrap, :fill, :inset, :border, :radius, :shrink body is the copy string; or :runs [{:style :s :text "..."} {:gap 4} ...] for mixed styles on one line
:box :w, :h, :fill, :fill-alpha, :inset (padding: n, [v h], [t r b l]), :border, :radius, :clip, :shrink (as wide as its content), :align, :valign (:top :middle :bottom) children flow inside
:row :gap, :align (:top :center :bottom :baseline), :w children side by side; a child's :w fixes its width, :flex n shares the rest
:grid :cols (e.g. [[:fr 1] [:fr 2] 40 :auto]), :gap, :col-gap, :row-gap, :align, :w children fill cells left to right
:columns :count, :gap, :w, :h text flowing over columns
:photo :src (asset), :w, :h, :focus [fx fy] (which part stays in view, 0-1), :zoom {:scale s :origin [ox oy]}, :contain (whole image, no crop), :shape (:arch :circle), :radius, :border [w color] covers its frame like CSS object-fit; framing people set in the web app applies on top; may have children (overlays)
:svg :src, :w, :h vector artwork (SVG or PDF asset)
:rect :w, :h, :fill, :alpha, :border, :radius
:rule :w, :h (thickness), :color, :alpha, :dash (:dashed :dotted) a line
:disc :d, :fill a circle with centred children
:badge :style, :min-w, :h, :pad-x, :border [w color], :radius, :fill copy centred in a pill that grows with the text
:space :h vertical space in a flow

:border is [width color], [width color alpha :dashed], or per side {:top [1 :ink] :bottom [...]}. Colours are names from tokens.edn :colors (or "#RRGGBB"). Every element can carry :id (for per-language overrides) and :mt.

Text styles

tokens.edn :styles maps names to {:family :sans :size 10 :weight 400 :italic false :lh 1.3 :tracking 0.05 :upper true :color :ink :alpha 0.8}. :lh is a multiple of the size (or :normal), :tracking is in em. Copy markup: **strong** and *em* (styled by the node's :strong/:em maps, e.g. {:strong {:weight 700} :em {:italic true}}), and a newline breaks the line; \* is a literal asterisk.

doc.core (require it as [doc.core :as doc])

doc/tokens (tokens.edn), doc/page ({:w :h :unit}), doc/page-count, doc/langs, (doc/lang), (doc/page-number), (doc/page-id), (doc/resolve-style :body {:size 12}), (doc/line-edges style) (the line box above/below the baseline, for exact vertical maths), and (doc/element :name (fn [node] hiccup)) to define your own elements, used like built-ins:

(ns lib.elements (:require [doc.core :as doc]))
(doc/element :stripes
  (fn [{:keys [w h colors]}]
    (into [:grid {:cols (vec (repeat (count colors) [:fr 1])) :w w}]
          (for [c colors] [:rect {:h h :fill c}]))))
;; then, in any page: [:stripes {:x 20 :y 40 :w 30 :h 2 :colors [:accent :ink :muted]}]

Everything in lib/ loads before pages; pages require lib namespaces by their path (lib/deck/layout.clj is deck.layout, or lib.layout for lib/layout.clj; the ns form decides). Code runs in a sandbox (no files, network or Java) with a time limit.