summaryrefslogtreecommitdiffstats
path: root/src/devices/grohtml/html-table.cpp
blob: 3d7dfcd5299c379c8abfb260dde7f34f27e92867 (plain) (blame)
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
// -*- C++ -*-
/* Copyright (C) 2002-2020 Free Software Foundation, Inc.
 *
 *  Gaius Mulley (gaius@glam.ac.uk) wrote html-table.cpp
 *
 *  html-table.h
 *
 *  provides the methods necessary to handle indentation and tab
 *  positions using html tables.
 */

/*
This file is part of groff.

groff 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.

groff 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 this program.  If not, see <http://www.gnu.org/licenses/>. */

#include "driver.h"
#include "stringclass.h"
#include "cset.h"
#include "html-table.h"
#include "ctype.h"
#include "html.h"
#include "html-text.h"

#if !defined(TRUE)
#   define TRUE  (1==1)
#endif
#if !defined(FALSE)
#   define FALSE (1==0)
#endif

extern html_dialect dialect;


tabs::tabs ()
  : tab(NULL)
{
}

tabs::~tabs ()
{
  delete_list();
}

/*
 *  delete_list - frees the tab list and sets tab to NULL.
 */

void tabs::delete_list (void)
{
  tab_position *p = tab;
  tab_position *q;

  while (p != NULL) {
    q = p;
    p = p->next;
    delete q;
  }
  tab = NULL;
}

void tabs::clear (void)
{
  delete_list();
}

/*
 *  compatible - returns TRUE if the tab stops in, s, do
 *               not conflict with the current tab stops.
 *               The new tab stops are _not_ placed into
 *               this class.
 */

int tabs::compatible (const char *s)
{
  char align;
  int  total=0;
  tab_position *last = tab;

  if (last == NULL)
    return FALSE;  // no tab stops defined

  // move over tag name
  while ((*s != (char)0) && !isspace(*s))
    s++;

  while (*s != (char)0 && last != NULL) {
    // move over white space
    while ((*s != (char)0) && isspace(*s))
      s++;
    // collect alignment
    align = *s;
    // move over alignment
    s++;
    // move over white space
    while ((*s != (char)0) && isspace(*s))
      s++;
    // collect tab position
    total = atoi(s);
    // move over tab position
    while ((*s != (char)0) && !isspace(*s))
      s++;
    if (last->alignment != align || last->position != total)
      return FALSE;

    last = last->next;
  }
  return TRUE;
}

/*
 *  init - scans the string, s, and initializes the tab stops.
 */

void tabs::init (const char *s)
{
  char align;
  int  total=0;
  tab_position *last = NULL;

  clear(); // remove any tab stops

  // move over tag name
  while ((*s != (char)0) && !isspace(*s))
    s++;

  while (*s != (char)0) {
    // move over white space
    while ((*s != (char)0) && isspace(*s))
      s++;
    // collect alignment
    align = *s;
    // move over alignment
    s++;
    // move over white space
    while ((*s != (char)0) && isspace(*s))
      s++;
    // collect tab position
    total = atoi(s);
    // move over tab position
    while ((*s != (char)0) && !isspace(*s))
      s++;
    if (last == NULL) {
      tab = new tab_position;
      last = tab;
    } else {
      last->next = new tab_position;
      last = last->next;
    }
    last->alignment = align;
    last->position = total;
    last->next = NULL;    
  }
}

/*
 *  check_init - define tab stops using, s, providing none already exist.
 */

void tabs::check_init (const char *s)
{
  if (tab == NULL)
    init(s);
}

/*
 *  find_tab - returns the tab number corresponding to the position, pos.
 */

int tabs::find_tab (int pos)
{
  tab_position *p;
  int i=0;

  for (p = tab; p != NULL; p = p->next) {
    i++;
    if (p->position == pos)
      return i;
  }
  return 0;
}

/*
 *  get_tab_pos - returns the, nth, tab position
 */

int tabs::get_tab_pos (int n)
{
  tab_position *p;

  n--;
  for (p = tab; (p != NULL) && (n>0); p = p->next) {
    n--;
    if (n == 0)
      return p->position;
  }
  return 0;
}

char tabs::get_tab_align (int n)
{
  tab_position *p;

  n--;
  for (p = tab; (p != NULL) && (n>0); p = p->next) {
    n--;
    if (n == 0)
      return p->alignment;
  }
  return 'L';
}

/*
 *  dump_tab - display tab positions
 */

