From 7d7cb04a82eb7945e379fcbfa3eb476020f238fe Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Wed, 15 Mar 2023 16:05:31 +1300 Subject: [PATCH] Expose utility to inform atlas construction. --- Graphics/Layout.hs | 16 ++++++++++++++-- cattrap.cabal | 4 +++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Graphics/Layout.hs b/Graphics/Layout.hs index 1f1dc15..0335beb 100644 --- a/Graphics/Layout.hs +++ b/Graphics/Layout.hs @@ -1,16 +1,22 @@ {-# LANGUAGE OverloadedStrings, RecordWildCards #-} module Graphics.Layout where -import Data.Text.ParagraphLayout (Paragraph(..), ParagraphOptions(..), Fragment) +import Data.Text.ParagraphLayout (Paragraph(..), ParagraphOptions(..), Fragment(..)) import Graphics.Layout.Box as B import Graphics.Layout.Grid as G import Graphics.Layout.Flow as F import Graphics.Layout.Inline as I -import Graphics.Layout.CSS.Internal (Font') +import Graphics.Layout.CSS.Internal (Font'(..)) import Data.Maybe (fromMaybe) +-- To gather glyphs for atlases. +import qualified Data.IntSet as IS +import qualified Data.Map.Strict as M +import qualified Data.Text.Glyphize as Hb +import Graphics.Text.Font.Choose (Pattern) + data LayoutItem m n x = LayoutFlow x (PaddedBox m n) [LayoutItem m n x] | LayoutGrid x (Grid m n) [(GridItem m n, LayoutItem m n x)] @@ -315,3 +321,9 @@ boxLayout parent self paginate = self8 (_, self6) = boxMaxHeight parent self5 (_, self7) = boxHeight parent self6 self8 = boxPosition (0, 0) self7 + +-- Useful for assembling glyph atlases. +glyphsPerFont :: LayoutItem x y z -> M.Map Pattern IS.IntSet +glyphsPerFont (LayoutSpan _ font self) = pattern font `M.singleton` IS.fromList glyphs + where glyphs = map fromEnum $ map Hb.codepoint $ map fst $ fragmentGlyphs self +glyphsPerFont node = M.unionsWith IS.union $ map glyphsPerFont $ layoutGetChilds node diff --git a/cattrap.cabal b/cattrap.cabal index 9802aa2..d996870 100644 --- a/cattrap.cabal +++ b/cattrap.cabal @@ -23,7 +23,9 @@ library Graphics.Layout.Inline, Graphics.Layout.Inline.CSS -- other-modules: -- other-extensions: - build-depends: base >=4.12 && <4.16, css-syntax, scientific, text, stylist-traits, fontconfig-pure, harfbuzz-pure, bytestring, balkon + build-depends: base >=4.12 && <4.16, containers, + css-syntax, scientific, text, stylist-traits, + fontconfig-pure, harfbuzz-pure, bytestring, balkon -- hs-source-dirs: default-language: Haskell2010 ghc-options: -Wincomplete-patterns -- 2.30.2