-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditor.js
More file actions
754 lines (637 loc) · 24.1 KB
/
Copy pathEditor.js
File metadata and controls
754 lines (637 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
* This file is part of min.
*
* min is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* min is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with min. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2014 Richard Pospesel, Kevin Hart, Lei Hu, Siyu Zhu, David Stalnaker,
* Christopher Sasarak, Robert LiVolsi, Awelemdy Orakwue, and Richard Zanibbi
* (Document and Pattern Recognition Lab, RIT)
*/
/*
Contains code for managing the canvas. Originally there was supposed to be a canvas for each
layer (image layer, stroke layer, bounding box layer), but currently Min is implemented with
only one.
Methods:
1. save_state/restore_state: Save and restore Min states in their
entirety. Currently not used.
2.
3. add_selected_segment - Mark a segment as selected so the bounding box appears
around it.
4. update_selected_bb - Update the location and size of the bounding box based on
the location and sizes of the selected segments.
5. remove_segment/remove_selected_segment
6. add_canvas - create a canvas and add it to the list of contexts (canvases)
*/
function Editor() { }
// Possible EditorModes
Editor.modes = {
"DrawMode": new DrawMode(),
"RectSelectMode": new RectSelectMode(),
};
// Code for modes/mode switching
Editor.current_mode = null;
Editor.instance = null;
Editor.initialize = function(in_equation_canvas_name, in_toolbar_name)
{
// Make this red in annotation mode, as a reminder.
Editor.segment_color = "#FF4444";
// Support other tablet devices.
Editor.using_tablet = Modernizr.touch;
if(Editor.using_tablet == true)
{
$('body').addClass('touch');
// removeh over css
for(var i = 0; i < document.styleSheets.length; i++)
{
if ( document.styleSheets[ i ].rules != null ) {
for(var j = 0; j < document.styleSheets[i].rules.length; j++)
{
if(document.styleSheets[i].rules[j].cssText.match("hover") != null)
{
document.styleSheets[i].deleteRule(j--);
}
}
}
}
// Disabled. Kept as a clue for later on.
//document.getElementById( "getInkMLbutton" ).innerHTML = "Save InkML";
}
Editor.canvas_div = document.getElementById(String(in_equation_canvas_name));
Editor.toolbar_div = document.getElementById(String(in_toolbar_name));
// canvas size beneath
Editor.fit_to_screen();
// array of div elements
Editor.toolbar_buttons = new Array();
// set up our divs (canvas, toolbar);
Editor.build_buttons(in_toolbar_name);
// top left hand corner of our canvases relative to window
Editor.div_position = findPosition(Editor.canvas_div);
// build our layers (two of each for double buffering)
Editor.contexts = new Array();
Editor.canvases = new Array();
// get our convases
Editor.add_canvas(); // canvas 0 - pen strokes
// initialize managers
RenderManager.initialize(Editor.canvas_width, Editor.canvas_width, Editor.canvases.length);
CollisionManager.initialize();
RecognitionManager.initialize();
CorrectionMenu.initialize();
// initialize slider
Editor.slider = new Slider();
// create the event bindings.
Editor.setup_events();
// list of segments we deal with
Editor.segments = new Array();
// segments currently selected
Editor.selected_segments = new Array();
// bounding box before we started resizing
Editor.original_bb = null;
// current bounding box
Editor.selected_bb = null;
// variables for resizing
Editor.selected_size = new Vector2(0,0);
Editor.selected_position = new Vector2(0,0);
Editor.resize_offset = new Vector2(0,0);
// can be either "Stroke" or "Rectangle"
Editor.selection_method = null;
// start and end positions for rectangle selection
Editor.start_rect_selection = null;
Editor.end_rect_selection = null;
Editor.previous_stroke_position = null;
// the stroke currently being created
Editor.current_stroke = null;
// an image object the load method will set
Editor.temp_image = null;
// a text object the user can type into
Editor.current_text = null;
// the edge we are currently grabbing in resize mode
Editor.grabbed_edge = -1;
// initialize mouse variables
Editor.mouse1_down = false;
Editor.mouse2_down = false;
Editor.mouse_position = new Vector2(-1,-1);
Editor.mouse_position_prev = new Vector2(-1,-1);
Editor.undo_stack = new Array();
Editor.redo_stack = new Array();
Editor.action_list = new Array();
Editor.current_action = null;
Editor.FileReader = true;
// When we first start, switch to DrawMode.
$("#pen").trigger('click');
Editor.current_expression_id = 0;
}
Editor.set_current_expression_id = function(id) {
Editor.current_expression_id = id;
RenderManager.render();
Editor.clear_selected_segments();
}
Editor.save_state = function(clear)
{
var state = {
segments: [],
recognition_results: []
};
for (var i = 0; i < Editor.segments.length; i++) {
var seg = Editor.segments[i];
state.segments.push(seg.save_state());
}
for (var i = 0; i < RecognitionManager.result_table.length; i++) {
var result = RecognitionManager.result_table[i];
state.recognition_results.push(result.save_state());
}
if (clear) {
RecognitionManager.result_table = [];
}
return JSON.stringify(state);
}
Editor.restore_state = function(json_string)
{
var state = JSON.parse(json_string);
for (var i = 0; i < state.segments.length; i++) {
seg_state = state.segments[i];
var seg;
switch(seg_state.type_id) {
case PenStroke.type_id:
seg = PenStroke.restore_state(seg_state);
break;
case SymbolSegment.type_id:
seg = SymbolSegment.restore_state(seg_state);
break;
}
Editor.add_segment(seg);
}
for (var i = 0; i < state.recognition_results.length; i++) {
result = state.recognition_results[i];
RecognitionManager.result_table.push(RecognitionResult.restore_state(result));
}
RenderManager.render();
}
// RZ: new (?) method to obtain the unique set ids for selected objects.
Editor.get_selected_set_ids = function()
{
var idsFound = new Array();
for (var k = 0; k < Editor.selected_segments.length; k++)
{
nextId = Editor.selected_segments[k].set_id;
if ( ! idsFound.contains(nextId) )
idsFound.push(nextId);
}
return idsFound;
}
// determines if the given segment is in the selected list
Editor.segment_selected = function(in_segment)
{
for(var k = 0; k < Editor.selected_segments.length; k++)
{
if(Editor.selected_segments[k] == in_segment)
return true;
if(Editor.selected_segments[k].type_id == SegmentGroup.type_id)
{
if(Editor.selected_segments[k].contains_segment(in_segment))
return true;
}
}
return false;
}
// RZ: recover symbols using data and methods already in the grouping
// objects.
Editor.forget_symbol_groups = function()
{
// Undo symbol groupings backward in time (on the Undo stack)
for (var i = Editor.undo_stack.length - 1; i > -1; i--)
{
nextAction = Editor.undo_stack[i];
if (nextAction.isGrouping )
{
//console.log("forgetting..." + i);
nextAction.ForgetSymbolGrouping();
//console.log("Segment ids: " + Editor.get_current_segment_ids().length);
//console.log(Editor.get_current_segment_ids());
} else if (nextAction.isComposite)
{
for (var j = nextAction.action_list.length - 1; j > -1; j--)
{
nextCompositeAction = nextAction.action_list[j];
if (nextCompositeAction.isGrouping)
{
//console.log("forgetting...compound " + i + " action " + j);
nextCompositeAction.ForgetSymbolGrouping();
//console.log("Segment ids: " + Editor.get_current_segment_ids().length);
//console.log(Editor.get_current_segment_ids());
}
}
}
}
}
Editor.restore_symbol_groups = function()
{
// Undo symbol groupings backward in time (on the Undo stack)
for (var i = 0; i < Editor.undo_stack.length; i++)
{
nextAction = Editor.undo_stack[i];
if (nextAction.isGrouping )
{
//console.log("restoring..." + i);
nextAction.RestoreSymbolGrouping();
//console.log("Segment ids:" + Editor.get_current_segment_ids().length);
//console.log(Editor.get_current_segment_ids());
} else if (nextAction.isComposite)
{
for (var j = 0; j < nextAction.action_list.length; j++)
{
nextCompositeAction = nextAction.action_list[j];
if (nextCompositeAction.isGrouping)
{
//console.log("restoring...compound " + i + " action " + j);
nextCompositeAction.RestoreSymbolGrouping();
//console.log("Segment ids:" + Editor.get_current_segment_ids().length);
//console.log(Editor.get_current_segment_ids());
}
}
}
}
}
// RZ: New method to get the current segment id set. Have to
// perform a linear scan of all the (primitive) segments to
// obtain this.
Editor.get_current_segment_ids = function()
{
var result = new Array();
for (var i = 0; i < Editor.segments.length; i++) {
nextId = Editor.segments[i].set_id;
if (! result.contains(nextId) )
result.push(nextId);
}
return result;
}
Editor.get_segment_by_id = function(in_id)
{
var result = new Array();
for(var k = 0; k < Editor.segments.length; k++)
{
if(Editor.segments[k].set_id == in_id)
result.push(Editor.segments[k]);
}
return result;
}
// add a segment to the editor's selected list
Editor.add_selected_segment = function(in_segment)
{
if(Editor.selected_segments.contains(in_segment))
return;
var segment_mins = in_segment.worldMinPosition();
var segment_maxs = in_segment.worldMaxPosition();
var segment_draw_mins = in_segment.worldMinDrawPosition();
var segment_draw_maxs = in_segment.worldMaxDrawPosition();
// update selected bounding box
if(Editor.selected_segments.length == 0)
{
Editor.selected_bb = new BoundingBox(segment_mins, segment_maxs, segment_draw_mins, segment_draw_maxs);
}
else
{
for(var k = 0; k < Editor.selected_segments.length; k++)
if(Editor.selected_segments[k] == in_segment)
return;
// update logical extents
if(segment_mins.x < Editor.selected_bb.mins.x)
Editor.selected_bb.mins.x = segment_mins.x;
if(segment_mins.y < Editor.selected_bb.mins.y)
Editor.selected_bb.mins.y = segment_mins.y;
if(segment_maxs.x > Editor.selected_bb.maxs.x)
Editor.selected_bb.maxs.x = segment_maxs.x;
if(segment_maxs.y > Editor.selected_bb.maxs.y)
Editor.selected_bb.maxs.y = segment_maxs.y;
// update render extents
if(segment_draw_mins.x < Editor.selected_bb.render_mins.x)
Editor.selected_bb.render_mins.x = segment_draw_mins.x;
if(segment_draw_mins.y < Editor.selected_bb.render_mins.y)
Editor.selected_bb.render_mins.y = segment_draw_mins.y;
if(segment_draw_maxs.x > Editor.selected_bb.render_maxs.x)
Editor.selected_bb.render_maxs.x = segment_draw_maxs.x;
if(segment_draw_maxs.y > Editor.selected_bb.render_maxs.y)
Editor.selected_bb.render_maxs.y = segment_draw_maxs.y;
}
// finally add to the selected lsit
Editor.selected_segments.push(in_segment);
}
// updates the extents of the selected bounding box
Editor.update_selected_bb = function()
{
if(Editor.selected_segments.length == 0)
{
Editor.selected_bb = null;
return;
}
else if(Editor.selected_segments.length == 1)
{
Editor.selected_bb = new BoundingBox(Editor.selected_segments[0].worldMinPosition(), Editor.selected_segments[0].worldMaxPosition(), Editor.selected_segments[0].worldMinDrawPosition(), Editor.selected_segments[0].worldMaxDrawPosition());
}
else
{
var mins = Editor.selected_segments[0].worldMinPosition();
var maxs = Editor.selected_segments[0].worldMaxPosition();
var render_mins = Editor.selected_segments[0].worldMinDrawPosition();
var render_maxs = Editor.selected_segments[0].worldMaxDrawPosition();
for(var k = 1; k < Editor.selected_segments.length; k++)
{
// lgoical extents
var seg_mins = Editor.selected_segments[k].worldMinPosition();
var seg_maxs = Editor.selected_segments[k].worldMaxPosition();
if(seg_mins.x < mins.x)
mins.x = seg_mins.x;
if(seg_mins.y < mins.y)
mins.y = seg_mins.y;
if(seg_maxs.x > maxs.x)
maxs.x = seg_maxs.x;
if(seg_maxs.y > maxs.y)
maxs.y = seg_maxs.y;
// render extents
var render_seg_mins = Editor.selected_segments[k].worldMinDrawPosition();
var render_seg_maxs = Editor.selected_segments[k].worldMaxDrawPosition();
if(render_seg_mins.x < render_mins.x)
render_mins.x = render_seg_mins.x;
if(render_seg_mins.y < render_mins.y)
render_mins.y = render_seg_mins.y;
if(render_seg_maxs.x > render_maxs.x)
render_maxs.x = render_seg_maxs.x;
if(render_seg_maxs.y > render_maxs.y)
render_maxs.y = render_seg_maxs.y;
}
Editor.selected_bb = new BoundingBox(mins, maxs, render_mins, render_maxs);
}
}
// adds segment to be managed to the editor
Editor.add_segment = function(in_segment)
{
if(Editor.segments.contains(in_segment))
return;
Editor.segments.push(in_segment);
}
// removes a segment from the editor's control
Editor.remove_segment = function(in_segment)
{
if(in_segment == null) return;
var ui = Segment.unique_id(in_segment);
for(var k = 0; k < Editor.segments.length; k++)
{
if(Segment.unique_id(Editor.segments[k]) == ui)
{
Editor.segments.splice(k, 1);
break;
}
}
}
Editor.remove_selected_segment = function(in_segment)
{
if(in_segment == null) return;
for(var k = 0; k < Editor.selected_segments.length; k++)
if(Editor.selected_segments[k] == in_segment)
{
Editor.selected_segments.splice(k, 1);
return;
}
}
sort_segments = function(a, b)
{
return a.set_id - b.set_id;
}
// empties selected segments list
Editor.clear_selected_segments = function()
{
Editor.selected_segments.length = 0;
Editor.selected_bb = null;
Editor.selected_position = new Vector2(0,0);
Editor.selected_size = new Vector2(0,0);
}
// adds a new canvas to the contexts list
Editor.add_canvas = function()
{
var svg_canvas = Editor.build_canvas();
svg_canvas.style.zIndex = Editor.canvases.length;
Editor.canvases.push(svg_canvas);
Editor.canvas_div.appendChild(svg_canvas);
Editor.contexts.push(svg_canvas);
// OLD
// var canvas = Editor.build_canvas();
// canvas.style.zIndex = Editor.canvases.length;
// Editor.canvases.push(canvas);
// Editor.canvas_div.appendChild(canvas);
//Editor.contexts.push(canvas);
}
// Builds the canvas which is an SVG object
Editor.build_canvas = function()
{
var svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.setAttribute("onclick", "event.preventDefault();");
svg.setAttribute("ontouchmove", "event.preventDefault();");
svg.setAttribute("ontouchstart", "event.preventDefault();");
svg.setAttributeNS(null,"width", Editor.canvas_width);
svg.setAttributeNS(null,"height", Editor.canvas_height);
svg.style.position = "absolute";
svg.setAttributeNS(null,"tabindex", "0");
svg.style.left = "0px";
svg.style.top = "0px";
return svg;
}
Editor.add_action = function(action)
{
Editor.redo_stack.length = 0;
if(Editor.undo_stack.length > 0)
{
var prev_action = Editor.undo_stack.pop();
if(prev_action.shouldKeep() == true)
Editor.undo_stack.push(prev_action);
}
if(Editor.action_list.length > 0)
{
var prev_action = Editor.action_list.pop();
if(prev_action.shouldKeep() == true)
Editor.action_list.push(prev_action);
}
Editor.undo_stack.push(action);
Editor.current_action = action;
Editor.action_list.push(action);
}
Editor.undo = function()
{
if(Editor.button_states[Buttons.Undo].enabled == false)
return;
//Editor.clear_selected_segments();
while(Editor.undo_stack.length > 0)
{
var action = Editor.undo_stack.pop();
console.log("Undo stack contains: " + Editor.undo_stack.length);
console.log(" Undone: " + action);
if(action.shouldKeep())
{
action.Undo()
Editor.redo_stack.push(action);
switch(Editor.state)
{
case EditorState.StrokeSelecting:
Editor.state = EditorState.ReadyToStrokeSelect;
break;
case EditorState.RectangleSelecting:
Editor.state = EditorState.ReadyToRectangleSelect;
break;
case EditorState.MiddleOfStroke:
Editor.state = EditorState.ReadyToStroke;
break;
case EditorState.MiddleOfText:
Editor.state = EditorState.ReadyForText;
}
RenderManager.render();
Editor.action_list.push(new Undo());
return;
}
}
}
Editor.redo = function()
{
if(Editor.button_states[Buttons.Redo].enabled == false)
return;
if(Editor.redo_stack.length > 0)
{
var action = Editor.redo_stack.pop();
console.log("Redo stack contains: " + Editor.redo_stack.length);
console.log(" Redone: " + action);
action.Apply();
Editor.undo_stack.push(action);
Editor.action_list.push(new Redo());
RenderManager.render();
}
}
Editor.printUndoStack = function()
{
console.log("---");
for(var k = 0; k < Editor.undo_stack.length; k++)
{
console.log(Editor.undo_stack[k].toXML());
}
}
// Opens correction menu upon click on change recognition button
Editor.open_correction_menu = function(e)
{
if(Editor.selected_segments.length > 0){
var eq_canv = $("#equation_canvas").off(Editor.current_mode.event_strings.onUp,
Editor.current_mode.onUpAfterMove).off(Editor.current_mode.event_strings.onMove,
Editor.current_mode.beginMovingSegmentsFromMove).off(Editor.current_mode.event_strings.onDown,
Editor.current_mode.onDownSegmentsSelected);
RenderManager.bounding_box.style.visibility = "visible";
Editor.state = EditorState.SegmentsSelected;
Editor.relabel();
}
}
////////////////////////////////////////////////////////////////////////////////////
// Generating CROHME InkML Output
////////////////////////////////////////////////////////////////////////////////////
// DIFFERS from min : used by the slider in the GT version.
Editor.getStrokeString = function() {
var segments = new Array();
var segarray = Editor.segments.slice( 0 );
segarray.sort( function( o1, o2 ) { return o1.instance_id - o2.instance_id } );
var inkml="";
for ( var i = 0; i < segarray.length; i++ ) {
var stroke = segarray[ i ];
var strokeid = stroke.instance_id;
var segid = stroke.set_id;
// translation for absolute positioning
var tx = stroke.translation.x;
var ty = stroke.translation.y;
var sx = stroke.scale.x;
var sy = stroke.scale.y;
// add to proper segment
if ( segments[ segid ] == null ) segments[ segid ] = new Array();
segments[ segid ].push( strokeid );
// add stroke data to inkml
inkml += "\n<trace id=\"" + strokeid + "\" seg=\"" + segid + "\">";
var strokedata = new Array();
for ( var j = 0; j < stroke.points.length; j++ ) {
strokedata.push( ( ( stroke.points[ j ].x * sx ) + tx ) + " " + ( ( stroke.points[ j ].y * sy ) + ty ) );
}
inkml += strokedata.join( ", " );
inkml += "\n</trace>";
}
return inkml;
}
// RZ: moved here from Editor.align, so that it could be used elsewhere easily.
Editor.MathJaxRender = function(tex_math)
{
// Divs used for alignment
// !! RZ: This was tricky to find - this is how the expression
// becomes centered/moved after 'align' operations - see style.css.
var outer_div = document.createElement("div");
outer_div.setAttribute("id","outer_div");
// Get BB (min, max coordinates) for segments on the canvas
// or selection.
var s = Editor.get_seg_dimensions(Editor.segments);
if (Editor.selected_segments.length > 0 && EditorState.SegmentsSelected)
s = Editor.get_seg_dimensions(Editor.selected_segments);
// Main div with content in it
var elem = document.createElement("div");
elem.setAttribute("id","Alignment_Tex");
elem.style.visibility = "hidden";
elem.style.fontSize = "500%";
elem.style.position = "absolute";
elem.innerHTML = '\\[' + tex_math + '\\]'; // So MathJax can render it
outer_div.appendChild(elem);
Editor.canvas_div.appendChild(outer_div);
// Change rendered to SVG and have MathJax display it
// First re-render, then copy tex to the outer_div/canvas.
MathJax.Hub.Queue(["setRenderer", MathJax.Hub, "SVG"]);
MathJax.Hub.Queue(["Rerender", MathJax.Hub, elem],
[ function() {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, elem],
[Editor.copy_tex, elem, outer_div, s]);
}]);
}
// Generate (CROHME) InkML output.
Editor.getInkML = function() {
// Break groups apart so that segments match symbols, not whole symbol groups.
Editor.forget_symbol_groups();
var inkml = "<!-- CROHME InkML Data File -->\n<ink xmlns=\"http://www.w3.org/2003/InkML\">";
// Annotation data - for ground truthing tool (*** DIFFERS FROM new min)
inkml += "\n<traceFormat>";
inkml += "\n<channel name=\"X\" type=\"decimal\"/>";
inkml += "\n<channel name=\"Y\" type=\"decimal\"/>";
inkml += "\n</traceFormat>";
inkml += "\n<annotation type=\"truth\">$ " + Editor.slider.getCurrentExpression() + "$</annotation>";
inkml += "\n<annotation type=\"writer\">" + Editor.slider.getCurrentFileName() + "</annotation>";
//var strString = Editor.stroke_string; // Prevent modifying stroke attributes.
// RZ: line below will use stroke string; but when annotating, we only want to use
// the original stroke data collected from a writer.
var strString = Editor.getStrokeString();
// Find and replace segment id's, which may change.
// slice(0) copies all elements of the array, which we then sort by
// instance (i.e. primitive) identifier.
var segarray = Editor.segments.slice( 0 );
segarray.sort( function( o1, o2 ) { return o1.instance_id - o2.instance_id } );
for ( var i = 0; i < segarray.length; i++ ) {
var stroke = segarray[ i ];
var strokeid = stroke.instance_id;
var segid = stroke.set_id;
console.log("Replace stroke id " + strokeid + " segment id as " + segid);
// Replace segment id for each stroke.
//console.log("strokeid: " + strokeid + " segid: " + segid);
pattern = new RegExp("\"" + strokeid + "\" seg=\"\\d+\"","g");
replacement = "\"" + strokeid + "\" seg=\"" + segid + "\"";
strString = strString.replace(pattern,replacement);
}
inkml += strString;
inkml += "\n</ink>";
// Restore the symbol groupings (to keep canvas state consistent).
Editor.restore_symbol_groups();
return inkml;
}