Skip to content

Commit

Permalink
Fixed a very minor bug getting bounds of a blank text region (for bac…
Browse files Browse the repository at this point in the history
…kground coloring)
  • Loading branch information
AngusJohnson committed Mar 14, 2023
1 parent 1f42fab commit 49a73fc
Showing 1 changed file with 59 additions and 4 deletions.
63 changes: 59 additions & 4 deletions source/Img32.SVG.Reader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

(*******************************************************************************
* Author : Angus Johnson *
* Version : 4.3 *
* Date : 27 September 2022 *
* Version : 4.4 *
* Date : 14 March 2023 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2019-2022 *
* *
Expand Down Expand Up @@ -318,6 +318,7 @@ TSubtextElement = class(TShapeElement)
protected
text : UTF8String;
procedure GetPaths(const drawDat: TDrawData); override;
function GetBounds: TRectD; override;
public
constructor Create(parent: TSvgElement; svgEl: TSvgTreeEl); override;
end;
Expand Down Expand Up @@ -1373,7 +1374,7 @@ procedure TFilterElement.Clear;

function TFilterElement.GetRelFracLimit: double;
begin
//always assume fractional values below 2.5 are relative
// assume fractional values below 2.5 are always relative
Result := 2.5;
end;
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -2070,6 +2071,7 @@ procedure TShapeElement.Draw(image: TImage32; drawDat: TDrawData);

if not (filled or stroked) or not hasPaths then Exit;
drawDat.bounds := GetBoundsD(drawPathsF);
if drawDat.bounds.IsEmpty then drawDat.bounds := GetBounds;

img := image;
clipRec2 := NullRect;
Expand Down Expand Up @@ -2781,6 +2783,7 @@ procedure TTextElement.DoOffsetX(dx: double);
procedure TTextElement.GetPaths(const drawDat: TDrawData);
var
i : integer;
dy : double;
el : TSvgElement;
di : TDrawData;
topTextEl : TTextElement;
Expand All @@ -2791,6 +2794,7 @@ procedure TTextElement.GetPaths(const drawDat: TDrawData);

if Self is TTSpanElement then
begin
// nb: don't use GetTopTextElement here
el := fParent;
while (el is TTSpanElement) do
el := el.fParent;
Expand Down Expand Up @@ -2819,6 +2823,23 @@ procedure TTextElement.GetPaths(const drawDat: TDrawData);
currentPt.Y := elRectWH.top.rawVal else
currentPt.Y := 0;
startX := currentPt.X;
topTextEl := nil;
end;

if (di.fontInfo.textLength > 0) and
Assigned(fReader.fFontCache) then
begin
with fReader.fFontCache.FontReader.FontInfo do
dy := descent/ (ascent + descent);
if not Assigned(topTextEl) then
topTextEl := GetTopTextElement;
with fDrawData.bounds do
begin
Left := topTextEl.currentPt.X;
Bottom := topTextEl.currentPt.Y + di.fontInfo.size * dy;
Right := Left + di.fontInfo.textLength;
Top := Bottom - di.fontInfo.size;
end;
end;

for i := 0 to fChilds.Count -1 do
Expand Down Expand Up @@ -2908,6 +2929,17 @@ function FixSpaces(const text: UnicodeString): UnicodeString;
end;
//------------------------------------------------------------------------------

function IsBlankText(const text: UnicodeString): Boolean;
var
i: integer;
begin
Result := false;
for i := 1 to Length(text) do
if (text[i] > #32) and (text[i] <> #160) then Exit;
Result := true;
end;
//------------------------------------------------------------------------------

procedure TSubtextElement.GetPaths(const drawDat: TDrawData);
var
el : TSvgElement;
Expand Down Expand Up @@ -2941,7 +2973,13 @@ procedure TSubtextElement.GetPaths(const drawDat: TDrawData);
{$ENDIF}
s := FixSpaces(s);

drawPathsC := fReader.fFontCache.GetTextOutline(0, 0, s, tmpX);
if IsBlankText(s) then
begin
drawPathsC := nil;
tmpX := drawDat.fontInfo.textLength;
end else
drawPathsC := fReader.fFontCache.GetTextOutline(0, 0, s, tmpX);

//by not changing the fontCache.FontHeight, the quality of
//small font render improves very significantly (though of course
//this requires additional glyph scaling and offsetting).
Expand All @@ -2953,6 +2991,8 @@ procedure TSubtextElement.GetPaths(const drawDat: TDrawData);
X := X + tmpX * scale;
end;

if not Assigned(drawPathsC) then Exit;

with drawDat.fontInfo do
if baseShift.rawVal = 0 then
bs := 0 else
Expand All @@ -2964,6 +3004,21 @@ procedure TSubtextElement.GetPaths(const drawDat: TDrawData);
MatrixApply(mat, drawPathsC);
drawPathsF := drawPathsC;
end;
//------------------------------------------------------------------------------

function TSubtextElement.GetBounds: TRectD;
var
textEl: TTextElement;
begin
textEl := TTextElement(fParent);
{$IFDEF UNICODE}
if IsBlankText(UTF8ToUnicodeString(text)) then
{$ELSE}
if IsBlankText(Utf8Decode(text)) then
{$ENDIF}
Result := textEl.fDrawData.bounds else
Result := inherited GetBounds;
end;

//------------------------------------------------------------------------------
// TTSpanElement
Expand Down

0 comments on commit 49a73fc

Please sign in to comment.