OLD | NEW |
(Empty) | |
| 1 // |
| 2 // Copyright 2014 Google Inc. All rights reserved. |
| 3 // |
| 4 // Use of this source code is governed by a BSD-style |
| 5 // license that can be found in the LICENSE file or at |
| 6 // https://developers.google.com/open-source/licenses/bsd |
| 7 // |
| 8 |
| 9 library charted.tool.build_unicode_segmentation_data; |
| 10 |
| 11 import 'dart:async'; |
| 12 import 'package:http/http.dart' as http; |
| 13 import 'package:charted/core/text_metrics/segmentation_utils.dart'; |
| 14 |
| 15 /// Unicode version |
| 16 /// A new version of unicode is available every June. |
| 17 const VERSION = '7.0.0'; |
| 18 |
| 19 /// URI for downloading the grapheme properties file. |
| 20 const UCD_PROPERTIES_URL = |
| 21 'http://www.unicode.org/Public/${VERSION}/ucd/auxiliary/GraphemeBreakPropert
y.txt'; |
| 22 |
| 23 const LIBRARY_NAME = 'charted.core.text_metrics.segmentation_data'; |
| 24 |
| 25 /// License header for the generated file. |
| 26 const HEADER = """ |
| 27 // |
| 28 // Copyright 2014 Google Inc. All rights reserved. |
| 29 // |
| 30 // Use of this source code is governed by a BSD-style |
| 31 // license that can be found in the LICENSE file or at |
| 32 // https://developers.google.com/open-source/licenses/bsd |
| 33 // |
| 34 // This is a generated file. |
| 35 // Please use tool/build_unicode_segmentation-data.dart to update |
| 36 // |
| 37 |
| 38 /// Code ranges by their types for use with grapheme segmentation |
| 39 /// of text in charts. |
| 40 library $LIBRARY_NAME; |
| 41 |
| 42 import "package:charted/core/text_metrics/segmentation_utils.dart"; |
| 43 |
| 44 """; |
| 45 |
| 46 Future<String> _getPropertiesFile() => http.read(UCD_PROPERTIES_URL); |
| 47 |
| 48 void _dumpPropertiesData(String data) { |
| 49 StringBuffer buffer = new StringBuffer(); |
| 50 RegExp lineRegExp = |
| 51 new RegExp(r'([0-9A-F]{4})..([0-9A-F]{4})?\s+;\s([a-zA-Z]+)\s'); |
| 52 |
| 53 buffer.write(HEADER); |
| 54 buffer.writeln('const CODE_POINT_BLOCKS = const['); |
| 55 |
| 56 List<Iterable> items = []; |
| 57 data.split('\n').forEach((String line) { |
| 58 Match match = lineRegExp.matchAsPrefix(line); |
| 59 if (match == null) return; |
| 60 |
| 61 int start = int.parse(match.group(1), radix:16); |
| 62 int end = |
| 63 match.group(2) == null ? start : int.parse(match.group(2), radix:16); |
| 64 |
| 65 items.add([start, end, CodeUnitCategory[match.group(3)]]); |
| 66 items.sort((a, b) => a.first.compareTo(b.first)); |
| 67 }); |
| 68 |
| 69 buffer.write(items.map((List range) => range.join(', ')).join(',\n ')); |
| 70 buffer.writeln(); |
| 71 buffer.writeln('];'); |
| 72 print(buffer.toString()); |
| 73 } |
| 74 |
| 75 main() { |
| 76 _getPropertiesFile().then((data) => _dumpPropertiesData(data)); |
| 77 } |
OLD | NEW |