From 63a571647035682e5d177186b58d59a58ce37a8c Mon Sep 17 00:00:00 2001 From: Jaro Date: Wed, 26 Apr 2023 19:26:55 +0200 Subject: [PATCH] Define BoxOptions and TextOptions for formatting. Differentiating the two record types reflects the fact that the root inline box cannot be styled but its text contents can. --- balkon.cabal | 2 + .../ParagraphLayout/Internal/BoxOptions.hs | 52 +++++++++++++++++++ .../ParagraphLayout/Internal/TextOptions.hs | 50 ++++++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/Data/Text/ParagraphLayout/Internal/BoxOptions.hs create mode 100644 src/Data/Text/ParagraphLayout/Internal/TextOptions.hs diff --git a/balkon.cabal b/balkon.cabal index 41f9d4d..31af3d7 100644 --- a/balkon.cabal +++ b/balkon.cabal @@ -102,6 +102,7 @@ library balkon-internal -- Modules exported to tests and to the public part of the library. exposed-modules: Data.Text.ParagraphLayout.Internal.BiDiReorder, + Data.Text.ParagraphLayout.Internal.BoxOptions, Data.Text.ParagraphLayout.Internal.Break, Data.Text.ParagraphLayout.Internal.Fragment, Data.Text.ParagraphLayout.Internal.LineHeight, @@ -117,6 +118,7 @@ library balkon-internal Data.Text.ParagraphLayout.Internal.Run, Data.Text.ParagraphLayout.Internal.Span, Data.Text.ParagraphLayout.Internal.TextContainer, + Data.Text.ParagraphLayout.Internal.TextOptions, Data.Text.ParagraphLayout.Internal.Zipper -- Modules used purely internally and not in any tests. diff --git a/src/Data/Text/ParagraphLayout/Internal/BoxOptions.hs b/src/Data/Text/ParagraphLayout/Internal/BoxOptions.hs new file mode 100644 index 0000000..1a96ed7 --- /dev/null +++ b/src/Data/Text/ParagraphLayout/Internal/BoxOptions.hs @@ -0,0 +1,52 @@ +module Data.Text.ParagraphLayout.Internal.BoxOptions + ( BoxOptions (..) + , BoxSpacing (..) + , defaultBoxOptions + ) +where + +import Data.Int (Int32) + +-- | Style options to be applied to an inline box. +-- +-- This record type is likely to be extended in the future. +-- Use `defaultBoxOptions` and update it with specific record selectors +-- instead of constructing `BoxOptions` directly. +data BoxOptions = BoxOptions + + { boxSpacing :: BoxSpacing + -- Determines amount of empty space to add before the first fragment + -- and/or after the last fragment of this box. + -- + -- This can be used to reserve space for a left margin, border, + -- and/or padding. + + } + deriving (Eq) + +-- | Determines the amount of empty space to add around a box. +data BoxSpacing + + -- | Specification using absolute directions, unaffected by text direction. + -- + -- (However, text direction is still used in case of fragmentation, + -- to determine how to map the first (last) fragment to left (right).) + = BoxSpacingLeftRight + + Int32 + -- ^ Amount of empty space to add to the left side of the + -- first fragment (for LTR text) or last fragment (for RTL text) + -- created from this box. + + Int32 + -- ^ Amount of empty space to add to the right side of the + -- first fragment (for RTL text) or last fragment (for LTR text) + -- created from this box. + + deriving (Eq, Show, Read) + +-- | `BoxOptions` with default values. +defaultBoxOptions :: BoxOptions +defaultBoxOptions = BoxOptions + { boxSpacing = BoxSpacingLeftRight 0 0 + } diff --git a/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs new file mode 100644 index 0000000..f55f35a --- /dev/null +++ b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs @@ -0,0 +1,50 @@ +module Data.Text.ParagraphLayout.Internal.TextOptions + ( TextOptions (..) + , defaultTextOptions + ) +where + +import Data.Text.Glyphize (Font, emptyFont) + +import Data.Text.ParagraphLayout.Internal.LineHeight + +-- | Style options to be applied to a text sequence. +-- +-- This record type is likely to be extended in the future. +-- Use `defaultTextOptions` and update it with specific record selectors +-- instead of constructing `TextOptions` directly. +data TextOptions = TextOptions + + { textFont :: Font + -- ^ Font to be used for shaping and measurement. + -- Make sure to set its scale (see `Data.Text.Glyphize.optionScale`) using + -- the same units that you want in the output. + + , textLineHeight :: LineHeight + -- ^ Preferred line height of the resulting fragments. + + , textLanguage :: String + -- ^ IETF BCP 47 language tag, such as the value expected to be found in + -- the HTML @lang@ attribute, specifying the primary language for the + -- span's text content. An empty string explicitly means "language unknown". + -- + -- Used for selecting the appropriate glyphs and line breaking rules, + -- primarily in East Asian languages. + + -- TODO: textVerticalAlign + -- TODO: textLetterSpacing + -- TODO: textWordSpacing + -- TODO: textDirection + -- TODO: textFontFeatures + -- TODO: textSoftBreaks + + } + deriving (Eq) + +-- | `TextOptions` with default values. +defaultTextOptions :: TextOptions +defaultTextOptions = TextOptions + { textFont = emptyFont + , textLineHeight = Normal + , textLanguage = "" + } -- 2.30.2