void tabs::dump_tabs (void)
{
  int i=1;
  tab_position *p;

  for (p = tab; p != NULL; p = p->next) {
    printf("tab %d is %d\n", i, p->position);
    i++;
  }
}

/*
 *  html_table - methods
 */

html_table::html_table (simple_output *op, int linelen)
  : out(op), columns(NULL), linelength(linelen), last_col(NULL), start_space(FALSE)
{
  tab_stops = new tabs();
}

html_table::~html_table ()
{
  cols *c;
  if (tab_stops != NULL)
    delete tab_stops;
  
  c = columns;
  while (columns != NULL) {
    columns = columns->next;
    delete c;
    c = columns;
  }
}

/*
 *  remove_cols - remove a list of columns as defined by, c.
 */

void html_table::remove_cols (cols *c)
{
  cols *p;

  while (c != NULL) {
    p = c;
    c = c->next;
    delete p;
  }
}

/*
 *  set_linelength - sets the line length value in this table.
 *                   It also adds an extra blank column to the
 *                   table should linelen exceed the last column.
 */

void html_table::set_linelength (int linelen)
{
  cols *p = NULL;
  cols *c;
  linelength = linelen;

  for (c = columns; c != NULL; c = c->next) {
    if (c->right > linelength) {
      c->right = linelength;
      remove_cols(c->next);
      c->next = NULL;
      return;
    }
    p = c;
  }
  if (p != NULL && p->right > 0 && linelength > p->right)
    add_column(p->no+1, p->right, linelength, 'L');
}

/*
 *  get_effective_linelength -
 */

int html_table::get_effective_linelength (void)
{
  if (columns != NULL)
    return linelength - columns->left;
  else
    return linelength;
}

/*
 *  add_indent - adds the indent to a table.
 */

void html_table::add_indent (int indent)
{
  if (columns != NULL && columns->left > indent)
    add_column(0, indent, columns->left, 'L');
}

/*
 *  emit_table_header - emits the html header for this table.
 */

void html_table::emit_table_header (int space)
{
  if (columns == NULL)
    return;

  // dump_table();

  last_col = NULL;
  if (linelength > 0) {
    out->nl();
    out->nl();

    out->put_string("<table width=\"100%\"")
      .put_string(" border=\"0\" rules=\"none\" frame=\"void\"\n")
      .put_string("       cellspacing=\"0\" cellpadding=\"0\"");
    out->put_string(">")
      .nl();
    if (dialect == xhtml)
      emit_colspan();
    out->put_string("<tr valign=\"top\" align=\"left\"");
    if (space) {
      out->put_string(" style=\"margin-top: ");
      out->put_string(STYLE_VERTICAL_SPACE);
      out->put_string("\"");
    }
    out->put_string(">").nl();
  }
}

/*
 *  get_right - returns the right most position of this column.
 */

int html_table::get_right (cols *c)
{
  if (c != NULL && c->right > 0)
    return c->right;
  if (c->next != NULL)
    return c->left;
  return linelength;
}

/*
 *  set_space - assigns start_space. Used to determine the
 *              vertical alignment when generating the next table row.
 */

void html_table::set_space (int space)
{
  start_space = space;
}

/*
 *  emit_colspan - emits a series of colspan entries defining the
 *                 table columns.
 */

void html_table::emit_colspan (void)
{
  cols *b = columns;
  cols *c = columns;
  int   width = 0;

  out->put_string("<colgroup>");
  while (c != NULL) {
    if (b != NULL && b != c && is_gap(b))
      /*
       *   blank column for gap
       */
      out->put_string("<col width=\"")
	.put_number(is_gap(b))
	.put_string("%\" class=\"center\"></col>")
	.nl();
    
    width = (get_right(c)*100 + get_effective_linelength()/2)
	      / get_effective_linelength()
             - (c->left*100 + get_effective_linelength()/2)
		/get_effective_linelength();
    switch (c->alignment) {
    case 'C':
      out->put_string("<col width=\"")
	  .put_number(width)
	  .put_string("%\" class=\"center\"></col>")
	  .nl();
      break;
    case 'R':
      out->put_string("<col width=\"")
	  .put_number(width)
	  .put_string("%\" class=\"right\"></col>")
	  .nl();
      break;
    default:
      out->put_string("<col width=\"")
	  .put_number(width)
	  .put_string("%\"></col>")
	  .nl();
    }
    b = c;
    c = c->next;
  }
  out->put_string("</colgroup>").nl();
}

/*
 *  emit_td - writes out a <td> tag with a corresponding width
 *            if the dialect is html4.
 */

