Author Topic: Operate on selected glyphs  (Read 2943 times)

Ken Krugh

  • Hero Member
  • *****
  • Posts: 24
    • Email
Operate on selected glyphs
« on: 2012-12-02, 14:24:49 »
I'm currently using this code to create a collection of glyphs from what is selected in the font window:
Code: [Select]
f=fl.font
selectedGlyphs = [ f.glyphs[gIdx] for gIdx in range(len(f.glyphs)) if fl.Selected(gIdx) ]

My problem is that the glyph index order within the font (as the above code gets it) doesn't always match the order in which they appear on the screen and the order on the screen is what I want.

Is there a way to get the order in which things appear on the screen?

Thanks,
Ken
« Last Edit: 2012-12-02, 14:31:25 by Ken Krugh »

Adam Twardoch (FontLab)

  • Product and marketing manager, Fontlab Ltd.
  • Administrator
  • Hero Member
  • *****
  • Posts: 563
  • FontLab Studio 5.1.2, Mac OS X 10.6.8
    • FontLab
Re: Operate on selected glyphs
« Reply #1 on: 2012-12-02, 16:42:44 »
The following produces a list of glyph names in the order as shown in FontLab Studio's Font Window in the "Names mode".

Best,
Adam

Code: [Select]
#FLM: Print glyphnames in encoding order [SL]

def GlyphnamesByEncoding(f = fl.font):
  gnsByIndex = []
  for gi in range(len(f.glyphs)):
    g = f.glyphs[gi]
    gn = g.name
    gnsByIndex.append(gn)
  e = f.encoding
  el = len(e)
  gnsByEncoding = [""] * el
  for gn in gnsByIndex:
    ei = e.FindName(gn)
    if ei > -1:
      gnsByEncoding[ei] = gn
  gnsByEncoding = filter(lambda gn: gn != '', gnsByEncoding)
  gnsSorted = gnsByIndex
  gnsSorted.sort(key=(gnsByEncoding+gnsByIndex).index)
  return gnsSorted

f = fl.font
gns = GlyphnamesByEncoding(f)
print gns
Regards,
Adam Twardoch
Fontlab Ltd.

Ken Krugh

  • Hero Member
  • *****
  • Posts: 24
    • Email
Re: Operate on selected glyphs
« Reply #2 on: 2012-12-08, 11:06:52 »
Thanks for posting Adam.

Am I correct in thinking that this code kind of emulates the names mode by sorting the glyph names?

What exactally does .encoding return? When I first saw it I thought it might have been a collection of something (glyphs or glyph names) that were displayed in the window, but it doesn't appear to be that.

Oh, and related to this: I'd love to be able to select a unicode range from the encoding menu, run a script and have it set all the other fonts I have open that same view.

Thanks again,
Ken
« Last Edit: 2012-12-08, 11:12:13 by Ken Krugh »

Eigi

  • Beta: FontLab Studio Mac
  • Hero Member
  • ***
  • Posts: 100
    • e-font
Re: Operate on selected glyphs
« Reply #3 on: 2012-12-08, 11:49:30 »
Hello Ken,

An encoding is a data structure of its own. It is more or less a list of encoding records.
see:
http://www.e-font.de/flpydoc/html/Encoding.xml.html
and
http://www.e-font.de/flpydoc/html/EncodingRecord.xml.html

Best
Eigi

Adam Twardoch (FontLab)

  • Product and marketing manager, Fontlab Ltd.
  • Administrator
  • Hero Member
  • *****
  • Posts: 563
  • FontLab Studio 5.1.2, Mac OS X 10.6.8
    • FontLab
Re: Operate on selected glyphs
« Reply #4 on: 2012-12-09, 00:09:33 »
The font.enconding is always what you see as "yellow highlighted" in the Names mode, yes.
Regards,
Adam Twardoch
Fontlab Ltd.