Compare two lists (comparing fontName before/after)

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Compare two lists (comparing fontName before/after)

Post by thatkeith » Sun Mar 25, 2018 11:08 pm

I'm working with 'start using font', and it's actually helping tremendously now that my copy of Suitcase (the font management utility) is glitching a little with the current Mac OS. But I'm having trouble getting a reliable and efficient way of referencing the font I start using.

The fontNames function returns a very bizarrely-ordered list of every single typeface (plus the LC "(Default), (Menu), (Message)" etc. items), and the notes for 'start using font' say "To determine the name of the newly loaded font, compare the value of the fontName before and after loading the font."

First, what's the most efficient way to compare two lists to find the differences?

Second, where's the right place to make feature requests? There really should be a way to get the newly-activated font name/s returned directly as the result of using the 'start using font' command.

Puzzled k
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7024
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by FourthWorld » Sun Mar 25, 2018 11:17 pm

I'm a bit slow this afternoon: if you're looking for the name of a font you explicitly loaded with the "start using" command, wouldn't you know its name?
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 6261
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Compare two lists (comparing fontName before/after)

Post by dunbarx » Mon Mar 26, 2018 12:48 am

Assuming that you need this anyway, or more generally, "how does one compare two lists?", the answer can be tricky.

For example, as you noted, the list returned from asking for the "fontNames" is oddly ordered. When you start or stop using a font, the revised list is also oddly ordered, and ostensibly different. Sorting each list might help as a general rule.

How to compare is up to you. You could arrange each list in an array and use the intersect command, or loop through one list and see if a corresponding element exists in the other. Whatever. The tricky part is trusting that the elements of each list are intact. This has caused me problems in the past, where tiny changes in one element may throw everything off.

Or on, if you see what I mean. Anyway, if you practice a bit with two arbitrary lists, you should soon see the best way forward.

Craig Newman

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7024
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by FourthWorld » Mon Mar 26, 2018 2:04 am

With v9 getting the difference between two arrays is now a one-liner - here's a function built around it, with an example mouseUp handler to call it:

Code: Select all

on mouseUp
   put the fontnames into t1
   put t1 into t2
   repeat 10
      delete any line of t2
   end repeat
   --
   put ListDiff( t1, t2)
end mouseUp


function ListDiff pList1, pList2
   split pList1 by cr and null
   split pList2 by cr and null
   difference pList1 with pList2
   put the keys of pList1 into pList1
   sort lines of pList1
   return pList1
end ListDiff
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by thatkeith » Mon Mar 26, 2018 9:40 am

One line!? Wow – that just clinched it! I'm downloading the latest RC for 9 right now. Thanks! :)

As for knowing the name... if only it was that simple! It's all a bit like the White Knight's song in Alice through the Looking Glass; the name of the font, what it's called and what it *is* can be quite different things. (For example, a font file could be called "Funk Family complete.otf", contain a number of typefaces referred to as "Funk", one of which could be called "Funk Italic" or perhaps "Funk Oblique", or "Funk-Oblique", or...)

It would be a tremendous help if LC returned the actual system-referenceable font names of the fonts activated through the 'start using font' command. And as a typographer, I'd love to see it distinguish and note the actual style names as well as the font family name itself. :lol:
The White Knight, excerpt:
'The name of the song is called Haddocks' Eyes.'
'Oh, that's the name of the song, is it?' Alice said, trying to feel interested.
'No, you don't understand,' the Knight said, looking a little vexed. 'That's what the name is called. The name really is The Aged Aged Man.'
'Then I ought to have said "That's what the song is called"?' Alice corrected herself.
'No, you oughtn't: that's quite another thing! The song is called Ways and Means: but that's only what it's called, you know!'
'Well, what is the song, then?' said Alice, who was by this time completely bewildered.
'I was coming to that,' the Knight said. 'The song really is A-Sitting On A Gate: and the tune's my own invention.'
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7024
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by FourthWorld » Tue Mar 27, 2018 6:58 am

A mess indeed. I had no idea. I wonder if LiveCode Builder could tap into the APIs needed to build the sort of function you're looking for.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by thatkeith » Tue Mar 27, 2018 9:14 am

