Skip to main content

How to change a font style?

In this example we use the Entities.dxf file. You can find it in the CAD VCL package. As you can see, the text "English Русский Französisch François" has the Arial font style.

Entities.dxf

Entities.dxf by default

TsgDXFText gets its font from the DXF text style (TsgDXFStyle), so it is possible to change a font for a certain TsgDXFStyle.

  1. Add DXF to the uses section.
uses
... DXF;

  1. Create a procedure to change the font style. Declare its parameters:
    • ATargetImage: TsgDXFImage is the CAD file with the text you want to change.
    • AStyleName, ANewFontName, ANewPrimaryFontName: string are parameters of the corresponding TsgDXFStyle properties.
procedure ChangeStylePrimaryFont(ATargetImage: TsgDXFImage;
const AStyleName, ANewFontName, ANewPrimaryFontName: string);

  1. Declare the local variables vDXFStyle and vDXFText. Specify TsgDXFStyle and TsgDXFText as their types respectively. Also, declare the counter I of the Integer type.
var
I: Integer;
vDXFStyle: TsgDXFStyle;
vDXFText: TsgDXFText;
  1. Define vDXFStyle. Set the FontName and PrimaryFont properties. Use the Loads method to fill the internal data of the entity to prepare it for drawing.
begin
vDXFStyle := ATargetImage.Converter.StyleByName(AStyleName);
if not Assigned(vDXFStyle) then
Exit;
vDXFStyle.FontName := ANewFontName;
vDXFStyle.PrimaryFont := ANewPrimaryFontName;
ATargetImage.Converter.Loads(vDXFStyle);
  1. Create a cycle to go through the entities and find the text. Then change its font style.
  begin
if ATargetImage.CurrentLayout.Entities[I] is TsgDXFText then
begin
vDXFText := TsgDXFText(ATargetImage.CurrentLayout.Entities[I]);
if vDXFText.Style.Name = vDXFStyle.Name then
begin
ATargetImage.Converter.Loads(vDXFText);
end;
end;
end;
  1. Finally, use the GetExtents method to recalculate drawing extents.
 ATargetImage.GetExtents();

You can see the font style changes in the following picture.

Entities.dxf

Entities.dxf after the changes

The text style named 'STANDARD' is added by default to any DWG/DXF drawing created with CAD VCL, and it uses the font 'txt.shx'. The given example changes the font style to Courier New (cour.ttf).

You have created the procedure to change a font style.

Note

CAD VCL is able to display non-Latin characters but the font you set should support them. If you choose a font that doesn't support these characters originally, the texts in your file will be shown in Arial.

The full code listing.

procedure ChangeStylePrimaryFont(ATargetImage: TsgCADImage;
const AStyleName, ANewFontName, ANewPrimaryFontName: string);
var
I: Integer;
vDXFStyle: TsgDXFStyle;
vDXFText: TsgDXFText;
begin
vDXFStyle := ATargetImage.Converter.StyleByName(AStyleName);
if not Assigned(vDXFStyle) then
Exit;
vDXFStyle.FontName := ANewFontName;
vDXFStyle.PrimaryFont := ANewPrimaryFontName;
ATargetImage.Converter.Loads(vDXFStyle);
for I := 0 to ATargetImage.CurrentLayout.Count - 1 do
begin
if ATargetImage.CurrentLayout.Entities[I] is TsgDXFText then
begin
vDXFText := TsgDXFText(ATargetImage.CurrentLayout.Entities[I]);
if vDXFText.Style.Name = vDXFStyle.Name then
begin
ATargetImage.Converter.Loads(vDXFText);
end;
end;
end;
ATargetImage.GetExtents();
end;
...
FChangeStylePrimaryFont(DWGImage, 'STANDARD', 'Courier New', 'cour.ttf');