-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlfmt.cpp
7653 lines (6554 loc) · 230 KB
/
htmlfmt.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
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/html/htmlfmt.cpp,v 1.4 1999/07/11 00:46:40 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1997 by Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
htmlfmt.cpp - HTML formatter
Function
Notes
Modified
09/07/97 MJRoberts - Creation
*/
#include <stdlib.h>
#include <memory.h>
#ifndef TADSHTML_H
#include "tadshtml.h"
#endif
#ifndef HTMLFMT_H
#include "htmlfmt.h"
#endif
#ifndef HTMLPRS_H
#include "htmlprs.h"
#endif
#ifndef HTMLTAGS_H
#include "htmltags.h"
#endif
#ifndef HTMLSYS_H
#include "htmlsys.h"
#endif
#ifndef HTMLDISP_H
#include "htmldisp.h"
#endif
#ifndef HTMLTXAR_H
#include "htmltxar.h"
#endif
#ifndef HTMLRC_H
#include "htmlrc.h"
#endif
#ifndef HTMLHASH_H
#include "htmlhash.h"
#endif
#ifndef HTML_OS_H
#include "html_os.h"
#endif
#ifndef HTMLRF_H
#include "htmlrf.h"
#endif
#ifndef HTMLSND_H
#include "htmlsnd.h"
#endif
/* ------------------------------------------------------------------------ */
/*
* Display factory for unlinkable objects
*/
class CHtmlFmtDispFactoryNormal: public CHtmlFmtDispFactory
{
public:
CHtmlFmtDispFactoryNormal(CHtmlFormatter *formatter)
: CHtmlFmtDispFactory(formatter)
{
}
virtual CHtmlDisp *new_disp_text(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
return new (formatter_)
CHtmlDispText(win, font, txt, txtlen, txtofs);
}
virtual CHtmlDispTextInput *new_disp_text_input(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new ((CHtmlFormatter *)0) CHtmlDispTextInput(
win, font, txt, txtlen, txtofs);
}
virtual class CHtmlDisp *new_disp_soft_hyphen(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new (formatter_)
CHtmlDispSoftHyphen(win, font, txt, txtlen, txtofs);
}
virtual class CHtmlDisp *new_disp_nbsp(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new (formatter_)
CHtmlDispNBSP(win, font, txt, txtlen, txtofs);
}
virtual class CHtmlDisp *new_disp_space(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs,
int wid, int pre)
{
return new (formatter_)
CHtmlDispSpace(win, font, txt, txtlen, txtofs, wid, pre);
}
virtual CHtmlDisp *new_disp_text_pre(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
return new (formatter_)
CHtmlDispTextPre(win, font, txt, txtlen, txtofs);
}
virtual CHtmlDisp *new_disp_text_debug(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt,
size_t txtlen,
unsigned long txtofs,
unsigned long linenum)
{
return new (formatter_)
CHtmlDispTextDebugsrc(win, font, txt, txtlen, txtofs, linenum);
}
virtual CHtmlDispImg *new_disp_img(class CHtmlSysWin *win,
class CHtmlResCacheObject *image,
CStringBuf *alt,
HTML_Attrib_id_t align,
long hspace, long vspace,
long width, int width_set,
long height, int height_set,
long border, CHtmlUrl *usemap,
int ismap)
{
return new (formatter_)
CHtmlDispImg(win, image, alt, align, hspace, vspace,
width, width_set, height, height_set,
border, usemap, ismap);
}
};
/*
* Display factory for unlinkable objects in character-wrap mode
*/
class CHtmlFmtDispFactoryCharWrap: public CHtmlFmtDispFactoryNormal
{
public:
CHtmlFmtDispFactoryCharWrap(class CHtmlFormatter *fmt)
: CHtmlFmtDispFactoryNormal(fmt) { }
/* create character-wrapping text items */
virtual CHtmlDisp *new_disp_text(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
return new (formatter_)
CHtmlDispTextCharWrap(win, font, txt, txtlen, txtofs);
}
virtual CHtmlDispTextInput *new_disp_text_input(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new ((CHtmlFormatter *)0) CHtmlDispTextInputCharWrap(
win, font, txt, txtlen, txtofs);
}
};
/*
* Display factor for linkable objects
*/
class CHtmlFmtDispFactoryLink: public CHtmlFmtDispFactory
{
public:
CHtmlFmtDispFactoryLink(CHtmlFormatter *formatter)
: CHtmlFmtDispFactory(formatter)
{
}
virtual CHtmlDisp *new_disp_text(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
/* create the text item */
return new (formatter_)
CHtmlDispTextLink(win, font, txt, txtlen, txtofs,
formatter_->get_cur_link());
}
virtual class CHtmlDisp *new_disp_soft_hyphen(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new (formatter_)
CHtmlDispSoftHyphenLink(win, font, txt, txtlen, txtofs,
formatter_->get_cur_link());
}
virtual class CHtmlDisp *new_disp_nbsp(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
return new (formatter_)
CHtmlDispNBSPLink(win, font, txt, txtlen, txtofs,
formatter_->get_cur_link());
}
virtual class CHtmlDisp *new_disp_space(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs,
int wid, int pre)
{
return new (formatter_)
CHtmlDispSpaceLink(win, font, txt, txtlen, txtofs, wid, pre,
formatter_->get_cur_link());
}
virtual CHtmlDispTextInput *new_disp_text_input(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
/* input text can't be meaningfully linked; use a regular text item */
return new ((CHtmlFormatter *)0) CHtmlDispTextInput(
win, font, txt, txtlen, txtofs);
}
virtual CHtmlDisp *new_disp_text_pre(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
/* create the text item */
return new (formatter_)
CHtmlDispTextPreLink(win, font, txt, txtlen, txtofs,
formatter_->get_cur_link());
}
virtual CHtmlDisp *new_disp_text_debug(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt,
size_t txtlen,
unsigned long txtofs,
unsigned long linenum)
{
/* create the text item */
return new (formatter_)
CHtmlDispTextDebugsrc(win, font, txt, txtlen,
txtofs, linenum);
}
virtual CHtmlDispImg *new_disp_img(class CHtmlSysWin *win,
class CHtmlResCacheObject *image,
CStringBuf *alt,
HTML_Attrib_id_t align,
long hspace, long vspace,
long width, int width_set,
long height, int height_set,
long border, CHtmlUrl *usemap,
int ismap)
{
/* create and return the image item */
return new (formatter_)
CHtmlDispImgLink(win, image, alt, align, hspace, vspace,
width, width_set, height, height_set,
border, usemap, ismap,
formatter_->get_cur_link());
}
};
/*
* Display factory for linked objects in character-wrap mode
*/
class CHtmlFmtDispFactoryLinkCharWrap: public CHtmlFmtDispFactoryLink
{
public:
CHtmlFmtDispFactoryLinkCharWrap(class CHtmlFormatter *fmt)
: CHtmlFmtDispFactoryLink(fmt) { }
/* create character-wrapping text items */
virtual CHtmlDisp *new_disp_text(class CHtmlSysWin *win,
class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen,
unsigned long txtofs)
{
return new (formatter_)
CHtmlDispTextLinkCharWrap(win, font, txt, txtlen, txtofs,
formatter_->get_cur_link());
}
virtual CHtmlDispTextInput *new_disp_text_input(
class CHtmlSysWin *win, class CHtmlSysFont *font,
const textchar_t *txt, size_t txtlen, unsigned long txtofs)
{
/* input text can't be meaningfully linked; use a regular text item */
return new ((CHtmlFormatter *)0) CHtmlDispTextInputCharWrap(
win, font, txt, txtlen, txtofs);
}
};
/* ------------------------------------------------------------------------ */
/*
* formatter implementation
*/
/*
* statics
*/
CHtmlResCache *CHtmlFormatter::res_cache_ = 0;
CHtmlResFinder *CHtmlFormatter::res_finder_ = 0;
int CHtmlFormatter::formatter_cnt_ = 0;
CHtmlSoundQueue *CHtmlFormatter::sound_queues_[4] = { 0, 0, 0, 0 };
/*
* construction
*/
CHtmlFormatter::CHtmlFormatter(CHtmlParser *parser)
{
/* count the new formatter */
++formatter_cnt_;
/* if this is the first formatter, initialize the static singletons */
if (formatter_cnt_ == 1)
{
/* create the global resource cache */
res_cache_ = new CHtmlResCache();
/* create the global resource finder */
res_finder_ = new CHtmlResFinder();
}
/* save parser setting */
parser_ = parser;
/* window is as yet unknown */
win_ = 0;
/* no font yet */
curfont_ = 0;
/* no lines stored yet */
line_count_ = 0;
line_id_ = 0;
/* allocate the line starts table */
line_starts_ = new CHtmlLineStarts;
/* there's no display list yet */
body_disp_ = 0;
disp_head_ = disp_tail_ = 0;
/* there's no deferred floater list yet */
defer_head_ = defer_tail_ = 0;
/* no selection yet */
sel_start_ = sel_end_ = 0;
/* create the tab stop hash table */
tab_table_ = new CHtmlHashTable(32, new CHtmlHashFuncCI);
/* create the image map hash table */
map_table_ = new CHtmlHashTable(128, new CHtmlHashFuncCI);
cur_map_ = 0;
/* use fairly large heap allocation units for our heap allocator */
heap_alloc_unit_ = 16*1024;
/* no heap pages yet */
heap_pages_alloced_ = 0;
heap_pages_max_ = 0;
heap_pages_ = 0;
heap_page_cur_ = 0;
heap_page_cur_ofs_ = 0;
/* not in a link yet */
cur_link_ = 0;
/* not in a DIV yet */
cur_div_ = 0;
/* set up the display item factories */
disp_factory_normal_ = new CHtmlFmtDispFactoryNormal(this);
disp_factory_charwrap_ = new CHtmlFmtDispFactoryCharWrap(this);
disp_factory_link_ = new CHtmlFmtDispFactoryLink(this);
disp_factory_link_charwrap_ = new CHtmlFmtDispFactoryLinkCharWrap(this);
cur_disp_factory_ = disp_factory_normal_;
/* start off in word-wrap mode */
char_wrap_mode_ = FALSE;
/* start at first sound generation */
sound_gen_id_ = 1;
/* no saved table environments yet */
table_env_save_sp_ = 0;
/* not yet freezing adjustments */
freeze_display_adjust_ = FALSE;
/* we haven't set any positions yet */
disp_max_y_pos_ = 0;
layout_max_y_pos_ = 0;
}
/*
* destruction
*/
CHtmlFormatter::~CHtmlFormatter()
{
size_t i;
/* delete any display list */
delete_display_list();
/* delete line starts table */
delete line_starts_;
/* delete the tab stop hash table */
delete tab_table_;
/* delete the image map table */
delete map_table_;
/* if there's an image map under construction, delete it */
if (cur_map_ != 0)
delete cur_map_;
/* delete the display item heap pages */
for (i = 0 ; i < heap_pages_alloced_ ; ++i)
th_free(heap_pages_[i].mem);
/* delete the heap page array as well */
if (heap_pages_ != 0)
th_free(heap_pages_);
/* delete the display item factories */
delete disp_factory_normal_;
delete disp_factory_charwrap_;
delete disp_factory_link_;
delete disp_factory_link_charwrap_;
/* count the loss of a formatter */
--formatter_cnt_;
/* if there are no formatters left in the system, delete the statics */
if (formatter_cnt_ == 0)
{
/* delete the global resource cache */
delete res_cache_;
/* delete the global resource finder */
delete res_finder_;
}
/* delete the body display item if it's still around */
if (body_disp_ != 0)
delete body_disp_;
}
/*
* Unset the window. This is called just before my window is about to be
* deleted.
*/
void CHtmlFormatter::unset_win()
{
CHtmlDisp *cur;
/* if we've already unset our window, there's nothing to do here */
if (win_ == 0)
return;
/* unset the window in our display list and defer list */
for (cur = disp_head_ ; cur != 0 ; cur = cur->get_next_disp())
cur->unset_win();
for (cur = defer_head_ ; cur != 0 ; cur = cur->get_next_disp())
cur->unset_win();
/* unset the window in the special body display item */
body_disp_->unset_win();
/* forget our window */
win_ = 0;
/* delete the special "body" display item */
delete body_disp_;
body_disp_ = 0;
}
/*
* delete the display list
*/
void CHtmlFormatter::delete_display_list()
{
/* delete everything in the display list */
CHtmlDisp::delete_list(disp_head_);
/* delete everything in the deferred display list */
CHtmlDisp::delete_list(defer_head_);
/* clear out the division list */
div_list_.clear();
cur_div_ = 0;
/* there's nothing in the lists now */
disp_head_ = disp_tail_ = 0;
defer_head_ = defer_tail_ = 0;
/* forget any current line start we were keeping */
line_head_ = 0;
/*
* Reset the display item pool - start at the first byte of the
* first page. Note that we don't delete the old pages; we'll
* re-use any existing pages if we're reformatting.
*/
heap_page_cur_ = 0;
heap_page_cur_ofs_ = 0;
}
/*
* Terminate playback of all active media in the display list.
*/
void CHtmlFormatter::cancel_playback()
{
CHtmlDisp *cur;
/* run through the display lists and cancel all playback */
for (cur = disp_head_ ; cur != 0 ; cur = cur->get_next_disp())
cur->cancel_playback();
for (cur = defer_head_ ; cur != 0 ; cur = cur->get_next_disp())
cur->cancel_playback();
/* notify the body display item (if we still have one) */
if (body_disp_ != 0)
body_disp_->cancel_playback();
}
/*
* set the window that will be displaying our data
*/
void CHtmlFormatter::set_win(class CHtmlSysWin *win,
const CHtmlRect *margins)
{
/* remember the window */
win_ = win;
/* store the physical margins provided by the window */
phys_margins_ = *margins;
/* initialize everything by resetting to the top of the page */
start_at_top(FALSE);
/*
* Create the special "body" pseudo-display item. Don't actually add
* this to the display list; just create it and set it aside.
*/
body_disp_ = new (0) CHtmlDispBody(win_);
}
/*
* set the physical margins
*/
void CHtmlFormatter::set_phys_margins(const CHtmlRect *margins)
{
/* store the margins */
phys_margins_ = *margins;
/* we'll need to reformat everything */
start_at_top(FALSE);
}
/*
* Clear the display list
*/
void CHtmlFormatter::reset_disp_list()
{
/* if there's no window, there's nothing to do */
if (win_ == 0)
return;
/* advise the window that we're about to delete the display list */
win_->advise_clearing_disp_list();
/* delete any old display list */
delete_display_list();
/* no lines yet in line start list */
line_starts_->clear_count();
line_count_ = 0;
line_id_ = 0;
}
/*
* Initialize or reset internal formatting settings. This resets all
* internal state *except* the display list, which we leave intact; this is
* normally used to reset state during start_at_top().
*/
void CHtmlFormatter::reset_formatter_state(int reset_sounds)
{
/* not in title mode */
title_mode_ = FALSE;
/* if there's no window, there's nothing to do */
if (win_ == 0)
return;
/* we're at the upper left corner of the document */
curpos_.set(margin_left_, phys_margins_.top);
disp_max_y_pos_ = 0;
layout_max_y_pos_ = 0;
/* make sure the window forgets any background image */
win_->set_html_bg_image(0);
/* allow formatting */
stop_formatting_ = FALSE;
/* at the beginning of a line */
last_was_newline_ = TRUE;
line_spacing_ = 0;
/* we don't have anything in the line under construction yet */
avail_line_width_ = 0;
/* default to wrapping lines that are too wide to fit in the margins */
wrap_lines_ = TRUE;
/* we're not indented from the margins yet */
margin_left_ = phys_margins_.left;
margin_right_ = phys_margins_.right;
temp_margins_ = FALSE;
/* clear the margin stack */
margin_stack_depth_ = 0;
left_flow_stk_.reset();
right_flow_stk_.reset();
/* use default alignment */
div_alignment_ = HTML_Attrib_invalid;
blk_alignment_ = HTML_Attrib_invalid;
/* ask the window for the default starting font */
curfont_ = win_->get_default_font();
/* default base font size is 3 */
font_base_size_ = 3;
/* nothing in the current line yet */
line_head_ = 0;
/* maximum line width is nil so far */
max_line_width_ = 0;
outer_max_line_width_ = 0;
/* no pending tab yet */
pending_tab_ = FALSE;
pending_tab_disp_ = 0;
/* clear out the tab table */
tab_table_->delete_all_entries();
/* not in a link yet */
cur_link_ = 0;
/* clear out the image map table */
map_table_->delete_all_entries();
/* get rid of any image map under construction */
if (cur_map_ != 0)
{
delete cur_map_;
cur_map_ = 0;
}
/* we haven't seen any ordered lists yet - start at sequence number 1 */
ol_cont_seqnum_ = 1;
/* no current or previous tag yet */
curtag_ = prvtag_ = 0;
/* no table yet */
table_pass_ = 0;
current_table_ = 0;
/*
* If we're resetting sounds, clear all current sounds in all layers,
* and start a new generation of sounds. Otherwise, keep the
* generation ID unchanged, so that we can tell that sounds we've
* already played during this generation should not be played again.
*/
if (reset_sounds)
{
/* clear current sounds in all layers */
cancel_sound(HTML_Attrib_invalid, 0.0, FALSE, FALSE);
/* start a new sound generation */
++sound_gen_id_;
}
/* start with the normal display item factory */
cur_disp_factory_ = disp_factory_normal_;
/* start off in word-wrap mode */
char_wrap_mode_ = FALSE;
/* no saved table environments yet */
table_env_save_sp_ = 0;
}
/*
* reset the formatter to restart from the beginning of the document
*/
void CHtmlFormatter::start_at_top(int reset_sounds)
{
/* reset the formatting state */
reset_formatter_state(reset_sounds);
/* clear the display list */
reset_disp_list();
}
/*
* Get the text array pointer
*/
class CHtmlTextArray *CHtmlFormatter::get_text_array() const
{
return parser_ != 0 ? parser_->get_text_array() : 0;
}
/*
* get the character at the given text offset
*/
textchar_t CHtmlFormatter::get_char_at_ofs(unsigned long ofs) const
{
/* ask the parser's text array for the character */
return parser_->get_text_array()->get_char(ofs);
}
/*
* Get the number of characters between two offsets
*/
unsigned long CHtmlFormatter::get_chars_in_ofs_range(
unsigned long startofs, unsigned long endofs) const
{
/* ask the parser's text array for the information */
return parser_->get_text_array()->get_char_count(startofs, endofs);
}
/*
* Get a pointer to the text starting at the given offset
*/
const textchar_t *CHtmlFormatter::get_text_ptr(unsigned long ofs) const
{
/* get the text from the parser's text array */
return parser_->get_text_array()->get_text(ofs);
}
/* increment a text offset */
unsigned long CHtmlFormatter::inc_text_ofs(unsigned long ofs) const
{
return parser_->get_text_array()->inc_ofs(ofs);
}
/* decrement a text offset */
unsigned long CHtmlFormatter::dec_text_ofs(unsigned long ofs) const
{
return parser_->get_text_array()->dec_ofs(ofs);
}
/*
* get the maximum text address assigned so far
*/
unsigned long CHtmlFormatter::get_text_ofs_max() const
{
return get_text_array()->get_max_addr();
}
/*
* Search for the given text
*/
int CHtmlFormatter::search_for_text(const textchar_t *txt, size_t txtlen,
int exact_case, int whole_word,
int wrap, int dir,
unsigned long startofs,
unsigned long *match_start,
unsigned long *match_end)
{
/* ask the parser's text array to perform the search */
return parser_->get_text_array()->search(
txt, txtlen, exact_case, whole_word,
wrap, dir, startofs, match_start, match_end);
}
/*
* Extract text from the text stream into the given buffer. Pulls the
* text from the given starting text offset to the given ending text
* offset.
*/
void CHtmlFormatter::extract_text(CStringBuf *buf,
unsigned long start_ofs,
unsigned long end_ofs) const
{
CHtmlDisp *disp;
/* traverse our display items and pull out their text values */
for (disp = find_by_txtofs(start_ofs, FALSE, FALSE) ;
disp != 0 ; disp = disp->get_next_disp())
{
/* if we're past the ending offset, we're done */
if (disp->is_past_text_ofs(end_ofs))
break;
/* add this item's text to the buffer */
disp->add_text_to_buf_clip(buf, start_ofs, end_ofs);
}
}
/*
* Do some formatting and return. We'll remember where we were, so the
* client can repeatedly call this routine to format an entire document.
* We return without formatting everything so that an interactive
* program can continue servicing user interface events while a long
* formatting operation is in progress.
*/
void CHtmlFormatter::do_formatting()
{
/*
* make sure we have a non-container as the trailing tag, so that we
* can properly process exits
*/
parser_->fix_trailing_cont();
/*
* Keep going until we've built a line to display, or we run out of
* tags, whichever comes first. Do not stop at the end of a line if
* the line is within a table, since we need to format each table as
* a unit.
*/
for (line_output_ = FALSE ;
curtag_ != 0 && curtag_->ready_to_format() && !stop_formatting_
&& (!line_output_ || table_pass_ != 0) ; )
{
/* format the current tag */
curtag_->format(win_, this);
/* remember the last tag we formatted */
prvtag_ = curtag_;
/* move on to the next tag in format order */
curtag_ = curtag_->get_next_fmt_tag(this);
}
/*
* If we just did the last tag, figure the positions for items in
* the partially-filled last line, since we'd normally figure line
* positions at the next line break.
*/
if (curtag_ == 0 || stop_formatting_)
{
/* put the last line's items temporarily on a new line */
++line_id_;
/* adjust positions of items in final line */
set_line_positions(line_head_, 0);
/*
* in case we re-do the last line, do it at the same line number
* as we just did
*/
--line_id_;
}
}
/*
* Figure out if I have more work to do.
*/
int CHtmlFormatter::more_to_do()
{
/* if we've halted formatting, there's nothing left to do */
if (stop_formatting_)
return FALSE;
/* if the current tag isn't ready to proceed, stop for now */
if (curtag_ != 0 && !curtag_->ready_to_format())
return FALSE;
/* if we have a non-null current tag, there's definitely more to do */
if (curtag_ != 0)
return TRUE;
/*
* There's no current tag, so we must have formatted everything we
* had in the parse tree when last we formatted anything; however,
* more parse tree may have since been built. To find out, we can
* look to see if there's a tag following the last tag we parsed; if
* so, move to that new tag and proceed.
*/
if (prvtag_ != 0)
{
/*
* make sure we have a non-container as the trailing tag, so that
* we can properly process exits
*/
parser_->fix_trailing_cont();
/*
* Ask the previous tag that we formatted to check again to see
* if there's anything after it, and re-set the current tag to
* point to the item after the last tag. If we added anything,
* this will find it.
*/
curtag_ = prvtag_->get_next_fmt_tag(this);
/* if we have a non-null current tag after that, there's more to do */
if (curtag_ != 0 && !stop_formatting_)
return TRUE;
}
/* nothing left to do */
return FALSE;
}
/*
* Add an item to the line starts table
*/
void CHtmlFormatter::add_line_start(CHtmlDisp *item)
{
/*
* only add the item if we're not in multi-column mode, and the item
* is non-null
*/
if (item != 0 && !is_multi_col())
{
unsigned long item_ofs;
/*
* The line starts table is indexed in ascending order of both y
* position and text offset. The y position is taken care of
* automatically by our insistence that we only add line starts in
* single-column areas and our monotonic y positioning strategy,
* so we need only check the text offset. If this item has a
* lower text offset than previous items, remove old items until
* this one is greater than the last item.
*/
item_ofs = item->get_text_ofs();
for ( ; line_count_ != 0 ; --line_count_)
{
CHtmlDisp *cur;
/* get this item */
cur = line_starts_->get(line_count_ - 1);
/*
* If this one is ordered properly with respect to text
* offset, we can add the new one after it. Note that, since
* we always make this check before adding an item, the entire
* existing list is always in order; so as soon as we find an
* item we're higher than, we know we're higher than all of
* the earlier items as well.
*/
if (cur->get_text_ofs() <= item_ofs)
break;
}
/* add the item */
line_starts_->add(line_count_, item, curpos_.y);
++line_count_;
}
/* update the line ID */
++line_id_;
}
/*
* Add an item to the display list. This is an internal routine that
* simply links the item into the list without performing any formatting
* on it.
*/
void CHtmlFormatter::add_to_disp_list(CHtmlDisp *item)
{
CHtmlDisp *nxt;