void html_table::emit_td (int percentage, const char *s)
{
  if (percentage) {
    if (dialect == html4) {
      out->put_string("<td width=\"")
	.put_number(percentage)
	.put_string("%\"");
      if (s != NULL)
	out->put_string(s);
      out->nl();
    }
    else {
      out->put_string("<td");
      if (s != NULL)
	out->put_string(s);
      out->nl();
    }
  }
}

/*
 *  emit_col - moves onto column, n.
 */

void html_table::emit_col (int n)
{
  cols *c = columns;
  cols *b = columns;
  int   width = 0;

  // must be a different row
  if (last_col != NULL && n <= last_col->no)
    emit_new_row();

  while (c != NULL && c->no < n)
    c = c->next;

  // can we find column, n?
  if (c != NULL && c->no == n) {
    // shutdown previous column
    if (last_col != NULL)
      out->put_string("</td>").nl();

    // find previous column
    if (last_col == NULL)
      b = columns;
    else
      b = last_col;
    
    // have we a gap?
    if (last_col != NULL) {
      emit_td(is_gap(b), "></td>");
      b = b->next;
    }

    // move across to column n
    while (b != c) {
      // we compute the difference after converting positions
      // to avoid rounding errors
      width = (get_right(b)*100 + get_effective_linelength()/2)
		/ get_effective_linelength()
	      - (b->left*100 + get_effective_linelength()/2)
		  /get_effective_linelength();
      emit_td(width, "></td>");
      // have we a gap?
      emit_td(is_gap(b), "></td>");
      b = b->next;
    }
    width = (get_right(b)*100 + get_effective_linelength()/2)
	      / get_effective_linelength()
	    - (b->left*100 + get_effective_linelength()/2)
		/get_effective_linelength();
    switch (b->alignment) {
    case 'C':
      emit_td(width, " align=center>");
      break;
    case 'R':
      emit_td(width, " align=right>");
      break;
    default:
      emit_td(width);
    }
    // remember column, b
    last_col = b;
  }
}

/*
 *  finish_row -
 */

void html_table::finish_row (void)
{
  int n = 0;
  cols *c;

  if (last_col != NULL) {
    for (c = last_col->next; c != NULL; c = c->next)
      n = c->no;
    
    if (n > 0)
      emit_col(n);
#if 1
    if (last_col != NULL) {
      out->put_string("</td>");
      last_col = NULL;
    }
#endif
    out->put_string("</tr>").nl();
  }
}

/*
 *  emit_new_row - move to the next row.
 */

void html_table::emit_new_row (void)
{
  finish_row();

  out->put_string("<tr valign=\"top\" align=\"left\"");
  if (start_space) {
    out->put_string(" style=\"margin-top: ");
    out->put_string(STYLE_VERTICAL_SPACE);
    out->put_string("\"");
  }
  out->put_string(">").nl();
  start_space = FALSE;
  last_col = NULL;
}

void html_table::emit_finish_table (void)
{
  finish_row();
  out->put_string("</table>");
}

/*
 *  add_column - adds a column. It returns FALSE if hstart..hend
 *               crosses into a different columns.
 */

int html_table::add_column (int coln, int hstart, int hend, char align)
{
  cols *c = get_column(coln);

  if (c == NULL)
    return insert_column(coln, hstart, hend, align);
  else
    return modify_column(c, hstart, hend, align);
}

/*
 *  get_column - returns the column, coln.
 */

cols *html_table::get_column (int coln)
{
  cols *c = columns;

  while (c != NULL && coln != c->no)
    c = c->next;

  if (c != NULL && coln == c->no)
    return c;
  else
    return NULL;
}

/*
 *  insert_column - inserts a column, coln.
 *                  It returns TRUE if it does not bump into
 *                  another column.
 */

int html_table::insert_column (int coln, int hstart, int hend, char align)
{
  cols *c = columns;
  cols *l = columns;
  cols *n = NULL;

  while (c != NULL && c->no < coln) {
    l = c;
    c = c->next;
  }
  if (l != NULL && l->no>coln && hend > l->left)
    return FALSE;	// new column bumps into previous one

  l = NULL;
  c = columns;
  while (c != NULL && c->no < coln) {
    l = c;
    c = c->next;
  }

  if ((l != NULL) && (hstart < l->right))
    return FALSE;	// new column bumps into previous one
  
  if ((l != NULL) && (l->next != NULL) &&
      (l->next->left < hend))
    return FALSE;  // new column bumps into next one

  n = new cols;
  if (l == NULL) {
    n->next = columns;
    columns = n;
  } else {
    n->next = l->next;
    l->next = n;
  }
  n->left = hstart;
  n->right = hend;
  n->no = coln;
  n->alignment = align;
  return TRUE;
}