I wonder too. But I'm a bit stuck trying to find out anything about LCB... I got here from a search: https://livecode.com/topic/livecode-bui ... reference/ and the main content is unscrollable both in Safari and Chrome. Perhaps I've arrived at the page in some unexpected way – but it's pretty bad news for the page to behave like that. :(
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by thatkeith » Tue Mar 27, 2018 9:40 am

The example hander was really helpful btw! I've adjusted it and got that side of things working really well, thanks. Onward and upward! :)

/* Aside:
Re the mess that is font naming, there is of course a logical structure but it's rather complex. Styles are normally the expected italic, bold, regular, bold italic... but they can be essentially anything at all. In Fontographer, styles are named design parameters, and these can be individual or combined choices from width (45 listed options), weight (three dozen options), slope (nine options), and 'Other' (28 options and the ability to write in your own).
I've stirred this mud myself: I made a font based on a folded paper lettering design one of my students, Constanca, created for a conference. One variation has 'cut' effects simulating the shadowed overlaps of the paper folds. This style I called 'Cut' and the other is 'Solid', so in the Font menu the typeface is Constanca Solid and Constanca Cut. Also, many years ago I made a typeface for friends as their wedding present. The lighter weight's style name was 'Jean' and the heavier was 'Steve' – because logic. ;)
In short, font name structure is logical but convoluted, and style names can't be guessed with 100% accuracy, even by resorting to lookup table comparisons.
*/

k
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3811
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Compare two lists (comparing fontName before/after)

Post by richmond62 » Tue Mar 27, 2018 10:18 am

the main content is unscrollable both in Safari and Chrome.
That's not good: over "here" with Firefox (outdated version) on Mac OS 10.7.5 it scrolls well: but I fail to see the relevance of LCB to the font listing 'problem'.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3811
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Compare two lists (comparing fontName before/after)

Post by richmond62 » Tue Mar 27, 2018 10:42 am

Messing around on LiveCode 8.1.8 I started by looking at something I haven't played with since about 2002:

revFontLoad

presumably (?) that's how you are going to 'start using' a font. You are not very clear on this point.

Now, were I to use revFontLoad and I was slightly "schizo" insofar as I didn't
know what font my revFontLoad was loading, or, more to the point,
I wanted my end-user, chummy, to know which font was being used . . .

or, just possibly, I wanted to ensure that revFontLoad was "strutting it's funky stuff" . . .

I'd do something like this:

Code: Select all

on openCard
  put the textFont of fld "ff" into fld "fontInUse"
  revFontLoad "Charcoal.ttf"
  put " / " & the textFont of fld "ff" after fld "fontInUse"
end openCard
which would yield only "/" as loading a font DOES NOT set the textFont of a fld to it.

However, at the risk of seeming naive, what is the point of this "start using font" 'thing' anyway?
It seems to be functionally identical to revFontLoad.

One of the rather obvious reasons you cannot find details of this in the 8.1.8 documentation is because
it isn't mentioned!

"start using font" IS documented it the LC 9.0 RC 1 documentation where it is claimed it was introduced in version 6.5.

'start using font' IS documented in the LC 7.1.4 documentation; so someone slipped up there.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3811
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Compare two lists (comparing fontName before/after)

Post by richmond62 » Tue Mar 27, 2018 10:56 am

AND, just to "p*ss someone off" I set up a stack with one button and a scrolling list field
called "fl".

I put this code into the button:

Code: Select all

on mouseUp
   put the fontNames into fld "fl"
   if "Devawriter" is among the lines of fld "fl" then
      put "Yup"
      end if
end mouseUp
and that worked....

So, I'd thought I'd have a spot of fun . . . and popped this into my button:

Code: Select all

on mouseUp
   revFontLoad "/Volumes/STORAGE_2/Free Fonts/Happy Clean/TTF/Happy Party.ttf"
   put the fontNames into fld "fl"
   if "Happy Party" is among the lines of fld "fl" then
      put "Yup"
      end if
end mouseUp
AND, that worked as well :D

So, as I was on a roll I went "one step beyond the happy, happy monster show" with this:

Code: Select all

on mouseUp
   start using font file "/Volumes/STORAGE_2/Free Fonts/Happy Clean/TTF/Happy Party Alt 2.ttf"
   put the fontNames into fld "fl"
   if "Happy Party Alt 2" is among the lines of fld "fl" then
      put "Yup"
      end if
