• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2# Copyright 2020 Google LLC.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6'''
7get_examples.py: Populate docs/examples/ from the list of named fiddles.
8'''
9
10import os
11import re
12import sys
13
14if sys.version_info[0] < 3:
15  from urllib2 import urlopen
16  from HTMLParser import HTMLParser
17  def unescape(v): return HTMLParser().unescape(v)
18else:
19  from urllib.request import urlopen
20  from html.parser import HTMLParser
21  from html import unescape
22
23def cxx_bool(v): return 'true' if v else 'false'
24
25assert os.pardir == '..'  and '/' in [os.sep, os.altsep]
26
27def parse_fiddle_sk(x):
28  class FiddleSk(HTMLParser):
29    def __init__(self):
30        HTMLParser.__init__(self)
31        self.attrs = {}
32    def handle_starttag(self, tag, attrs):
33      if tag == 'fiddle-sk':
34        self.attrs = dict(attrs)
35  fiddle = FiddleSk()
36  fiddle.feed(x)
37  return fiddle.attrs
38
39def process_fiddle(name):
40  if name == 'MAD_Magazine_Oct_1985':
41    return
42  filename = 'docs/examples/%s.cpp' % name
43  if os.path.exists(filename):
44    return
45  url = 'https://fiddle.skia.org/c/@' + name
46  content = urlopen(url).read()
47  regex = (r'(<fiddle-sk\s[^>]*>)\s*<textarea-numbers-sk>\s*'
48           r'<textarea [^>]*>(.*)</textarea>')
49  match = re.search(regex, content.decode('utf-8'), flags=re.S)
50  if not match:
51    sys.stderr.write('error: %s\n' % url)
52  keys = parse_fiddle_sk(match.group(1))
53  code = unescape(match.group(2))
54
55  width = keys.get('width', '256')
56  height = keys.get('height', '256')
57  source_image = keys.get('source', 256)
58  duration = keys.get('duration', '0')
59  textonly = 'textonly' in keys
60  srgb = not textonly and 'srgb' in keys
61  f16 = srgb and 'f16' in keys
62  offscreen = 'offscreen' in keys
63
64  sys.stdout.write('Writing to: %s\n' % filename)
65  sys.stdout.flush()
66  with open(filename, 'w') as o:
67    o.write('// Copyright 2020 Google LLC.\n'
68            '// Use of this source code is governed by a BSD-style'
69            ' license that can be found in the LICENSE file.\n'
70            '#include "tools/fiddle/examples.h"\n')
71    if offscreen:
72      o.write('REGISTER_FIDDLE(')
73      o.write(', '.join([name,
74                         width,
75                         height,
76                         cxx_bool(textonly),
77                         source_image,
78                         duration,
79                         cxx_bool(srgb),
80                         cxx_bool(f16),
81                         cxx_bool(offscreen),
82                         keys.get('offscreen_width', '64'),
83                         keys.get('offscreen_height', '64'),
84                         keys.get('offscreen_sample_count', '0'),
85                         keys.get('offscreen_texturable', 'false'),
86                         keys.get('offscreen_mipmap', 'false')]))
87    elif srgb:
88      o.write('REG_FIDDLE_SRGB(')
89      o.write(', '.join([name, width, height, cxx_bool(textonly),
90                         source_image, duration, cxx_bool(f16)]))
91    elif duration:
92      o.write('REG_FIDDLE_ANIMATED(')
93      o.write(', '.join([name, width, height, cxx_bool(textonly),
94                         source_image, duration]))
95    else:
96      o.write('REG_FIDDLE(')
97      o.write(', '.join([name, width, height, cxx_bool(textonly),
98                         source_image]))
99    o.write(') {\n')
100    o.write(code)
101    o.write('\n}  // END FIDDLE\n')
102
103def main():
104  os.chdir(os.path.dirname(__file__) + '/../..')
105  for line in urlopen('https://fiddle.skia.org/named/'):
106    line_match = re.search(r'/c/@([A-Za-z0-9_-]*)', line.decode('utf-8'))
107    if not line_match:
108      continue
109    name = line_match.group(1)
110    process_fiddle(name)
111
112if __name__ == '__main__':
113  main()
114