module Main (main) where import Data.Foldable (foldl') import Data.Text (Text) import Data.Text qualified as Text import Data.Text.IO qualified as Text.IO import System.Environment (getArgs) import Text.Printf (printf) import Demo qualified import KB.Text.Layout.Break qualified as Break import KB.Text.Layout.Html qualified as Html import KB.Text.Layout.Measure (Span (..), Style) import KB.Text.Layout.Measure qualified as Measure import KB.Text.Shape qualified as TextShape unit, gutter, pad :: Float unit = 11 gutter = 16 pad = 12 main :: IO () main = do args <- getArgs let (headFile, bodyFile) = case args of a : b : _ -> (a, b) [a] -> (a, a) [] -> ("demos/assets/Ubuntu-B.kbts.zst", "demos/assets/NotoSans-Regular.kbts.zst") TextShape.withContext \shape -> do headFont <- Demo.pushFont shape headFile bodyFont <- Demo.pushFont shape bodyFile ctx <- Measure.createLayoutContext shape heading <- Measure.newStyle ctx headFont 1.4 body <- Measure.newStyle ctx bodyFont 1.0 pill <- Measure.newStyle ctx bodyFont 1.0 printf "heading em %.3f units, body em %.3f units at a shared cap grid\n" heading.em body.em let atomPill label = do w <- (+ 0.6) <$> Measure.measure ctx pill label pure (AtomSpan pill label w) v24 <- atomPill "v2.4" rc1 <- atomPill "v2.5-rc1" prepped <- traverse (Measure.prepareStyled ctx) (corpus heading body v24 rc1) let cssFont family style extra = Demo.fontCss unit style [family] <> extra styleCss k | k == heading.key = cssFont "Head" heading ";color:#101828" | k == pill.key = cssFont "Body" pill ";background:#e3e8ff;border-radius:6px;text-align:center;color:#3538cd" | otherwise = cssFont "Body" body ";color:#475467" section cols containerPx = let colPx = (containerPx - gutter * fromIntegral (cols - 1)) / fromIntegral cols textW = (colPx - 2 * pad) / unit card prep = let ranges = Break.layoutGreedy prep textW opts = (Demo.options unit textW body (cssFont "Body" body "")){Html.styleCss = styleCss} in (Html.height opts (length ranges) + 2 * pad, Html.render opts prep ranges) (placed, height) = pack cols gutter (map card prepped) cardDiv (col, y, h, inner) = Demo.at (fromIntegral col * (colPx + gutter)) y ( ";width:" <> Demo.px (colPx - 2 * pad) <> ";height:" <> Demo.px (h - 2 * pad) <> ";padding:" <> Demo.px pad <> ";background:#f7f7fb;border-radius:8px" ) inner in Text.concat [ Demo.caption (Text.pack (printf "%d column(s) at %.0fpx, %.1f caps of text per column" cols containerPx textW)) , Demo.canvas containerPx height (Text.concat (map cardDiv placed)) ] fontFace = Demo.fontFaces [("Head", headFile), ("Body", bodyFile)] sections = [section 3 820, section 2 544, section 1 268, section 2 360] Text.IO.writeFile "demos/masonry.html" $ Html.page "kb-text-layout masonry" (fontFace <> Text.intercalate "