getFuriganaKanaSegments static method

List<RubyTextData> getFuriganaKanaSegments(
  1. {required String text,
  2. required String reading}
)

Generate a list of RubyTextData for kana given a text and its reading.

Implementation

static List<RubyTextData> getFuriganaKanaSegments({
  required String text,
  required String reading,
}) {
  List<RubyTextData> newSegments = [];
  int start = 0;
  bool state = text[0] == reading[0];

  for (int i = 0; i < text.length; i++) {
    bool newState = text[i] == reading[i];
    if (state == newState) {
      continue;
    }
    newSegments.add(
      RubyTextData(
        text.substring(start, i),
        ruby: state ? '' : reading.substring(start, i),
      ),
    );
    state = newState;
    start = i;
  }

  newSegments.add(
    RubyTextData(
      text.substring(start, text.length),
      ruby: state ? '' : reading.substring(start, text.length),
    ),
  );

  return newSegments;
}