@@ -200,28 +200,38 @@ TCSS = record
200
200
// / <returns>string. Required length unit as text.</returns>
201
201
class function LengthUnit (const LU: TCSSLengthUnit): string; static;
202
202
203
- // / <summary>Builds a space separated list of lengths using specified
204
- // / units.</summary>
205
- // / <param name="List">array of Integer [in] List of lengths.</param>
206
- // / <param name="LU">TCSSLengthUnit [in] Specifies length unit to apply tp
207
- // / each length.</param>
208
- // / <returns>string. Required spaced separated list.</returns>
209
- class function LengthList (const List: array of Integer;
203
+ // / <summary>Builds a space separated list of lengths using the specified
204
+ // / unit.</summary>
205
+ // / <param name="List"><c>array of Single</c> [in] List of lengths.</param>
206
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Specifies length unit to
207
+ // / apply to each length.</param>
208
+ // / <returns><c>string</c>. Required spaced separated list.</returns>
209
+ // / <remarks>Note that lengths are rounded to a maximum of 2 decimal
210
+ // / places.</remarks>
211
+ class function LengthList (const List: array of Single;
210
212
const LU: TCSSLengthUnit = cluPixels): string; static;
211
213
212
214
// / <summary>Creates a CSS "margin" property.</summary>
213
- // / <param name="Margin">array of Integer [in] Array of margin widths. Must
214
- // / contain either 1, 2 or 4 values.</param>
215
- // / <returns>string. Required CSS property.</returns>
216
- class function MarginProp (const Margin: array of Integer): string;
217
- overload; static;
215
+ // / <param name="Margin"><c>array of Single</c> [in] Array of margin
216
+ // / widths. Must contain either 1, 2 or 4 values.</param>
217
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
218
+ // / for each margin width. Defaults to <c>cluPixels</c>.</param>
219
+ // / <returns><c>string</c>. Required CSS property.</returns>
220
+ // / <remarks>Note that margin values are rounded to a maximum of 2 decimal
221
+ // / places.</remarks>
222
+ class function MarginProp (const Margin: array of Single;
223
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
218
224
219
225
// / <summary>Creates a CSS "padding" property.</summary>
220
- // / <param name="Padding">array of Integer [in] Array of padding widths.
221
- // / Must contain either 1, 2 or 4 values.</param>
222
- // / <returns>string. Required CSS property.</returns>
223
- class function PaddingProp (const Padding: array of Integer): string;
224
- overload; static;
226
+ // / <param name="Padding"><c>array of Single</c> [in] Array of padding
227
+ // / widths. Must contain either 1, 2 or 4 values.</param>
228
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
229
+ // / for each padding width. Defaults to <c>cluPixels</c>.</param>
230
+ // / <returns><c>string</c>. Required CSS property.</returns>
231
+ // / <remarks>Note that padding values are rounded to a maximum of 2 decimal
232
+ // / places.</remarks>
233
+ class function PaddingProp (const Padding: array of Single;
234
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
225
235
226
236
public
227
237
// / <summary>Creates a CSS "color" property.</summary>
@@ -312,54 +322,77 @@ TCSS = record
312
322
313
323
// / <summary>Creates CSS "margin" property with same width on all edges.
314
324
// / </summary>
315
- // / <param name="Margin">Integer [in] Margin width in pixels.</param>
316
- // / <returns>string. Required CSS property.</returns>
317
- class function MarginProp (const Margin: Integer): string; overload; static;
325
+ // / <param name="Margin"><c>Single</c> [in] Margin width.</param>
326
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
327
+ // / for the margin width. Defaults to <c>cluPixels</c>.</param>
328
+ // / <returns><c>string</c>. Required CSS property.</returns>
329
+ // / <remarks>Note that the margin value is rounded to a maximum of 2
330
+ // / decimal places.</remarks>
331
+ class function MarginProp (const Margin: Single;
332
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
318
333
319
334
// / <summary>Creates CSS "margin" property with potentially different
320
335
// / margin widths on each side.</summary>
321
- // / <param name="Top">Integer [in] Top margin in pixels.</param>
322
- // / <param name="Right">Integer [in] Right margin in pixels.</param>
323
- // / <param name="Bottom">Integer [in] Bottom margin in pixels.</param>
324
- // / <param name="Left">Integer [in] Left margin in pixels.</param>
325
- // / <returns>string. Required CSS property.</returns>
326
- class function MarginProp (const Top, Right, Bottom, Left: Integer): string;
327
- overload; static;
336
+ // / <param name="Top"><c>Single</c> [in] Top margin.</param>
337
+ // / <param name="Right"><c>Single</c> [in] Right margin.</param>
338
+ // / <param name="Bottom"><c>Single</c> [in] Bottom margin.</param>
339
+ // / <param name="Left"><c>Single</c> [in] Left margin.</param>
340
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
341
+ // / for each margin width. Defaults to <c>cluPixels</c>.</param>
342
+ // / <returns><c>string</c>. Required CSS property.</returns>
343
+ // / <remarks>Note that margin values are rounded to a maximum of 2 decimal
344
+ // / places.</remarks>
345
+ class function MarginProp (const Top, Right, Bottom, Left: Single;
346
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
328
347
329
348
// / <summary>Creates CSS "margin" or "margin-xxx" property (where "xxx" is
330
349
// / a side).</summary>
331
- // / <param name="Side">TCSSSide [in] Specifies side(s) of element whose
332
- // / margin is to be set.</param>
333
- // / <param name="Margin">Integer [in] Width of margin in pixels.</param>
334
- // / <returns>string. Required CSS property.</returns>
335
- class function MarginProp (const Side: TCSSSide; const Margin: Integer):
336
- string; overload; static;
350
+ // / <param name="Side"><c>TCSSSide</c> [in] Specifies the side(s) of the
351
+ // / element whose margin is to be set.</param>
352
+ // / <param name="Margin"><c>Single</c> [in] Width of margin in pixels.</param>
353
+ // / <returns><c>string</c>. Required CSS property.</returns>
354
+ // / <remarks>Note that the margin is rounded to a maximum of 2 decimal
355
+ // / places.</remarks>
356
+ class function MarginProp (const Side: TCSSSide; const Margin: Single;
357
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
337
358
338
359
// / <summary>Creates CSS "padding" property with same width on all sides.
339
360
// / </summary>
340
- // / <param name="Padding">Integer [in] Padding width in pixels.</param>
341
- // / <returns>string. Required CSS property.</returns>
342
- class function PaddingProp (const Padding: Integer): string; overload;
343
- static;
361
+ // / <param name="Padding"><c>Single</c> [in] Padding width.</param>
362
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
363
+ // / for the padding width. Defaults to <c>cluPixels</c>.</param>
364
+ // / <returns><c>string</c>. Required CSS property.</returns>
365
+ // / <remarks>Note that the padding value is rounded to a maximum of 2
366
+ // / decimal places.</remarks>
367
+ class function PaddingProp (const Padding: Single;
368
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
344
369
345
370
// / <summary>Creates CSS "padding" property with potentially different
346
371
// / padding widths on each side.</summary>
347
- // / <param name="Top">Integer [in] Top margin in pixels.</param>
348
- // / <param name="Right">Integer [in] Right margin in pixels.</param>
349
- // / <param name="Bottom">Integer [in] Bottom margin in pixels.</param>
350
- // / <param name="Left">Integer [in] Left margin in pixels.</param>
351
- // / <returns>string. Required CSS property.</returns>
352
- class function PaddingProp (const Top, Right, Bottom, Left: Integer):
353
- string; overload; static;
372
+ // / <param name="Top"><c>Single</c> [in] Top margin.</param>
373
+ // / <param name="Right"><c>Single</c> [in] Right margin.</param>
374
+ // / <param name="Bottom"><c>Single</c> [in] Bottom margin.</param>
375
+ // / <param name="Left"><c>Single</c> [in] Left margin.</param>
376
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
377
+ // / for each padding width. Defaults to <c>cluPixels</c>.</param>
378
+ // / <returns><c>string</c>. Required CSS property.</returns>
379
+ // / <remarks>Note that padding values are rounded to a maximum of 2 decimal
380
+ // / places.</remarks>
381
+ class function PaddingProp (const Top, Right, Bottom, Left: Single;
382
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
354
383
355
384
// / <summary>Creates CSS "padding" or "padding-xxx" property (where "xxx"
356
385
// / is a side).</summary>
357
- // / <param name="Side">TCSSSide [in] Specifies side(s) of element whose
358
- // / padding is to be set.</param>
359
- // / <param name="Padding">Integer [in] Width of padding in pixels.</param>
360
- // / <returns>string. Required CSS property.</returns>
361
- class function PaddingProp (const Side: TCSSSide; const Padding: Integer):
362
- string; overload; static;
386
+ // / <param name="Side"><c>TCSSSide</c> [in] Specifies side(s) of element
387
+ // / whose padding is to be set.</param>
388
+ // / <param name="Padding"><c>Single</c> [in] Width of padding.</param>
389
+ // / <param name="LU"><c>TCSSLengthUnit</c> [in] Optional length unit to use
390
+ // / for the padding width. Defaults to <c>cluPixels</c>.</param>
391
+ // / <returns><c>string</c>. Required CSS property.</returns>
392
+ // / <remarks>Note that the padding value is rounded to a maximum of 2
393
+ // / decimal places.</remarks>
394
+ class function PaddingProp (const Side: TCSSSide; const Padding: Single;
395
+ const LU: TCSSLengthUnit = cluPixels): string; overload; static;
363
396
364
397
// / <summary>Creates a CSS "text-decoration" property.</summary>
365
398
// / <returns>string. Required CSS property.</returns>
@@ -477,7 +510,7 @@ implementation
477
510
478
511
uses
479
512
// Delphi
480
- SysUtils, Windows,
513
+ SysUtils, Windows, Math,
481
514
// Project
482
515
UIStringList, UStrUtils;
483
516
@@ -519,7 +552,7 @@ class function TCSS.BorderProp(const Side: TCSSSide; const WidthPx: Cardinal;
519
552
)
520
553
else
521
554
// Hiding border
522
- Result := Format(' %s: %s;' , [BorderSides[Side], LengthList([Cardinal( 0 ) ])]);
555
+ Result := Format(' %s: %s;' , [BorderSides[Side], LengthList([0 ])]);
523
556
end ;
524
557
525
558
class function TCSS.ColorProp (const Color: TColor): string;
@@ -641,11 +674,32 @@ class function TCSS.InlineDisplayProp(const Show: Boolean): string;
641
674
Result := DisplayProp(BlockDisplayStyles[Show]);
642
675
end ;
643
676
644
- class function TCSS.LengthList (const List: array of Integer ;
677
+ class function TCSS.LengthList (const List: array of Single ;
645
678
const LU: TCSSLengthUnit): string;
679
+
680
+ function FmtLength (const L: Single): string;
681
+ var
682
+ NumX100: Int64;
683
+ WholePart, DecPart: Int64;
684
+ begin
685
+ Assert(not (L < 0 ), ' TCSS.LengthList: Length < 0' ); // avoiding using >=
686
+ NumX100 := Round(Abs(L) * 100 );
687
+ WholePart := NumX100 div 100 ;
688
+ DecPart := NumX100 mod 100 ;
689
+ Result := IntToStr(WholePart);
690
+ if DecPart <> 0 then
691
+ begin
692
+ Result := Result + ' .' ; // TODO: check CSS spec re localisation of '.'
693
+ if DecPart mod 10 = 0 then
694
+ Result := Result + IntToStr(DecPart div 10 )
695
+ else
696
+ Result := Result + IntToStr(DecPart);
697
+ end ;
698
+ end ;
699
+
646
700
var
647
701
Idx: Integer; // loops thru list of values
648
- ALength: Integer; // a length from list
702
+ ALength: Single; // a length from list
649
703
begin
650
704
Assert((LU <> cluAuto) or (Length(List) = 1 ),
651
705
' TCSS.LengthList: List size may only be 1 when length type is cltAuto' );
@@ -659,7 +713,7 @@ class function TCSS.LengthList(const List: array of Integer;
659
713
ALength := List[Idx];
660
714
if Result <> ' ' then
661
715
Result := Result + ' ' ;
662
- Result := Result + IntToStr (ALength);
716
+ Result := Result + FmtLength (ALength);
663
717
if ALength <> 0 then
664
718
Result := Result + LengthUnit(LU); // only add unit if length not 0
665
719
end ;
@@ -701,32 +755,35 @@ class function TCSS.ListStyleTypeProp(const Value: TCSSListStyleType): string;
701
755
Result := ' list-style-type: ' + Types[Value ] + ' ;' ;
702
756
end ;
703
757
704
- class function TCSS.MarginProp (const Margin: array of Integer): string;
758
+ class function TCSS.MarginProp (const Margin: array of Single;
759
+ const LU: TCSSLengthUnit): string;
705
760
begin
706
761
Assert(Length(Margin) in [1 ,2 ,4 ],
707
762
' TCSS.MarginProp: Invalid margin parameters' );
708
- Result := ' margin: ' + LengthList(Margin) + ' ;' ;
763
+ Result := ' margin: ' + LengthList(Margin, LU ) + ' ;' ;
709
764
end ;
710
765
711
- class function TCSS.MarginProp (const Top, Right, Bottom, Left: Integer): string;
766
+ class function TCSS.MarginProp (const Top, Right, Bottom, Left: Single;
767
+ const LU: TCSSLengthUnit): string;
712
768
begin
713
- Result := MarginProp([Top, Right, Bottom, Left]);
769
+ Result := MarginProp([Top, Right, Bottom, Left], LU );
714
770
end ;
715
771
716
- class function TCSS.MarginProp (const Margin: Integer): string;
772
+ class function TCSS.MarginProp (const Margin: Single; const LU: TCSSLengthUnit):
773
+ string;
717
774
begin
718
- Result := MarginProp([Margin]);
775
+ Result := MarginProp([Margin], LU );
719
776
end ;
720
777
721
- class function TCSS.MarginProp (const Side: TCSSSide; const Margin: Integer):
722
- string;
778
+ class function TCSS.MarginProp (const Side: TCSSSide; const Margin: Single;
779
+ const LU: TCSSLengthUnit): string;
723
780
const
724
781
// Map of element sides to associated margin properties
725
782
MarginSides: array [TCSSSide] of string = (
726
783
' margin' , ' margin-top' , ' margin-left' , ' margin-bottom' , ' margin-right'
727
784
);
728
785
begin
729
- Result := Format(' %s: %s;' , [MarginSides[Side], LengthList([Margin])]);
786
+ Result := Format(' %s: %s;' , [MarginSides[Side], LengthList([Margin], LU )]);
730
787
end ;
731
788
732
789
class function TCSS.MaxHeightProp (const HeightPx: Integer): string;
@@ -747,33 +804,35 @@ class function TCSS.OverflowProp(const Value: TCSSOverflowValue;
747
804
Result := Format(' %0:s: %1:s;' , [Props[Direction], Values[Value ]]);
748
805
end ;
749
806
750
- class function TCSS.PaddingProp (const Padding: array of Integer): string;
807
+ class function TCSS.PaddingProp (const Padding: array of Single;
808
+ const LU: TCSSLengthUnit): string;
751
809
begin
752
810
Assert(Length(Padding) in [1 ,2 ,4 ],
753
811
' TCSS.PaddingProp: Invalid padding parameters' );
754
- Result := ' padding: ' + LengthList(Padding) + ' ;' ;
812
+ Result := ' padding: ' + LengthList(Padding, LU ) + ' ;' ;
755
813
end ;
756
814
757
- class function TCSS.PaddingProp (const Top, Right, Bottom, Left: Integer):
758
- string;
815
+ class function TCSS.PaddingProp (const Top, Right, Bottom, Left: Single;
816
+ const LU: TCSSLengthUnit): string;
759
817
begin
760
- Result := PaddingProp([Top, Right, Bottom, Left]);
818
+ Result := PaddingProp([Top, Right, Bottom, Left], LU );
761
819
end ;
762
820
763
- class function TCSS.PaddingProp (const Padding: Integer): string;
821
+ class function TCSS.PaddingProp (const Padding: Single;
822
+ const LU: TCSSLengthUnit): string;
764
823
begin
765
- Result := PaddingProp([Padding]);
824
+ Result := PaddingProp([Padding], LU );
766
825
end ;
767
826
768
- class function TCSS.PaddingProp (const Side: TCSSSide;
769
- const Padding: Integer ): string;
827
+ class function TCSS.PaddingProp (const Side: TCSSSide; const Padding: Single;
828
+ const LU: TCSSLengthUnit ): string;
770
829
const
771
830
// Map of element sides to associated padding properties
772
831
PaddingSides: array [TCSSSide] of string = (
773
832
' padding' , ' padding-top' , ' padding-left' , ' padding-bottom' , ' padding-right'
774
833
);
775
834
begin
776
- Result := Format(' %s: %s;' , [PaddingSides[Side], LengthList([Padding])]);
835
+ Result := Format(' %s: %s;' , [PaddingSides[Side], LengthList([Padding], LU )]);
777
836
end ;
778
837
779
838
class function TCSS.TextAlignProp (const TA: TCSSTextAlign): string;
0 commit comments