Browse Source

fontsize: compute on init, use absolute font sizes

mattias 1 year ago
parent
commit
00dfe7ea7f
1 changed files with 108 additions and 15 deletions
  1. 108 15
      src/base/fresnel.dom.pas

+ 108 - 15
src/base/fresnel.dom.pas

@@ -519,6 +519,7 @@ type
     function CSSReadNextToken(const aValue: string; var p: integer): string; // read linear-gradient with out the brackets
     function CheckCSSLength(Attr: TFresnelCSSAttribute; const AValue: string; const Checks: TFresnelLengthChecks = []): boolean; virtual;
     procedure ComputeCSSAttribute(Attr: TFresnelCSSAttribute); virtual;
+    procedure ComputeCSSFontSize; virtual;
     function GetDPI(IsHorizontal: boolean): TFresnelLength; virtual;
     function GetViewport: TFresnelViewport; virtual;
     procedure SetViewportConnected(AValue: boolean); virtual;
@@ -3396,6 +3397,109 @@ begin
   FCSSComputed[Attr]:=aValue;
 end;
 
+procedure TFresnelElement.ComputeCSSFontSize;
+
+  function GetViewportFontSize: TFresnelLength;
+  var
+    ViewPort: TFresnelViewport;
+  begin
+    ViewPort:=GetViewport;
+    Result:=ViewPort.GetComputedCSSLength(fcaFontSize,false,true);
+    if IsNan(Result) then
+      Result:=10;
+  end;
+
+  function GetParentFontSize: TFresnelLength;
+  begin
+    if Parent=nil then
+      Result:=10
+    else begin
+      Result:=Parent.GetComputedCSSLength(fcaFontSize,true);
+      if Result<0.1 then
+        Result:=0.1
+      else if Result>100000 then
+        Result:=100000;
+    end;
+  end;
+
+  function GetSmaller: TFresnelLength;
+  var
+    aRatio, aParentSize, aViewportSize: TFresnelLength;
+  begin
+    aParentSize:=GetParentFontSize;
+    aViewportSize:=GetViewportFontSize;
+    aRatio:=aParentSize / aViewportSize;
+    if aRatio<=0.6 then
+      Result:=aParentSize * 0.89
+    else if aRatio<=0.75 then
+      Result:=aViewportSize * 0.6
+    else if aRatio<=0.89 then
+      Result:=aViewportSize * 0.75
+    else if aRatio<=1.0 then
+      Result:=aViewportSize * 0.89
+    else if aRatio<=1.2 then
+      Result:=aViewportSize * 1.0
+    else if aRatio<=1.5 then
+      Result:=aViewportSize * 1.2
+    else if aRatio<=2 then
+      Result:=aViewportSize * 1.5
+    else if aRatio<=3 then
+      Result:=aViewportSize * 2.0
+    else
+      Result:=aViewportSize * 3.0;
+  end;
+
+  function GetLarger: TFresnelLength;
+  var
+    aRatio, aParentSize, aViewportSize: TFresnelLength;
+  begin
+    aParentSize:=GetParentFontSize;
+    aViewportSize:=GetViewportFontSize;
+    aRatio:=aParentSize / aViewportSize;
+    if aRatio>3.0 then
+      Result:=aParentSize * 1.5
+    else if aRatio>2.0 then
+      Result:=aViewportSize * 3.0
+    else if aRatio>1.5 then
+      Result:=aViewportSize * 2.0
+    else if aRatio>1.2 then
+      Result:=aViewportSize * 1.5
+    else if aRatio>1.0 then
+      Result:=aViewportSize * 1.2
+    else if aRatio>=0.89 then
+      Result:=aViewportSize * 1.0
+    else if aRatio>=0.75 then
+      Result:=aViewportSize * 0.89
+    else if aRatio>=0.6 then
+      Result:=aViewportSize * 0.75
+    else
+      Result:=aViewportSize * 0.6;
+  end;
+
+var
+  Size: TFresnelLength;
+  s, NewFontSize: String;
+begin
+  s:=GetComputedCSSString(fcaFontSize,true);
+  case s of
+  'xx-small': Size:=GetViewportFontSize*0.6;
+  'x-small': Size:=GetViewportFontSize*0.75;
+  'smaller': Size:=GetSmaller;
+  'small': Size:=GetViewportFontSize*0.89;
+  'medium': Size:=GetViewportFontSize*0.6;
+  'large': Size:=GetViewportFontSize*1.2;
+  'larger': Size:=GetLarger;
+  'x-large': Size:=GetViewportFontSize*2;
+  'xx-large': Size:=GetViewportFontSize*3;
+  else
+    if not CSSStrToFloat(s,Size) then
+      Size:=10;
+  end;
+  NewFontSize:=FloatToCSSStr(Size);
+  if s<>NewFontSize then
+    CSSComputedAttribute[fcaFontSize]:=NewFontSize;
+end;
+
 function TFresnelElement.GetDPI(IsHorizontal: boolean): TFresnelLength;
 begin
   if Parent<>nil then
@@ -3862,25 +3966,15 @@ begin
   if fesFontDescValid in FStates then
     exit(FFont);
   Include(FStates,fesFontDescValid);
+  ViewPort:=GetViewport;
+
   FFontDesc.Family:=GetComputedCSSString(fcaFontFamily,true);
   FFontDesc.Style:=GetComputedCSSString(fcaFontStyle,true);
   FFontDesc.Variant_:=GetComputedCSSString(fcaFontVariant,true);
 
   s:=GetComputedCSSString(fcaFontSize,true);
-  case s of
-  'xx-small': FFontDesc.Size:=6;
-  'x-small': FFontDesc.Size:=7;
-  'smaller': FFontDesc.Size:=8;
-  'small': FFontDesc.Size:=8;
-  'medium': FFontDesc.Size:=10;
-  'large': FFontDesc.Size:=13;
-  'larger': FFontDesc.Size:=15;
-  'x-large': FFontDesc.Size:=20;
-  'xx-large': FFontDesc.Size:=30;
-  else
-    if not CSSStrToFloat(s,FFontDesc.Size) then
-      FFontDesc.Size:=10;
-  end;
+  if not CSSStrToFloat(s,FFontDesc.Size) then
+    FFontDesc.Size:=10;
 
   s:=GetComputedCSSString(fcaFontWeight,true);
   case s of
@@ -3906,7 +4000,6 @@ begin
     FFont:=nil;
   end;
 
-  ViewPort:=GetViewport;
   FFont:=ViewPort.AllocateFont(FFontDesc);
   Result:=FFont;
   if FFont<>nil then