/*
 *  modify_column - given a column, c, modify the width to
 *                  contain hstart..hend.
 *                  It returns TRUE if it does not clash with
 *                  the next or previous column.
 */

int html_table::modify_column (cols *c, int hstart, int hend, char align)
{
  cols *l = columns;

  while (l != NULL && l->next != c)
    l = l->next;

  if ((l != NULL) && (hstart < l->right))
    return FALSE;	// new column bumps into previous one
  
  if ((c->next != NULL) && (c->next->left < hend))
    return FALSE;  // new column bumps into next one

  if (c->left > hstart)
    c->left = hstart;

  if (c->right < hend)
    c->right = hend;

  c->alignment = align;

  return TRUE;
}

/*
 *  find_tab_column - finds the column number for position, pos.
 *                    It searches through the list tab stops.
 */

int html_table::find_tab_column (int pos)
{
  // remember the first column is reserved for untabbed glyphs
  return tab_stops->find_tab(pos)+1;
}

/*
 *  find_column - find the column number for position, pos.
 *                It searches through the list of columns.
 */

int html_table::find_column (int pos)
{
  int   p=0;
  cols *c;

  for (c = columns; c != NULL; c = c->next) {
    if (c->left > pos)
      return p;
    p = c->no;
  }
  return p;
}

/*
 *  no_columns - returns the number of table columns (rather than tabs)
 */

int html_table::no_columns (void)
{
  int n=0;
  cols *c;

  for (c = columns; c != NULL; c = c->next)
    n++;
  return n;
}

/*
 *  is_gap - returns the gap between column, c, and the next column.
 */

int html_table::is_gap (cols *c)
{
  if (c == NULL || c->right <= 0 || c->next == NULL)
    return 0;
  else
    // we compute the difference after converting positions
    // to avoid rounding errors
    return (c->next->left*100 + get_effective_linelength()/2)
	     / get_effective_linelength()
	   - (c->right*100 + get_effective_linelength()/2)
	       / get_effective_linelength();
}

/*
 *  no_gaps - returns the number of table gaps between the columns
 */

int html_table::no_gaps (void)
{
  int n=0;
  cols *c;

  for (c = columns; c != NULL; c = c->next)
    if (is_gap(c))
      n++;
  return n;
}

/*
 *  get_tab_pos - returns the, nth, tab position
 */

int html_table::get_tab_pos (int n)
{
  return tab_stops->get_tab_pos(n);
}

char html_table::get_tab_align (int n)
{
  return tab_stops->get_tab_align(n);
}


void html_table::dump_table (void)
{
  if (columns != NULL) {
    cols *c;
    for (c = columns; c != NULL; c = c->next) {
      printf("column %d  %d..%d  %c\n", c->no, c->left, c->right, c->alignment);
    }
  } else
    tab_stops->dump_tabs();
}

/*
 *  html_indent - creates an indent with indentation, ind, given
 *                a line length of linelength.
 */

html_indent::html_indent (simple_output *op, int ind, int pageoffset, int linelength)
{
  table = new html_table(op, linelength);

  table->add_column(1, ind+pageoffset, linelength, 'L');
  table->add_indent(pageoffset);
  in = ind;
  pg = pageoffset;
  ll = linelength;
}

html_indent::~html_indent (void)
{
  end();
  delete table;
}

void html_indent::begin (int space)
{
  if (in + pg == 0) {
    if (space) {
      table->out->put_string(" style=\"margin-top: ");
      table->out->put_string(STYLE_VERTICAL_SPACE);
      table->out->put_string("\"");
    }
  }
  else {
    //
    // we use exactly the same mechanism for calculating
    // indentation as html_table::emit_col
    //
    table->out->put_string(" style=\"margin-left:")
      .put_number(((in + pg) * 100 + ll/2) / ll -
		  (ll/2)/ll)
      .put_string("%;");

    if (space) {
      table->out->put_string(" margin-top: ");
      table->out->put_string(STYLE_VERTICAL_SPACE);
    }
    table->out->put_string("\"");
  }
}

void html_indent::end (void)
{
}

/*
 *  get_reg - collects the registers as supplied during initialization.
 */

void html_indent::get_reg (int *ind, int *pageoffset, int *linelength)
{
  *ind = in;
  *pageoffset = pg;
  *linelength = ll;
}