Can you get a list of OSX fonts somehow?
-
I managed to get a list to manually test all the fonts [via copy/paste]... they all work
but I can't work out how I can have each as a group, or how even how to get SU to just make them...macFonts.each_with_index do |item, index| puts %(Sketchup.active_model.entities.add_3d_text("#{item}" , TextAlignCenter, "#{item}" , true, false, 1.0, 0.0, #{index}, true, 0.05)) end
works as a puts which I can copy/paste in batches to make, but fails as a direct method
macFonts.each_with_index do |item, index| Sketchup.active_model.entities.add_3d_text("#{item}" , TextAlignCenter, "#{item}" , true, false, 1.0, 0.0, #{index}, true, 0.05) end
...why???
john -
Oh it's just a head-smackin' boo-boo.
Thez
arg takes aNumeric
and theindex
is already aNumeric
subclass (Integer
,)
so you do not use the#{
...}
string replacement syntax.
(But you don't want to stack them vertically, move them -Y so they are all in a column (see below.)Another quirk about
add_3d_text()
, is that unlike the OEM tool, the Ruby method does not put the primitives into a group.So we encourage you to create group(s) then call the method on the group's
Entities
collection:macFonts.each_with_index do |item, index| grp = Sketchup.active_model.entities.add_group() grp.entities.add_3d_text( "#{item}", TextAlignCenter, "#{item}", true, false, 1.0, 0.0, 0.0, true, 0.05 ) grp.name= item grp.move!( Geom;;Transformation.new(Geom;;Vector3d.new(0,-index,0)) ) end
-
cheers dan,
This is a one off, so dosen't really matter, but out of curiousity
as both 3DText and Group generation can lock up the mac, how can I split this into a batch mode, so SU makes first 12, then next 12, then next 12, etc... but still uses the original index for position.
Could I need to make a hash using -index%(:#{item}) then split into new arrays of hashes with max items(12), then process those, I was looking atmacFonts.each_with_index do |item, index| puts %(#{item}, #{index}).to_a end
and then
# use as array.chunk class Array def chunk(pieces=2) len = self.length; mid = (len/pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start+mid last = last-1 unless len%pieces >= i chunks << self[start..last] || [] start = last+1 end chunks end end
then
macFonts.chunk 12
then process those, but I do get lost... john
-
Hi dan,
@unknownuser said:
It is very bad to modify base classes. (spanky spanky)
it a one off, and the stand alone version was more complex for me to use...
# use as standalone function def chunk_array(array, pieces=2) len = array.length; mid = (len/pieces) chunks = [] start = 0 1.upto(pieces) do |i| last = start+mid last = last-1 unless len%pieces >= i chunks << array[start..last] || [] start = last+1 end chunks end
I also do most my testing in a separate version of SU with Pluggins turned off, and often check if methods are defined already first...
that works at the about the same speed as the first, not too slow, considering there are 297 fonts.
what i was musing, was, is there a way to have SU make one, return true, make next, return true, then next, etc...
so each operation is a single font... To see if that's actually faster or not. In theory it should be slower, but SU appears to bog down exponentially when I do them manually in sets of 6 V 12 V 18 V 24, and over 24 takes longer to run then the whole lot from either of your scripts.Another mac 3D Font oddity I have noticed:
when using the native tool, I can make
""
as 3D text,
which should be the apple icon although it may show as the windows icon on yours.If I use code to try and generate the 2D Text (from console or a webdialog) although I see 'it' in the input window, I get some bizarre encoded string.
cheers again
john -
@driven said:
what i was musing, was, is there a way to have SU make one, return true, make next, return true, then next, etc... so each operation is a single font...
Set
chunk = 1
@driven said:
..., but SU appears to bog down exponentially when I do them manually in sets of 6 V 12 V 18 V 24, and over 24 takes longer to run then the whole lot from either of your scripts.
It's a caveat of using high quality. A lot of primitives are going onto the undo stack.
The higher the number of elements in the model, the slower things get.
I think we already tried saving the model between operations (to clear the undo stack,) BUT tests showed it did not speed things up.@driven said:
... when using the native tool, I can make
""
as 3D text,
which should be the apple icon although it may show as the windows icon on yours.If I use code to try and generate the 2D Text (from console or a webdialog) although I see 'it' in the input window, I get some bizarre encoded string.
It could be a unicode UTF16 codepoint. Ruby is UTF8.
There are sometimes tricks you can do with
Array.pack
andString.unpack
.Code-wise, try to save .rb files in "UTF8 (without BOM)" aka "ANSI as UTF8" encoding. I don't know what your Mac code editor calls it, but that's what Notepad++ does.
-
@dan rathbun said:
It could be a unicode UTF16 codepoint. Ruby is UTF8.
I often use them as puts on mac only scripts to remind me it won't work on a PC, they show up in console returns, so that side is ok, also I can make 3D Text versions with the native tool (which I thought was based on Todd's 3D TextTool ruby)
WebDialogs are UTF8 as well and they show up htere in most fonts [some don't have the charset]@unknownuser said:
There are sometimes tricks you can do with
Array.pack
andString.unpack
.I looked into .encode to see if I could add a conditional to make one from code, i'll look into .pack... cheers
@unknownuser said:
Code-wise, try to save .rb files in "UTF8 (without BOM)" aka "ANSI as UTF8" encoding. I don't know what your Mac code editor calls it, but that's what Notepad++ does.
everything 'ruby' on the mac is "UTF8 (without BOM)" with unix line endings, even 'pasteBoard', 'Terminal' and 'Applescript editor' are since OSX 10.5.
It's only some downloaded rubies that need changing, although if it's just line endings any will work in SU.
john -
-
It is very bad to modify base classes. (spanky spanky)
This should do the job:
chunksize = 12 chunk = 1 limit = macFonts.length model = Sketchup.active_model fsize = 1.0 linespacing = 1.2 bold = true italic = false thick = 0.05 filled = true quality = 0.0 i = 0 while i < limit begin # model.start_operation("3D Fontnames (#{chunk})") # chunksize.times do |n| # break if i == limit # item = macFonts[i] grp = model.entities.add_group() grp.entities.add_3d_text( "#{item}", TextAlignCenter, "#{item}", bold, italic, fsize, quality, 0.0, filled, thick ) grp.name= item grp.move!( Geom;;Transformation.new(Geom;;Vector3d.new(0,-(i*linespacing),0)) ) # i += 1 # end # chunk # model.commit_operation() # rescue Exception => e puts("\n*** macFonts group Error! ***") puts(" i = #{i}") puts(" chunk = #{chunk}") puts(" font = #{macFonts[i]}\n") model.abort_operation() puts("Error #<#{e.class.name}; #{e.message}>") puts(e.backtrace) if $VERBOSE raise end chunk += 1 end # while
EDIT: Added a few Exception messages, and option vars before iteration.
EDIT(2): Addedlinespacing
var. -
EDIT(2) previous code post: Added
linespacing
var. -
cheers again,
linespacing is a nice touch.
all the code runs, returns nil. then draws view with the new groups, changing chuncksize doesn't alter that.
it doesn't take too long.
only two fonts don't fully form, may be the size, but not bad really.so, the main purpose was to check that the osascript fonts were the right one, and they are...
and I learnt a little ruby
thanks dan
john -
It occurs to me that because the fontsize is only 1 inch, that many arc segments are ridiculously small, perhaps overloading the SketchUp engine.
Try setting:
fsize = 1.0.feet linspacing = 1.25.feet
-
Yeh, I tried that, didn't make a lot of difference,
I use 100 and 120fsize
needs to be in inches, doesn't it? -
-
I missed the dot... oops should stick to copy/paste
-
I tried this:
<span class="syntaxdefault"><br />t</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">Time</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">now<br /> macFonts</span><span class="syntaxkeyword">=[]<br /> </span><span class="syntaxdefault">macFonts</span><span class="syntaxkeyword">=(`</span><span class="syntaxstring">osascript -e 'tell application "Font Book" to set macFonts to name of every font family'</span><span class="syntaxkeyword">`).</span><span class="syntaxdefault">split</span><span class="syntaxkeyword">(</span><span class="syntaxstring">","</span><span class="syntaxkeyword">).</span><span class="syntaxdefault">uniq</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each </span><span class="syntaxkeyword">{|</span><span class="syntaxdefault">a</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">strip</span><span class="syntaxkeyword">! if </span><span class="syntaxdefault">a</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">respond_to</span><span class="syntaxkeyword">? ;</span><span class="syntaxdefault">strip</span><span class="syntaxkeyword">! }.</span><span class="syntaxdefault">sort<br /> quitFontBook</span><span class="syntaxkeyword">=(`</span><span class="syntaxstring">osascript -e 'tell application "Font Book" to quit'</span><span class="syntaxkeyword">`)<br /> if </span><span class="syntaxdefault">macFonts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length </span><span class="syntaxkeyword">> </span><span class="syntaxdefault">1<br /> then quitFontBook<br /> end<br /> macFonts</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">length<br />puts Time</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">now</span><span class="syntaxkeyword">-</span><span class="syntaxdefault">t<br /></span>
On the first run it yielded 8.500629 seconds.
The next was 2.422791 and 2.286048 seconds.
If I comment out the code the closes Font Book the times are 0.241677 and 0.228576.
So the overhead of launching Font Book is just all too high.
-
but you only need to run once, and write the 'Users' personal font-list to file [save in TT-Lib]
if the User instals new fonts [very rare] they could delete the file and make a new one, or that could be an option?
john -
That could be a fallback. But I'm still hoping to find a method that works quickly which I can use. the fc-list is fast - it's just that I cannot get the output from that. ..and for some reason I cannot seem to direct the output to a file... Maybe one can trigger a new console command that might actually be able to write to file?
-
But this is bringing us full circle
You might as well try to use something like:
fonts_file="\"path/to/fonts.txt\"" system(
fc-list : file family | grep /Library > fonts_file)
Or something similar ? #{fonts_file} ??... Whatever can be made to work !
Or even a more convoluted
UI.openURL('file:///'+commandfilepath)
And then read the ttf-font names from the fonts_fileThere is a simple solution [KISS]...
In my own [far less sophisticated] '2dText' tool and some of my similar tools like 'AreaTextTag', which make 3dText as '2d flat editable text', my drop-down-list of the available fonts is simply a combined list of some ttf fonts that are usually shipped with recent PCs A_N_D MACs.
For the reduced list in the AreaTextTag it a simple array of a reduced list, typed in the code that the user can customize. In the more wide-ranging 2dText tool it's read in a simple txt text file ['2DtextTool_Fonts.txt' in the tool's subfolder] of the 44 common font-names - the user can edit this [there are instructions in the tool's Help] - either to remove unwanted ones [e.g. in case he doesn't use 'Wingdings' very much], or to add custom one he has also installed on his computer [like 'Architext' or 'Consolas'] that are not in the default combo list... This allows the user to have their own customized fonts list and it's not that hard to alter/update/customize either... -
Other option(s):
(a) Use
UI.openpanel
to let users manually pick font files, that are then added to the plugin's fontlist (kept as a hash file in the plugin's folder. The file distro's with a base set of standard Mac fonts.)(b) Use the SDK and C/C++/Objective-C to get the list of Fonts Sketchup knows.
(From memory, I do think Sketchup's known fonts is exposed in the SDK.)
-
I've found the KISS Applescript version, just figuring out if it's easier to strip the results first or in ruby.
the basic can then run as osascript...set search1 to do shell script "mdfind -onlyin /Library/Fonts 'kMDItemContentTypeTree = \"public.truetype-ttf-font\"'" as text set search2 to do shell script "mdfind -onlyin ~/Library/Fonts 'kMDItemContentTypeTree = \"public.truetype-ttf-font\"'" as text
results look like this
%(#0000FF)[/Library/Fonts/NISC18030.ttf
/Library/Fonts/Kai.ttf
/Library/Fonts/Hei.ttf
/Library/Fonts/BiauKai.ttf
/Library/Fonts/Apple LiSung Light.ttf
/Library/Fonts/Apple LiGothic Medium.ttf
/Library/Fonts/Microsoft/Lucida Console.ttf
/Library/Fonts/Microsoft/Candara Bold Italic.ttf]also, I think if a plugins is offering font manipulation for artistic usage, it should have all the 'SU' fonts and let the User decide which to use
john
Advertisement