Need help setting font by code

Need help setting font by code

Timothy_BrooksU4G6R
Participant Participant
857 Views
9 Replies
Message 1 of 10

Need help setting font by code

Timothy_BrooksU4G6R
Participant
Participant

I could use a little help.

I have a client that wants us to switch out all the fonts on drawings to be Ture type font to make pdf's searchable. So I wrote a lisp routing changes ‘romans.shx’ to ‘romans_IV25.ttf.’

I am not getting any error with lisp, but it’s not working right.  So have two styles, one set by code and one set by the “Text Style” form. When I do an entget they both look the same…

 

Set by form:

style:((-1 . <Entity name: 208f1283d00>) (0 . STYLE) (330 . <Entity name: 208f0bcd830>) (5 . 3040) (100 .

AcDbSymbolTableRecord) (100 . AcDbTextStyleTableRecord) (2 . Standard_STD) (70 . 0) (40 . 0.0) (41 . 0.85) (50 . 0.0) (71 . 0) (42 . 0.2) (3 . romans_IV25.ttf) (4 . ))

 

Set by code:

style:((-1 . <Entity name: 208f0d51b30>) (0 . STYLE) (330 . <Entity name: 208f0bcd830>) (5 . 3A63) (100 . AcDbSymbolTableRecord) (100 . AcDbTextStyleTableRecord) (2 . STD) (70 . 0) (40 . 0.0) (41 . 0.85) (50 . 0.0) (71 . 0) (42 . 0.125) (3 . romans_IV25.ttf) (4 . ))

 

However when I open the ‘Text Style’ form, the one set by code is showing as missing font, even thought it's set to exact same file name.

 

ScreeenCapture....any help would be grateful.

0 Likes
858 Views
9 Replies
Replies (9)
Message 2 of 10

LDShaw
Collaborator
Collaborator

I had to do this. to set my arial.

(findfile (setq f (strcat (getenv "windir") "\\Fonts\\Arial.ttf")))

 

I was changing all my fonts to arial for better Revit compatibility.  

(if (findfile (setq f (strcat (getenv "windir") "\\Fonts\\Arial.ttf")))
    (progn
      (vlax-for s (vla-get-textstyles (setq doc (vla-get-activedocument (vlax-get-acad-object))))
        (vla-put-fontfile s f)
        (vla-put-width s 1.0)
        ;; RJP » 2020-04-29
        (vla-setfont s "Arial" :vlax-false :vlax-false 0 34)
      )
      (vla-regen doc acallviewports)
    )
  )

 

0 Likes
Message 3 of 10

pendean
Community Legend
Community Legend

@Timothy_BrooksU4G6R wrote:

...I have a client that wants us to switch out all the fonts on drawings to be Ture type font...


Are you sure your client wants to receive PDFs from you using a font they do not have?

and the same for all the users they opt to share those PDFs with?

 

Font substitutions in PDF viewers and editors are going to change the format of the text and look of your drawings on their computers.

 

Reach out to your client and confirm: I suspect they are not going to like what you are proposing here.

 

Best wishes.

0 Likes
Message 4 of 10

Timothy_BrooksU4G6R
Participant
Participant

True Type Font are Microsoft complain fonts, they are installed on every windows computer. When used that also make searchable pdf's. So yes the client will have this font.

0 Likes
Message 5 of 10

Timothy_BrooksU4G6R
Participant
Participant

I haven't got around to learn VisualLisp, still using the old school Autolisp...but here my code I am using...

 

		(findfile (setq f (strcat (getenv "windir") "\\Fonts\\" newFontName)))
			(princ (strcat "new font " f))
		(princ "\n")
		
		(entmod (subst (cons 3 f) (assoc 3 st) st))

now it's inserting this...

style:((-1 . <Entity name: 208efbca460>) (0 . STYLE) (330 . <Entity name: 208f0bcd830>) (5 . DB8E) (100 . AcDbSymbolTableRecord) (100 . AcDbTextStyleTableRecord) (2 . Border_Text) (70 . 0) (40 . 0.0) (41 . 0.9) (50 . 0.0) (71 . 0) (42 . 0.09375) (3 . C:\windows\Fonts\romans_IV25.ttf) (4 . ))

 

...and still shows as missing font, even thought that file exist.

0 Likes
Message 6 of 10

ec-cad
Collaborator
Collaborator

