Browse Source

dom: using Font.GetSize

mattias 1 year ago
parent
commit
454509c967
2 changed files with 55 additions and 28 deletions
  1. 12 22
      src/base/fresnel.dom.pas
  2. 43 6
      tests/base/TCFresnelCSS.pas

+ 12 - 22
src/base/fresnel.dom.pas

@@ -876,7 +876,6 @@ type
     procedure ComputeCSSValues; virtual; // call resolver to collect CSS values and resolve shorthands
     procedure ComputeCSSAfterLayoutNode(Layouter: TFresnelLayouter); virtual; // after layouter node, before layouter traverse children
     function GetCSSString(AttrID: TCSSNumericalID; Compute: boolean; out Complete: boolean): string; virtual;
-    function GetComputedFontSize: TFresnelLength; virtual;
     function GetComputedLength(Attr: TFresnelCSSAttribute; UseNaNOnFail: boolean = false): TFresnelLength; virtual; // on fail returns NaN
     function GetComputedString(Attr: TFresnelCSSAttribute): string; virtual;
     function GetComputedCSSString(AttrID: TCSSNumericalID): string; virtual; overload;
@@ -1019,7 +1018,6 @@ type
     procedure DomChanged; override;
     procedure Disconnecting; virtual;
     function AllocateFont(const Desc: TFresnelFontDesc): IFresnelFont; virtual;
-    function GetComputedFontSize: TFresnelLength; override;
     function GetCSSString(AttrID: TCSSNumericalID; Compute: boolean; out Complete: boolean): string; override;
     class function CSSTypeID: TCSSNumericalID; override;
     class function CSSTypeName: TCSSString; override;
@@ -2688,7 +2686,7 @@ end;
 
 function TFresnelCSSRegistry.GetFontSize(El: TFresnelElement): string;
 begin
-  Result:=FloatToCSSPx(El.GetComputedFontSize);
+  Result:=FloatToCSSPx(El.Font.GetSize);
 end;
 
 function TFresnelCSSRegistry.GetMargin(El: TFresnelElement): string;
@@ -3770,11 +3768,6 @@ begin
     raise EFresnelFont.Create('TFresnelViewport.AllocateFont no FontEngine');
 end;
 
-function TFresnelViewport.GetComputedFontSize: TFresnelLength;
-begin
-  Result:=FresnelDefaultFontSize;
-end;
-
 function TFresnelViewport.GetCSSString(AttrID: TCSSNumericalID; Compute: boolean; out
   Complete: boolean): string;
 var
@@ -3796,6 +3789,8 @@ begin
       fcaZIndex: Result:='0';
       fcaWidth: Result:=FloatToCSSStr(Width)+'px';
       fcaHeight: Result:=FloatToCSSStr(Height)+'px';
+      fcaFontSize: Result:=FloatToCSSStr(FresnelDefaultFontSize)+'px'; // todo use system font
+      fcaFontWeight: Result:=FloatToCSSStr(FresnelFontWeightNormal); // todo use system font
       else
         Result:=inherited;
       end;
@@ -4420,11 +4415,6 @@ begin
   Result:=true;
 end;
 
-function TFresnelElement.GetComputedFontSize: TFresnelLength;
-begin
-  Result:=GetFont.GetSize;
-end;
-
 function TFresnelElement.GetDPI(IsHorizontal: boolean): TFresnelLength;
 begin
   if Parent<>nil then
@@ -5133,15 +5123,15 @@ begin
     cu_pt: Result:=Viewport.DPI[IsHorizontal]/72; // 1pt = 1/72 in
     cu_pc: Result:=Viewport.DPI[IsHorizontal]/6; // 1pc = 12 pt
     // relative to element's font
-    cu_em: Result:=GetComputedFontSize; // todo
-    cu_ex: Result:=GetComputedFontSize/2; // todo
-    cu_cap: Result:=GetComputedFontSize; // todo
-    cu_ch: Result:=GetComputedFontSize; // todo
+    cu_em: Result:=Font.GetSize; // todo
+    cu_ex: Result:=Font.GetSize/2; // todo
+    cu_cap: Result:=Font.GetSize; // todo
+    cu_ch: Result:=Font.GetSize; // todo
     // relative to root's font
-    cu_rem: Result:=Viewport.GetComputedFontSize; // todo
-    cu_rex: Result:=Viewport.GetComputedFontSize/2; // todo
-    cu_rcap: Result:=Viewport.GetComputedFontSize; // todo
-    cu_rchh: Result:=Viewport.GetComputedFontSize; // todo
+    cu_rem: Result:=Viewport.Font.GetSize; // todo
+    cu_rex: Result:=Viewport.Font.GetSize/2; // todo
+    cu_rcap: Result:=Viewport.Font.GetSize; // todo
+    cu_rchh: Result:=Viewport.Font.GetSize; // todo
     // relative to default viewport size
     cu_vw,cu_svw,cu_lvw,cu_dvw: Result:=Viewport.Width/100;
     cu_vh,cu_svh,cu_lvh,cu_dvh: Result:=Viewport.Height/100;
@@ -5307,7 +5297,7 @@ begin
         end else if aComp.FloatUnit=cuPercent then
         begin
           if Parent<>nil then
-            FFontDesc.Size:=aComp.Float*Parent.GetComputedFontSize/100;
+            FFontDesc.Size:=aComp.Float*Parent.Font.GetSize/100;
         end;
       end;
     end;

+ 43 - 6
tests/base/TCFresnelCSS.pas

