@@ -21,6 +21,8 @@ public IEnumerable<Word> GetWords(IReadOnlyList<Letter> letters)
21
21
22
22
var lettersSoFar = new List < Letter > ( 10 ) ;
23
23
24
+ var gapCountsSoFarByFontSize = new Dictionary < double , Dictionary < double , int > > ( ) ;
25
+
24
26
var y = default ( double ? ) ;
25
27
var lastX = default ( double ? ) ;
26
28
var lastLetter = default ( Letter ) ;
@@ -68,15 +70,48 @@ public IEnumerable<Word> GetWords(IReadOnlyList<Letter> letters)
68
70
continue ;
69
71
}
70
72
73
+ var letterHeight = Math . Max ( lastLetter . GlyphRectangle . Height , letter . GlyphRectangle . Height ) ;
74
+
71
75
var gap = letter . Location . X - ( lastLetter . Location . X + lastLetter . Width ) ;
72
76
var nextToLeft = letter . Location . X < lastX . Value - 1 ;
73
- var nextBigSpace = gap > Math . Max ( lastLetter . GlyphRectangle . Height , letter . GlyphRectangle . Height ) * 0.39 ;
77
+ var nextBigSpace = gap > letterHeight * 0.39 ;
74
78
var nextIsWhiteSpace = string . IsNullOrWhiteSpace ( letter . Value ) ;
75
79
var nextFontDiffers = ! string . Equals ( letter . FontName , lastLetter . FontName , StringComparison . OrdinalIgnoreCase ) && gap > letter . Width * 0.1 ;
76
80
var nextFontSizeDiffers = Math . Abs ( letter . FontSize - lastLetter . FontSize ) > 0.1 ;
77
81
var nextTextOrientationDiffers = letter . TextOrientation != lastLetter . TextOrientation ;
78
82
79
- if ( nextToLeft || nextBigSpace || nextIsWhiteSpace || nextFontDiffers || nextFontSizeDiffers || nextTextOrientationDiffers )
83
+ var suspectGap = false ;
84
+
85
+ if ( ! nextFontSizeDiffers && letter . FontSize > 0 && gap >= 0 )
86
+ {
87
+ var fontSize = Math . Round ( letter . FontSize ) ;
88
+ if ( ! gapCountsSoFarByFontSize . TryGetValue ( fontSize , out var gapCounts ) )
89
+ {
90
+ gapCounts = new Dictionary < double , int > ( ) ;
91
+ gapCountsSoFarByFontSize [ fontSize ] = gapCounts ;
92
+ }
93
+
94
+ var gapRounded = Math . Round ( gap , 2 ) ;
95
+ if ( ! gapCounts . ContainsKey ( gapRounded ) )
96
+ {
97
+ gapCounts [ gapRounded ] = 0 ;
98
+ }
99
+
100
+ gapCounts [ gapRounded ] ++ ;
101
+
102
+ // More than one type of gap.
103
+ if ( gapCounts . Count > 1 && gap > letterHeight * 0.16 )
104
+ {
105
+ var mostCommonGap = gapCounts . OrderByDescending ( x => x . Value ) . First ( ) ;
106
+
107
+ if ( gap > ( mostCommonGap . Key * 5 ) && mostCommonGap . Value > 1 )
108
+ {
109
+ suspectGap = true ;
110
+ }
111
+ }
112
+ }
113
+
114
+ if ( nextToLeft || nextBigSpace || nextIsWhiteSpace || nextFontDiffers || nextFontSizeDiffers || nextTextOrientationDiffers || suspectGap )
80
115
{
81
116
if ( lettersSoFar . Count > 0 )
82
117
{
0 commit comments