TurboPower_ipro: Optimize 'px' in TIpHtmlNodeCore.GetFontSizeFromCSS. Fix possibly missing Result in function CssMarginToProps.

This commit is contained in:
Juha 2024-02-07 14:37:21 +02:00
parent c9e4a59c62
commit bc201de1ed

View File

@ -4731,29 +4731,29 @@ begin
Owner.IdList.AddObject(FId, Self); Owner.IdList.AddObject(FId, Self);
end; end;
function CssMarginToProps(CssMargin: TCSSMargin;
out ElemMargin: TIpHtmlElemMargin): boolean;
begin
ElemMargin.Style:=hemsAuto;
ElemMargin.Size:=0;
if CssMargin.Style=cmsNone then exit(false);
if CssMargin.Style=cmsAuto then exit(true);
if CssMargin.Style=cmsPx then begin
ElemMargin.Style:=hemsPx;
ElemMargin.Size:=CssMargin.Size;
exit(true);
end;
if CssMargin.Style=cmsEm then begin
ElemMargin.Style:=hemsPx;
ElemMargin.Size:=10*CssMargin.Size; // 1em = 1 current font size
exit(true);
end;
Result:=false;
debugln(['TIpHtmlNodeCore.ApplyCSSProps.CssMarginToProps note: margin style not supported ',ord(CssMargin.Style)]);
end;
procedure TIpHtmlNodeCore.ApplyCSSProps(const ACSSProps: TCSSProps; procedure TIpHtmlNodeCore.ApplyCSSProps(const ACSSProps: TCSSProps;
const props: TIpHtmlProps); const props: TIpHtmlProps);
function CssMarginToProps(CssMargin: TCSSMargin;
out ElemMargin: TIpHtmlElemMargin): boolean;
begin
ElemMargin.Style:=hemsAuto;
ElemMargin.Size:=0;
if CssMargin.Style=cmsNone then exit(false);
if CssMargin.Style=cmsAuto then exit(true);
if CssMargin.Style=cmsPx then begin
ElemMargin.Style:=hemsPx;
ElemMargin.Size:=CssMargin.Size;
exit(true);
end;
if CssMargin.Style=cmsEm then begin
ElemMargin.Style:=hemsPx;
ElemMargin.Size:=10*CssMargin.Size; // 1em = 1 current font size
exit(true);
end;
debugln(['TIpHtmlNodeCore.ApplyCSSProps.CssMarginToProps note: margin style not supported ',ord(CssMargin.Style)]);
end;
var var
ElemMargin: TIpHtmlElemMargin; ElemMargin: TIpHtmlElemMargin;
begin begin
@ -4827,8 +4827,35 @@ begin
Result := Props.Alignment; Result := Props.Alignment;
end; end;
function TIpHtmlNodeCore.GetFontSizeFromCSS(CurrentFontSize:Integer; var // Remember previous in/out values for function GetFPxSize.
aFontSize: string):Integer; PrevFontSize: string;
PrevPxResult: Integer;
// Calculate points based on screen resolution :(
// at 96dpi CSS21 recommneds 1px=0.26 mm
// TODO: use screen resolution, check printing!
function GetFPxSize(aFontSize: string): Integer;
var
i: Integer;
dd: double;
begin
// Optimize consecutive identical values. Return a saved value.
if aFontSize=PrevFontSize then
exit(PrevPxResult);
// Calculate
i := pos('px', aFontSize);
if i>0 then begin
dd := StrToFloatDef(copy(aFontSize,1,i-1), -1.0);
Result := Round(dd * 0.7370241);
PrevFontSize := aFontSize;
PrevPxResult := Result;
end
else
result := -1;
end;
function TIpHtmlNodeCore.GetFontSizeFromCSS(CurrentFontSize: Integer;
aFontSize: string): Integer;
function GetFSize(aUnits: string): double; function GetFSize(aUnits: string): double;
var var
@ -4840,7 +4867,7 @@ function TIpHtmlNodeCore.GetFontSizeFromCSS(CurrentFontSize:Integer;
else else
result := -1.0; result := -1.0;
end; end;
function GetParentFontSize: integer; function GetParentFontSize: integer;
begin begin
if (FParentNode is TIpHtmlNodeBlock) then if (FParentNode is TIpHtmlNodeBlock) then
@ -4854,45 +4881,35 @@ function TIpHtmlNodeCore.GetFontSizeFromCSS(CurrentFontSize:Integer;
else else
result := CurrentFontSize; result := CurrentFontSize;
end; end;
var var
P: double; P: double;
//ParentFSize: Integer; i: Integer;
begin begin
result := CurrentFontSize; result := CurrentFontSize;
// check px (most common)
i:=GetFPxSize(aFontSize);
if i>0 then
exit(i);
// check pt // check pt
P:=GetFSize('pt'); P:=GetFSize('pt');
if P>0 then begin if P>0 then
result := round(P); exit(round(P));
exit;
end;
// check px
P:=GetFSize('px');
if P>0 then begin
// calculate points based on screen resolution :(
// at 96dpi CSS21 recommneds 1px=0.26 mm
// TODO: use screen resolution, check printing!
Result := Round(P*0.7370241);
exit;
end;
//todo: em, ex are supposed to be based on the computed pixel size of //todo: em, ex are supposed to be based on the computed pixel size of
// parent node, tpipro has no provision for this.... // parent node, tpipro has no provision for this....
// check % // check %
P:=GetFSize('%'); P:=GetFSize('%');
if P>0 then begin if P>0 then
result := round(GetParentFontSize * P/100); exit(round(GetParentFontSize * P/100));
exit;
end;
// check em // check em
P:=GetFSize('em'); P:=GetFSize('em');
if P>0 then begin if P>0 then
result := round(GetParentFontSize * P); result := round(GetParentFontSize * P);
end;
end; end;
constructor TIpHtmlNodeCore.Create(ParentNode: TIpHtmlNode); constructor TIpHtmlNodeCore.Create(ParentNode: TIpHtmlNode);