Skip to content

Commit 86e0bcd

Browse files
committed
pygments progress
1 parent 4e33349 commit 86e0bcd

28 files changed

+795
-231
lines changed

src/WinPrint.Console/OutWinPrint.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ await Task.Run(() => ServiceLocator.Current.UpdateService.GetLatestVersionAsync(
481481
if (!MyInvocation.BoundParameters.TryGetValue("Language", out var contentTypeEngine)) {
482482
if (!MyInvocation.BoundParameters.TryGetValue("ContentTypeEngine", out contentTypeEngine)) {
483483
// If neither were specified, smartly pick CTE
484-
contentTypeEngine = ContentTypeEngineBase.GetContentType(FileName);
484+
contentTypeEngine = ContentTypeEngineBase.GetContentTypeOrLanguage(FileName);
485485
}
486486
}
487487

@@ -792,20 +792,20 @@ public object GetDynamicParameters() {
792792
// -ContentTypeEngine
793793
runtimeDict.Add("ContentTypeEngine", new RuntimeDefinedParameter("ContentTypeEngine", typeof(string), new Collection<Attribute>() {
794794
new ParameterAttribute() {
795-
HelpMessage = "Optional name of the WinPrint Content Type Engine (or Language) to use (e.g. \"text/plain\" or \"csharp\". Specifying a langauge will choose the \"text/code\" CTE.",
795+
HelpMessage = "Optional name of the WinPrint Content Type Engine (or Language) to use (e.g. \"TextCte\".",
796796
ParameterSetName = "Print"
797797
},
798-
new ValidateSetAttribute(ContentTypeEngineBase.GetDerivedClassesCollection().Select(cte => cte.ContentTypeEngineName).ToArray())
798+
new ValidateSetAttribute(ContentTypeEngineBase.GetDerivedClassesCollection().Select(cte => cte.GetType().Name).ToArray())
799799
}));
800800

801-
//// -Language
802-
//runtimeDict.Add("Language", new RuntimeDefinedParameter("Language", typeof(String), new Collection<Attribute>() {
803-
// new ParameterAttribute() {
804-
// HelpMessage = "Optional language to use for syntax highlighting. Specifying a langauge will choose the \"text/code\" CTE.",
805-
// ParameterSetName = "Print"
806-
// },
807-
// new ValidateSetAttribute(ModelLocator.Current.Associations.Languages.Select(l => l.Id).ToArray())
808-
//}));
801+
// -Language
802+
runtimeDict.Add("Language", new RuntimeDefinedParameter("Language", typeof(String), new Collection<Attribute>() {
803+
new ParameterAttribute() {
804+
HelpMessage = "Optional language to use for syntax highlighting.",
805+
ParameterSetName = "Print"
806+
},
807+
new ValidateSetAttribute(ModelLocator.Current.Associations.Languages.Select(l => l.Id).ToArray())
808+
}));
809809

810810
return runtimeDict;
811811
}

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.26</Version>
12+
<Version>2.0.3.76</Version>
1313
<Company>Kindel Systems</Company>
1414
<Product>winprint</Product>
1515
<Authors>Charlie Kindel</Authors>

src/WinPrint.Core/ContentTypeEngines/AnsiCte.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace WinPrint.Core.ContentTypeEngines {
1919
/// Implements text/plain file type support.
2020
/// </summary>
2121
public class AnsiCte : ContentTypeEngineBase, IDisposable {
22-
private static readonly string _contentType = "text/ansi";
22+
private static readonly string[] _supportedContentTypes = { "text/plain", "text/ansi" };
2323
/// <summary>
2424
/// ContentType identifier (shorthand for class name).
2525
/// </summary>
26-
public override string ContentTypeEngineName => _contentType;
26+
public override string[] SupportedContentTypes => _supportedContentTypes;
2727

2828
public static AnsiCte Create() {
2929
var engine = new AnsiCte();
@@ -84,7 +84,7 @@ public override async Task<bool> SetDocumentAsync(string doc) {
8484
public override async Task<int> RenderAsync(System.Drawing.Printing.PrinterResolution printerResolution, EventHandler<string> reflowProgress) {
8585
LogService.TraceMessage();
8686

87-
if (document == null) {
87+
if (Document == null) {
8888
throw new ArgumentNullException("document can't be null for Render");
8989
}
9090

@@ -134,15 +134,14 @@ public override async Task<int> RenderAsync(System.Drawing.Printing.PrinterResol
134134
_minLineLen = (int)((PageSize.Width - lineNumberWidth) / MeasureString(g, _cachedFont, "W").Width);
135135

136136
// Note, MeasureLines may increment numPages due to form feeds and line wrapping
137-
IAnsiDecoder _vt100 = new AnsiDecoder();
138-
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
139137
_screen = new DynamicScreen(_minLineLen);
140-
_vt100.Encoding = CodePagesEncodingProvider.Instance.GetEncoding("ibm437");
141-
_vt100.Subscribe(_screen);
138+
IAnsiDecoder vt100 = new AnsiDecoder();
139+
vt100.Encoding = Encoding;
140+
vt100.Subscribe(_screen);
142141

143-
var bytes = _vt100.Encoding.GetBytes(document);
142+
var bytes = vt100.Encoding.GetBytes(Document);
144143
if (bytes != null && bytes.Length > 0) {
145-
_vt100.Input(bytes);
144+
vt100.Input(bytes);
146145
}
147146

148147
#if TESTVT100
@@ -279,11 +278,12 @@ public override void PaintPage(Graphics g, int pageNum) {
279278
fg = run.Attributes.ForegroundColor;
280279

281280
var text = _screen.Lines[i].Text[run.Start..(run.Start + run.Length)];
282-
var width = MeasureString(g, font, text).Width;
283-
RectangleF rect = new RectangleF(xPos, yPos, width, _lineHeight);
284-
g.DrawString(text, font, new SolidBrush(fg), rect, StringFormat);
285281

286-
xPos += width;
282+
var proposedSize = new SizeF(PageSize.Width, _lineHeight);
283+
var size = g.MeasureString(text, font, proposedSize, ContentTypeEngineBase.StringFormat, out int charsFitted, out int linesFilled);
284+
g.DrawString(text, font, new SolidBrush(fg), xPos, yPos, ContentTypeEngineBase.StringFormat);
285+
286+
xPos += size.Width;
287287
}
288288
if (ContentSettings.Diagnostics) {
289289
g.DrawRectangle(Pens.Red, lineNumberWidth, yPos, PageSize.Width - lineNumberWidth, _lineHeight);

src/WinPrint.Core/ContentTypeEngines/ContentTypeEngineBase.cs

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.IO;
1111
using System.Linq;
1212
using System.Runtime.CompilerServices;
13+
using System.Text;
1314
using System.Text.Json.Serialization;
1415
using System.Threading.Tasks;
1516
using Serilog;
@@ -21,8 +22,9 @@ namespace WinPrint.Core.ContentTypeEngines {
2122
/// Base class for Content/File Type Engines (CTEs)
2223
/// </summary>
2324
public abstract class ContentTypeEngineBase : ModelBase, INotifyPropertyChanged {
24-
private static string _defaultCteName = "text/plain";
25-
private static string _defaultSyntaxHighlighterCteName = "text/prism";
25+
public static string DefaultContentType = "text/plain";
26+
public static string DefaultCteClassName = "AnsiCte";
27+
public static string DefaultSyntaxHighlighterCteNameClassName = "PrismCte";
2628

2729
public new event PropertyChangedEventHandler PropertyChanged;
2830
protected new void OnPropertyChanged([CallerMemberName] string propertyName = null) {
@@ -49,14 +51,8 @@ protected void OnSettingsChanged(bool reflow) {
4951
/// <summary>
5052
/// ContentType identifier (shorthand for class name).
5153
/// </summary>
52-
public virtual string ContentTypeEngineName => _contentTypeEngineName;
53-
private static readonly string _contentTypeEngineName = "base";
54-
55-
public string Language {
56-
get => _fileType;
57-
set => SetField(ref _fileType, value);
58-
}
59-
private string _fileType;
54+
public virtual string[] SupportedContentTypes => _supportedContentTypes;
55+
private static readonly string[] _supportedContentTypes = null;
6056

6157
/// <summary>
6258
/// Calculated page size. Set by Sheet view model.
@@ -75,10 +71,18 @@ public string Language {
7571
/// </summary>
7672
[JsonIgnore]
7773
public string Document {
78-
get => document; set =>
74+
get => _document; set =>
7975
//LogService.TraceMessage($"Document is {document.Length} chars.");
80-
SetField(ref document, value);
76+
SetField(ref _document, value);
8177
}
78+
internal string _document = null;
79+
80+
/// <summary>
81+
/// The contents encdding of the file to be printed.
82+
/// </summary>
83+
[JsonIgnore]
84+
public Encoding Encoding { get => _encoding; set => SetField(ref _encoding, value); }
85+
private Encoding _encoding = Encoding.Default;
8286

8387
/// <summary>
8488
/// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
@@ -92,8 +96,6 @@ public static ICollection<ContentTypeEngineBase> GetDerivedClassesCollection() {
9296
return objects;
9397
}
9498

95-
internal string document = null;
96-
9799
/// <summary>
98100
/// These are the global StringFormat settings; set here to ensure all rendering and measuring uses same settings
99101
/// </summary>
@@ -130,12 +132,14 @@ public virtual async Task<int> RenderAsync(System.Drawing.Printing.PrinterResolu
130132
/// Creates the appropriate Content Type Engine instance given a content type string.
131133
/// </summary>
132134
/// <param name="contentType"></param>
133-
/// <returns></returns>
134-
public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string contentType) {
135+
/// <returns>ContentEngine, Language</returns>
136+
public static (ContentTypeEngineBase cte, string language) CreateContentTypeEngine(string contentType) {
135137
Debug.Assert(!string.IsNullOrEmpty(contentType));
138+
Debug.Assert(ModelLocator.Current.Associations != null);
139+
Debug.Assert(ModelLocator.Current.Associations.Languages != null);
136140

137-
// If contentType matches one of our CTE ContentTypeNames, this will succeed.
138-
ContentTypeEngineBase cte = GetDerivedClassesCollection().FirstOrDefault(c => c.ContentTypeEngineName == contentType);
141+
// If contentType matches one of our CTE Names, this will succeed.
142+
ContentTypeEngineBase cte = GetDerivedClassesCollection().FirstOrDefault(c => contentType == c.GetType().Name);
139143
string language = string.Empty;
140144

141145
if (cte == null) {
@@ -152,42 +156,60 @@ public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string c
152156
// ".ans"
153157
// },
154158
// Is it a file extension?
155-
var extLanguage = ModelLocator.Current.Associations.Languages.Where(lang => lang.Extensions.Contains(contentType)).FirstOrDefault();
159+
var extLanguage = ModelLocator.Current.Associations.Languages
160+
.Where(lang => lang.Extensions.Contains(contentType))
161+
.FirstOrDefault();
156162
if (extLanguage != null && !string.IsNullOrEmpty(extLanguage.Id)) {
157163
// Is Id a Cte Name?
158-
cte = GetDerivedClassesCollection().FirstOrDefault(c => c.ContentTypeEngineName == extLanguage.Id);
164+
cte = GetDerivedClassesCollection().FirstOrDefault(c => c.SupportedContentTypes.Contains(extLanguage.Id));
165+
159166
if (cte != null) {
160-
return cte;
167+
return (cte, string.Empty);
161168
}
162169
// It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
163170
language = extLanguage.Id;
164171
}
165172
else {
166173
// Is it found in a langauge alias?
167-
var alias = ModelLocator.Current.Associations.Languages.Where(lang => lang.Aliases.Contains(contentType)).FirstOrDefault();
174+
var alias = ModelLocator.Current.Associations.Languages
175+
.Where(lang => lang.Id == contentType || lang.Aliases.Contains(contentType))
176+
.FirstOrDefault();
168177
if (alias != null) {
169-
// Is Id a Cte Name?
170-
cte = GetDerivedClassesCollection().Where(c => c.ContentTypeEngineName == alias.Id).FirstOrDefault();
178+
// Is the Id supported directly?
179+
// If by muplitple, pick the default.
180+
var collection = GetDerivedClassesCollection()
181+
.Where(c => c.SupportedContentTypes.Contains(alias.Id));
182+
if (collection != null) {
183+
if (collection.Count() > 1) {
184+
cte = collection.Where(c => c.GetType().Name == DefaultCteClassName).First();
185+
}
186+
else {
187+
cte = collection.FirstOrDefault();
188+
}
189+
}
171190
if (cte != null) {
172-
return cte;
191+
return (cte, string.Empty);
173192
}
174193
// It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
175194
language = contentType;
176195
}
177196
}
178197

179198
if (string.IsNullOrEmpty(language)) {
180-
cte = GetDerivedClassesCollection().Where(c => c.ContentTypeEngineName == _defaultCteName).FirstOrDefault();
199+
cte = GetDerivedClassesCollection()
200+
.Where(c => c.SupportedContentTypes.Contains(DefaultCteClassName))
201+
.FirstOrDefault();
181202
}
182203
else {
183204
// It is a language. Needs to be Syntax Highlighted. Use the default Syntax Highlighter CTE
184-
cte = GetDerivedClassesCollection().Where(c => c.ContentTypeEngineName == _defaultSyntaxHighlighterCteName).FirstOrDefault();
185-
cte.Language = language;
205+
cte = GetDerivedClassesCollection()
206+
.Where(c => c.GetType().Name == DefaultSyntaxHighlighterCteNameClassName)
207+
.First();
186208
}
187209
}
188210

189211
Debug.Assert(cte != null);
190-
return cte;
212+
return (cte, language);
191213
}
192214

193215
/// <summary>
@@ -196,8 +218,8 @@ public static async Task<ContentTypeEngineBase> CreateContentTypeEngine(string c
196218
/// </summary>
197219
/// <param name="filePath"></param>
198220
/// <returns>The content type</returns>
199-
public static string GetContentType(string filePath) {
200-
var contentType = "text/plain";
221+
public static string GetContentTypeOrLanguage(string filePath) {
222+
var contentType = DefaultCteClassName;
201223

202224
if (string.IsNullOrEmpty(filePath)) {
203225
return contentType;
@@ -210,11 +232,18 @@ public static string GetContentType(string filePath) {
210232
var ext = Path.GetExtension(filePath).ToLower();
211233
if (ext != string.Empty) {
212234
if (ModelLocator.Current.Associations.FilesAssociations.TryGetValue("*" + ext, out var ct)) {
213-
contentType = ct;
235+
// Now find Id in Languages
236+
contentType = ModelLocator.Current.Associations.Languages
237+
.Where(lang => lang.Id == ct)
238+
.DefaultIfEmpty(new Langauge() { Id = DefaultContentType })
239+
.First().Id;
214240
}
215241
else {
216242
// No direct file extension, look in Languages
217-
contentType = ModelLocator.Current.Associations.Languages.Where(lang => lang.Extensions.Contains(ext)).DefaultIfEmpty(new Langauge() { Id = "text/plain" }).First().Id;
243+
contentType = ModelLocator.Current.Associations.Languages
244+
.Where(lang => lang.Extensions.Contains(ext))
245+
.DefaultIfEmpty(new Langauge() { Id = DefaultContentType })
246+
.First().Id;
218247
}
219248
}
220249
else {

src/WinPrint.Core/ContentTypeEngines/HtmlCte.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Drawing;
33
using System.IO;
4+
using System.Text;
45
using System.Threading.Tasks;
56
using LiteHtmlSharp;
67
using Serilog;
@@ -14,11 +15,8 @@ namespace WinPrint.Core.ContentTypeEngines {
1415
/// Implements generic HTML file type support.
1516
/// </summary>
1617
public class HtmlCte : ContentTypeEngineBase, IDisposable {
17-
private static readonly string _contentType = "text/html";
18-
/// <summary>
19-
/// ContentType identifier (shorthand for class name).
20-
/// </summary>
21-
public override string ContentTypeEngineName => _contentType;
18+
private static readonly string[] _supportedContentTypes = { "text/html" };
19+
public override string[] SupportedContentTypes => _supportedContentTypes;
2220

2321
public static HtmlCte Create() {
2422
var content = new HtmlCte();
@@ -55,13 +53,6 @@ private void Dispose(bool disposing) {
5553

5654
private Bitmap _htmlBitmap;
5755

58-
//public async override Task<bool> LoadAsync(string filePath) {
59-
// ready = false;
60-
// litehtml = null;
61-
// htmlBitmap = null;
62-
// return await base.LoadAsync(filePath);
63-
//}
64-
6556
public override async Task<bool> SetDocumentAsync(string doc) {
6657
_ready = false;
6758
_litehtml = null;
@@ -124,7 +115,7 @@ public override async Task<int> RenderAsync(System.Drawing.Printing.PrinterResol
124115

125116
//Logging.TraceMessage("litehtml.Document.CreateFromString(document)");
126117
reflowProgress?.Invoke(this, "litehtml.Document.CreateFromString(document)");
127-
_litehtml.Document.CreateFromString(document);
118+
_litehtml.Document.CreateFromString(Document);
128119
//Logging.TraceMessage("back from litehtml.Document.CreateFromString(document)");
129120
reflowProgress?.Invoke(this, "back from litehtml.Document.CreateFromString(document)");
130121

0 commit comments

Comments
 (0)