Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read colors and set UFO keys for Glyphs ColorIndex and public.markColor #285

Merged
merged 1 commit into from
Nov 23, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Lib/glyphsLib/builder/glyph.py
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@

from __future__ import (print_function, division, absolute_import,
unicode_literals)
import logging
logger = logging.getLogger(__name__)

import glyphsLib
from .common import to_ufo_time
@@ -35,9 +37,21 @@ def to_ufo_glyph(self, ufo_glyph, layer, glyph_data):
if last_change is not None:
ufo_glyph.lib[GLYPHLIB_PREFIX + 'lastChange'] = to_ufo_time(last_change)
color_index = glyph_data.color
if color_index is not None and color_index >= 0:
if color_index is not None:
ufo_glyph.lib[GLYPHLIB_PREFIX + 'ColorIndex'] = color_index
ufo_glyph.lib[PUBLIC_PREFIX + 'markColor'] = GLYPHS_COLORS[color_index]
color_tuple = None
if isinstance(color_index, list):
if not all(i in range(0, 256) for i in color_index):
logger.warn('Invalid color tuple {} for glyph {}. '
'Values must be in range 0-255'.format(color_index, glyph_data.name))
else:
color_tuple = ','.join('{0:.4f}'.format(i/255) if i in range(1, 255) else str(i//255) for i in color_index)
elif isinstance(color_index, int) and color_index in range(len(GLYPHS_COLORS)):
color_tuple = GLYPHS_COLORS[color_index]
else:
logger.warn('Invalid color index {} for {}'.format(color_index, glyph_data.name))
if color_tuple is not None:
ufo_glyph.lib[PUBLIC_PREFIX + 'markColor'] = color_tuple
export = glyph_data.export
if not export:
ufo_glyph.lib[GLYPHLIB_PREFIX + 'Export'] = export
35 changes: 35 additions & 0 deletions tests/builder_test.py
Original file line number Diff line number Diff line change
@@ -1131,5 +1131,40 @@ def test_to_ufo_draw_paths_qcurve(self):
self.assertEqual(first_segment_type, 'qcurve')


class GlyphPropertiesTest(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're testing to_ufos function (or that what you are calling in the test), so this test_glyph_color method should go to ToUfosTest class, I think.

@belluzj wdyt?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me the current class name is good because it spells out the intent of the tests (test glyph properties) rather than the technical details (the test calls to_ufos). Beyond that, I don't know what the best practice is for organizing tests in python...


def test_glyph_color(self):
font = generate_minimal_font()
glyph = GSGlyph(name='a')
glyph2 = GSGlyph(name='b')
glyph3 = GSGlyph(name='c')
glyph4 = GSGlyph(name='d')
glyph.color = [244, 0, 138, 1]
glyph2.color = 3
glyph3.color = 88
glyph4.color = [800, 0, 138, 1]
font.glyphs.append(glyph)
font.glyphs.append(glyph2)
font.glyphs.append(glyph3)
font.glyphs.append(glyph4)
layer = GSLayer()
layer2 = GSLayer()
layer3 = GSLayer()
layer4 = GSLayer()
layer.layerId = font.masters[0].id
layer2.layerId = font.masters[0].id
layer3.layerId = font.masters[0].id
layer4.layerId = font.masters[0].id
glyph.layers.append(layer)
glyph2.layers.append(layer2)
glyph3.layers.append(layer3)
glyph4.layers.append(layer4)
ufo = to_ufos(font)[0]
self.assertEqual(ufo['a'].lib.get('public.markColor'), '0.9569,0,0.5412,0.0039')
self.assertEqual(ufo['b'].lib.get('public.markColor'), '0.97,1,0,1')
self.assertEqual(ufo['c'].lib.get('public.markColor'), None)
self.assertEqual(ufo['d'].lib.get('public.markColor'), None)


if __name__ == '__main__':
unittest.main()