Menu

[r3166]: / trunk / Src / UCSSUtils.pas  Maximize  Restore  History

Download this file

743 lines (649 with data), 28.3 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
{
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2006-2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Static method record that assists in generating CSS properties.
}
unit UCSSUtils;
interface
uses
// Delphi
Graphics;
type
/// <summary>Enumeration of values of CSS font-weight property.</summary>
TCSSFontWeight = (
cfwNormal, // absolute: normal (same as cfw400)
cfwBold, // absolute: bold (same as cfw700)
cfwBolder, // relative: heavier than current
cfwLighter, // relative: lighter than current
cfw100, cfw200, cfw300, cfw400, // ordered sequence of weights
cfw500, cfw600, cfw700, cfw800, cfw900 // .. each heavier than predecessor
);
type
/// <summary>Enumeration of values of CSS font-style property.</summary>
TCSSFontStyle = (
cfsNormal, // normal, upright, font
cfsItalic, // uses font labelled "italic" or failing that "oblique"
cfsOblique // uses font labelled "oblique"
);
type
/// <summary>Enumeration of values of CSS text-decoration property.</summary>
/// <remarks>Property takes a combination of one or more of these values.
/// </remarks>
TCSSTextDecoration = (
ctdNone, // no decoration
ctdUnderline, // text is underlines
ctdOverline, // text has overline
ctdLineThrough, // text is stirck through
ctdBlink // text is blinking
);
type
/// <summary>Set of values of CSS text-decoration property.</summary>
TCSSTextDecorations = set of TCSSTextDecoration;
type
/// <summary>Enumeration of generic font families recognised by CSS
/// font-family property.</summary>
TCSSFontGeneric = (
cfgDontCare, // no generic family specified
cfgSerif, // serif font (e.g. Times New Roman)
cfgSansSerif, // sans-serif font (e.g. Arial)
cfgCursive, // scripting font
cfgFantasy, // decorative font
cfgMonoSpace // fixed pitch font
);
type
/// <summary>Enumeration of border styles used in the CSS border property and
/// its derivatives.</summary>
TCSSBorderStyle = (
cbsNone, // no border
cbsDotted, // dotted line border
cbsDashed, // dashed line border
cbsSolid, // solid line border
cbsDouble, // double line border
cbsGroove, // 3D groove: colours based on color property
cbsRidge, // 3D ridge: colours based on color property
cbsInset, // 3D inset: colours based on color property
cbsOutset // 3D outset: colours based on color property
);
type
/// <summary>Enumeration of values of CSS text-align property.</summary>
TCSSTextAlign = (
ctaLeft, // text is left aligned: ragged right margin
ctaRight, // text is right aligned: ragged left margin
ctaCenter, // text is centred
ctaJustify // text is justified flush with right and left margins
);
type
/// <summary>Enumeration of values of CSS vertical-align property.</summary>
TCSSVerticalAlign = (
cvaBaseline, // align element baseline with parent's baseline
cvaSub, // subscript the element
cvaSuper, // superscript the element
cvaTop, // align top of element with tallest element on line
cvaTextTop, // align top of element with top of parent's font
cvaMiddle, // align vertical middle with baseline + 1/2 parent height
cvaBottom, // align bottom of element with lowest element on line
cvaTextBottom // align bottom of element with bottom of parent's font
);
type
/// <summary>Enumeration of sides that apply to various CSS properties that
/// apply to an element's bounding box.</summary>
/// <remarks>Used to specify a variant of a property, e.g. padding-top.
/// </remarks>
TCSSSide = (
cssAll, // refers to all sides of an element
cssTop, // top of element
cssLeft, // left of element
cssBottom, // bottom of element
cssRight // right of element
);
type
/// <summary>Enumeration of display styles used in CSS display property.
/// </summary>
TCSSDisplayStyle = (
cdsNone, // element not displayed
cdsBlock, // element displayed as a block
cdsInline // element displayed inline
);
type
/// <summary>Enumeration of units that apply to measurements of length used
/// in CSS.</summary>
TCSSLengthUnit = (
cluAuto, // "auto"
cluPixels, // pixels
cluEm, // "em" values
cluPercent // percentage values
);
type
/// <summary>Enumeration of CSS overflow property values.</summary>
TCSSOverflowValue = (
covVisible, // overflow is not clipped and overflows
covHidden, // overflow is clipped, rest of the content invisible
covScroll, // overflow is clipped, scroll-bar is added
covAuto, // if overflow is clipped a scroll-bar is added
covInherit // value inherited from the parent element
);
type
/// <summary>
/// Container for static methods that return CSS properties as text.
/// </summary>
TCSS = record
strict private
/// <summary>Converts a Delphi TColor to a CSS compatible colour string.
/// </summary>
/// <param name="Color">TColor [in] Colour to map to CSS colour.</param>
/// <returns>string. CSS code for Color.</returns>
/// <remarks>Any system colors (like clBtnFace) are mapped to the actual
/// colour according to the current Windows settings.</remarks>
class function ColorToCSS(const Color: TColor): string; static;
/// <summary>Gets the text representing the given unit of length.</summary>
/// <param name="LU">TCSSLengthUnit [in] Required length unit.</param>
/// <returns>string. Required length unit as text.</returns>
class function LengthUnit(const LU: TCSSLengthUnit): string; static;
/// <summary>Builds a space separated list of lengths using specified
/// units.</summary>
/// <param name="List">array of Integer [in] List of lengths.</param>
/// <param name="LU">TCSSLengthUnit [in] Specifies length unit to apply tp
/// each length.</param>
/// <returns>string. Required spaced separated list.</returns>
class function LengthList(const List: array of Integer;
const LU: TCSSLengthUnit = cluPixels): string; static;
/// <summary>Creates a CSS "margin" property.</summary>
/// <param name="Margin">array of Integer [in] Array of margin widths. Must
/// contain either 1, 2 or 4 values.</param>
/// <returns>string. Required CSS property.</returns>
class function MarginProp(const Margin: array of Integer): string;
overload; static;
/// <summary>Creates a CSS "padding" property.</summary>
/// <param name="Padding">array of Integer [in] Array of padding widths.
/// Must contain either 1, 2 or 4 values.</param>
/// <returns>string. Required CSS property.</returns>
class function PaddingProp(const Padding: array of Integer): string;
overload; static;
public
/// <summary>Creates a CSS "color" property.</summary>
/// <param name="Color">TColor [in] Desired foreground colour.</param>
/// <returns>string. Required CSS property.</returns>
class function ColorProp(const Color: TColor): string; static;
/// <summary>Creates a CSS "background color" property.</summary>
/// <param name="Color">TColor [in] Desired background colour.</param>
/// <returns>string. Required CSS property.</returns>
class function BackgroundColorProp(const Color: TColor): string; static;
/// <summary>Creates a string of CSS properties that fully describe a font.
/// </summary>
/// <param name="Font">TFont [in] Font for which properties are required.
/// </param>
/// <returns>string. Space separated list of required CSS properties.
/// </returns>
class function FontProps(const Font: TFont): string; static;
/// <summary>Creates a CSS "font-family" property for a named font and a
/// generic font type.</summary>
/// <param name="FontName">string [in] Name of required font.</param>
/// <param name="Generic">TCSSFontGeneric [in] Generic font family to use
/// if font not available. Ignored if Generic=cfgDontCare.</param>
/// <returns>string. Required CSS property.</returns>
class function FontFamilyProp(const FontName: string;
const Generic: TCSSFontGeneric): string; static;
/// <summary>Creates a CSS "font-size" property for a font sized in points.
/// </summary>
/// <param name="PtSize">Cardinal [in] Font's point size.</param>
/// <returns>string. Required CSS property.</returns>
class function FontSizeProp(const PtSize: Cardinal): string; static;
/// <summary>Creates a CSS "font-style" property.</summary>
/// <param name="FS">TCSSFontStyle [in] Required font style.</param>
/// <returns>string. Required CSS property.</returns>
class function FontStyleProp(const FS: TCSSFontStyle): string; overload;
static;
/// <summary>Creates a CSS "font-style" property.</summary>
/// <param name="FS">TFontStyles [in] Set of font styles. If set contains
/// fsItalic the "italic" style it is used, otherwise "normal" style is
/// used.</param>
/// <returns>string. Required CSS property.</returns>
class function FontStyleProp(const FS: TFontStyles): string; overload;
static;
/// <summary>Creates a CSS "font-weight" property.</summary>
/// <param name="FW">TCSSFontWeight [in] Required font weight.</param>
/// <returns>string. Required CSS property.</returns>
class function FontWeightProp(const FW: TCSSFontWeight): string; overload;
static;
/// <summary>Creates a CSS "font-weight" property.</summary>
/// <param name="FS">TFontStyles [in] Set of font styles. If set contains
/// fsBold then "bold" weight is used, otherwise "normal" weight is used.
/// </param>
/// <returns>string. Required CSS property.</returns>
class function FontWeightProp(const FS: TFontStyles): string; overload;
static;
{Creates a CSS "font-weight" property from set of styles.
@param FS [in] Set of font styles.
@return Required property.
}
/// <summary>Creates CSS "border" or "border-xxx" property (where "xxx"
/// denotes a side).</summary>
/// <param name="Side">TCSSSide [in] Specifies side(s) of element where
/// border is to be drawn.</param>
/// <param name="WidthPx">Cardinal [in] Width of border in pixels. Value of
/// 0 hides border.</param>
/// <param name="Style">TCSSBorderStyle [in] Border style. Value of cbsNone
/// hides border.</param>
/// <param name="Color">TColor [in] Border colour. Value of clNone hides
/// border.</param>
/// <returns>string. Required CSS property.</returns>
class function BorderProp(const Side: TCSSSide; const WidthPx: Cardinal;
const Style: TCSSBorderStyle; const Color: TColor): string; static;
/// <summary>Creates CSS "border" or "border-xxx" property (where "xxx"
/// denotes a side) which hides any existing border.</summary>
/// <param name="Side">TCSSSide [in] Specifies side(s) of element where
/// border is to be hidden.</param>
/// <returns>string. Required CSS property.</returns>
class function HideBorderProp(const Side: TCSSSide): string; static;
/// <summary>Creates CSS "margin" property with same width on all edges.
/// </summary>
/// <param name="Margin">Integer [in] Margin width in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function MarginProp(const Margin: Integer): string; overload; static;
/// <summary>Creates CSS "margin" property with potentially different
/// margin widths on each side.</summary>
/// <param name="Top">Integer [in] Top margin in pixels.</param>
/// <param name="Right">Integer [in] Right margin in pixels.</param>
/// <param name="Bottom">Integer [in] Bottom margin in pixels.</param>
/// <param name="Left">Integer [in] Left margin in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function MarginProp(const Top, Right, Bottom, Left: Integer): string;
overload; static;
/// <summary>Creates CSS "margin" or "margin-xxx" property (where "xxx" is
/// a side).</summary>
/// <param name="Side">TCSSSide [in] Specifies side(s) of element whose
/// margin is to be set.</param>
/// <param name="Margin">Integer [in] Width of margin in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function MarginProp(const Side: TCSSSide; const Margin: Integer):
string; overload; static;
/// <summary>Creates CSS "padding" property with same width on all sides.
/// </summary>
/// <param name="Padding">Integer [in] Padding width in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function PaddingProp(const Padding: Integer): string; overload;
static;
/// <summary>Creates CSS "padding" property with potentially different
/// padding widths on each side.</summary>
/// <param name="Top">Integer [in] Top margin in pixels.</param>
/// <param name="Right">Integer [in] Right margin in pixels.</param>
/// <param name="Bottom">Integer [in] Bottom margin in pixels.</param>
/// <param name="Left">Integer [in] Left margin in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function PaddingProp(const Top, Right, Bottom, Left: Integer):
string; overload; static;
/// <summary>Creates CSS "padding" or "padding-xxx" property (where "xxx"
/// is a side).</summary>
/// <param name="Side">TCSSSide [in] Specifies side(s) of element whose
/// padding is to be set.</param>
/// <param name="Padding">Integer [in] Width of padding in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function PaddingProp(const Side: TCSSSide; const Padding: Integer):
string; overload; static;
/// <summary>Creates a CSS "text-decoration" property.</summary>
/// <returns>string. Required CSS property.</returns>
/// <param name="Decorations">TCSSTextDecorations [in] Set of decorations
/// to include in property. Can either be [ctdNone], empty set, or any
/// combination of other ctdXXX values. Empty set is equivalent to
/// [ctdNone].</param>
/// <returns>string. Required CSS property.</returns>
class function TextDecorationProp(Decorations: TCSSTextDecorations): string;
overload; static;
/// <summary>Creates a CSS "text-decoration" property.</summary>
/// <returns>string. Required CSS property.</returns>
/// <param name="FS">TFontStyles [in] Set of font styles. If set contains
/// fsStrikeOut or fsUnderline appropriate "line-through" and/or
/// "underline" decoration is used, otherwise "none" is used.</param>
/// <returns>string. Required CSS property.</returns>
class function TextDecorationProp(const FS: TFontStyles): string; overload;
static;
/// <summary>Creates a CSS "text-align" property.</summary>
/// <param name="TA">TCSSTextAlign [in] Required text alignment.</param>
/// <returns>string. Required CSS property.</returns>
class function TextAlignProp(const TA: TCSSTextAlign): string; static;
/// <summary>Creates a CSS "vertical-align" property.</summary>
/// <param name="VA">TCSSVerticalAlign [in] Required vertical alignment.
/// </param>
/// <returns>string. Required CSS property.</returns>
class function VerticalAlignProp(const VA: TCSSVerticalAlign): string;
static;
/// <summary>Creates a CSS "max-height" property.</summary>
/// <param name="HeightPx">Integer [in] Required height in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function MaxHeightProp(const HeightPx: Integer): string; static;
/// <summary>Creates a CSS "height" property.</summary>
/// <param name="HeightPx">Integer [in] Required height in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function HeightProp(const HeightPx: Integer): string; static;
/// <summary>Creates a CSS "width" property.</summary>
/// <param name="WidthPx">Integer [in] Required width in pixels.</param>
/// <returns>string. Required CSS property.</returns>
class function WidthProp(const WidthPx: Integer): string; overload; static;
/// <summary>Creates a CSS "width" property.</summary>
/// <param name="LengthUnit">TCSSLengthUnit [in] Units used to specify
/// width. If cltAuto then Width is ignored.</param>
/// <param name="Width">Integer [in] Required width in specified units.
/// </param>
/// <returns>string. Required CSS property.</returns>
class function WidthProp(const LengthUnit: TCSSLengthUnit;
const Width: Integer): string; overload; static;
/// <summary>Creates a CSS "display" property.</summary>
/// <param name="Style">TCSSDisplayStyle [in] Required display style.
/// </param>
/// <returns>string. Required CSS property.</returns>
class function DisplayProp(const Style: TCSSDisplayStyle): string; static;
/// <summary>Creates a CSS "display" property that causes its element to be
/// either hidden or displayed as a "block".</summary>
/// <param name="Show">Boolean [in] Flag indicating if block is to be
/// displayed (True) or hidden (False).</param>
/// <returns>string. Required CSS property.</returns>
class function BlockDisplayProp(const Show: Boolean): string; static;
/// <summary>Creates a CSS "overflow" property.</summary>
/// <param name="Value">TCSSOverflowValue [in] Required overflow handling.
/// </param>
/// <returns>string. Required CSS property.</returns>
class function OverflowProp(const Value: TCSSOverflowValue): string; static;
end;
implementation
uses
// Delphi
SysUtils, Windows,
// Project
UIStringList, UStrUtils;
{ TCSS }
class function TCSS.BackgroundColorProp(const Color: TColor): string;
begin
Result := Format('background-color: %s;', [ColorToCSS(Color)]);
end;
class function TCSS.BlockDisplayProp(const Show: Boolean): string;
const
// Map of flag onto required display style
BlockDisplayStyles: array[Boolean] of TCSSDisplayStyle = (cdsNone, cdsBlock);
begin
Result := DisplayProp(BlockDisplayStyles[Show]);
end;
class function TCSS.BorderProp(const Side: TCSSSide; const WidthPx: Cardinal;
const Style: TCSSBorderStyle; const Color: TColor): string;
const
// Map of element sides to associated border properties
BorderSides: array[TCSSSide] of string = (
'border', 'border-top', 'border-left', 'border-bottom', 'border-right'
);
// Map of border styles to property values
BorderStyles: array[TCSSBorderStyle] of string = (
'none', 'dotted', 'dashed', 'solid', 'double',
'groove', 'ridge', 'inset', 'outset'
);
begin
if (WidthPx > 0) and (Style <> cbsNone) and (Color <> clNone) then
// Displaying border
Result := Format(
'%s: %s %s %s;',
[BorderSides[Side], ColorToCSS(Color), BorderStyles[Style],
LengthList([WidthPx])]
)
else
// Hiding border
Result := Format('%s: %s;', [BorderSides[Side], LengthList([Cardinal(0)])]);
end;
class function TCSS.ColorProp(const Color: TColor): string;
begin
Result := Format('color: %s;', [ColorToCSS(Color)]);
end;
class function TCSS.ColorToCSS(const Color: TColor): string;
var
ColorRGB: Integer; // RGB code for the colour
begin
ColorRGB := ColorToRGB(Color); // this translates system colours to actual
Result := Format(
'#%0.2X%0.2X%0.2X',
[GetRValue(ColorRGB), GetGValue(ColorRGB), GetBValue(ColorRGB)]
);
end;
class function TCSS.DisplayProp(const Style: TCSSDisplayStyle): string;
const
// Map of display ids to associated property values
DisplayStyles: array[TCSSDisplayStyle] of string = (
'none', 'block', 'inline'
);
begin
Result := Format('display: %s;', [DisplayStyles[Style]]);
end;
class function TCSS.FontFamilyProp(const FontName: string;
const Generic: TCSSFontGeneric): string;
const
// Map of generic font families to font names
Generics: array[TCSSFontGeneric] of string = (
'', 'serif', 'sans-serif', 'cursive', 'fantasy', 'monospace'
);
var
Names: IStringList; // used to build list of font names
begin
Names := TIStringList.Create;
Names.Add(StrQuoteSpaced(FontName));
if Generic <> cfgDontCare then
Names.Add(StrQuoteSpaced(Generics[Generic]));
// Generate property
Result := Format('font-family: %s;', [Names.GetText(', ', False)]);
end;
class function TCSS.FontProps(const Font: TFont): string;
var
PropList: IStringList; // list of font properties
begin
PropList := TIStringList.Create(
[
FontFamilyProp(Font.Name, cfgDontCare),
FontSizeProp(Font.Size),
FontWeightProp(Font.Style),
FontStyleProp(Font.Style),
TCSS.TextDecorationProp(Font.Style),
ColorProp(Font.Color)
]
);
Result := PropList.GetText(' ', False);
end;
class function TCSS.FontSizeProp(const PtSize: Cardinal): string;
begin
Result := Format('font-size: %dpt;', [PtSize]);
end;
class function TCSS.FontStyleProp(const FS: TCSSFontStyle): string;
const
// Maps font style to property value
FontStyles: array[TCSSFontStyle] of string = ('normal', 'italic', 'oblique');
begin
Result := Format('font-style: %s;', [FontStyles[FS]]);
end;
class function TCSS.FontStyleProp(const FS: TFontStyles): string;
begin
if fsItalic in FS then
Result := FontStyleProp(cfsItalic)
else
Result := FontStyleProp(cfsNormal);
end;
class function TCSS.FontWeightProp(const FW: TCSSFontWeight): string;
const
// Map of font weights to property values
FontWeights: array[TCSSFontWeight] of string = (
'normal', 'bold', 'bolder', 'lighter',
'100', '200', '300', '400', '500', '600', '700', '800', '900'
);
begin
Result := Format('font-weight: %s;', [FontWeights[FW]]);
end;
class function TCSS.FontWeightProp(const FS: TFontStyles): string;
begin
if fsBold in FS then
Result := FontWeightProp(cfwBold)
else
Result := FontWeightProp(cfwNormal);
end;
class function TCSS.HeightProp(const HeightPx: Integer): string;
begin
Result := Format('height: %s;', [LengthList([HeightPx])]);
end;
class function TCSS.HideBorderProp(const Side: TCSSSide): string;
begin
Result := BorderProp(Side, 0, cbsNone, clNone);
end;
class function TCSS.LengthList(const List: array of Integer;
const LU: TCSSLengthUnit): string;
var
Idx: Integer; // loops thru list of values
ALength: Integer; // a length from list
begin
Assert((LU <> cluAuto) or (Length(List) = 1),
'TCSS.LengthList: List size may only be 1 when length type is cltAuto');
if LU = cluAuto then
Result := LengthUnit(LU)
else
begin
Result := '';
for Idx := Low(List) to High(List) do
begin
ALength := List[Idx];
if Result <> '' then
Result := Result + ' ';
Result := Result + IntToStr(ALength);
if ALength <> 0 then
Result := Result + LengthUnit(LU); // only add unit if length not 0
end;
end;
end;
class function TCSS.LengthUnit(const LU: TCSSLengthUnit): string;
const
Units: array[TCSSLengthUnit] of string = (
'auto', 'px', 'em', '%'
);
begin
Result := Units[LU];
end;
class function TCSS.MarginProp(const Margin: array of Integer): string;
begin
Assert(Length(Margin) in [1,2,4],
'TCSS.MarginProp: Invalid margin parameters');
Result := 'margin: ' + LengthList(Margin) + ';';
end;
class function TCSS.MarginProp(const Top, Right, Bottom, Left: Integer): string;
begin
Result := MarginProp([Top, Right, Bottom, Left]);
end;
class function TCSS.MarginProp(const Margin: Integer): string;
begin
Result := MarginProp([Margin]);
end;
class function TCSS.MarginProp(const Side: TCSSSide; const Margin: Integer):
string;
const
// Map of element sides to associated margin properties
MarginSides: array[TCSSSide] of string = (
'margin', 'margin-top', 'margin-left', 'margin-bottom', 'margin-right'
);
begin
Result := Format('%s: %s;', [MarginSides[Side], LengthList([Margin])]);
end;
class function TCSS.MaxHeightProp(const HeightPx: Integer): string;
begin
Result := Format('max-height: %s;', [LengthList([HeightPx])]);
end;
class function TCSS.OverflowProp(const Value: TCSSOverflowValue): string;
const
Values: array[TCSSOverflowValue] of string = (
'visible', 'hidden', 'scroll', 'auto', 'inherit'
);
begin
Result := Format('overflow: %s;', [Values[Value]]);
end;
class function TCSS.PaddingProp(const Padding: array of Integer): string;
begin
Assert(Length(Padding) in [1,2,4],
'TCSS.PaddingProp: Invalid padding parameters');
Result := 'padding: ' + LengthList(Padding) + ';';
end;
class function TCSS.PaddingProp(const Top, Right, Bottom, Left: Integer):
string;
begin
Result := PaddingProp([Top, Right, Bottom, Left]);
end;
class function TCSS.PaddingProp(const Padding: Integer): string;
begin
Result := PaddingProp([Padding]);
end;
class function TCSS.PaddingProp(const Side: TCSSSide;
const Padding: Integer): string;
const
// Map of element sides to associated padding properties
PaddingSides: array[TCSSSide] of string = (
'padding', 'padding-top', 'padding-left', 'padding-bottom', 'padding-right'
);
begin
Result := Format('%s: %s;', [PaddingSides[Side], LengthList([Padding])]);
end;
class function TCSS.TextAlignProp(const TA: TCSSTextAlign): string;
const
// Map of text alignment to associated property values
TextAligns: array[TCSSTextAlign] of string = (
'left', 'right', 'center', 'justify'
);
begin
Result := Format('text-align: %s;', [TextAligns[TA]]);
end;
class function TCSS.TextDecorationProp(Decorations: TCSSTextDecorations):
string;
const
// Map of text decoration ids to associated property values
TextDecorations: array[TCSSTextDecoration] of string =
('none', 'underline', 'overline', 'line-through', 'blink');
var
D: TCSSTextDecoration; // loops thru all decorations
List: IStringList; // list of decoration values
begin
Assert((Decorations = []) or (Decorations = [ctdNone])
or (Decorations * [ctdNone] = []),
'TCSS.TextDecorationProp: Invalid combination of values'
);
if Decorations = [] then
Decorations := [ctdNone];
List := TIStringList.Create;
for D := Low(TCSSTextDecoration) to High(TCSSTextDecoration) do
if D in Decorations then
List.Add(TextDecorations[D]);
Result := Format('text-decoration: %s;', [List.GetText(' ', False)]);
end;
class function TCSS.TextDecorationProp(const FS: TFontStyles): string;
var
Decorations: TCSSTextDecorations; // required text decorations
begin
Decorations := [];
if fsUnderline in FS then
Include(Decorations, ctdUnderline);
if fsStrikeOut in FS then
Include(Decorations, ctdLineThrough);
Result := TextDecorationProp(Decorations);
end;
class function TCSS.VerticalAlignProp(const VA: TCSSVerticalAlign): string;
const
// Map of vertical alignement ids to associated property values
VerticalAligns: array[TCSSVerticalAlign] of string = (
'baseline', 'sub', 'super', 'top', 'text-top', 'middle', 'bottom',
'text-bottom'
);
begin
Result := Format('vertical-align: %s;', [VerticalAligns[VA]]);
end;
class function TCSS.WidthProp(const WidthPx: Integer): string;
begin
Result := WidthProp(cluPixels, WidthPx);
end;
class function TCSS.WidthProp(const LengthUnit: TCSSLengthUnit;
const Width: Integer): string;
begin
Result := Format('width: %s;', [LengthList([Width], LengthUnit)]);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.