SolidCode / SolidPython

A python frontend for solid modelling that compiles to OpenSCAD
1.1k stars 171 forks source link

use() functionality to import fonts #165

Open rockstorm101 opened 3 years ago

rockstorm101 commented 3 years ago

This is somewhat a feature request more than an issue really as I guess I'm attempting something a bit borderline here. According to OpenSCAD docs one should be able to use fonts "importing" them like: use <ttf/super-font/my-super-font-in-italics.ttf>

However, attempting such thing through SP via solid.use() does not work.

Traceback (most recent call last):
  File "/tmp/test/venv/lib/python3.9/site-packages/solid/objects.py", line 856, in use
    contents = scad_file_path.read_text()
  File "/usr/lib/python3.9/pathlib.py", line 1256, in read_text
    return f.read()
  File "/usr/lib/python3.9/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x92 in position 16: invalid start byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/tmp/src/test.py", line 15, in <module>
    sp.use("../aux/awesome-font-bold.ttf")
  File "/tmp/test/venv/lib/python3.9/site-packages/solid/objects.py", line 858, in use
    raise Exception(f"Failed to import SCAD module '{scad_file_path}' with error: {e} ")
Exception: Failed to import SCAD module '../aux/awesome-font-bold.ttf' with error: 'utf-8' codec can't decode byte 0x92 in position 16: invalid start byte 
etjones commented 3 years ago

Hm. So in this situation, the correct SolidPython translation would be just to pass through a use <PATH.*tf> string? I'm not in love with that design choice on OpenSCAD's part ("Let's just use use<> for any interaction with the filesystem!"), but SolidPython's is not to reason why.

I think this is an entirely reasonable feature to request and I think it's probably pretty straightforward to add. My work is a little crazy at the moment so this may not happen right away, but I'll look to take care of this in the next release. Thanks for submitting!