Суммирование:
Не удалось найти способ вывода знака ангстрема с помощью библиотеки AggPas.
===============================================
Функция рисования текста в библиотеке AggPas принимает параметр PAnsiChar. Мне интересно, как я могу использовать PAnsiChar, чтобы указывать на текст, содержащий ангстрем (Å)?
SetThreadLocale($0409); // No difference.
ShowMessage(Chr(0197)); // Correct
ShowMessage(AnsiString(Chr(0197))); // Wrong - question mark
ShowMessage(PAnsiChar(Chr(0197))); // AV
ShowMessage(UTF8String(Chr(0197))); // Correct
ShowMessage(UTF8Encode(Chr(0197))); // Correct
ShowMessage(RawByteString(Chr(0197))); // Wrong - question mark
ShowMessage(AnsiString(UTF8String(Chr(0197)))); // Wrong - question mark
ShowMessage(AnsiString(UTF8Encode(Chr(0197)))); // Correct
ShowMessage(RawByteString(UTF8String(Chr(0197)))); // Wrong - question mark
ShowMessage(RawByteString(UTF8Encode(Chr(0197)))); // Correct
ShowMessage(PAnsiChar(AnsiString(UTF8Encode(Chr(0197))))); // Wrong - strange character
ShowMessage(PAnsiChar(RawByteString(UTF8Encode(Chr(0197))))); // Wrong - strange character
Для вашего удобства процедура DrawTextCenterAligned в следующем коде не может выводить букву ангстрема.
unit u2DRenderEngine_aggpas;
interface
uses
u2DRenderEngine, uMathVector3D,
agg_2D,
Graphics, IniFiles, Types;
type
T2DRenderEngine_aggpas = class;
T2DRenderEngine_aggpas = class(T2DRenderEngine)
private
fFontBMP: TBitmap;
fVG: Agg2D;
protected
function GetActualStringBoundingBox(aText: string; aFont: TFont)
: TRect; override;
public
constructor Create;
destructor Destroy; override;
procedure AttachBMP(aBMP: TBitmap; flip_y: Boolean);
procedure Flush; override;
procedure DrawLine(aP, bP: TPoint3D; aPen: TPen); override;
procedure DrawCircle(Center: TPoint3D; Radius: Extended;
R, G, B: Integer); override;
procedure FillCircle(Center: TPoint3D; Radius: Extended;
R, G, B: Integer); override;
procedure DrawPolygon(aPts: TAPoint3D; R, G, B: Integer); override;
procedure FillPolygon(aPts: TAPoint3D; R, G, B: Integer); override;
procedure DrawTextLeftAligned(aLeft: TPoint3D; aText: string; aFont: TFont;
clearBackground: Boolean); override;
procedure DrawTextCenterAligned(aCenter: TPoint3D; aText: string;
aFont: TFont; clearBackground: Boolean); override;
end;
implementation
uses
u2DUtils_Vcl, SysUtils, Math;
{ TRenderEngine_2D_aggpas }
constructor T2DRenderEngine_aggpas.Create;
begin
inherited;
fFontBMP := TBitmap.Create;
fFontBMP.Width := 2;
fFontBMP.Height := 2;
fVG.Construct;
end;
destructor T2DRenderEngine_aggpas.Destroy;
begin
inherited;
end;
procedure T2DRenderEngine_aggpas.AttachBMP(aBMP: TBitmap; flip_y: Boolean);
var
tmpBuffer: pointer;
tmpStride: Integer;
begin
if aBMP.Empty then
raise Exception.Create('AttachBMP: aBMP is Empty!');
if aBMP.PixelFormat <> pf32bit then
raise Exception.Create('AttachBMP: aBMP should be 32bit!');
tmpStride := Integer(aBMP.ScanLine[1]) - Integer(aBMP.ScanLine[0]);
if tmpStride < 0 then
tmpBuffer := aBMP.ScanLine[aBMP.Height - 1]
else
tmpBuffer := aBMP.ScanLine[0];
if flip_y then
tmpStride := tmpStride * -1;
fVG.attach(tmpBuffer, aBMP.Width, aBMP.Height, tmpStride);
end;
procedure T2DRenderEngine_aggpas.Flush;
begin
end;
procedure T2DRenderEngine_aggpas.DrawLine(aP, bP: TPoint3D; aPen: TPen);
begin
fVG.line(aP.X, aP.Y, bP.X, bP.Y);
end;
procedure T2DRenderEngine_aggpas.DrawCircle(Center: TPoint3D; Radius: Extended;
R, G, B: Integer);
begin
fVG.lineColor(R, G, B);
fVG.noFill;
fVG.ellipse(Center.X, Center.Y, Radius, Radius);
end;
procedure T2DRenderEngine_aggpas.FillCircle(Center: TPoint3D; Radius: Extended;
R, G, B: Integer);
begin
fVG.fillColor(R, G, B);
fVG.noLine;
fVG.ellipse(Center.X, Center.Y, Radius, Radius);
end;
procedure T2DRenderEngine_aggpas.DrawPolygon(aPts: TAPoint3D; R, G, B: Integer);
var
Len, I: Integer;
poly: array of double;
begin
Len := Length(aPts);
SetLength(poly, Len * 2);
for I := 0 to Len - 1 do
begin
poly[2 * I] := aPts[I].X;
poly[2 * I + 1] := aPts[I].Y;
end;
fVG.lineColor(R, G, B);
fVG.noFill;
fVG.polygon(@poly[0], 4);
end;
procedure T2DRenderEngine_aggpas.FillPolygon(aPts: TAPoint3D; R, G, B: Integer);
var
Len, I: Integer;
poly: array of double;
begin
Len := Length(aPts);
SetLength(poly, Len * 2);
for I := 0 to Len - 1 do
begin
poly[2 * I] := aPts[I].X;
poly[2 * I + 1] := aPts[I].Y;
end;
fVG.fillColor(R, G, B);
fVG.noLine;
fVG.polygon(@poly[0], 4);
end;
procedure T2DRenderEngine_aggpas.DrawTextLeftAligned(aLeft: TPoint3D;
aText: string; aFont: TFont; clearBackground: Boolean);
var
tmpRect: TRect;
tmpRectWidth, tmpRectHeight: Integer;
tmpPt: TPoint3D;
begin
tmpRect := GetActualStringBoundingBox(aText, aFont);
tmpRectWidth := tmpRect.Right - tmpRect.Left;
tmpRectHeight := tmpRect.Bottom - tmpRect.Top;
tmpPt.X := aLeft.X;
tmpPt.Y := aLeft.Y - tmpRectHeight;
if clearBackground then
begin
fVG.fillColor(255, 255, 255);
fVG.noLine;
fVG.Rectangle(tmpPt.X, tmpPt.Y, tmpPt.X + tmpRectWidth,
tmpPt.Y + tmpRectHeight);
end;
// Font & Colors
fVG.fillColor(0, 0, 0);
fVG.noLine;
fVG.TextHints(True);
if Agg2DUsesFreeType then
fVG.Font(PAnsiChar(AnsiString(UTF8Encode(LowerCase(aFont.Name) + '.ttf'))),
Abs(aFont.Height))
else
fVG.Font('Arial', 40.0);
// Text
fVG.Text(tmpPt.X, tmpPt.Y + tmpRectHeight, PAnsiChar(AnsiString(aText)));
end;
procedure T2DRenderEngine_aggpas.DrawTextCenterAligned(aCenter: TPoint3D;
aText: string; aFont: TFont; clearBackground: Boolean);
var
tmpRect: TRect;
tmpRectWidth, tmpRectHeight: Integer;
tmpPt: TPoint3D;
begin
tmpRect := GetActualStringBoundingBox(aText, aFont);
tmpRectWidth := tmpRect.Right - tmpRect.Left;
tmpRectHeight := tmpRect.Bottom - tmpRect.Top;
tmpPt.X := aCenter.X - tmpRectWidth / 2.0;
tmpPt.Y := aCenter.Y - tmpRectHeight / 2.0;
if clearBackground then
begin
fVG.fillColor(255, 255, 255);
fVG.noLine;
fVG.Rectangle(tmpPt.X, tmpPt.Y, tmpPt.X + tmpRectWidth,
tmpPt.Y + tmpRectHeight);
end;
// Font & Colors
fVG.fillColor(0, 0, 0);
fVG.noLine;
fVG.TextHints(True);
if Agg2DUsesFreeType then
fVG.Font(PAnsiChar(AnsiString(UTF8Encode(LowerCase(aFont.Name) + '.ttf'))),
Abs(aFont.Height))
else
fVG.Font('Arial', 40.0);
// Text
fVG.Text(tmpPt.X, tmpPt.Y + tmpRectHeight, PAnsiChar(AnsiString(aText)));
end;
function T2DRenderEngine_aggpas.GetActualStringBoundingBox(aText: string;
aFont: TFont): TRect;
var
tmpRectWidth, tmpRectHeight: Integer;
begin
Self.fFontBMP.Canvas.Font.Assign(aFont);
tmpRectWidth := Self.fFontBMP.Canvas.TextWidth(aText);
tmpRectHeight := Self.fFontBMP.Canvas.TextHeight(aText);
// 2011-03-07 hard-coded
tmpRectWidth := Ceil(tmpRectWidth * 1.05);
// 2011-03-07 hard-coded
tmpRectHeight := Ceil(tmpRectHeight * 0.70);
FillChar(Result, SizeOf(Result), 0);
Result.Right := tmpRectWidth;
Result.Bottom := tmpRectHeight;
end;
end.