mekkablue
(Rainer Erich Scheichelbauer)
November 2, 2022, 7:28am
21
OK, here is a snippet you can paste in Window > Macro Panel. Select the glyphs and run the code from the macro window. It will prompt you for a folder and then store the SVGs with glyph names and an .svg
suffix:
from AppKit import NSSize
GlyphAsImage = NSClassFromString("GlyphAsImage")
folder = GetFolder(allowsMultipleSelection=False)
if folder:
folderURL = NSURL.fileURLWithPath_(folder)
for layer in Font.selectedLayers:
preset = {
"active": True,
"name": "SVG",
"size": layer.ascender,
"factor": 1,
"format": "SVG"
}
layerSize = NSSize(layer.width, layer.ascender)
svgData = GlyphAsImage.svgDataWithLayer_origSize_settings_(layer, layerSize, preset)
svgFileName = layer.parent.name + ".svg"
svgURL = folderURL.URLByAppendingPathComponent_(svgFileName)
svgData.writeToURL_atomically_(svgURL, False)
(edit by @GeorgSeifert : I simplified the code a bit)
2 Likes
xyzajacf
(Filip Paldia)
March 13, 2023, 12:47pm
23
I am trying to figure out how to get SVG from GSinstance instead of selectedLayers.
I guess trying to use the filter will not work. Right?
Which direction should I look for?
mekkablue
(Rainer Erich Scheichelbauer)
March 14, 2023, 7:12am
24
Interpolate the layers and pass them through this one by one. Best to use an export plug-in.
1 Like
To get an interpolated layer:
instance = Font.instances[1]
interpolatedFont = instance.interpolatedFontProxy
interpolatedGlyph = interpolatedFont.glyphs["A"]
interpolatedLayer = interpolatedGlyph.layers[0]
1 Like
Hi Mekka,
Can I get a script that export the selected glyphs as png images?
I need to be able to select the image size (for example 512x512, or 716x716, or whatever)
I need to be able to draw a rectangle to represent the bbox
Ideally, a small line to represent also the baseline and the xheight (just a tiny little short line, for example from 0,0 to 0,10 on the baseline)
It all need to be centered inside the png image, having a little 10px margin arround it.
If you need more detail, get in touch with me via Skype.
Thanks!
SCarewe
(Sebastian Carewe)
March 16, 2023, 11:51pm
27
Why don’t you use DrawBot for this? You can use DrawBot straight from within Glyphs.
mekkablue
(Rainer Erich Scheichelbauer)
March 17, 2023, 12:10am
28
You can do that natively from Glyphs. Take a look at the PS and EPS export plugins.
dergraph
(Wolf Bо̄ese)
May 23, 2023, 12:42pm
29
What i use for the drawbot plugin to save as SVG
from robofab.world import CurrentFont, RGlyph
import GlyphsApp
height = 700
margin = 200
minMargin = 0
font = CurrentFont()
layers = GlyphsApp.Glyphs.font.selectedLayers
Scale = float(height) / float(font.info.unitsPerEm)
for l in layers:
g = RGlyph(layer=l)
print(g.name)
minX, minY, maxX, maxY = g.box
extraSpace = 0
offset = 0
offsetY = 0
offsetX = 0
extraSpace *= Scale
offset *= Scale
width = margin + (g.width * Scale) + margin + extraSpace
newPage(width, height + (2 * margin))
# Draw transparent square
fill(0, 0, 0, 0) # Set fill color with alpha (0 for transparency)
rect(0, 0, width, height + (2 * margin))
fill(0, 0, 0) # Set fill color to black
translate(margin + offsetY, margin + offsetX)
scale(Scale)
drawGlyph(g)
saveImage("export/" + g.name + ".svg")
2 Likes
I tried this macro panel script on glyphs/shapes that are using Native Color gradients, but most colors came out as flat colors, some with slight gradient (not same as design). Is it possible to retain the gradient data for the exported SVGs? Thanks!
Update: I used picosvg to do the job instead
thierryc
(Thierry Charbonnel)
April 21, 2024, 10:38am
31
Thank you very much! However, this macro write the ‘A’ file over the ‘a’ file on a case-insensitive Mac OS.
Introducing my small fix
Lowercase are prefixed now. Not perfect but…
Additionally, you have the option to save them in a different folder or modify the prefix as needed.
from AppKit import NSSize
GlyphAsImage = NSClassFromString("GlyphAsImage")
folder = GetFolder(allowsMultipleSelection=False)
if folder:
folderURL = NSURL.fileURLWithPath_(folder)
for layer in Font.selectedLayers:
preset = {
"active": True,
"name": "SVG",
"size": layer.ascender,
"factor": 1,
"format": "SVG"
}
layerSize = NSSize(layer.width, layer.ascender)
svgData = GlyphAsImage.svgDataWithLayer_origSize_settings_(layer, layerSize, preset)
svgFileName = layer.parent.name + ".svg"
if layer.parent.name.islower():
svgFileName = '_' + svgFileName
print("Lowercase name detected.")
svgURL = folderURL.URLByAppendingPathComponent_(svgFileName)
svgData.writeToURL_atomically_(svgURL, False)
thierryc
(Thierry Charbonnel)
April 21, 2024, 10:53am
32
Same fix to fix the a on A file on a case-insensitive Mac OS.
A small fix
Thank you very much for these scripts / macro.
from robofab.world import CurrentFont, RGlyph
import GlyphsApp
height = 700
margin = 200
minMargin = 0
font = CurrentFont()
layers = GlyphsApp.Glyphs.font.selectedLayers
Scale = float(height) / float(font.info.unitsPerEm)
for l in layers:
g = RGlyph(layer=l)
print(g.name)
minX, minY, maxX, maxY = g.box
extraSpace = 0
offset = 0
offsetY = 0
offsetX = 0
extraSpace *= Scale
offset *= Scale
width = margin + (g.width * Scale) + margin + extraSpace
newPage(width, height + (2 * margin))
# Draw transparent square
fill(0, 0, 0, 0) # Set fill color with alpha (0 for transparency)
rect(0, 0, width, height + (2 * margin))
fill(0, 0, 0) # Set fill color to black
translate(margin + offsetY, margin + offsetX)
scale(Scale)
drawGlyph(g)
name = g.name
if g.name.islower():
name = '_' + g.name
saveImage("export/" + name + ".svg")
There is a method to convert glyphs names to file names:
fileName = GSGlyphsInfo.glyphNameToFileName_(glyphName)
It is used to write .ufos and .glyphspackages
1 Like
Here is a version that does write PNG and SVG and doesn’t use robo code (that is not developed any more and will be removed on some point).
import os
from GlyphsApp import Glyphs, GSGlyphsInfo
from drawBot import newPage, fill, rect, translate, scale, drawPath, saveImage
height = 700
margin = 200
minMargin = 0
font = Glyphs.font
layers = font.selectedLayers
filePath = os.path.dirname(font.filepath)
Scale = float(height) / float(font.upm)
for l in layers:
#g = l.parent
print(l.parent.name)
bounds = l.bounds
extraSpace = 0
offset = 0
offsetY = 0
offsetX = 0
extraSpace *= Scale
offset *= Scale
width = margin + (l.width * Scale) + margin + extraSpace
newPage(width, height + (2 * margin))
# Draw transparent square
#fill(0, 0, 0, 0) # Set fill color with alpha (0 for transparency)
#rect(0, 0, width, height + (2 * margin))
fill(0, 0, 0) # Set fill color to black
translate(margin + offsetY, margin + offsetX)
scale(Scale)
drawPath(l.bezierPath)
name = GSGlyphsInfo.glyphNameToFileName_(l.parent.name)
saveImage(os.path.join(filePath, name + ".svg"))
saveImage(os.path.join(filePath, name + ".png"))
3 Likes
thierryc
(Thierry Charbonnel)
April 23, 2024, 2:29pm
35
Great feature/method!
It works perfectly for me.