Parcourir la source

skia: font weight

mattias il y a 1 an
Parent
commit
7da4e80190

+ 8 - 1
demo/Slider/CustomSlider.lpi

@@ -3,9 +3,13 @@
   <ProjectOptions>
     <Version Value="12"/>
     <General>
+      <Flags>
+        <MainUnitHasCreateFormStatements Value="False"/>
+        <MainUnitHasTitleStatement Value="False"/>
+        <MainUnitHasScaledStatement Value="False"/>
+      </Flags>
       <SessionStorage Value="InProjectDir"/>
       <Title Value="CustomSlider"/>
-      <Scaled Value="True"/>
       <ResourceType Value="res"/>
       <UseXPManifest Value="True"/>
       <Icon Value="0"/>
@@ -55,6 +59,9 @@
       <UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
     </SearchPaths>
     <Linking>
+      <Debugging>
+        <DebugInfoType Value="dsDwarf2"/>
+      </Debugging>
       <Options>
         <Win32>
           <GraphicApplication Value="True"/>

+ 5 - 3
demo/Slider/MainUnit.pas

@@ -36,10 +36,12 @@ implementation
 procedure TFresnelSliderForm.FresnelSliderFormCreate(Sender: TObject);
 begin
   Stylesheet.Text:=
-    '.SliderCaptionDiv {'
+     'div { border: 1px solid; }'
+    +'.SliderCaptionDiv {'
     +'  margin-bottom: 4px;'
     +'  font-size: 12px;'
     +'  width: 100%;'
+    +'  border: 1px solid;'
     +'}'
     +'.SliderLabel {'
     +'}'
@@ -47,7 +49,7 @@ begin
     +'  margin-left: 5px;'
     +'  font-size: 13px;'
     +'  font-weight: 700;'
-    +'  color: #f663020;'
+    +'  color: #f66020;'
     +'}'
     +'.Slider {'
     +'  display: none;' // testing
@@ -81,7 +83,7 @@ begin
     +'  margin-left: -.6em;'
     +'}'
     ;
-  Div1.Style:='Width: 150px';
+  Div1.Style:='width: 150px';
 end;
 
 end.

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

@@ -29,7 +29,6 @@ interface
 uses
   Classes, SysUtils, Math, FPImage, sortbase,
   fpCSSResolver, fpCSSTree, fpCSSParser, FCL.Events,
-//  LazLoggerBase,
   Fresnel.Classes, Fresnel.Events;
 
 type
@@ -41,6 +40,7 @@ type
 
 const
   FresnelDefaultDPI = 96;
+  FresnelFontWeightNormal = 400;
 
 type
   // CSS attributes of the TFresnelElement class
@@ -312,7 +312,7 @@ type
     function GetSize: TFresnelLength;
     function GetStyle: string;
     function GetVariant: string;
-    function GetWeight: string;
+    function GetWeight: TFresnelLength;
     function TextSize(const aText: string): TFresnelPoint;
     function TextSizeMaxWidth(const aText: string; MaxWidth: TFresnelLength): TFresnelPoint;
     function GetTool: TObject;
@@ -342,7 +342,7 @@ type
     Size: TFresnelLength;
     Style: string;
     Variant_: string;
-    Weight: string;
+    Weight: TFresnelLength;
     function Compare(const Desc: TFresnelFontDesc): integer;
     class function CompareDescriptors(const A, B: TFresnelFontDesc): integer; static;
   end;
@@ -1051,7 +1051,7 @@ begin
   if Result<>0 then exit;
   Result:=CompareText(Variant_,Desc.Variant_);
   if Result<>0 then exit;
