How do I render text along an arc in an image?

2019-08-05 16:56发布

问题:

I have the following code in Delphi 7 to draw Copyright text along the curved edge of a DVD. We are using an old version of Graphics32.

We are switching to Delphi XE5 with the latest code from Graphics32 and this code no longer compiles; in particular LoadArcCurve and drawingBuffer.RenderFittedText no longer exists as methods.

procedure TCDLabel.DrawCopyrightText(const drawingBuffer: TBitmap32Ex);
var
  FixedPointArray : TArrayOfFixedPoint;
  Center : TFixedPoint;
  vAngle1 : double;
  vAngle2 : double;
  radius : integer;
  CopyrightText : string;
  textColor : TColor32;
begin
  radius := (fImageSize div 2) - 30;
  UpdateTextTransform(8{2.3}, drawingBuffer);
  Center.x := GR32.Fixed(fImageSize div 2);
  Center.y := GR32.Fixed(fImageSize div 2);
  vAngle1 := DegToRad(-130);
  textColor := clWhite32;
  vAngle2 := DegToRad(0);
  LoadArcCurve(Center, GR32.Fixed(radius), GR32.Fixed(radius), vAngle1, vAngle2, FixedPointArray);
  CopyrightText := Format('%s %s Dystopia Unlimited. All rights reserved.', [GetCopyrightSymbol, fCopyrightYears]);
  drawingBuffer.RenderFittedText(CopyrightText, textColor, pdoAntialising or pdoFilling, FixedPointArray);
  FixedPointArray := NIL;
end; {DrawCopyrightText}

I have the following code snippet in Delphi XE5 using the latest Graphic32 code and have attempted various other similar methods without success.

canvas := TCanvas32.Create(drawingBuffer); // drawingBuffer is a TBitmap32
try
  canvas.Brushes.Add(TStrokeBrush);
  canvas.Brushes[0].Visible := TRUE;
  (canvas.Brushes[0] as TStrokeBrush).StrokeWidth := 2;
  (canvas.Brushes[0] as TStrokeBrush).FillColor := clWhite32;

  canvas.Path.BeginPath;
  canvas.Path.Arc(Center, -130, 0, radius);
  canvas.Path.EndPath;
  TextToPath(drawingBuffer.Font.Handle, canvas.Path, FloatRect(0, 0, fImageSize, fImageSize), CopyrightText);

All of the examples in the new Graphics32 that I can find appear to draw directly onto a Delphi control canvas while I am required to draw onto a TBitmap32.

How do I render text along an arc in an image/bitmap using Delphi XE5 and the latest version of Graphics32?

回答1:

I think that the best way to implement what you describe is to use Angus Johnson's excellent extension to graphics32, GR32_Text.



回答2:

Using the pointer to Angus Johnson's work that David Heffernan provided, the following code is the solution to my question.

This code uses units: GR32_Lines, GR32_Text, GR32_Misc as well as others. It also does not protect memory or do any of the other protective processes required for releasable code.

procedure DrawCopyrightText(const drawingBuffer: TBitmap32);
var
  fixedPointArray : TArrayOfFixedPoint;
  CopyrightText : string;
  ttFont : TTrueTypeFont;
  text32 : TText32;
  i: integer;
  polyPolyPts: TArrayOfArrayOfArrayOfFixedPoint;
begin
  CopyrightText := Format('%s %s Dystopia Unlimited. All rights reserved.', [GetCopyrightSymbol, fCopyrightYears]);

  text32 := TText32.Create;
  ttFont := TTrueTypeFont.Create(COPYRIGHT_FONT_NAME, COPYRIGHT_FONT_SIZE);
  fixedPointArray := GetArcPoints(FloatRect(30, 30, 2370, 2370), -140, 0);
  polyPolyPts := text32.GetEx(fixedPointArray, CopyrightText, ttFont, aLeft, aMiddle, true, 2);
  for i := 0 to high(polyPolyPts) do
    if length(polyPolyPts[i]) > 0 then
      SimpleFill(drawingBuffer, polyPolyPts[i], clWhite32, clWhite32);
end;