SirOllepopple
SirOllepopple

Reputation: 154

Is there a way to normalize unicode characters in dart/flutter

Is there a way in dart/flutter that can convert any fancy unicode text back to its regular counterpart?

for example:

Input: 𝑻𝒉𝒆 ℚ𝕦𝕚𝕔𝕜 Brown 🅵🅾🆇 𝔍𝔲𝔪𝔭𝔢𝔡 ⓞⓥⓔⓡ ʇɥǝ 𝗟𝗮𝘇𝘆 𝙳𝚘𝚐

Output: The Quick Brown Fox Jumped over the Lazy Dog

Thank you!

reference: https://onlinetools.com/unicode/normalize-unicode-text

Upvotes: 3

Views: 883

Answers (2)

mezoni
mezoni

Reputation: 11220

I don't think this can be called normalization.
But there is something that can be done.
Big problem with turned letters. These characters are not so easy to transform. Without them, everything would be much simpler.

Below is a small example.

import 'package:unicode/decomposer.dart';
import 'package:unicode/decomposers/circle.dart';
import 'package:unicode/decomposers/font.dart';
import 'package:unicode/decomposers/wide.dart';
import 'package:unicode/unicode.dart' as unicode;

void main(List<String> args) {
  const s1 =
      '𝑻𝒉𝒆 ℚ𝕦𝕚𝕔𝕜 Brown 🅵🅾🆇 𝔍𝔲𝔪𝔭𝔢𝔡 ⓞⓥⓔⓡ ʇɥǝ 𝗟𝗮𝘇𝘆 𝙳𝚘𝚐';
  final s2 = decompose(s1, _decomposers);
  print(s1);
  print(s2);
}

final _decomposer1 = LetterMappingDecomposer([
  ('ʇ'.c, 't'.c),
  ('ɥ'.c, 'h'.c),
  ('ǝ'.c, 'e'.c),
]);

final _decomposer2 = LetterCasingDecomposer([
  ('🅰'.c, '🆉'.c, 'A'.c - '🅰'.c),
]);

final _decomposers = [
  const FontDecomposer(),
  const WideDecomposer(),
  const CircleDecomposer(),
  _decomposer2,
  _decomposer1,
];

String char2Str(int c) {
  return String.fromCharCode(c);
}

extension on String {
  int get c => unicode.toRune(this);
}

Output:

𝑻𝒉𝒆 ℚ𝕦𝕚𝕔𝕜 Brown 🅵🅾🆇 𝔍𝔲𝔪𝔭𝔢𝔡 ⓞⓥⓔⓡ ʇɥǝ 𝗟𝗮𝘇𝘆 𝙳𝚘𝚐
The Quick Brown FOX Jumped over the Lazy Dog

Upvotes: 0

You can check out this package on pub.dev

ENS Normalize

Upvotes: 0

Related Questions