@@ -128,6 +128,7 @@ type
     procedure Test_BorderColor_AsString;
     procedure Test_BorderStyle_AsString;
     procedure Test_BorderWidth_AsString;
+    procedure Test_Border_AsString;
 
     procedure TestVar_NoDefault;
     procedure TestVar_Initial;
@@ -723,8 +724,8 @@ begin
   Div1.Parent:=Body;
 
   Viewport.ApplyCSS;
-  AssertEquals('Body.GetComputedFontSize',Body.GetComputedFontSize,30);
-  AssertEquals('Div1.GetComputedFontSize',Div1.GetComputedFontSize,60);
+  AssertEquals('Body.GetComputedFontSize',Body.Font.GetSize,30);
+  AssertEquals('Div1.GetComputedFontSize',Div1.Font.GetSize,60);
 end;
 
 procedure TTestFresnelCSS.Test_FontSize_AsString;
@@ -740,7 +741,7 @@ begin
   Body.Parent:=Viewport;
 
   Viewport.ApplyCSS;
-  AssertEquals('Body.GetComputedFontSize',30,Body.GetComputedFontSize);
+  AssertEquals('Body.GetComputedFontSize',30,Body.Font.GetSize);
   AssertEquals('Body.GetComputedFontSize','30px',Body.GetComputedString(fcaFontSize));
 end;
 
@@ -822,6 +823,42 @@ begin
   AssertEquals('Div1.GetComputedString(fcaBorderWidth)','1px 2px 3px 4px',Div1.GetComputedString(fcaBorderWidth));
 end;
 
+procedure TTestFresnelCSS.Test_Border_AsString;
+var
+  Div1: TDiv;
+begin
+  Viewport.Stylesheet.Text:=LinesToStr([
+    'div {',
+    'border:1px red solid;',
+    '}']);
+  Div1:=TDiv.Create(Viewport);
+  Div1.Name:='Div1';
+  Div1.Parent:=Viewport;
+
+  Viewport.ApplyCSS;
+  AssertEquals('Div1.GetComputedString(fcaBorderTopWidth)','1px',Div1.GetComputedString(fcaBorderTopWidth));
+  AssertEquals('Div1.GetComputedString(fcaBorderRightWidth)','1px',Div1.GetComputedString(fcaBorderRightWidth));
+  AssertEquals('Div1.GetComputedString(fcaBorderBottomWidth)','1px',Div1.GetComputedString(fcaBorderBottomWidth));
+  AssertEquals('Div1.GetComputedString(fcaBorderLeftWidth)','1px',Div1.GetComputedString(fcaBorderLeftWidth));
+
+  AssertEquals('Div1.GetComputedString(fcaBorderTopStyle)','solid',Div1.GetComputedString(fcaBorderTopStyle));
+  AssertEquals('Div1.GetComputedString(fcaBorderRightStyle)','solid',Div1.GetComputedString(fcaBorderRightStyle));
+  AssertEquals('Div1.GetComputedString(fcaBorderBottomStyle)','solid',Div1.GetComputedString(fcaBorderBottomStyle));
+  AssertEquals('Div1.GetComputedString(fcaBorderLeftStyle)','solid',Div1.GetComputedString(fcaBorderLeftStyle));
+
+  AssertEquals('Div1.GetComputedString(fcaBorderTopColor)','red',Div1.GetComputedString(fcaBorderTopColor));
+  AssertEquals('Div1.GetComputedString(fcaBorderRightColor)','red',Div1.GetComputedString(fcaBorderRightColor));
+  AssertEquals('Div1.GetComputedString(fcaBorderBottomColor)','red',Div1.GetComputedString(fcaBorderBottomColor));
+  AssertEquals('Div1.GetComputedString(fcaBorderLeftColor)','red',Div1.GetComputedString(fcaBorderLeftColor));
+
+  AssertEquals('Div1.GetComputedString(fcaBorderTop)','red solid 1px',Div1.GetComputedString(fcaBorderTop));
+  AssertEquals('Div1.GetComputedString(fcaBorderRight)','red solid 1px',Div1.GetComputedString(fcaBorderRight));
+  AssertEquals('Div1.GetComputedString(fcaBorderBottom)','red solid 1px',Div1.GetComputedString(fcaBorderBottom));
+  AssertEquals('Div1.GetComputedString(fcaBorderLeft)','red solid 1px',Div1.GetComputedString(fcaBorderLeft));
+
+  AssertEquals('Div1.GetComputedString(fcaBorderWidth)','red solid 1px',Div1.GetComputedString(fcaBorder));
+end;
+
 procedure TTestFresnelCSS.TestVar_NoDefault;
 var
   Body: TBody;
@@ -863,8 +900,8 @@ begin
   Div1.Parent:=Body;
 
   Viewport.ApplyCSS;
-  AssertEquals('Body.GetComputedFontSize',Body.GetComputedFontSize,30);
-  AssertEquals('Div1.GetComputedFontSize',Div1.GetComputedFontSize,FresnelDefaultFontSize);
+  AssertEquals('Body.GetComputedFontSize',Body.Font.GetSize,30);
+  AssertEquals('Div1.GetComputedFontSize',Div1.Font.GetSize,FresnelDefaultFontSize);
 end;
 
 procedure TTestFresnelCSS.TestVar_Inline;
@@ -881,7 +918,7 @@ begin
   Div1.Style:='--size:28px;';
 
   Viewport.ApplyCSS;
-  AssertEquals('Div1.GetComputedFontSize',28,Div1.GetComputedFontSize);
+  AssertEquals('Div1.GetComputedFontSize',28,Div1.Font.GetSize);
 end;
 
 Initialization