tesseract  5.0.0-alpha-619-ge9db
scrollview.h
Go to the documentation of this file.
1 // File: scrollview.h
3 // Description: ScrollView
4 // Author: Joern Wanke
5 //
6 // (C) Copyright 2007, Google Inc.
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
18 //
19 // ScrollView is designed as an UI which can be run remotely. This is the
20 // client code for it, the server part is written in java. The client consists
21 // mainly of 2 parts:
22 // The "core" ScrollView which sets up the remote connection,
23 // takes care of event handling etc.
24 // The other part of ScrollView consists of predefined API calls through LUA,
25 // which can basically be used to get a zoomable canvas in which it is possible
26 // to draw lines, text etc.
27 // Technically, thanks to LUA, its even possible to bypass the here defined LUA
28 // API calls at all and generate a java user interface from scratch (or
29 // basically generate any kind of java program, possibly even dangerous ones).
30 
31 #ifndef TESSERACT_VIEWER_SCROLLVIEW_H_
32 #define TESSERACT_VIEWER_SCROLLVIEW_H_
33 // TODO(rays) Move ScrollView into the tesseract namespace.
34 #ifndef OCR_SCROLLVIEW_H__
35 
36 #include <cstdio>
37 #include <mutex>
38 
39 class ScrollView;
40 class SVNetwork;
41 class SVSemaphore;
42 struct SVPolyLineBuffer;
43 
45  SVET_DESTROY, // Window has been destroyed by user.
46  SVET_EXIT, // User has destroyed the last window by clicking on the 'X'.
47  SVET_CLICK, // Left button pressed.
48  SVET_SELECTION, // Left button selection.
49  SVET_INPUT, // There is some input (single key or a whole string).
50  SVET_MOUSE, // The mouse has moved with a button pressed.
51  SVET_MOTION, // The mouse has moved with no button pressed.
52  SVET_HOVER, // The mouse has stayed still for a second.
53  SVET_POPUP, // A command selected through a popup menu.
54  SVET_MENU, // A command selected through the menubar.
55  SVET_ANY, // Any of the above.
56 
57  SVET_COUNT // Array sizing.
58 };
59 
60 struct SVEvent {
61  ~SVEvent() { delete [] parameter; }
62  SVEvent* copy();
63  SVEventType type = SVET_DESTROY; // What kind of event.
64  ScrollView* window = nullptr; // Window event relates to.
65  char* parameter = nullptr; // Any string that might have been passed as argument.
66  int x = 0; // Coords of click or selection.
67  int y = 0;
68  int x_size = 0; // Size of selection.
69  int y_size = 0;
70  int command_id = 0; // The ID of the possibly associated event (e.g. MENU)
71  int counter = 0; // Used to detect which kind of event to process next.
72 
73  SVEvent() = default;
74  SVEvent(const SVEvent&);
75  SVEvent& operator=(const SVEvent&);
76 };
77 
78 // The SVEventHandler class is used for Event handling: If you register your
79 // class as SVEventHandler to a ScrollView Window, the SVEventHandler will be
80 // called whenever an appropriate event occurs.
82  public:
83  virtual ~SVEventHandler();
84 
85 // Gets called by the SV Window. Does nothing on default, overwrite this
86 // to implement the desired behaviour
87  virtual void Notify(const SVEvent* sve) { (void)sve; }
88 };
89 
90 // The ScrollView class provides the expernal API to the scrollviewer process.
91 // The scrollviewer process manages windows and displays images, graphics and
92 // text while allowing the user to zoom and scroll the windows arbitrarily.
93 // Each ScrollView class instance represents one window, and stuff is drawn in
94 // the window through method calls on the class. The constructor is used to
95 // create the class instance (and the window).
96 
97 class ScrollView {
98  public:
99 // Color enum for pens and brushes.
100  enum Color {
149  GREEN_YELLOW // Make sure this one is last.
150 };
151 
152  ~ScrollView();
153 
154 #ifndef GRAPHICS_DISABLED
155 
156 // Create a window. The pixel size of the window may be 0,0, in which case
157 // a default size is selected based on the size of your canvas.
158 // The canvas may not be 0,0 in size!
159  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
160  int x_canvas_size, int y_canvas_size);
161 // With a flag whether the x axis is reversed.
162  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
163  int x_canvas_size, int y_canvas_size, bool y_axis_reversed);
164 // Connect to a server other than localhost.
165  ScrollView(const char* name, int x_pos, int y_pos, int x_size, int y_size,
166  int x_canvas_size, int y_canvas_size, bool y_axis_reversed,
167  const char* server_name);
168 /*******************************************************************************
169 * Event handling
170 * To register as listener, the class has to derive from the SVEventHandler
171 * class, which consists of a notifyMe(SVEvent*) function that should be
172 * overwritten to process the event the way you want.
173 *******************************************************************************/
174 
175 // Add an Event Listener to this ScrollView Window.
176  void AddEventHandler(SVEventHandler* listener);
177 
178 // Block until an event of the given type is received.
180 
181 // Block until any event on any window is received.
183 
184 /*******************************************************************************
185 * Getters and Setters
186 *******************************************************************************/
187 
188 // Returns the title of the window.
189  const char* GetName() { return window_name_; }
190 
191 // Returns the unique ID of the window.
192  int GetId() { return window_id_; }
193 
194 /*******************************************************************************
195 * API functions for LUA calls
196 * the implementations for these can be found in svapi.cc
197 * (keep in mind that the window is actually created through the ScrollView
198 * constructor, so this is not listed here)
199 *******************************************************************************/
200 
201 // Draw a Pix on (x,y).
202  void Image(struct Pix* image, int x_pos, int y_pos);
203 
204 // Flush buffers and update display.
205  static void Update();
206 
207 // Exit the program.
208  static void Exit();
209 
210 // Update the contents of a specific window.
211  void UpdateWindow();
212 
213 // Erase all content from the window, but do not destroy it.
214  void Clear();
215 
216 // Set pen color with an enum.
217  void Pen(Color color);
218 
219 // Set pen color to RGB (0-255).
220  void Pen(int red, int green, int blue);
221 
222 // Set pen color to RGBA (0-255).
223  void Pen(int red, int green, int blue, int alpha);
224 
225 // Set brush color with an enum.
226  void Brush(Color color);
227 
228 // Set brush color to RGB (0-255).
229  void Brush(int red, int green, int blue);
230 
231 // Set brush color to RGBA (0-255).
232  void Brush(int red, int green, int blue, int alpha);
233 
234 // Set attributes for future text, like font name (e.g.
235 // "Times New Roman"), font size etc..
236 // Note: The underlined flag is currently not supported
237  void TextAttributes(const char* font, int pixel_size,
238  bool bold, bool italic, bool underlined);
239 
240 // Draw line from (x1,y1) to (x2,y2) with the current pencolor.
241  void Line(int x1, int y1, int x2, int y2);
242 
243 // Set the stroke width of the pen.
244  void Stroke(float width);
245 
246 // Draw a rectangle given upper left corner and lower right corner.
247 // The current pencolor is used as outline, the brushcolor to fill the shape.
248  void Rectangle(int x1, int y1, int x2, int y2);
249 
250 // Draw an ellipse centered on (x,y).
251 // The current pencolor is used as outline, the brushcolor to fill the shape.
252  void Ellipse(int x, int y, int width, int height);
253 
254 // Draw text with the current pencolor
255  void Text(int x, int y, const char* mystring);
256 
257 // Draw an image from a local filename. This should be faster than createImage.
258 // WARNING: This only works on a local machine. This also only works image
259 // types supported by java (like bmp,jpeg,gif,png) since the image is opened by
260 // the server.
261  void Image(const char* image, int x_pos, int y_pos);
262 
263 // Set the current position to draw from (x,y). In conjunction with...
264  void SetCursor(int x, int y);
265 
266 // ...this function, which draws a line from the current to (x,y) and then
267 // sets the new position to the new (x,y), this can be used to easily draw
268 // polygons using vertices
269  void DrawTo(int x, int y);
270 
271 // Set the SVWindow visible/invisible.
272  void SetVisible(bool visible);
273 
274 // Set the SVWindow always on top or not always on top.
275  void AlwaysOnTop(bool b);
276 
277 // Shows a modal dialog with "msg" as question and returns 'y' or 'n'.
278  int ShowYesNoDialog(const char* msg);
279 
280 // Shows a modal dialog with "msg" as question and returns a char* string.
281 // Constraint: As return, only words (e.g. no whitespaces etc.) are allowed.
282  char* ShowInputDialog(const char* msg);
283 
284 // Adds a messagebox to the SVWindow. This way, it can show the messages...
285  void AddMessageBox();
286 
287 // ...which can be added by this command.
288 // This is intended as an "debug" output window.
289  void AddMessage(const char* format, ...);
290 
291 // Zoom the window to the rectangle given upper left corner and
292 // lower right corner.
293  void ZoomToRectangle(int x1, int y1, int x2, int y2);
294 
295 // Custom messages (manipulating java code directly) can be send through this.
296 // Send a message to the server and attach the Id of the corresponding window.
297 // Note: This should only be called if you are know what you are doing, since
298 // you are fiddling with the Java objects on the server directly. Calling
299 // this just for fun will likely break your application!
300 // It is public so you can actually take use of the LUA functionalities, but
301 // be careful!
302  void SendMsg(const char* msg, ...);
303 
304 // Custom messages (manipulating java code directly) can be send through this.
305 // Send a message to the server without adding the
306 // window id. Used for global events like Exit().
307 // Note: This should only be called if you are know what you are doing, since
308 // you are fiddling with the Java objects on the server directly. Calling
309 // this just for fun will likely break your application!
310 // It is public so you can actually take use of the LUA functionalities, but
311 // be careful!
312  static void SendRawMessage(const char* msg);
313 
314 /*******************************************************************************
315 * Add new menu entries to parent. If parent is "", the entry gets added to the
316 * main menubar (toplevel).
317 *******************************************************************************/
318 // This adds a new submenu to the menubar.
319  void MenuItem(const char* parent, const char* name);
320 
321 // This adds a new (normal) menu entry with an associated eventID, which should
322 // be unique among menubar eventIDs.
323  void MenuItem(const char* parent, const char* name, int cmdEvent);
324 
325  // This adds a new checkbox entry, which might initially be flagged.
326  void MenuItem(const char* parent, const char* name,
327  int cmdEvent, bool flagged);
328 
329 // This adds a new popup submenu to the popup menu. If parent is "", the entry
330 // gets added at "toplevel" popupmenu.
331  void PopupItem(const char* parent, const char* name);
332 
333 // This adds a new popup entry with the associated eventID, which should be
334 // unique among popup eventIDs.
335 // If value and desc are given, on a click the server will ask you to modify
336 // the value and return the new value.
337  void PopupItem(const char* parent, const char* name,
338  int cmdEvent, const char* value, const char* desc);
339 
340 // Returns the correct Y coordinate for a window, depending on whether it might
341 // have to be flipped (by ySize).
342  int TranslateYCoordinate(int y);
343 
344  private:
345 // Transfers a binary Image.
346  void TransferBinaryImage(struct Pix* image);
347 // Transfers a gray scale Image.
348  void TransferGrayImage(struct Pix* image);
349 // Transfers a 32-Bit Image.
350  void Transfer32bppImage(struct Pix* image);
351 
352 // Sets up ScrollView, depending on the variables from the constructor.
353  void Initialize(const char* name, int x_pos, int y_pos, int x_size,
354  int y_size, int x_canvas_size, int y_canvas_size,
355  bool y_axis_reversed, const char* server_name);
356 
357 // Send the current buffered polygon (if any) and clear it.
358  void SendPolygon();
359 
360  // Start the message receiving thread.
361  static void MessageReceiver();
362 
363 // Place an event into the event_table (synchronized).
364  void SetEvent(SVEvent* svevent);
365 
366 // Wake up the semaphore.
367  void Signal();
368 
369 // Returns the unique, shared network stream.
370  static SVNetwork* GetStream() { return stream_; }
371 
372  // Starts a new event handler.
373  // Called asynchronously whenever a new window is created.
374  void StartEventHandler();
375 
376 // Escapes the ' character with a \, so it can be processed by LUA.
377  char* AddEscapeChars(const char* input);
378 
379  // The event handler for this window.
380  SVEventHandler* event_handler_;
381  // The name of the window.
382  const char* window_name_;
383  // The id of the window.
384  int window_id_;
385  // The points of the currently under-construction polyline.
386  SVPolyLineBuffer* points_;
387  // Whether the axis is reversed.
388  bool y_axis_is_reversed_;
389  // Set to true only after the event handler has terminated.
390  bool event_handler_ended_;
391  // If the y axis is reversed, flip all y values by ySize.
392  int y_size_;
393  // # of created windows (used to assign an id to each ScrollView* for svmap).
394  static int nr_created_windows_;
395  // Serial number of sent images to ensure that the viewer knows they
396  // are distinct.
397  static int image_index_;
398 
399  // The stream through which the c++ client is connected to the server.
400  static SVNetwork* stream_;
401 
402  // Table of all the currently queued events.
403  SVEvent* event_table_[SVET_COUNT];
404 
405  // Mutex to access the event_table_ in a synchronized fashion.
406  std::mutex* mutex_;
407 
408  // Semaphore to the thread belonging to this window.
409  SVSemaphore* semaphore_;
410 #endif // GRAPHICS_DISABLED
411 };
412 
413 #endif // OCR_SCROLLVIEW_H__
414 #endif // TESSERACT_VIEWER_SCROLLVIEW_H_
ScrollView::GREY
Definition: scrollview.h:133
ScrollView::SANDY_BROWN
Definition: scrollview.h:121
ScrollView
Definition: scrollview.h:97
ScrollView::Brush
void Brush(Color color)
Definition: scrollview.cpp:723
SVET_DESTROY
Definition: scrollview.h:45
SVEvent::y_size
int y_size
Definition: scrollview.h:69
SVSemaphore
Definition: svutil.h:44
SVEventType
SVEventType
Definition: scrollview.h:44
ScrollView::DARK_GREEN
Definition: scrollview.h:124
ScrollView::SendRawMessage
static void SendRawMessage(const char *msg)
Definition: scrollview.cpp:410
ScrollView::MAROON
Definition: scrollview.h:135
ScrollView::AddMessage
void AddMessage(const char *format,...)
Definition: scrollview.cpp:560
SVET_CLICK
Definition: scrollview.h:47
SVEvent::copy
SVEvent * copy()
Definition: scrollview.cpp:59
SVET_INPUT
Definition: scrollview.h:49
ScrollView::SetVisible
void SetVisible(bool visible)
Definition: scrollview.cpp:548
ScrollView::ScrollView
ScrollView(const char *name, int x_pos, int y_pos, int x_size, int y_size, int x_canvas_size, int y_canvas_size)
Calls Initialize with default argument for server_name_ & y_axis_reversed.
Definition: scrollview.cpp:260
ScrollView::LIGHT_GREY
Definition: scrollview.h:130
ScrollView::AddMessageBox
void AddMessageBox()
Definition: scrollview.cpp:577
SVET_POPUP
Definition: scrollview.h:53
ScrollView::DARK_SLATE_GREY
Definition: scrollview.h:131
ScrollView::MenuItem
void MenuItem(const char *parent, const char *name)
Definition: scrollview.cpp:679
SVEventHandler
Definition: scrollview.h:81
SVEvent::counter
int counter
Definition: scrollview.h:71
SVET_EXIT
Definition: scrollview.h:46
ScrollView::DARK_OLIVE_GREEN
Definition: scrollview.h:125
ScrollView::STEEL_BLUE
Definition: scrollview.h:118
ScrollView::Clear
void Clear()
Definition: scrollview.cpp:588
ScrollView::BROWN
Definition: scrollview.h:120
SVPolyLineBuffer
Definition: scrollview.cpp:45
ScrollView::CYAN
Definition: scrollview.h:107
ScrollView::NONE
Definition: scrollview.h:101
ScrollView::Pen
void Pen(Color color)
Definition: scrollview.cpp:717
ScrollView::DrawTo
void DrawTo(int x, int y)
Definition: scrollview.cpp:524
ScrollView::Image
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:763
ScrollView::MEDIUM_BLUE
Definition: scrollview.h:113
ScrollView::DIM_GREY
Definition: scrollview.h:132
ScrollView::Stroke
void Stroke(float width)
Definition: scrollview.cpp:593
SVET_MOTION
Definition: scrollview.h:51
ScrollView::ZoomToRectangle
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:755
ScrollView::AwaitEventAnyWindow
SVEvent * AwaitEventAnyWindow()
Definition: scrollview.cpp:464
ScrollView::PLUM
Definition: scrollview.h:139
ScrollView::BLUE
Definition: scrollview.h:108
ScrollView::VIOLET
Definition: scrollview.h:147
ScrollView::GetName
const char * GetName()
Definition: scrollview.h:187
ScrollView::ORCHID
Definition: scrollview.h:137
ScrollView::GOLD
Definition: scrollview.h:122
ScrollView::FOREST_GREEN
Definition: scrollview.h:126
SVEvent::y
int y
Definition: scrollview.h:67
ScrollView::DARK_TURQUOISE
Definition: scrollview.h:146
ScrollView::BLACK
Definition: scrollview.h:102
SVEvent::parameter
char * parameter
Definition: scrollview.h:65
ScrollView::KHAKI
Definition: scrollview.h:134
ScrollView::ORANGE
Definition: scrollview.h:136
ScrollView::TAN
Definition: scrollview.h:144
ScrollView::UpdateWindow
void UpdateWindow()
Definition: scrollview.cpp:703
ScrollView::LIGHT_BLUE
Definition: scrollview.h:112
ScrollView::MAGENTA
Definition: scrollview.h:109
ScrollView::Exit
static void Exit()
Definition: scrollview.cpp:582
SVEventHandler::~SVEventHandler
virtual ~SVEventHandler()
ScrollView::AlwaysOnTop
void AlwaysOnTop(bool b)
Definition: scrollview.cpp:554
SVET_MOUSE
Definition: scrollview.h:50
ScrollView::SKY_BLUE
Definition: scrollview.h:116
SVET_COUNT
Definition: scrollview.h:57
ScrollView::YELLOW
Definition: scrollview.h:105
ScrollView::TranslateYCoordinate
int TranslateYCoordinate(int y)
Definition: scrollview.cpp:825
ScrollView::WHITE
Definition: scrollview.h:103
SVEvent::operator=
SVEvent & operator=(const SVEvent &)
ScrollView::AQUAMARINE
Definition: scrollview.h:110
ScrollView::SendMsg
void SendMsg(const char *msg,...)
Send a message to the server, attaching the window id.
Definition: scrollview.cpp:392
SVEvent::SVEvent
SVEvent()=default
ScrollView::WHEAT
Definition: scrollview.h:148
SVEvent::type
SVEventType type
Definition: scrollview.h:63
ScrollView::~ScrollView
~ScrollView()
Definition: scrollview.cpp:360
ScrollView::RED
Definition: scrollview.h:104
ScrollView::PINK
Definition: scrollview.h:138
SVET_ANY
Definition: scrollview.h:55
ScrollView::AwaitEvent
SVEvent * AwaitEvent(SVEventType type)
Definition: scrollview.cpp:443
SVEvent::x_size
int x_size
Definition: scrollview.h:68
ScrollView::LIME_GREEN
Definition: scrollview.h:127
ScrollView::GOLDENROD
Definition: scrollview.h:123
ScrollView::VIOLET_RED
Definition: scrollview.h:142
ScrollView::SALMON
Definition: scrollview.h:143
ScrollView::YELLOW_GREEN
Definition: scrollview.h:129
ScrollView::TextAttributes
void TextAttributes(const char *font, int pixel_size, bool bold, bool italic, bool underlined)
Definition: scrollview.cpp:634
ScrollView::PALE_GREEN
Definition: scrollview.h:128
SVEvent
Definition: scrollview.h:60
SVEventHandler::Notify
virtual void Notify(const SVEvent *sve)
Definition: scrollview.h:87
ScrollView::GREEN
Definition: scrollview.h:106
ScrollView::DARK_SLATE_BLUE
Definition: scrollview.h:111
SVET_HOVER
Definition: scrollview.h:52
SVEvent::command_id
int command_id
Definition: scrollview.h:70
ScrollView::Line
void Line(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:531
ScrollView::TURQUOISE
Definition: scrollview.h:145
ScrollView::AddEventHandler
void AddEventHandler(SVEventHandler *listener)
Add an Event Listener to this ScrollView Window.
Definition: scrollview.cpp:415
SVEvent::x
int x
Definition: scrollview.h:66
tesstrain_utils.type
type
Definition: tesstrain_utils.py:141
ScrollView::Update
static void Update()
Definition: scrollview.cpp:708
SVET_MENU
Definition: scrollview.h:54
ScrollView::SetCursor
void SetCursor(int x, int y)
Definition: scrollview.cpp:518
ScrollView::CORAL
Definition: scrollview.h:119
ScrollView::Text
void Text(int x, int y, const char *mystring)
Definition: scrollview.cpp:651
ScrollView::Color
Color
Definition: scrollview.h:100
SVEvent::window
ScrollView * window
Definition: scrollview.h:64
ScrollView::INDIAN_RED
Definition: scrollview.h:140
ScrollView::ShowInputDialog
char * ShowInputDialog(const char *msg)
Definition: scrollview.cpp:731
ScrollView::ShowYesNoDialog
int ShowYesNoDialog(const char *msg)
Definition: scrollview.cpp:743
ScrollView::MIDNIGHT_BLUE
Definition: scrollview.h:114
ScrollView::GetId
int GetId()
Definition: scrollview.h:190
ScrollView::Ellipse
void Ellipse(int x, int y, int width, int height)
Definition: scrollview.cpp:608
ScrollView::Rectangle
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:599
ScrollView::SLATE_BLUE
Definition: scrollview.h:117
ScrollView::GREEN_YELLOW
Definition: scrollview.h:149
SVNetwork
Definition: svutil.h:66
SVEvent::~SVEvent
~SVEvent()
Definition: scrollview.h:61
SVET_SELECTION
Definition: scrollview.h:48
ScrollView::ORANGE_RED
Definition: scrollview.h:141
ScrollView::PopupItem
void PopupItem(const char *parent, const char *name)
Definition: scrollview.cpp:685
ScrollView::NAVY_BLUE
Definition: scrollview.h:115