-  Result:=CompareText(Weight,Desc.Weight);
+  Result:=CompareValue(Weight,Desc.Weight);
 end;
 
 class function TFresnelFontDesc.CompareDescriptors(const A, B: TFresnelFontDesc
@@ -1273,13 +1273,15 @@ var
       CSSResolver.Compute(El,El.StyleElements);
       El.ComputeCSSInit;
     end;
+    // create or free layout nodes
     Layouter.ComputeCSS(El);
-    if El.GetComputedCSSString(fcaDisplayBox,false)='none' then
-      exit;
+
     El.ComputeCSSAfterLayoutNode(Layouter);
     El.WriteComputedAttributes('TFresnelViewport.ApplyCSS');
+
     for i:=0 to El.NodeCount-1 do
       Traverse(El.Nodes[i]);
+
     Layouter.ComputedChildrenCSS(El);
   end;
 
@@ -2711,7 +2713,7 @@ begin
   if (AttrID<FFresnelElementBaseAttrID) or (AttrID>ord(High(TFresnelCSSAttribute))+FFresnelElementBaseAttrID) then
     exit;
   Attr:=TFresnelCSSAttribute(AttrID-FFresnelElementBaseAttrID);
-  FCSSComputed[Attr]:=Value;
+  SetCSSElComputedAttribute(Attr,Value);
 end;
 
 procedure TFresnelElement.SetParent(const AValue: TFresnelElement);
@@ -3375,7 +3377,7 @@ begin
   FFontDesc.Family:=GetComputedCSSString(fcaFontFamily,true);
   FFontDesc.Style:=GetComputedCSSString(fcaFontStyle,true);
   FFontDesc.Variant_:=GetComputedCSSString(fcaFontVariant,true);
-  FFontDesc.Weight:=GetComputedCSSString(fcaFontWeight,true);
+
   s:=GetComputedCSSString(fcaFontSize,true);
   case s of
   'xx-small': FFontDesc.Size:=6;
@@ -3391,6 +3393,18 @@ begin
     if not CSSStrToFloat(s,FFontDesc.Size) then
       FFontDesc.Size:=10;
   end;
+
+  s:=GetComputedCSSString(fcaFontWeight,true);
+  case s of
+  'normal': FFontDesc.Weight:=FresnelFontWeightNormal;
+  'bold': FFontDesc.Weight:=700;
+  'lighter': FFontDesc.Weight:=300;
+  'bolder': FFontDesc.Weight:=500;
+  else
+    if not CSSStrToFloat(s,FFontDesc.Weight) then
+      FFontDesc.Weight:=FresnelFontWeightNormal;
+  end;
+
   FFontDesc.Kerning:=GetComputedCSSString(fcaFontKerning,true);
   if FFont<>nil then
   begin
@@ -4131,10 +4145,21 @@ begin
 end;
 
 procedure TFresnelElement.ComputeCSSAfterLayoutNode(Layouter: TFresnelLayouter);
+
+  procedure Convert(Attr: TFresnelCSSAttribute; IsHorizontal: boolean);
+  var
+    Value: String;
+  begin
+    Value:=CSSComputedAttribute[Attr];
+    ConvertCSSValueToPixel(Layouter,Attr,IsHorizontal,Value);
+    CSSComputedAttribute[Attr]:=Value;
+  end;
+
 var
   Attr: TFresnelCSSAttribute;
-  Value: String;
 begin
+  Convert(fcaFontSize,false);
+
   // convert non-pixel units to pixels
   ConvertCSSBorderWidthToPix(fcaBorderLeftWidth);
   ConvertCSSBorderWidthToPix(fcaBorderRightWidth);
@@ -4163,9 +4188,7 @@ begin
       fcaPaddingTop,
       fcaPaddingBottom] do
   begin
-    Value:=CSSComputedAttribute[Attr];
-    ConvertCSSValueToPixel(Layouter,Attr,true,Value);
-    CSSComputedAttribute[Attr]:=Value;
+    Convert(Attr,true);
   end;
 
   // simple vertical values
@@ -4179,9 +4202,7 @@ begin
       fcaMinHeight,
       fcaMaxHeight] do
   begin
-    Value:=CSSComputedAttribute[Attr];
-    ConvertCSSValueToPixel(Layouter,Attr,false,Value);
-    CSSComputedAttribute[Attr]:=Value;
+    Convert(Attr,false);
   end;
 
   // compound values

+ 1 - 1
src/base/fresnel.layouter.pas

@@ -306,7 +306,7 @@ begin
     ChildEl:=ChildNode.Element;
 
     ChildTop:=FLineBorderBoxTop-BFCNode.MarginTop;
-    //writeln('TFLBlockFormattingContext.PlaceLineNodes ',ChildEl.Name,' ',BFCNode.Left,' ',ChildTop,' BFCNode.Height=',BFCNode.ContentBoxHeight);
+    //writeln('TFLBlockFormattingContext.PlaceLineNodes ',ChildEl.GetPath,' ',BFCNode.Left,' ',ChildTop,' BFCNode.Height=',BFCNode.ContentBoxHeight);
     ChildBottom:=FLineBorderBoxTop
                 +BFCNode.BorderTop+BFCNode.PaddingTop
                 +BFCNode.ContentBoxHeight

+ 7 - 12
src/lcl/fresnel.lcl.pas

@@ -34,7 +34,7 @@ type
     Size: TFresnelLength;
     Style: string;
     Variant_: string;
-    Weight: string;
+    Weight: TFresnelLength;
     LCLFont: TFont;
     destructor Destroy; override;
     function GetFamily: string;
@@ -42,7 +42,7 @@ type
     function GetSize: TFresnelLength;
     function GetStyle: string;
     function GetVariant: string;
-    function GetWeight: string;
+    function GetWeight: TFresnelLength;
     function TextSize(const aText: string): TFresnelPoint; virtual;
     function TextSizeMaxWidth(const aText: string; MaxWidth: TFresnelLength
       ): TFresnelPoint; virtual;
@@ -167,7 +167,7 @@ begin
   if Result<>0 then exit;
   Result:=CompareText(Font1.Style,Font2.Style);
   if Result<>0 then exit;
