Coder Social home page Coder Social logo

Comments (4)

hyvyys avatar hyvyys commented on July 17, 2024 1

For anyone interested, since flPinPoint has no __slots__ or __dict__ and seems basically immutable, I used regex to recreate the anchors. Kudos to FL developers for creating friendly APIs...

This is how I cleared all anchor, advance width and sidebearing expressions across masters in selected glyphs:

#FLM: Clear All Expressions

import re
from typerig.proxy import pFont

def clearExpressions():
  font = pFont()
  
  for glyph in font.selected_pGlyphs():
    for layer in glyph.masters():
      glyph.setLSBeq("", layer.name)
      glyph.setRSBeq("", layer.name)
      glyph.setADVeq("", layer.name)
      
      anchors = []
      for anchor in layer.anchors:
        x = re.search("(x=)(-?\d+)",str(anchor)).group(2);
        y = re.search("(y=)(-?\d+)",str(anchor)).group(2);
        name = re.search("(objectName=')([^']+)'",str(anchor)).group(2);
        anchors.append({ 'x': int(x), 'y': int(y), 'name': name });

      layer.clearAnchors();
      for a in anchors:
        glyph.addAnchor((a['x'], a['y']), a['name'], layer.name)
    glyph.update()

  fontlab.Update(fontlab.CurrentFont())
  print 'cleared expressions'

clearExpressions()

from typerig.

hyvyys avatar hyvyys commented on July 17, 2024 1

Thanks. I usually try to learn by listing the properties of an object using dir e.g. print dir(anchor). I could swear it didn't work yesterday, but apparently it does work.

This would be the more sane version of the code then:

#FLM: Clear All Expressions

import re
from typerig.proxy import pFont

def clearExpr():
  font = pFont()
  
  for glyph in font.selected_pGlyphs():
    for layer in glyph.masters():
      glyph.setLSBeq("", layer.name)
      glyph.setRSBeq("", layer.name)
      glyph.setADVeq("", layer.name)
      for anchor in layer.anchors:
        anchor.expressionX = ""
        anchor.expressionY = ""
    glyph.update()

  fontlab.Update(fontlab.CurrentFont())
  print 'cleared expressions'

clearExpr()

from typerig.

kateliev avatar kateliev commented on July 17, 2024

hey @hyvyys thank you for your patience and interesting solution :)

flPinPoint() is the correct anchor object in FL 6&7. In fact flAnchor does nothing (or at least nothing that has interest to us) :) Moving anchors around (setting .x or .y) is done using the .point property of the flPinPont obejct. The .point property is of QtCore.QPointF class so you could set your anchors as follows:

import fontlab as fl6
import fontgate as fgt
import PythonQt as pqt

from typerig.proxy.fl import *

font = pFont()
g = eGlyph()

a = g.anchors()[0]
a.point = pqt.QtCore.QPointF(10,10) # Will move the anchor to (10,10)
g.updateObject(g.fl)

Also do not forget that we have all of these already implemented in TypeRig, thus the eGlyph() object has the following methods (direct copy from the code):

def dropAnchor(self, name, layer, coordTuple, alignTuple=(None,None), tolerance=5, italic=False):
		'''Drop anchor at given layer
		Args:
			name (str): Anchor Name
			layer (int or str): Layer index or name, works with both
			coordTuple (int, int): New anchor coordinates or auto aligment offsets*
			alignTuple (str,str): New anchor aligment*
			tolerance (int): Outline feature auto detection tolerance*

		*Aligment rules: (width, height)
			- (None,None) - Uses coordinates given
			- width - (L) Left; (R) Right; (A) Auto Bottom with tolerance; (AT) Auto Top with tolerance; (C) Center;
			- height - (T) Top; (B) Bottom; (C) Center;
		Returns:
			None
		'''

def moveAnchor(self, name, layer, coordTuple=(0,0), alignTuple=(None,None), tolerance=5, italic=False):
	'''Move anchor at given layer
	Args:
		name (str): Anchor Name
		layer (int or str): Layer index or name, works with both
		coordTuple (int, int): New anchor coordinates or auto aligment offsets*
		alignTuple (str,str): New anchor aligment*
		tolerance (int): Outline feature auto detection tolerance*

	*Aligment rules: (width, height)
		- (None,None) - Uses coordinates given
		- width - (L) Left; (R) Right; (A) Auto Bottom with tolerance; (AT) Auto Top with tolerance; (C) Center;
		- height - (T) Top; (B) Bottom; (C) Center;
	Returns:
		flPinPoint
		'''

Also the base pGlyph() object has:

def anchors(self, layer=None):
		'''Return list of anchors (list[flAnchor]) at given layer (int or str)'''
		
def addAnchor(self, coordTuple, name, layer=None, isAnchor=True):
	'''	Adds named Anchor at given layer.
	Args:
		coordTuple (tuple(float,float)): Anchor coordinates X, Y
		name (str): Anchor name
		layer (int or str): Layer index or name. If None returns ActiveLayer
		isAnchor (bool): True creates a true flAnchor, False ? (flPinPoint)
	Returns:
		None
	'''
def newAnchor(self, coordTuple, name, anchorType=1):
	'''	
	Creates new anchor
	Args:
		coordTuple (tuple(float,float)): Anchor coordinates X, Y
		name (str): Anchor name
		anchorType: 
	Returns:
		flPinPoint
	'''
def clearAnchors(self, layer=None):
	'''Remove all anchors at given layer (int or str)'''

def findAnchor(self, anchorName, layer=None):
	'''Find anchor by name/tag'''

def findAnchorCoords(self, anchorName, layer=None):
	'''Find anchor coordinates by name/tag '''

Note: pGlyph() object is proxy glyph object, while eGlyph() object is and extension of pGlyph(), thus you just need to import eGlyph()

PS: Feel free to explore the TR code and find solutions to your problems. Every object and function is fully documented inside the code - i just don't have time to export it as external API

from typerig.

kateliev avatar kateliev commented on July 17, 2024

Also acessing anchor expressions in flPinPoint is done trough .expressionX, .expressionY and we have the following in eGlyph():

def exprAnchor(self, name, layer, expression_x=None, expression_y=None):
		'''Set anchor expressions at given layer
		Args:
			name (str): Anchor Name
			layer (int or str): Layer index or name, works with both
			expression_x (str): Expression for evaluating anchors X coordinate
			expression_Y (str): Expression for evaluating anchors Y coordinate

		Returns:
			flPinPoint
		'''

from typerig.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.