Skip to content

Commit 2ea425a

Browse files
committed
fixed tab support in AnsiCte
1 parent 4839c4c commit 2ea425a

File tree

11 files changed

+125
-57
lines changed

11 files changed

+125
-57
lines changed

src/WinPrint.Console/WinPrint.Console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AssemblyName>winprint</AssemblyName>
1010
<StartupObject></StartupObject>
1111

12-
<Version>2.0.3.90</Version>
12+
<Version>2.0.3.95</Version>
1313
<Company>Kindel Systems</Company>
1414
<Product>winprint</Product>
1515
<Authors>Charlie Kindel</Authors>

src/WinPrint.Core/ContentTypeEngines/AnsiCte.cs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static AnsiCte Create() {
4141
private DynamicScreen _screen;
4242
public IAnsiDecoderClient DecoderClient { get => (IAnsiDecoderClient)_screen; }
4343

44-
private float _lineHeight;
44+
private SizeF _charSize;
4545
private int _linesPerPage;
4646

4747
private float lineNumberWidth;
@@ -118,24 +118,24 @@ public override async Task<int> RenderAsync(System.Drawing.Printing.PrinterResol
118118
g.PageUnit = GraphicsUnit.Display; // Display is 1/100th"
119119
}
120120

121-
_lineHeight = _cachedFont.GetHeight(dpiY);
121+
_charSize = MeasureString(g, _cachedFont, "W");
122122

123-
if (PageSize.Height < _lineHeight) {
123+
if (PageSize.Height < (int)Math.Floor(_charSize.Height)) {
124124
throw new InvalidOperationException("The line height is greater than page height.");
125125
}
126126

127127
// Round down # of lines per page to ensure lines don't clip on bottom
128-
_linesPerPage = (int)Math.Floor(PageSize.Height / _lineHeight);
128+
_linesPerPage = (int)Math.Floor(PageSize.Height / (int)Math.Floor(_charSize.Height));
129129

130130
// 3 digits + 1 wide - Will support 999 lines before line numbers start to not fit
131131
// TODO: Make line number width dynamic
132132
// Note, MeasureString is actually dependent on lineNumberWidth!
133-
lineNumberWidth = ContentSettings.LineNumbers ? MeasureString(g, _cachedFont, new string('0', 4)).Width : 0;
133+
lineNumberWidth = ContentSettings.LineNumbers ? _charSize.Width * 4 : 0;
134134

135135
// This is the shortest line length (in chars) that we think we'll see.
136136
// This is used as a performance optimization (probably premature) and
137137
// could be 0 with no functional change.
138-
_minLineLen = (int)((PageSize.Width - lineNumberWidth) / MeasureString(g, _cachedFont, "W").Width);
138+
_minLineLen = (int)((PageSize.Width - lineNumberWidth) / (int)Math.Floor(_charSize.Width));
139139

140140
// Note, MeasureLines may increment numPages due to form feeds and line wrapping
141141
_screen = new DynamicScreen(_minLineLen);
@@ -177,9 +177,9 @@ private SizeF MeasureString(Graphics g, string text, System.Drawing.Font font, o
177177
g.TextRenderingHint = ContentTypeEngineBase.TextRenderingHint;
178178

179179
// determine width
180-
var fontHeight = _lineHeight;
180+
var fontHeight = (int)Math.Floor(_charSize.Height);
181181
// Use page settings including lineNumberWidth
182-
var proposedSize = new SizeF(PageSize.Width, _lineHeight + (_lineHeight / 2));
182+
var proposedSize = new SizeF(PageSize.Width, (int)Math.Floor(_charSize.Height) + ((int)Math.Floor(_charSize.Height) / 2));
183183
var size = g.MeasureString(text, font, proposedSize, ContentTypeEngineBase.StringFormat, out charsFitted, out linesFilled);
184184

185185
// TODO: HACK to work around MeasureString not working right on Linux
@@ -206,7 +206,7 @@ public override void PaintPage(Graphics g, int pageNum) {
206206
var firstLineOnPage = _linesPerPage * (pageNum - 1);
207207
int i;
208208
for (i = firstLineOnPage; i < firstLineOnPage + _linesPerPage && i < _screen.Lines.Count; i++) {
209-
var yPos = (i - (_linesPerPage * (pageNum - 1))) * _lineHeight;
209+
var yPos = (i - (_linesPerPage * (pageNum - 1))) * (int)Math.Floor(_charSize.Height);
210210
var x = ContentSettings.LineNumberSeparator ? (int)(lineNumberWidth - 6 - MeasureString(g, _cachedFont, $"{_screen.Lines[i].LineNumber}").Width) : 0;
211211
// Line #s
212212
if (_screen.Lines[i].LineNumber > 0) {
@@ -220,7 +220,7 @@ public override void PaintPage(Graphics g, int pageNum) {
220220
// Line # separator (draw even if there's no line number, but stop at end of doc)
221221
// TODO: Support setting color of line #s and separator
222222
if (ContentSettings.LineNumbers && ContentSettings.LineNumberSeparator && lineNumberWidth != 0) {
223-
g.DrawLine(Pens.Gray, lineNumberWidth - 2, yPos, lineNumberWidth - 2, yPos + _lineHeight);
223+
g.DrawLine(Pens.Gray, lineNumberWidth - 2, yPos, lineNumberWidth - 2, yPos + (int)Math.Floor(_charSize.Height));
224224
}
225225

226226
// Text
@@ -244,14 +244,25 @@ public override void PaintPage(Graphics g, int pageNum) {
244244

245245
var text = _screen.Lines[i].Text[run.Start..(run.Start + run.Length)];
246246

247-
var proposedSize = new SizeF(PageSize.Width, _lineHeight);
248-
var size = g.MeasureString(text, font, proposedSize, ContentTypeEngineBase.StringFormat, out int charsFitted, out int linesFilled);
249-
g.DrawString(text, font, new SolidBrush(fg), xPos, yPos, ContentTypeEngineBase.StringFormat);
247+
for (var c = 0; c < text.Length; c++) {
248+
g.DrawString($"{text[c]}", font, new SolidBrush(fg), xPos + (c * (int)Math.Floor(_charSize.Width)), yPos, ContentTypeEngineBase.StringFormat);
249+
}
250+
251+
if (ContentSettings.Diagnostics && run.HasTab) {
252+
var pen = new Pen(Color.Red, 1);
253+
g.DrawRectangle(pen, xPos, yPos, text.Length * (int)Math.Floor(_charSize.Width), (int)Math.Floor(_charSize.Height));
254+
g.DrawString($"→", font, new SolidBrush(Color.DarkGray), xPos, yPos, ContentTypeEngineBase.StringFormat);
255+
}
256+
xPos += (int)Math.Floor(_charSize.Width) * text.Length;
257+
258+
//var proposedSize = new SizeF(PageSize.Width, _lineHeight);
259+
//var size = g.MeasureString(text, font, proposedSize, ContentTypeEngineBase.StringFormat, out int charsFitted, out int linesFilled);
260+
//g.DrawString(text, font, new SolidBrush(fg), xPos, yPos, ContentTypeEngineBase.StringFormat);
250261

251-
xPos += size.Width;
262+
//xPos += size.Width;
252263
}
253264
if (ContentSettings.Diagnostics) {
254-
g.DrawRectangle(Pens.Red, lineNumberWidth, yPos, PageSize.Width - lineNumberWidth, _lineHeight);
265+
g.DrawRectangle(Pens.Red, lineNumberWidth, yPos, PageSize.Width - lineNumberWidth, (int)Math.Floor(_charSize.Height));
255266
}
256267
}
257268

src/WinPrint.Core/Services/PygmentsConverterService.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public async Task<string> ConvertAsync(string document, string style, string lan
7575
if (!string.IsNullOrEmpty($"{file}.an") && File.Exists($"{file}.an")) {
7676
Log.Debug("Reading {file}", $"{file}.an");
7777
document = await File.ReadAllTextAsync($"{file}.an", Encoding.UTF8).ConfigureAwait(true);
78+
79+
// HACK: Because of this bug: https://github.com/pygments/pygments/issues/1435
80+
if (document[^1] == '\n')
81+
document = document.Remove(document.Length - 1, 1);
7882
}
7983
}
8084

src/WinPrint.Core/WinPrint.Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PropertyGroup>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<Platforms>AnyCPU;x64;x86</Platforms>
7-
<Version>2.0.3.562</Version>
7+
<Version>2.0.3.591</Version>
88
<Company>Kindel Systems</Company>
99
<Product>winprint</Product>
1010
<Authors>Charlie Kindel</Authors>
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
First Line ------ 012345678901234567890123456789012345678901234567890|
2-
1 2 3 4 5 6 7 8|
2+
0 1 2 3 4 5 6 7|
33
This is a test of the emergency broadcast system of America.
4-
This is a test of the emergency broadcast system of Americaa
4+
This is a test of the emergency broadcast system of Americas
55
| %^*&^ * & % $ $ ()(* * * * *& &*(*&@&!*&!) +}{ } :";' :" ''.,/? s |
66
01234567890123456789012345678901234567890123456789012345678901234567890|
77
[ <> >< {} }{ .. ,, // ?? '' "" ;; :: \\ || == ++ -- __ () ** ^^ %% $$ ]
@@ -17,15 +17,14 @@ The following lines should be same length as this one when using a fixed-pitch f
1717
0123456789 +==================================+ 012345678901234567890123456789012
1818
0123456789 | | 01234567890123456789012345678901
1919
0123456789 | Unicode Power Symbols: ⏻ ⏼ ⏽ ⭘ ⏾ | 0123456789012345678901234567890
20-
0123456789 | With a fixed pitch font this | 01234567890123456789012345678
21-
0123456789 | will look correct. | 0123456789012345678901234567
22-
0123456789 | | 012345678901234567890123456
23-
0123456789 +----------------------------------+ 01234567890123456789012345
24-
0123456789 0123456789012345678901234
25-
01234567890123456789012345678901234567890123456789012345678901234567890123
26-
Line 16 - Tab tests:
27-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
28-
1 2 3 4 5 6 7 8 9 10
20+
0123456789 | With a fixed pitch font this | 012345678901234567890123456789
21+
0123456789 | will look correct. | 01234567890123456789012345678
22+
0123456789 | | 0123456789012345678901234567
23+
0123456789 +----------------------------------+ 012345678901234567890123456
24+
0123456789 01234567890123456789012345
25+
Line 25 - Tab tests:
26+
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
27+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |9
2928
<-Tab
3029
<-4 spaces
3130
<-Tab-> <-Tab
@@ -36,15 +35,16 @@ Line 16 - Tab tests:
3635
01230123 <-2 tabs
3736
012301230123 <-1 tabs
3837
0123012301230123<-0 tabs
39-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
40-
0123 5678 0123456789012345678901234567890123
41-
0123 5 +====Illustrating Tab Expansion====+ 012345678901234567890123456789012
42-
0123 56 | | 01234567890123456789012345678901
43-
0123 567 | Unicode Power Symbols: ⏻ ⏼ ⏽ ⭘ ⏾ | 0123456789012345678901234567890
44-
0 5678 | With a fixed pitch font this | 01234567890123456789012345678
45-
01 5678 | will look correct. | 0123456789012345678901234567
46-
012 5678 | | 012345678901234567890123456
47-
1 +----------------------------------+ 01234567890123456789012345
48-
0123 5678 0123456789012345678901234
49-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
38+
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
39+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |9
40+
0123 8 23456789012345678901234567890123 890
41+
0123 8 +====Illustrating Tab Expansion====+ 2345678901234567890123456789012 4567890
42+
0123 89 | | 23456789012345678901234567890123 890
43+
0123 890 | Unicode Power Symbols: ⏻ ⏼ ⏽ ⭘ ⏾ | 234567890123456789012345678901234 890
44+
0 8901 | With a fixed pitch font this | 2345678901234567890123456789012345 890
45+
01 8901 | will look correct. | 23456789012345678901234567890123456 890
46+
012 8901 | | 2345678901234567890123456789012345678
47+
4 +----------------------------------+ 23456789012345678901234567890123456789
48+
0123 8901 234567890123456789012345678901234567890
49+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |9
5050
Line 50 - End of File

testfiles/Fixed Pitch Alignment.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
First Line ------ 012345678901234567890123456789012345678901234567890|
2-
1 2 3 4 5 6 7 8|
2+
0 1 2 3 4 5 6 7|
33
This is a test of the emergency broadcast system of America.
4-
This is a test of the emergency broadcast system of Americaa
4+
This is a test of the emergency broadcast system of Americas
55
| %^*&^ * & % $ $ ()(* * * * *& &*(*&@&!*&!) +}{ } :";' :" ''.,/? s |
66
01234567890123456789012345678901234567890123456789012345678901234567890|
77
[ <> >< {} }{ .. ,, // ?? '' "" ;; :: \\ || == ++ -- __ () ** ^^ %% $$ ]
@@ -22,10 +22,9 @@ The following lines should be same length as this one when using a fixed-pitch f
2222
0123456789 | | 0123456789012345678901234567
2323
0123456789 +----------------------------------+ 012345678901234567890123456
2424
0123456789 01234567890123456789012345
25-
012345678901234567890123456789012345678901234567890123456789012345678901234
26-
Line 16 - Tab tests:
27-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
28-
1 2 3 4 5 6 7 8 9 10
25+
Line 25 - Tab tests:
26+
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
27+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |
2928
<-Tab
3029
<-4 spaces
3130
<-Tab-> <-Tab
@@ -36,15 +35,16 @@ Line 16 - Tab tests:
3635
01230123 <-2 tabs
3736
012301230123 <-1 tabs
3837
0123012301230123<-0 tabs
39-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
40-
0123 8 23456789012345678901234567890123
41-
0123 8 +====Illustrating Tab Expansion====+ 2345678901234567890123456789012
42-
0123 89 | | 234567890123456789012345678901
43-
0123 890 | Unicode Power Symbols: ⏻ ⏼ ⏽ ⭘ ⏾ | 23456789012345678901234567890
44-
0 8901 | With a fixed pitch font this | 2345678901234567890123456789
45-
01 8901 | will look correct. | 234567890123456789012345678
46-
012 8901 | | 23456789012345678901234567
47-
4 +----------------------------------+ 2345678901234567890123456
48-
0123 8901 234567890123456789012346
49-
01234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
38+
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
39+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |
40+
0123 8 23456789012345678901234567890123 89
41+
0123 8 +====Illustrating Tab Expansion====+ 2345678901234567890123456789012 456789
42+
0123 89 | | 23456789012345678901234567890123 89
43+
0123 890 | Unicode Power Symbols: ⏻ ⏼ ⏽ ⭘ ⏾ | 234567890123456789012345678901234 89
44+
0 8901 | With a fixed pitch font this | 2345678901234567890123456789012345 89
45+
01 8901 | will look correct. | 23456789012345678901234567890123456 89
46+
012 8901 | | 2345678901234567890123456789012345678
47+
4 +----------------------------------+ 23456789012345678901234567890123456789
48+
0123 8901 23456789012345678901234567890123456789
49+
0 | | 1 | | 2| | |3 | | 4| | |5 | | 6| | |7 | | 8| | |
5050
Line 50 - End of File

0 commit comments

Comments
 (0)