pgedit.cpp

来自「一个google的OCR源码」· C++ 代码 · 共 1,866 行 · 第 1/4 页

CPP
1,866
字号
  fclose(infp);  image_win->AddMessage("Reading file " "%s" "...", name_str.string());  source_block_list->clear();                                 // appends to SOURCE  pgeditor_read_file(name_str, source_block_list);  source_changed = FALSE;  image_win->AddMessage("Doing automatic Tidy Target...");  viewing_source = FALSE;        // Force viewing source  do_tidy_cmd();  image_win->AddMessage("Doing automatic Tidy Target...Done");}/********************************************************************** *  do_re_display() * *  Redisplay page **********************************************************************/void                                 // function to calldo_re_display(BOOL8 word_painter(BLOCK *, ROW *, WERD *)) {  BLOCK_IT block_it(current_block_list);  BLOCK *block;  int block_count = 1;  ROW_IT row_it;  ROW *row;  WERD_IT word_it;  WERD *word;  image_win->Clear();  if (display_image != 0) {    sv_show_sub_image(&page_image, 0, 0,      page_image.get_xsize(), page_image.get_ysize(),      image_win, 0, 0);  }  for (block_it.mark_cycle_pt();  !block_it.cycled_list(); block_it.forward()) {    block = block_it.data();    row_it.set_to_list(block->row_list());    for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {      row = row_it.data();      word_it.set_to_list(row->word_list());      for (word_it.mark_cycle_pt();      !word_it.cycled_list(); word_it.forward()) {        word = word_it.data();        word_painter(block, row, word);      }      if (display_baselines)        row->plot_baseline(image_win, ScrollView::GREEN);    }    if (display_blocks)      block->plot(image_win, block_count++, ScrollView::RED);  }  image_win->Update();}/********************************************************************** *  do_tidy_cmd() * *  Tidy TARGET page **********************************************************************/const TBOX do_tidy_cmd() {  // tidy  ICOORD shift_vector;  TBOX tidy_box;                  // Just the tidy area  TBOX source_box;                // source file area  source_box = block_list_bounding_box(source_block_list);  // find src area  if (!target_block_list.empty()) {    tidy_box = block_list_compress(&target_block_list);    /* Shift tidied target above the source image area. */    shift_vector = ICOORD(0, source_box.top() + BLOCK_SPACING)      - tidy_box.botleft();    block_list_move(&target_block_list, shift_vector);    tidy_box.move(shift_vector);  }  source_box += tidy_box;                                 // big enough for both  build_image_window(source_box);  do_view_cmd();  return tidy_box;}/********************************************************************** *  do_view_cmd() * *  View TARGET/View SOURCE command **********************************************************************/void do_view_cmd() {  viewing_source = !viewing_source;  image_win->Clear();  if (viewing_source) {    current_block_list = source_block_list;    current_image_changed = &source_changed;    other_block_list = &target_block_list;    other_image_changed = &target_changed;    do_re_display(&word_display);  }  else {    current_block_list = &target_block_list;    current_image_changed = &target_changed;    other_block_list = source_block_list;    other_image_changed = &source_changed;    do_re_display(&word_display);  }}/********************************************************************** *  do_write_file() * *  Serialise a block list to file * *  If writing image, tidy page and move to(0,0) first **********************************************************************/void do_write_file(           // serialise                  ) {  char* name = image_win->ShowInputDialog("File Name");  FILE *infp;                    // input file  char msg_str[80];  TBOX enclosing_box;                                 // if file exists  if ((infp = fopen(name, "r")) != NULL) {    fclose(infp);    sprintf(msg_str, "Overwrite file " "%s" "?(Y/N)", name);    int a = image_win->ShowYesNoDialog(msg_str);	if (a != 'y') { image_win->AddMessage("Write cancelled"); delete[] name; return; }  }  infp = fopen(name, "w");      // can we write to it?  if (infp == NULL) {  image_win->AddMessage("Cant write to file " "%s" "", name);    delete[] name;    return;  }  fclose(infp);  delete [] name;  if (!viewing_source && !target_block_list.empty()) {                                 // Tidy & move to(0,0)    image_win->AddMessage("Automatic tidy...");    viewing_source = TRUE;       // Stay viewing target!    enclosing_box = do_tidy_cmd();    block_list_move(&target_block_list, -enclosing_box.botleft());    image_win->AddMessage("Writing file...");    pgeditor_write_file(name, &target_block_list);                                 // move back    block_list_move(&target_block_list,      enclosing_box.botleft());  }  else {    image_win->AddMessage("Writing file...");    pgeditor_write_file(name, current_block_list);  }  image_win->AddMessage("Writing file...Done");  *current_image_changed = FALSE;}/********************************************************************** *  notify() * *  Event handler that processes incoming events, either forwarding *  them to process_cmd_win_event or process_image_event. * **********************************************************************/void PGEventHandler::Notify(const SVEvent* event) {  char myval = '0';  if (event->type == SVET_POPUP) {#ifndef GRAPHICS_DISABLEDve->Notify(event);#endif  } // These are handled by Var. Editor  else if (event->type == SVET_EXIT) { stillRunning = false; }  else if (event->type == SVET_MENU) {     if (strcmp(event->parameter, "true") == 0) { myval = 'T'; }     else if (strcmp(event->parameter, "false") == 0) { myval = 'F'; }     process_cmd_win_event(event->command_id, &myval);  }  else {     process_image_event(*event);     // else    pgeditor_show_point(*event);  }  current_word_quit.set_value(FALSE);  selection_quit.set_value(FALSE);                                 // replot all var wins}/********************************************************************** *  pgeditor_main() * *  Top level editor operation: *  Setup a new window and an according event handler * **********************************************************************/void pgeditor_main(BLOCK_LIST *blocks) {  source_block_list = blocks;  current_block_list = blocks;  if (current_block_list->empty())    return;  stillRunning = true;  build_image_window(block_list_bounding_box(source_block_list));  do_re_display(&word_display);  word_display_mode.turn_on_bit(DF_BOX);#ifndef GRAPHICS_DISABLED  ve = new VariablesEditor(image_win);#endif  PGEventHandler pgEventHandler;  image_win->AddEventHandler(&pgEventHandler);  image_win->AddMessageBox();  SVMenuNode* svMenuRoot = build_menu_new();  svMenuRoot->BuildMenu(image_win);  image_win->SetVisible(true);  image_win->AwaitEvent(SVET_DESTROY);}/********************************************************************** * pgeditor_msg() * * Display a message - in the command window if there is one, or to stdout **********************************************************************/void pgeditor_msg( // message display                  const char *msg) {    image_win->AddMessage(msg);}/********************************************************************** * pgeditor_read_file() * * Deserialise source file **********************************************************************/void pgeditor_read_file(                   // of serialised file                        STRING &name,                        BLOCK_LIST *blocks  // block list to add to                       ) {  int c;                         // input character  FILE *infp;                    // input file  BLOCK_IT block_it(blocks);  // iterator  BLOCK *block;                  // current block  ICOORD page_tr;                // topright of page  const char *filename_extension;  block_it.move_to_last();                                 // ptr to last dot  filename_extension = strrchr(name.string(), '.');  #ifdef __UNIX__  /*    TEXTROW*                tessrows;      TBLOB*                  tessblobs;      TPOINT                  tess_tr;    if (strcmp(filename_extension, ".r" ) == 0)    {      tprintf("Converting from .r file format.\n" );      tessrows = get_tess_row_file(name.string(),	// get the row file                          &tess_tr );      page_tr = ICOORD(tess_tr.x, tess_tr.y );      make_blocks_from_rows(tessrows, name.string(),	// reconstruct blocks                    page_tr, TRUE, &block_it );    }    else if (strcmp(filename_extension, ".b" ) == 0)    {      tprintf("Converting from .b file format.\n" );      tessblobs = get_tess_blob_file(name.string(),	// get the blob file                          &tess_tr );      page_tr = ICOORD(tess_tr.x, tess_tr.y );      make_blocks_from_blobs(tessblobs, name.string(),                              // reconstruct blocks                    page_tr, FALSE,blocks);    }     else*/  if (strcmp(filename_extension, ".pb") == 0) {    tprintf("Converting from .pb file format.\n");                                 // construct blocks    read_and_textord(name.string(), blocks);  }  else  #endif  if ((strcmp(filename_extension, ".pg") == 0) ||    // read a .pg file                               // or a .sp file (strcmp(filename_extension, ".sp") == 0)) {    tprintf("Reading %s file format.\n", filename_extension);    infp = fopen(name.string(), "r");    if (infp == NULL)      CANTOPENFILE.error("pgeditor_read_file", EXIT, name.string());    // can't open file    while(((c = fgetc(infp)) != EOF) &&(ungetc(c, infp) != EOF)) {                               // get one      block = BLOCK::de_serialise(infp);                               // add to list      block_it.add_after_then_move(block);    }    fclose(infp);  } else {    edges_and_textord(name.string(), blocks);  }}/********************************************************************** * pgeditor_show_point() * * Display the coordinates of a point in the command window **********************************************************************/void pgeditor_show_point( // display coords                         SVEvent *event) {  image_win->AddMessage("Pointing at(%d, %d)", event->x, event->y);}/********************************************************************** *  pgeditor_write_file() * *  Serialise a block list to file * **********************************************************************/void pgeditor_write_file(                   // serialise                         char *name,         // file name                         BLOCK_LIST *blocks  // block list to write                        ) {  FILE *infp;                    // input file  BLOCK_IT block_it(blocks);  // block iterator  BLOCK *block;                  // current block  ROW_IT row_it;                 // row iterator  infp = fopen(name, "w");      // create output file  if (infp == NULL)    CANTCREATEFILE.error("pgeditor_write_file", EXIT, name);  for (block_it.mark_cycle_pt();  !block_it.cycled_list(); block_it.forward()) {    block = block_it.extract();    row_it.set_to_list(block->row_list());    for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward())                                 // ensure correct      row_it.data()->recalc_bounding_box();    block->serialise(infp);     // serialize     non-empty    block_it.add_after_then_move(block);  }  fclose(infp);}/********************************************************************** *  process_cmd_win_event() * *  Process a command returned from the command window * (Just call the appropriate command handler) **********************************************************************/BOOL8 process_cmd_win_event(                 // UI command semantics                            inT32 cmd_event,  // which menu item?                            char *new_value   // any prompt data                           ) {  char msg[160];  BOOL8 exit = FALSE;  switch(cmd_event) {    case NULL_CMD_EVENT:      break;    case VIEW_CMD_EVENT:      do_view_cmd();      break;    case CHANGE_DISP_CMD_EVENT:    case DELETE_CMD_EVENT:    case CHANGE_TEXT_CMD_EVENT:    case TOGGLE_SEG_CMD_EVENT:    case DUMP_WERD_CMD_EVENT:    case SHOW_POINT_CMD_EVENT:    case ROW_SPACE_STAT_CMD_EVENT:    case BLOCK_SPACE_STAT_CMD_EVENT:    case SHOW_BLN_WERD_CMD_EVENT:    case SEGMENT_WERD_CMD_EVENT:      mode =(CMD_EVENTS) cmd_event;      break;    case COPY_CMD_EVENT:      mode =(CMD_EVENTS) cmd_event;      if (!viewing_source)        image_win->AddMessage("Can't COPY while viewing target!");      break;    case BOUNDING_BOX_CMD_EVENT:      if (new_value[0] == 'T')        word_display_mode.turn_on_bit(DF_BOX);      else        word_display_mode.turn_off_bit(DF_BOX);      mode = CHANGE_DISP_CMD_EVENT;      break;    case CORRECT_TEXT_CMD_EVENT:      if (new_value[0] == 'T')        word_display_mode.turn_on_bit(DF_TEXT);      else        word_display_mode.turn_off_bit(DF_TEXT);      mode = CHANGE_DISP_CMD_EVENT;      break;    case POLYGONAL_CMD_EVENT:      if (new_value[0] == 'T')        word_display_mode.turn_on_bit(DF_POLYGONAL);      else        word_display_mode.turn_off_bit(DF_POLYGONAL);      mode = CHANGE_DISP_CMD_EVENT;      break;    case BL_NORM_CMD_EVENT:      if (new_value[0] == 'T')        word_display_mode.turn_on_bit(DF_BN_POLYGONAL);      else        word_display_mode.turn_off_bit(DF_BN_POLYGONAL);      mode = CHANGE_DISP_CMD_EVENT;      break;    case BITMAP_CMD_EVENT:      if (new_value[0] == 'T')        word_display_mode.turn_on_bit(DF_EDGE_STEP);      else        word_display_mode.turn_off_bit(DF_EDGE_STEP);      mode = CHANGE_DISP_CMD_EVENT;      break;    case UNIFORM_DISP_CMD_EVENT:      do_re_display(&word_set_display);      *current_image_changed = TRUE;      break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?