Text overlap after converting DWG to PNG

Hello,
Using the Python version of aspose cad 24.4 and the code below to convert DWG to PNG, the text overlaps as shown in the following figure. How can I avoid this?
result.jpg (192.9 KB)

import aspose.cad as cad
from aspose.cad.imageoptions import PngOptions


image = cad.Image.load("example.dwg")

cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()

options = PngOptions()
options.vector_rasterization_options = cadRasterizationOptions

image.save("result.png", options)

This is the relevant files
relatedFiles.zip (6.1 MB)

@ALEX1228,
Hi.
This happens because the text in the initial file is written with SHX fonts. Some of them have TTF alternatives and they are applied automatically, but some are not. This file contains a lot of different SHX fonts and it is possible to apply them separately at export. Here is the example how to do it and how to print names of fonts used in text styles in the initial file:

import aspose.cad as cad
from aspose.cad.imageoptions import PngOptions
from aspose.pycore import cast

image = cad.Image.load("example.dwg")

cad_image = cast(cad.fileformats.cad.CadImage, image)

for style in cad_image.styles:
    print(style.primary_font_name, style.big_font_name)
    
cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()
cadRasterizationOptions.shx_fonts =[
    "path_to_fonts\HZTXT.shx",
    "path_to_fonts\STEDI.shx",
    "path_to_fonts\SIMPLEX.shx",
    "path_to_fonts\COMPLEX.shx",
    "path_to_fonts\tssdeng.shx",
    "path_to_fonts\romanc.shx",
    ]

options = PngOptions()
options.vector_rasterization_options = cadRasterizationOptions

image.save("result.png", options)

result.jpg (151.5 KB)

Please note that not all fonts used in the file were applied in this example.

When I put it on production it gave me the following error.

Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'System.Text.Encoding.CodePages, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
   at Aspose.WrpGen.Interop.ExchangeHost.InitServices()

I reinstalled the Aspose.CAD package, and then this issue was resolved. Thank for your help.

@ALEX1228,
you are welcome :slight_smile:

Hello,
When I use the STEDI.shx font, most of the text turns into question marks, like this???
result.zip (2.4 MB)

. Could you please tell me how you resolved this issue?

@ALEX1228,
I used the example provided above to generate the attached file. Please try to use all fonts from my example and make sure that path to SHX files is correct.

yeah, I had used all fonts, but still didn’t get the result as you did.
relatedFiles.zip (3.7 MB)

@ALEX1228,
I used absolute paths for local testing:

cadRasterizationOptions.shx_fonts =[
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\HZTXT.shx",
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\STEDI.shx",
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\SIMPLEX.shx",
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\COMPLEX.shx",
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\tssdeng.shx",
    "C:\Program Files\Autodesk\AutoCAD 2019\Fonts\romanc.shx",
   ]

and this appears to be important. Please give us some time to check this better.

I also attempted using the absolute path, but the outcome remained unchanged. Could it be that I didn’t install AutoCad and use its fonts?

"C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\HZTXT.SHX",
    "C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\STEDI.shx",
    "C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\SIMPLEX.shx",
    "C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\COMPLEX.shx",
    "C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\tssdeng.shx",
    "C:\\Users\\Dell\\PycharmProjects\\DXF2IMGS\\path_to_fonts\\romanc.shx"

@ALEX1228,
OK, so it seems the issue appears because there are different versions of hztxt.shx :slight_smile: My file works fine, e.g., it can be found here.
The final code sample has just minor changes, e.g., loading fonts from folder:

import aspose.cad as cad
from aspose.cad.imageoptions import PngOptions
from aspose.pycore import cast
import os

image = cad.Image.load("example.dwg")

cad_image = cast(cad.fileformats.cad.CadImage, image)

cadRasterizationOptions = cad.imageoptions.CadRasterizationOptions()

base_path = "fonts\\"
fonts = []
for filename in os.listdir(base_path):
    path = base_path + filename
    fonts.append(path)

print(fonts)
cadRasterizationOptions.shx_fonts = fonts

options = PngOptions()
options.vector_rasterization_options = cadRasterizationOptions

image.save("result.png", options)

We can not process the hztxt.shx file from your sample project correctly at the moment. You can try to use it too, but loading of the file should be a bit different:

loadOptions = cad.LoadOptions()
loadOptions.specified_encoding = cad.CodePages.SIMP_CHINESE
image = cad.Image.load("example.dwg", loadOptions)

and the result for this font has incorrect spacing. We have created CADPYTHON-30 to investigate this better and improve export of this font file.

1 Like

It works and the result is good. Really appreciate you help. :smiling_face_with_three_hearts:

1 Like

@ALEX1228,
happy to hear it worked :slight_smile:

1 Like