From 54a48183bc82fc203be8888f0cc122a9a2a698cc Mon Sep 17 00:00:00 2001 From: michael Date: Wed, 17 Feb 2021 13:26:18 +0000 Subject: [PATCH] * Fix from Joellin to correctly read monospace fonts git-svn-id: trunk@48694 - --- packages/fcl-pdf/src/fpparsettf.pp | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/fcl-pdf/src/fpparsettf.pp b/packages/fcl-pdf/src/fpparsettf.pp index fc7c243b5c..9dc055fca4 100644 --- a/packages/fcl-pdf/src/fpparsettf.pp +++ b/packages/fcl-pdf/src/fpparsettf.pp @@ -822,13 +822,13 @@ begin if embed and not Embeddable then raise ETTF.Create(rsFontEmbeddingNotAllowed); PrepareEncoding(Encoding); -// MissingWidth:=ToNatural(Widths[Chars[CharCodes^[32]]].AdvanceWidth); // Char(32) - Space character - FMissingWidth := Widths[Chars[CharCodes^[32]]].AdvanceWidth; // Char(32) - Space character +// MissingWidth:=ToNatural(GetAdvanceWidth(Chars[CharCodes^[32]])); // Char(32) - Space character + FMissingWidth := GetAdvanceWidth(Chars[CharCodes^[32]]); // Char(32) - Space character for I:=0 to 255 do begin if (CharCodes^[i]>=0) and (CharCodes^[i]<=High(Chars)) - and (Widths[Chars[CharCodes^[i]]].AdvanceWidth> 0) and (CharNames^[i]<> '.notdef') then - CharWidth[I]:= ToNatural(Widths[Chars[CharCodes^[I]]].AdvanceWidth) + and (GetAdvanceWidth(Chars[CharCodes^[i]])> 0) and (CharNames^[i]<> '.notdef') then + CharWidth[I]:= ToNatural(GetAdvanceWidth(Chars[CharCodes^[I]])) else CharWidth[I]:= FMissingWidth; end; @@ -930,8 +930,19 @@ begin end; function TTFFileInfo.GetAdvanceWidth(AIndex: word): word; +var + i: SizeInt; begin - Result := Widths[AIndex].AdvanceWidth; + // There may be more glyphs than elements in the array, in which + // case the last entry is to be used. + // https://docs.microsoft.com/en-us/typography/opentype/spec/hmtx + i := Length(Widths); + if AIndex >= i then + Dec(i) + else + i := AIndex; + + Result := Widths[i].AdvanceWidth; end; function TTFFileInfo.ItalicAngle: single; @@ -972,7 +983,7 @@ function TTFFileInfo.GetMissingWidth: integer; begin if FMissingWidth = 0 then begin - FMissingWidth := Widths[Chars[CharCodes^[32]]].AdvanceWidth; // 32 is in reference to the Space character + FMissingWidth := GetAdvanceWidth(Chars[CharCodes^[32]]); // 32 is in reference to the Space character end; Result := FMissingWidth; end;