Custom file format

Hello,

I am aware that it is possible to write an export format plugin using GlyphsSDK, but I am wondering whether it is also possible to write a custom file format plugin for Glyphs 3, that would enable saving and opening files of that format. Ideally I would like to store my projects in a compressed binary format (.bglyphs or something of the kind). If this is not possible, could it be considered in a future release?

Why would you want to use a binary file format? Is there a specific limitation that you encounter with the current file format?

Plain text .glyphs file grows much faster in size than a binary would. This can pose a problem for version control, and a binary format may also be just a bit more handy when sending the file around.

Version control systems can save only the differences for text files but typically have to save the entire file for every revision when using binary files. That should give you much improved disk space utilization for text files over binary files.

You might also want to look into using .glyphspackage files where each glyph is stored as its own file. Depending on the version control system you are using, that might reduce space usage significantly.

1 Like

Regardless of whether somebody else finds this useful or sensible, I would just like to know whether a custom file plugin with open and save capabilities is possible. An alternative to .glyphs, .glyphspackage, and .ufo file formats, if you will.

You can do that. Have a look at his repo: GitHub - schriftgestalt/BDFFileFormat

I have looked at that already, but as far as I understand this only adds another format in the export dialog. Not in the save and open ones. Am I missing something?

You have to setup yourself as an “Editor” (CFBundleTypeRole = Editor) for your file type and implement - (BOOL)writeFont:(GSFont *)font toURL:(NSURL *)URL error:(out NSError **)error or in python: def writeFont_toURL_error_(self, font, URL, None)
And:
- (GSFont *)fontFromURL:(NSURL *)URL ofType:(NSString *)type error:(out NSError **)error; or python: def fontFromURL_ofType_error_(self, URL, type, NONE)

But if you just need a binary format, Glyphs has one build in. I didn’t use if for some time so it is actually broken right now. But I should be able to fix it.

But, as Florian already pointed out, there are no real benefits to that format. Yes, the files will be a bit smaller but the size of the repository will be much bigger very quickly with binary files.

Thank you. I will try that. Is there perhaps an open-source plugin that does that already? Or an example of it?

That may be very true for git, but not for subversion or any other centralized VCS that employs a binary delta. In such cases the repository can be an efficient storage for binaries. Of course these files render diff and merge useless, but as much as I like the .glyphs format, I would not be resolving merge conflicts by hand, or looking at diffs anyway.

You really only need to add those two methods. What you do in them is only relevant to your file format.

I have added writeFont_toURL_error_ and fontFromURL_ofType_error_ functions to the BDF file format plugin, but the format does not show up in the ‘Save As…’ dialog. Should the BDFFileFormat class inherit from GeneralPlugin instead? It says here that file format plug-ins provide only font export functionality through the Export dialog, and not the Save and Open functionalities.

Does the Info.plist in the BDF plugin require any additional modifications as well? CFBundleTypeRole is already set to Editor.

You were right. I fixed it that you can add a new file type to Glyphs with a plugin.
If you write in python, you need implement those two methods:


	@objc.python_method
	def read(self, filepath, fileType):
		font = GSFont()
		font.disableUpdateInterface()
		# read the file into that font object here
		font.enableUpdateInterface()
		return font

	@objc.python_method
	def export(self, font, filepath=None):
		# write the file here
		return True

That will work from version 3084.

1 Like

That’s great news! I will try it out as soon as 3084 is released. Thank you for taking the time to look into this.

I have implemented the two methods in the BDF file format plugin. While reading works, saving results in an exception. I am attaching a log file below.

Somehow you added a NSNull value as an argument when calling the NSSavePanel.

The thing is GetSaveFile inside the export function (where I presume NSSavePanel would be called, since I am not calling it anywhere else explicitly) is not even getting invoked. In fact, I think this exception happens before the export function is entered. I believe this to be so, because a File > Export, instead of a File > Save As which is what I am trying to do, is successful and I get NSLog messages from the export function.