From efcef179a0e0ca9bf126b3b5a32a8094f55faaf5 Mon Sep 17 00:00:00 2001 From: Jaro Date: Sat, 6 May 2023 11:52:44 +0200 Subject: [PATCH] Add required textDirection to TextOptions. --- CHANGELOG.md | 2 ++ lib/Data/Text/ParagraphLayout/Rich.hs | 1 + .../Text/ParagraphLayout/Internal/Plain.hs | 4 +++- .../ParagraphLayout/Internal/TextOptions.hs | 18 ++++++++++++---- .../Text/ParagraphLayout/Internal/TreeSpec.hs | 21 +++++++++++-------- .../ParagraphLayout/Rich/ParagraphData.hs | 4 ++-- test/Data/Text/ParagraphLayout/SpanData.hs | 8 +++---- 7 files changed, 38 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b61709..6b0fe57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ * Supports multiple fonts in the same paragraph. Where line height varies, text will be aligned by its top edge. + * Requires user to set base text direction (LTR or RTL). + * Moved existing plain text interface into `Data.Text.ParagraphLayout.Plain` submodule and marked it as deprecated. diff --git a/lib/Data/Text/ParagraphLayout/Rich.hs b/lib/Data/Text/ParagraphLayout/Rich.hs index d2de932..fac6ef7 100644 --- a/lib/Data/Text/ParagraphLayout/Rich.hs +++ b/lib/Data/Text/ParagraphLayout/Rich.hs @@ -33,6 +33,7 @@ module Data.Text.ParagraphLayout.Rich , textFont , textLineHeight , textLanguage + , textDirection -- ** Verification , paragraphSpanBounds , paragraphSpanTexts diff --git a/src/Data/Text/ParagraphLayout/Internal/Plain.hs b/src/Data/Text/ParagraphLayout/Internal/Plain.hs index 9397a46..394694f 100644 --- a/src/Data/Text/ParagraphLayout/Internal/Plain.hs +++ b/src/Data/Text/ParagraphLayout/Internal/Plain.hs @@ -2,6 +2,8 @@ module Data.Text.ParagraphLayout.Internal.Plain (layoutPlain) where +import Data.Text.Glyphize (Direction (DirLTR)) + import Data.Text.ParagraphLayout.Internal.BoxOptions import Data.Text.ParagraphLayout.Internal.Fragment import Data.Text.ParagraphLayout.Internal.ParagraphOptions @@ -36,7 +38,7 @@ plainToRich (P.Paragraph arr off spans opts) = R.Paragraph arr off rootNode opts (boxFromPlain baseOpts i s) defaultBoxOptions indexedSpans = zip [0 ..] spans - baseOpts = defaultTextOptions + baseOpts = (defaultTextOptions DirLTR) { textFont = paragraphFont opts , textLineHeight = paragraphLineHeight opts } diff --git a/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs index f55f35a..3a1a9bb 100644 --- a/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs +++ b/src/Data/Text/ParagraphLayout/Internal/TextOptions.hs @@ -4,7 +4,7 @@ module Data.Text.ParagraphLayout.Internal.TextOptions ) where -import Data.Text.Glyphize (Font, emptyFont) +import Data.Text.Glyphize (Direction, Font, emptyFont) import Data.Text.ParagraphLayout.Internal.LineHeight @@ -31,10 +31,16 @@ data TextOptions = TextOptions -- Used for selecting the appropriate glyphs and line breaking rules, -- primarily in East Asian languages. + , textDirection :: Direction + -- ^ Base text direction. + -- Used to determine which box fragment gets the left spacing and which + -- gets the right spacing when broken over multiple lines. + -- + -- TODO: Also use for the visual order of boxes. + -- TODO: textVerticalAlign -- TODO: textLetterSpacing -- TODO: textWordSpacing - -- TODO: textDirection -- TODO: textFontFeatures -- TODO: textSoftBreaks @@ -42,9 +48,13 @@ data TextOptions = TextOptions deriving (Eq) -- | `TextOptions` with default values. -defaultTextOptions :: TextOptions -defaultTextOptions = TextOptions +defaultTextOptions + :: Direction + -- ^ Required value for `textDirection`. + -> TextOptions +defaultTextOptions dir = TextOptions { textFont = emptyFont , textLineHeight = Normal , textLanguage = "" + , textDirection = dir } diff --git a/test/Data/Text/ParagraphLayout/Internal/TreeSpec.hs b/test/Data/Text/ParagraphLayout/Internal/TreeSpec.hs index f01305e..bc87407 100644 --- a/test/Data/Text/ParagraphLayout/Internal/TreeSpec.hs +++ b/test/Data/Text/ParagraphLayout/Internal/TreeSpec.hs @@ -6,6 +6,9 @@ import Data.Text.ParagraphLayout.Internal.ResolvedBox import Data.Text.ParagraphLayout.Internal.TextOptions import Data.Text.ParagraphLayout.Internal.Tree +t :: TextOptions +t = defaultTextOptions undefined + complexTree :: RootNode String complexTree = RootBox $ Box [ TextSequence "a" 1 @@ -20,7 +23,7 @@ complexTree = RootBox $ Box [ TextSequence "d" 1 , TextSequence "e" 1 ] - defaultTextOptions + t ) defaultBoxOptions , TextSequence "f" 1 @@ -33,16 +36,16 @@ complexTree = RootBox $ Box (Box [ TextSequence "h" 1 ] - defaultTextOptions + t ) defaultBoxOptions , TextSequence "i" 1 ] - defaultTextOptions + t ) defaultBoxOptions ] - defaultTextOptions + t ) defaultBoxOptions , InlineBox @@ -54,7 +57,7 @@ complexTree = RootBox $ Box [ TextSequence "j" 1 , TextSequence "k" 1 ] - defaultTextOptions + t ) defaultBoxOptions , InlineBox @@ -65,20 +68,20 @@ complexTree = RootBox $ Box (Box [ TextSequence "l" 1 ] - defaultTextOptions + t ) defaultBoxOptions ] - defaultTextOptions + t ) defaultBoxOptions , TextSequence "m" 1 ] - defaultTextOptions + t ) defaultBoxOptions ] - defaultTextOptions + t leafData :: Leaf d -> d leafData (TextLeaf d _ _ _) = d diff --git a/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs b/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs index 19bb42e..40892e2 100644 --- a/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs +++ b/test/Data/Text/ParagraphLayout/Rich/ParagraphData.hs @@ -6,7 +6,7 @@ module Data.Text.ParagraphLayout.Rich.ParagraphData where import Data.Text (pack) -import Data.Text.Glyphize (Font) +import Data.Text.Glyphize (Direction (DirLTR), Font) import Data.Text.ParagraphLayout.Internal.BoxOptions import Data.Text.ParagraphLayout.Internal.ParagraphOptions @@ -15,7 +15,7 @@ import Data.Text.ParagraphLayout.Internal.TextOptions import Data.Text.ParagraphLayout.Internal.TreeOfTexts t_ :: TextOptions -t_ = defaultTextOptions +t_ = defaultTextOptions DirLTR b_ :: BoxOptions b_ = defaultBoxOptions diff --git a/test/Data/Text/ParagraphLayout/SpanData.hs b/test/Data/Text/ParagraphLayout/SpanData.hs index 63fec2f..d254cad 100644 --- a/test/Data/Text/ParagraphLayout/SpanData.hs +++ b/test/Data/Text/ParagraphLayout/SpanData.hs @@ -6,7 +6,7 @@ module Data.Text.ParagraphLayout.SpanData where import Data.Text (pack) -import Data.Text.Glyphize (Font) +import Data.Text.Glyphize (Direction (DirLTR), Font) import Data.Text.ParagraphLayout.Internal.BoxOptions import Data.Text.ParagraphLayout.Internal.LineHeight @@ -23,7 +23,7 @@ emptySpan font = ResolvedSpan , spanIndex = 0 , spanOffsetInParagraph = 0 , spanText = pack "" - , spanTextOptions = defaultTextOptions + , spanTextOptions = (defaultTextOptions DirLTR) { textFont = font , textLineHeight = Normal , textLanguage = "en" @@ -39,7 +39,7 @@ czechHello font = ResolvedSpan , spanIndex = 0 , spanOffsetInParagraph = 0 , spanText = pack "Ahoj, světe!" - , spanTextOptions = defaultTextOptions + , spanTextOptions = (defaultTextOptions DirLTR) { textFont = font , textLineHeight = Normal , textLanguage = "cs" @@ -55,7 +55,7 @@ serbianMixedScript font = ResolvedSpan , spanIndex = 0 , spanOffsetInParagraph = 0 , spanText = pack "Vikipedija (Википедија)" - , spanTextOptions = defaultTextOptions + , spanTextOptions = (defaultTextOptions DirLTR) { textFont = font , textLineHeight = Normal , textLanguage = "sr" -- 2.30.2