Curious that it 'finds' the Font file for one Style, but not the other ?

As for 'Microsoft' common fonts, I don't have that Font on my Windows 11 Pro, and AutoCAD 2021.

Here are a couple of .txt files, that show what's on my machine.

So, I would suggest changing those Styles to use "isocp2__.ttf".

See if that works for you.

 

ECCAD

 

0 Likes
Message 7 of 10

LDShaw
Collaborator
Collaborator
@ronjonp helped me with my code. Perhaps he'll come along and save the day.
0 Likes
Message 8 of 10

ronjonp
Mentor
Mentor

This worked for me to change the Romans.shx styles to RomanS_IV25.ttf

(if (findfile (setq f (strcat (getenv "windir") "\\Fonts\\Romans_IV25.ttf")))
  (progn (vlax-for s (vla-get-textstyles (setq doc (vla-get-activedocument (vlax-get-acad-object))))
	   (and (wcmatch (strcase (vla-get-fontfile s)) "*ROMANS*SHX*") (vla-put-fontfile s f))
	   ;; (vla-put-width s 1.0)
	   ;; RJP » 2020-04-29
	   ;; (vla-setfont s "Arial" :vlax-false :vlax-false 0 34)
	 )
	 (vla-regen doc acallviewports)
  )
)
0 Likes
Message 9 of 10

Timothy_BrooksU4G6R
Participant
Participant

I rewrote the code more visual lisp commands, a little of pain because I just don't know it very well. But ended working just fine....

(defun c:csf (/ f doc)

  ; map font file (old . new)
  (setq fontMap
        '(("ROMANS.SHX" . "romans_IV25.ttf")
		("TXT.SHX" . "txt_IV25.ttf")
		("ROMAND.SHX" . "romans_IV25.ttf")
          ("ARIAL.SHX" . "arial.ttf")          ; Example mapping
          ("TAHOMA.SHX" . "tahoma.ttf")
          
          ;; Add more mappings as needed
          ))

   ; map font name (old . new)
  (setq fontTypes
        '(("ROMANS.SHX" . "Romans_Iv25")
		("TXT.SHX" . "Txt_Iv25")
		("ROMAND.SHX" . "Romans_Iv25")
          ("ARIAL.SHX" . "Arial")          ; Example mapping
          ("TAHOMA.SHX" .  "Tahoma")
          
          ;; Add more mappings as needed
          ))

  (princ "\nRunning...") ;tell user your running
  
  (COMMAND "PURGE" "A" "*" "N");purge junk out of drawing
  
 (progn
      (vlax-for s (vla-get-textstyles (setq doc (vla-get-activedocument (vlax-get-acad-object)))) ;start a for loop for styles
	  
	(setq fontName (vla-get-fontfile s)) ;get existing font name

	(setq fontName (strcase fontName)) ;convert to all caps cuz lisp is case sensitive
	 (setq newFontName (cdr (assoc fontName fontMap))) ; Find the new TrueType font
	(setq newFont (cdr (assoc fontName fontTypes))) ;Find name 

	(if (and (not (null fontName))
	(setq fontName (strcase fontName))
             (wcmatch fontName "*.SHX")) ;if the existing font has .shx in it lets convert it

	 ;if a new font name is found do this
	  (progn
	(if newFontName
	  (progn
		(findfile (setq f (strcat (getenv "windir") "\\Fonts\\" newFontName)))	

       		 (vla-put-fontfile s f)
       		 (vla-put-width s 1.0)
        ;; RJP » 2020-04-29
        (vla-setfont s newFont :vlax-false :vlax-false 0 34)
	    );end progn
	 
	;else if not font found just make Arial
	 (progn
		(findfile (setq f (strcat (getenv "windir") "\\Fonts\\arial.ttf")))
	   (vla-put-fontfile s f)
       		 (vla-put-width s 1.0)
	   (vla-setfont s "Arial" :vlax-false :vlax-false 0 34)
	 );end progn
	);ebd if a new font name is found do this
	  
	);end first if progn
	
	);end frst if
      )
      (vla-regen doc acallviewports)
    )

  (princ)
)
(vl-load-com)
Message 10 of 10

Sea-Haven
Mentor
Mentor

I don't have "romans_IV25.ttf" either. Like ec-cad use a different font that is close. I am using Bricscad and maybe its added by Autocad install.

0 Likes