#!/usr/bin/env python3 from lxml import etree from utils.misc import downloadWithProgressBar, UnicodeXMLURL from utils import mathfont # Retrieve the unicode.xml file if necessary. unicodeXML = downloadWithProgressBar(UnicodeXMLURL) # Extract the mathvariants transformation. xsltTransform = etree.XSLT(etree.XML('''\ ''')) # Put the mathvariant transforms into a Python structure. mathvariantTransforms = {} root = xsltTransform(etree.parse(unicodeXML)).getroot() def parseCodePoint(aHexaString): return int("0x%s" % aHexaString[1:], 16) for entry in root: mathvariant = entry.get("mathvariant") baseChar = parseCodePoint(entry.get("baseChar")) transformedChar = parseCodePoint(entry.get("transformedChar")) if mathvariant not in mathvariantTransforms: mathvariantTransforms[mathvariant] = {} mathvariantTransforms[mathvariant][baseChar] = transformedChar # There is no "isolated" mathvariant. del mathvariantTransforms["isolated"] # Automatic mathvariant uses the same transform as italic. # It is handled specially (see below). mathvariantTransforms["auto"] = mathvariantTransforms["italic"] # Create a WOFF font for each mathvariant. for mathvariant in mathvariantTransforms: if mathvariant == "auto": continue font = mathfont.create("mathvariant-%s" % mathvariant, "Copyright (c) 2016 MathML Association") for baseChar in mathvariantTransforms[mathvariant]: if baseChar not in font: mathfont.createGlyphFromValue(font, baseChar) transformedChar = mathvariantTransforms[mathvariant][baseChar] mathfont.createGlyphFromValue(font, transformedChar) mathfont.save(font) # Create a MathML and CSS test for each mathvariant. for mathvariant in mathvariantTransforms: print("Generating tests for %s..." % mathvariant, end="") reftest = open("../relations/css-styling/mathvariant-%s.html" % mathvariant, "w") reftestReference = open("../relations/css-styling/mathvariant-%s-ref.html" % mathvariant, "w") CSSreftest = open("../../css/css-text/text-transform/math/text-transform-math-%s-001.tentative.html" % mathvariant, "w") CSSreftestReference = open("../../css/css-text/text-transform/math/text-transform-math-%s-001.tentative-ref.html" % mathvariant, "w") source = '\ \n\ \n\ \n\ \n\ %s\n' reftest.write(source % ("mathvariant %s" % mathvariant)) reftestReference.write(source % ("mathvariant %s (reference)" % mathvariant)) CSSreftest.write(source % ("text-transform math-%s" % mathvariant)) CSSreftestReference.write(source % ("text-transform math-%s (reference)" % mathvariant)) if mathvariant == "auto": mathAssert = "Verify that a single-char is equivalent to an with the transformed italic unicode character." mapping = "italic" else: mathAssert = "Verify that a single-char with a %s mathvariant is equivalent to an with the transformed unicode character." % mathvariant mapping = mathvariant source ='\ \n\ \n\ \n\ \n\ \n\ \n' reftest.write(source % (mapping, mathvariant, mathAssert)) source = '\ \n\ \n\ \n\ \n\ \n' CSSreftest.write(source % (mapping, mathvariant, mathvariant)) WOFFfont = "mathvariant-%s.woff" % mapping source = '\ \n\ \n\ \n\

Test passes if all the equalities below are true.

\n' % WOFFfont reftest.write(source) reftestReference.write(source) CSSreftest.write(source) CSSreftestReference.write(source) charIndex = 0 for baseChar in mathvariantTransforms[mathvariant]: transformedChar = mathvariantTransforms[mathvariant][baseChar] if mathvariant == "auto": tokenTag = '&#x%0X;' % baseChar tokenTagRef = '&#x%0X;' % transformedChar else: tokenTag = '&#x%0X;' % (mathvariant, baseChar) tokenTagRef = '&#x%0X;' % transformedChar reftest.write(' %s=%05X' % (tokenTag, transformedChar)) reftestReference.write(' %s=%05X' % (tokenTagRef, transformedChar)) CSSreftest.write(' &#x%0X;=%05X' % (mathvariant, baseChar, transformedChar)) CSSreftestReference.write(' &#x%0X;=%05X' % (transformedChar, transformedChar)) charIndex += 1 if charIndex % 10 == 0: reftest.write('
') reftestReference.write('
') CSSreftest.write('
') CSSreftestReference.write('
') reftest.write('\n') reftestReference.write('\n') CSSreftest.write('\n') CSSreftestReference.write('\n') source = '\n\n' reftest.write(source) reftestReference.write(source) CSSreftest.write(source) CSSreftestReference.write(source) reftest.close() reftestReference.close() CSSreftest.close() CSSreftestReference.close() print(" done.")