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.
TsgDXFText gets its font from the DXF text style (TsgDXFStyle), so it is possible to change a font for a certain TsgDXFStyle.
- Add
DXFto theusessection.
uses
... DXF;
- Create a procedure to change the font style. Declare its parameters:
ATargetImage: TsgDXFImageis the CAD file with the text you want to change.AStyleName, ANewFontName, ANewPrimaryFontName: stringare parameters of the correspondingTsgDXFStyleproperties.
procedure ChangeStylePrimaryFont(ATargetImage: TsgDXFImage;
const AStyleName, ANewFontName, ANewPrimaryFontName: string);
- Declare the local variables
vDXFStyleandvDXFText. SpecifyTsgDXFStyleandTsgDXFTextas their types respectively. Also, declare the counterIof theIntegertype.
var
I: Integer;
vDXFStyle: TsgDXFStyle;
vDXFText: TsgDXFText;
- Define
vDXFStyle. Set theFontNameandPrimaryFontproperties. Use theLoadsmethod 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);
- 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;
- Finally, use the
GetExtentsmethod to recalculate drawing extents.
ATargetImage.GetExtents();
You can see the font style changes in the following picture.
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.
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');