proc-template.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import sys
  2. import xml.etree.ElementTree as ET
  3. import re
  4. if len(sys.argv) != 2:
  5. print 'Invalid usage; see Makefile'
  6. sys.exit(1)
  7. operations = re.findall(r'\w+', sys.argv[1])
  8. namespaces = {
  9. '': 'http://www.w3.org/2000/svg',
  10. 'dc': 'http://purl.org/dc/elements/1.1/',
  11. 'cc': 'http://creativecommons.org/ns#',
  12. 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  13. 'svg': 'http://www.w3.org/2000/svg',
  14. 'sodipodi': 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
  15. 'inkscape': 'http://www.inkscape.org/namespaces/inkscape',
  16. }
  17. for prefix, namespace in namespaces.items():
  18. ET.register_namespace(prefix, namespace)
  19. tree = ET.parse(sys.stdin)
  20. root = tree.getroot()
  21. def setFillColor(el, c):
  22. css = el.get('style')
  23. if css is not None:
  24. css = re.sub(r'fill:\s*#[0-9a-f]{6};', 'fill:'+c+';', css)
  25. el.set('style', css)
  26. def setFillOpacity(el, v):
  27. css = el.get('style')
  28. if css is not None:
  29. css = re.sub(r'fill-opacity:[^;]+;', '', css)
  30. css += ';fill-opacity:'+str(v)+';'
  31. el.set('style', css)
  32. def deleteElement(tree, el):
  33. if el is not None:
  34. for parent in tree.iter():
  35. if el in parent:
  36. parent.remove(el)
  37. # Handle deleting unnecessary items
  38. if not 'tagline1' in operations:
  39. g = root.find('.//svg:g[@id="tagline2v1"]', namespaces)
  40. if g is not None:
  41. deleteElement(tree, g)
  42. if not 'tagline2' in operations:
  43. g = root.find('.//svg:g[@id="tagline2v2"]', namespaces)
  44. if g is not None:
  45. deleteElement(tree, g)
  46. if 'tagline1' in operations or 'tagline2' in operations:
  47. bgName = "backgroundTagline"
  48. else:
  49. # Get rid of the first (common) line of the tagline
  50. deleteElement(tree, root.find('.//svg:g[@id="tagline1"]', namespaces))
  51. # "CrySP" only stays if we're not a pictorial mark
  52. if 'word' in operations:
  53. bgName = "backgroundWord"
  54. else:
  55. deleteElement(tree, root.find('.//svg:g[@id="crysp"]', namespaces))
  56. if 'icon' in operations:
  57. bgName = "backgroundIcon"
  58. else:
  59. bgName = "backgroundPictorial"
  60. # We only keep one "background rectangle" based on whether we have a
  61. # tagline or not. This keeps an appropriately sized rectangle so that
  62. # cropping to the drawing works properly.
  63. bg = root.find('.//svg:rect[@id="'+bgName+'"]', namespaces)
  64. for wrongBg in ['backgroundTagline', 'backgroundWord', 'backgroundPictorial', 'backgroundIcon']:
  65. if wrongBg != bgName:
  66. deleteElement(tree, root.find('.//svg:rect[@id="'+wrongBg+'"]', namespaces))
  67. # Handle background color
  68. if bg is None:
  69. print 'No background element found'
  70. sys.exit(1)
  71. if 'whitebg' in operations:
  72. setFillColor(bg, '#ffffff')
  73. elif 'blackbg' in operations:
  74. setFillColor(bg, '#000000')
  75. elif 'clearbg' in operations:
  76. setFillOpacity(bg, 0)
  77. # Handle foreground color
  78. fgColor = None
  79. if 'whitefg' in operations:
  80. fgColor = '#ffffff'
  81. elif 'blackfg' in operations:
  82. fgColor = '#000000'
  83. if fgColor is not None:
  84. # "CrySP" text
  85. for glyph in root.findall('.//svg:g[@id="crysp"]/svg:path', namespaces):
  86. setFillColor(glyph, fgColor)
  87. # Tagline text (full lab name)
  88. # Some fill styles are set in the groups instead of the glyph paths
  89. for glyph in root.findall('.//svg:g[@id="tagline"]//*', namespaces):
  90. setFillColor(glyph, fgColor)
  91. # The pictorial mark too if requested
  92. if 'mono' in operations:
  93. for glyph in root.findall('.//svg:g[@id="pictorial-mark"]/svg:path', namespaces):
  94. setFillColor(glyph, fgColor)
  95. # Strip font data from text that has been converted to paths
  96. for el in root.findall('.//*[@style]', namespaces):
  97. css = el.get('style')
  98. css = re.sub(r'font-(?:feature-settings|variant-caps|variant-ligatures|variant-numeric):[^;"]+[;"]', '', css)
  99. el.set('style', css)
  100. tree.write(sys.stdout, xml_declaration=True)