Hi.
I'm new with unicode, so I hope anybody can help me. I want write unicode to a PDF, now I need the width and height of the unicode text for text formatting. For AnsiString I have this functions in PDF class:
function PDFClass.TextWidth(Text: AnsiString): Single;
var
i: integer;
ch: AnsiChar;
tmpWidth: Single;
chv: Integer;
begin
Result := 0;
for I := 1 to Length(Text) do
begin
ch := Text[i];
chv := CurrentFontObj.GetCharWidth(Text, I);
tmpWidth := chv * CurrentFontObj.Size / 1000;
if FHorizontalScaling <> 100 then
tmpWidth := tmpWidth * FHorizontalScaling / 100;
if tmpWidth > 0 then
tmpWidth := tmpWidth + FCharSpace
else
tmpWidth := 0;
if (ch = ' ') and (FWordSpace > 0) and (i <> Length(Text)) then
tmpWidth := tmpWidth + FWordSpace;
Result := Result + tmpWidth;
end;
Result := (Result / DocScale);
end;
function PDFClass.TextHeight(Text: AnsiString): Real;
begin
Result := CurrentFontObj.Size * CurrentFontObj.Ascent / 1000;
end;
Can anybody help me with this function for unicode text? I use this component in my C++Builder 2009 with UnicodeString.
CurrentFontObj is from class PDFFontObj
PDFFontObj = class(TObject)
private
Name: AnsiString;
Size: Single;
ArrIndex: Integer;
Saved: boolean;
OldName: AnsiString;
Ascent: Integer;
FActive: boolean;
IsUsed: boolean;
UniLen: Integer;
FontLen: Integer;
IsUnicode: boolean;
IsVertical: boolean;
OrdinalName: AnsiString;
IsStandard: boolean;
FontStyle: TFontStyles;
FontCharset: TFontCharset;
IsMonospaced: boolean;
OutTextM: OUTLINETEXTMETRIC;
ABCArray: array[0..255] of ABC;
Symbols: array of CDescript;
UnicodeTable: array of IndexedChar;
SymbolTable: array[32..255] of boolean;
function GetCharWidth(AText: AnsiString; APos: integer): integer;
function GetCodeByID(ID: Word): Word;
procedure CopyFontFetures(InFnt: PDFFontObj);
procedure GetFontFeatures;
procedure ParseFontName;
procedure ClearTables;
end;
function PDFFontObj.GetCharWidth(AText: AnsiString; APos: integer): integer;
var
ChCode: Byte;
begin
ChCode := Ord(AText[APos]);
if not IsMonospaced then
Result := ABCArray[ChCode].abcA + Integer(ABCArray[ChCode].abcB) + ABCArray[ChCode].abcC
else
Result := ABCArray[0].abcA + Integer(ABCArray[0].abcB) + ABCArray[0].abcC;
end;