-
Notifications
You must be signed in to change notification settings - Fork 9.7k
/
Copy pathtopitch.cpp
1838 lines (1733 loc) · 67.2 KB
/
topitch.cpp
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
/**********************************************************************
* File: topitch.cpp (Formerly to_pitch.c)
* Description: Code to determine fixed pitchness and the pitch if fixed.
* Author: Ray Smith
* Created: Tue Aug 24 16:57:29 BST 1993
*
* (C) Copyright 1993, Hewlett-Packard Ltd.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/
#ifdef __UNIX__
#include <assert.h>
#endif
#include "stderr.h"
#include "blobbox.h"
#include "statistc.h"
#include "drawtord.h"
#include "makerow.h"
#include "pitsync1.h"
#include "pithsync.h"
#include "tovars.h"
#include "wordseg.h"
#include "topitch.h"
#include "helpers.h"
// Include automatically generated configuration file if running autoconf.
#ifdef HAVE_CONFIG_H
#include "config_auto.h"
#endif
#define EXTERN
EXTERN BOOL_VAR (textord_all_prop, FALSE, "All doc is proportial text");
EXTERN BOOL_VAR (textord_debug_pitch_test, FALSE,
"Debug on fixed pitch test");
EXTERN BOOL_VAR (textord_disable_pitch_test, FALSE,
"Turn off dp fixed pitch algorithm");
EXTERN BOOL_VAR (textord_fast_pitch_test, FALSE,
"Do even faster pitch algorithm");
EXTERN BOOL_VAR (textord_debug_pitch_metric, FALSE,
"Write full metric stuff");
EXTERN BOOL_VAR (textord_show_row_cuts, FALSE, "Draw row-level cuts");
EXTERN BOOL_VAR (textord_show_page_cuts, FALSE, "Draw page-level cuts");
EXTERN BOOL_VAR (textord_pitch_cheat, FALSE,
"Use correct answer for fixed/prop");
EXTERN BOOL_VAR (textord_blockndoc_fixed, FALSE,
"Attempt whole doc/block fixed pitch");
EXTERN double_VAR (textord_projection_scale, 0.200, "Ding rate for mid-cuts");
EXTERN double_VAR (textord_balance_factor, 1.0,
"Ding rate for unbalanced char cells");
#define FIXED_WIDTH_MULTIPLE 5
#define BLOCK_STATS_CLUSTERS 10
#define MAX_ALLOWED_PITCH 100 //max pixel pitch.
/**********************************************************************
* compute_fixed_pitch
*
* Decide whether each row is fixed pitch individually.
* Correlate definite and uncertain results to obtain an individual
* result for each row in the TO_ROW class.
**********************************************************************/
void compute_fixed_pitch(ICOORD page_tr, // top right
TO_BLOCK_LIST *port_blocks, // input list
float gradient, // page skew
FCOORD rotation, // for drawing
BOOL8 testing_on) { // correct orientation
TO_BLOCK_IT block_it; //iterator
TO_BLOCK *block; //current block;
TO_ROW_IT row_it; //row iterator
TO_ROW *row; //current row
int block_index; //block number
int row_index; //row number
#ifndef GRAPHICS_DISABLED
if (textord_show_initial_words && testing_on) {
if (to_win == NULL)
create_to_win(page_tr);
}
#endif
block_it.set_to_list (port_blocks);
block_index = 1;
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
compute_block_pitch(block, rotation, block_index, testing_on);
block_index++;
}
if (!try_doc_fixed (page_tr, port_blocks, gradient)) {
block_index = 1;
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
if (!try_block_fixed (block, block_index))
try_rows_fixed(block, block_index, testing_on);
block_index++;
}
}
block_index = 1;
for (block_it.mark_cycle_pt(); !block_it.cycled_list();
block_it.forward()) {
block = block_it.data ();
POLY_BLOCK* pb = block->block->poly_block();
if (pb != NULL && !pb->IsText()) continue; // Non-text doesn't exist!
row_it.set_to_list (block->get_rows ());
row_index = 1;
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
fix_row_pitch(row, block, port_blocks, row_index, block_index);
row_index++;
}
block_index++;
}
#ifndef GRAPHICS_DISABLED
if (textord_show_initial_words && testing_on) {
ScrollView::Update();
}
#endif
}
/**********************************************************************
* fix_row_pitch
*
* Get a pitch_decision for this row by voting among similar rows in the
* block, then similar rows over all the page, or any other rows at all.
**********************************************************************/
void fix_row_pitch(TO_ROW *bad_row, // row to fix
TO_BLOCK *bad_block, // block of bad_row
TO_BLOCK_LIST *blocks, // blocks to scan
inT32 row_target, // number of row
inT32 block_target) { // number of block
inT16 mid_cuts;
int block_votes; //votes in block
int like_votes; //votes over page
int other_votes; //votes of unlike blocks
int block_index; //number of block
int row_index; //number of row
int maxwidth; //max pitch
TO_BLOCK_IT block_it = blocks; //block iterator
TO_ROW_IT row_it;
TO_BLOCK *block; //current block
TO_ROW *row; //current row
float sp_sd; //space deviation
STATS block_stats; //pitches in block
STATS like_stats; //pitches in page
block_votes = like_votes = other_votes = 0;
maxwidth = (inT32) ceil (bad_row->xheight * textord_words_maxspace);
if (bad_row->pitch_decision != PITCH_DEF_FIXED
&& bad_row->pitch_decision != PITCH_DEF_PROP) {
block_stats.set_range (0, maxwidth);
like_stats.set_range (0, maxwidth);
block_index = 1;
for (block_it.mark_cycle_pt(); !block_it.cycled_list();
block_it.forward()) {
block = block_it.data();
POLY_BLOCK* pb = block->block->poly_block();
if (pb != NULL && !pb->IsText()) continue; // Non text doesn't exist!
row_index = 1;
row_it.set_to_list (block->get_rows ());
for (row_it.mark_cycle_pt (); !row_it.cycled_list ();
row_it.forward ()) {
row = row_it.data ();
if ((bad_row->all_caps
&& row->xheight + row->ascrise
<
(bad_row->xheight + bad_row->ascrise) * (1 +
textord_pitch_rowsimilarity)
&& row->xheight + row->ascrise >
(bad_row->xheight + bad_row->ascrise) * (1 -
textord_pitch_rowsimilarity))
|| (!bad_row->all_caps
&& row->xheight <
bad_row->xheight * (1 + textord_pitch_rowsimilarity)
&& row->xheight >
bad_row->xheight * (1 - textord_pitch_rowsimilarity))) {
if (block_index == block_target) {
if (row->pitch_decision == PITCH_DEF_FIXED) {
block_votes += textord_words_veto_power;
block_stats.add ((inT32) row->fixed_pitch,
textord_words_veto_power);
}
else if (row->pitch_decision == PITCH_MAYBE_FIXED
|| row->pitch_decision == PITCH_CORR_FIXED) {
block_votes++;
block_stats.add ((inT32) row->fixed_pitch, 1);
}
else if (row->pitch_decision == PITCH_DEF_PROP)
block_votes -= textord_words_veto_power;
else if (row->pitch_decision == PITCH_MAYBE_PROP
|| row->pitch_decision == PITCH_CORR_PROP)
block_votes--;
}
else {
if (row->pitch_decision == PITCH_DEF_FIXED) {
like_votes += textord_words_veto_power;
like_stats.add ((inT32) row->fixed_pitch,
textord_words_veto_power);
}
else if (row->pitch_decision == PITCH_MAYBE_FIXED
|| row->pitch_decision == PITCH_CORR_FIXED) {
like_votes++;
like_stats.add ((inT32) row->fixed_pitch, 1);
}
else if (row->pitch_decision == PITCH_DEF_PROP)
like_votes -= textord_words_veto_power;
else if (row->pitch_decision == PITCH_MAYBE_PROP
|| row->pitch_decision == PITCH_CORR_PROP)
like_votes--;
}
}
else {
if (row->pitch_decision == PITCH_DEF_FIXED)
other_votes += textord_words_veto_power;
else if (row->pitch_decision == PITCH_MAYBE_FIXED
|| row->pitch_decision == PITCH_CORR_FIXED)
other_votes++;
else if (row->pitch_decision == PITCH_DEF_PROP)
other_votes -= textord_words_veto_power;
else if (row->pitch_decision == PITCH_MAYBE_PROP
|| row->pitch_decision == PITCH_CORR_PROP)
other_votes--;
}
row_index++;
}
block_index++;
}
if (block_votes > textord_words_veto_power) {
bad_row->fixed_pitch = block_stats.ile (0.5);
bad_row->pitch_decision = PITCH_CORR_FIXED;
}
else if (block_votes <= textord_words_veto_power && like_votes > 0) {
bad_row->fixed_pitch = like_stats.ile (0.5);
bad_row->pitch_decision = PITCH_CORR_FIXED;
}
else {
bad_row->pitch_decision = PITCH_CORR_PROP;
if (block_votes == 0 && like_votes == 0 && other_votes > 0
&& (textord_debug_pitch_test || textord_debug_pitch_metric))
tprintf
("Warning:row %d of block %d set prop with no like rows against trend\n",
row_target, block_target);
}
}
if (textord_debug_pitch_metric) {
tprintf(":b_votes=%d:l_votes=%d:o_votes=%d",
block_votes, like_votes, other_votes);
tprintf("x=%g:asc=%g\n", bad_row->xheight, bad_row->ascrise);
}
if (bad_row->pitch_decision == PITCH_CORR_FIXED) {
if (bad_row->fixed_pitch < textord_min_xheight) {
if (block_votes > 0)
bad_row->fixed_pitch = block_stats.ile (0.5);
else if (block_votes == 0 && like_votes > 0)
bad_row->fixed_pitch = like_stats.ile (0.5);
else {
tprintf
("Warning:guessing pitch as xheight on row %d, block %d\n",
row_target, block_target);
bad_row->fixed_pitch = bad_row->xheight;
}
}
if (bad_row->fixed_pitch < textord_min_xheight)
bad_row->fixed_pitch = (float) textord_min_xheight;
bad_row->kern_size = bad_row->fixed_pitch / 4;
bad_row->min_space = (inT32) (bad_row->fixed_pitch * 0.6);
bad_row->max_nonspace = (inT32) (bad_row->fixed_pitch * 0.4);
bad_row->space_threshold =
(bad_row->min_space + bad_row->max_nonspace) / 2;
bad_row->space_size = bad_row->fixed_pitch;
if (bad_row->char_cells.empty() && !bad_row->blob_list()->empty()) {
tune_row_pitch (bad_row, &bad_row->projection,
bad_row->projection_left, bad_row->projection_right,
(bad_row->fixed_pitch +
bad_row->max_nonspace * 3) / 4, bad_row->fixed_pitch,
sp_sd, mid_cuts, &bad_row->char_cells, FALSE);
}
}
else if (bad_row->pitch_decision == PITCH_CORR_PROP
|| bad_row->pitch_decision == PITCH_DEF_PROP) {
bad_row->fixed_pitch = 0.0f;
bad_row->char_cells.clear ();
}
}
/**********************************************************************
* compute_block_pitch
*
* Decide whether each block is fixed pitch individually.
**********************************************************************/
void compute_block_pitch(TO_BLOCK *block, // input list
FCOORD rotation, // for drawing
inT32 block_index, // block number
BOOL8 testing_on) { // correct orientation
TBOX block_box; //bounding box
block_box = block->block->bounding_box ();
if (testing_on && textord_debug_pitch_test) {
tprintf ("Block %d at (%d,%d)->(%d,%d)\n",
block_index,
block_box.left (), block_box.bottom (),
block_box.right (), block_box.top ());
}
block->min_space = (inT32) floor (block->xheight
* textord_words_default_minspace);
block->max_nonspace = (inT32) ceil (block->xheight
* textord_words_default_nonspace);
block->fixed_pitch = 0.0f;
block->space_size = (float) block->min_space;
block->kern_size = (float) block->max_nonspace;
block->pr_nonsp = block->xheight * words_default_prop_nonspace;
block->pr_space = block->pr_nonsp * textord_spacesize_ratioprop;
if (!block->get_rows ()->empty ()) {
ASSERT_HOST (block->xheight > 0);
find_repeated_chars(block, textord_show_initial_words && testing_on);
#ifndef GRAPHICS_DISABLED
if (textord_show_initial_words && testing_on)
//overlap_picture_ops(TRUE);
ScrollView::Update();
#endif
compute_rows_pitch(block,
block_index,
textord_debug_pitch_test &&testing_on);
}
}
/**********************************************************************
* compute_rows_pitch
*
* Decide whether each row is fixed pitch individually.
**********************************************************************/
BOOL8 compute_rows_pitch( //find line stats
TO_BLOCK *block, //block to do
inT32 block_index, //block number
BOOL8 testing_on //correct orientation
) {
inT32 maxwidth; //of spaces
TO_ROW *row; //current row
inT32 row_index; //row number.
float lower, upper; //cluster thresholds
TO_ROW_IT row_it = block->get_rows ();
row_index = 1;
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
ASSERT_HOST (row->xheight > 0);
row->compute_vertical_projection ();
maxwidth = (inT32) ceil (row->xheight * textord_words_maxspace);
if (row_pitch_stats (row, maxwidth, testing_on)
&& find_row_pitch (row, maxwidth,
textord_dotmatrix_gap + 1, block, block_index,
row_index, testing_on)) {
if (row->fixed_pitch == 0) {
lower = row->pr_nonsp;
upper = row->pr_space;
row->space_size = upper;
row->kern_size = lower;
}
}
else {
row->fixed_pitch = 0.0f; //insufficient data
row->pitch_decision = PITCH_DUNNO;
}
row_index++;
}
return FALSE;
}
/**********************************************************************
* try_doc_fixed
*
* Attempt to call the entire document fixed pitch.
**********************************************************************/
BOOL8 try_doc_fixed( //determine pitch
ICOORD page_tr, //top right
TO_BLOCK_LIST *port_blocks, //input list
float gradient //page skew
) {
inT16 master_x; //uniform shifts
inT16 pitch; //median pitch.
int x; //profile coord
int prop_blocks; //correct counts
int fixed_blocks;
int total_row_count; //total in page
//iterator
TO_BLOCK_IT block_it = port_blocks;
TO_BLOCK *block; //current block;
TO_ROW_IT row_it; //row iterator
TO_ROW *row; //current row
inT16 projection_left; //edges
inT16 projection_right;
inT16 row_left; //edges of row
inT16 row_right;
ICOORDELT_LIST *master_cells; //cells for page
float master_y; //uniform shifts
float shift_factor; //page skew correction
float row_shift; //shift for row
float final_pitch; //output pitch
float row_y; //baseline
STATS projection; //entire page
STATS pitches (0, MAX_ALLOWED_PITCH);
//for median
float sp_sd; //space sd
inT16 mid_cuts; //no of cheap cuts
float pitch_sd; //sync rating
if (block_it.empty ()
// || block_it.data()==block_it.data_relative(1)
|| !textord_blockndoc_fixed)
return FALSE;
shift_factor = gradient / (gradient * gradient + 1);
row_it.set_to_list (block_it.data ()->get_rows ());
master_x = row_it.data ()->projection_left;
master_y = row_it.data ()->baseline.y (master_x);
projection_left = MAX_INT16;
projection_right = -MAX_INT16;
prop_blocks = 0;
fixed_blocks = 0;
total_row_count = 0;
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
row_it.set_to_list (block->get_rows ());
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
total_row_count++;
if (row->fixed_pitch > 0)
pitches.add ((inT32) (row->fixed_pitch), 1);
//find median
row_y = row->baseline.y (master_x);
row_left =
(inT16) (row->projection_left -
shift_factor * (master_y - row_y));
row_right =
(inT16) (row->projection_right -
shift_factor * (master_y - row_y));
if (row_left < projection_left)
projection_left = row_left;
if (row_right > projection_right)
projection_right = row_right;
}
}
if (pitches.get_total () == 0)
return FALSE;
projection.set_range (projection_left, projection_right);
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
row_it.set_to_list (block->get_rows ());
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
row_y = row->baseline.y (master_x);
row_left =
(inT16) (row->projection_left -
shift_factor * (master_y - row_y));
for (x = row->projection_left; x < row->projection_right;
x++, row_left++) {
projection.add (row_left, row->projection.pile_count (x));
}
}
}
row_it.set_to_list (block_it.data ()->get_rows ());
row = row_it.data ();
#ifndef GRAPHICS_DISABLED
if (textord_show_page_cuts && to_win != NULL)
projection.plot (to_win, projection_left,
row->intercept (), 1.0f, -1.0f, ScrollView::CORAL);
#endif
final_pitch = pitches.ile (0.5);
pitch = (inT16) final_pitch;
pitch_sd =
tune_row_pitch (row, &projection, projection_left, projection_right,
pitch * 0.75, final_pitch, sp_sd, mid_cuts,
&row->char_cells, FALSE);
if (textord_debug_pitch_metric)
tprintf
("try_doc:props=%d:fixed=%d:pitch=%d:final_pitch=%g:pitch_sd=%g:sp_sd=%g:sd/trc=%g:sd/p=%g:sd/trc/p=%g\n",
prop_blocks, fixed_blocks, pitch, final_pitch, pitch_sd, sp_sd,
pitch_sd / total_row_count, pitch_sd / pitch,
pitch_sd / total_row_count / pitch);
#ifndef GRAPHICS_DISABLED
if (textord_show_page_cuts && to_win != NULL) {
master_cells = &row->char_cells;
for (block_it.mark_cycle_pt (); !block_it.cycled_list ();
block_it.forward ()) {
block = block_it.data ();
row_it.set_to_list (block->get_rows ());
for (row_it.mark_cycle_pt (); !row_it.cycled_list ();
row_it.forward ()) {
row = row_it.data ();
row_y = row->baseline.y (master_x);
row_shift = shift_factor * (master_y - row_y);
plot_row_cells(to_win, ScrollView::GOLDENROD, row, row_shift, master_cells);
}
}
}
#endif
row->char_cells.clear ();
return FALSE;
}
/**********************************************************************
* try_block_fixed
*
* Try to call the entire block fixed.
**********************************************************************/
BOOL8 try_block_fixed( //find line stats
TO_BLOCK *block, //block to do
inT32 block_index //block number
) {
return FALSE;
}
/**********************************************************************
* try_rows_fixed
*
* Decide whether each row is fixed pitch individually.
**********************************************************************/
BOOL8 try_rows_fixed( //find line stats
TO_BLOCK *block, //block to do
inT32 block_index, //block number
BOOL8 testing_on //correct orientation
) {
TO_ROW *row; //current row
inT32 row_index; //row number.
inT32 def_fixed = 0; //counters
inT32 def_prop = 0;
inT32 maybe_fixed = 0;
inT32 maybe_prop = 0;
inT32 dunno = 0;
inT32 corr_fixed = 0;
inT32 corr_prop = 0;
float lower, upper; //cluster thresholds
TO_ROW_IT row_it = block->get_rows ();
row_index = 1;
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
ASSERT_HOST (row->xheight > 0);
if (row->fixed_pitch > 0 &&
fixed_pitch_row(row, block->block, block_index)) {
if (row->fixed_pitch == 0) {
lower = row->pr_nonsp;
upper = row->pr_space;
row->space_size = upper;
row->kern_size = lower;
}
}
row_index++;
}
count_block_votes(block,
def_fixed,
def_prop,
maybe_fixed,
maybe_prop,
corr_fixed,
corr_prop,
dunno);
if (testing_on
&& (textord_debug_pitch_test
|| textord_blocksall_prop || textord_blocksall_fixed)) {
tprintf ("Initially:");
print_block_counts(block, block_index);
}
if (def_fixed > def_prop * textord_words_veto_power)
block->pitch_decision = PITCH_DEF_FIXED;
else if (def_prop > def_fixed * textord_words_veto_power)
block->pitch_decision = PITCH_DEF_PROP;
else if (def_fixed > 0 || def_prop > 0)
block->pitch_decision = PITCH_DUNNO;
else if (maybe_fixed > maybe_prop * textord_words_veto_power)
block->pitch_decision = PITCH_MAYBE_FIXED;
else if (maybe_prop > maybe_fixed * textord_words_veto_power)
block->pitch_decision = PITCH_MAYBE_PROP;
else
block->pitch_decision = PITCH_DUNNO;
return FALSE;
}
/**********************************************************************
* print_block_counts
*
* Count up how many rows have what decision and print the results.
**********************************************************************/
void print_block_counts( //find line stats
TO_BLOCK *block, //block to do
inT32 block_index //block number
) {
inT32 def_fixed = 0; //counters
inT32 def_prop = 0;
inT32 maybe_fixed = 0;
inT32 maybe_prop = 0;
inT32 dunno = 0;
inT32 corr_fixed = 0;
inT32 corr_prop = 0;
count_block_votes(block,
def_fixed,
def_prop,
maybe_fixed,
maybe_prop,
corr_fixed,
corr_prop,
dunno);
tprintf ("Block %d has (%d,%d,%d)",
block_index, def_fixed, maybe_fixed, corr_fixed);
if (textord_blocksall_prop && (def_fixed || maybe_fixed || corr_fixed))
tprintf (" (Wrongly)");
tprintf (" fixed, (%d,%d,%d)", def_prop, maybe_prop, corr_prop);
if (textord_blocksall_fixed && (def_prop || maybe_prop || corr_prop))
tprintf (" (Wrongly)");
tprintf (" prop, %d dunno\n", dunno);
}
/**********************************************************************
* count_block_votes
*
* Count the number of rows in the block with each kind of pitch_decision.
**********************************************************************/
void count_block_votes( //find line stats
TO_BLOCK *block, //block to do
inT32 &def_fixed, //add to counts
inT32 &def_prop,
inT32 &maybe_fixed,
inT32 &maybe_prop,
inT32 &corr_fixed,
inT32 &corr_prop,
inT32 &dunno) {
TO_ROW *row; //current row
TO_ROW_IT row_it = block->get_rows ();
for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
row = row_it.data ();
switch (row->pitch_decision) {
case PITCH_DUNNO:
dunno++;
break;
case PITCH_DEF_PROP:
def_prop++;
break;
case PITCH_MAYBE_PROP:
maybe_prop++;
break;
case PITCH_DEF_FIXED:
def_fixed++;
break;
case PITCH_MAYBE_FIXED:
maybe_fixed++;
break;
case PITCH_CORR_PROP:
corr_prop++;
break;
case PITCH_CORR_FIXED:
corr_fixed++;
break;
}
}
}
/**********************************************************************
* row_pitch_stats
*
* Decide whether each row is fixed pitch individually.
**********************************************************************/
BOOL8 row_pitch_stats( //find line stats
TO_ROW *row, //current row
inT32 maxwidth, //of spaces
BOOL8 testing_on //correct orientation
) {
BLOBNBOX *blob; //current blob
int gap_index; //current gap
inT32 prev_x; //end of prev blob
inT32 cluster_count; //no of clusters
inT32 prev_count; //of clusters
inT32 smooth_factor; //for smoothing stats
TBOX blob_box; //bounding box
float lower, upper; //cluster thresholds
//gap sizes
float gaps[BLOCK_STATS_CLUSTERS];
//blobs
BLOBNBOX_IT blob_it = row->blob_list ();
STATS gap_stats (0, maxwidth);
STATS cluster_stats[BLOCK_STATS_CLUSTERS + 1];
//clusters
smooth_factor =
(inT32) (row->xheight * textord_wordstats_smooth_factor + 1.5);
if (!blob_it.empty ()) {
prev_x = blob_it.data ()->bounding_box ().right ();
blob_it.forward ();
while (!blob_it.at_first ()) {
blob = blob_it.data ();
if (!blob->joined_to_prev ()) {
blob_box = blob->bounding_box ();
if (blob_box.left () - prev_x < maxwidth)
gap_stats.add (blob_box.left () - prev_x, 1);
prev_x = blob_box.right ();
}
blob_it.forward ();
}
}
if (gap_stats.get_total () == 0) {
return FALSE;
}
cluster_count = 0;
lower = row->xheight * words_initial_lower;
upper = row->xheight * words_initial_upper;
gap_stats.smooth (smooth_factor);
do {
prev_count = cluster_count;
cluster_count = gap_stats.cluster (lower, upper,
textord_spacesize_ratioprop,
BLOCK_STATS_CLUSTERS, cluster_stats);
}
while (cluster_count > prev_count && cluster_count < BLOCK_STATS_CLUSTERS);
if (cluster_count < 1) {
return FALSE;
}
for (gap_index = 0; gap_index < cluster_count; gap_index++)
gaps[gap_index] = cluster_stats[gap_index + 1].ile (0.5);
//get medians
if (testing_on) {
tprintf ("cluster_count=%d:", cluster_count);
for (gap_index = 0; gap_index < cluster_count; gap_index++)
tprintf (" %g(%d)", gaps[gap_index],
cluster_stats[gap_index + 1].get_total ());
tprintf ("\n");
}
qsort (gaps, cluster_count, sizeof (float), sort_floats);
//Try to find proportional non-space and space for row.
lower = row->xheight * words_default_prop_nonspace;
upper = row->xheight * textord_words_min_minspace;
for (gap_index = 0; gap_index < cluster_count
&& gaps[gap_index] < lower; gap_index++);
if (gap_index == 0) {
if (testing_on)
tprintf ("No clusters below nonspace threshold!!\n");
if (cluster_count > 1) {
row->pr_nonsp = gaps[0];
row->pr_space = gaps[1];
}
else {
row->pr_nonsp = lower;
row->pr_space = gaps[0];
}
}
else {
row->pr_nonsp = gaps[gap_index - 1];
while (gap_index < cluster_count && gaps[gap_index] < upper)
gap_index++;
if (gap_index == cluster_count) {
if (testing_on)
tprintf ("No clusters above nonspace threshold!!\n");
row->pr_space = lower * textord_spacesize_ratioprop;
}
else
row->pr_space = gaps[gap_index];
}
//Now try to find the fixed pitch space and non-space.
upper = row->xheight * words_default_fixed_space;
for (gap_index = 0; gap_index < cluster_count
&& gaps[gap_index] < upper; gap_index++);
if (gap_index == 0) {
if (testing_on)
tprintf ("No clusters below space threshold!!\n");
row->fp_nonsp = upper;
row->fp_space = gaps[0];
}
else {
row->fp_nonsp = gaps[gap_index - 1];
if (gap_index == cluster_count) {
if (testing_on)
tprintf ("No clusters above space threshold!!\n");
row->fp_space = row->xheight;
}
else
row->fp_space = gaps[gap_index];
}
if (testing_on) {
tprintf
("Initial estimates:pr_nonsp=%g, pr_space=%g, fp_nonsp=%g, fp_space=%g\n",
row->pr_nonsp, row->pr_space, row->fp_nonsp, row->fp_space);
}
return TRUE; //computed some stats
}
/**********************************************************************
* find_row_pitch
*
* Check to see if this row could be fixed pitch using the given spacings.
* Blobs with gaps smaller than the lower threshold are assumed to be one.
* The larger threshold is the word gap threshold.
**********************************************************************/
BOOL8 find_row_pitch( //find lines
TO_ROW *row, //row to do
inT32 maxwidth, //max permitted space
inT32 dm_gap, //ignorable gaps
TO_BLOCK *block, //block of row
inT32 block_index, //block_number
inT32 row_index, //number of row
BOOL8 testing_on //correct orientation
) {
BOOL8 used_dm_model; //looks lik dot matrix
float min_space; //estimate threshold
float non_space; //gap size
float gap_iqr; //interquartile range
float pitch_iqr;
float dm_gap_iqr; //interquartile range
float dm_pitch_iqr;
float dm_pitch; //pitch with dm on
float pitch; //revised estimate
float initial_pitch; //guess at pitch
STATS gap_stats (0, maxwidth);
//centre-centre
STATS pitch_stats (0, maxwidth);
row->fixed_pitch = 0.0f;
initial_pitch = row->fp_space;
if (initial_pitch > row->xheight * (1 + words_default_fixed_limit))
initial_pitch = row->xheight;//keep pitch decent
non_space = row->fp_nonsp;
if (non_space > initial_pitch)
non_space = initial_pitch;
min_space = (initial_pitch + non_space) / 2;
if (!count_pitch_stats (row, &gap_stats, &pitch_stats,
initial_pitch, min_space, TRUE, FALSE, dm_gap)) {
dm_gap_iqr = 0.0001;
dm_pitch_iqr = maxwidth * 2.0f;
dm_pitch = initial_pitch;
}
else {
dm_gap_iqr = gap_stats.ile (0.75) - gap_stats.ile (0.25);
dm_pitch_iqr = pitch_stats.ile (0.75) - pitch_stats.ile (0.25);
dm_pitch = pitch_stats.ile (0.5);
}
gap_stats.clear ();
pitch_stats.clear ();
if (!count_pitch_stats (row, &gap_stats, &pitch_stats,
initial_pitch, min_space, TRUE, FALSE, 0)) {
gap_iqr = 0.0001;
pitch_iqr = maxwidth * 3.0f;
}
else {
gap_iqr = gap_stats.ile (0.75) - gap_stats.ile (0.25);
pitch_iqr = pitch_stats.ile (0.75) - pitch_stats.ile (0.25);
if (testing_on)
tprintf
("First fp iteration:initial_pitch=%g, gap_iqr=%g, pitch_iqr=%g, pitch=%g\n",
initial_pitch, gap_iqr, pitch_iqr, pitch_stats.ile (0.5));
initial_pitch = pitch_stats.ile (0.5);
if (min_space > initial_pitch
&& count_pitch_stats (row, &gap_stats, &pitch_stats,
initial_pitch, initial_pitch, TRUE, FALSE, 0)) {
min_space = initial_pitch;
gap_iqr = gap_stats.ile (0.75) - gap_stats.ile (0.25);
pitch_iqr = pitch_stats.ile (0.75) - pitch_stats.ile (0.25);
if (testing_on)
tprintf
("Revised fp iteration:initial_pitch=%g, gap_iqr=%g, pitch_iqr=%g, pitch=%g\n",
initial_pitch, gap_iqr, pitch_iqr, pitch_stats.ile (0.5));
initial_pitch = pitch_stats.ile (0.5);
}
}
if (textord_debug_pitch_metric)
tprintf("Blk=%d:Row=%d:%c:p_iqr=%g:g_iqr=%g:dm_p_iqr=%g:dm_g_iqr=%g:%c:",
block_index, row_index, 'X',
pitch_iqr, gap_iqr, dm_pitch_iqr, dm_gap_iqr,
pitch_iqr > maxwidth && dm_pitch_iqr > maxwidth ? 'D' :
(pitch_iqr * dm_gap_iqr <= dm_pitch_iqr * gap_iqr ? 'S' : 'M'));
if (pitch_iqr > maxwidth && dm_pitch_iqr > maxwidth) {
row->pitch_decision = PITCH_DUNNO;
if (textord_debug_pitch_metric)
tprintf ("\n");
return FALSE; //insufficient data
}
if (pitch_iqr * dm_gap_iqr <= dm_pitch_iqr * gap_iqr) {
if (testing_on)
tprintf
("Choosing non dm version:pitch_iqr=%g, gap_iqr=%g, dm_pitch_iqr=%g, dm_gap_iqr=%g\n",
pitch_iqr, gap_iqr, dm_pitch_iqr, dm_gap_iqr);
gap_iqr = gap_stats.ile (0.75) - gap_stats.ile (0.25);
pitch_iqr = pitch_stats.ile (0.75) - pitch_stats.ile (0.25);
pitch = pitch_stats.ile (0.5);
used_dm_model = FALSE;
}
else {
if (testing_on)
tprintf
("Choosing dm version:pitch_iqr=%g, gap_iqr=%g, dm_pitch_iqr=%g, dm_gap_iqr=%g\n",
pitch_iqr, gap_iqr, dm_pitch_iqr, dm_gap_iqr);
gap_iqr = dm_gap_iqr;
pitch_iqr = dm_pitch_iqr;
pitch = dm_pitch;
used_dm_model = TRUE;
}
if (textord_debug_pitch_metric) {
tprintf ("rev_p_iqr=%g:rev_g_iqr=%g:pitch=%g:",
pitch_iqr, gap_iqr, pitch);
tprintf ("p_iqr/g=%g:p_iqr/x=%g:iqr_res=%c:",
pitch_iqr / gap_iqr, pitch_iqr / block->xheight,
pitch_iqr < gap_iqr * textord_fpiqr_ratio
&& pitch_iqr < block->xheight * textord_max_pitch_iqr
&& pitch < block->xheight * textord_words_default_maxspace
? 'F' : 'P');
}
if (pitch_iqr < gap_iqr * textord_fpiqr_ratio
&& pitch_iqr < block->xheight * textord_max_pitch_iqr
&& pitch < block->xheight * textord_words_default_maxspace)
row->pitch_decision = PITCH_MAYBE_FIXED;
else
row->pitch_decision = PITCH_MAYBE_PROP;
row->fixed_pitch = pitch;
row->kern_size = gap_stats.ile (0.5);
row->min_space = (inT32) (row->fixed_pitch + non_space) / 2;
if (row->min_space > row->fixed_pitch)
row->min_space = (inT32) row->fixed_pitch;
row->max_nonspace = row->min_space;
row->space_size = row->fixed_pitch;
row->space_threshold = (row->max_nonspace + row->min_space) / 2;
row->used_dm_model = used_dm_model;
return TRUE;
}
/**********************************************************************
* fixed_pitch_row
*
* Check to see if this row could be fixed pitch using the given spacings.
* Blobs with gaps smaller than the lower threshold are assumed to be one.
* The larger threshold is the word gap threshold.
**********************************************************************/
BOOL8 fixed_pitch_row(TO_ROW *row, // row to do
BLOCK* block,
inT32 block_index // block_number
) {
const char *res_string; // pitch result
inT16 mid_cuts; // no of cheap cuts
float non_space; // gap size
float pitch_sd; // error on pitch
float sp_sd = 0.0f; // space sd
non_space = row->fp_nonsp;
if (non_space > row->fixed_pitch)
non_space = row->fixed_pitch;
POLY_BLOCK* pb = block != NULL ? block->poly_block() : NULL;
if (textord_all_prop || (pb != NULL && !pb->IsText())) {
// Set the decision to definitely proportional.
pitch_sd = textord_words_def_prop * row->fixed_pitch;
row->pitch_decision = PITCH_DEF_PROP;
} else {
pitch_sd = tune_row_pitch (row, &row->projection, row->projection_left,
row->projection_right,
(row->fixed_pitch + non_space * 3) / 4,
row->fixed_pitch, sp_sd, mid_cuts,
&row->char_cells,
block_index == textord_debug_block);
if (pitch_sd < textord_words_pitchsd_threshold * row->fixed_pitch
&& ((pitsync_linear_version & 3) < 3