Author Topic: Font file issues  (Read 9884 times)

0 Members and 1 Guest are viewing this topic.

MeasureUp

  • Bull Frog
  • Posts: 462
Font file issues
« on: February 06, 2011, 06:28:47 PM »
I try to create a new text style when working on client's drawings.
Here is my code

Code: [Select]
(command "._-style" "Arial" "arial.ttf" "0.0" "1.0" "0.0" "No" "No")

Then I have an error message:

Quote
Command: (command "._-style" "Arial" "arial.ttf" "0.0" "1.0" "0.0" "No" "No")
._-style Enter name of text style or [?] <STANDARD>: Arial
New style.
Specify full font name or font filename (TTF or SHX) <txt>: arial.ttf
Font file doesn't exist.
Command: 0.0 Unknown command "0".  Press F1 for help.

Command: 1.0 Unknown command "0".  Press F1 for help.

Command: 0.0 Unknown command "0".  Press F1 for help.

Command: No Unknown command "NO".  Press F1 for help.

Command: No Unknown command "NO".  Press F1 for help.

Command: nil

AFAIK, all "ttf" fonts are resided in "c:/windows/fonts" directory.
I then explore the font foder & found a "arial_2.ttf" instead of "arial.ttf".
The strange thing is that the following returns "c:\\windows\\fonts\\arial.ttf".

Code: [Select]
(findfile "c:\\windows\\fonts\\arial.ttf")

I then did a couple of searches by using "findfile" method again & these are found
Quote
"c:\\windows\\fonts\\arial_0.ttf"
"c:\\windows\\fonts\\arial_1.ttf"
"c:\\windows\\fonts\\arial_2.ttf"
"c:\\windows\\fonts\\arial_3.ttf"
"c:\\windows\\fonts\\arial_4.ttf"
"c:\\windows\\fonts\\arial_5.ttf"
"c:\\windows\\fonts\\arial_6.ttf"
"c:\\windows\\fonts\\arial_7.ttf"
"c:\\windows\\fonts\\arial_8.ttf"
"c:\\windows\\fonts\\arial_9.ttf"

These font files do not exist in the "c:/windows/fonts" directory in fact.

Of course, I can rename the file name of "arial_2.ttf" to the original "arial.ttf".
But now my question is in the code how to check if the "arial.ttf" exists before creating the "arial" text style?

Thanks for your help.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Font file issues
« Reply #1 on: February 06, 2011, 06:50:15 PM »
Just to demonstrate a possible idea - not really a practical solution:

