unicode - 在 Dart 中处理字素簇

标签 unicode dart flutter icu grapheme-cluster

据我所知,Dart 不支持字素集群,尽管有人说支持它:

在实现之前,我有哪些用于迭代字素集群的选项?例如,如果我有这样的字符串:

String family = '\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}'; // 👨‍👩‍👧
String myString = 'Let me introduce my $family to you.';

并且在五码点家族表情符号之后有一个光标:

enter image description here

如何将光标向左移动一个用户感知的字符?

(在这种特殊情况下,我知道字素簇的大小,所以我可以做到,但我真正要问的是找到任意长的字素簇的长度。)

更新

我从 this article 看到Swift 使用系统的 ICU图书馆。在 Flutter 中可能会出现类似的情况。

补充代码

对于那些想玩弄我上面的例子的人,这里有一个演示项目。按钮将光标向右或向左移动。目前需要按 8 次按钮才能将光标移过家庭表情符号。

enter image description here

main.dart

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Grapheme cluster testing')),
        body: BodyWidget(),
      ),
    );
  }
}

class BodyWidget extends StatefulWidget {
  @override
  _BodyWidgetState createState() => _BodyWidgetState();
}

class _BodyWidgetState extends State<BodyWidget> {

  TextEditingController controller = TextEditingController(
      text: 'Let me introduce my \u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467} to you.'
  );

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(
          controller: controller,
        ),
        Row(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                child: Text('<<'),
                onPressed: () {
                  _moveCursorLeft();
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                child: Text('>>'),
                onPressed: () {
                  _moveCursorRight();
                },
              ),
            ),
          ],
        )
      ],
    );
  }

  void _moveCursorLeft() {
    int currentCursorPosition = controller.selection.start;
    if (currentCursorPosition == 0)
      return;
    int newPosition = currentCursorPosition - 1;
    controller.selection = TextSelection(baseOffset: newPosition, extentOffset: newPosition);
  }

  void _moveCursorRight() {
    int currentCursorPosition = controller.selection.end;
    if (currentCursorPosition == controller.text.length)
      return;
    int newPosition = currentCursorPosition + 1;
    controller.selection = TextSelection(baseOffset: newPosition, extentOffset: newPosition);
  }
}

最佳答案

更新:使用 https://pub.dartlang.org/packages/icu

示例代码:

import 'package:flutter/material.dart';


import 'dart:async';
import 'package:icu/icu.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Grapheme cluster testing')),
        body: BodyWidget(),
      ),
    );
  }
}

class BodyWidget extends StatefulWidget {
  @override
  _BodyWidgetState createState() => _BodyWidgetState();
}

class _BodyWidgetState extends State<BodyWidget> {
  final ICUString icuText = ICUString('Let me introduce my \u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467} to you.\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}');
  TextEditingController controller;
  _BodyWidgetState() {
    controller = TextEditingController(
      text: icuText.toString()
  );
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        TextField(
          controller: controller,
        ),
        Row(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                child: Text('<<'),
                onPressed: () async {
                  await _moveCursorLeft();
                },
              ),
            ),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: RaisedButton(
                child: Text('>>'),
                onPressed: () async {
                  await _moveCursorRight();
                },
              ),
            ),
          ],
        )
      ],
    );
  }

  void _moveCursorLeft() async {
    int currentCursorPosition = controller.selection.start;
    if (currentCursorPosition == 0)
      return;
    int newPosition = await icuText.previousGraphemePosition(currentCursorPosition);
    controller.selection = TextSelection(baseOffset: newPosition, extentOffset: newPosition);
  }

  void _moveCursorRight() async {
    int currentCursorPosition = controller.selection.end;
    if (currentCursorPosition == controller.text.length)
      return;
    int newPosition = await icuText.nextGraphemePosition(currentCursorPosition);
    controller.selection = TextSelection(baseOffset: newPosition, extentOffset: newPosition);
  }
}


原答案:

在 Dart/Flutter 完全实现 ICU 之前,我认为最好的选择是使用 PlatformChannel传递 Unicode 字符串 native(iOS Swift4+ 或 Android Java/Kotlin)在那里迭代/操作,然后发回结果。

  • 对于 Swift4+,它与您提到的文章一样是开箱即用的(不是 Swift3-,不是 ObjC)
  • 对于 Java/Kotlin,将 Oracle 的 BreakIterator 替换为 ICU library的,效果更好。除了导入语句之外没有任何变化。

我建议使用原生操作(而不是在 Dart 上使用)的原因是因为 Unicode 有太多的事情要处理,例如规范化、规范等价、ZWNJ、ZWJ、ZWSP 等。

如果您需要一些示例代码,请在下方评论。

关于unicode - 在 Dart 中处理字素簇,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54483177/

相关文章:

Java Unicode 变量名称梵文

flutter - 如何使 InkWell `onLongPress()` 覆盖其内部 TextField 上的点击?

Flutter Bloc 显示 : "Unexpected null value"

dart - 具有默认文本的 TextFormField

PDF:具有不同 ToUnicode Cmap 的重复字体名称

python - lua cjson 无法解码特定的 unicode 字符?

dart - dart 中单独隔离的最大内存量

flutter - flutter希望在其子级上调用父 View 以刷新主视图

Flutter - 存储经过身份验证的用户详细信息

bash - 是否有 bash(或任何其他 shell)脚本来检测当前终端是否支持 unicode 字符?