-  Result:=CompareText(Font1.Weight,Font2.Weight);
+  Result:=CompareValue(Font1.Weight,Font2.Weight);
   if Result<>0 then exit;
   Result:=CompareText(Font1.Variant_,Font2.Variant_);
   if Result<>0 then exit;
@@ -185,7 +185,7 @@ begin
   if Result<>0 then exit;
   Result:=CompareText(Desc^.Style,aFont.Style);
   if Result<>0 then exit;
-  Result:=CompareText(Desc^.Weight,aFont.Weight);
+  Result:=CompareValue(Desc^.Weight,aFont.Weight);
   if Result<>0 then exit;
   Result:=CompareText(Desc^.Variant_,aFont.Variant_);
   if Result<>0 then exit;
@@ -576,13 +576,8 @@ begin
     aFont.LCLFont:=aLCLFont;
     aLCLFont.Size:=round(aFont.Size);
     aStyle:=[];
-    case aFont.Weight of
-    'bold',
-    'bolder',
-    '500',
-    '600',
-    '700': Include(aStyle,fsBold);
-    end;
+    if aFont.Weight>=500 then
+      Include(aStyle,fsBold);
     case aFont.Style of
     'italic': Include(aStyle,fsItalic);
     end;
@@ -640,7 +635,7 @@ begin
   Result:=Variant_;
 end;
 
-function TFresnelLCLFont.GetWeight: string;
+function TFresnelLCLFont.GetWeight: TFresnelLength;
 begin
   Result:=Weight;
 end;

+ 18 - 6
src/skia/fresnel.skiarenderer.pas

@@ -35,7 +35,7 @@ type
     CSSKerning: string;
     CSSStyle: string;
     CSSVariant_: string;
-    CSSWeight: string;
+    CSSWeight: TFresnelLength;
     procedure AddRef;
     procedure Release;
     property RefCount: integer read FRefCount;
@@ -58,7 +58,7 @@ type
     function GetSize: TFresnelLength;
     function GetStyle: string;
     function GetVariant: string;
-    function GetWeight: string;
+    function GetWeight: TFresnelLength;
     function TextSize(const aText: string): TFresnelPoint; virtual;
     function TextSizeMaxWidth(const aText: string; MaxWidth: TFresnelLength
       ): TFresnelPoint; virtual;
@@ -146,7 +146,7 @@ begin
   if Result<>0 then exit;
   Result:=CompareText(Face1.CSSVariant_,Face2.CSSVariant_);
   if Result<>0 then exit;
-  Result:=CompareText(Face1.CSSWeight,Face2.CSSWeight);
+  Result:=CompareValue(Face1.CSSWeight,Face2.CSSWeight);
 end;
 
 function CompareFresnelFontDescWithSkiaFont(Key, Item: Pointer): integer;
@@ -172,7 +172,7 @@ begin
   if Result<>0 then exit;
   Result:=CompareText(Desc^.Variant_,Face.CSSVariant_);
   if Result<>0 then exit;
-  Result:=CompareText(Desc^.Weight,Face.CSSWeight);
+  Result:=CompareValue(Desc^.Weight,Face.CSSWeight);
 end;
 
 { TFresnelSkiaTypeFace }
@@ -234,7 +234,7 @@ begin
   Result:=TypeFace.CSSVariant_;
 end;
 
-function TFresnelSkiaFont.GetWeight: string;
+function TFresnelSkiaFont.GetWeight: TFresnelLength;
 begin
   Result:=TypeFace.CSSWeight;
 end;
@@ -326,6 +326,10 @@ function TFresnelSkiaFontEngine.Allocate(const Desc: TFresnelFontDesc
 var
   aFont: TFresnelSkiaFont;
   aTypeFace: TFresnelSkiaTypeFace;
+  aFontStyle: TSkFontStyle;
+  SkWeight: integer;
+  SkWidth: Integer;
+  SkSlant: TSkFontSlant;
 begin
   aFont:=FindFont(Desc);
   if aFont<>nil then
@@ -341,7 +345,15 @@ begin
     aTypeFace.CSSVariant_:=Desc.Variant_;
     aTypeFace.CSSWeight:=Desc.Weight;
     FTypeFaces.Add(aTypeFace);
-    aTypeface.SKTypeFace := TSkTypeface.MakeFromName('Monospace', TSkFontStyle.Normal);
+
+    aFontStyle:=TSkFontStyle.Normal;
+    SkWeight:=round(Min(Max(aTypeFace.CSSWeight,0),ord(TSkFontWeight.ExtraBlack)));
+    SkWidth:=aFontStyle.Width;
+    SkSlant:=aFontStyle.Slant;
+    if SkWeight<>aFontStyle.Weight then
+      aFontStyle:=TSkFontStyle.Create(SkWeight,SkWidth,SkSlant);
+
+    aTypeface.SKTypeFace := TSkTypeface.MakeFromName('Monospace', aFontStyle);
   end;
 
   aFont:=TFresnelSkiaFont.Create;