Repository: tesseract-ocr/tesseract
File: src/api/baseapi.cpp (C++)

Code before:
  delete it;
  return result;
}

static void AddBoxToTSV(const PageIterator *it, PageIteratorLevel level, std::string &text) {
  int left, top, right, bottom;
  it->BoundingBox(level, &left, &top, &right, &bottom);
  text += "\t" + std::to_string(left);
  text += "\t" + std::to_string(top);
  text += "\t" + std::to_string(right - left);
  text += "\t" + std::to_string(bottom - top);
}

/**
 * Make a TSV-formatted string from the internal data structures.
 * page_number is 0-based but will appear in the output as 1-based.
 * Returned string must be freed with the delete [] operator.
 */
char *TessBaseAPI::GetTSVText(int page_number) {
  if (tesseract_ == nullptr || (page_res_ == nullptr && Recognize(nullptr) < 0))
    return nullptr;

  int lcnt = 1, bcnt = 1, pcnt = 1, wcnt = 1;
  int page_id = page_number + 1; // we use 1-based page numbers.

  int page_num = page_id;
  int block_num = 0;
  int par_num = 0;
  int line_num = 0;
  int word_num = 0;

  std::string tsv_str;
  tsv_str += "1\t" + std::to_string(page_num); // level 1 - page
  tsv_str += "\t" + std::to_string(block_num);
  tsv_str += "\t" + std::to_string(par_num);
  tsv_str += "\t" + std::to_string(line_num);
  tsv_str += "\t" + std::to_string(word_num);
  tsv_str += "\t" + std::to_string(rect_left_);
  tsv_str += "\t" + std::to_string(rect_top_);
  tsv_str += "\t" + std::to_string(rect_width_);
  tsv_str += "\t" + std::to_string(rect_height_);
  tsv_str += "\t-1\t\n";

  ResultIterator *res_it = GetIterator();
  while (!res_it->Empty(RIL_BLOCK)) {
    if (res_it->Empty(RIL_WORD)) {
      res_it->Next(RIL_WORD);
      continue;
    }

    // Add rows for any new block/paragraph/textline.

Review comment (suggestion):
again manual tuple. Lots of tuple<> below. Use typedef (using).

Code after:
  strncpy(result, text.c_str(), text.length() + 1);
  delete it;
  return result;
}

size_t TessBaseAPI::GetNumberOfTables()
{
  return constUniqueInstance<std::vector<TessTable>>().size();
}

std::tuple<int,int,int,int> TessBaseAPI::GetTableBoundingBox(unsigned i)
{
  const std::vector<TessTable>& t = constUniqueInstance<std::vector<TessTable>>();
  
  if(i >= t.size())
    return std::tuple<int,int,int,int>(0, 0, 0, 0);
  
  const int height = tesseract_->ImageHeight();
  
  return std::make_tuple<int,int,int,int>(
    t[i].box.left(), height - t[i].box.top(),
    t[i].box.right(), height - t[i].box.bottom());
}


std::vector<std::tuple<int,int,int,int>> TessBaseAPI::GetTableRows(unsigned i)
{
  const std::vector<TessTable>& t = constUniqueInstance<std::vector<TessTable>>();
  
  if(i >= t.size())
    return std::vector<std::tuple<int,int,int,int>>();
  
  std::vector<std::tuple<int,int,int,int>> rows(t[i].rows.size());
  const int height = tesseract_->ImageHeight();
  
  for(unsigned j = 0; j < t[i].rows.size(); ++j)
    rows[j] = std::make_tuple<int,int,int,int>(
      t[i].rows[j].left(), height - t[i].rows[j].top(),
      t[i].rows[j].right(), height - t[i].rows[j].bottom());
  
  return rows;
}

std::vector<std::tuple<int,int,int,int> > TessBaseAPI::GetTableCols(unsigned i)
{
  const std::vector<TessTable>& t = constUniqueInstance<std::vector<TessTable>>();
  
  if(i >= t.size())
    return std::vector<std::tuple<int,int,int,int>>();
  
  std::vector<std::tuple<int,int,int,int>> cols(t[i].cols.size());