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