Skip to content

Commit

Permalink
Improve demo script
Browse files Browse the repository at this point in the history
* make sure there is a spacecenter to draw in
* make sure we handle spaces with discrete locations better

Add note about settig the previewlocation in the docs
  • Loading branch information
LettError committed Dec 12, 2023
1 parent 3e463f3 commit c1f0c65
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
10 changes: 10 additions & 0 deletions DesignspaceEditor2.roboFontExt/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,16 @@ <h3>Making a glyph</h3>
What is happening here? Step by step: <b>CurrentDesignspace</b> returns a UFOOperator object for the current designspace.
<b>d.newDefaultLocation()</b> creates a dict representing the default location in design coordinates. For this example it is a quick way to get a valid location, but you will probably want to see more exotic locations. Then, <b>makeOneGlyph</b> creates a new glyph object for the given glyphName and a location dict. The result is a <b>MathGlyph</b> object that can be converted to a regular RoboFont glyph object.

<h3>Setting the preview location</h3>

DSE2 keeps track of the "current preview location". You can set this location with the UI and axis sliders in the Preview Window. But you can also set it with a script.

<pre><code>
d = CurrentDesignspace()
previewLoc = d.randomLocation()
d.setPreviewLocation(previewLoc)</code></pre>


<h3>Going through all designspaces</h3>

You can use <code>AllDesignspaces()</code> to get a list of all open designspace documents. Optionally you can pass a font object with <code>usingFont</code> to get only the designspaces that use that font.
Expand Down
43 changes: 29 additions & 14 deletions Scripting examples/previewFont_to_spacecenter.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,48 @@
from fontTools.designspaceLib import InstanceDescriptor
from mojo.UI import CurrentSpaceCenter
from mojo.UI import CurrentSpaceCenter, OpenSpaceCenter

# Scripting with live designspaces
# demo erik@letterror.com 8.12.2023
# demo erik@letterror.com 12.12.2023

# have a desigspace open in DSE2
d = CurrentDesignspace()

# have a spacecenter open
sp = CurrentSpaceCenter()
ds = CurrentDesignspace()

# you probably want more control over what axis values to look at
# and maybe even a fancy interface
# but that is beyond the scope of this demo
# so for now, a random location
loc = d.randomLocation()
loc = ds.randomLocation()
# the location is just a dict with axisName: axisValue pairs.
print("location", loc)

# we can split this random location into its continuous and discrete parts
continuousPart, discretePart = ds.splitLocation(loc)
print("continuous part of the location:", continuousPart)
print("discrete part of the location:", discretePart)

# see the location is just a dict with axisName: axisValue pairs,
print(loc)
# get the default font
# note we ask for the default of a specific discrete location here
defaultFont = ds.findDefaultFont(discreteLocation=discretePart)

# make an "instance descriptor" object to specify what we want to see
preview = InstanceDescriptor()
#preview.familyName = "MyPreview"
#preview.styleName = "MyStyle"
preview.designLocation = loc
preview.familyName = defaultFont.info.familyName

# now we're asking the designspace to make a font object with these specs
r = d.makeInstance(preview)
print(r)
# This makes the whole font. If you're just looking at a couple of characters
# there are faster ways to do that.
previewFont = ds.makeInstance(preview).asFontParts()

# finally, we give the resulting font to the space center.
sp.setFont(r)
# have a spacecenter open
sp = CurrentSpaceCenter()
if sp is None:
sp = OpenSpaceCenter(previewFont)

#
sp.setFont(previewFont)
sp.setRaw(f'HHOHOO \n {defaultFont.info.styleName}')

# optionally, open the font window
#previewFont.openInterface()

0 comments on commit c1f0c65

Please sign in to comment.