Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes bug in TFPDF.GetStringWidth when char is unicode. #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions source/fpdf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1082,10 +1082,33 @@ procedure TFPDF.SetUnderline(fUnderline: Boolean);
end;

function TFPDF.GetStringWidth(const vText: String): Double;

function TryMapToLatin1(const vOrd: Integer; out Value: Integer): Boolean;
var
I: Integer;
begin
if vOrd <= 255 then
begin
Value := vOrd;
Result := True;
Exit;
end;

for I := 0 to 255 do
if (Self.CurrentFont.uv1[I] = vOrd) or (Self.CurrentFont.uv2[I] = vOrd) then
begin
Result := True;
Value := I;
Exit;
end;

Result := False;
end;

var
cw: TFPDFFontInfo;
lines: TStringArray;
vw, vw1, l, i, j: Integer;
vw, vw1, l, i, j, cw_ord: Integer;
begin
// Get width of a string in the current font
Result := 0;
Expand All @@ -1100,7 +1123,12 @@ function TFPDF.GetStringWidth(const vText: String): Double;
l := Length(lines[i]);
vw1 := 0;
for j := 1 to l do
vw1 := vw1 + cw[ord(lines[i][j])];
begin
if TryMapToLatin1(ord(lines[i][j]), cw_ord) then
vw1 := vw1 + cw[cw_ord]
else
vw1 := vw1 + 1000;
end;
if vw1 > vw then
vw := vw1;
end;
Expand Down