首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用AggPas绘制angstrom?

如何使用AggPas绘制angstrom?
EN

Stack Overflow用户
提问于 2011-03-09 19:48:03
回答 2查看 563关注 0票数 1

Summarization:

在找到如何使用AggPas库输出angstrom符号方面没有任何进展。

===============================================

AggPas库中的文本绘图函数接受一个PAnsiChar参数。我想知道如何使用PAnsiChar来指向包含angstrom ( am )的文本?

代码语言:javascript
复制
  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过程不能输出angstrom字母。

代码语言:javascript
复制
    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.
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-09 19:58:14

如果这个单位把它的输入作为PAnsiChar,你就完了。除非您的系统上的默认代码页能够编码该字符,否则根本无法将该信息放入ANSI CHAR中。如果这样的编码是可用的,那么现在显示问号的所有例程都会显示正确的字符。

略长的信息:

Unicode是编码大量的字符,包括所有的文字,特殊的符号,音符,空间是如此广阔,有编码的克林贡字符!Ansi字符是使用表查找进行编码的,该表查找将可以用一个字节编码的值映射到所选的Unicode字符。

当使用AnsiString时,一次只能使用少于256个Unicode字符。当您尝试将一个Unicode字符编码为一个AnsiString时,您实际上将在代码页表中进行查找,寻找指向原始Unicode字符的代码。如果没有这样的编码是可用的,你会得到著名的问号!

下面是一个将字符串转换为UTF8字符串并将其作为AnsiString返回的例程(所有UTF8实际上都是有效的,但没有意义-- AnsiString):

代码语言:javascript
复制
function Utf8AsAnsiString(s:string):AnsiString;
var utf8s:UTF8String;
begin
  utf8s := UTF8String(s);
  SetLength(Result, Length(utf8s));
  if Length(utf8s) > 0 then
    Move(utf8s[1], Result[1], Length(utf8s));
end;

您可以传递此函数的结果,并希望该单元能够处理UTF8。祝好运。

票数 2
EN

Stack Overflow用户

发布于 2011-03-09 20:02:42

您需要一个中间步骤将char加载到字符串中,如下所示:

代码语言:javascript
复制
const
  ANGSTROM = Chr(0197);

procedure ShowAngstrom;
var
  message: AnsiString;
begin
  message := ANGSTROM;
  ShowMessage(PAnsiChar(message));
end;

编辑:,这里是一个猜测,如果这对AggPas不起作用,问题可能是什么。

我不熟悉AggPas,但我使用了Asphyre图形库,它的文本绘图系统,早在Unicode时代,就要求你通过给它一个字体文件和一系列可以打印的字符来生成一个专门的位图。(从那以后,我不确定它是否有所改进;我已经有一段时间没有使用它了。)任何超出这个范围的字符都无法正确打印。

如果AggPas以类似的方式工作,那么您所拥有的字体图像可能不包含任何针对197个字符的内容,因此无论您的Delphi代码多么正确,它都没有什么可映射的,您也无法获得正确的输出。看看你能不能证实这一点。

如果不是..。然后我就没有想法了,我希望这里的其他人更熟悉这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5251317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档