forked from ArthurHub/HTML-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssBox.cs
More file actions
1449 lines (1286 loc) · 54.9 KB
/
CssBox.cs
File metadata and controls
1449 lines (1286 loc) · 54.9 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
// "Therefore those skilled at the unorthodox
// are infinite as heaven and earth,
// inexhaustible as the great rivers.
// When they come to an end,
// they begin again,
// like the days and months;
// they die and are reborn,
// like the four seasons."
//
// - Sun Tsu,
// "The Art of War"
using System;
using System.Collections.Generic;
using System.Globalization;
using TheArtOfDev.HtmlRenderer.Adapters;
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
using TheArtOfDev.HtmlRenderer.Core.Entities;
using TheArtOfDev.HtmlRenderer.Core.Handlers;
using TheArtOfDev.HtmlRenderer.Core.Parse;
using TheArtOfDev.HtmlRenderer.Core.Utils;
namespace TheArtOfDev.HtmlRenderer.Core.Dom
{
/// <summary>
/// Represents a CSS Box of text or replaced elements.
/// </summary>
/// <remarks>
/// The Box can contains other boxes, that's the way that the CSS Tree
/// is composed.
///
/// To know more about boxes visit CSS spec:
/// http://www.w3.org/TR/CSS21/box.html
/// </remarks>
internal class CssBox : CssBoxProperties, IDisposable
{
#region Fields and Consts
/// <summary>
/// the parent css box of this css box in the hierarchy
/// </summary>
private CssBox _parentBox;
/// <summary>
/// the root container for the hierarchy
/// </summary>
protected HtmlContainerInt _htmlContainer;
/// <summary>
/// the html tag that is associated with this css box, null if anonymous box
/// </summary>
private readonly HtmlTag _htmltag;
private readonly List<CssRect> _boxWords = new List<CssRect>();
private readonly List<CssBox> _boxes = new List<CssBox>();
private readonly List<CssLineBox> _lineBoxes = new List<CssLineBox>();
private readonly List<CssLineBox> _parentLineBoxes = new List<CssLineBox>();
private readonly Dictionary<CssLineBox, RRect> _rectangles = new Dictionary<CssLineBox, RRect>();
/// <summary>
/// the inner text of the box
/// </summary>
private SubString _text;
/// <summary>
/// Do not use or alter this flag
/// </summary>
/// <remarks>
/// Flag that indicates that CssTable algorithm already made fixes on it.
/// </remarks>
internal bool _tableFixed;
protected bool _wordsSizeMeasured;
private CssBox _listItemBox;
private CssLineBox _firstHostingLineBox;
private CssLineBox _lastHostingLineBox;
/// <summary>
/// handler for loading background image
/// </summary>
private ImageLoadHandler _imageLoadHandler;
#endregion
/// <summary>
/// Init.
/// </summary>
/// <param name="parentBox">optional: the parent of this css box in html</param>
/// <param name="tag">optional: the html tag associated with this css box</param>
public CssBox(CssBox parentBox, HtmlTag tag)
{
if (parentBox != null)
{
_parentBox = parentBox;
_parentBox.Boxes.Add(this);
}
_htmltag = tag;
}
/// <summary>
/// Gets the HtmlContainer of the Box.
/// WARNING: May be null.
/// </summary>
public HtmlContainerInt HtmlContainer
{
get { return _htmlContainer ?? (_htmlContainer = _parentBox != null ? _parentBox.HtmlContainer : null); }
set { _htmlContainer = value; }
}
/// <summary>
/// Gets or sets the parent box of this box
/// </summary>
public CssBox ParentBox
{
get { return _parentBox; }
set
{
//Remove from last parent
if (_parentBox != null)
_parentBox.Boxes.Remove(this);
_parentBox = value;
//Add to new parent
if (value != null)
_parentBox.Boxes.Add(this);
}
}
/// <summary>
/// Gets the children boxes of this box
/// </summary>
public List<CssBox> Boxes
{
get { return _boxes; }
}
/// <summary>
/// Is the box is of "br" element.
/// </summary>
public bool IsBrElement
{
get { return _htmltag != null && _htmltag.Name.Equals("br", StringComparison.InvariantCultureIgnoreCase); }
}
/// <summary>
/// is the box "Display" is "Inline", is this is an inline box and not block.
/// </summary>
public bool IsInline
{
get { return (Display == CssConstants.Inline || Display == CssConstants.InlineBlock) && !IsBrElement; }
}
/// <summary>
/// is the box "Display" is "Block", is this is an block box and not inline.
/// </summary>
public bool IsBlock
{
get { return Display == CssConstants.Block; }
}
/// <summary>
/// Is the css box clickable (by default only "a" element is clickable)
/// </summary>
public virtual bool IsClickable
{
get { return HtmlTag != null && HtmlTag.Name == HtmlConstants.A && !HtmlTag.HasAttribute("id"); }
}
/// <summary>
/// Get the href link of the box (by default get "href" attribute)
/// </summary>
public virtual string HrefLink
{
get { return GetAttribute(HtmlConstants.Href); }
}
/// <summary>
/// Gets the containing block-box of this box. (The nearest parent box with display=block)
/// </summary>
public CssBox ContainingBlock
{
get
{
if (ParentBox == null)
{
return this; //This is the initial containing block.
}
var box = ParentBox;
while (!box.IsBlock &&
box.Display != CssConstants.ListItem &&
box.Display != CssConstants.Table &&
box.Display != CssConstants.TableCell &&
box.ParentBox != null)
{
box = box.ParentBox;
}
//Comment this following line to treat always superior box as block
if (box == null)
throw new Exception("There's no containing block on the chain");
return box;
}
}
/// <summary>
/// Gets the HTMLTag that hosts this box
/// </summary>
public HtmlTag HtmlTag
{
get { return _htmltag; }
}
/// <summary>
/// Gets if this box represents an image
/// </summary>
public bool IsImage
{
get { return Words.Count == 1 && Words[0].IsImage; }
}
/// <summary>
/// Tells if the box is empty or contains just blank spaces
/// </summary>
public bool IsSpaceOrEmpty
{
get
{
if ((Words.Count != 0 || Boxes.Count != 0) && (Words.Count != 1 || !Words[0].IsSpaces))
{
foreach (CssRect word in Words)
{
if (!word.IsSpaces)
{
return false;
}
}
}
return true;
}
}
/// <summary>
/// Gets or sets the inner text of the box
/// </summary>
public SubString Text
{
get { return _text; }
set
{
_text = value;
_boxWords.Clear();
}
}
/// <summary>
/// Gets the line-boxes of this box (if block box)
/// </summary>
internal List<CssLineBox> LineBoxes
{
get { return _lineBoxes; }
}
/// <summary>
/// Gets the linebox(es) that contains words of this box (if inline)
/// </summary>
internal List<CssLineBox> ParentLineBoxes
{
get { return _parentLineBoxes; }
}
/// <summary>
/// Gets the rectangles where this box should be painted
/// </summary>
internal Dictionary<CssLineBox, RRect> Rectangles
{
get { return _rectangles; }
}
/// <summary>
/// Gets the BoxWords of text in the box
/// </summary>
internal List<CssRect> Words
{
get { return _boxWords; }
}
/// <summary>
/// Gets the first word of the box
/// </summary>
internal CssRect FirstWord
{
get { return Words[0]; }
}
/// <summary>
/// Gets or sets the first linebox where content of this box appear
/// </summary>
internal CssLineBox FirstHostingLineBox
{
get { return _firstHostingLineBox; }
set { _firstHostingLineBox = value; }
}
/// <summary>
/// Gets or sets the last linebox where content of this box appear
/// </summary>
internal CssLineBox LastHostingLineBox
{
get { return _lastHostingLineBox; }
set { _lastHostingLineBox = value; }
}
/// <summary>
/// Create new css box for the given parent with the given html tag.<br/>
/// </summary>
/// <param name="tag">the html tag to define the box</param>
/// <param name="parent">the box to add the new box to it as child</param>
/// <returns>the new box</returns>
public static CssBox CreateBox(HtmlTag tag, CssBox parent = null)
{
ArgChecker.AssertArgNotNull(tag, "tag");
if (tag.Name == HtmlConstants.Img)
{
return new CssBoxImage(parent, tag);
}
else if (tag.Name == HtmlConstants.Iframe)
{
return new CssBoxFrame(parent, tag);
}
else if (tag.Name == HtmlConstants.Hr)
{
return new CssBoxHr(parent, tag);
}
else
{
return new CssBox(parent, tag);
}
}
/// <summary>
/// Create new css box for the given parent with the given optional html tag and insert it either
/// at the end or before the given optional box.<br/>
/// If no html tag is given the box will be anonymous.<br/>
/// If no before box is given the new box will be added at the end of parent boxes collection.<br/>
/// If before box doesn't exists in parent box exception is thrown.<br/>
/// </summary>
/// <remarks>
/// To learn more about anonymous inline boxes visit: http://www.w3.org/TR/CSS21/visuren.html#anonymous
/// </remarks>
/// <param name="parent">the box to add the new box to it as child</param>
/// <param name="tag">optional: the html tag to define the box</param>
/// <param name="before">optional: to insert as specific location in parent box</param>
/// <returns>the new box</returns>
public static CssBox CreateBox(CssBox parent, HtmlTag tag = null, CssBox before = null)
{
ArgChecker.AssertArgNotNull(parent, "parent");
var newBox = new CssBox(parent, tag);
newBox.InheritStyle();
if (before != null)
{
newBox.SetBeforeBox(before);
}
return newBox;
}
/// <summary>
/// Create new css block box.
/// </summary>
/// <returns>the new block box</returns>
public static CssBox CreateBlock()
{
var box = new CssBox(null, null);
box.Display = CssConstants.Block;
return box;
}
/// <summary>
/// Create new css block box for the given parent with the given optional html tag and insert it either
/// at the end or before the given optional box.<br/>
/// If no html tag is given the box will be anonymous.<br/>
/// If no before box is given the new box will be added at the end of parent boxes collection.<br/>
/// If before box doesn't exists in parent box exception is thrown.<br/>
/// </summary>
/// <remarks>
/// To learn more about anonymous block boxes visit CSS spec:
/// http://www.w3.org/TR/CSS21/visuren.html#anonymous-block-level
/// </remarks>
/// <param name="parent">the box to add the new block box to it as child</param>
/// <param name="tag">optional: the html tag to define the box</param>
/// <param name="before">optional: to insert as specific location in parent box</param>
/// <returns>the new block box</returns>
public static CssBox CreateBlock(CssBox parent, HtmlTag tag = null, CssBox before = null)
{
ArgChecker.AssertArgNotNull(parent, "parent");
var newBox = CreateBox(parent, tag, before);
newBox.Display = CssConstants.Block;
return newBox;
}
/// <summary>
/// Measures the bounds of box and children, recursively.<br/>
/// Performs layout of the DOM structure creating lines by set bounds restrictions.
/// </summary>
/// <param name="g">Device context to use</param>
public void PerformLayout(RGraphics g)
{
try
{
PerformLayoutImp(g);
}
catch (Exception ex)
{
HtmlContainer.ReportError(HtmlRenderErrorType.Layout, "Exception in box layout", ex);
}
}
/// <summary>
/// Paints the fragment
/// </summary>
/// <param name="g">Device context to use</param>
public void Paint(RGraphics g)
{
try
{
if (Display != CssConstants.None && Visibility == CssConstants.Visible)
{
// don't call paint if the rectangle of the box is not in visible rectangle
bool visible = Rectangles.Count == 0;
if (!visible)
{
var clip = g.GetClip();
var rect = ContainingBlock.ClientRectangle;
rect.X -= 2;
rect.Width += 2;
rect.Offset(new RPoint(-HtmlContainer.Location.X, -HtmlContainer.Location.Y));
rect.Offset(HtmlContainer.ScrollOffset);
clip.Intersect(rect);
if (clip != RRect.Empty)
visible = true;
}
if (visible)
PaintImp(g);
}
}
catch (Exception ex)
{
HtmlContainer.ReportError(HtmlRenderErrorType.Paint, "Exception in box paint", ex);
}
}
/// <summary>
/// Set this box in
/// </summary>
/// <param name="before"></param>
public void SetBeforeBox(CssBox before)
{
int index = _parentBox.Boxes.IndexOf(before);
if (index < 0)
throw new Exception("before box doesn't exist on parent");
_parentBox.Boxes.Remove(this);
_parentBox.Boxes.Insert(index, this);
}
/// <summary>
/// Move all child boxes from <paramref name="fromBox"/> to this box.
/// </summary>
/// <param name="fromBox">the box to move all its child boxes from</param>
public void SetAllBoxes(CssBox fromBox)
{
foreach (var childBox in fromBox._boxes)
childBox._parentBox = this;
_boxes.AddRange(fromBox._boxes);
fromBox._boxes.Clear();
}
/// <summary>
/// Splits the text into words and saves the result
/// </summary>
public void ParseToWords()
{
_boxWords.Clear();
int startIdx = 0;
bool preserveSpaces = WhiteSpace == CssConstants.Pre || WhiteSpace == CssConstants.PreWrap;
bool respoctNewline = preserveSpaces || WhiteSpace == CssConstants.PreLine;
while (startIdx < _text.Length)
{
while (startIdx < _text.Length && _text[startIdx] == '\r')
startIdx++;
if (startIdx < _text.Length)
{
var endIdx = startIdx;
while (endIdx < _text.Length && char.IsWhiteSpace(_text[endIdx]) && _text[endIdx] != '\n')
endIdx++;
if (endIdx > startIdx)
{
if (preserveSpaces)
_boxWords.Add(new CssRectWord(this, HtmlUtils.DecodeHtml(_text.Substring(startIdx, endIdx - startIdx)), false, false));
}
else
{
endIdx = startIdx;
while (endIdx < _text.Length && !char.IsWhiteSpace(_text[endIdx]) && _text[endIdx] != '-' && WordBreak != CssConstants.BreakAll && !CommonUtils.IsAsianCharecter(_text[endIdx]))
endIdx++;
if (endIdx < _text.Length && (_text[endIdx] == '-' || WordBreak == CssConstants.BreakAll || CommonUtils.IsAsianCharecter(_text[endIdx])))
endIdx++;
if (endIdx > startIdx)
{
var hasSpaceBefore = !preserveSpaces && (startIdx > 0 && _boxWords.Count == 0 && char.IsWhiteSpace(_text[startIdx - 1]));
var hasSpaceAfter = !preserveSpaces && (endIdx < _text.Length && char.IsWhiteSpace(_text[endIdx]));
_boxWords.Add(new CssRectWord(this, HtmlUtils.DecodeHtml(_text.Substring(startIdx, endIdx - startIdx)), hasSpaceBefore, hasSpaceAfter));
}
}
// create new-line word so it will effect the layout
if (endIdx < _text.Length && _text[endIdx] == '\n')
{
endIdx++;
if (respoctNewline)
_boxWords.Add(new CssRectWord(this, "\n", false, false));
}
startIdx = endIdx;
}
}
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
if (_imageLoadHandler != null)
_imageLoadHandler.Dispose();
foreach (var childBox in Boxes)
{
childBox.Dispose();
}
}
#region Private Methods
/// <summary>
/// Measures the bounds of box and children, recursively.<br/>
/// Performs layout of the DOM structure creating lines by set bounds restrictions.<br/>
/// </summary>
/// <param name="g">Device context to use</param>
protected virtual void PerformLayoutImp(RGraphics g)
{
if (Display != CssConstants.None)
{
RectanglesReset();
MeasureWordsSize(g);
}
if (IsBlock || Display == CssConstants.ListItem || Display == CssConstants.Table || Display == CssConstants.InlineTable || Display == CssConstants.TableCell)
{
// Because their width and height are set by CssTable
if (Display != CssConstants.TableCell && Display != CssConstants.Table)
{
double width = ContainingBlock.Size.Width
- ContainingBlock.ActualPaddingLeft - ContainingBlock.ActualPaddingRight
- ContainingBlock.ActualBorderLeftWidth - ContainingBlock.ActualBorderRightWidth;
if (Width != CssConstants.Auto && !string.IsNullOrEmpty(Width))
{
width = CssValueParser.ParseLength(Width, width, this);
}
Size = new RSize(width, Size.Height);
// must be separate because the margin can be calculated by percentage of the width
Size = new RSize(width - ActualMarginLeft - ActualMarginRight, Size.Height);
}
if (Display != CssConstants.TableCell)
{
var prevSibling = DomUtils.GetPreviousSibling(this);
double left = ContainingBlock.Location.X + ContainingBlock.ActualPaddingLeft + ActualMarginLeft + ContainingBlock.ActualBorderLeftWidth;
double top = (prevSibling == null && ParentBox != null ? ParentBox.ClientTop : ParentBox == null ? Location.Y : 0) + MarginTopCollapse(prevSibling) + (prevSibling != null ? prevSibling.ActualBottom + prevSibling.ActualBorderBottomWidth : 0);
Location = new RPoint(left, top);
ActualBottom = top;
}
//If we're talking about a table here..
if (Display == CssConstants.Table || Display == CssConstants.InlineTable)
{
CssLayoutEngineTable.PerformLayout(g, this);
}
else
{
//If there's just inline boxes, create LineBoxes
if (DomUtils.ContainsInlinesOnly(this))
{
ActualBottom = Location.Y;
CssLayoutEngine.CreateLineBoxes(g, this); //This will automatically set the bottom of this block
}
else if (_boxes.Count > 0)
{
foreach (var childBox in Boxes)
{
childBox.PerformLayout(g);
}
ActualRight = CalculateActualRight();
ActualBottom = MarginBottomCollapse();
}
}
}
else
{
var prevSibling = DomUtils.GetPreviousSibling(this);
if (prevSibling != null)
{
if (Location == RPoint.Empty)
Location = prevSibling.Location;
ActualBottom = prevSibling.ActualBottom;
}
}
ActualBottom = Math.Max(ActualBottom, Location.Y + ActualHeight);
CreateListItemBox(g);
var actualWidth = Math.Max(GetMinimumWidth() + GetWidthMarginDeep(this), Size.Width < 90999 ? ActualRight - HtmlContainer.Root.Location.X : 0);
HtmlContainer.ActualSize = CommonUtils.Max(HtmlContainer.ActualSize, new RSize(actualWidth, ActualBottom - HtmlContainer.Root.Location.Y));
}
/// <summary>
/// Assigns words its width and height
/// </summary>
/// <param name="g"></param>
internal virtual void MeasureWordsSize(RGraphics g)
{
if (!_wordsSizeMeasured)
{
if (BackgroundImage != CssConstants.None && _imageLoadHandler == null)
{
_imageLoadHandler = new ImageLoadHandler(HtmlContainer, OnImageLoadComplete);
_imageLoadHandler.LoadImage(BackgroundImage, HtmlTag != null ? HtmlTag.Attributes : null);
}
MeasureWordSpacing(g);
if (Words.Count > 0)
{
foreach (var boxWord in Words)
{
if (FontVariant == CssConstants.SmallCaps)
boxWord.Width = boxWord.Text != "\n" ? g.MeasureSmallCapString(boxWord.Text, ActualFont, ActualFontForSmallCaps).Width : 0;
else
boxWord.Width = boxWord.Text != "\n" ? g.MeasureString(boxWord.Text, ActualFont).Width : 0;
boxWord.Height = ActualFont.Height;
}
}
_wordsSizeMeasured = true;
}
}
/// <summary>
/// Get the parent of this css properties instance.
/// </summary>
/// <returns></returns>
protected override sealed CssBoxProperties GetParent()
{
return _parentBox;
}
/// <summary>
/// Gets the index of the box to be used on a (ordered) list
/// </summary>
/// <returns></returns>
private int GetIndexForList()
{
bool reversed = !string.IsNullOrEmpty(ParentBox.GetAttribute("reversed"));
int index;
if (!int.TryParse(ParentBox.GetAttribute("start"), out index))
{
if (reversed)
{
index = 0;
foreach (CssBox b in ParentBox.Boxes)
{
if (b.Display == CssConstants.ListItem)
index++;
}
}
else
{
index = 1;
}
}
foreach (CssBox b in ParentBox.Boxes)
{
if (b.Equals(this))
return index;
if (b.Display == CssConstants.ListItem)
index += reversed ? -1 : 1;
}
return index;
}
/// <summary>
/// Creates the <see cref="_listItemBox"/>
/// </summary>
/// <param name="g"></param>
private void CreateListItemBox(RGraphics g)
{
if (Display == CssConstants.ListItem && ListStyleType != CssConstants.None)
{
if (_listItemBox == null)
{
_listItemBox = new CssBox(null, null);
_listItemBox.InheritStyle(this);
_listItemBox.Display = CssConstants.Inline;
_listItemBox.HtmlContainer = HtmlContainer;
if (ListStyleType.Equals(CssConstants.Disc, StringComparison.InvariantCultureIgnoreCase))
{
_listItemBox.Text = new SubString("•");
}
else if (ListStyleType.Equals(CssConstants.Circle, StringComparison.InvariantCultureIgnoreCase))
{
_listItemBox.Text = new SubString("o");
}
else if (ListStyleType.Equals(CssConstants.Square, StringComparison.InvariantCultureIgnoreCase))
{
_listItemBox.Text = new SubString("♠");
}
else if (ListStyleType.Equals(CssConstants.Decimal, StringComparison.InvariantCultureIgnoreCase))
{
_listItemBox.Text = new SubString(GetIndexForList().ToString(CultureInfo.InvariantCulture) + ".");
}
else if (ListStyleType.Equals(CssConstants.DecimalLeadingZero, StringComparison.InvariantCultureIgnoreCase))
{
_listItemBox.Text = new SubString(GetIndexForList().ToString("00", CultureInfo.InvariantCulture) + ".");
}
else
{
_listItemBox.Text = new SubString(CommonUtils.ConvertToAlphaNumber(GetIndexForList(), ListStyleType) + ".");
}
_listItemBox.ParseToWords();
_listItemBox.PerformLayoutImp(g);
_listItemBox.Size = new RSize(_listItemBox.Words[0].Width, _listItemBox.Words[0].Height);
}
_listItemBox.Words[0].Left = Location.X - _listItemBox.Size.Width - 5;
_listItemBox.Words[0].Top = Location.Y + ActualPaddingTop; // +FontAscent;
}
}
/// <summary>
/// Searches for the first word occurrence inside the box, on the specified linebox
/// </summary>
/// <param name="b"></param>
/// <param name="line"> </param>
/// <returns></returns>
internal CssRect FirstWordOccourence(CssBox b, CssLineBox line)
{
if (b.Words.Count == 0 && b.Boxes.Count == 0)
{
return null;
}
if (b.Words.Count > 0)
{
foreach (CssRect word in b.Words)
{
if (line.Words.Contains(word))
{
return word;
}
}
return null;
}
else
{
foreach (CssBox bb in b.Boxes)
{
CssRect w = FirstWordOccourence(bb, line);
if (w != null)
{
return w;
}
}
return null;
}
}
/// <summary>
/// Gets the specified Attribute, returns string.Empty if no attribute specified
/// </summary>
/// <param name="attribute">Attribute to retrieve</param>
/// <returns>Attribute value or string.Empty if no attribute specified</returns>
internal string GetAttribute(string attribute)
{
return GetAttribute(attribute, string.Empty);
}
/// <summary>
/// Gets the value of the specified attribute of the source HTML tag.
/// </summary>
/// <param name="attribute">Attribute to retrieve</param>
/// <param name="defaultValue">Value to return if attribute is not specified</param>
/// <returns>Attribute value or defaultValue if no attribute specified</returns>
internal string GetAttribute(string attribute, string defaultValue)
{
return HtmlTag != null ? HtmlTag.TryGetAttribute(attribute, defaultValue) : defaultValue;
}
/// <summary>
/// Gets the minimum width that the box can be.<br/>
/// The box can be as thin as the longest word plus padding.<br/>
/// The check is deep thru box tree.<br/>
/// </summary>
/// <returns>the min width of the box</returns>
internal double GetMinimumWidth()
{
double maxWidth = 0;
CssRect maxWidthWord = null;
GetMinimumWidth_LongestWord(this, ref maxWidth, ref maxWidthWord);
double padding = 0f;
if (maxWidthWord != null)
{
var box = maxWidthWord.OwnerBox;
while (box != null)
{
padding += box.ActualBorderRightWidth + box.ActualPaddingRight + box.ActualBorderLeftWidth + box.ActualPaddingLeft;
box = box != this ? box.ParentBox : null;
}
}
return maxWidth + padding;
}
/// <summary>
/// Gets the longest word (in width) inside the box, deeply.
/// </summary>
/// <param name="box"></param>
/// <param name="maxWidth"> </param>
/// <param name="maxWidthWord"> </param>
/// <returns></returns>
private static void GetMinimumWidth_LongestWord(CssBox box, ref double maxWidth, ref CssRect maxWidthWord)
{
if (box.Words.Count > 0)
{
foreach (CssRect cssRect in box.Words)
{
if (cssRect.Width > maxWidth)
{
maxWidth = cssRect.Width;
maxWidthWord = cssRect;
}
}
}
else
{
foreach (CssBox childBox in box.Boxes)
GetMinimumWidth_LongestWord(childBox, ref maxWidth, ref maxWidthWord);
}
}
/// <summary>
/// Get the total margin value (left and right) from the given box to the given end box.<br/>
/// </summary>
/// <param name="box">the box to start calculation from.</param>
/// <returns>the total margin</returns>
private static double GetWidthMarginDeep(CssBox box)
{
double sum = 0f;
if (box.Size.Width > 90999 || (box.ParentBox != null && box.ParentBox.Size.Width > 90999))
{
while (box != null)
{
sum += box.ActualMarginLeft + box.ActualMarginRight;
box = box.ParentBox;
}
}
return sum;
}
/// <summary>
/// Gets the maximum bottom of the boxes inside the startBox
/// </summary>
/// <param name="startBox"></param>
/// <param name="currentMaxBottom"></param>
/// <returns></returns>
internal double GetMaximumBottom(CssBox startBox, double currentMaxBottom)
{
foreach (var line in startBox.Rectangles.Keys)
{
currentMaxBottom = Math.Max(currentMaxBottom, startBox.Rectangles[line].Bottom);
}
foreach (var b in startBox.Boxes)
{
currentMaxBottom = Math.Max(currentMaxBottom, GetMaximumBottom(b, currentMaxBottom));
}
return currentMaxBottom;
}
/// <summary>
/// Get the <paramref name="minWidth"/> and <paramref name="maxWidth"/> width of the box content.<br/>
/// </summary>
/// <param name="minWidth">The minimum width the content must be so it won't overflow (largest word + padding).</param>
/// <param name="maxWidth">The total width the content can take without line wrapping (with padding).</param>
internal void GetMinMaxWidth(out double minWidth, out double maxWidth)
{
double min = 0f;
double maxSum = 0f;
double paddingSum = 0f;
double marginSum = 0f;
GetMinMaxSumWords(this, ref min, ref maxSum, ref paddingSum, ref marginSum);
maxWidth = paddingSum + maxSum;
minWidth = paddingSum + (min < 90999 ? min : 0);
}
/// <summary>
/// Get the <paramref name="min"/> and <paramref name="maxSum"/> of the box words content and <paramref name="paddingSum"/>.<br/>
/// </summary>
/// <param name="box">the box to calculate for</param>
/// <param name="min">the width that allows for each word to fit (width of the longest word)</param>
/// <param name="maxSum">the max width a single line of words can take without wrapping</param>
/// <param name="paddingSum">the total amount of padding the content has </param>
/// <param name="marginSum"></param>
/// <returns></returns>
private static void GetMinMaxSumWords(CssBox box, ref double min, ref double maxSum, ref double paddingSum, ref double marginSum)
{
double? oldSum = null;
// not inline (block) boxes start a new line so we need to reset the max sum
if (box.Display != CssConstants.Inline && box.Display != CssConstants.TableCell && box.WhiteSpace != CssConstants.NoWrap)
{
oldSum = maxSum;
maxSum = marginSum;
}
// add the padding
paddingSum += box.ActualBorderLeftWidth + box.ActualBorderRightWidth + box.ActualPaddingRight + box.ActualPaddingLeft;
// for tables the padding also contains the spacing between cells
if (box.Display == CssConstants.Table)
paddingSum += CssLayoutEngineTable.GetTableSpacing(box);
if (box.Words.Count > 0)
{
// calculate the min and max sum for all the words in the box
foreach (CssRect word in box.Words)
{
maxSum += word.FullWidth + (word.HasSpaceBefore ? word.OwnerBox.ActualWordSpacing : 0);
min = Math.Max(min, word.Width);
}
// remove the last word padding
if (box.Words.Count > 0 && !box.Words[box.Words.Count - 1].HasSpaceAfter)
maxSum -= box.Words[box.Words.Count - 1].ActualWordSpacing;
}
else
{
// recursively on all the child boxes
for (int i = 0; i < box.Boxes.Count; i++)
{
CssBox childBox = box.Boxes[i];
marginSum += childBox.ActualMarginLeft + childBox.ActualMarginRight;
//maxSum += childBox.ActualMarginLeft + childBox.ActualMarginRight;
GetMinMaxSumWords(childBox, ref min, ref maxSum, ref paddingSum, ref marginSum);
marginSum -= childBox.ActualMarginLeft + childBox.ActualMarginRight;
}
}
// max sum is max of all the lines in the box
if (oldSum.HasValue)