-
Notifications
You must be signed in to change notification settings - Fork 673
Expand file tree
/
Copy pathHtmlTextRenderer.cs
More file actions
3225 lines (2820 loc) · 122 KB
/
HtmlTextRenderer.cs
File metadata and controls
3225 lines (2820 loc) · 122 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
namespace FastReport.Utils
{
public class HtmlTextRenderer : IDisposable
{
#region Definitions
/// <summary>
/// Context for HTML rendering. <br/>
/// Using this structure instead of the class's private fields is recommended. <br/>
/// This allows for future optimizations and helps avoid constructors with numerous arguments.
/// </summary>
public struct RendererContext
{
internal int angle;
internal float widthRatio;
internal string text;
internal IGraphics g;
internal FontFamily font;
internal float size;
internal FontStyle style; // no keep
internal Color color; // no keep
internal Color underlineColor;
internal RectangleF rect;
internal bool underlines;
internal StringFormat format; // no keep
internal HorzAlign horzAlign;
internal VertAlign vertAlign;
internal ParagraphFormat paragraphFormat;
internal bool forceJustify;
internal float scale;
internal float fontScale;
internal InlineImageCache cache;
internal bool isPrinting;
internal bool isDifferentTabPositions;
internal bool keepLastLineSpace; // Classic objects need false, translated objects need true
}
#endregion
#region Internal Fields
public static readonly System.Globalization.CultureInfo CultureInfo = System.Globalization.CultureInfo.InvariantCulture;
#endregion Internal Fields
#region Private Fields
private int angle;
private float widthRatio;
private const char SOFT_ENTER = '\u2028';
private List<RectangleFColor> backgrounds;
private InlineImageCache cache;
private RectangleF displayRect;
private bool everUnderlines;
private FontFamily font;
private float fontLineHeight;
private float scale;
private bool forceJustify;
private StringFormat format;
private IGraphics graphics;
private HorzAlign horzAlign;
private ParagraphFormat paragraphFormat;
private List<HtmlTextRenderer.Paragraph> paragraphs;
private bool rightToLeft;
private float size;
private List<LineFColor> strikeouts;
private string text;
private Color underlineColor;
private List<LineFColor> underlines;
private VertAlign vertAlign;
private StyleDescriptor initalStyle;
private float fontScale;
private FastString cacheString = new FastString(100);
private bool isPrinting;
private bool isDifferentTabPositions;
internal bool keepLastSpace = false;
#endregion Private Fields
#region Public Properties
public IEnumerable<RectangleFColor> Backgrounds { get { return backgrounds; } }
public RectangleF DisplayRect { get { return displayRect; } }
public float Scale { get { return scale; } }
public float FontScale { get { return fontScale; } set { fontScale = value; } }
public HorzAlign HorzAlign { get { return horzAlign; } }
public ParagraphFormat ParagraphFormat { get { return paragraphFormat; } }
public IEnumerable<Paragraph> Paragraphs { get { return paragraphs; } }
public bool RightToLeft
{
get { return rightToLeft; }
}
public IEnumerable<LineFColor> Stikeouts { get { return strikeouts; } }
public float[] TabPositions
{
get
{
float firstTabStop;
return format.GetTabStops(out firstTabStop);
}
}
public float TabSize
{
get
{
// re fix tab offset #2823 sorry linux users, on linux firstTab is firstTab not tabSizes[0]
float[] tabSizes = TabPositions;
if (tabSizes.Length > 1)
return tabSizes[1];
return 0;
}
}
public float TabOffset
{
get
{
// re fix tab offset #2823 sorry linux users, on linux firstTab is firstTab not tabSizes[0]
float[] tabSizes = TabPositions;
if (tabSizes.Length > 0)
return tabSizes[0];
return 0;
}
}
public IEnumerable<LineFColor> Underlines { get { return underlines; } }
public bool WordWrap
{
get { return (format.FormatFlags & StringFormatFlags.NoWrap) == 0; }
}
/// <summary>
/// Gets the angle of rotation.
/// </summary>
public int Angle
{
get { return angle; }
}
/// <summary>
/// Gets the width ratio of the object.
/// </summary>
public float WidthRatio
{
get { return widthRatio; }
}
#endregion Public Properties
////TODO this is a problem with dotnet, because typographic width
////float width_dotnet = 2.7f;
#region Public Constructors
/// <summary>
/// Initializes a new instance of the HTML text renderer with a specified rendering context.
/// </summary>
/// <param name="context">The rendering context for the HTML renderer.</param>
public HtmlTextRenderer(RendererContext context)
{
this.angle = context.angle % 360;
this.widthRatio = context.widthRatio;
this.text = context.text;
this.graphics = context.g;
this.font = context.font;
this.size = context.size;
this.underlineColor = context.underlineColor;
this.displayRect = context.rect;
this.everUnderlines = context.underlines;
this.format = context.format;
this.horzAlign = context.horzAlign;
this.vertAlign = context.vertAlign;
this.paragraphFormat = context.paragraphFormat;
this.forceJustify = context.forceJustify;
this.scale = context.scale;
this.fontScale = context.fontScale;
this.cache = context.cache;
this.isPrinting = context.isPrinting;
this.isDifferentTabPositions = context.isDifferentTabPositions;
this.keepLastSpace = context.keepLastLineSpace;
paragraphs = new List<HtmlTextRenderer.Paragraph>();
rightToLeft = (context.format.FormatFlags & StringFormatFlags.DirectionRightToLeft) == StringFormatFlags.DirectionRightToLeft;
// Dispose it
this.format = StringFormat.GenericTypographic.Clone() as StringFormat;
if (RightToLeft)
this.format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
float firstTab;
float[] tabs = context.format.GetTabStops(out firstTab);
this.format.SetTabStops(firstTab, tabs);
this.format.Alignment = StringAlignment.Near;
this.format.LineAlignment = StringAlignment.Near;
this.format.Trimming = StringTrimming.None;
this.format.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
//FFormat.DigitSubstitutionMethod = StringDigitSubstitute.User;
//FFormat.DigitSubstitutionLanguage = 0;
this.format.FormatFlags |= StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit;
//FFormat.FormatFlags |= StringFormatFlags.NoFontFallback;
backgrounds = new List<RectangleFColor>();
this.underlines = new List<LineFColor>();
strikeouts = new List<LineFColor>();
//FDisplayRect.Width -= width_dotnet * scale;
initalStyle = new StyleDescriptor(context.style, context.color, BaseLine.Normal, this.font, this.size * this.fontScale);
using (Font f = initalStyle.GetFont())
{
fontLineHeight = f.GetHeight(graphics.Graphics);
}
StringFormatFlags saveFlags = this.format.FormatFlags;
StringTrimming saveTrimming = this.format.Trimming;
// if word wrap is set, ignore trimming
if (WordWrap)
this.format.Trimming = StringTrimming.Word;
SplitToParagraphs(text);
AdjustParagraphLines();
// restore original values
displayRect = context.rect;
this.format.FormatFlags = saveFlags;
this.format.Trimming = saveTrimming;
}
public HtmlTextRenderer(string text, IGraphics g, FontFamily font, float size,
FontStyle style, Color color, Color underlineColor, RectangleF rect, bool underlines,
StringFormat format, HorzAlign horzAlign, VertAlign vertAlign,
ParagraphFormat paragraphFormat, bool forceJustify, float scale, float fontScale, InlineImageCache cache, bool isPrinting = false, bool isDifferentTabPositions = false)
{
this.cache = cache;
this.scale = scale;
this.fontScale = fontScale;
paragraphs = new List<HtmlTextRenderer.Paragraph>();
this.text = text;
graphics = g;
this.font = font;
displayRect = rect;
rightToLeft = (format.FormatFlags & StringFormatFlags.DirectionRightToLeft) == StringFormatFlags.DirectionRightToLeft;
// Dispose it
this.format = StringFormat.GenericTypographic.Clone() as StringFormat;
if (RightToLeft)
this.format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
float firstTab;
float[] tabs = format.GetTabStops(out firstTab);
this.format.SetTabStops(firstTab, tabs);
this.format.Alignment = StringAlignment.Near;
this.format.LineAlignment = StringAlignment.Near;
this.format.Trimming = StringTrimming.None;
this.format.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.None;
this.underlineColor = underlineColor;
//FFormat.DigitSubstitutionMethod = StringDigitSubstitute.User;
//FFormat.DigitSubstitutionLanguage = 0;
this.format.FormatFlags |= StringFormatFlags.NoClip | StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit;
//FFormat.FormatFlags |= StringFormatFlags.NoFontFallback;
this.horzAlign = horzAlign;
this.vertAlign = vertAlign;
this.paragraphFormat = paragraphFormat;
this.font = font;
this.size = size;
this.isPrinting = isPrinting;
this.isDifferentTabPositions = isDifferentTabPositions;
everUnderlines = underlines;
backgrounds = new List<RectangleFColor>();
this.underlines = new List<LineFColor>();
strikeouts = new List<LineFColor>();
//FDisplayRect.Width -= width_dotnet * scale;
initalStyle = new StyleDescriptor(style, color, BaseLine.Normal, this.font, this.size * this.fontScale);
using (Font f = initalStyle.GetFont())
{
fontLineHeight = f.GetHeight(g.Graphics);
}
this.forceJustify = forceJustify;
StringFormatFlags saveFlags = this.format.FormatFlags;
StringTrimming saveTrimming = this.format.Trimming;
// if word wrap is set, ignore trimming
if (WordWrap)
this.format.Trimming = StringTrimming.Word;
SplitToParagraphs(text);
AdjustParagraphLines();
// restore original values
displayRect = rect;
this.format.FormatFlags = saveFlags;
this.format.Trimming = saveTrimming;
}
#endregion Public Constructors
#region Public Methods
public void AddUnknownWord(List<CharWithIndex> w, Paragraph paragraph, StyleDescriptor style, int charIndex, ref Line line, ref Word word, ref float width, ref int tabIndex)
{
if (w[0].Char == ' ')
{
if (word == null || word.Type == WordType.Normal)
{
word = new Word(this, line, WordType.WhiteSpace);
line.Words.Add(word);
}
Run r = new RunText(this, word, style, w, width, charIndex);
word.Runs.Add(r);
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
else
{
if (word == null || word.Type != WordType.Normal)
{
word = new Word(this, line, WordType.Normal);
line.Words.Add(word);
}
Run r = new RunText(this, word, style, w, width, charIndex);
word.Runs.Add(r);
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
}
public float CalcHeight()
{
int charsFit = 0;
return CalcHeight(out charsFit);
}
public float CalcHeight(out int charsFit)
{
charsFit = -1;
float height = 0;
float displayHeight = displayRect.Height;
float lineSpacing = 0;
foreach (Paragraph paragraph in paragraphs)
{
foreach (Line line in paragraph.Lines)
{
line.CalcMetrics();
height += line.Height;
if (charsFit < 0 && height > displayHeight)
{
charsFit = line.OriginalCharIndex;
}
height += lineSpacing = line.LineSpacing;
}
}
if (!keepLastSpace) // It looks like TextProcessors keep this value for every line.
height -= lineSpacing;
if (charsFit < 0)
charsFit = text.Length;
return height;
}
public float CalcWidth()
{
float width = 0;
foreach (Paragraph paragraph in paragraphs)
{
foreach (Line line in paragraph.Lines)
{
if (width < line.Width)
width = line.Width;
}
}
return width;
}
#endregion Public Methods
#region Internal Methods
/// <summary>
/// Returns splited string
/// </summary>
/// <param name="text">text for splitting</param>
/// <param name="charactersFitted">index of first character of second string</param>
/// <param name="result">second part of string</param>
/// <param name="endOnEnter">returns true if ends on enter</param>
/// <returns>first part of string</returns>
internal static string BreakHtml(string text, int charactersFitted, out string result, out bool endOnEnter)
{
endOnEnter = false;
Stack<SimpleFastReportHtmlElement> elements = new Stack<SimpleFastReportHtmlElement>();
SimpleFastReportHtmlReader reader = new SimpleFastReportHtmlReader(text);
while (reader.IsNotEOF)
{
if (reader.Position >= charactersFitted)
{
StringBuilder firstPart = new StringBuilder();
if (reader.Character.Char == SOFT_ENTER)
firstPart.Append(text.Substring(0, reader.LastPosition));
else
firstPart.Append(text.Substring(0, reader.Position));
foreach (SimpleFastReportHtmlElement el in elements)
{
SimpleFastReportHtmlElement el2 = new SimpleFastReportHtmlElement(el.name, true);
firstPart.Append(el2.ToString());
}
SimpleFastReportHtmlElement[] arr = elements.ToArray();
StringBuilder secondPart = new StringBuilder();
for (int i = arr.Length - 1; i >= 0; i--)
secondPart.Append(arr[i].ToString());
secondPart.Append(text.Substring(reader.Position));
endOnEnter = reader.Character.Char == '\n';
result = secondPart.ToString();
return firstPart.ToString();
}
if (!reader.Read())
{
if (reader.Element.isEnd)
{
int enumIndex = 1;
using (Stack<SimpleFastReportHtmlElement>.Enumerator enumerator = elements.GetEnumerator())
{
while (enumerator.MoveNext())
{
SimpleFastReportHtmlElement el = enumerator.Current;
if (el.name == reader.Element.name)
{
for (int i = 0; i < enumIndex; i++)
elements.Pop();
break;
}
else
enumIndex++;
}
}
}
else if (!reader.Element.IsSelfClosed) elements.Push(reader.Element);
}
}
result = "";
return text;
}
internal void Draw()
{
// set clipping
IGraphicsState state = graphics.Save();
RectangleF dRect = displayRect;
// round x and y to an integer to avoid clipping the characters of the first line
dRect.Inflate(displayRect.Left % 1, displayRect.Top % 1);
graphics.SetClip(dRect, CombineMode.Intersect);
if (Angle != 0)
{
PointF center = new PointF(displayRect.Left + displayRect.Width / 2, displayRect.Top + displayRect.Height / 2);
// Translate the origin to the center of the rectangle
graphics.TranslateTransform(center.X, center.Y);
// Rotate the graphics by the specified angle
graphics.RotateTransform(Angle);
// Translate the origin back to the original position
graphics.TranslateTransform(-center.X, -center.Y);
}
// reset alignment
//StringAlignment saveAlign = FFormat.Alignment;
//StringAlignment saveLineAlign = FFormat.LineAlignment;
//FFormat.Alignment = StringAlignment.Near;
//FFormat.LineAlignment = StringAlignment.Near;
//if (FRightToLeft)
// foreach (RectangleFColor rect in FBackgrounds)
// using (Brush brush = new SolidBrush(rect.Color))
// FGraphics.FillRectangle(brush, rect.Left - rect.Width, rect.Top, rect.Width, rect.Height);
//else
foreach (RectangleFColor rect in backgrounds)
using (Brush brush = new SolidBrush(rect.Color))
graphics.FillRectangle(brush, rect.Left, rect.Top, rect.Width, rect.Height);
foreach (Paragraph p in paragraphs)
foreach (Line l in p.Lines)
{
//#if DEBUG
// FGraphics.DrawRectangle(Pens.Blue, FDisplayRect.Left, l.Top, FDisplayRect.Width, l.Height);
//#endif
foreach (Word w in l.Words)
switch (w.Type)
{
case WordType.Normal:
foreach (Run r in w.Runs)
{
r.Draw();
}
break;
}
}
//if (RightToLeft)
//{
// foreach (LineFColor line in FUnderlines)
// using (Pen pen = new Pen(line.Color, line.Width))
// FGraphics.DrawLine(pen, 2 * line.Left - line.Right, line.Top, line.Left, line.Top);
// foreach (LineFColor line in FStrikeouts)
// using (Pen pen = new Pen(line.Color, line.Width))
// FGraphics.DrawLine(pen, 2 * line.Left - line.Right, line.Top, line.Left, line.Top);
//}
//else
//{
foreach (LineFColor line in underlines)
using (Pen pen = new Pen(line.Color, line.Width))
graphics.DrawLine(pen, line.Left, line.Top, line.Right, line.Top);
foreach (LineFColor line in strikeouts)
using (Pen pen = new Pen(line.Color, line.Width))
graphics.DrawLine(pen, line.Left, line.Top, line.Right, line.Top);
//}
// restore alignment and clipping
//FFormat.Alignment = saveAlign;
//FFormat.LineAlignment = saveLineAlign;
graphics.Restore(state);
}
#endregion Internal Methods
#region Private Methods
private void AdjustParagraphLines()
{
// calculate text height
float height = 0;
height = CalcHeight();
// calculate Y offset
float offsetY = displayRect.Top;
if (vertAlign == VertAlign.Center)
offsetY += (displayRect.Height - height) / 2;
else if (vertAlign == VertAlign.Bottom)
offsetY += (displayRect.Height - height) - 1;
for (int i = 0; i < paragraphs.Count; i++)
{
Paragraph paragraph = paragraphs[i];
paragraph.AlignLines(i == paragraphs.Count - 1 && forceJustify);
// adjust line tops
foreach (Line line in paragraph.Lines)
{
line.Top = offsetY;
line.MakeUnderlines();
line.MakeStrikeouts();
line.MakeBackgrounds();
offsetY += line.Height + line.LineSpacing;
}
}
}
private void CssStyle(StyleDescriptor style, Dictionary<string, string> dict)
{
if (dict == null)
return;
string tStr;
// If "font-style" contains "italic" or "oblique", apply the Italic style to the text.
if (dict.TryGetValue("font-style", out tStr))
{
if (tStr.Contains("italic") || tStr.Contains("oblique"))
style.FontStyle |= FontStyle.Italic;
}
// If "font-weight" contains "bold", apply the Bold style to the text.
if (dict.TryGetValue("font-weight", out tStr))
{
if (tStr.Contains("bold"))
style.FontStyle |= FontStyle.Bold;
}
// If "text-decoration" contains both "underline" and "line-through", apply both styles to the text.
// Otherwise, check and apply each style individually.
if (dict.TryGetValue("text-decoration", out tStr))
{
if (tStr.Contains("underline") && tStr.Contains("line-through"))
style.FontStyle |= FontStyle.Underline | FontStyle.Strikeout;
else
{
if (tStr.Contains("underline"))
style.FontStyle |= FontStyle.Underline;
if (tStr.Contains("line-through"))
style.FontStyle |= FontStyle.Strikeout;
}
}
if (dict.TryGetValue("font-size", out tStr))
{
if (EndsWith(tStr, "px"))
try { style.Size = fontScale * 0.75f * Single.Parse(tStr.Substring(0, tStr.Length - 2), CultureInfo); } catch { }
else if (EndsWith(tStr, "pt"))
try { style.Size = fontScale * Single.Parse(tStr.Substring(0, tStr.Length - 2), CultureInfo); } catch { }
else if (EndsWith(tStr, "em"))
try { style.Size *= Single.Parse(tStr.Substring(0, tStr.Length - 2), CultureInfo); } catch { }
}
if (dict.TryGetValue("font-family", out tStr))
style.Font = new FontFamily(tStr);
if (dict.TryGetValue("color", out tStr))
{
if (StartsWith(tStr, "#"))
try { style.Color = Color.FromArgb((int)(0xFF000000 + uint.Parse(tStr.Substring(1), System.Globalization.NumberStyles.HexNumber))); } catch { }
else if (StartsWith(tStr, "rgba"))
{
int i1 = tStr.IndexOf('(');
int i2 = tStr.IndexOf(')');
string[] strs = tStr.Substring(i1 + 1, i2 - i1 - 1).Split(',');
if (strs.Length == 4)
{
float r, g, b, a;
try
{
r = Single.Parse(strs[0], CultureInfo);
g = Single.Parse(strs[1], CultureInfo);
b = Single.Parse(strs[2], CultureInfo);
a = Single.Parse(strs[3], CultureInfo);
style.Color = Color.FromArgb((int)(a * 0xFF), (int)r, (int)g, (int)b);
}
catch { }
}
}
else if (StartsWith(tStr, "rgb"))
{
int i1 = tStr.IndexOf('(');
int i2 = tStr.IndexOf(')');
string[] strs = tStr.Substring(i1 + 1, i2 - i1 - 1).Split(',');
if (strs.Length == 3)
{
float r, g, b;
try
{
r = Single.Parse(strs[0], CultureInfo);
g = Single.Parse(strs[1], CultureInfo);
b = Single.Parse(strs[2], CultureInfo);
style.Color = Color.FromArgb((int)r, (int)g, (int)b);
}
catch { }
}
}
else style.Color = Color.FromName(tStr);
}
if (dict.TryGetValue("background-color", out tStr))
{
if (StartsWith(tStr, "#"))
try { style.BackgroundColor = Color.FromArgb((int)(0xFF000000 + uint.Parse(tStr.Substring(1), System.Globalization.NumberStyles.HexNumber))); } catch { }
else if (StartsWith(tStr, "rgba"))
{
int i1 = tStr.IndexOf('(');
int i2 = tStr.IndexOf(')');
string[] strs = tStr.Substring(i1 + 1, i2 - i1 - 1).Split(',');
if (strs.Length == 4)
{
float r, g, b, a;
try
{
r = Single.Parse(strs[0], CultureInfo);
g = Single.Parse(strs[1], CultureInfo);
b = Single.Parse(strs[2], CultureInfo);
a = Single.Parse(strs[3], CultureInfo);
style.BackgroundColor = Color.FromArgb((int)(a * 0xFF), (int)r, (int)g, (int)b);
}
catch { }
}
}
else if (StartsWith(tStr, "rgb"))
{
int i1 = tStr.IndexOf('(');
int i2 = tStr.IndexOf(')');
string[] strs = tStr.Substring(i1 + 1, i2 - i1 - 1).Split(',');
if (strs.Length == 3)
{
float r, g, b;
try
{
r = Single.Parse(strs[0], CultureInfo);
g = Single.Parse(strs[1], CultureInfo);
b = Single.Parse(strs[2], CultureInfo);
style.BackgroundColor = Color.FromArgb((int)r, (int)g, (int)b);
}
catch { }
}
}
else style.BackgroundColor = Color.FromName(tStr);
}
}
private bool EndsWith(string str1, string str2)
{
int len1 = str1.Length;
int len2 = str2.Length;
if (len1 < len2) return false;
switch (len2)
{
case 0: return true;
case 1: return str1[len1 - 1] == str2[len2 - 1];
case 2: return str1[len1 - 1] == str2[len2 - 1] && str1[len1 - 2] == str2[len2 - 2];
case 3: return str1[len1 - 1] == str2[len2 - 1] && str1[len1 - 2] == str2[len2 - 2] && str1[len1 - 3] == str2[len2 - 3];
case 4: return str1[len1 - 1] == str2[len2 - 1] && str1[len1 - 2] == str2[len2 - 2] && str1[len1 - 3] == str2[len2 - 3] && str1[len1 - 4] == str2[len2 - 4];
default: return str1.EndsWith(str2);
}
}
private float GetTabPosition(float pos)
{
float tabOffset = TabOffset;
float tabSize = TabSize;
int tabPosition = (int)((pos - tabOffset) / tabSize);
if (pos < tabOffset)
return tabOffset;
return (tabPosition + 1) * tabSize + tabOffset;
}
private float GetTabPosition(float pos, int tabIndex)
{
float tabOffset = 0;
float tabSize;
float firstTabOffset;
float[] tabsPos = format.GetTabStops(out firstTabOffset);
if (tabIndex <= 1)
{
tabOffset = TabOffset;
tabSize = TabSize;
}
else
{
for (int i = 0; i < tabIndex; i++)
{
tabOffset += tabsPos[i];
}
tabSize = tabsPos[tabIndex];
}
int tabPosition = (int)((pos - tabOffset) / tabSize);
if (pos < tabOffset)
return tabOffset;
return (tabPosition + 1) * tabSize + tabOffset;
}
private void SplitToParagraphs(string text)
{
Stack<SimpleFastReportHtmlElement> elements = new Stack<SimpleFastReportHtmlElement>();
SimpleFastReportHtmlReader reader = new SimpleFastReportHtmlReader(this.text);
List<CharWithIndex> currentWord = new List<CharWithIndex>();
float width = paragraphFormat.SkipFirstLineIndent ? 0 : GetStartPosition(true);
Paragraph paragraph = new Paragraph(this);
int charIndex = 0;
int tabIndex = 0;
Line line = new Line(this, paragraph, charIndex);
paragraph.Lines.Add(line);
paragraphs.Add(paragraph);
Word word = null;
StyleDescriptor style = new StyleDescriptor(initalStyle);
//bool softReturn = false;
//CharWithIndex softReturnChar = new CharWithIndex();
while (reader.IsNotEOF)
{
if (reader.Read())
{
switch (reader.Character.Char)
{
case ' ':
if (word == null)
{
word = new Word(this, line, WordType.WhiteSpace);
line.Words.Add(word);
}
if (word.Type == WordType.WhiteSpace)
currentWord.Add(reader.Character);
else
{
if (currentWord.Count > 0)
{
Run r = new RunText(this, word, style, currentWord, width, charIndex);
word.Runs.Add(r);
currentWord.Clear();
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
currentWord.Add(reader.Character);
word = new Word(this, line, WordType.WhiteSpace);
line.Words.Add(word);
charIndex = reader.LastPosition;
}
break;
case '\t':
if (word != null)
{
if (currentWord.Count > 0)
{
Run r = new RunText(this, word, style, currentWord, width, charIndex);
word.Runs.Add(r);
currentWord.Clear();
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
}
else
{
if (currentWord.Count > 0)
{
AddUnknownWord(currentWord, paragraph, style, charIndex, ref line, ref word, ref width, ref tabIndex);
}
}
charIndex = reader.LastPosition;
word = new Word(this, line, WordType.Tab);
Run tabRun = new RunText(this, word, style, new List<CharWithIndex>(new CharWithIndex[] { reader.Character }), width, charIndex);
word.Runs.Add(tabRun);
float width2 = GetTabPosition(width);
if (isDifferentTabPositions)
{
width2 = GetTabPosition(width, tabIndex);
}
if (width2 < width) width2 = width;
if (line.Words.Count > 0 && width2 > displayRect.Width)
{
tabRun.Left = 0;
line = new Line(this, paragraph, charIndex);
tabIndex = 0;
paragraph.Lines.Add(line);
width = 0;
width2 = GetTabPosition(width);
if (isDifferentTabPositions)
{
width2 = GetTabPosition(width, tabIndex);
}
}
// decrease by (DrawUtils.ScreenDpi / 96f) repeats the work of the Word, if the next tab position is a pixel further than the left indent,
// then the tab stop occurs in the tab position, otherwise the stop will be in the place of the left indentation
if (width < -paragraphFormat.FirstLineIndent && width2 - (DrawUtils.ScreenDpi / 96f) > -paragraphFormat.FirstLineIndent)
{
width2 = -paragraphFormat.FirstLineIndent;
}
tabIndex++;
line.Words.Add(word);
tabRun.Width = width2 - width;
width = width2;
word = null;
break;
case SOFT_ENTER://soft enter
if (word != null)
{
if (currentWord.Count > 0)
{
Run r = new RunText(this, word, style, currentWord, width, charIndex);
word.Runs.Add(r);
currentWord.Clear();
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
}
else
{
if (currentWord.Count > 0)
{
AddUnknownWord(currentWord, paragraph, style, charIndex, ref line, ref word, ref width, ref tabIndex);
}
}
charIndex = reader.Position;
//currentWord.Append(' ')
//RunText runText = new RunText(this, word, style, new List<CharWithIndex>(new CharWithIndex[] { reader.Character }), width, charIndex);
//runText.Width = 0;
//word.Runs.Add(runText);
line = new Line(this, paragraph, charIndex);
word = null;
width = GetStartPosition();
currentWord.Clear();
paragraph.Lines.Add(line);
break;
case '\n':
if (word != null)
{
if (currentWord.Count > 0)
{
Run r = new RunText(this, word, style, currentWord, width, charIndex);
word.Runs.Add(r);
currentWord.Clear();
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
}
else
{
if (currentWord.Count > 0)
{
AddUnknownWord(currentWord, paragraph, style, charIndex, ref line, ref word, ref width, ref tabIndex);
}
}
charIndex = reader.Position;
paragraph = new Paragraph(this);
paragraphs.Add(paragraph);
line = new Line(this, paragraph, charIndex);
word = null;
width = GetStartPosition(true);
paragraph.Lines.Add(line);
break;
case '\r'://ignore
break;
default:
if (word == null)
{
word = new Word(this, line, WordType.Normal);
line.Words.Add(word);
}
if (word.Type == WordType.Normal)
currentWord.Add(reader.Character);
else
{
if (currentWord.Count > 0)
{
Run r = new RunText(this, word, style, currentWord, width, charIndex);
word.Runs.Add(r);
currentWord.Clear();
width += r.Width;
if (width > displayRect.Width)
line = WrapLine(paragraph, line, charIndex, displayRect.Width, ref width, ref word, ref tabIndex);
}
currentWord.Add(reader.Character);
word = new Word(this, line, WordType.Normal);
line.Words.Add(word);
charIndex = reader.LastPosition;
}
break;
}
}
else
{
StyleDescriptor newStyle = new StyleDescriptor(initalStyle);
SimpleFastReportHtmlElement element = reader.Element;
if (!element.IsSelfClosed)
{
if (element.isEnd)
{
int enumIndex = 1;
using (Stack<SimpleFastReportHtmlElement>.Enumerator enumerator = elements.GetEnumerator())
{
while (enumerator.MoveNext())
{
SimpleFastReportHtmlElement el = enumerator.Current;
if (el.name == element.name)
{
for (int i = 0; i < enumIndex; i++)
elements.Pop();
break;
}
else
enumIndex++;
}
}
}
else elements.Push(element);
SimpleFastReportHtmlElement[] arr = elements.ToArray();
for (int i = arr.Length - 1; i >= 0; i--)
{
SimpleFastReportHtmlElement el = arr[i];
switch (el.name)
{
case "b":
newStyle.FontStyle |= FontStyle.Bold;
break;
case "i":
newStyle.FontStyle |= FontStyle.Italic;
break;