Thursday, November 30, 2023
Home3D Modeling3D Textual content and Fonts - Ruby API

3D Textual content and Fonts – Ruby API


So I attempted to get somewhat artistic this time round and use one thing aside from Arial as my default font in my Title Block software (which makes use of the add_3d_text technique) that’s a part of my Medeek Challenge extension:

It labored nice on my system (I’ve Microsoft Phrase put in), and naturally failed miserably on anybody who didn’t have this font put in.

The marginally annoying factor is that SketchUp’s error doesn’t actually let you know that the font is the problem, it simply fails to create the 3D textual content.

Relatively than laborious code in a choose few fonts that I do know everybody ought to have I feel it might be higher to have my plugin code decide what is out there on the customers system after which present these as choices. Clearly not all fonts work with this technique, so it might be extra sophisticated than simply enumerating all of the put in fonts on a Mac or PC.

How would I’m going about doing that? Does anybody have any expertise with doing this form of factor?


Up till now I’ve purposely restricted my plugins to a small set of fonts that I assumed have been commonplace throughout any platform:

Arial
Tahoma
Helvetica
Instances New Roman
Courier New
Verdana
Georgia
Palatino
Trebuchet MS

You would possibly permit the person so as to add or to or subtract from the record ?


On Home windows the filenames are:

fonts = Dir.glob("C:/Home windows/Fonts/*.ttf")

You’ll discover usually the italics have an “i” within the identify, daring a “b”, mild a “l”, and many others. with combos.



1 Like

Now I want to determine what to do for Mac customers.

Additionally the put in font names are enumerated within the Home windows registry.
You possibly can learn the entries utilizing the Fiddle I feel

The bottom line is right here:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionFonts



1 Like

I exploit this instance…

Observe the way it makes use of some constants like FOLDER and NAME which you’ll want to regulate to your arrange…
This instance is in search of a font identify “Txt”, with a file-name of “txt_____.ttf”, shipped with this specific customized extension, and which the person wants to put in first off…
It additionally defaults to “Arial” if the desired font isn’t discovered, so that you most likely wish to alter these too…
If works for PC & MAC, a MAC will crash if the font is lacking, a PC will easy do nothing in any respect !

### Font test
@@font = "Txt"
@@fontname = "txt_____.ttf"
@@txtfont = File.be part of(FOLDER, @@fontname)
if RUBY_PLATFORM.downcase =~ /mswin|mingw/ ### computer
	discover = false
	Dir.entries("C:/Home windows/Fonts").grep(/ttf$/).grep(/^txt/).every
		if e =~ /^txt_/
			discover = true
			break
		finish
		
	if discover
		#UI.messagebox("#{NAME}:nn'#{@@font}' font put in.")
	else ### lacking
		UI.messagebox("#{NAME}:nn'#{@@font}' font is NOT put in.nWithout it References will likely be in 'Arial' font.nInstall it and restart.nIf you wouldn't have permission, get an 'Admin' to put in it from:n#{@@txtfont}")
		@@font = "Arial"
	finish
else ### mac
	if discover = %x(mdfind -onlyin ~/Library/Fonts 'kMDItemKind  == "TrueType font" && kMDItemDisplayName  == "#{@@fontname}"')
	elsif discover = %x(mdfind -onlyin /Library/Fonts 'kMDItemKind  == "TrueType font" && kMDItemDisplayName  == "#{@@fontname}"')
	elsif discover = %x(mdfind -onlyin /Community/Library/Fonts 'kMDItemKind  == "TrueType font" && kMDItemDisplayName  == "#{@@fontname}"')
	elsif discover = %x(mdfind -onlyin /System/Library/Fonts 'kMDItemKind  == "TrueType font" && kMDItemDisplayName  == "#{@@fontname}"')
	elsif discover = %x(mdfind -onlyin /System Folder/Library/Fonts 'kMDItemKind  == "TrueType font" && kMDItemDisplayName  == "#{@@fontname}"')
	else
		discover=false
	finish
	if discover && !discover.empty?
		#UI.messagebox("#{@@font} font exists #{discover}.")
	else
		UI.messagebox("#{NAME}:nn'#{@@font}' font is NOT put in.nWithout it References will likely be in 'Arial' font.nInstall it and restart.nIf you wouldn't have permission, get an 'Admin' to put in it from:n#{@@txtfont}")
		@@font = "Arial"
	finish
finish



2 Likes

Truly a better means can be to make use of a backtick (aka %x) string to make use of Home windows’ reg.exe command line utility.

def get_fonts # MS Home windows
  fonts = []
  Dir.chdir(__dir__) do |path|
    %x[reg export "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionFonts" "fonts.txt" /y]
    sleep 0.5
    strains = File.readlines(
      'fonts.txt', chomp: true, encoding: 'UTF-16LE:UTF-8'
    )
    strains.every line
    places "Fonts names retrieved: #{fonts.dimension}"
    nil
  finish
  return fonts
finish

The reg.exe utility exports in .reg format which is importable again in any home windows registry. So I drive a .txt extension. Additionally it writes in UTF-16 Little Endian w/ Bit Order Mark encoding, so we should be certain to make use of this exterior encoding for IO strategies.



1 Like

Right here is an excellent easier method that does not use an exterior program,
… nor want to jot down an exterior font record file:

module SomeAuthor
  module Fonts

    require 'win32/registry'

    lengthen self

    MACHINE ||= Win32::Registry::HKEY_LOCAL_MACHINE
    FONTKEY ||="SOFTWAREMicrosoftWindows NTCurrentVersionFonts"
    
    def get_fonts
      MACHINE.open(FONTKEY) do |fontkey|
        fontlist = []
        fontkey.each_value 
          identify.delete_suffix!(' (TrueType)')
          fontlist << identify 
        
        fontlist
      finish
    finish ###

  finish # submodule Fonts
finish # toplevel namespace



1 Like

Okay, I’m am going to do this one and see if I could make it work.



1 Like

I wrote it as a library module, however might wrap all of it up into 1 technique …

    def get_fonts
      require 'win32/registry' if !outlined?(::Win32) || !outlined?(::Win32::Registry)
      Win32::Registry::HKEY_LOCAL_MACHINE.open(
        'SOFTWAREMicrosoftWindows NTCurrentVersionFonts'
      ) do |fontkey|
        fontlist = []
        fontkey.each_value 
          identify.delete_suffix!(' (TrueType)')
          fontlist << identify 
        
        fontlist
      finish
    finish ### get_fonts

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments