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

3D Textual content and Fonts – Ruby API


So I attempted to get slightly artistic this time round and use one thing apart from Arial as my default font in my Title Block instrument (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.

Quite than onerous code in a choose few fonts that I do know everybody ought to have I feel it could be higher to have my plugin code decide what is obtainable on the customers system after which present these as choices. Clearly not all fonts work with this technique, so it could be extra difficult 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 type of factor?


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

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

You may permit the consumer so as to add or to or subtract from the listing ?


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 so forth. 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 may learn the entries utilizing the Fiddle I feel

The secret’s 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 searching for a font identify “Txt”, with a file-name of “txt_____.ttf”, shipped with this explicit customized extension, and which the consumer wants to put in first off…
It additionally defaults to “Arial” if the desired font is just not discovered, so that you most likely wish to modify 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 examine
@@font = "Txt"
@@fontname = "txt_____.ttf"
@@txtfont = File.be a part of(FOLDER, @@fontname)
if RUBY_PLATFORM.downcase =~ /mswin|mingw/ ### laptop
	discover = false
	Dir.entries("C:/Home windows/Fonts").grep(/ttf$/).grep(/^txt/).everye	
	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 don'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 don't have permission, get an 'Admin' to put in it from:n#{@@txtfont}")
		@@font = "Arial"
	finish
finish



2 Likes

Really a neater manner could 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
    traces = File.readlines(
      'fonts.txt', chomp: true, encoding: 'UTF-16LE:UTF-8'
    )
    traces.every line
    places "Fonts names retrieved: #{fonts.measurement}"
    nil
  finish
  return fonts
finish

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



1 Like

Here’s a a good less complicated strategy that does not use an exterior program:

module SomeAuthor

  require 'win32/registry'

  module Fonts

    lengthen self

    MACHINE ||= Win32::Registry::HKEY_LOCAL_MACHINE

    FONTKEY ||="SOFTWAREMicrosoftWindows NTCurrentVersionFonts"
    
    def get_fonts
      MACHINE.open(FONTKEY) do |fontkey|
        fontlist = []
        fontkey.each_value identify, kind, information
        fontlist
      finish
    finish ###

  finish # submodule Fonts

finish # toplevel namespace

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments