コード例 #1
0
def _add_fvar(font, axes, instances):
    """
	Add 'fvar' table to font.

	axes is an ordered dictionary of DesignspaceAxis objects.

	instances is list of dictionary objects with 'location', 'stylename',
	and possibly 'postscriptfontname' entries.
	"""

    assert axes
    assert isinstance(axes, OrderedDict)

    log.info("Generating fvar")

    fvar = newTable('fvar')
    nameTable = font['name']

    for a in axes.values():
        axis = Axis()
        axis.axisTag = Tag(a.tag)
        # TODO Skip axes that have no variation.
        axis.minValue, axis.defaultValue, axis.maxValue = a.minimum, a.default, a.maximum
        axis.axisNameID = nameTable.addMultilingualName(a.labelNames, font)
        axis.flags = int(a.hidden)
        fvar.axes.append(axis)

    for instance in instances:
        coordinates = instance.location

        if "en" not in instance.localisedStyleName:
            if not instance.styleName:
                raise VarLibValidationError(
                    f"Instance at location '{coordinates}' must have a default English "
                    "style name ('stylename' attribute on the instance element or a "
                    "stylename element with an 'xml:lang=\"en\"' attribute).")
            localisedStyleName = dict(instance.localisedStyleName)
            localisedStyleName["en"] = tounicode(instance.styleName)
        else:
            localisedStyleName = instance.localisedStyleName

        psname = instance.postScriptFontName

        inst = NamedInstance()
        inst.subfamilyNameID = nameTable.addMultilingualName(
            localisedStyleName)
        if psname is not None:
            psname = tounicode(psname)
            inst.postscriptNameID = nameTable.addName(psname)
        inst.coordinates = {
            axes[k].tag: axes[k].map_backward(v)
            for k, v in coordinates.items()
        }
        #inst.coordinates = {axes[k].tag:v for k,v in coordinates.items()}
        fvar.instances.append(inst)

    assert "fvar" not in font
    font['fvar'] = fvar

    return fvar
コード例 #2
0
ファイル: __init__.py プロジェクト: khaledhosny/fonttools
def _add_fvar(font, axes, instances):
	"""
	Add 'fvar' table to font.

	axes is an ordered dictionary of DesignspaceAxis objects.

	instances is list of dictionary objects with 'location', 'stylename',
	and possibly 'postscriptfontname' entries.
	"""

	assert axes
	assert isinstance(axes, OrderedDict)

	log.info("Generating fvar")

	fvar = newTable('fvar')
	nameTable = font['name']

	for a in axes.values():
		axis = Axis()
		axis.axisTag = Tag(a.tag)
		# TODO Skip axes that have no variation.
		axis.minValue, axis.defaultValue, axis.maxValue = a.minimum, a.default, a.maximum
		axis.axisNameID = nameTable.addMultilingualName(a.labelNames, font)
		axis.flags = int(a.hidden)
		fvar.axes.append(axis)

	for instance in instances:
		coordinates = instance.location

		if "en" not in instance.localisedStyleName:
			assert instance.styleName
			localisedStyleName = dict(instance.localisedStyleName)
			localisedStyleName["en"] = tounicode(instance.styleName)
		else:
			localisedStyleName = instance.localisedStyleName

		psname = instance.postScriptFontName

		inst = NamedInstance()
		inst.subfamilyNameID = nameTable.addMultilingualName(localisedStyleName)
		if psname is not None:
			psname = tounicode(psname)
			inst.postscriptNameID = nameTable.addName(psname)
		inst.coordinates = {axes[k].tag:axes[k].map_backward(v) for k,v in coordinates.items()}
		#inst.coordinates = {axes[k].tag:v for k,v in coordinates.items()}
		fvar.instances.append(inst)

	assert "fvar" not in font
	font['fvar'] = fvar

	return fvar
コード例 #3
0
 def test_toXML(self):
     font = MakeFont()
     axis = Axis()
     axis.decompile(FVAR_AXIS_DATA)
     AddName(font, "Optical Size").nameID = 256
     axis.axisNameID = 256
     axis.flags = 0xABC
     writer = XMLWriter(BytesIO())
     axis.toXML(writer, font)
     self.assertEqual([
         '', '<!-- Optical Size -->', '<Axis>', '<AxisTag>opsz</AxisTag>',
         '<Flags>0xABC</Flags>', '<MinValue>-0.5</MinValue>',
         '<DefaultValue>1.3</DefaultValue>', '<MaxValue>1.5</MaxValue>',
         '<AxisNameID>256</AxisNameID>', '</Axis>'
     ], xml_lines(writer))
コード例 #4
0
ファイル: _f_v_a_r_test.py プロジェクト: MrBrezina/fonttools
 def test_toXML(self):
     font = MakeFont()
     axis = Axis()
     axis.decompile(FVAR_AXIS_DATA)
     AddName(font, "Optical Size").nameID = 256
     axis.axisNameID = 256
     axis.flags = 0xABC
     writer = XMLWriter(BytesIO())
     axis.toXML(writer, font)
     self.assertEqual([
         '',
         '<!-- Optical Size -->',
         '<Axis>',
             '<AxisTag>opsz</AxisTag>',
             '<Flags>0xABC</Flags>',
             '<MinValue>-0.5</MinValue>',
             '<DefaultValue>1.3</DefaultValue>',
             '<MaxValue>1.5</MaxValue>',
             '<AxisNameID>256</AxisNameID>',
         '</Axis>'
     ], xml_lines(writer))