summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-03-06 23:32:59 +0100
committertv <tv@krebsco.de>2026-03-06 23:36:06 +0100
commit18fffd492fe7134ef1cc53e1725d1709ddbde20b (patch)
tree95a89458b1b7ebc67b02a53394609df5f095bd18 /src
parent7a9f03584573cbcef94a610e6aafa5d8be89722e (diff)
HyphenateSegments: Hyphenator -> Language
This way we don't need to define bad orphan instances.
Diffstat (limited to 'src')
-rw-r--r--src/TextViewport/Buffer/Item.hs8
-rw-r--r--src/TextViewport/Render/Segmentation.hs24
2 files changed, 13 insertions, 19 deletions
diff --git a/src/TextViewport/Buffer/Item.hs b/src/TextViewport/Buffer/Item.hs
index b08aaf4..81d18ef 100644
--- a/src/TextViewport/Buffer/Item.hs
+++ b/src/TextViewport/Buffer/Item.hs
@@ -15,13 +15,7 @@ data SegmentStrategy
= NoSegments
| FixedWidthSegments
| HyphenateSegments
- { hsDict :: H.Hyphenator
+ { hsLang :: H.Language
, hsCache :: HM.HashMap Text [(Text, Text)]
}
deriving (Eq, Show)
-
-instance Show H.Hyphenator where
- show _ = "<Hyphenator>"
-
-instance Eq H.Hyphenator where
- a == b = False
diff --git a/src/TextViewport/Render/Segmentation.hs b/src/TextViewport/Render/Segmentation.hs
index 584798e..55971ce 100644
--- a/src/TextViewport/Render/Segmentation.hs
+++ b/src/TextViewport/Render/Segmentation.hs
@@ -38,7 +38,7 @@ applyStrategy FixedWidthSegments width itemIx txt =
| (lineIx, (off, chunk)) <- zip [0..] allChunks
]
-applyStrategy (HyphenateSegments dict cache0) width itemIx txt =
+applyStrategy (HyphenateSegments lang cache0) width itemIx txt =
let rawLines = T.splitOn "\n" txt
-- fold over each physical line, accumulating:
@@ -59,13 +59,13 @@ applyStrategy (HyphenateSegments dict cache0) width itemIx txt =
-- -> Text
-- -> ([(Int, Text)], HM.HashMap Text [(Text, Text)], Int)
segmentOneLine (acc, cache, off0) line =
- let (chunks, cache1) = segmentWithHyphenationTeXLite dict width line cache
+ let (chunks, cache1) = segmentWithHyphenationTeXLite lang width line cache
offsets = scanOffsetsFrom off0 chunks
offNext = off0 + T.length line + 1
acc' = acc `DL.append` DL.fromList (zip offsets chunks)
in (acc', cache1, offNext)
--segmentOneLine (acc, cache, off0) line =
- -- let chunks = segmentWithHyphenationTeXLite dict width line
+ -- let chunks = segmentWithHyphenationTeXLite lang width line
-- offsets = scanOffsetsFrom off0 chunks
-- offNext = off0 + T.length line + 1 -- +1 for newline
-- acc' = acc ++ zip offsets chunks
@@ -102,17 +102,17 @@ scanOffsetsFrom start = go start
go !o (t:ts) = o : go (o + T.length t) ts
segmentWithHyphenationTeXLite
- :: H.Hyphenator
+ :: H.Language
-> Int
-> Text
-> HM.HashMap Text [(Text, Text)]
-> ([Text], HM.HashMap Text [(Text, Text)])
-segmentWithHyphenationTeXLite dict width txt cache0 =
+segmentWithHyphenationTeXLite lang width txt cache0 =
go cache0 (T.words txt)
where
go cache [] = ([], cache)
go cache ws =
- case lineCandidates dict width cache ws of
+ case lineCandidates lang width cache ws of
([], cache1) ->
let chunks = breakWordSafe width ws
in (chunks, cache1)
@@ -138,12 +138,12 @@ breakWordSafe width ws =
type Candidate = (Text, [Text], Bool)
lineCandidates
- :: H.Hyphenator
+ :: H.Language
-> Int
-> HM.HashMap Text [(Text, Text)]
-> [Text]
-> ([(Text, [Text], Bool)], HM.HashMap Text [(Text, Text)])
-lineCandidates dict width cache0 ws0 =
+lineCandidates lang width cache0 ws0 =
go [] [] cache0 ws0
where
go _ acc cache [] = (acc, cache)
@@ -162,7 +162,7 @@ lineCandidates dict width cache0 ws0 =
case HM.lookup w cache of
Just hs -> (hs, cache)
Nothing ->
- let hs = hyphenateWord dict w
+ let hs = hyphenateWord lang w
in (hs, HM.insert w hs cache)
hyphCands =
@@ -178,9 +178,9 @@ lineCandidates dict width cache0 ws0 =
then go (line ++ [w]) acc2 cache1 ws
else (acc2, cache1)
-hyphenateWord :: H.Hyphenator -> Text -> [(Text, Text)]
-hyphenateWord dict word =
- let parts = H.hyphenate dict (T.unpack word)
+hyphenateWord :: H.Language -> Text -> [(Text, Text)]
+hyphenateWord lang word =
+ let parts = H.hyphenate (H.languageHyphenator lang) (T.unpack word)
in [ ( T.pack (concat (take i parts))
, T.pack (concat (drop i parts))
)