-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEditor.Events.js
More file actions
1486 lines (1273 loc) · 49.5 KB
/
Copy pathEditor.Events.js
File metadata and controls
1486 lines (1273 loc) · 49.5 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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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)
*/
/*
This class contains some of the logic and events for the Editor as a whole.
Events are defined for the following:
onImageLoad (browsers supporting FileReader only)
Methods that change the state of the editor are:
1. groupTool - Adds selected segments to one segment group.
2. deleteTool
3. typeTool
4. relabel
5. clear
Other methods:
1. align - Align objects on the canvas and populate the query bar with a LaTeX
string.
2. createGrid - Convert objects into a grid (e.g. matrix).
2. search - submit the LaTeX query to a search engine.
etc
*/
var EditorState =
{
// select tool states
"ReadyToStrokeSelect" : 14,
"StrokeSelecting" : 15,
"ReadyToRectangleSelect" : 2,
"RectangleSelecting" : 3,
// pen states
"ReadyToStroke" : 4,
"MiddleOfStroke" : 5,
// text tool states
"ReadyForText" : 6, "MiddleOfText" : 7,
// Segment (and primitive) selection, labeling
"SegmentsSelected" : 8,
"MovingSegments" : 9,
"Resizing" : 10,
"Relabeling" : 11,
"PinchResizing": 12,
// Editing text box.
"InTextBox" : 13,
// New: moving a symbol in edit mode; touch and hold state.
};
var TouchAndHoldState = {
"NoTouchAndHold": 0,
"MouseDownAndStationary": 1,
"FingerDownAndStationary": 2 // same as the above state, but happening on a touchscreen
};
Editor.lastEvent = null;
Editor.moveQueue = null;
Editor.touchAndHoldFlag = TouchAndHoldState.NoTouchAndHold;
Editor.texFiles = "";
Editor.texFileList = "";
Editor.dataCollection = false;
Editor.stroke_string = "";
// Called when Min is starting up. Just calls other methods
Editor.setup_events = function()
{
var button_index = 0; // Sets default initial state (pen/touch entry)
Editor.timeStamp = null;
Editor.prevTimeStamp = null;
PermEvents.setup_window();
PermEvents.setup_toolbar();
PermEvents.setup_document();
PermEvents.check_url();
//var dataCollection = Editor.dataCollection;
// Select the pen tool
Editor.button_states[Buttons.Pen].enabled = true;
}
// RESTORED from earlier code.
Editor.setCursor = function ()
{
var canvas = document.getElementById("equation_canvas");
switch (Editor.state)
{
case EditorState.StrokeSelecting:
case EditorState.ReadyToStrokeSelect:
canvas.style.cursor = "crosshair";
break;
default:
canvas.style.cursor = "default";
break;
}
}
Editor.fit_to_screen = function(event)
{
var root_div = document.getElementById("equation_editor_root");
root_div.style.width = window.innerWidth + "px";
root_div.style.height = window.innerHeight + "px";
Editor.canvas_width = Editor.canvas_div.offsetWidth;
Editor.canvas_height = Editor.canvas_div.offsetHeight;
Editor.div_position = findPosition(Editor.canvas_div);
window.scroll(0,0);
}
Editor.mapCanvasBackspace = function(e)
{
if(e.keyCode == KeyCode.backspace)
{
// HACK: no text box now.
// (for ground truth tools)
textBox = document.getElementById("tex_result");
if (document.querySelector(":focus") == textBox) {
// Act as normal.
} else {
// If we're not in the text box, need to avoid going 'back'
// when we press backspace in Safari and some other browsers.
switch (Editor.state)
{
case EditorState.MiddleOfText:
//e.preventDefault();
//Editor.current_text.popCharacter();
break;
default:
// Otherwise, delete any selections.
e.preventDefault();
Editor.deleteTool();
$("#equation_canvas").off("keypress",Editor.current_mode.close_mode()).on("keypress", Editor.current_mode.init_mode());
break;
}
}
if(e.keyCode == KeyCode.del) {
Editor.deleteTool();
$("#equation_canvas").off("keypress",Editor.current_mode.close_mode()).on("keypress", Editor.current_mode.init_mode());
}
}
}
Editor.onKeyPress = function(e)
{
// For touch-and-hold
Editor.lastEvent = e;
if (Editor.touchAndHoldFlag == TouchAndHoldState.MouseDownAndStationary)
return;
if(e.keyCode == KeyCode.enter && Editor.state == EditorState.MiddleOfText) {
$(Editor.canvas_div).off(Editor.current_mode.event_strings.onDown, Editor.current_mode.stopTextInput);
Editor.current_mode.stopTextInput(e);
e.stopPropagation();
Editor.enterHit = true;
return;
}
// RLAZ: skip deletes (46) and backspaces (8), handled in mapCanvasBackspace()
if(e.keyCode == KeyCode.backspace || e.keyCode == KeyCode.del)
return;
}
// Calls DRACULAE
Editor.align = function()
{
switch(Editor.state)
{
case EditorState.MiddleOfText:
Editor.current_text.finishEntry();
if(Editor.current_action.toString() == "EditText")
Editor.current_action.set_current_text(Editor.current_text.text);
else if(Editor.current_action.toString() == "AddSegments")
Editor.current_action.buildSegmentXML();
Editor.current_text = null;
}
RenderManager.clear_canvas();
// an array of tuples
// recognition result, min bb, max bb, set id
var data = new Array();
// iterate through all of the segment sets and identify each
// bounding box (and symbol)
// segments are in order by set id
// add null pointer so we can easily render last set in list
var segSet = Editor.segments;
if (Editor.state == EditorState.SegmentsSelected &&
Editor.selected_segments.length > 0)
segSet = Editor.selected_segments;
segSet.push(null);
var set_segments = new Array();
for(var k = 0; k < segSet.length; k++)
{
var seg = segSet[k];
if(set_segments.length == 0)
set_segments.push(seg);
else if(seg == null || seg.set_id != set_segments[0].set_id)
{
var mins = set_segments[0].worldMinPosition();
var maxs = set_segments[0].worldMaxPosition();
for(var j = 1; j < set_segments.length ; j++)
{
var seg_min = set_segments[j].worldMinPosition();
var seg_max = set_segments[j].worldMaxPosition();
if(seg_min.x < mins.x)
mins.x = seg_min.x;
if(seg_min.y < mins.y)
mins.y = seg_min.y;
if(seg_max.x > maxs.x)
maxs.x = seg_max.x;
if(seg_max.y > maxs.y)
maxs.y = seg_max.y;
}
var origMins = mins.clone();
var origMaxs = maxs.clone();
var recognition_result = RecognitionManager.getRecognition(set_segments[0].set_id);
// If a text segment, account for the DRACULAE making
// x's smaller than t's, etc
if (set_segments[0].constructor == SymbolSegment) {
size = Vector2.Subtract(maxs, mins);
if (-1 != $.inArray(set_segments[0].text, Editor.x_height_chars)) {
mins.y += size.y / 2;
}
if (-1 != $.inArray(set_segments[0].text, Editor.descender_chars)) {
mins.y += size.y / 2;
maxs.y += size.y / 2;
}
}
var tuple = new Tuple(recognition_result, mins, maxs, origMins, origMaxs);
data.push(tuple);
set_segments.length = 0;
set_segments.push(seg);
}
else
set_segments.push(seg);
}
// Remove the null segment that we pushed on the list.
segSet.pop();
// Construct URL request here.
var sb = new StringBuilder();
var subexprs = new Array();
var subBSTs = new Array();
sb.append("?segments=<SegmentList>");
for(var k = 0; k < data.length; k++)
{
var t = data[k];
sb.append("<Segment symbol=\"");
if(t.item1.symbols.length == 0)
sb.append("?\" min=\"");
else {
var latex;
if(t.item1.symbols[0] == "<")
latex = "lt";
else if(t.item1.symbols[0] == ">")
latex = "gt";
else if (t.item1.symbols[0] == ",")
latex = ",";
else if (t.item1.symbols[0] == "/")
latex = "/";
else
latex = RecognitionManager.symbol_to_latex[ t.item1.symbols[0] ];
//console.log("HELP! : " + t.item1.symbols[0] + " LATEX: " + latex);
if(latex == null){
latex = RecognitionManager.symbol_to_latex[RecognitionManager.unicode_to_symbol[ t.item1.symbols[0].toLowerCase() ]];
if(latex == null) {
// symbols not in our generic table: use substitutions to
// avoid problems with tex characters (treated as centered objects
// by DRACULAE).
sb.append("SID" + subexprs.length).append("\" min=\"");
subexprs.push(new Tuple(t.item1.symbols[0].trim(), "SID" + subexprs.length));
subBSTs.push(new Tuple(t.item1.bst, "SID" + subBSTs.length));
}
else
sb.append(latex).append("\" min=\"");
}
else
sb.append(latex).append("\" min=\"");
}
// Simpler, single option from before....
//sb.append(t.item1.symbols[0]).append("\" min=\"");
sb.append(new Vector2(Math.floor(t.item2.x), Math.floor(t.item2.y)).toString()).append("\" max=\"");
sb.append(new Vector2(Math.floor(t.item3.x), Math.floor(t.item3.y)).toString()).append("\" id=\"");
sb.append(t.item1.set_id).append("\"/>");
}
sb.append("</SegmentList>");
console.log(sb.toString());
//console.log("Segment string");
var mergedSymbols = false;
$.ajax
(
{
url: Editor.align_server_url + sb.toString(),
success: function(in_data, textStatus, xmlhttp)
{
// parse response here
var new_dimensions = new Array();
// parse response xml
// RZ: modified draculae server to provide BST information.
var xmldoc = in_data;
var segment_nodes = xmldoc.getElementsByTagName("Segment");
var tex_nodes = xmldoc.getElementsByTagName( "TexString" );
var bsts = xmldoc.getElementsByTagName("BST");
// Addition from new min.
var joined_nodes = xmldoc.getElementsByTagName("JoinedSegments");
// Composite action to collect symbol and subexpression merges.
var allMergeActions = new CompositeAction();
if(segment_nodes.length == 0)
{
alert("DRACULAE Error: " + in_data);
return;
}
tex_math = "?";
// Update the current slide with the TeX.
if ( tex_nodes.length != 0 ) {
var tex_string = tex_nodes[ 0 ].textContent;
// get just the math, removing spaces; small change to preserve spacing.
var tex_math = tex_string.split("$").slice(1,-1).join("").replace( /\s+/g, " " );
// Replace subexpressions and special characters.
for (var i = 0; i < subexprs.length; i++)
tex_math = tex_math.replace(subexprs[i].item2, subexprs[i].item1);
// Pattern replacements to match CROHME 2014 expression grammar.
// Replacements for matrices/vectors bound by parentheses ('()'),
// brackets ('[]'), and braces ('{}').
tex_math = tex_math.replace(/\(\s*\\begin\{array\}\{[^\}]*\}/g, "\\begin{pmatrix}");
tex_math = tex_math.replace(/\\end\{array\}\s*\)/g, "\\end{pmatrix}");
tex_math = tex_math.replace(/\\lbrack\s*\\begin\{array\}\{[^\}]*\}/g, "\\begin{bmatrix}");
tex_math = tex_math.replace(/\\end\{array\}\s*\\rbrack/g, "\\end{bmatrix}");
tex_math = tex_math.replace(/\{\s*\\begin\{array\}\{[^\}]*\}/g, "\\begin{Bmatrix}");
tex_math = tex_math.replace(/\\end\{array\}\s*\}/g, "\\end{Bmatrix}");
tex_math = tex_math.replace(/\|\s*\\begin\{array\}\{[^\}]*\}/g, "\\begin{vmatrix}");
tex_math = tex_math.replace(/\\end\{array\}\s*\|/g, "\\end{vmatrix}");
console.log("REWRITTEN LATEX:\n" + tex_math);
// Filter \left and \right demarcators, handle delimiters.
// CHANGE: use pmatrix, bmatrix etc. indicators (see Action.GroupSegments)
/*tex_math = tex_math.replace(/\\left/g, "");
tex_math = tex_math.replace(/\\right/g, "");
tex_math = tex_math.replace(/\(/g, "\\left(");
tex_math = tex_math.replace(/\)/g, "\\right)");
tex_math = tex_math.replace(/\[/g, "\\left[");
tex_math = tex_math.replace(/\]/g, "\\right]");
tex_math = tex_math.replace(/\\{/g, "\\left\{");
tex_math = tex_math.replace(/\\}/g, "\\right\}");
tex_math = tex_math.replace(/\\lbrack/g, "\\left\\lbrack");
tex_math = tex_math.replace(/\\rbrack/g, "\\right\\rbrack");*/
// Clean up and save TeX to the current slider pane.
// Ampersand (col. seprators) must be handled specially for HTML.
var slider_tex = tex_math;
slider_tex = slider_tex.replace(/&/g, "&");
Editor.slider.updateSlide( null, slider_tex );
// Construct BST; save to current slider pane.
var bstFinal = bsts[0].textContent;
// Replace subexpressions and special characters.
for (var i = 0; i < subBSTs.length; i++)
bstFinal = bstFinal.replace(subBSTs[i].item2, subBSTs[i].item1);
// HACK! Remove SUBSC for ',' '.' and \dots.
// DEBUG: '.' matches any character - had to remove this.
bstFinal = bstFinal.replace(/,\s*SUBSC\s\{\s*([^\}]+)\}/g, "\n$1");
bstFinal = bstFinal.replace(/\.\s*SUBSC\s\{\s*([^\}]+)\}/g, "\n$1");
bstFinal = bstFinal.replace(/ldots\s*SUBSC\s\{\s*([^\}]+)\}/g, "\n$1");
// Don't forget to replace ',' by 'COMMA')
//bstFinal = bstFinal.replace(/\n\,/g, "\nCOMMA");
Editor.slider.updateBST(bstFinal);
//console.log("Align BST Output ------------------------");
//console.log(bstFinal);
// Modification from newer min: represent and classify merged
// symbols (e.g. when DRACULAE detects two lines as an '=', etc.)
// Go through and change text attribute of segments for JoinedSegments
//
//
// DEBUG: for labeling, this is a problem, e.g. merging decimal
// numbers, which we *do not* want to merge.
if(joined_nodes.length == -5){
console.log(joined_nodes);
mergedSymbols = true;
compoundSymbols = false; //DEBUG - square root expressions are
// returned as a 'join.'
for(var i = 0; i < joined_nodes.length; i++) {
// Parse information on merged segments.
var str = joined_nodes[i].attributes.getNamedItem("id").value;
var ids = str.substring(0,str.length-1).split(",");
var texSymbol = joined_nodes[i].attributes.getNamedItem("symbol").value;
var symbol =
RecognitionManager.latex_to_symbol[
joined_nodes[i].attributes.getNamedItem("symbol").value];
console.log("JOINED SYMBOL: " + texSymbol);
if(symbol == null)
symbol = joined_nodes[i].attributes.getNamedItem(
"symbol").value;
else if (texSymbol == "sqrt")
compoundSymbols = true;
if(symbol == "")
break;
// Create a new segment/group.
var segs = new Array();
for(var j = 0; j < ids.length; j++)
segs.push(
Editor.get_segment_by_id( parseInt(ids[j]) )[0]);
allMergeActions.add_action_end(new GroupSegments(segs, symbol, false, compoundSymbols));
}
}
}
// Update the MathJax SVG for the canvas if a subexpression has
// not been selected.
if ( Editor.selected_segments.length == 0)
Editor.MathJaxRender(tex_math);
// If a group was selected, then merge the symbols into
// a new segment, labeled by the TeX result.
// Also add BST output.
//
// ASSUMPTION: grouping only used to define individual cells;
// no nesting of cells or grids in a cell.
if (Editor.selected_segments.length > 1
&& Editor.get_selected_set_ids().length > 1
&& EditorState.SegmentsSelected)
allMergeActions.add_action_end(
new GroupSegments(Editor.selected_segments, tex_math, false, true,
bsts[0].textContent));
// If there are any merge actions, add as one composite action.
if (allMergeActions.action_list.length > 0)
Editor.add_action(allMergeActions);
// DEBUG: to obtain valid BST in ground truth output, we need to
// make a recursive call, so that the BST is defined using merged symbols.
//if (mergedSymbols) {
// Dirty, but works for now.
// Editor.align();
//}
// Make sure to visualize the new grouping(s)
RenderManager.render();
},
error: function(jqXHR, textStatus, errorThrown)
{
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
}
}
);
}
Editor.join_segments = function(new_recognition, symbol, set_id){
var set_from_symbols_list = false;
for ( var i = 0; i < new_recognition.symbols.length; i++ ) {
if ( new_recognition.symbols[ i ] == symbol ) {
var sym = symbol;
var cer = new_recognition.certainties[ i ];
new_recognition.symbols.splice( i, 1 );
new_recognition.certainties.splice( i, 1 );
new_recognition.symbols.unshift( sym );
new_recognition.certainties.unshift( cer );
new_recognition.set_id = set_id;
RecognitionManager.result_table.push( new_recognition );
set_from_symbols_list = true;
break;
}
}
// If no recognition was found in the result list, force the new symbol
if(!set_from_symbols_list){
var sym = symbol;
var cer = 1;
new_recognition.symbols.splice( 0, 1 );
new_recognition.certainties.splice( 0, 1 );
new_recognition.symbols.unshift( sym );
new_recognition.certainties.unshift( cer );
new_recognition.set_id = set_id;
RecognitionManager.result_table.push( new_recognition );
}
}
/*
Scale the SVG to fit the canvas by decreasing its font size by 5%
*/
Editor.scale_tex = function(elem){
var root = document.getElementById("Alignment_Tex").getElementsByClassName("MathJax_SVG")[0].firstChild;
var rect = root.firstChild.getBoundingClientRect();
var math_width = rect.left+rect.width;
var math_height = rect.top+rect.height;
if(math_width > (Editor.canvas_width-15) || math_height > (Editor.canvas_height-15)){
elem.style.fontSize = (parseInt(elem.style.fontSize.split("%")[0]) - 10) + "%";
MathJax.Hub.Queue(["Rerender",MathJax.Hub,elem], [$.proxy(Editor.scale_tex(elem), this)]);
}else{
return;
}
}
/* Gets the MathJax rendered SVG from the div, sorts them and canvas segments before
applying alignment to the symbols on the canvas.
*/
// NOTE: 'itemx' is a reference to tuple element x (e.g. item1 for first tuple element).
// NOTE: if lower case letters on baseline, will move down slightly,
// as top left will be where the "ascender" line is placed.
Editor.copy_tex = function(elem, outer_div, s)
{
// Get the top-left and bottom right corner coordinates for symbols on canvas (as s).
// Use only selected symbols if appropriate.
// Obtain bounding box width and height for symbols on canvas; item1/item2 are
// min and max (x,y) coordinates, respectively.
var horMinOffset = 20; //pixels
var verMinOffset = 20;
var topLeft = new Vector2(horMinOffset, verMinOffset);
//console.log("s: " + s.item1.x + "," + s.item1.y);
//console.log("tld: " + topLeft.x + "," + topLeft.y);
var rect_size = Vector2.Subtract(s.item2, s.item1);
var dim_tuple = new Tuple(rect_size.x, rect_size.y); // need to scale to fit canvas elements
// Obtain the rescaled width from MathJax.
var root =
document.getElementById("Alignment_Tex").getElementsByClassName("MathJax_SVG")[0].firstChild;
var rect = root.firstChild.getBoundingClientRect();
// Set the scale - take the canvas size into account from the MathJax
// result.
var target_width = (rect.width / rect.height) * dim_tuple.item2;
var target_height = dim_tuple.item2;
var cwidth = $("#equation_canvas").width();
var toolheight = $("#toolbar").height();
var cheight = $("#equation_canvas").height() -
toolheight;
//console.log("cwidth: " + cwidth + " theight: " + toolheight
// + " cheight: " + cheight);
// Scale to fit on the canvas.
var threshold = 0.85;
if (target_width > cwidth * threshold )
{
target_width = cwidth * threshold;
target_height = target_width * (rect.height / rect.width);
}
if (target_height > cheight * threshold)
{
target_height = cheight * threshold;
target_width = target_height * (rect.width / rect.height);
}
//console.log("x: " + s.item1.x + " y: " + s.item1.y);
//console.log("rect x:" + rect.left + " rect y: " + rect.top);
// Calculate scale and append to g element of MathJax
var scale = new Vector2( (target_width / rect.width), (target_height / rect.height) );
// First group tag groups all MathJax SVGs
var group = root.getElementsByTagName("g")[0];
group.setAttribute("transform", "scale("+scale.x+","+scale.y+") matrix(1 0 0 -1 0 0)");
var root = document.getElementById("Alignment_Tex").getElementsByClassName("MathJax_SVG")[0].firstChild;
group = root.getElementsByTagName("g")[0];
elem.style.width = group.getBoundingClientRect().width + "px";
SVGTopLeft = new Vector2(group.getBoundingClientRect().left, group.getBoundingClientRect().top);
// Displacement from the SVG rectangle to a point at top-left of the canvas.
topLeftTranslation = new Vector2.Subtract(topLeft, SVGTopLeft);
//console.log("SVGTopLeft: " + SVGTopLeft.x + ", " + SVGTopLeft.y);
//console.log("TRANSLATION: " + topLeftTranslation.x + ", " + topLeftTranslation.y);
// Make sure it fits the canvas
//Editor.scale_tex(elem); // Just reduces the font size by 5%
root = document.getElementById("Alignment_Tex").getElementsByClassName("MathJax_SVG")[0].firstChild;
// Retrieve symbols from the div element in previous routine
var use_tag_array = root.getElementsByTagName("use");
var rect_tag_array = root.getElementsByTagName("rect");
use_tag_array = Array.prototype.slice.call(use_tag_array);
rect_tag_array = Array.prototype.slice.call(rect_tag_array);
var elements = use_tag_array.concat(rect_tag_array);
// Sort the svg and canvas elements
Editor.forget_symbol_groups();
var canvas_elements = Editor.sort_canvas_elements();
x_pos = Editor.group_svg([], root.firstChild);
x_pos.sort(Editor.compare_numbers);
//Editor.print_sorted(x_pos, "use");
//Editor.print_sorted(canvas_elements, "canvas");
// Start transformation process and alignment process.
var transform_action = new TransformSegments(Editor.segments);
Editor.apply_alignment(x_pos, canvas_elements, topLeftTranslation);
transform_action.add_new_transforms(Editor.segments);
transform_action.Apply();
Editor.add_action(transform_action);
Editor.restore_symbol_groups();
x_pos = [];
Editor.canvas_div.removeChild(outer_div);
MathJax.Hub.Queue(["setRenderer", MathJax.Hub, "HTML-CSS"]);
}
// Creates a square root horizontal line and appends it to RenderManager's div for the sqrt
Editor.create_segment = function(x_pos){
var sqrt;
var horizontal_bar;
var found = false;
var data = String.fromCharCode(parseInt("221A",16));
var segs = x_pos.slice(0, x_pos.length);
for(var i = 0; i < x_pos.length; i++){
if(x_pos[i].item3.getAttribute("href") == "#MJMAIN-221A"){
sqrt = x_pos[i].item3.getBoundingClientRect();
for(var j = 0; j < segs.length; j++){
var rect = segs[j].item3.getBoundingClientRect();
if(rect.left < sqrt.right && segs[j].item3.tagName == "rect" && rect.top > sqrt.top){
found = true;
horizontal_bar = segs[j];
break;
}
}
}
if(found)
break;
}
if(found){
// copy rect element and put in RenderManager div
for(var k = 0; k < RenderManager.segment_set_divs.length; k++){
if(RenderManager.segment_set_divs[k].getAttribute("data-recognition") == data){
var BBox_rect = RenderManager.segment_set_divs[k].getBoundingClientRect();
var clone = horizontal_bar.item3.cloneNode(true);
clone.removeAttribute("x");
clone.removeAttribute("y");
clone.removeAttribute("stroke");
clone.setAttribute("fill", Editor.segment_fill);
var x = BBox_rect.right;
var y = BBox_rect.top;
clone.setAttribute("transform", "translate(" + x + "," + y + ")");
RenderManager.segment_set_divs[k].getElementsByTagName("g")[0].appendChild(clone);
}
}
}
}
/* Joins SVG segments as one because MathJax splits some of them up sometimes
Mainly groups elements with href #MJMAIN-AF
*/
Editor.group_svg = function(elements, g){
var children = g.childNodes;
var notcontains_g = g.getElementsByTagName("g");
if(notcontains_g.length == 0){ // Base case reached
for(var j = 0; j < children.length; j++){
if(children[j].getAttribute("width") == "0"){
continue; // Don't add it to the elements array. Not needed
}
if(children[j].getAttribute("href") == "#MJMAIN-AF" ){
// Usually grouped together
var parent_rect = g.getBoundingClientRect();
var rect = children[j].getBoundingClientRect();
var tuple_x = new Tuple(Math.round(rect.left), Math.round(rect.top), children[j], true, parent_rect.width, parent_rect.height);
elements.push(tuple_x);
break;
}else{
var rect = children[j].getBoundingClientRect();
var tuple_x = new Tuple(Math.round(rect.left), Math.round(rect.top), children[j], false);
elements.push(tuple_x);
}
}
return elements;
}else{
// More g tags to explore
for(var i = 0; i < children.length; i++){
if( (children[i].tagName == "use" || children[i].tagName == "rect") && children[i].getAttribute("width") != "0"){
if(children[i].getAttribute("href") == "#MJMAIN-AF" ){
var parent_rect = g.getBoundingClientRect();
var rect = children[i].getBoundingClientRect();
var tuple_x = new Tuple(Math.round(rect.left), Math.round(rect.top), children[i], true, parent_rect.width, parent_rect.height);
elements.push(tuple_x);
break;
}else{
var rect = children[i].getBoundingClientRect();
var tuple_x = new Tuple(Math.round(rect.left), Math.round(rect.top), children[i], false);
elements.push(tuple_x);
}
}else
elements = Editor.group_svg(elements, children[i]);
}
}
return elements;
}
/*
A function that returns the world min and max position for joined segments like the
the plus symbol (top left, bottom right).
*/
Editor.get_seg_dimensions = function(set_segments)
{
var mins = set_segments[0].worldMinDrawPosition();
var maxs = set_segments[0].worldMaxDrawPosition();
// Find the extent of the symbol (BB)
for(var j = 1; j < set_segments.length; j++){
var seg_min = set_segments[j].worldMinDrawPosition();
var seg_max = set_segments[j].worldMaxDrawPosition();
if(seg_min.x < mins.x)
mins.x = seg_min.x;
if(seg_min.y < mins.y)
mins.y = seg_min.y;
if(seg_max.x > maxs.x)
maxs.x = seg_max.x;
if(seg_max.y > maxs.y)
maxs.y = seg_max.y;
}
return new Tuple(mins, maxs);
}
// Returns (set_id, BBox) quintuples for a list of 'segments'
// (i.e. individual primitives).
//
// This was taken from ".align" for convenience.
Editor.get_segment_BBoxes = function (seg_list)
{
var segSet = seg_list;
segSet.push(null);
data = new Array();
var set_segments = new Array();
for(var k = 0; k < segSet.length; k++)
{
var seg = segSet[k];
if(set_segments.length == 0)
set_segments.push(seg);
else if(seg == null || seg.set_id != set_segments[0].set_id)
{
var mins = set_segments[0].worldMinPosition();
var maxs = set_segments[0].worldMaxPosition();
for(var j = 1; j < set_segments.length ; j++)
{
var seg_min = set_segments[j].worldMinPosition();
var seg_max = set_segments[j].worldMaxPosition();
if(seg_min.x < mins.x)
mins.x = seg_min.x;
if(seg_min.y < mins.y)
mins.y = seg_min.y;
if(seg_max.x > maxs.x)
maxs.x = seg_max.x;
if(seg_max.y > maxs.y)
maxs.y = seg_max.y;
}
var tuple = new Tuple(set_segments[0].set_id, mins, maxs);
data.push(tuple);
set_segments.length = 0;
set_segments.push(seg);
}
else
set_segments.push(seg);
}
// Remove the null segment that we pushed on the list.
segSet.pop();
return data;
}
// Returns the BBox of an element
Editor.get_BBox = function(seg)
{
var elem_rect;
if(seg.constructor == SymbolSegment)
elem_rect = seg.element.getBoundingClientRect();
else
elem_rect = seg.inner_svg.getBoundingClientRect();
return elem_rect;
}
/* Sorts the render svg from mathjax from left to right and any segment whose x coordinate
collides with another is sorted from top to bottom. Just compares the tops.
*/
Editor.sort_svg_positions = function(array)
{
var x_pos = new Array(); // all x coordinates
var current_x, current_y;
for(var i = 0; i < array.length; i++){
current_x = parseInt(array[i].getBoundingClientRect().left.toFixed(2));
current_y = parseInt(array[i].getBoundingClientRect().top.toFixed(2));
var tuple_x = new Tuple(current_x,current_y, array[i]);
x_pos.push(tuple_x);
}
x_pos.sort(Editor.compare_numbers);
// Remove zero width elements
for(var i = 0; i < x_pos.length; i++){
if(x_pos[i].item3.getAttribute("width") == "0"){
x_pos.splice(i, 1);
}
}
return x_pos;
}
// Prints the sorted SVG and canvas segments
Editor.print_sorted = function(array, type)
{
var s;
if(type == "use")
s = "Use tag: ";
else
s = "Canvas tag: ";
for(var l = 0; l < array.length; l++){
if(type == "use" && array[l].item3.tagName == "use"){
var unicode = array[l].item3.getAttribute("href").split("-")[1];
var text = String.fromCharCode(parseInt(unicode,16));
s += text;
}else if(type == "use" && array[l].item3.tagName == "rect"){
s += "-";
}else{
s += array[l].item3.text;
}
}
console.log(s);
}
// Sorts all svg elements by x and y
// DIFF: if there are selected segments, consider only those.
Editor.sort_canvas_elements = function()
{
var sorted = new Array();
var sorted_set_ids = new Array();
var current_x, current_y;
var Segments = Editor.segments;
//if(Editor.selected_segments.length > 0 &&
// Editor.state == EditorState.SegmentsSelected)
// Segments = Editor.selected_segments;
for(var i = 0; i < Segments.length; i++){
var seg = Segments[i];
var seg_rect = Editor.get_BBox(seg);
current_x = parseInt(seg_rect.left.toFixed(2));
current_y = parseInt(seg_rect.top.toFixed(2))
if(sorted_set_ids.contains(seg.set_id)){
var last_element = sorted.pop();
if(last_element.item1 > current_x) // use lowest x
sorted.push(new Tuple(current_x,current_y,seg));
else
sorted.push(new Tuple(last_element.item1,last_element.item2,seg));
}else{
sorted.push(new Tuple(current_x,current_y,seg));
sorted_set_ids.push(seg.set_id);
}
}
sorted.sort(Editor.compare_numbers);
return sorted;
}
// Sorts all svg elements by x and y for a given set of segments.
Editor.sort_segments = function( segments )
{
var sorted = new Array();
var sorted_set_ids = new Array();
var current_x, current_y;
for(var i = 0; i < segments.length; i++){
var seg = segments[i];
var seg_rect = Editor.get_BBox(seg);
current_x = parseInt(seg_rect.left.toFixed(2));
current_y = parseInt(seg_rect.top.toFixed(2))
if(sorted_set_ids.contains(seg.set_id)){
var last_element = sorted.pop();
if(last_element.item1 > current_x) // use lowest x
sorted.push(new Tuple(current_x,current_y,seg));
else
sorted.push(new Tuple(last_element.item1,last_element.item2,seg));
}else{
sorted.push(new Tuple(current_x,current_y,seg));
sorted_set_ids.push(seg.set_id);
}
}
sorted.sort(Editor.compare_numbers);
return sorted;
}
// Compares passed in tuples by sorting by x and y
Editor.compare_numbers = function(a, b)
{
if (a.item1 == b.item1 && a.item2 == b.item2) return 0;
else if (a.item1 == b.item1) return a.item2 > b.item2 ? 1 : -1;
else return a.item1 > b.item1 ? 1 : -1;
}
// Compare x coordinates.
Editor.compare_asc = function(a, b)
{
if (a == b ) return 0;
else return a > b ? 1 : -1;
}
Editor.orderBBLeftRight = function(t1, t2)
{
console.log("t1");
console.log(t1);
var BB1Left = t1.item2.x;
var BB2Left = t2.item2.x;
return Editor.compare_asc(BB1Left, BB2Left);
}
Editor.orderRowsTopDown = function(t1, t2)
{
var BB1Top = t1.x.item2.y;
var BB2Top = t2.x.item2.y;
return Editor.compare_asc(BB1Top, BB2Top);
}
/* Maps elements on canvas to corresponding MathJax rendered SVG symbol ('array')
* and then moves symbols around to match the MathJax-generated SVG output.
Note: This methods relies on the fact that Canvas segments have their
recognition result as an instance. This is set in the RenderManager after
recognition is gotten. "PenStroke_Object".Text - Recognition result for the