7 /* Our current implementation of combining characters requires
8 * wcwidth(). Therefore the configure script should have disabled
9 * CONFIG_COMBINE if wcwidth() doesn't exist. */
11 #define _XOPEN_SOURCE 500 /* for wcwidth */
24 #include "cache/cache.h"
25 #include "config/options.h"
26 #include "document/docdata.h"
27 #include "document/document.h"
28 #include "document/html/frames.h"
29 #include "document/html/parser.h"
30 #include "document/html/parser/parse.h"
31 #include "document/html/renderer.h"
32 #include "document/html/tables.h"
33 #include "document/options.h"
34 #include "document/refresh.h"
35 #include "document/renderer.h"
36 #include "intl/charsets.h"
37 #include "osdep/types.h"
38 #include "protocol/uri.h"
39 #include "session/session.h"
40 #include "terminal/color.h"
41 #include "terminal/draw.h"
42 #include "util/color.h"
43 #include "util/conv.h"
44 #include "util/error.h"
45 #include "util/hash.h"
46 #include "util/lists.h"
47 #include "util/memory.h"
48 #include "util/string.h"
49 #include "util/time.h"
50 #include "viewer/text/form.h"
51 #include "viewer/text/view.h"
52 #include "viewer/text/vs.h"
55 #include "document/html/internal.h"
57 /* Types and structs */
65 struct link_state_info
{
67 unsigned char *target
;
69 struct form_control
*form
;
72 struct table_cache_entry_key
{
82 struct table_cache_entry
{
83 LIST_HEAD(struct table_cache_entry
);
85 struct table_cache_entry_key key
;
89 /* Max. entries in table cache used for nested tables. */
90 #define MAX_TABLE_CACHE_ENTRIES 16384
92 /* Global variables */
93 static int table_cache_entries
;
94 static struct hash
*table_cache
;
97 struct renderer_context
{
98 int last_link_to_move
;
99 struct tag
*last_tag_to_move
;
100 /* All tags between document->tags and this tag (inclusive) should
101 * be aligned to the next line break, unless some real content follows
102 * the tag. Therefore, this virtual tags list accumulates new tags as
103 * they arrive and empties when some real content is written; if a line
104 * break is inserted in the meanwhile, the tags follow it (ie. imagine
105 * <a name="x"> <p>, then the "x" tag follows the line breaks inserted
106 * by the <p> tag). */
107 struct tag
*last_tag_for_newline
;
109 struct link_state_info link_state_info
;
111 struct conv_table
*convert_table
;
113 /* Used for setting cache info from HTTP-EQUIV meta tags. */
114 struct cache_entry
*cached
;
117 int subscript
; /* Count stacked subscripts */
118 int supscript
; /* Count stacked supscripts */
120 unsigned int empty_format
:1;
121 unsigned int nobreak
:1;
122 unsigned int nosearchable
:1;
123 unsigned int nowrap
:1; /* Activated/deactivated by SP_NOWRAP. */
126 static struct renderer_context renderer_context
;
130 static void line_break(struct html_context
*);
131 static void put_chars(struct html_context
*, unsigned char *, int);
133 #define X(x_) (part->box.x + (x_))
134 #define Y(y_) (part->box.y + (y_))
136 #define SPACES_GRANULARITY 0x7F
138 #define ALIGN_SPACES(x, o, n) mem_align_alloc(x, o, n, SPACES_GRANULARITY)
141 set_screen_char_color(struct screen_char
*schar
,
142 color_T bgcolor
, color_T fgcolor
,
143 enum color_flags color_flags
,
144 enum color_mode color_mode
)
146 struct color_pair colors
= INIT_COLOR_PAIR(bgcolor
, fgcolor
);
148 set_term_color(schar
, &colors
, color_flags
, color_mode
);
152 realloc_line(struct html_context
*html_context
, struct document
*document
,
155 struct screen_char
*pos
, *end
;
159 if (!realloc_lines(document
, y
))
162 line
= &document
->data
[y
];
163 orig_length
= line
->length
;
165 if (length
< orig_length
)
168 if (!ALIGN_LINE(&line
->chars
, line
->length
, length
+ 1))
171 /* We cannot rely on the aligned allocation to clear the members for us
172 * since for line splitting we simply trim the length. Question is if
173 * it is better to to clear the line after the splitting or here. */
174 end
= &line
->chars
[length
];
177 set_screen_char_color(end
, par_format
.color
.background
, 0x0,
178 COLOR_ENSURE_CONTRAST
, /* for bug 461 */
179 document
->options
.color_mode
);
181 for (pos
= &line
->chars
[line
->length
]; pos
< end
; pos
++) {
182 copy_screen_chars(pos
, end
, 1);
185 line
->length
= length
+ 1;
191 expand_lines(struct html_context
*html_context
, struct part
*part
,
192 int x
, int y
, int lines
, color_T bgcolor
)
196 assert(part
&& part
->document
);
197 if_assert_failed
return;
199 if (!use_document_bg_colors(&part
->document
->options
))
202 par_format
.color
.background
= bgcolor
;
204 for (line
= 0; line
< lines
; line
++)
205 realloc_line(html_context
, part
->document
, Y(y
+ line
), X(x
));
209 realloc_spaces(struct part
*part
, int length
)
211 if (length
< part
->spaces_len
)
214 if (!ALIGN_SPACES(&part
->spaces
, part
->spaces_len
, length
))
217 if (!ALIGN_SPACES(&part
->char_width
, part
->spaces_len
, length
))
221 part
->spaces_len
= length
;
227 #define LINE(y_) part->document->data[Y(y_)]
228 #define POS(x_, y_) LINE(y_).chars[X(x_)]
229 #define LEN(y_) int_max(LINE(y_).length - part->box.x, 0)
232 /* When we clear chars we want to preserve and use the background colors
233 * already in place else we could end up ``staining'' the background especial
234 * when drawing table cells. So make the cleared chars share the colors in
237 * This function does not update document.comb_x and document.comb_y.
238 * That is the caller's responsibility. */
240 clear_hchars(struct html_context
*html_context
, int x
, int y
, int width
,
241 struct screen_char
*a
)
244 struct screen_char
*pos
, *end
;
246 assert(html_context
);
247 if_assert_failed
return;
249 part
= html_context
->part
;
251 assert(part
&& part
->document
&& width
> 0);
252 if_assert_failed
return;
254 if (realloc_line(html_context
, part
->document
, Y(y
), X(x
) + width
- 1) < 0)
257 assert(part
->document
->data
);
258 if_assert_failed
return;
261 end
= pos
+ width
- 1;
267 copy_screen_chars(pos
++, end
, 1);
270 /* TODO: Merge parts with get_format_screen_char(). --jonas */
271 /* Allocates the required chars on the given line and returns the char at
272 * position (x, y) ready to be used as a template char. */
273 static inline struct screen_char
*
274 get_frame_char(struct html_context
*html_context
, struct part
*part
,
275 int x
, int y
, unsigned char data
,
276 color_T bgcolor
, color_T fgcolor
)
278 struct screen_char
*template;
280 assert(html_context
);
281 if_assert_failed
return NULL
;
283 assert(part
&& part
->document
&& x
>= 0 && y
>= 0);
284 if_assert_failed
return NULL
;
286 if (realloc_line(html_context
, part
->document
, Y(y
), X(x
)) < 0)
289 assert(part
->document
->data
);
290 if_assert_failed
return NULL
;
292 template = &POS(x
, y
);
293 template->data
= data
;
294 template->attr
= SCREEN_ATTR_FRAME
;
295 set_screen_char_color(template, bgcolor
, fgcolor
,
296 part
->document
->options
.color_flags
,
297 part
->document
->options
.color_mode
);
303 draw_frame_hchars(struct part
*part
, int x
, int y
, int width
,
304 unsigned char data
, color_T bgcolor
, color_T fgcolor
,
305 struct html_context
*html_context
)
307 struct screen_char
*template;
310 if_assert_failed
return;
312 template = get_frame_char(html_context
, part
, x
+ width
- 1, y
, data
, bgcolor
, fgcolor
);
313 if (!template) return;
315 /* The template char is the last we need to draw so only decrease @width. */
316 for (width
-= 1; width
; width
--, x
++) {
317 copy_screen_chars(&POS(x
, y
), template, 1);
322 draw_frame_vchars(struct part
*part
, int x
, int y
, int height
,
323 unsigned char data
, color_T bgcolor
, color_T fgcolor
,
324 struct html_context
*html_context
)
326 struct screen_char
*template = get_frame_char(html_context
, part
, x
, y
,
327 data
, bgcolor
, fgcolor
);
329 if (!template) return;
331 /* The template char is the first vertical char to be drawn. So
332 * copy it to the rest. */
333 for (height
-= 1, y
+= 1; height
; height
--, y
++) {
334 if (realloc_line(html_context
, part
->document
, Y(y
), X(x
)) < 0)
337 copy_screen_chars(&POS(x
, y
), template, 1);
341 static inline struct screen_char
*
342 get_format_screen_char(struct html_context
*html_context
,
343 enum link_state link_state
)
345 static struct text_style ta_cache
= INIT_TEXT_STYLE(-1, 0x0, 0x0);
346 static struct screen_char schar_cache
;
348 if (memcmp(&ta_cache
, &format
.style
, sizeof(ta_cache
))) {
349 copy_struct(&ta_cache
, &format
.style
);
350 struct text_style final_style
= format
.style
;
352 if (link_state
!= LINK_STATE_NONE
353 && html_context
->options
->underline_links
) {
354 final_style
.attr
|= AT_UNDERLINE
;
357 get_screen_char_template(&schar_cache
, html_context
->options
, final_style
);
360 if (!!(schar_cache
.attr
& SCREEN_ATTR_UNSEARCHABLE
)
361 ^ !!renderer_context
.nosearchable
) {
362 schar_cache
.attr
^= SCREEN_ATTR_UNSEARCHABLE
;
368 /* document.comb_x and document.comb_y exist only when CONFIG_COMBINE
369 * is defined. assert() does nothing if CONFIG_FASTMEM is defined. */
370 #if defined(CONFIG_COMBINE) && !defined(CONFIG_FASTMEM)
371 /** Assert that path->document->comb_x and part->document->comb_y
372 * refer to an allocated struct screen_char, or comb_x is -1.
374 * The CONFIG_COMBINE variant of set_hline() can update the
375 * screen_char.data at these coordinates. Sometimes, the coordinates
376 * have not been valid, and the update has corrupted memory. These
377 * assertions should catch that bug if it happens again.
379 * @post This function can leave ::assert_failed set, so the caller
380 * should use ::if_assert_failed, perhaps with discard_comb_x_y(). */
382 assert_comb_x_y_ok(const struct document
*document
)
385 if (document
->comb_x
!= -1) {
386 assert(document
->comb_y
>= 0);
387 assert(document
->comb_y
< document
->height
);
388 assert(document
->comb_x
>= 0);
389 assert(document
->comb_x
< document
->data
[document
->comb_y
].length
);
393 # define assert_comb_x_y_ok(document) ((void) 0)
396 #ifdef CONFIG_COMBINE
397 /** Discard any combining characters that have not yet been combined
398 * with to the previous base character. */
400 discard_comb_x_y(struct document
*document
)
402 document
->comb_x
= -1;
403 document
->comb_y
= -1;
404 document
->combi_length
= 0;
407 # define discard_comb_x_y(document) ((void) 0)
410 #ifdef CONFIG_COMBINE
412 move_comb_x_y(struct part
*part
, int xf
, int yf
, int xt
, int yt
)
414 if (part
->document
->comb_x
!= -1
415 && part
->document
->comb_y
== Y(yf
)
416 && part
->document
->comb_x
>= X(xf
)) {
418 part
->document
->comb_x
+= xt
- xf
;
419 part
->document
->comb_y
+= yt
- yf
;
421 discard_comb_x_y(part
->document
);
425 # define move_comb_x_y(part, xf, yf, xt, yt) ((void) 0)
429 /* First possibly do the format change and then find out what coordinates
430 * to use since sub- or superscript might change them */
432 set_hline(struct html_context
*html_context
, unsigned char *chars
, int charslen
,
433 enum link_state link_state
)
435 struct part
*const part
= html_context
->part
;
436 struct screen_char
*const schar
= get_format_screen_char(html_context
,
439 const int y
= part
->cy
;
442 const int utf8
= html_context
->options
->utf8
;
446 if_assert_failed
return len
;
448 assert(charslen
>= 0);
450 if (realloc_spaces(part
, x
+ charslen
))
453 /* U+00AD SOFT HYPHEN characters in HTML documents are
454 * supposed to be displayed only if the word is broken at that
455 * point. ELinks currently does not use them, so it should
456 * not display them. If the input @chars is in UTF-8, then
457 * set_hline() discards the characters. If the input is in
458 * some other charset, then set_hline() does not know which
459 * byte that charset uses for U+00AD, so it cannot discard
460 * the characters; instead, the translation table used by
461 * convert_string() has already discarded the characters.
463 * Likewise, if the input @chars is in UTF-8, then it may
464 * contain U+00A0 NO-BREAK SPACE characters; but if the input
465 * is in some other charset, then the translation table
466 * has mapped those characters to NBSP_CHAR. */
468 if (part
->document
) {
469 struct document
*const document
= part
->document
;
471 assert_comb_x_y_ok(document
);
472 if_assert_failed
discard_comb_x_y(document
);
474 /* Reallocate LINE(y).chars[] to large enough. The
475 * last parameter of realloc_line is the index of the
476 * last element to which we may want to write,
477 * i.e. one less than the required size of the array.
478 * Compute the required size by assuming that each
479 * byte of input will need at most one character cell.
480 * (All double-cell characters take up at least two
481 * bytes in UTF-8, and there are no triple-cell or
482 * wider characters.) However, if there already is an
483 * incomplete character in document->buf, then
484 * the first byte of input can result in a double-cell
485 * character, so we must reserve one extra element. */
486 orig_length
= realloc_line(html_context
, document
,
487 Y(y
), X(x
) + charslen
);
488 if (orig_length
< 0) /* error */
491 unsigned char *const end
= chars
+ charslen
;
494 if (document
->buf_length
) {
495 /* previous char was broken in the middle */
496 int length
= utf8charlen(document
->buf
);
498 unsigned char *buf_ptr
= document
->buf
;
500 for (i
= document
->buf_length
; i
< length
&& chars
< end
;) {
501 document
->buf
[i
++] = *chars
++;
503 document
->buf_length
= i
;
504 document
->buf
[i
] = '\0';
505 data
= utf8_to_unicode(&buf_ptr
, buf_ptr
+ i
);
506 if (data
!= UCS_NO_CHAR
) {
507 /* FIXME: If there was invalid
508 * UTF-8 in the buffer,
509 * @utf8_to_unicode may have left
510 * some bytes unused. Those
511 * bytes should be pulled back
512 * into @chars, rather than
513 * discarded. This is not
514 * trivial to implement because
515 * each byte may have arrived in
516 * a separate call. */
517 document
->buf_length
= 0;
520 /* Still not full char */
521 assert_comb_x_y_ok(document
);
522 LINE(y
).length
= orig_length
;
523 assert_comb_x_y_ok(document
);
524 if_assert_failed
discard_comb_x_y(document
);
529 while (chars
< end
) {
530 /* ELinks does not use NBSP_CHAR in UTF-8. */
532 data
= utf8_to_unicode(&chars
, end
);
533 if (data
== UCS_NO_CHAR
) {
537 unsigned char attr
= schar
->attr
;
539 schar
->data
= *chars
++;
540 schar
->attr
= SCREEN_ATTR_FRAME
;
541 copy_screen_chars(&POS(x
, y
), schar
, 1);
543 part
->char_width
[x
++] = 0;
548 for (i
= 0; chars
< end
;i
++) {
549 document
->buf
[i
] = *chars
++;
551 document
->buf_length
= i
;
558 if (data
== UCS_SOFT_HYPHEN
)
561 if (data
== UCS_NO_BREAK_SPACE
562 && html_context
->options
->wrap_nbsp
)
565 #ifdef CONFIG_COMBINE
566 if (wcwidth((wchar_t)data
)) {
567 if (document
->combi_length
) {
568 if (document
->comb_x
!= -1) {
569 unicode_val_T prev
= get_combined(document
->combi
, document
->combi_length
+ 1);
571 assert_comb_x_y_ok(document
);
572 if_assert_failed prev
= UCS_NO_CHAR
;
574 if (prev
!= UCS_NO_CHAR
)
575 document
->data
[document
->comb_y
]
576 .chars
[document
->comb_x
].data
= prev
;
578 document
->combi_length
= 0;
580 document
->combi
[0] = data
;
582 if (document
->combi_length
< (UCS_MAX_LENGTH_COMBINED
- 1)) {
583 document
->combi
[++document
->combi_length
] = data
;
588 part
->spaces
[x
] = (data
== UCS_SPACE
);
590 if (unicode_to_cell(data
) == 2) {
591 schar
->data
= (unicode_val_T
)data
;
592 part
->char_width
[x
] = 2;
593 copy_screen_chars(&POS(x
++, y
), schar
, 1);
594 schar
->data
= UCS_NO_CHAR
;
596 part
->char_width
[x
] = 0;
598 part
->char_width
[x
] = unicode_to_cell(data
);
599 schar
->data
= (unicode_val_T
)data
;
601 #ifdef CONFIG_COMBINE
602 document
->comb_x
= X(x
);
603 document
->comb_y
= Y(y
);
604 assert_comb_x_y_ok(document
);
605 if_assert_failed
discard_comb_x_y(document
);
607 copy_screen_chars(&POS(x
++, y
), schar
, 1);
608 } /* while chars < end */
609 } else { /* not UTF-8 */
610 for (; charslen
> 0; charslen
--, x
++, chars
++) {
611 part
->char_width
[x
] = 1;
612 if (*chars
== NBSP_CHAR
) {
614 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
616 part
->spaces
[x
] = (*chars
== ' ');
617 schar
->data
= *chars
;
619 copy_screen_chars(&POS(x
, y
), schar
, 1);
621 } /* end of UTF-8 check */
623 /* Assert that we haven't written past the end of the
624 * LINE(y).chars array. @x here is one greater than
625 * the last one used in POS(x, y). Instead of this,
626 * we could assert(X(x) < LINE(y).length) immediately
627 * before each @copy_screen_chars call above, but
628 * those are in an inner loop that should be fast. */
629 assert(X(x
) <= LINE(y
).length
);
630 /* Some part of the code is apparently using LINE(y).length
631 * for line-wrapping decisions. It may currently be too
632 * large because it was allocated above based on @charslen
633 * which is the number of bytes, not the number of cells.
634 * Change the length to the correct size, but don't let it
635 * get smaller than it was on entry to this function. */
636 assert_comb_x_y_ok(document
);
637 LINE(y
).length
= int_max(orig_length
, X(x
));
638 assert_comb_x_y_ok(document
);
639 if_assert_failed
discard_comb_x_y(document
);
641 } else { /* part->document == NULL */
643 unsigned char *const end
= chars
+ charslen
;
645 while (chars
< end
) {
648 data
= utf8_to_unicode(&chars
, end
);
649 if (data
== UCS_SOFT_HYPHEN
)
652 if (data
== UCS_NO_BREAK_SPACE
653 && html_context
->options
->wrap_nbsp
)
655 part
->spaces
[x
] = (data
== UCS_SPACE
);
657 part
->char_width
[x
] = unicode_to_cell(data
);
658 if (part
->char_width
[x
] == 2) {
661 part
->char_width
[x
] = 0;
663 if (data
== UCS_NO_CHAR
) {
664 /* this is at the end only */
668 } /* while chars < end */
670 } else { /* not UTF-8 */
671 for (; charslen
> 0; charslen
--, x
++, chars
++) {
672 part
->char_width
[x
] = 1;
673 if (*chars
== NBSP_CHAR
) {
674 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
676 part
->spaces
[x
] = (*chars
== ' ');
680 } /* end of part->document check */
685 /* First possibly do the format change and then find out what coordinates
686 * to use since sub- or superscript might change them */
688 set_hline(struct html_context
*html_context
, unsigned char *chars
, int charslen
,
689 enum link_state link_state
)
691 struct part
*part
= html_context
->part
;
692 struct screen_char
*schar
= get_format_screen_char(html_context
,
698 if_assert_failed
return;
700 if (realloc_spaces(part
, x
+ charslen
))
703 if (part
->document
) {
704 if (realloc_line(html_context
, part
->document
,
705 Y(y
), X(x
) + charslen
- 1) < 0)
708 for (; charslen
> 0; charslen
--, x
++, chars
++) {
709 if (*chars
== NBSP_CHAR
) {
711 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
713 part
->spaces
[x
] = (*chars
== ' ');
714 schar
->data
= *chars
;
716 copy_screen_chars(&POS(x
, y
), schar
, 1);
719 for (; charslen
> 0; charslen
--, x
++, chars
++) {
720 if (*chars
== NBSP_CHAR
) {
721 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
723 part
->spaces
[x
] = (*chars
== ' ');
728 #endif /* CONFIG_UTF8 */
731 move_links(struct html_context
*html_context
, int xf
, int yf
, int xt
, int yt
)
735 int nlink
= renderer_context
.last_link_to_move
;
738 assert(html_context
);
739 if_assert_failed
return;
741 part
= html_context
->part
;
743 assert(part
&& part
->document
);
744 if_assert_failed
return;
746 if (!realloc_lines(part
->document
, Y(yt
)))
749 for (; nlink
< part
->document
->nlinks
; nlink
++) {
750 struct link
*link
= &part
->document
->links
[nlink
];
753 for (i
= 0; i
< link
->npoints
; i
++) {
754 /* Fix for bug 479 (part one) */
755 /* The scenario that triggered it:
757 * Imagine a centered element containing a really long
758 * word (over half of the screen width long) followed
759 * by a few links with no spaces between them where all
760 * the link text combined with the really long word
761 * will force the line to be wrapped. When rendering
762 * the line first words (including link text words) are
763 * put on one line. Then wrapping is performed moving
764 * all links from current line to the one below. Then
765 * the current line (now only containing the really
766 * long word) is centered. This will trigger a call to
767 * move_links() which will increment.
769 * Without the fix below the centering of the current
770 * line will increment last_link_to_move to that of the
771 * last link which means centering of the next line
772 * with all the links will only move the last link
773 * leaving all the other links' points dangling and
774 * causing buggy link highlighting.
776 * Even links like textareas will be correctly handled
777 * because @last_link_to_move is a way to optimize how
778 * many links move_links() will have to iterate and
779 * this little fix will only decrease the effect of the
780 * optimization by always ensuring it is never
781 * incremented too far. */
782 if (!matched
&& link
->points
[i
].y
> Y(yf
)) {
787 if (link
->points
[i
].y
!= Y(yf
))
792 if (link
->points
[i
].x
< X(xf
))
796 link
->points
[i
].y
= Y(yt
);
797 link
->points
[i
].x
+= -xf
+ xt
;
799 int to_move
= link
->npoints
- (i
+ 1);
801 assert(to_move
>= 0);
804 memmove(&link
->points
[i
],
805 &link
->points
[i
+ 1],
807 sizeof(*link
->points
));
816 renderer_context
.last_link_to_move
= nlink
;
820 /* Don't move tags when removing links. */
824 tag
= renderer_context
.last_tag_to_move
;
826 while (list_has_next(part
->document
->tags
, tag
)) {
829 if (tag
->y
== Y(yf
)) {
831 if (tag
->x
>= X(xf
)) {
836 } else if (!matched
&& tag
->y
> Y(yf
)) {
837 /* Fix for bug 479 (part two) */
841 if (!matched
) renderer_context
.last_tag_to_move
= tag
;
845 /* This function does not update document.comb_x and document.comb_y.
846 * That is the caller's responsibility. */
848 copy_chars(struct html_context
*html_context
, int x
, int y
, int width
, struct screen_char
*d
)
852 assert(html_context
);
853 if_assert_failed
return;
855 part
= html_context
->part
;
857 assert(width
> 0 && part
&& part
->document
&& part
->document
->data
);
858 if_assert_failed
return;
860 if (realloc_line(html_context
, part
->document
, Y(y
), X(x
) + width
- 1) < 0)
863 copy_screen_chars(&POS(x
, y
), d
, width
);
867 move_chars(struct html_context
*html_context
, int x
, int y
, int nx
, int ny
)
871 assert(html_context
);
872 if_assert_failed
return;
874 part
= html_context
->part
;
876 assert(part
&& part
->document
&& part
->document
->data
);
877 if_assert_failed
return;
879 if (LEN(y
) - x
<= 0) return;
880 copy_chars(html_context
, nx
, ny
, LEN(y
) - x
, &POS(x
, y
));
882 assert_comb_x_y_ok(part
->document
);
883 move_comb_x_y(part
, x
, y
, nx
, ny
);
884 LINE(y
).length
= X(x
);
885 assert_comb_x_y_ok(part
->document
);
886 if_assert_failed
discard_comb_x_y(part
->document
);
887 move_links(html_context
, x
, y
, nx
, ny
);
890 /** Shift the line @a y to the right by @a shift character cells,
891 * and update document.comb_x and document.comb_y. */
893 shift_chars(struct html_context
*html_context
, int y
, int shift
)
896 struct screen_char
*a
;
899 assert(html_context
);
900 if_assert_failed
return;
902 part
= html_context
->part
;
904 assert(part
&& part
->document
&& part
->document
->data
);
905 if_assert_failed
return;
909 a
= fmem_alloc(len
* sizeof(*a
));
912 copy_screen_chars(a
, &POS(0, y
), len
);
914 assert_comb_x_y_ok(part
->document
);
915 if_assert_failed
discard_comb_x_y(part
->document
);
917 clear_hchars(html_context
, 0, y
, shift
, a
);
918 copy_chars(html_context
, shift
, y
, len
, a
);
921 move_links(html_context
, 0, y
, shift
, y
);
922 move_comb_x_y(part
, 0, y
, shift
, y
);
924 assert_comb_x_y_ok(part
->document
);
925 if_assert_failed
discard_comb_x_y(part
->document
);
929 del_chars(struct html_context
*html_context
, int x
, int y
)
933 assert(html_context
);
934 if_assert_failed
return;
936 part
= html_context
->part
;
938 assert(part
&& part
->document
&& part
->document
->data
);
939 if_assert_failed
return;
941 assert_comb_x_y_ok(part
->document
);
942 if_assert_failed
discard_comb_x_y(part
->document
);
944 LINE(y
).length
= X(x
);
945 move_comb_x_y(part
, x
, y
, -1, -1);
946 move_links(html_context
, x
, y
, -1, -1);
948 assert_comb_x_y_ok(part
->document
);
949 if_assert_failed
discard_comb_x_y(part
->document
);
952 #if TABLE_LINE_PADDING < 0
953 # define overlap_width(x) (x).width
955 # define overlap_width(x) int_min((x).width, \
956 html_context->options->box.width - TABLE_LINE_PADDING)
958 #define overlap(x) int_max(overlap_width(x) - (x).rightmargin, 0)
961 split_line_at(struct html_context
*html_context
, int width
)
965 int new_width
= width
+ par_format
.rightmargin
;
967 assert(html_context
);
968 if_assert_failed
return 0;
970 part
= html_context
->part
;
973 if_assert_failed
return 0;
975 /* Make sure that we count the right margin to the total
976 * actual box width. */
977 int_lower_bound(&part
->box
.width
, new_width
);
979 if (part
->document
) {
980 assert(part
->document
->data
);
981 if_assert_failed
return 0;
983 if (html_context
->options
->utf8
984 && width
< part
->spaces_len
&& part
->char_width
[width
] == 2) {
985 move_chars(html_context
, width
, part
->cy
, par_format
.leftmargin
, part
->cy
+ 1);
986 del_chars(html_context
, width
, part
->cy
);
990 assertm(POS(width
, part
->cy
).data
== ' ',
991 "bad split: %c", POS(width
, part
->cy
).data
);
992 move_chars(html_context
, width
+ 1, part
->cy
, par_format
.leftmargin
, part
->cy
+ 1);
993 del_chars(html_context
, width
, part
->cy
);
999 if (!(html_context
->options
->utf8
1000 && width
< part
->spaces_len
1001 && part
->char_width
[width
] == 2))
1003 width
++; /* Since we were using (x + 1) only later... */
1005 tmp
= part
->spaces_len
- width
;
1007 /* 0 is possible and I'm paranoid ... --Zas */
1008 memmove(part
->spaces
, part
->spaces
+ width
, tmp
);
1010 memmove(part
->char_width
, part
->char_width
+ width
, tmp
);
1015 if_assert_failed tmp
= 0;
1016 memset(part
->spaces
+ tmp
, 0, width
);
1018 memset(part
->char_width
+ tmp
, 0, width
);
1021 if (par_format
.leftmargin
> 0) {
1022 tmp
= part
->spaces_len
- par_format
.leftmargin
;
1023 assertm(tmp
> 0, "part->spaces_len - par_format.leftmargin == %d", tmp
);
1024 /* So tmp is zero, memmove() should survive that. Don't recover. */
1025 memmove(part
->spaces
+ par_format
.leftmargin
, part
->spaces
, tmp
);
1027 memmove(part
->char_width
+ par_format
.leftmargin
, part
->char_width
, tmp
);
1033 if (part
->cx
== width
) {
1035 int_lower_bound(&part
->box
.height
, part
->cy
);
1038 part
->cx
-= width
- par_format
.leftmargin
;
1039 int_lower_bound(&part
->box
.height
, part
->cy
+ 1);
1044 /* Here, we scan the line for a possible place where we could split it into two
1045 * (breaking it, because it is too long), if it is overlapping from the maximal
1047 /* Returns 0 if there was found no spot suitable for breaking the line.
1048 * 1 if the line was split into two.
1049 * 2 if the (second) splitted line is blank (that is useful to determine
1050 * ie. if the next line_break() should really break the line; we don't
1051 * want to see any blank lines to pop up, do we?). */
1053 split_line(struct html_context
*html_context
)
1058 assert(html_context
);
1059 if_assert_failed
return 0;
1061 part
= html_context
->part
;
1064 if_assert_failed
return 0;
1067 if (html_context
->options
->utf8
) {
1068 for (x
= overlap(par_format
); x
>= par_format
.leftmargin
; x
--) {
1070 if (x
< part
->spaces_len
&& (part
->spaces
[x
]
1071 || (part
->char_width
[x
] == 2
1072 /* Ugly hack. If we haven't place for
1073 * double-width characters we print two
1074 * double-width characters. */
1075 && x
!= par_format
.leftmargin
)))
1076 return split_line_at(html_context
, x
);
1079 for (x
= par_format
.leftmargin
; x
< part
->cx
; x
++) {
1080 if (x
< part
->spaces_len
&& (part
->spaces
[x
]
1081 || (part
->char_width
[x
] == 2
1082 /* We want to break line after _second_
1083 * double-width character. */
1084 && x
> par_format
.leftmargin
)))
1085 return split_line_at(html_context
, x
);
1090 for (x
= overlap(par_format
); x
>= par_format
.leftmargin
; x
--)
1091 if (x
< part
->spaces_len
&& part
->spaces
[x
])
1092 return split_line_at(html_context
, x
);
1094 for (x
= par_format
.leftmargin
; x
< part
->cx
; x
++)
1095 if (x
< part
->spaces_len
&& part
->spaces
[x
])
1096 return split_line_at(html_context
, x
);
1099 /* Make sure that we count the right margin to the total
1100 * actual box width. */
1101 int_lower_bound(&part
->box
.width
, part
->cx
+ par_format
.rightmargin
);
1106 /* Insert @new_spaces spaces before the coordinates @x and @y,
1107 * adding those spaces to whatever link is at those coordinates. */
1108 /* TODO: Integrate with move_links. */
1110 insert_spaces_in_link(struct part
*part
, int x
, int y
, int new_spaces
)
1112 int i
= part
->document
->nlinks
;
1118 struct link
*link
= &part
->document
->links
[i
];
1119 int j
= link
->npoints
;
1122 struct point
*point
= &link
->points
[j
];
1124 if (point
->x
!= x
|| point
->y
!= y
)
1127 if (!realloc_points(link
, link
->npoints
+ new_spaces
))
1130 link
->npoints
+= new_spaces
;
1131 point
= &link
->points
[link
->npoints
- 1];
1133 while (new_spaces
--) {
1144 /* This function is very rare exemplary of clean and beautyful code here.
1145 * Please handle with care. --pasky */
1147 justify_line(struct html_context
*html_context
, int y
)
1150 struct screen_char
*line
; /* we save original line here */
1157 assert(html_context
);
1158 if_assert_failed
return;
1160 part
= html_context
->part
;
1162 assert(part
&& part
->document
&& part
->document
->data
);
1163 if_assert_failed
return;
1167 if_assert_failed
return;
1169 line
= fmem_alloc(len
* sizeof(*line
));
1172 /* It may sometimes happen that the line is only one char long and that
1173 * char is space - then we're going to write to both [0] and [1], but
1174 * we allocated only one field. Thus, we've to do (len + 1). --pasky */
1175 space_list
= fmem_alloc((len
+ 1) * sizeof(*space_list
));
1181 copy_screen_chars(line
, &POS(0, y
), len
);
1183 /* Skip leading spaces */
1188 while (line
[pos
].data
== ' ')
1191 /* Yes, this can be negative, we know. But we add one to it always
1192 * anyway, so it's ok. */
1193 space_list
[spaces
++] = pos
- 1;
1197 for (; pos
< len
; pos
++)
1198 if (line
[pos
].data
== ' ')
1199 space_list
[spaces
++] = pos
;
1201 space_list
[spaces
] = len
;
1205 /* Diff is the difference between the width of the paragraph
1206 * and the current length of the line. */
1207 diff
= overlap(par_format
) - len
;
1209 /* We check diff > 0 because diff can be negative (i.e., we have
1210 * an unbroken line of length > overlap(par_format))
1211 * even when spaces > 1 if the line has only non-breaking spaces. */
1212 if (spaces
> 1 && diff
> 0) {
1216 /* Allocate enough memory for the justified line.
1217 * If the memory is not available, then leave the
1218 * line unchanged, rather than halfway there. The
1219 * following loop assumes the allocation succeeded. */
1220 if (!realloc_line(html_context
, html_context
->part
->document
,
1221 Y(y
), X(overlap(par_format
))))
1224 for (word
= 0; word
< spaces
; word
++) {
1225 /* We have to increase line length by 'diff' num. of
1226 * characters, so we move 'word'th word 'word_shift'
1227 * characters right. */
1228 int word_start
= space_list
[word
] + 1;
1229 int word_len
= space_list
[word
+ 1] - word_start
;
1234 assert(word_len
>= 0);
1235 if_assert_failed
continue;
1237 word_shift
= (word
* diff
) / (spaces
- 1);
1238 new_start
= word_start
+ word_shift
;
1240 /* Assert that the realloc_line() above
1241 * allocated enough memory for the word
1242 * and the preceding spaces. */
1243 assert(LEN(y
) >= new_start
+ word_len
);
1244 if_assert_failed
continue;
1246 /* Copy the original word, without any spaces.
1247 * word_len may be 0 here. */
1248 copy_screen_chars(&POS(new_start
, y
),
1249 &line
[word_start
], word_len
);
1251 /* Copy the space that preceded the word,
1252 * duplicating it as many times as necessary.
1253 * This preserves its attributes, such as
1254 * background color and underlining. If this
1255 * is the first word, then skip the copy
1256 * because there might not be a space there
1257 * and anyway it need not be duplicated. */
1261 for (spacex
= prev_end
; spacex
< new_start
;
1263 copy_screen_chars(&POS(spacex
, y
),
1264 &line
[word_start
- 1],
1269 /* Remember that any links at the right side
1270 * of the added spaces have moved, and the
1271 * spaces themselves may also belong to a
1273 new_spaces
= new_start
- prev_end
- 1;
1274 if (word
&& new_spaces
) {
1275 move_comb_x_y(part
, prev_end
+ 1, y
, new_start
, y
);
1276 move_links(html_context
, prev_end
+ 1, y
, new_start
, y
);
1277 insert_spaces_in_link(part
,
1278 new_start
, y
, new_spaces
);
1281 prev_end
= new_start
+ word_len
;
1286 fmem_free(space_list
);
1291 align_line(struct html_context
*html_context
, int y
, int last
)
1297 assert(html_context
);
1298 if_assert_failed
return;
1300 part
= html_context
->part
;
1302 assert(part
&& part
->document
&& part
->document
->data
);
1303 if_assert_failed
return;
1307 if (!len
|| par_format
.align
== ALIGN_LEFT
)
1310 if (par_format
.align
== ALIGN_JUSTIFY
) {
1312 justify_line(html_context
, y
);
1316 shift
= overlap(par_format
) - len
;
1317 if (par_format
.align
== ALIGN_CENTER
)
1320 shift_chars(html_context
, y
, shift
);
1324 init_link_event_hooks(struct html_context
*html_context
, struct link
*link
)
1326 link
->event_hooks
= mem_calloc(1, sizeof(*link
->event_hooks
));
1327 if (!link
->event_hooks
) return;
1329 #define add_evhook(list_, type_, src_) \
1331 struct script_event_hook *evhook; \
1335 evhook = mem_calloc(1, sizeof(*evhook)); \
1336 if (!evhook) break; \
1338 evhook->type = type_; \
1339 evhook->src = stracpy(src_); \
1340 add_to_list(*(list_), evhook); \
1343 init_list(*link
->event_hooks
);
1344 add_evhook(link
->event_hooks
, SEVHOOK_ONCLICK
, format
.onclick
);
1345 add_evhook(link
->event_hooks
, SEVHOOK_ONDBLCLICK
, format
.ondblclick
);
1346 add_evhook(link
->event_hooks
, SEVHOOK_ONMOUSEOVER
, format
.onmouseover
);
1347 add_evhook(link
->event_hooks
, SEVHOOK_ONHOVER
, format
.onhover
);
1348 add_evhook(link
->event_hooks
, SEVHOOK_ONFOCUS
, format
.onfocus
);
1349 add_evhook(link
->event_hooks
, SEVHOOK_ONMOUSEOUT
, format
.onmouseout
);
1350 add_evhook(link
->event_hooks
, SEVHOOK_ONBLUR
, format
.onblur
);
1355 static struct link
*
1356 new_link(struct html_context
*html_context
, unsigned char *name
, int namelen
)
1358 struct document
*document
;
1363 assert(html_context
);
1364 if_assert_failed
return NULL
;
1366 part
= html_context
->part
;
1369 if_assert_failed
return NULL
;
1371 document
= part
->document
;
1374 if_assert_failed
return NULL
;
1376 link_number
= part
->link_num
;
1378 if (!ALIGN_LINK(&document
->links
, document
->nlinks
, document
->nlinks
+ 1))
1381 link
= &document
->links
[document
->nlinks
++];
1382 link
->number
= link_number
- 1;
1383 if (document
->options
.use_tabindex
) link
->number
+= format
.tabindex
;
1384 link
->accesskey
= format
.accesskey
;
1385 link
->title
= null_or_stracpy(format
.title
);
1386 link
->where_img
= null_or_stracpy(format
.image
);
1389 link
->target
= null_or_stracpy(format
.target
);
1390 link
->data
.name
= memacpy(name
, namelen
);
1391 /* if (strlen(url) > 4 && !c_strncasecmp(url, "MAP@", 4)) { */
1393 && ((format
.link
[0]|32) == 'm')
1394 && ((format
.link
[1]|32) == 'a')
1395 && ((format
.link
[2]|32) == 'p')
1396 && (format
.link
[3] == '@')
1397 && format
.link
[4]) {
1398 link
->type
= LINK_MAP
;
1399 link
->where
= stracpy(format
.link
+ 4);
1401 link
->type
= LINK_HYPERTEXT
;
1402 link
->where
= null_or_stracpy(format
.link
);
1406 struct form_control
*fc
= format
.form
;
1413 link
->type
= LINK_FIELD
;
1416 link
->type
= LINK_AREA
;
1420 link
->type
= LINK_CHECKBOX
;
1423 link
->type
= LINK_SELECT
;
1430 link
->type
= LINK_BUTTON
;
1432 link
->data
.form_control
= fc
;
1433 /* At this point, format.form might already be set but
1434 * the form_control not registered through SP_CONTROL
1435 * yet, therefore without fc->form set. It is always
1436 * after the "good" last form was already processed,
1437 * though, so we can safely just take that. */
1439 if (!form
&& !list_empty(document
->forms
))
1440 form
= document
->forms
.next
;
1441 link
->target
= null_or_stracpy(form
? form
->target
: NULL
);
1444 link
->color
.background
= format
.style
.color
.background
;
1445 link
->color
.foreground
= link_is_textinput(link
)
1446 ? format
.style
.color
.foreground
1447 : format
.color
.clink
;
1449 init_link_event_hooks(html_context
, link
);
1451 document
->links_sorted
= 0;
1456 html_special_tag(struct document
*document
, unsigned char *t
, int x
, int y
)
1462 if_assert_failed
return;
1464 tag_len
= strlen(t
);
1465 /* One byte is reserved for name in struct tag. */
1466 tag
= mem_alloc(sizeof(*tag
) + tag_len
);
1471 memcpy(tag
->name
, t
, tag_len
+ 1);
1472 add_to_list(document
->tags
, tag
);
1473 if (renderer_context
.last_tag_for_newline
== (struct tag
*) &document
->tags
)
1474 renderer_context
.last_tag_for_newline
= tag
;
1479 put_chars_conv(struct html_context
*html_context
,
1480 unsigned char *chars
, int charslen
)
1484 assert(html_context
);
1485 if_assert_failed
return;
1487 part
= html_context
->part
;
1489 assert(part
&& chars
&& charslen
);
1490 if_assert_failed
return;
1492 if (format
.style
.attr
& AT_GRAPHICS
) {
1493 put_chars(html_context
, chars
, charslen
);
1497 convert_string(renderer_context
.convert_table
, chars
, charslen
,
1498 html_context
->options
->cp
,
1499 (format
.style
.attr
& AT_NO_ENTITIES
) ? CSM_NONE
: CSM_DEFAULT
,
1500 NULL
, (void (*)(void *, unsigned char *, int)) put_chars
, html_context
);
1504 put_link_number(struct html_context
*html_context
)
1506 struct part
*part
= html_context
->part
;
1507 unsigned char s
[64];
1508 unsigned char *fl
= format
.link
;
1509 unsigned char *ft
= format
.target
;
1510 unsigned char *fi
= format
.image
;
1511 struct form_control
*ff
= format
.form
;
1514 format
.link
= format
.target
= format
.image
= NULL
;
1518 ulongcat(s
, &slen
, part
->link_num
, sizeof(s
) - 3, 0);
1522 renderer_context
.nosearchable
= 1;
1523 put_chars(html_context
, s
, slen
);
1524 renderer_context
.nosearchable
= 0;
1526 if (ff
&& ff
->type
== FC_TEXTAREA
) line_break(html_context
);
1528 /* We might have ended up on a new line after the line breaking
1529 * or putting the link number chars. */
1530 if (part
->cx
== -1) part
->cx
= par_format
.leftmargin
;
1538 #define assert_link_variable(old, new) \
1539 assertm(!(old), "Old link value [%s]. New value [%s]", old, new);
1542 init_link_state_info(unsigned char *link
, unsigned char *target
,
1543 unsigned char *image
, struct form_control
*form
)
1545 assert_link_variable(renderer_context
.link_state_info
.image
, image
);
1546 assert_link_variable(renderer_context
.link_state_info
.target
, target
);
1547 assert_link_variable(renderer_context
.link_state_info
.link
, link
);
1549 renderer_context
.link_state_info
.link
= null_or_stracpy(link
);
1550 renderer_context
.link_state_info
.target
= null_or_stracpy(target
);
1551 renderer_context
.link_state_info
.image
= null_or_stracpy(image
);
1552 renderer_context
.link_state_info
.form
= form
;
1556 done_link_state_info(void)
1558 mem_free_if(renderer_context
.link_state_info
.link
);
1559 mem_free_if(renderer_context
.link_state_info
.target
);
1560 mem_free_if(renderer_context
.link_state_info
.image
);
1561 memset(&renderer_context
.link_state_info
, 0,
1562 sizeof(renderer_context
.link_state_info
));
1567 process_link(struct html_context
*html_context
, enum link_state link_state
,
1568 unsigned char *chars
, int charslen
, int cells
)
1571 process_link(struct html_context
*html_context
, enum link_state link_state
,
1572 unsigned char *chars
, int charslen
)
1573 #endif /* CONFIG_UTF8 */
1575 struct part
*part
= html_context
->part
;
1579 switch (link_state
) {
1580 case LINK_STATE_SAME
: {
1581 unsigned char *name
;
1583 if (!part
->document
) return;
1585 assertm(part
->document
->nlinks
> 0, "no link");
1586 if_assert_failed
return;
1588 link
= &part
->document
->links
[part
->document
->nlinks
- 1];
1590 name
= get_link_name(link
);
1592 unsigned char *new_name
;
1594 new_name
= straconcat(name
, chars
,
1595 (unsigned char *) NULL
);
1598 link
->data
.name
= new_name
;
1602 /* FIXME: Concatenating two adjectent <a> elements to a single
1603 * link is broken since we lose the event handlers for the
1604 * second one. OTOH simply appending them here won't fly since
1605 * we may get here multiple times for even a single link. We
1606 * will probably need some SP_ for creating a new link or so.
1612 case LINK_STATE_NEW
:
1615 init_link_state_info(format
.link
, format
.target
,
1616 format
.image
, format
.form
);
1617 if (!part
->document
) return;
1619 /* Trim leading space from the link text */
1620 while (x_offset
< charslen
&& chars
[x_offset
] <= ' ')
1624 charslen
-= x_offset
;
1628 #endif /* CONFIG_UTF8 */
1631 link
= new_link(html_context
, chars
, charslen
);
1636 case LINK_STATE_NONE
:
1638 INTERNAL("bad link_state %i", (int) link_state
);
1642 /* Add new canvas positions to the link. */
1644 if (realloc_points(link
, link
->npoints
+ cells
))
1646 if (realloc_points(link
, link
->npoints
+ charslen
))
1647 #endif /* CONFIG_UTF8 */
1649 struct point
*point
= &link
->points
[link
->npoints
];
1650 int x
= X(part
->cx
) + x_offset
;
1651 int y
= Y(part
->cy
);
1654 link
->npoints
+= cells
;
1656 for (; cells
> 0; cells
--, point
++, x
++)
1658 link
->npoints
+= charslen
;
1660 for (; charslen
> 0; charslen
--, point
++, x
++)
1661 #endif /* CONFIG_UTF8 */
1669 static inline enum link_state
1670 get_link_state(struct html_context
*html_context
)
1672 enum link_state state
;
1674 if (!(format
.link
|| format
.image
|| format
.form
)) {
1675 state
= LINK_STATE_NONE
;
1677 } else if ((renderer_context
.link_state_info
.link
1678 || renderer_context
.link_state_info
.image
1679 || renderer_context
.link_state_info
.form
)
1680 && !xstrcmp(format
.link
, renderer_context
.link_state_info
.link
)
1681 && !xstrcmp(format
.target
, renderer_context
.link_state_info
.target
)
1682 && !xstrcmp(format
.image
, renderer_context
.link_state_info
.image
)
1683 && format
.form
== renderer_context
.link_state_info
.form
) {
1685 return LINK_STATE_SAME
;
1688 state
= LINK_STATE_NEW
;
1691 done_link_state_info();
1697 html_has_non_space_chars(unsigned char *chars
, int charslen
)
1701 while (pos
< charslen
)
1702 if (!isspace(chars
[pos
++]))
1709 put_chars(struct html_context
*html_context
, unsigned char *chars
, int charslen
)
1711 enum link_state link_state
;
1715 #endif /* CONFIG_UTF8 */
1717 assert(html_context
);
1718 if_assert_failed
return;
1720 part
= html_context
->part
;
1723 if_assert_failed
return;
1725 assert(chars
&& charslen
);
1726 if_assert_failed
return;
1728 /* If we are not handling verbatim aligning and we are at the begining
1729 * of a line trim whitespace. */
1730 if (part
->cx
== -1) {
1731 /* If we are not handling verbatim aligning trim leading
1733 if (!html_is_preformatted()) {
1734 while (charslen
&& *chars
== ' ') {
1739 if (charslen
< 1) return;
1742 part
->cx
= par_format
.leftmargin
;
1745 /* For preformatted html always update 'the last tag' so we never end
1746 * up moving tags to the wrong line (Fixes bug 324). For all other html
1747 * it is moved only when the line being rendered carry some real
1748 * non-whitespace content. */
1749 if (html_is_preformatted()
1750 || html_has_non_space_chars(chars
, charslen
)) {
1751 renderer_context
.last_tag_for_newline
= (struct tag
*) &part
->document
->tags
;
1754 int_lower_bound(&part
->box
.height
, part
->cy
+ 1);
1756 link_state
= get_link_state(html_context
);
1758 if (link_state
== LINK_STATE_NEW
) {
1761 /* Don't add inaccessible links. It seems to be caused
1762 * by the parser putting a space char after stuff like
1763 * <img>-tags or comments wrapped in <a>-tags. See bug
1764 * 30 for test case. */
1765 while (x_offset
< charslen
&& chars
[x_offset
] <= ' ')
1768 /* For pure spaces reset the link state */
1769 if (x_offset
== charslen
)
1770 link_state
= LINK_STATE_NONE
;
1771 else if (html_context
->options
->links_numbering
)
1772 put_link_number(html_context
);
1776 #endif /* CONFIG_UTF8 */
1777 set_hline(html_context
, chars
, charslen
, link_state
);
1779 if (link_state
!= LINK_STATE_NONE
) {
1781 process_link(html_context
, link_state
, chars
, charslen
,
1784 process_link(html_context
, link_state
, chars
, charslen
);
1785 #endif /* CONFIG_UTF8 */
1789 if (renderer_context
.nowrap
1790 && part
->cx
+ cells
> overlap(par_format
))
1795 if (renderer_context
.nowrap
1796 && part
->cx
+ charslen
> overlap(par_format
))
1799 part
->cx
+= charslen
;
1800 #endif /* CONFIG_UTF8 */
1802 renderer_context
.nobreak
= 0;
1804 if (!(html_context
->options
->wrap
|| html_is_preformatted())) {
1805 while (part
->cx
> overlap(par_format
)
1806 && part
->cx
> par_format
.leftmargin
) {
1807 int x
= split_line(html_context
);
1811 align_line(html_context
, part
->cy
- 1, 0);
1812 renderer_context
.nobreak
= !!(x
- 1);
1816 assert(charslen
> 0);
1820 part
->xa
+= charslen
;
1821 #endif /* CONFIG_UTF8 */
1822 int_lower_bound(&part
->max_width
, part
->xa
1823 + par_format
.leftmargin
+ par_format
.rightmargin
1824 - (chars
[charslen
- 1] == ' '
1825 && !html_is_preformatted()));
1833 line_break(struct html_context
*html_context
)
1838 assert(html_context
);
1839 if_assert_failed
return;
1841 part
= html_context
->part
;
1844 if_assert_failed
return;
1846 int_lower_bound(&part
->box
.width
, part
->cx
+ par_format
.rightmargin
);
1848 if (renderer_context
.nobreak
) {
1849 renderer_context
.nobreak
= 0;
1855 if (!part
->document
|| !part
->document
->data
) goto end
;
1857 if (!realloc_lines(part
->document
, part
->box
.height
+ part
->cy
+ 1))
1860 if (part
->cx
> par_format
.leftmargin
&& LEN(part
->cy
) > part
->cx
- 1
1861 && POS(part
->cx
- 1, part
->cy
).data
== ' ') {
1862 del_chars(html_context
, part
->cx
- 1, part
->cy
);
1866 if (part
->cx
> 0) align_line(html_context
, part
->cy
, 1);
1868 for (tag
= renderer_context
.last_tag_for_newline
;
1869 tag
&& tag
!= (struct tag
*) &part
->document
->tags
;
1872 tag
->y
= Y(part
->cy
+ 1);
1879 memset(part
->spaces
, 0, part
->spaces_len
);
1881 memset(part
->char_width
, 0, part
->spaces_len
);
1886 html_special_form(struct part
*part
, struct form
*form
)
1890 assert(part
&& form
);
1891 assert(form
->form_num
> 0);
1892 assert(form
->form_end
== INT_MAX
);
1893 if_assert_failed
return;
1895 if (!part
->document
) {
1900 /* Make a fake form with form_num == 0 so that there is
1901 * something to use if form controls appear above the first
1902 * actual FORM element. There can never be a real form with
1903 * form_num == 0 because the form_num is the position after the
1904 * "<form" characters and that's already five characters. The
1905 * fake form does not have a name, and it gets a form_view and
1906 * becomes visible to ECMAScript only if it actually has
1907 * controls in it. */
1908 if (list_empty(part
->document
->forms
)) {
1909 nform
= init_form();
1914 nform
->form_num
= 0;
1915 add_to_list(part
->document
->forms
, nform
);
1918 /* Make sure the new form ``claims'' its slice of the form range
1919 * maintained in the form_num and form_end variables. */
1920 foreach (nform
, part
->document
->forms
) {
1921 if (form
->form_num
< nform
->form_num
1922 || nform
->form_end
< form
->form_num
)
1925 /* First check if the form has identical form numbers.
1926 * That should only be the case when the form being
1927 * added is in fact the same form in which case it
1928 * should be dropped. The fact that this can happen
1929 * suggests that the table renderering can be confused.
1930 * See bug 647 for a test case.
1931 * Do not compare form->form_end here because it is
1932 * normally set by this function and that has obviously
1933 * not yet been done. */
1934 if (nform
->form_num
== form
->form_num
) {
1939 /* The form start is inside an already added form, so
1940 * partition the space of the existing form and get
1942 form
->form_end
= nform
->form_end
;
1943 nform
->form_end
= form
->form_num
- 1;
1944 assertm(nform
->form_num
<= nform
->form_end
,
1945 "[%d:%d] [%d:%d]", nform
->form_num
, nform
->form_end
,
1946 form
->form_num
, form
->form_end
);
1947 add_to_list(part
->document
->forms
, form
);
1951 ERROR("hole between forms");
1957 html_special_form_control(struct part
*part
, struct form_control
*fc
)
1962 if_assert_failed
return;
1964 if (!part
->document
) {
1965 done_form_control(fc
);
1970 fc
->g_ctrl_num
= renderer_context
.g_ctrl_num
++;
1972 if (list_empty(part
->document
->forms
)) {
1973 /* No forms encountered yet, that means a homeless form
1974 * control. Generate a dummy form for those Flying
1978 add_to_list(part
->document
->forms
, form
);
1980 /* Attach this form control to the last form encountered. */
1981 form
= part
->document
->forms
.next
;
1983 add_to_list(form
->items
, fc
);
1987 /** Assert that each form in the list has a different form.form_num
1988 * ... form.form_end range and that the ranges are contiguous and
1989 * together cover all numbers from 0 to INT_MAX. Alternatively, the
1990 * whole list may be empty. This function can be called from a
1991 * debugger, or automatically from some places.
1993 * This function may leave assert_failed = 1; the caller must use
1994 * if_assert_failed. */
1996 assert_forms_list_ok(LIST_OF(struct form
) *forms
)
1998 int saw_form_num_0
= 0;
2001 if (list_empty(*forms
)) return;
2003 /* O(n^2) algorithm, but it's only for debugging. */
2004 foreach (outer
, *forms
) {
2008 if (outer
->form_num
== 0)
2011 foreach (inner
, *forms
) {
2012 assert(inner
== outer
2013 || inner
->form_num
> outer
->form_end
2014 || outer
->form_num
> inner
->form_end
);
2015 if (outer
->form_end
== inner
->form_num
- 1)
2019 if (outer
->form_end
== INT_MAX
)
2020 assert(followers
== 0);
2022 assert(followers
== 1);
2025 assert(saw_form_num_0
== 1);
2027 #else /* !CONFIG_DEBUG */
2028 # define assert_forms_list_ok(forms) ((void) 0)
2029 #endif /* !CONFIG_DEBUG */
2031 /* Reparents form items based on position in the source. */
2033 check_html_form_hierarchy(struct part
*part
)
2035 struct document
*document
= part
->document
;
2036 INIT_LIST_OF(struct form_control
, form_controls
);
2038 struct form_control
*fc
, *next
;
2040 if (list_empty(document
->forms
))
2043 assert_forms_list_ok(&document
->forms
);
2046 /* Take out all badly placed form items. */
2048 foreach (form
, document
->forms
) {
2050 assertm(form
->form_num
<= form
->form_end
,
2051 "%p [%d : %d]", form
, form
->form_num
, form
->form_end
);
2053 foreachsafe (fc
, next
, form
->items
) {
2054 if (form
->form_num
<= fc
->position
2055 && fc
->position
<= form
->form_end
)
2058 move_to_top_of_list(form_controls
, fc
);
2062 /* Re-insert the form items the correct places. */
2064 foreachsafe (fc
, next
, form_controls
) {
2066 foreach (form
, document
->forms
) {
2067 if (fc
->position
< form
->form_num
2068 || form
->form_end
< fc
->position
)
2072 move_to_top_of_list(form
->items
, fc
);
2077 assert(list_empty(form_controls
));
2081 color_link_lines(struct html_context
*html_context
)
2083 struct document
*document
= html_context
->part
->document
;
2084 struct color_pair colors
= INIT_COLOR_PAIR(par_format
.color
.background
, 0x0);
2085 enum color_mode color_mode
= document
->options
.color_mode
;
2086 enum color_flags color_flags
= document
->options
.color_flags
;
2089 for (y
= 0; y
< document
->height
; y
++) {
2092 for (x
= 0; x
< document
->data
[y
].length
; x
++) {
2093 struct screen_char
*schar
= &document
->data
[y
].chars
[x
];
2095 set_term_color(schar
, &colors
, color_flags
, color_mode
);
2097 /* XXX: Entering hack zone! Change to clink color after
2098 * link text has been recolored. */
2099 if (schar
->data
== ':' && colors
.foreground
== 0x0)
2100 colors
.foreground
= format
.color
.clink
;
2103 colors
.foreground
= 0x0;
2108 html_special(struct html_context
*html_context
, enum html_special_type c
, ...)
2112 struct document
*document
;
2113 void *ret_val
= NULL
;
2115 assert(html_context
);
2116 if_assert_failed
return NULL
;
2118 part
= html_context
->part
;
2121 if_assert_failed
return NULL
;
2123 document
= part
->document
;
2129 unsigned char *t
= va_arg(l
, unsigned char *);
2131 html_special_tag(document
, t
, X(part
->cx
), Y(part
->cy
));
2136 struct form
*form
= va_arg(l
, struct form
*);
2138 html_special_form(part
, form
);
2143 struct form_control
*fc
= va_arg(l
, struct form_control
*);
2145 html_special_form_control(part
, fc
);
2149 ret_val
= renderer_context
.convert_table
;
2152 ret_val
= (void *) (long) !!document
;
2154 case SP_CACHE_CONTROL
:
2156 struct cache_entry
*cached
= renderer_context
.cached
;
2158 cached
->cache_mode
= CACHE_MODE_NEVER
;
2162 case SP_CACHE_EXPIRES
:
2164 time_t expires
= va_arg(l
, time_t);
2165 struct cache_entry
*cached
= renderer_context
.cached
;
2167 if (!expires
|| cached
->cache_mode
== CACHE_MODE_NEVER
)
2170 timeval_from_seconds(&cached
->max_age
, expires
);
2176 struct frameset_param
*fsp
= va_arg(l
, struct frameset_param
*);
2177 struct frameset_desc
*frameset_desc
;
2179 if (!fsp
->parent
&& document
->frame_desc
)
2182 frameset_desc
= create_frameset(fsp
);
2183 if (!fsp
->parent
&& !document
->frame_desc
)
2184 document
->frame_desc
= frameset_desc
;
2186 ret_val
= frameset_desc
;
2191 struct frameset_desc
*parent
= va_arg(l
, struct frameset_desc
*);
2192 unsigned char *name
= va_arg(l
, unsigned char *);
2193 unsigned char *url
= va_arg(l
, unsigned char *);
2195 add_frameset_entry(parent
, NULL
, name
, url
);
2199 renderer_context
.nowrap
= !!va_arg(l
, int);
2203 unsigned long seconds
= va_arg(l
, unsigned long);
2204 unsigned char *t
= va_arg(l
, unsigned char *);
2207 if (document
->refresh
)
2208 done_document_refresh(document
->refresh
);
2209 document
->refresh
= init_document_refresh(t
, seconds
);
2213 case SP_COLOR_LINK_LINES
:
2214 if (document
&& use_document_bg_colors(&document
->options
))
2215 color_link_lines(html_context
);
2220 struct uri
*uri
= va_arg(l
, struct uri
*);
2222 add_to_uri_list(&document
->css_imports
, uri
);
2227 #ifdef CONFIG_ECMASCRIPT
2229 struct uri
*uri
= va_arg(l
, struct uri
*);
2231 add_to_uri_list(&document
->ecmascript_imports
, uri
);
2243 free_table_cache(void)
2246 struct hash_item
*item
;
2249 /* We do not free key here. */
2250 foreach_hash_item (item
, *table_cache
, i
) {
2251 mem_free_if(item
->value
);
2254 free_hash(&table_cache
);
2255 table_cache_entries
= 0;
2260 format_html_part(struct html_context
*html_context
,
2261 unsigned char *start
, unsigned char *end
,
2262 int align
, int margin
, int width
, struct document
*document
,
2263 int x
, int y
, unsigned char *head
,
2268 struct tag
*saved_last_tag_to_move
= renderer_context
.last_tag_to_move
;
2269 int saved_empty_format
= renderer_context
.empty_format
;
2270 int saved_margin
= html_context
->margin
;
2271 int saved_last_link_to_move
= renderer_context
.last_link_to_move
;
2273 /* Hash creation if needed. */
2275 table_cache
= init_hash8();
2276 } else if (!document
) {
2277 /* Search for cached entry. */
2278 struct table_cache_entry_key key
;
2279 struct hash_item
*item
;
2281 /* Clear key to prevent potential alignment problem
2282 * when keys are compared. */
2283 memset(&key
, 0, sizeof(key
));
2288 key
.margin
= margin
;
2291 key
.link_num
= link_num
;
2293 item
= get_hash_item(table_cache
,
2294 (unsigned char *) &key
,
2296 if (item
) { /* We found it in cache, so just copy and return. */
2297 part
= mem_alloc(sizeof(*part
));
2299 copy_struct(part
, &((struct table_cache_entry
*)
2300 item
->value
)->part
);
2306 assertm(y
>= 0, "format_html_part: y == %d", y
);
2307 if_assert_failed
return NULL
;
2310 struct node
*node
= mem_alloc(sizeof(*node
));
2313 int node_width
= !html_context
->table_level
? INT_MAX
: width
;
2315 set_box(&node
->box
, x
, y
, node_width
, 1);
2316 add_to_list(document
->nodes
, node
);
2319 renderer_context
.last_link_to_move
= document
->nlinks
;
2320 renderer_context
.last_tag_to_move
= (struct tag
*) &document
->tags
;
2321 renderer_context
.last_tag_for_newline
= (struct tag
*) &document
->tags
;
2323 renderer_context
.last_link_to_move
= 0;
2324 renderer_context
.last_tag_to_move
= (struct tag
*) NULL
;
2325 renderer_context
.last_tag_for_newline
= (struct tag
*) NULL
;
2328 html_context
->margin
= margin
;
2329 renderer_context
.empty_format
= !document
;
2331 done_link_state_info();
2332 renderer_context
.nobreak
= 1;
2334 part
= mem_calloc(1, sizeof(*part
));
2335 if (!part
) goto ret
;
2337 part
->document
= document
;
2342 part
->link_num
= link_num
;
2344 html_state
= init_html_parser_state(html_context
, ELEMENT_IMMORTAL
, align
, margin
, width
);
2346 parse_html(start
, end
, part
, head
, html_context
);
2348 done_html_parser_state(html_context
, html_state
);
2350 int_lower_bound(&part
->max_width
, part
->box
.width
);
2352 renderer_context
.nobreak
= 0;
2354 done_link_state_info();
2355 mem_free_if(part
->spaces
);
2357 mem_free_if(part
->char_width
);
2361 struct node
*node
= document
->nodes
.next
;
2363 node
->box
.height
= y
- node
->box
.y
+ part
->box
.height
;
2367 renderer_context
.last_link_to_move
= saved_last_link_to_move
;
2368 renderer_context
.last_tag_to_move
= saved_last_tag_to_move
;
2369 renderer_context
.empty_format
= saved_empty_format
;
2371 html_context
->margin
= saved_margin
;
2373 if (html_context
->table_level
> 1 && !document
2375 && table_cache_entries
< MAX_TABLE_CACHE_ENTRIES
) {
2376 /* Create a new entry. */
2377 /* Clear memory to prevent bad key comparaison due to alignment
2379 struct table_cache_entry
*tce
= mem_calloc(1, sizeof(*tce
));
2382 tce
->key
.start
= start
;
2384 tce
->key
.align
= align
;
2385 tce
->key
.margin
= margin
;
2386 tce
->key
.width
= width
;
2388 tce
->key
.link_num
= link_num
;
2389 copy_struct(&tce
->part
, part
);
2391 if (!add_hash_item(table_cache
,
2392 (unsigned char *) &tce
->key
,
2393 sizeof(tce
->key
), tce
)) {
2396 table_cache_entries
++;
2405 render_html_document(struct cache_entry
*cached
, struct document
*document
,
2406 struct string
*buffer
)
2408 struct html_context
*html_context
;
2410 unsigned char *start
;
2412 struct string title
;
2415 assert(cached
&& document
);
2416 if_assert_failed
return;
2418 if (!init_string(&head
)) return;
2420 if (cached
->head
) add_to_string(&head
, cached
->head
);
2422 start
= buffer
->source
;
2423 end
= buffer
->source
+ buffer
->length
;
2425 html_context
= init_html_parser(cached
->uri
, &document
->options
,
2426 start
, end
, &head
, &title
,
2427 put_chars_conv
, line_break
,
2429 if (!html_context
) return;
2431 renderer_context
.g_ctrl_num
= 0;
2432 renderer_context
.cached
= cached
;
2433 renderer_context
.convert_table
= get_convert_table(head
.source
,
2434 document
->options
.cp
,
2435 document
->options
.assume_cp
,
2437 &document
->cp_status
,
2438 document
->options
.hard_assume
);
2440 html_context
->options
->utf8
= is_cp_utf8(document
->options
.cp
);
2441 #endif /* CONFIG_UTF8 */
2442 html_context
->doc_cp
= document
->cp
;
2445 /* CSM_DEFAULT because init_html_parser() did not
2446 * decode entities in the title. */
2447 document
->title
= convert_string(renderer_context
.convert_table
,
2448 title
.source
, title
.length
,
2449 document
->options
.cp
,
2450 CSM_DEFAULT
, NULL
, NULL
, NULL
);
2452 done_string(&title
);
2454 part
= format_html_part(html_context
, start
, end
, par_format
.align
,
2455 par_format
.leftmargin
,
2456 document
->options
.box
.width
, document
,
2457 0, 0, head
.source
, 1);
2459 /* Drop empty allocated lines at end of document if any
2460 * and adjust document height. */
2461 while (document
->height
&& !document
->data
[document
->height
- 1].length
)
2462 mem_free_if(document
->data
[--document
->height
].chars
);
2464 /* Calculate document width. */
2468 document
->width
= 0;
2469 for (i
= 0; i
< document
->height
; i
++)
2470 int_lower_bound(&document
->width
, document
->data
[i
].length
);
2474 document
->options
.needs_width
= 1;
2476 /* FIXME: This needs more tuning since if we are centering stuff it
2478 document
->options
.needs_width
=
2479 (document
->width
+ (document
->options
.margin
2480 >= document
->options
.width
));
2483 document
->color
.background
= par_format
.color
.background
;
2485 done_html_parser(html_context
);
2487 /* Drop forms which has been serving as a placeholder for form items
2488 * added in the wrong order due to the ordering of table rendering. */
2492 foreach (form
, document
->forms
) {
2496 if (list_empty(form
->items
))
2503 /* @part was residing in html_context so it has to stay alive until
2504 * done_html_parser(). */
2508 #if 0 /* debug purpose */
2510 FILE *f
= fopen("forms", "ab");
2511 struct form_control
*form
;
2513 fprintf(f
,"FORM:\n");
2514 foreach (form
, document
->forms
) {
2515 fprintf(f
, "g=%d f=%d c=%d t:%d\n",
2516 form
->g_ctrl_num
, form
->form_num
,
2517 form
->ctrl_num
, form
->type
);
2519 fprintf(f
,"fragment: \n");
2520 for (qq
= start
; qq
< end
; qq
++) fprintf(f
, "%c", *qq
);
2521 fprintf(f
,"----------\n\n");