1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'dart:ui' show TextRange; |
6 | |
7 | import 'text_editing.dart'; |
8 | |
9 | export 'dart:ui' show TextPosition, TextRange; |
10 | |
11 | export 'text_editing.dart' show TextSelection; |
12 | |
13 | /// A read-only interface for accessing visual information about the |
14 | /// implementing text. |
15 | abstract class TextLayoutMetrics { |
16 | // TODO(gspencergoog): replace when we expose this ICU information. |
17 | /// Check if the given code unit is a white space or separator |
18 | /// character. |
19 | /// |
20 | /// Includes newline characters from ASCII and separators from the |
21 | /// [unicode separator category](https://www.compart.com/en/unicode/category/Zs) |
22 | static bool isWhitespace(int codeUnit) { |
23 | switch (codeUnit) { |
24 | case 0x9: // horizontal tab |
25 | case 0xA: // line feed |
26 | case 0xB: // vertical tab |
27 | case 0xC: // form feed |
28 | case 0xD: // carriage return |
29 | case 0x1C: // file separator |
30 | case 0x1D: // group separator |
31 | case 0x1E: // record separator |
32 | case 0x1F: // unit separator |
33 | case 0x20: // space |
34 | case 0xA0: // no-break space |
35 | case 0x1680: // ogham space mark |
36 | case 0x2000: // en quad |
37 | case 0x2001: // em quad |
38 | case 0x2002: // en space |
39 | case 0x2003: // em space |
40 | case 0x2004: // three-per-em space |
41 | case 0x2005: // four-er-em space |
42 | case 0x2006: // six-per-em space |
43 | case 0x2007: // figure space |
44 | case 0x2008: // punctuation space |
45 | case 0x2009: // thin space |
46 | case 0x200A: // hair space |
47 | case 0x202F: // narrow no-break space |
48 | case 0x205F: // medium mathematical space |
49 | case 0x3000: // ideographic space |
50 | break; |
51 | default: |
52 | return false; |
53 | } |
54 | return true; |
55 | } |
56 | |
57 | /// Check if the given code unit is a line terminator character. |
58 | /// |
59 | /// Includes newline characters from ASCII |
60 | /// (https://www.unicode.org/standard/reports/tr13/tr13-5.html). |
61 | static bool isLineTerminator(int codeUnit) { |
62 | switch (codeUnit) { |
63 | case 0x0A: // line feed |
64 | case 0x0B: // vertical feed |
65 | case 0x0C: // form feed |
66 | case 0x0D: // carriage return |
67 | case 0x85: // new line |
68 | case 0x2028: // line separator |
69 | case 0x2029: // paragraph separator |
70 | return true; |
71 | default: |
72 | return false; |
73 | } |
74 | } |
75 | |
76 | /// {@template flutter.services.TextLayoutMetrics.getLineAtOffset} |
77 | /// Return a [TextSelection] containing the line of the given [TextPosition]. |
78 | /// {@endtemplate} |
79 | TextSelection getLineAtOffset(TextPosition position); |
80 | |
81 | /// {@macro flutter.painting.TextPainter.getWordBoundary} |
82 | TextRange getWordBoundary(TextPosition position); |
83 | |
84 | /// {@template flutter.services.TextLayoutMetrics.getTextPositionAbove} |
85 | /// Returns the TextPosition above the given offset into the text. |
86 | /// |
87 | /// If the offset is already on the first line, the given offset will be |
88 | /// returned. |
89 | /// {@endtemplate} |
90 | TextPosition getTextPositionAbove(TextPosition position); |
91 | |
92 | /// {@template flutter.services.TextLayoutMetrics.getTextPositionBelow} |
93 | /// Returns the TextPosition below the given offset into the text. |
94 | /// |
95 | /// If the offset is already on the last line, the given offset will be |
96 | /// returned. |
97 | /// {@endtemplate} |
98 | TextPosition getTextPositionBelow(TextPosition position); |
99 | } |
100 | |