end mouseUp
AND, that worked as well :lol:

Of course your "before and after" requirements can ALL be worked out using the above.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3811
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Compare two lists (comparing fontName before/after)

Post by richmond62 » Tue Mar 27, 2018 11:05 am

There's no need for any reliance on a listField, of course:

Code: Select all

on mouseUp
   start using font file "/Volumes/STORAGE_2/Free Fonts/Happy Clean/TTF/Happy Party Alt 2.ttf"
   put the fontNames into FONTZ
   if FONTZ contains "Happy Party Alt 2" then
      put "Yup"
      end if
end mouseUp

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by thatkeith » Tue Mar 27, 2018 2:33 pm

richmond62 wrote:
Tue Mar 27, 2018 10:18 am
the main content is unscrollable both in Safari and Chrome.
That's not good: over "here" with Firefox (outdated version) on Mac OS 10.7.5 it scrolls well: but I fail to see the relevance of LCB to the font listing 'problem'.
Possibly no relevance at all. I started looking there because of what Richard said:
FourthWorld wrote:I wonder if LiveCode Builder could tap into the APIs needed to build the sort of function you're looking for.
and I thought I should see what I could grok about it before asking any questions along those lines. I didn't get anywhere, sadly, as the only pages I found were non-responsive. :-/
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

thatkeith
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 258
Joined: Mon Mar 01, 2010 7:13 pm
Location: London, UK
Contact:

Re: Compare two lists (comparing fontName before/after)

Post by thatkeith » Tue Mar 27, 2018 2:53 pm

richmond62 wrote:
Tue Mar 27, 2018 10:42 am
revFontLoad

presumably (?) that's how you are going to 'start using' a font. You are not very clear on this point.
Many apologies! I did actually mean the "start using font" command. I saw revFontLoad in the LC 8.1.9 dictionary but I presumed as that was very similar to a newer command but lacked the 'globally' adverb of the newer one, AND operating on the assumption that rev- stuff is a little more likely than average to be heading towards deprecated (I know, it is a bit of a hasty assumption :( ) I stuck with 'start using font'.

The 'globally' option is actually very helpful in that it switches from the faster-to-load but local-only font installing to making it available to all applications.
However, at the risk of seeming naive, what is the point of this "start using font" 'thing' anyway?
It seems to be functionally identical to revFontLoad.
The option to load a font just in the LC environment, for testing and perhaps generating previews (not globally), vs the slower process of loading a font for universal use (globally) is definitely attractive to me. I just wish it returned a clean list of the actual font names that it dealt with.

With grateful thanks to Richard (FourthWorld) for suggesting and giving a sample of LC9's 'difference' ability, this is my very basic proof of concept for loading, previewing and unloading fonts, using a list-style field that contains a full-path list of some typeface files.

Code: Select all

on mouseUp
   get the hilitedLine of me
   if the textStyle of line it of me is empty then
      useThisFont
      set the textStyle of line it of me to bold
   else
      stop using font file line it of me
      set the textStyle of line it of me to empty
   end if
   set the hilitedLine of me to empty
end mouseUp

on useThisFont
   put the fontnames into tList1
   sort lines of tList1
   start using font file line the hilitedLine of me of me -- globally
   put the fontnames into tList2
   sort lines of tList2
   put ListDiff( tList1, tList2) into tNewFont
   set the textFont of field "sample" to tNewFont
   -- put tNewFont
end useThisFont

function ListDiff pList1, pList2
   split pList1 by cr and null
   split pList2 by cr and null
   difference pList2 with pList1
   put the keys of pList2 into pList2
   sort lines of pList2
   return pList2
end ListDiff
Clicking fast seems to trip up the connection between whether a line is bold or not and whether it's actually loaded, but that's not a concern right now.

k
Senior Lecturer, LCC (University of the Arts London)
Technical Editor, MacUser (1996-2015)
360 VR media specialist

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 3811
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Compare two lists (comparing fontName before/after)

Post by richmond62 » Tue Mar 27, 2018 6:39 pm

The option to load a font just in the LC environment
Indeed: but that does not answer why you eschew using revFontLoad.

Post Reply

Return to “Talking LiveCode”