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