getCfiHighlightRects method

List getCfiHighlightRects(
  1. String cfi
)

Gets the highlighted area for the specifies cfi path. See HighlightBox for more information.

Implementation

List<HighlightBox> getCfiHighlightRects(String cfi) {
  if (cfi.isEmpty) {
    return [];
  }

  Pointer<BufferString> bufferCfi = BufferString.fromDartString(cfi);

  Pointer<BufferArray> highlightBoxesArray = (getCfiHighlightRectsNative?.call(_sdk, bufferCfi))!;

  freeBufferString(bufferCfi);

  List<HighlightBox> boxes = [];

  int highlightBoxesArrayLength = highlightBoxesArray.ref.count;
  Pointer<HighlightBoxCore> nativeBoxes = highlightBoxesArray.ref.data.cast();
  for (int i = 0; i < highlightBoxesArrayLength; i++) {
    Pointer<HighlightBoxCore> nativeBox = nativeBoxes.elementAt(i);
    int globalPage = nativeBox.ref.globalPage;
    Pointer<BufferMap> rectsMap = nativeBox.ref.cfiPositionsMap;
    Map<String, List<Rect>> cfiPositionsMap = {};
    Pointer<Pointer<BufferMapPair>> pairs = rectsMap.ref.pairs.cast();
    for (int j = 0; j < rectsMap.ref.count; j++) {
      Pointer<BufferMapPair> pair = pairs[j];
      Pointer<BufferString> cfiBuffer = Pointer.fromAddress(pair.ref.key);
      Pointer<BufferArray> rectsArray = Pointer.fromAddress(pair.ref.value);
      List<Rect> rects = [];
      Pointer<RectCore> nativeRects = rectsArray.ref.data.cast();
      for (int k = 0; k < rectsArray.ref.count; k++) {
        Pointer<RectCore> nativeRect = nativeRects.elementAt(k);
        double left = nativeRect.ref.left;
        double top = nativeRect.ref.top;
        double right = nativeRect.ref.right;
        double bottom = nativeRect.ref.bottom;
        Rect rect = Rect.fromLTRB(left, top, right, bottom);
        rects.add(rect);
      }
      String cfi = cfiBuffer.ref.toString();
      cfiPositionsMap[cfi] = rects;
    }
    HighlightBox box = HighlightBox(globalPage, cfiPositionsMap);
    boxes.add(box);
  }

  freeBufferArray(highlightBoxesArray);

  return boxes;
}