Repository navigation
Expand file tree
/
Copy pathcssbase.pas
More file actions
10254 lines (9795 loc) · 378 KB
/
Copy pathcssbase.pas
File metadata and controls
10254 lines (9795 loc) · 378 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
(*
Version 0.2
Author: Pavol Stugel (pstugel@gmail.com)
Warning: this is work in progress version, all methods can be changed in future!
Scope:
- very basic html(node) + basic css layout support. KISS (https://en.wikipedia.org/wiki/KISS_principle)
- Tailwind CSS like utility classes (see csstailwind.pas)
TODO (in random order):
- transition support
- opacity, inset box-shadow, line-height
- radial gradients, multiple backgrounds
- display:grid
- delphi support
History:
2026.10.10 - CSS engine rework: selectors and cascade, flexbox (direction, wrap, gap, grow/shrink/basis,
justify-content, align-items/self, auto margins, order), box-sizing, min/max sizes, em/rem units,
high dpi pixel ratio, float:right, font properties, text wrapping, full color table,
utility classes support (csstailwind.pas), tests and demo
- antialiased painting of rounded corners and border joins (cached corner bitmaps),
linear-gradient, background images (url, size, position, repeat), colors with alpha,
custom properties and var()
- box-shadow (9-slice cached templates), native drawing of images and horizontal/vertical gradients,
bitmaps in device resolution (Retina)
- transform (translate, scale, rotate, skew, matrix, transform-origin, translate/rotate/scale properties),
HitTest with inverse transformation, dashed and dotted borders, <img> element, element default styles,
basic html markup (csshtml.pas)
- scrolling (overflow auto/scroll, overlay scrollbars, mouse wheel, thumb dragging), z-index,
:focus and :focus-within, automatic minimum height of flex items in column
2020.05.01 - Align LCL controls working again
2020.04.17 - TCSSShape fixes, "0" length fixed
2020.02.22 - baseline align fix, added: display:inline-flex; basic owerflow support
2019.12.18 - fixed layout, code reorganization
2019.12.07 - added basic display:flex support, added position (absolute,relative,static) support, fixed bugs
2019.12.03 - added support for border and border-radius, fixed vertical-align for baseline
2019.11.21 - fixed font size calculation, added basic class support
*)
unit cssbase;
{$ifdef fpc}
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
{$else}
{$define delphi}
{$if declared(FireMonkeyVersion) AND (FireMonkeyVersion >= 1)}
{$define fmx}
{$endif}
{$define fmx}
{$endif}
interface
uses
Classes, SysUtils, Types,
{$ifdef fpc}
Controls, Graphics, Forms, GraphType, IntfGraphics, FPImage,
LCLIntf, LCLProc, LMessages, LCLType, contnrs, LazCanvas,
{$else}
// delphi
System.UITypes,
FMX.Controls, FMX.Graphics,
{$endif}
Math;
const
CSS_UNIVERSAL_SELECTOR: Char = '*'; //https://developer.mozilla.org/en-US/docs/Web/CSS/Universal_selectors
CSS_DEBUG_MODE: Boolean = False;
HTMLInterface = '{DD8167E4-D923-407A-AE4C-14BB93E254E3}';
CSS_DEG_RAD = Pi / 180;
CSS_DEFAULT_FONT_SIZE = 16; // px, browser default font size
CSS_UTILITY_ORDER_BASE = 100000000; // utility classes are after stylesheet rules
CSS_UTILITY_BREAKPOINT_ORDER = 1000000; // responsive utilities (sm:, md: ...) are after base utilities
type
TCSSBorderStyle = (cbsUndefined, cbsNone, cbsHidden, cbsDotted, cbsDashed, cbsSolid, cbsDouble, cbsGroove, cbsRidge, cbsInset, cbsOutset); // https://developer.mozilla.org/en-US/docs/Web/CSS/border-style
TCSSFontStyle = (cfsUndefined, cfsNormal, cfsItalic, cfsOblique, cfsInherit, cfsUnset); // https://developer.mozilla.org/en-US/docs/Web/CSS/font-style
TCSSFontWeightType = (cfwUndefined, cfwNormal, cfwBold, cfwLighter, cfwBolder, cfwInherit, cfwUnset); // https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight
TCSSLengthType = (cltUndefined, cltPx, cltPt, cltAuto, cltEm, cltPercentage, cltRem);
TCSSTextAlign = (ctaUndefined, ctaLeft, ctaRight, ctaCenter, ctaJustify); // https://developer.mozilla.org/en-US/docs/Web/CSS/text-align
TCSSColorType = (cctUndefined, cctNormal, cctTransparent, cctCurrentColor);
TCSSTextDecoration = (ctdUndefined, ctdUnderline, ctdNone, ctdLineThrough); // https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration
TCSSDisplayType = (cdtBlock, cdtInline, cdtFlex, cdtInlineFlex, cdtNone, cdtGrid); // https://developer.mozilla.org/en-US/docs/Web/CSS/display
TCSSFloatType = (cftUndefined, cftNone, cftLeft, cftRight); // https://developer.mozilla.org/en-US/docs/Web/CSS/float
TCSSSide = (csTop, csLeft, csBottom, csRight); // for margin, padding etc.
TCSSFlexWrap = (cfwNoWrap, cfwWrap, cfwWrapReverse); // https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap
TCSSFlexDirection = (cfdRow, cfdRowReverse, cfdColumn, cfdColumnReverse); // https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction
TCSSPosition = (cpStatic, cpRelative, cpAbsolute, cpFixed, cpSticky); // https://developer.mozilla.org/en-US/docs/Web/CSS/position
TCSSAlignItems = (caiStretch, caiBaseline, caiCenter, caiFlexStart, caiFlexEnd, caiAuto); // https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Aligning_Items_in_a_Flex_Container
TCSSJustifyContent = (cjcFlexStart, cjcFlexEnd, cjcCenter, cjcSpaceBetween, cjcSpaceAround, cjcSpaceEvenly); // https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content
TCSSOverflowType = (cotVisible, cotHidden, cotScroll, cotAuto); // https://developer.mozilla.org/en-US/docs/Web/CSS/overflow
TCSSBoxSizing = (cbxContentBox, cbxBorderBox); // https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing
TCSSWhiteSpace = (cwsNormal, cwsNoWrap); // https://developer.mozilla.org/en-US/docs/Web/CSS/white-space
// inherited properties explicitly set by style (others are inherited from parent node)
TCSSInheritedProp = (cipColor, cipFontSize, cipFontFamily, cipFontStyle, cipFontWeight, cipTextDecoration, cipTextAlign, cipWhiteSpace);
TCSSInheritedProps = set of TCSSInheritedProp;
{$ifndef fpc}
TStringArray = array of String;
{$endif}
TFloatPoint = record
x,y: Single;
end;
TPointArray = array of TFloatPoint;
TCSSLength = record // https://developer.mozilla.org/en-US/docs/Web/CSS/length
Value: Single;
LengthType: TCSSLengthType;
end;
TCSSInteger = Integer;
TCSSNumber = Single;
TCSSMatrix = array [0..5] of Single;
{ TMatrixTransform }
{
https://www.w3.org/TR/SVG/coords.html#NestedTransformations
Base Affine Translation Matrix
( M0 M2 M4 )
( M1 M3 M5 )
( 0 0 1 )
}
TMatrixTransform = class
protected
FData: TCSSMatrix;
public
constructor Create; overload; virtual;
constructor Create(M0, M1, M2, M3, M4, M5: Single); overload;
procedure Multiply(AMatrix: TMatrixTransform);
function Setup(M0, M1, M2, M3, M4, M5: Single): TMatrixTransform; inline;
function Reset: TMatrixTransform;
function FlipHorizontal: TMatrixTransform;
function FlipVertical: TMatrixTransform;
function Scale(AX, AY: Single): TMatrixTransform;
function Skew(AX, AY: Single): TMatrixTransform;
function Rotate(Angle: Single): TMatrixTransform;
function Translate(AX, AY: Single): TMatrixTransform;
procedure Transform(var P: TFloatPoint);
end;
TCSSColor = record
Value: TColor;
ColorType: TCSSColorType;
Alpha: Byte; // 255 = opaque
end;
TCSSBorder = record // https://developer.mozilla.org/en-US/docs/Web/CSS/border
Color: TCSSColor;
Style: TCSSBorderStyle;
Width: TCSSLength;
end;
TCSSBackground = record // https://developer.mozilla.org/en-US/docs/Web/CSS/background
Color: TCSSColor;
Image: String; // background-image: url(...) or linear-gradient(...), can contain var()
Size: String; // background-size: auto, cover, contain, <length> [<length>]
Position: String; // background-position
RepeatMode: String; // background-repeat: repeat (default), no-repeat
end;
TCSSBorderRadius = record // https://developer.mozilla.org/en-US/docs/Web/CSS/border-radius
Width,
Height: TCSSLength;
end;
TCSSFontWeight = record
FontType: TCSSFontWeightType;
Value: TFontStyles;
end;
TCSSFont = record // https://developer.mozilla.org/en-US/docs/Web/CSS/font
Family: String;
Size: TCSSLength;
Style: TCSSFontStyle;
Weight: TCSSFontWeight;
TextDecoration: TCSSTextDecoration; // TODO: maybe here is not right place
CachedText: String; // TODO: move this part away
CachedWidth,
CachedHeight,
CachedBaseLine: Integer;
CachedMinWidth: Integer; // width of longest word
CachedPixelRatio: Single;
end;
TCSSStyleSheet = class;
TCSSPaintShape = record
L, T, R, B: Single; // border box
BW: array[TCSSSide] of Single; // border widths
RX, RY: array[0..3] of Single; // corner radii: top-left, top-right, bottom-right, bottom-left
end;
TCSSPixel = packed record // 32 bit BGRA pixel (not premultiplied)
B, G, R, A: Byte;
end;
PCSSPixel = ^TCSSPixel;
{ TCSSImageData - decoded image for background-image }
TCSSImageData = class
Width, Height: Integer;
Pixels: array of TCSSPixel;
Premultiplied: array of LongWord; // A shl 24 or R shl 16 or G shl 8 or B, color multiplied by alpha
HasAlpha: Boolean;
Bitmap: TBitmap; // for native drawing, created on first use
destructor Destroy; override;
end;
TCSSRGBA = record
R, G, B, A: Single; // 0..1, not premultiplied
end;
TCSSGradientStop = record
Color: TCSSRGBA;
Pos: Single; // 0..1, -1 = not set
end;
TCSSGradient = record
DirX, DirY: Single; // direction of gradient line
Stops: array of TCSSGradientStop;
end;
TCSSShadow = record
X, Y, Blur, Spread: Integer; // canvas pixels
Color: TCSSColor;
Inset: Boolean;
end;
TCSSShadowArray = array of TCSSShadow;
TCSSMatrix2D = record // affine transformation: x' = A*x + C*y + E, y' = B*x + D*y + F
A, B, C, D, E, F: Single;
end;
TCSSTransformKind = (tkNone, tkTranslate, tkGeneral);
TCSSTiming = record // cubic-bezier() timing function
X1, Y1, X2, Y2: Single;
end;
TCSSColorChannels = array[0..5] of TCSSColor;
TCSSRectArray = array of TRect; // background, color, border top, right, bottom, left
PCSSAnim = ^TCSSAnim;
TCSSAnim = record // running transition and/or built-in animation of node
Start: QWord;
Duration, Delay: Integer; // ms
Timing: TCSSTiming;
ColorOn: array[0..5] of Boolean;
ColorFrom, ColorTo: TCSSColorChannels;
OpacityOn: Boolean;
OpacityFrom, OpacityTo, BaseOpacity: Single;
MatrixOn, MatrixPending: Boolean; // pending: target matrix is known after layout
MatrixFrom, MatrixTo: TCSSMatrix2D;
Frames: Integer; // built-in keyframes: 0 none, 1 spin, 2 ping, 3 pulse, 4 bounce
FramesDuration: Integer;
FramesStart: QWord;
end;
THtmlNode = class;
TCSSLayoutKey = record // input of LayoutGetNodeSize
PW, PH, FW, FH: Integer;
InFlex: Boolean;
end;
TCSSLayoutResult = record // size of node for input (cached during one layout pass)
Key: TCSSLayoutKey;
Margin, Padding, Border, Position: TRect;
BaseLine, ContentWidth, ContentHeight: Integer;
end;
TCSSLayoutState = record // saved layout of node (transformed node is laid out again in higher resolution)
Node: THtmlNode;
ContentRect, MarginRect, Margin, Padding, Border, Position: TRect;
BaseLine, ContentWidth, ContentHeight, LayoutWidth, LayoutHeight: Integer;
FontSize: Single;
Transform, TransformTarget: TCSSMatrix2D;
Kind: TCSSTransformKind;
Visible, LayoutRoot: Boolean;
end;
TCSSLayoutStates = array of TCSSLayoutState;
TCSSCombinator = (ccNone, ccDescendant, ccChild);
{ TCSSCompoundSelector - one part of selector eg. "div.button:hover" }
TCSSCompoundSelector = record
Element: String; // '' or '*' = any element
Id: String;
Classes: TStringArray;
Pseudos: TStringArray; // hover, active, first-child, last-child
Combinator: TCSSCombinator; // relation to previous (left) compound
end;
TCSSCompoundSelectors = array of TCSSCompoundSelector;
{ TCSSItem }
TCSSItem = class(TPersistent)
public
Attributes: String;
AlignSelf, // default auto
AlignItems: TCSSAlignItems;
Background: TCSSBackground;
BoxSizing: TCSSBoxSizing;
BoxShadow: String; // box-shadow, can contain var()
ObjectFit, // img: fill (default), contain, cover, none, scale-down
ObjectPosition: String;
Transform, // transform functions, can contain var()
TransformOrigin,
Translate, // individual transform properties
Rotate,
Scale: String;
Opacity: Single; // 0..1, default 1
PointerEventsNone: Boolean; // pointer-events: none (mouse ignores node and its children)
TransitionProperty, // all (default), none, list of properties
TransitionTiming: String; // ease, linear, ease-in, ease-out, ease-in-out, cubic-bezier()
TransitionDuration, // ms
TransitionDelay: Integer;
Animation: String; // built-in animations: spin, ping, pulse, bounce (animation: spin 1s linear infinite)
LineClamp: Integer; // line-clamp: text is cut to N lines with ellipsis (0 = none)
ScrollbarNone: Boolean; // scrollbar-width: none (content can still be scrolled)
AspectRatio: Single; // aspect-ratio: width / height (0 = auto)
Color: TCSSColor;
Cursor: TCursor;
Changed: Boolean;
Display: TCSSDisplayType;
Font: TCSSFont;
Float: TCSSFloatType;
FlexBasis: TCSSLength; // default auto
FlexDirection: TCSSFlexDirection;
FlexGrow: TCSSNumber; // default 0
FlexShrink: TCSSNumber; // default 1
FlexWrap: TCSSFlexWrap;
Border: array[TCSSSide] of TCSSBorder;
Margin: array[TCSSSide] of TCSSLength;
Padding: array[TCSSSide] of TCSSLength;
JustifyContent: TCSSJustifyContent;
Order: TCSSInteger; // default 0
Overflow, // overflow-x
OverflowY: TCSSOverflowType;
ZIndex: Integer; // paint order of siblings (positioned nodes), default 0
Position: TCSSPosition;
RadiusTopLeft,
RadiusTopRight,
RadiusBottomLeft,
RadiusBottomRight: TCSSBorderRadius;
RowGap,
ColumnGap: TCSSLength;
GridTemplateColumns, // grid-template-columns: repeat(3, minmax(0, 1fr)), 200px 1fr, auto 1fr ...
GridTemplateRows: String;
GridColumnSpan, // grid-column: span N (-1 = all columns, 0 = auto)
GridRowSpan: Integer; // grid-row: span N
Specified: TCSSInheritedProps; // inherited properties set by this style
TextAlign: TCSSTextAlign;
WhiteSpace: TCSSWhiteSpace;
Height,
Width,
MinHeight,
MinWidth,
MaxHeight,
MaxWidth: TCSSLength;
Top,
Left,
Right,
Bottom: TCSSLength;
Weight: Integer; // "order" in CSS stylesheet
Specificity: Integer; // selector specificity (id * 10000 + class * 100 + element)
SelectorText: String; // selector of this rule (stylesheet items only)
Selector: TCSSCompoundSelectors; // parsed SelectorText (see CompileSelector)
SelectorCompiled,
SelectorValid: Boolean;
CustomProps: TStringList; // custom properties (--name: value)
procedure CompileSelector;
constructor Create;
destructor Destroy; override;
procedure Reset;
procedure Parse(const AValue: String);
procedure ParseFromStyleSheet(AStyleSheet: TCSSStyleSheet; const APropertyName: String);
procedure AddProperty(AName: String; AValue: String);
procedure AddParsedProperty(AName: String; AValue: String);
procedure Assign(Source: TPersistent); override;
procedure Merge(AValue: TCSSItem);
function LayoutKey: String; // values which change layout (sizes, spacing, fonts, display ...)
function PaintKey: String; // values which change only painting (colors, borders, shadows, transforms ...)
end;
{ TCSSStyleSheet }
TCSSStyleSheet = class(TComponent)
private
FList: TStringList;
FStyle: String;
function GetSelectorByName(AName: String): TCSSItem;
function GetCount: Integer;
function GetItem(AIndex: Integer): TCSSItem;
procedure SetStyle(AValue: String);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property Selector[AName: String]: TCSSItem read GetSelectorByName;
property Count: Integer read GetCount;
property Items[AIndex: Integer]: TCSSItem read GetItem;
function Append(const AValue: String): TCSSStyleSheet;
published
property Style: String read FStyle write SetStyle;
end;
{ TCSSClassList }
TCSSClassList = class // https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
private
FList: TStringList;
function GetCount: Integer;
function GetItem(AIndex: Integer): String;
function GetName: String;
procedure SetName(AValue: String);
public
constructor Create;
destructor Destroy; override;
function Add(AName: String): TCSSClassList;
function Remove(AName: String): TCSSClassList;
function Replace(AFrom, ATo: String): TCSSClassList;
function Toggle(AName: String): TCSSClassList;
function Contains(const AName: String): Boolean;
property Count: Integer read GetCount; // length
property Name: String read GetName write SetName;
property Item[AIndex: Integer]: String read GetItem;
end;
{ THtmlSize }
THtmlSize = class(TPersistent)
public
Position,
ContentRect, // MarginRect - Margin
MarginRect: TRect; // Margin rect (whole region)
BaseLine: Integer; // px from bottom of MarginRect
Border,
Padding,
Margin: TRect;
ContentWidth, // content + border + padding
ContentHeight: Integer; // content + border + padding
FontSize: Single; // computed font size in px (0 = default font)
function TopSpace: Integer; inline;
function LeftSpace: Integer; inline;
function RightSpace: Integer; inline;
function BottomSpace: Integer; inline;
function RealContentWidth: Integer; inline;
function RealContentRect: TRect; inline; // ContentRect - Border - Padding
end;
THtmlNodeArray = array of THtmlNode;
{ THtmlNode }
THtmlNode = class(TPersistent)
private
FOnClick: TNotifyEvent;
FHovered: Boolean;
FActive: Boolean; // mouse is pressed on node (:active)
FViewportWidth: Integer; // root node only: width for responsive variants (sm:, md: ...)
FParentControl: TControl;
FChangedMark: Boolean; // node was changed since last layout of shape (its area is painted again)
FDirtyOld: TRect; // root only: old area of changed and removed nodes
FHasChanges, FChangedAll: Boolean; // root only: some nodes were changed, whole tree is painted again
FFreeMe: Boolean;
FElementName, // div, span ...
FId: String; // id for access
FAlignControlWasHidden: Boolean; // store if AlignControl was hidden by display style
FAlignControl: TControl;
FTotalCount,
FChildCount: Integer;
FFirstNode,
FLastNode,
FFirstChild,
FNextSibling,
FPrevSibling: THtmlNode;
FInlineStyle,
FHoverStyle: String;
FText: String;
FCompStyle: TCSSItem; // computed active to render style
FCompSize: THtmlSize; // computed size
// cache section here
FCachedFont: TCSSFont;
FStyleSheet: TCSSStyleSheet;
FClassList: TCSSClassList;
FAttributes: TStringList; // html attributes (src ...)
FData: TObject; // user data (object), freed with node when FOwnsData
FOwnsData: Boolean;
FScrollX, FScrollY: Integer; // scroll position of content (overflow auto/scroll/hidden)
FScrollMin, FScrollMax: TPoint;
FVThumb, FHThumb: TRect; // scrollbar thumbs (last paint)
FFocused: Boolean; // :focus (set by application or TCSSShape for aligned controls)
FTransform: TCSSMatrix2D; // transformation relative to border box (see ComputeTransform)
FTransformKind: TCSSTransformKind;
FTransformBmp: TBitmap; // cached painted node for scale, rotate
FTransformKey: String;
FTransformTarget: TCSSMatrix2D; // transformation from styles (FTransform is animated)
FTransformSrc: array of LongWord; // cached source of transformed node (premultiplied ARGB)
FTransformSrcW, FTransformSrcH: Integer;
FTransformSrcKey: String;
FAnim: PCSSAnim; // running transition or animation (nil = none)
FColorAnimated: Boolean; // inherited color is animated by transition of ancestor
FStyled: Boolean; // node was painted (transitions start only for painted nodes, as in browser)
FTransformSig: String; // resolved transform properties (change starts transition)
FClampKey, FClampResult: String; // line-clamp: text with ellipsis for width, lines, font and text (cache)
FStickyDY: Integer; // position: sticky - vertical offset of painted node (scroll of container)
FBgCache: array[0..5] of record // cached background bitmaps: 0..3 corners, 4 whole, 5 gradient strip
Key: String;
Bmp: TBitmap;
end;
FVisible: Boolean;
FPixelRatio: Single; // root node only: scale factor for high dpi
FPaintScale: Single; // root node only: device pixels per canvas pixel (2 on macOS Retina)
FRenderScale: Single; // root node only: scale of offscreen rendering of transformed node (default font is scaled)
FLayoutRoot: Boolean; // LayoutTo was called for this node
// layout cache: nested flex containers lay out items several times with the same input
FLayPass: Cardinal; // layout pass of cache
FLayResults: array[0..7] of TCSSLayoutResult;
FLayCount: Integer;
FLayState, FLayWanted: TCSSLayoutKey; // input of current layout of children, input of last request
FLayWantedPW, FLayWantedPH: Integer; // parent size of last request
FLayoutWidth, // size for root node set by LayoutTo (-1 = auto)
FLayoutHeight: Integer;
function GetStyleValue(AName: String): String;
procedure LayoutCalcPosition(ANode: THtmlNode; TargetWidth,
TargetHeight: Integer; out NewWidth, NewHeight: Integer);
procedure LayoutDoFlexAlign(AParentNode: THtmlNode; AList: TList; AWidth,
AHeight: Integer; out NewWidth, NewHeight: Integer);
function IsWhiteSpaceText: Boolean; // whitespace between elements in markup (no flex or grid item)
function HasPrevElementSibling: Boolean;
function StickyOffset: Integer;
procedure UpdateStickyOffsets;
function ClampLinesHeight(ALines: Integer): Integer;
function ClampedText(AMaxWidth: Integer): String;
function EllipsisText(AMaxWidth: Integer): String;
procedure LayoutDoGrid(AParentNode: THtmlNode; AList: TList; AWidth, AHeight: Integer; out NewWidth, NewHeight: Integer);
function LayoutDoVerticalAlign(AList: TList; ClearList: Boolean = True): Integer;
procedure LayoutGetNodeSize(ANode: THtmlNode; ParentWidth,
ParentHeight: Integer; InFlex: Boolean = False; AForceWidth: Integer = -1; AForceHeight: Integer = -1);
procedure ResolveInheritedStyle;
procedure IntSetAlignControl(AValue: TControl);
procedure SetHovered(AValue: Boolean);
procedure SetHoverStyle(AValue: String);
procedure SetInlineStyle(AValue: String);
procedure SetStyleValue(AName: String; AValue: String);
public
Tag: LongInt; // user data (number)
TagStr: String; // user data (text)
RootNode,
ParentNode: THtmlNode;
protected
procedure DrawNode(ACanvas: TCanvas; AClipRect: TRect); virtual;
procedure CalculateSize(out AWidth, AHeight, ABaseLine: Integer);virtual;
procedure MeasureText(AMaxWidth: Integer; out AWidth, AHeight, ABaseLine: Integer); virtual;
procedure AssignFontTo(AFont: TFont);
function BorderColor(ASide: TCSSSide): TColor;
function FlexTextRect(const AContent: TRect): TRect;
function MeasureLongestWord: Integer;
function MinContentWidth(AAutoMinimum: Boolean = False): Integer;
procedure CalcRadius(AWidth, AHeight: Integer; out ATopLeft, ATopRight, ABottomRight, ABottomLeft: TPoint);
procedure PaintBackgroundAndBorder(ACanvas: TCanvas);
procedure BackgroundImageRect(AImage: TCSSImageData; AWidth, AHeight: Integer; out ARect: TRectF);
function BackgroundLayer(const S: TCSSPaintShape; AWidth, AHeight: Integer; const AImage: String;
const ACornerW, ACornerH: array of Integer; AScale: Single; ASlot: Integer; const ARegion: TRect): TBitmap;
function GradientStrip(const AGradient: TCSSGradient; AWidth, AHeight: Integer; const AKey: String): TBitmap;
function ParseShadows(const AValue: String): TCSSShadowArray;
procedure PaintBoxShadow(ACanvas: TCanvas);
procedure PaintInsetShadow(ACanvas: TCanvas);
procedure ComputeTransform;
procedure UpdateTransformKind;
procedure UpdateTransitions(const AOldColors: TCSSColorChannels; AOldOpacity: Single; const AOldMatrix: TCSSMatrix2D);
procedure ApplyAnimation(ANow: QWord; out ADone: Boolean);
procedure StopAnimation;
function AnimMaxScale: Single;
procedure OffsetSubtree(DX, DY: Integer);
procedure PaintNodeTree(ACanvas: TCanvas; const AClipRect: TRect);
procedure PaintTransformed(ACanvas: TCanvas; const AClipRect: TRect);
procedure SaveLayout(var AStates: TCSSLayoutStates);
procedure PlaceAbsolute(AWidth, AHeight: Integer);
procedure UpdateScrollRange;
procedure PaintScrollbars(ACanvas: TCanvas);
public
constructor Create(AInlineStyle: String = '');virtual;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function ApplyStyle: THtmlNode;
function ApplyStyles: THtmlNode;
function RestyleState(ADeep: Boolean; var ADirty: TRect): Boolean; // after hover/active/focus change, True = layout needed
procedure ApplySubtree(ANode: THtmlNode);
procedure ApplyStyleForNode(ANode: THtmlNode);
procedure ApplyElementDefaults(ANode: THtmlNode);
function MatchesCompound(const ACompound: TCSSCompoundSelector): Boolean;
function Matches(const ASelector: TCSSCompoundSelectors): Boolean; overload;
function Matches(const ASelector: String): Boolean; overload;
function UtilityClass(const AClassName: String; out ADeclarations: String; out ASpecificity, AOrder: Integer): Boolean;
function SiblingIndex: Integer;
function HasHoveredAncestorWithClass(const AClass: String): Boolean;
function AddNode(ANode: THtmlNode): THtmlNode;
function AppendTo(AParentNode: THtmlNode): THtmlNode;
function InsertBefore(ANode, ARefNode: THtmlNode): THtmlNode; // like DOM, ARefNode = nil appends
function GetNext(ANode: THtmlNode; GoAboveChildren: Boolean = False): THtmlNode;
function GetElementsByClassName(AName: String): THtmlNodeArray;
function GetElementById(const AId: String): THtmlNode;
procedure LayoutTo(ALeft, ATop, AWidth, AHeight: Integer);
procedure AlignControlsTo(ALeft, ATop: Integer);
function NodeAtPosition(APos: TPoint; AClickable: Boolean = True): THtmlNode; // top node (clickable: its nearest node with OnClick)
function HitTest(const APos: TPoint): Boolean; virtual;
function Animating: Boolean; // root: some nodes are animated (transition or animation)
function AnimationTick(ANow: QWord): Boolean; // root: next frame of animations, True = repaint
function AnimationBounds: TRect; // root: painted area of animated nodes
function AnimationRects: TCSSRectArray; // root: painted areas of animated nodes (one per node)
function AnimatingTransitions: Boolean; // root: some transitions run (built-in animations need less fps)
function TransformPoint(const P: TPointF): TPointF;
function UntransformPoint(const P: TPointF): TPointF;
function ScreenToLayout(const P: TPointF): TPointF;
function LayoutToScreen(const P: TPointF): TPointF;
function TransformedBounds(const ARect: TRect): TRect;
property TransformKind: TCSSTransformKind read FTransformKind;
property TransformMatrix: TCSSMatrix2D read FTransform; // shown transformation (can be animated)
function ClipsContent: Boolean;
function PaddingBox: TRect;
function CanScroll(DX, DY: Integer): Boolean;
function ScrollBy(DX, DY: Integer): Boolean;
function SetScroll(AX, AY: Integer): Boolean;
function ThumbAt(const APos: TPoint): Integer;
function ScrollByThumb(AAxis, AStartScroll, ADelta: Integer): Boolean;
function PaintOrder: THtmlNodeArray;
function HasFocusWithin: Boolean;
function ScreenToLayoutClip(const P: TPointF; out AVisible: Boolean): TPointF;
function ScreenClipRect: TRect;
property ScrollX: Integer read FScrollX;
property ScrollY: Integer read FScrollY;
property ScrollMin: TPoint read FScrollMin;
property ScrollMax: TPoint read FScrollMax;
property Focused: Boolean read FFocused write FFocused;
function GetAttribute(const AName: String): String;
procedure SetAttribute(const AName, AValue: String);
property Attributes[const AName: String]: String read GetAttribute write SetAttribute; // also user texts (data-*)
property Data: TObject read FData write FData; // user object
property OwnsData: Boolean read FOwnsData write FOwnsData; // Data is freed with node
procedure PaintTo(ACanvas: TCanvas); overload;
procedure PaintTo(ACanvas: TCanvas; const AClipRect: TRect); overload;
function CalcLength(const AValue: TCSSLength; AParentLength: Integer): Integer;
function ResolveVars(const AValue: String; ADepth: Integer = 0): String;
function EffectiveColor(const AColor: TCSSColor): TColor;
function BorderWidth(ASide: TCSSSide): Integer;
function SetAlignControl(AValue: TControl): THtmlNode;
function SetClass(AValues: String): THtmlNode;
function SetInline(AValue: String): THtmlNode;
function SetId(AValue: String): THtmlNode;
function SetHover(AValue: String): THtmlNode;
function SetOnClick(AValue: TNotifyEvent): THtmlNode;
function SetTagStr(AValue: String): THtmlNode;
function SetText(const AValue: String): THtmlNode;
function Clear: THtmlNode;
function Delete(ANode: THtmlNode): THtmlNode;
procedure Detach;
function CountNodes(ANode: THtmlNode): Integer;
procedure Changed;
procedure NoteChanged(ANode: THtmlNode); // root: node (with subtree) is painted again, its old area is remembered
function TakeDirtyRect(const AOldRects: TCSSRectArray): TRect; // root: area to paint after layout (changes are cleared)
function PaintedRect: TRect; // area of node on screen (with shadow and transform), empty if not visible
function SubtreePaintedRect: TRect; // painted area of node and its descendants
function PaintedRects: TCSSRectArray; // painted areas of all nodes (preorder), compared after layout
property HasChanges: Boolean read FHasChanges;
property ChangedAll: Boolean read FChangedAll;
property ClassList: TCSSClassList read FClassList;
property CompStyle: TCSSItem read FCompStyle write FCompStyle;
property CompSize: THtmlSize read FCompSize write FCompSize;
property Element: String read FElementName write FElementName;
property FirstChild: THtmlNode read FFirstChild;
property Id: String read FId write FId;
property ParentControl: TControl read FParentControl write FParentControl;
property Style[AName: String]: String read GetStyleValue write SetStyleValue;
property Text: String read FText write FText;
property Hovered: Boolean read FHovered write SetHovered;
property Active: Boolean read FActive write FActive;
property ViewportWidth: Integer read FViewportWidth write FViewportWidth; // used from RootNode only, -1 = unknown
property ChildCount: Integer read FChildCount;
property Visible: Boolean read FVisible; // computed by layout (display:none node or parent)
property PixelRatio: Single read FPixelRatio write FPixelRatio; // used from RootNode only
property PaintScale: Single read FPaintScale write FPaintScale; // used from RootNode only, bitmaps are rendered in this resolution
published
property AlignControl: TControl read FAlignControl write IntSetAlignControl;
property InlineStyle: String read FInlineStyle write SetInlineStyle;
property HoverStyle: String read FHoverStyle write SetHoverStyle;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property StyleSheet: TCSSStyleSheet read FStyleSheet write FStyleSheet;
end;
THtmlNodeList = class
end;
{ utility classes (Tailwind like) support, see csstailwind.pas }
TCSSUtilityResolver = function(const AClassName: String; out ADeclarations: String; out AOrder: Integer): Boolean;
ICSSControl = interface
[HTMLInterface]
function GetBodyNode: THtmlNode;
procedure Changed;
end;
TColorObject = class
Value: TColor;
end;
var
CSSRootFontSize: Single = CSS_DEFAULT_FONT_SIZE; // px, base for "rem" units
CSSDefaultBoxSizing: TCSSBoxSizing = cbxContentBox; // initial box-sizing value (Tailwind like styles use border-box)
CSSAntialiasing: Boolean = True; // antialiasing of rounded corners and border joins
CSSDarkMode: Boolean = False; // dark: variant of utility classes is active (apply styles after change)
CSSAnimationsEnabled: Boolean = True; // False: transitions and animations are off (form designer)
type
TCSSStats = record // counters of layout and painting for performance tuning (application reads and resets them)
Layouts, Paints: Integer;
LayoutMs, PaintMs, MaxPaintMs: Double; // times are added by TCSSShape
PaintPixels: Int64; // area of painted rectangles (control pixels)
Bitmaps, Texts: Integer; // drawn bitmaps (corners, shadows, icons ...) and texts
BitmapMs, TextMs: Double; // time of drawing bitmaps and texts
StretchPixels: Int64; // area of bitmaps drawn resized (eg. semi-transparent backgrounds, sides of shadows)
Clips: Integer; // changes of clip rect of canvas
ClipMs: Double;
end;
var
CSSStats: TCSSStats;
CSSFastBlend: Boolean = True; // Windows: cached bitmaps are drawn by AlphaBlend directly (LCL premultiplies them in every draw)
CSSUtilityResolver: TCSSUtilityResolver = nil; // resolve unknown classes to CSS declarations
procedure ParseCSS(ACSS: String; ATarget: TStringList);
function ParseSelector(const ASelector: String; out ASpecificity: Integer): TCSSCompoundSelectors;
function CSSToColor(AValue: String): TCSSColor;
function CSSToLength(AValue: String): TCSSLength;
function CSSResolveUtility(const AClassName: String; out ADeclarations: String; out AOrder: Integer): Boolean;
procedure CSSClearUtilityCache;
procedure CSSClearPaintCache;
procedure CSSBeginUpdate; // many nodes are changed: shapes are not laid out after every change,
procedure CSSEndUpdate; // changed shapes are laid out once at the end (calls can be nested)
procedure CSSUpdateForget(AControl: TControl); // control is destroyed (removed from pending updates)
function CSSUpdatePending(AControl: TControl): Boolean; // in CSSBeginUpdate: control is laid out in CSSEndUpdate
function CSSAlphaBitmap(R, G, B, A: Byte): TBitmap;
procedure CSSRegisterImage(const AName: String; AGraphic: TGraphic);
function StrBefore(const AValue: String; const ASubstr: String): String;
function StrAfter(const AValue: String; const ASubstr: String): String;
function StrBetween(const AValue: String; const AStart, AEnd: String): String;
function HTMLDiv(AInlineStyle: String; AId: String = ''): THtmlNode;
function HTMLSpan(AInlineStyle: String; AText: String = ''; AId: String = ''): THtmlNode;
function HTMLNode(const AClass: String; const AText: String = ''; const AElement: String = 'div'): THtmlNode;
function CSSTimeMs: Double; // high resolution time in ms (for measuring)
// PointF and RectF are not in unit Types of FPC 3.2.2 (only in newer versions), they can be used from here
function PointF(AX, AY: Single): TPointF; inline;
function RectF(ALeft, ATop, ARight, ABottom: Single): TRectF; inline;
implementation
{$ifdef unix}
uses
Unix;
{$endif}
{$ifdef windows}
function QueryPerformanceCounter(var ACount: Int64): LongBool; stdcall; external 'kernel32' name 'QueryPerformanceCounter';
function QueryPerformanceFrequency(var AFreq: Int64): LongBool; stdcall; external 'kernel32' name 'QueryPerformanceFrequency';
{$endif}
function CSSTimeMs: Double;
{$ifdef windows}
var
C, F: Int64;
begin
C := 0;
F := 0;
QueryPerformanceCounter(C);
QueryPerformanceFrequency(F);
if F > 0 then Result := Double(C) * 1000 / Double(F) else Result := GetTickCount64; // Double: Single loses ms
end;
{$else}{$ifdef unix}
var
TV: TTimeVal;
begin
fpgettimeofday(@TV, nil);
Result := Double(TV.tv_sec) * 1000 + Double(TV.tv_usec) / 1000; // Double: Single loses ms
end;
{$else}
begin
Result := GetTickCount64;
end;
{$endif}{$endif}
{$ifdef windows}
type
{ TCSSWinBitmap - cached bitmap with premultiplied copy for AlphaBlend. LCL creates a premultiplied copy of a
32 bit bitmap in every draw, which is slow when hundreds of corners, shadows and icons are painted. }
TCSSWinBitmap = class(TBitmap)
private
FDC: HDC;
FDib, FOldBmp: HBITMAP;
public
procedure Prepare(AImage: TLazIntfImage);
destructor Destroy; override;
end;
function WinAlphaBlend(ADest: HDC; X, Y, W, H: Integer; ASrc: HDC; SX, SY, SW, SH: Integer; ABlend: LongWord): LongBool;
stdcall; external 'msimg32.dll' name 'AlphaBlend';
{$endif}
type
{ TCSSKeyList - sorted cache with exact keys, compared byte by byte (default comparison of TStringList uses
locale, which is slow: caches are searched for every corner, shadow and utility class) }
TCSSKeyList = class(TStringList)
protected
function DoCompareText(const S1, S2: String): PtrInt; override;
end;
function TCSSKeyList.DoCompareText(const S1, S2: String): PtrInt;
begin
Result := CompareStr(S1, S2);
end;
(*
Draw cached bitmap (resized when it has higher resolution, eg. Retina), counted in CSSStats
*)
procedure DrawCachedBitmap(ACanvas: TCanvas; const ARect: TRect; ABitmap: TBitmap);
var
T: Double;
begin
Inc(CSSStats.Bitmaps);
if (ABitmap.Width <> ARect.Width) or (ABitmap.Height <> ARect.Height) then
Inc(CSSStats.StretchPixels, Int64(ARect.Width) * ARect.Height);
T := CSSTimeMs;
{$ifdef windows}
if CSSFastBlend and (ABitmap is TCSSWinBitmap) and (TCSSWinBitmap(ABitmap).FDC <> 0) then begin
if (ARect.Width > 0) and (ARect.Height > 0) then
WinAlphaBlend(ACanvas.Handle, ARect.Left, ARect.Top, ARect.Width, ARect.Height, TCSSWinBitmap(ABitmap).FDC, 0, 0,
ABitmap.Width, ABitmap.Height, $01FF0000); // AC_SRC_OVER, constant alpha 255, AC_SRC_ALPHA (premultiplied)
end else
{$endif}
if (ABitmap.Width = ARect.Width) and (ABitmap.Height = ARect.Height) then ACanvas.Draw(ARect.Left, ARect.Top, ABitmap)
else ACanvas.StretchDraw(ARect, ABitmap);
CSSStats.BitmapMs := CSSStats.BitmapMs + CSSTimeMs - T;
end;
(*
Clip rect of canvas (counted in CSSStats). It is set always, also when it is the same: Cocoa can lose clip of bitmap
canvas after drawing. AEnable: clipping is enabled too (getter of Clipping says clip of canvas, setter enables it).
*)
procedure SetCanvasClip(ACanvas: TCanvas; const ARect: TRect; AEnable: Boolean = False);
var
T: Double;
begin
T := CSSTimeMs;
if AEnable then ACanvas.Clipping := True;
ACanvas.ClipRect := ARect;
Inc(CSSStats.Clips);
CSSStats.ClipMs := CSSStats.ClipMs + CSSTimeMs - T;
end;
function PointF(AX, AY: Single): TPointF; inline;
begin
Result.x := AX;
Result.y := AY;
end;
function RectF(ALeft, ATop, ARight, ABottom: Single): TRectF; inline;
begin
Result.Left := ALeft;
Result.Top := ATop;
Result.Right := ARight;
Result.Bottom := ABottom;
end;
const
CSS_COLOR_KEYWORDS : array[0..147] of record // https://developer.mozilla.org/en-US/docs/Web/CSS/named-color
k: String; // key
v: String; // value stored in CSS format is slow but better for future
end = (
(k:'aliceblue'; v:'#f0f8ff'),
(k:'antiquewhite'; v:'#faebd7'),
(k:'aqua'; v:'#00ffff'),
(k:'aquamarine'; v:'#7fffd4'),
(k:'azure'; v:'#f0ffff'),
(k:'beige'; v:'#f5f5dc'),
(k:'bisque'; v:'#ffe4c4'),
(k:'black'; v:'#000000'),
(k:'blanchedalmond'; v:'#ffebcd'),
(k:'blue'; v:'#0000ff'),
(k:'blueviolet'; v:'#8a2be2'),
(k:'brown'; v:'#a52a2a'),
(k:'burlywood'; v:'#deb887'),
(k:'cadetblue'; v:'#5f9ea0'),
(k:'chartreuse'; v:'#7fff00'),
(k:'chocolate'; v:'#d2691e'),
(k:'coral'; v:'#ff7f50'),
(k:'cornflowerblue'; v:'#6495ed'),
(k:'cornsilk'; v:'#fff8dc'),
(k:'crimson'; v:'#dc143c'),
(k:'cyan'; v:'#00ffff'),
(k:'darkblue'; v:'#00008b'),
(k:'darkcyan'; v:'#008b8b'),
(k:'darkgoldenrod'; v:'#b8860b'),
(k:'darkgray'; v:'#a9a9a9'),
(k:'darkgreen'; v:'#006400'),
(k:'darkgrey'; v:'#a9a9a9'),
(k:'darkkhaki'; v:'#bdb76b'),
(k:'darkmagenta'; v:'#8b008b'),
(k:'darkolivegreen'; v:'#556b2f'),
(k:'darkorange'; v:'#ff8c00'),
(k:'darkorchid'; v:'#9932cc'),
(k:'darkred'; v:'#8b0000'),
(k:'darksalmon'; v:'#e9967a'),
(k:'darkseagreen'; v:'#8fbc8f'),
(k:'darkslateblue'; v:'#483d8b'),
(k:'darkslategray'; v:'#2f4f4f'),
(k:'darkslategrey'; v:'#2f4f4f'),
(k:'darkturquoise'; v:'#00ced1'),
(k:'darkviolet'; v:'#9400d3'),
(k:'deeppink'; v:'#ff1493'),
(k:'deepskyblue'; v:'#00bfff'),
(k:'dimgray'; v:'#696969'),
(k:'dimgrey'; v:'#696969'),
(k:'dodgerblue'; v:'#1e90ff'),
(k:'firebrick'; v:'#b22222'),
(k:'floralwhite'; v:'#fffaf0'),
(k:'forestgreen'; v:'#228b22'),
(k:'fuchsia'; v:'#ff00ff'),
(k:'gainsboro'; v:'#dcdcdc'),
(k:'ghostwhite'; v:'#f8f8ff'),
(k:'gold'; v:'#ffd700'),
(k:'goldenrod'; v:'#daa520'),
(k:'gray'; v:'#808080'),
(k:'green'; v:'#008000'),
(k:'greenyellow'; v:'#adff2f'),
(k:'grey'; v:'#808080'),
(k:'honeydew'; v:'#f0fff0'),
(k:'hotpink'; v:'#ff69b4'),
(k:'indianred'; v:'#cd5c5c'),
(k:'indigo'; v:'#4b0082'),
(k:'ivory'; v:'#fffff0'),
(k:'khaki'; v:'#f0e68c'),
(k:'lavender'; v:'#e6e6fa'),
(k:'lavenderblush'; v:'#fff0f5'),
(k:'lawngreen'; v:'#7cfc00'),
(k:'lemonchiffon'; v:'#fffacd'),
(k:'lightblue'; v:'#add8e6'),
(k:'lightcoral'; v:'#f08080'),
(k:'lightcyan'; v:'#e0ffff'),
(k:'lightgoldenrodyellow'; v:'#fafad2'),
(k:'lightgray'; v:'#d3d3d3'),
(k:'lightgreen'; v:'#90ee90'),
(k:'lightgrey'; v:'#d3d3d3'),
(k:'lightpink'; v:'#ffb6c1'),
(k:'lightsalmon'; v:'#ffa07a'),
(k:'lightseagreen'; v:'#20b2aa'),
(k:'lightskyblue'; v:'#87cefa'),
(k:'lightslategray'; v:'#778899'),
(k:'lightslategrey'; v:'#778899'),
(k:'lightsteelblue'; v:'#b0c4de'),
(k:'lightyellow'; v:'#ffffe0'),
(k:'lime'; v:'#00ff00'),
(k:'limegreen'; v:'#32cd32'),
(k:'linen'; v:'#faf0e6'),
(k:'magenta'; v:'#ff00ff'),
(k:'maroon'; v:'#800000'),
(k:'mediumaquamarine'; v:'#66cdaa'),
(k:'mediumblue'; v:'#0000cd'),
(k:'mediumorchid'; v:'#ba55d3'),
(k:'mediumpurple'; v:'#9370db'),
(k:'mediumseagreen'; v:'#3cb371'),
(k:'mediumslateblue'; v:'#7b68ee'),
(k:'mediumspringgreen'; v:'#00fa9a'),
(k:'mediumturquoise'; v:'#48d1cc'),
(k:'mediumvioletred'; v:'#c71585'),
(k:'midnightblue'; v:'#191970'),
(k:'mintcream'; v:'#f5fffa'),
(k:'mistyrose'; v:'#ffe4e1'),
(k:'moccasin'; v:'#ffe4b5'),
(k:'navajowhite'; v:'#ffdead'),
(k:'navy'; v:'#000080'),
(k:'oldlace'; v:'#fdf5e6'),
(k:'olive'; v:'#808000'),
(k:'olivedrab'; v:'#6b8e23'),
(k:'orange'; v:'#ffa500'),
(k:'orangered'; v:'#ff4500'),
(k:'orchid'; v:'#da70d6'),
(k:'palegoldenrod'; v:'#eee8aa'),
(k:'palegreen'; v:'#98fb98'),
(k:'paleturquoise'; v:'#afeeee'),
(k:'palevioletred'; v:'#db7093'),
(k:'papayawhip'; v:'#ffefd5'),
(k:'peachpuff'; v:'#ffdab9'),
(k:'peru'; v:'#cd853f'),
(k:'pink'; v:'#ffc0cb'),
(k:'plum'; v:'#dda0dd'),
(k:'powderblue'; v:'#b0e0e6'),
(k:'purple'; v:'#800080'),
(k:'rebeccapurple'; v:'#663399'),
(k:'red'; v:'#ff0000'),
(k:'rosybrown'; v:'#bc8f8f'),
(k:'royalblue'; v:'#4169e1'),
(k:'saddlebrown'; v:'#8b4513'),
(k:'salmon'; v:'#fa8072'),
(k:'sandybrown'; v:'#f4a460'),
(k:'seagreen'; v:'#2e8b57'),
(k:'seashell'; v:'#fff5ee'),
(k:'sienna'; v:'#a0522d'),
(k:'silver'; v:'#c0c0c0'),
(k:'skyblue'; v:'#87ceeb'),
(k:'slateblue'; v:'#6a5acd'),
(k:'slategray'; v:'#708090'),
(k:'slategrey'; v:'#708090'),
(k:'snow'; v:'#fffafa'),
(k:'springgreen'; v:'#00ff7f'),
(k:'steelblue'; v:'#4682b4'),
(k:'tan'; v:'#d2b48c'),