Code: [Select]
(defun CreateTextStyle ( style font / result )

  (defun _GetSpecialFolder ( name / wshShell specialFolders result )
    ; MP
   
    (vl-catch-all-apply
       '(lambda (  )
            (setq
                wshShell       (vlax-create-object "WScript.Shell")
                specialFolders (vlax-get wshShell 'SpecialFolders)
                result         (vlax-invoke specialFolders 'Item name)
            )             
        )
    )

    (if specialFolders (vlax-release-object specialFolders))
    (if wshShell (vlax-release-object wshShell))
   
    result
  )

  (if (setq font (findfile (strcat (_GetSpecialFolder "Fonts") "\\" font)))
    (vla-put-Fontfile
      (setq result
        (vla-Add
          (vla-get-TextStyles
            (vla-get-ActiveDocument (vlax-get-acad-object))
          )
          style
        )
      )
      font
    )
  )
  result
)

e.g.:

Code: [Select]
(CreateTextStyle "NewStyle" "Arial.ttf")
Note that if the Style specified exists, the font file for that style will be changed accordingly, else the Style will be created. Either way, if the fontfile is found, the Text Style will be returned.

Lee

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #2 on: February 06, 2011, 07:12:21 PM »
Thanks Lee.
Hope I haven't misunderstood what you explained.

Actually I can create a new style with any name.
In this case I am not worrying the style name but the font file to be used.
As I described previously, the code can lie by returning "c:\\windows\\fonts\\arial.ttf" when I use "findfile" method,
because the "arial.ttf" doesn't exist instead of "arial_2.ttf".
To ensure the font does exist, I need to put a checking function before I create the new style.

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Font file issues
« Reply #3 on: February 06, 2011, 08:25:30 PM »
I wasn't pertaining to the name, that was just a convenience of the code, rather that this...

Code: [Select]
(setq font (findfile (strcat (_GetSpecialFolder "Fonts") "\\" font)))
...works fine for me  :-)

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #4 on: February 06, 2011, 09:56:57 PM »
Sorry, maybe I have not expained clearly.
I want the code returning a result something like "arial.ttf is not found" if the "arial.ttf" does not exist in the "c:/windows/fonts" directory.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Font file issues
« Reply #5 on: February 06, 2011, 10:39:14 PM »
Well, arial.ttf should exist in the Font folder as arial.ttf ... Windows may report it as a different name since it is in a special folder (I don't know why they ever thought that was a good idea) ...

Have you tried using the fully qualified path to the font?

Code: [Select]
(command "._-style" "Arial" "c:\\windows\\fonts\\arial.ttf" "0.0" "1.0" "0.0" "No" "No")
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #6 on: February 06, 2011, 10:47:42 PM »
Well, arial.ttf should exist in the Font folder as arial.ttf ... Windows may report it as a different name since it is in a special folder (I don't know why they ever thought that was a good idea) ...

Have you tried using the fully qualified path to the font?

Code: [Select]
(command "._-style" "Arial" "c:\\windows\\fonts\\arial.ttf" "0.0" "1.0" "0.0" "No" "No")
Thanks, Keith.
Yes, I have tried & had the same result (the messgae: the "arial.ttf" doesn't exist.)

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Font file issues
« Reply #7 on: February 07, 2011, 07:44:31 AM »
Sorry, maybe I have not expained clearly.
I want the code returning a result something like "arial.ttf is not found" if the "arial.ttf" does not exist in the "c:/windows/fonts" directory.

You could easily add an 'else' expression printing a message of that nature to my current code if need be - my question would be: does my code work for you? i.e. does it return a TextStyle object?

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #8 on: February 07, 2011, 11:06:17 PM »
Sorry, Lee.
I tried but it doesn't work.

Quote
... does my code work for you? i.e. does it return a TextStyle object?

Lee Mac

  • Seagull
  • Posts: 12913
  • London, England
Re: Font file issues
« Reply #9 on: February 08, 2011, 08:17:09 AM »
Sorry, Lee.
I tried but it doesn't work.

Quote
... does my code work for you? i.e. does it return a TextStyle object?

What error do you get, if any?

Pepe

  • Newt
  • Posts: 87
Re: Font file issues
« Reply #10 on: February 08, 2011, 01:08:36 PM »
Hi...

Just to get registered Font Files (at least in Windows XP); after that you can check if your choice is a member of them.

Code: [Select]
(defun Get_TTFs (/ RegKey RegTTF TtfFil)
  (setq RegKey "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
RegTTF (vl-registry-descendents RegKey "")
)
  (foreach n RegTTF
    (setq TtfFil (cons (vl-registry-read RegKey n) TtfFil))
    )
  (reverse TtfFil)
  )

Regards from Spain  :-).

alanjt

  • Needs a day job
  • Posts: 5352
  • Standby for witty remark...
Re: Font file issues
« Reply #11 on: February 08, 2011, 02:10:35 PM »
Hi...

Just to get registered Font Files (at least in Windows XP); after that you can check if your choice is a member of them.

Code: [Select]
(defun Get_TTFs (/ RegKey RegTTF TtfFil)
  (setq RegKey "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
RegTTF (vl-registry-descendents RegKey "")
)
  (foreach n RegTTF
    (setq TtfFil (cons (vl-registry-read RegKey n) TtfFil))
    )
  (reverse TtfFil)
  )

Regards from Spain  :-).

Nice.

Code: [Select]
(defun GetTTFs (/)
  ((lambda (reg)
     (mapcar (function (lambda (n) (cons n (vl-registry-read reg n))))
             (vl-registry-descendents reg "")
     )
   )
    "HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
  )
)

Code: [Select]
(defun _filter (lst filter)
  (vl-remove-if-not (function (lambda (x) (wcmatch (strcase (car x)) (strcase filter)))) lst)
)

eg.
Code: [Select]
(_filter (GetTTFs) "*ARIAL*")
Civil 3D 2019 ~ Windohz 7 64bit
Dropbox

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #12 on: February 08, 2011, 05:12:05 PM »
...
What error do you get, if any?
Thanks to Lee, again.
Here is what I got:
Code: [Select]
#<VLA-OBJECT IAcadTextStyle 000000002b774a68>

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #13 on: February 08, 2011, 05:15:20 PM »
Thanks to Pepe.
Your code returns all fonts in the "fonts" folder.
It looks nice.
But I also need a filter to show only the 'arial.ttf".

MeasureUp

  • Bull Frog
  • Posts: 462
Re: Font file issues
« Reply #14 on: February 08, 2011, 05:23:20 PM »
Thanks, Alan.
Your code can return the family of "arial" fonts.
It looks a lot better.
How to improve the filter to show only "arial.ttf" if it exists?
Thanks again.