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 /* Make sure the combined character is not considered as
577 part
->spaces
[x
- 1] = 0;
579 if (prev
!= UCS_NO_CHAR
)
580 document
->data
[document
->comb_y
]
581 .chars
[document
->comb_x
].data
= prev
;
583 document
->combi_length
= 0;
585 document
->combi
[0] = data
;
587 if (document
->combi_length
< (UCS_MAX_LENGTH_COMBINED
- 1)) {
588 document
->combi
[++document
->combi_length
] = data
;
593 part
->spaces
[x
] = (data
== UCS_SPACE
);
595 if (unicode_to_cell(data
) == 2) {
596 schar
->data
= (unicode_val_T
)data
;
597 part
->char_width
[x
] = 2;
598 copy_screen_chars(&POS(x
++, y
), schar
, 1);
599 schar
->data
= UCS_NO_CHAR
;
601 part
->char_width
[x
] = 0;
603 part
->char_width
[x
] = unicode_to_cell(data
);
604 schar
->data
= (unicode_val_T
)data
;
606 #ifdef CONFIG_COMBINE
607 document
->comb_x
= X(x
);
608 document
->comb_y
= Y(y
);
609 assert_comb_x_y_ok(document
);
610 if_assert_failed
discard_comb_x_y(document
);
612 copy_screen_chars(&POS(x
++, y
), schar
, 1);
613 } /* while chars < end */
614 } else { /* not UTF-8 */
615 for (; charslen
> 0; charslen
--, x
++, chars
++) {
616 part
->char_width
[x
] = 1;
617 if (*chars
== NBSP_CHAR
) {
619 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
621 part
->spaces
[x
] = (*chars
== ' ');
622 schar
->data
= *chars
;
624 copy_screen_chars(&POS(x
, y
), schar
, 1);
626 } /* end of UTF-8 check */
628 /* Assert that we haven't written past the end of the
629 * LINE(y).chars array. @x here is one greater than
630 * the last one used in POS(x, y). Instead of this,
631 * we could assert(X(x) < LINE(y).length) immediately
632 * before each @copy_screen_chars call above, but
633 * those are in an inner loop that should be fast. */
634 assert(X(x
) <= LINE(y
).length
);
635 /* Some part of the code is apparently using LINE(y).length
636 * for line-wrapping decisions. It may currently be too
637 * large because it was allocated above based on @charslen
638 * which is the number of bytes, not the number of cells.
639 * Change the length to the correct size, but don't let it
640 * get smaller than it was on entry to this function. */
641 assert_comb_x_y_ok(document
);
642 LINE(y
).length
= int_max(orig_length
, X(x
));
643 assert_comb_x_y_ok(document
);
644 if_assert_failed
discard_comb_x_y(document
);
646 } else { /* part->document == NULL */
648 unsigned char *const end
= chars
+ charslen
;
650 while (chars
< end
) {
653 data
= utf8_to_unicode(&chars
, end
);
654 if (data
== UCS_SOFT_HYPHEN
)
657 if (data
== UCS_NO_BREAK_SPACE
658 && html_context
->options
->wrap_nbsp
)
660 part
->spaces
[x
] = (data
== UCS_SPACE
);
662 part
->char_width
[x
] = unicode_to_cell(data
);
663 if (part
->char_width
[x
] == 2) {
666 part
->char_width
[x
] = 0;
668 if (data
== UCS_NO_CHAR
) {
669 /* this is at the end only */
673 } /* while chars < end */
675 } else { /* not UTF-8 */
676 for (; charslen
> 0; charslen
--, x
++, chars
++) {
677 part
->char_width
[x
] = 1;
678 if (*chars
== NBSP_CHAR
) {
679 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
681 part
->spaces
[x
] = (*chars
== ' ');
685 } /* end of part->document check */
690 /* First possibly do the format change and then find out what coordinates
691 * to use since sub- or superscript might change them */
693 set_hline(struct html_context
*html_context
, unsigned char *chars
, int charslen
,
694 enum link_state link_state
)
696 struct part
*part
= html_context
->part
;
697 struct screen_char
*schar
= get_format_screen_char(html_context
,
703 if_assert_failed
return;
705 if (realloc_spaces(part
, x
+ charslen
))
708 if (part
->document
) {
709 if (realloc_line(html_context
, part
->document
,
710 Y(y
), X(x
) + charslen
- 1) < 0)
713 for (; charslen
> 0; charslen
--, x
++, chars
++) {
714 if (*chars
== NBSP_CHAR
) {
716 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
718 part
->spaces
[x
] = (*chars
== ' ');
719 schar
->data
= *chars
;
721 copy_screen_chars(&POS(x
, y
), schar
, 1);
724 for (; charslen
> 0; charslen
--, x
++, chars
++) {
725 if (*chars
== NBSP_CHAR
) {
726 part
->spaces
[x
] = html_context
->options
->wrap_nbsp
;
728 part
->spaces
[x
] = (*chars
== ' ');
733 #endif /* CONFIG_UTF8 */
736 move_links(struct html_context
*html_context
, int xf
, int yf
, int xt
, int yt
)
740 int nlink
= renderer_context
.last_link_to_move
;
743 assert(html_context
);
744 if_assert_failed
return;
746 part
= html_context
->part
;
748 assert(part
&& part
->document
);
749 if_assert_failed
return;
751 if (!realloc_lines(part
->document
, Y(yt
)))
754 for (; nlink
< part
->document
->nlinks
; nlink
++) {
755 struct link
*link
= &part
->document
->links
[nlink
];
758 for (i
= 0; i
< link
->npoints
; i
++) {
759 /* Fix for bug 479 (part one) */
760 /* The scenario that triggered it:
762 * Imagine a centered element containing a really long
763 * word (over half of the screen width long) followed
764 * by a few links with no spaces between them where all
765 * the link text combined with the really long word
766 * will force the line to be wrapped. When rendering
767 * the line first words (including link text words) are
768 * put on one line. Then wrapping is performed moving
769 * all links from current line to the one below. Then
770 * the current line (now only containing the really
771 * long word) is centered. This will trigger a call to
772 * move_links() which will increment.
774 * Without the fix below the centering of the current
775 * line will increment last_link_to_move to that of the
776 * last link which means centering of the next line
777 * with all the links will only move the last link
778 * leaving all the other links' points dangling and
779 * causing buggy link highlighting.
781 * Even links like textareas will be correctly handled
782 * because @last_link_to_move is a way to optimize how
783 * many links move_links() will have to iterate and
784 * this little fix will only decrease the effect of the
785 * optimization by always ensuring it is never
786 * incremented too far. */
787 if (!matched
&& link
->points
[i
].y
> Y(yf
)) {
792 if (link
->points
[i
].y
!= Y(yf
))
797 if (link
->points
[i
].x
< X(xf
))
801 link
->points
[i
].y
= Y(yt
);
802 link
->points
[i
].x
+= -xf
+ xt
;
804 int to_move
= link
->npoints
- (i
+ 1);
806 assert(to_move
>= 0);
809 memmove(&link
->points
[i
],
810 &link
->points
[i
+ 1],
812 sizeof(*link
->points
));
821 renderer_context
.last_link_to_move
= nlink
;
825 /* Don't move tags when removing links. */
829 tag
= renderer_context
.last_tag_to_move
;
831 while (list_has_next(part
->document
->tags
, tag
)) {
834 if (tag
->y
== Y(yf
)) {
836 if (tag
->x
>= X(xf
)) {
841 } else if (!matched
&& tag
->y
> Y(yf
)) {
842 /* Fix for bug 479 (part two) */
846 if (!matched
) renderer_context
.last_tag_to_move
= tag
;
850 /* This function does not update document.comb_x and document.comb_y.
851 * That is the caller's responsibility. */
853 copy_chars(struct html_context
*html_context
, int x
, int y
, int width
, struct screen_char
*d
)
857 assert(html_context
);
858 if_assert_failed
return;
860 part
= html_context
->part
;
862 assert(width
> 0 && part
&& part
->document
&& part
->document
->data
);
863 if_assert_failed
return;
865 if (realloc_line(html_context
, part
->document
, Y(y
), X(x
) + width
- 1) < 0)
868 copy_screen_chars(&POS(x
, y
), d
, width
);
872 move_chars(struct html_context
*html_context
, int x
, int y
, int nx
, int ny
)
876 assert(html_context
);
877 if_assert_failed
return;
879 part
= html_context
->part
;
881 assert(part
&& part
->document
&& part
->document
->data
);
882 if_assert_failed
return;
884 if (LEN(y
) - x
<= 0) return;
885 copy_chars(html_context
, nx
, ny
, LEN(y
) - x
, &POS(x
, y
));
887 assert_comb_x_y_ok(part
->document
);
888 move_comb_x_y(part
, x
, y
, nx
, ny
);
889 LINE(y
).length
= X(x
);
890 assert_comb_x_y_ok(part
->document
);
891 if_assert_failed
discard_comb_x_y(part
->document
);
892 move_links(html_context
, x
, y
, nx
, ny
);
895 /** Shift the line @a y to the right by @a shift character cells,
896 * and update document.comb_x and document.comb_y. */
898 shift_chars(struct html_context
*html_context
, int y
, int shift
)
901 struct screen_char
*a
;
904 assert(html_context
);
905 if_assert_failed
return;
907 part
= html_context
->part
;
909 assert(part
&& part
->document
&& part
->document
->data
);
910 if_assert_failed
return;
914 a
= fmem_alloc(len
* sizeof(*a
));
917 copy_screen_chars(a
, &POS(0, y
), len
);
919 assert_comb_x_y_ok(part
->document
);
920 if_assert_failed
discard_comb_x_y(part
->document
);
922 clear_hchars(html_context
, 0, y
, shift
, a
);
923 copy_chars(html_context
, shift
, y
, len
, a
);
926 move_links(html_context
, 0, y
, shift
, y
);
927 move_comb_x_y(part
, 0, y
, shift
, y
);
929 assert_comb_x_y_ok(part
->document
);
930 if_assert_failed
discard_comb_x_y(part
->document
);
934 del_chars(struct html_context
*html_context
, int x
, int y
)
938 assert(html_context
);
939 if_assert_failed
return;
941 part
= html_context
->part
;
943 assert(part
&& part
->document
&& part
->document
->data
);
944 if_assert_failed
return;
946 assert_comb_x_y_ok(part
->document
);
947 if_assert_failed
discard_comb_x_y(part
->document
);
949 LINE(y
).length
= X(x
);
950 move_comb_x_y(part
, x
, y
, -1, -1);
951 move_links(html_context
, x
, y
, -1, -1);
953 assert_comb_x_y_ok(part
->document
);
954 if_assert_failed
discard_comb_x_y(part
->document
);
957 #if TABLE_LINE_PADDING < 0
958 # define overlap_width(x) (x).width
960 # define overlap_width(x) int_min((x).width, \
961 html_context->options->box.width - TABLE_LINE_PADDING)
963 #define overlap(x) int_max(overlap_width(x) - (x).rightmargin, 0)
966 split_line_at(struct html_context
*html_context
, int width
)
970 int new_width
= width
+ par_format
.rightmargin
;
972 assert(html_context
);
973 if_assert_failed
return 0;
975 part
= html_context
->part
;
978 if_assert_failed
return 0;
980 /* Make sure that we count the right margin to the total
981 * actual box width. */
982 int_lower_bound(&part
->box
.width
, new_width
);
984 if (part
->document
) {
985 assert(part
->document
->data
);
986 if_assert_failed
return 0;
988 if (html_context
->options
->utf8
989 && width
< part
->spaces_len
&& part
->char_width
[width
] == 2) {
990 move_chars(html_context
, width
, part
->cy
, par_format
.leftmargin
, part
->cy
+ 1);
991 del_chars(html_context
, width
, part
->cy
);
995 assertm(POS(width
, part
->cy
).data
== ' ',
996 "bad split: %c", POS(width
, part
->cy
).data
);
997 move_chars(html_context
, width
+ 1, part
->cy
, par_format
.leftmargin
, part
->cy
+ 1);
998 del_chars(html_context
, width
, part
->cy
);
1004 if (!(html_context
->options
->utf8
1005 && width
< part
->spaces_len
1006 && part
->char_width
[width
] == 2))
1008 width
++; /* Since we were using (x + 1) only later... */
1010 tmp
= part
->spaces_len
- width
;
1012 /* 0 is possible and I'm paranoid ... --Zas */
1013 memmove(part
->spaces
, part
->spaces
+ width
, tmp
);
1015 memmove(part
->char_width
, part
->char_width
+ width
, tmp
);
1020 if_assert_failed tmp
= 0;
1021 memset(part
->spaces
+ tmp
, 0, width
);
1023 memset(part
->char_width
+ tmp
, 0, width
);
1026 if (par_format
.leftmargin
> 0) {
1027 tmp
= part
->spaces_len
- par_format
.leftmargin
;
1028 assertm(tmp
> 0, "part->spaces_len - par_format.leftmargin == %d", tmp
);
1029 /* So tmp is zero, memmove() should survive that. Don't recover. */
1030 memmove(part
->spaces
+ par_format
.leftmargin
, part
->spaces
, tmp
);
1032 memmove(part
->char_width
+ par_format
.leftmargin
, part
->char_width
, tmp
);
1038 if (part
->cx
== width
) {
1040 int_lower_bound(&part
->box
.height
, part
->cy
);
1043 part
->cx
-= width
- par_format
.leftmargin
;
1044 int_lower_bound(&part
->box
.height
, part
->cy
+ 1);
1049 /* Here, we scan the line for a possible place where we could split it into two
1050 * (breaking it, because it is too long), if it is overlapping from the maximal
1052 /* Returns 0 if there was found no spot suitable for breaking the line.
1053 * 1 if the line was split into two.
1054 * 2 if the (second) splitted line is blank (that is useful to determine
1055 * ie. if the next line_break() should really break the line; we don't
1056 * want to see any blank lines to pop up, do we?). */
1058 split_line(struct html_context
*html_context
)
1063 assert(html_context
);
1064 if_assert_failed
return 0;
1066 part
= html_context
->part
;
1069 if_assert_failed
return 0;
1072 if (html_context
->options
->utf8
) {
1073 for (x
= overlap(par_format
); x
>= par_format
.leftmargin
; x
--) {
1075 if (x
< part
->spaces_len
&& (part
->spaces
[x
]
1076 || (part
->char_width
[x
] == 2
1077 /* Ugly hack. If we haven't place for
1078 * double-width characters we print two
1079 * double-width characters. */
1080 && x
!= par_format
.leftmargin
)))
1081 return split_line_at(html_context
, x
);
1084 for (x
= par_format
.leftmargin
; x
< part
->cx
; x
++) {
1085 if (x
< part
->spaces_len
&& (part
->spaces
[x
]
1086 || (part
->char_width
[x
] == 2
1087 /* We want to break line after _second_
1088 * double-width character. */
1089 && x
> par_format
.leftmargin
)))
1090 return split_line_at(html_context
, x
);
1095 for (x
= overlap(par_format
); x
>= par_format
.leftmargin
; x
--)
1096 if (x
< part
->spaces_len
&& part
->spaces
[x
])
1097 return split_line_at(html_context
, x
);
1099 for (x
= par_format
.leftmargin
; x
< part
->cx
; x
++)
1100 if (x
< part
->spaces_len
&& part
->spaces
[x
])
1101 return split_line_at(html_context
, x
);
1104 /* Make sure that we count the right margin to the total
1105 * actual box width. */
1106 int_lower_bound(&part
->box
.width
, part
->cx
+ par_format
.rightmargin
);
1111 /* Insert @new_spaces spaces before the coordinates @x and @y,
1112 * adding those spaces to whatever link is at those coordinates. */
1113 /* TODO: Integrate with move_links. */
1115 insert_spaces_in_link(struct part
*part
, int x
, int y
, int new_spaces
)
1117 int i
= part
->document
->nlinks
;
1123 struct link
*link
= &part
->document
->links
[i
];
1124 int j
= link
->npoints
;
1127 struct point
*point
= &link
->points
[j
];
1129 if (point
->x
!= x
|| point
->y
!= y
)
1132 if (!realloc_points(link
, link
->npoints
+ new_spaces
))
1135 link
->npoints
+= new_spaces
;
1136 point
= &link
->points
[link
->npoints
- 1];
1138 while (new_spaces
--) {
1149 /* This function is very rare exemplary of clean and beautyful code here.
1150 * Please handle with care. --pasky */
1152 justify_line(struct html_context
*html_context
, int y
)
1155 struct screen_char
*line
; /* we save original line here */
1162 assert(html_context
);
1163 if_assert_failed
return;
1165 part
= html_context
->part
;
1167 assert(part
&& part
->document
&& part
->document
->data
);
1168 if_assert_failed
return;
1172 if_assert_failed
return;
1174 line
= fmem_alloc(len
* sizeof(*line
));
1177 /* It may sometimes happen that the line is only one char long and that
1178 * char is space - then we're going to write to both [0] and [1], but
1179 * we allocated only one field. Thus, we've to do (len + 1). --pasky */
1180 space_list
= fmem_alloc((len
+ 1) * sizeof(*space_list
));
1186 copy_screen_chars(line
, &POS(0, y
), len
);
1188 /* Skip leading spaces */
1193 while (line
[pos
].data
== ' ')
1196 /* Yes, this can be negative, we know. But we add one to it always
1197 * anyway, so it's ok. */
1198 space_list
[spaces
++] = pos
- 1;
1202 for (; pos
< len
; pos
++)
1203 if (line
[pos
].data
== ' ')
1204 space_list
[spaces
++] = pos
;
1206 space_list
[spaces
] = len
;
1210 /* Diff is the difference between the width of the paragraph
1211 * and the current length of the line. */
1212 diff
= overlap(par_format
) - len
;
1214 /* We check diff > 0 because diff can be negative (i.e., we have
1215 * an unbroken line of length > overlap(par_format))
1216 * even when spaces > 1 if the line has only non-breaking spaces. */
1217 if (spaces
> 1 && diff
> 0) {
1221 /* Allocate enough memory for the justified line.
1222 * If the memory is not available, then leave the
1223 * line unchanged, rather than halfway there. The
1224 * following loop assumes the allocation succeeded. */
1225 if (!realloc_line(html_context
, html_context
->part
->document
,
1226 Y(y
), X(overlap(par_format
))))
1229 for (word
= 0; word
< spaces
; word
++) {
1230 /* We have to increase line length by 'diff' num. of
1231 * characters, so we move 'word'th word 'word_shift'
1232 * characters right. */
1233 int word_start
= space_list
[word
] + 1;
1234 int word_len
= space_list
[word
+ 1] - word_start
;
1239 assert(word_len
>= 0);
1240 if_assert_failed
continue;
1242 word_shift
= (word
* diff
) / (spaces
- 1);
1243 new_start
= word_start
+ word_shift
;
1245 /* Assert that the realloc_line() above
1246 * allocated enough memory for the word
1247 * and the preceding spaces. */
1248 assert(LEN(y
) >= new_start
+ word_len
);
1249 if_assert_failed
continue;
1251 /* Copy the original word, without any spaces.
1252 * word_len may be 0 here. */
1253 copy_screen_chars(&POS(new_start
, y
),
1254 &line
[word_start
], word_len
);
1256 /* Copy the space that preceded the word,
1257 * duplicating it as many times as necessary.
1258 * This preserves its attributes, such as
1259 * background color and underlining. If this
1260 * is the first word, then skip the copy
1261 * because there might not be a space there
1262 * and anyway it need not be duplicated. */
1266 for (spacex
= prev_end
; spacex
< new_start
;
1268 copy_screen_chars(&POS(spacex
, y
),
1269 &line
[word_start
- 1],
1274 /* Remember that any links at the right side
1275 * of the added spaces have moved, and the
1276 * spaces themselves may also belong to a
1278 new_spaces
= new_start
- prev_end
- 1;
1279 if (word
&& new_spaces
) {
1280 move_comb_x_y(part
, prev_end
+ 1, y
, new_start
, y
);
1281 move_links(html_context
, prev_end
+ 1, y
, new_start
, y
);
1282 insert_spaces_in_link(part
,
1283 new_start
, y
, new_spaces
);
1286 prev_end
= new_start
+ word_len
;
1291 fmem_free(space_list
);
1296 align_line(struct html_context
*html_context
, int y
, int last
)
1302 assert(html_context
);
1303 if_assert_failed
return;
1305 part
= html_context
->part
;
1307 assert(part
&& part
->document
&& part
->document
->data
);
1308 if_assert_failed
return;
1312 if (!len
|| par_format
.align
== ALIGN_LEFT
)
1315 if (par_format
.align
== ALIGN_JUSTIFY
) {
1317 justify_line(html_context
, y
);
1321 shift
= overlap(par_format
) - len
;
1322 if (par_format
.align
== ALIGN_CENTER
)
1325 shift_chars(html_context
, y
, shift
);
1329 init_link_event_hooks(struct html_context
*html_context
, struct link
*link
)
1331 link
->event_hooks
= mem_calloc(1, sizeof(*link
->event_hooks
));
1332 if (!link
->event_hooks
) return;
1334 #define add_evhook(list_, type_, src_) \
1336 struct script_event_hook *evhook; \
1340 evhook = mem_calloc(1, sizeof(*evhook)); \
1341 if (!evhook) break; \
1343 evhook->type = type_; \
1344 evhook->src = stracpy(src_); \
1345 add_to_list(*(list_), evhook); \
1348 init_list(*link
->event_hooks
);
1349 add_evhook(link
->event_hooks
, SEVHOOK_ONCLICK
, format
.onclick
);
1350 add_evhook(link
->event_hooks
, SEVHOOK_ONDBLCLICK
, format
.ondblclick
);
1351 add_evhook(link
->event_hooks
, SEVHOOK_ONMOUSEOVER
, format
.onmouseover
);
1352 add_evhook(link
->event_hooks
, SEVHOOK_ONHOVER
, format
.onhover
);
1353 add_evhook(link
->event_hooks
, SEVHOOK_ONFOCUS
, format
.onfocus
);
1354 add_evhook(link
->event_hooks
, SEVHOOK_ONMOUSEOUT
, format
.onmouseout
);
1355 add_evhook(link
->event_hooks
, SEVHOOK_ONBLUR
, format
.onblur
);
1360 static struct link
*
1361 new_link(struct html_context
*html_context
, unsigned char *name
, int namelen
)
1363 struct document
*document
;
1368 assert(html_context
);
1369 if_assert_failed
return NULL
;
1371 part
= html_context
->part
;
1374 if_assert_failed
return NULL
;
1376 document
= part
->document
;
1379 if_assert_failed
return NULL
;
1381 link_number
= part
->link_num
;
1383 if (!ALIGN_LINK(&document
->links
, document
->nlinks
, document
->nlinks
+ 1))
1386 link
= &document
->links
[document
->nlinks
++];
1387 link
->number
= link_number
- 1;
1388 if (document
->options
.use_tabindex
) link
->number
+= format
.tabindex
;
1389 link
->accesskey
= format
.accesskey
;
1390 link
->title
= null_or_stracpy(format
.title
);
1391 link
->where_img
= null_or_stracpy(format
.image
);
1394 link
->target
= null_or_stracpy(format
.target
);
1395 link
->data
.name
= memacpy(name
, namelen
);
1396 /* if (strlen(url) > 4 && !c_strncasecmp(url, "MAP@", 4)) { */
1398 && ((format
.link
[0]|32) == 'm')
1399 && ((format
.link
[1]|32) == 'a')
1400 && ((format
.link
[2]|32) == 'p')
1401 && (format
.link
[3] == '@')
1402 && format
.link
[4]) {
1403 link
->type
= LINK_MAP
;
1404 link
->where
= stracpy(format
.link
+ 4);
1406 link
->type
= LINK_HYPERTEXT
;
1407 link
->where
= null_or_stracpy(format
.link
);
1411 struct form_control
*fc
= format
.form
;
1418 link
->type
= LINK_FIELD
;
1421 link
->type
= LINK_AREA
;
1425 link
->type
= LINK_CHECKBOX
;
1428 link
->type
= LINK_SELECT
;
1435 link
->type
= LINK_BUTTON
;
1437 link
->data
.form_control
= fc
;
1438 /* At this point, format.form might already be set but
1439 * the form_control not registered through SP_CONTROL
1440 * yet, therefore without fc->form set. It is always
1441 * after the "good" last form was already processed,
1442 * though, so we can safely just take that. */
1444 if (!form
&& !list_empty(document
->forms
))
1445 form
= document
->forms
.next
;
1446 link
->target
= null_or_stracpy(form
? form
->target
: NULL
);
1449 link
->color
.background
= format
.style
.color
.background
;
1450 link
->color
.foreground
= link_is_textinput(link
)
1451 ? format
.style
.color
.foreground
1452 : format
.color
.clink
;
1454 init_link_event_hooks(html_context
, link
);
1456 document
->links_sorted
= 0;
1461 html_special_tag(struct document
*document
, unsigned char *t
, int x
, int y
)
1467 if_assert_failed
return;
1469 tag_len
= strlen(t
);
1470 /* One byte is reserved for name in struct tag. */
1471 tag
= mem_alloc(sizeof(*tag
) + tag_len
);
1476 memcpy(tag
->name
, t
, tag_len
+ 1);
1477 add_to_list(document
->tags
, tag
);
1478 if (renderer_context
.last_tag_for_newline
== (struct tag
*) &document
->tags
)
1479 renderer_context
.last_tag_for_newline
= tag
;
1484 put_chars_conv(struct html_context
*html_context
,
1485 unsigned char *chars
, int charslen
)
1489 assert(html_context
);
1490 if_assert_failed
return;
1492 part
= html_context
->part
;
1494 assert(part
&& chars
&& charslen
);
1495 if_assert_failed
return;
1497 if (format
.style
.attr
& AT_GRAPHICS
) {
1498 put_chars(html_context
, chars
, charslen
);
1502 convert_string(renderer_context
.convert_table
, chars
, charslen
,
1503 html_context
->options
->cp
,
1504 (format
.style
.attr
& AT_NO_ENTITIES
) ? CSM_NONE
: CSM_DEFAULT
,
1505 NULL
, (void (*)(void *, unsigned char *, int)) put_chars
, html_context
);
1509 * Converts a number in base 10 to a string in another base whose symbols are
1510 * represented by key. I the trivial case, key="0123456789". A more homerow
1511 * friendly key="gfdsahjkl;trewqyuiopvcxznm". Returns the length of link_sym.
1514 dec2qwerty(int num
, unsigned char *link_sym
, const unsigned char *key
, int base
)
1518 if (base
< 2) return 0;
1520 for (newlen
= 1, pow
= base
; pow
<= num
; ++newlen
, pow
*= base
);
1522 link_sym
[newlen
] = '\0';
1523 for (i
= 1; i
<= newlen
; ++i
) {
1524 int key_index
= num
% base
;
1525 link_sym
[newlen
- i
] = key
[key_index
];
1532 * Returns the value of link_sym in decimal according to key.
1535 qwerty2dec(const unsigned char *link_sym
, const unsigned char *key
, int base
)
1538 int symlen
= strlen(link_sym
);
1542 for (i
= 0, pow
= 1; i
< symlen
; ++i
, pow
*= base
) {
1544 while (key
[j
] != link_sym
[symlen
- 1 - i
]) ++j
;
1551 put_link_number(struct html_context
*html_context
)
1553 char *symkey
= get_opt_str("document.browse.links.label_key", NULL
);
1554 struct part
*part
= html_context
->part
;
1555 unsigned char s
[64];
1556 unsigned char *fl
= format
.link
;
1557 unsigned char *ft
= format
.target
;
1558 unsigned char *fi
= format
.image
;
1559 struct form_control
*ff
= format
.form
;
1561 int base
= strlen(symkey
);
1563 format
.link
= format
.target
= format
.image
= NULL
;
1567 slen
+= dec2qwerty(part
->link_num
, s
+ 1, symkey
, base
);
1571 renderer_context
.nosearchable
= 1;
1572 put_chars(html_context
, s
, slen
);
1573 renderer_context
.nosearchable
= 0;
1575 if (ff
&& ff
->type
== FC_TEXTAREA
) line_break(html_context
);
1577 /* We might have ended up on a new line after the line breaking
1578 * or putting the link number chars. */
1579 if (part
->cx
== -1) part
->cx
= par_format
.leftmargin
;
1587 #define assert_link_variable(old, new) \
1588 assertm(!(old), "Old link value [%s]. New value [%s]", old, new);
1591 init_link_state_info(unsigned char *link
, unsigned char *target
,
1592 unsigned char *image
, struct form_control
*form
)
1594 assert_link_variable(renderer_context
.link_state_info
.image
, image
);
1595 assert_link_variable(renderer_context
.link_state_info
.target
, target
);
1596 assert_link_variable(renderer_context
.link_state_info
.link
, link
);
1598 renderer_context
.link_state_info
.link
= null_or_stracpy(link
);
1599 renderer_context
.link_state_info
.target
= null_or_stracpy(target
);
1600 renderer_context
.link_state_info
.image
= null_or_stracpy(image
);
1601 renderer_context
.link_state_info
.form
= form
;
1605 done_link_state_info(void)
1607 mem_free_if(renderer_context
.link_state_info
.link
);
1608 mem_free_if(renderer_context
.link_state_info
.target
);
1609 mem_free_if(renderer_context
.link_state_info
.image
);
1610 memset(&renderer_context
.link_state_info
, 0,
1611 sizeof(renderer_context
.link_state_info
));
1616 process_link(struct html_context
*html_context
, enum link_state link_state
,
1617 unsigned char *chars
, int charslen
, int cells
)
1620 process_link(struct html_context
*html_context
, enum link_state link_state
,
1621 unsigned char *chars
, int charslen
)
1622 #endif /* CONFIG_UTF8 */
1624 struct part
*part
= html_context
->part
;
1628 switch (link_state
) {
1629 case LINK_STATE_SAME
: {
1630 unsigned char *name
;
1632 if (!part
->document
) return;
1634 assertm(part
->document
->nlinks
> 0, "no link");
1635 if_assert_failed
return;
1637 link
= &part
->document
->links
[part
->document
->nlinks
- 1];
1639 name
= get_link_name(link
);
1641 unsigned char *new_name
;
1643 new_name
= straconcat(name
, chars
,
1644 (unsigned char *) NULL
);
1647 link
->data
.name
= new_name
;
1651 /* FIXME: Concatenating two adjectent <a> elements to a single
1652 * link is broken since we lose the event handlers for the
1653 * second one. OTOH simply appending them here won't fly since
1654 * we may get here multiple times for even a single link. We
1655 * will probably need some SP_ for creating a new link or so.
1661 case LINK_STATE_NEW
:
1664 init_link_state_info(format
.link
, format
.target
,
1665 format
.image
, format
.form
);
1666 if (!part
->document
) return;
1668 /* Trim leading space from the link text */
1669 while (x_offset
< charslen
&& chars
[x_offset
] <= ' ')
1673 charslen
-= x_offset
;
1677 #endif /* CONFIG_UTF8 */
1680 link
= new_link(html_context
, chars
, charslen
);
1685 case LINK_STATE_NONE
:
1687 INTERNAL("bad link_state %i", (int) link_state
);
1691 /* Add new canvas positions to the link. */
1693 if (realloc_points(link
, link
->npoints
+ cells
))
1695 if (realloc_points(link
, link
->npoints
+ charslen
))
1696 #endif /* CONFIG_UTF8 */
1698 struct point
*point
= &link
->points
[link
->npoints
];
1699 int x
= X(part
->cx
) + x_offset
;
1700 int y
= Y(part
->cy
);
1703 link
->npoints
+= cells
;
1705 for (; cells
> 0; cells
--, point
++, x
++)
1707 link
->npoints
+= charslen
;
1709 for (; charslen
> 0; charslen
--, point
++, x
++)
1710 #endif /* CONFIG_UTF8 */
1718 static inline enum link_state
1719 get_link_state(struct html_context
*html_context
)
1721 enum link_state state
;
1723 if (!(format
.link
|| format
.image
|| format
.form
)) {
1724 state
= LINK_STATE_NONE
;
1726 } else if ((renderer_context
.link_state_info
.link
1727 || renderer_context
.link_state_info
.image
1728 || renderer_context
.link_state_info
.form
)
1729 && !xstrcmp(format
.link
, renderer_context
.link_state_info
.link
)
1730 && !xstrcmp(format
.target
, renderer_context
.link_state_info
.target
)
1731 && !xstrcmp(format
.image
, renderer_context
.link_state_info
.image
)
1732 && format
.form
== renderer_context
.link_state_info
.form
) {
1734 return LINK_STATE_SAME
;
1737 state
= LINK_STATE_NEW
;
1740 done_link_state_info();
1746 html_has_non_space_chars(unsigned char *chars
, int charslen
)
1750 while (pos
< charslen
)
1751 if (!isspace(chars
[pos
++]))
1758 put_chars(struct html_context
*html_context
, unsigned char *chars
, int charslen
)
1760 enum link_state link_state
;
1764 #endif /* CONFIG_UTF8 */
1766 assert(html_context
);
1767 if_assert_failed
return;
1769 part
= html_context
->part
;
1772 if_assert_failed
return;
1774 assert(chars
&& charslen
);
1775 if_assert_failed
return;
1777 /* If we are not handling verbatim aligning and we are at the begining
1778 * of a line trim whitespace. */
1779 if (part
->cx
== -1) {
1780 /* If we are not handling verbatim aligning trim leading
1782 if (!html_is_preformatted()) {
1783 while (charslen
&& *chars
== ' ') {
1788 if (charslen
< 1) return;
1791 part
->cx
= par_format
.leftmargin
;
1794 /* For preformatted html always update 'the last tag' so we never end
1795 * up moving tags to the wrong line (Fixes bug 324). For all other html
1796 * it is moved only when the line being rendered carry some real
1797 * non-whitespace content. */
1798 if (html_is_preformatted()
1799 || html_has_non_space_chars(chars
, charslen
)) {
1800 renderer_context
.last_tag_for_newline
= (struct tag
*) &part
->document
->tags
;
1803 int_lower_bound(&part
->box
.height
, part
->cy
+ 1);
1805 link_state
= get_link_state(html_context
);
1807 if (link_state
== LINK_STATE_NEW
) {
1810 /* Don't add inaccessible links. It seems to be caused
1811 * by the parser putting a space char after stuff like
1812 * <img>-tags or comments wrapped in <a>-tags. See bug
1813 * 30 for test case. */
1814 while (x_offset
< charslen
&& chars
[x_offset
] <= ' ')
1817 /* For pure spaces reset the link state */
1818 if (x_offset
== charslen
)
1819 link_state
= LINK_STATE_NONE
;
1820 else if (html_context
->options
->links_numbering
)
1821 put_link_number(html_context
);
1825 #endif /* CONFIG_UTF8 */
1826 set_hline(html_context
, chars
, charslen
, link_state
);
1828 if (link_state
!= LINK_STATE_NONE
) {
1830 process_link(html_context
, link_state
, chars
, charslen
,
1833 process_link(html_context
, link_state
, chars
, charslen
);
1834 #endif /* CONFIG_UTF8 */
1838 if (renderer_context
.nowrap
1839 && part
->cx
+ cells
> overlap(par_format
))
1844 if (renderer_context
.nowrap
1845 && part
->cx
+ charslen
> overlap(par_format
))
1848 part
->cx
+= charslen
;
1849 #endif /* CONFIG_UTF8 */
1851 renderer_context
.nobreak
= 0;
1853 if (!(html_context
->options
->wrap
|| html_is_preformatted())) {
1854 while (part
->cx
> overlap(par_format
)
1855 && part
->cx
> par_format
.leftmargin
) {
1856 int x
= split_line(html_context
);
1860 align_line(html_context
, part
->cy
- 1, 0);
1861 renderer_context
.nobreak
= !!(x
- 1);
1865 assert(charslen
> 0);
1869 part
->xa
+= charslen
;
1870 #endif /* CONFIG_UTF8 */
1871 int_lower_bound(&part
->max_width
, part
->xa
1872 + par_format
.leftmargin
+ par_format
.rightmargin
1873 - (chars
[charslen
- 1] == ' '
1874 && !html_is_preformatted()));
1882 line_break(struct html_context
*html_context
)
1887 assert(html_context
);
1888 if_assert_failed
return;
1890 part
= html_context
->part
;
1893 if_assert_failed
return;
1895 int_lower_bound(&part
->box
.width
, part
->cx
+ par_format
.rightmargin
);
1897 if (renderer_context
.nobreak
) {
1898 renderer_context
.nobreak
= 0;
1904 if (!part
->document
|| !part
->document
->data
) goto end
;
1906 if (!realloc_lines(part
->document
, part
->box
.height
+ part
->cy
+ 1))
1909 if (part
->cx
> par_format
.leftmargin
&& LEN(part
->cy
) > part
->cx
- 1
1910 && POS(part
->cx
- 1, part
->cy
).data
== ' ') {
1911 del_chars(html_context
, part
->cx
- 1, part
->cy
);
1915 if (part
->cx
> 0) align_line(html_context
, part
->cy
, 1);
1917 for (tag
= renderer_context
.last_tag_for_newline
;
1918 tag
&& tag
!= (struct tag
*) &part
->document
->tags
;
1921 tag
->y
= Y(part
->cy
+ 1);
1928 memset(part
->spaces
, 0, part
->spaces_len
);
1930 memset(part
->char_width
, 0, part
->spaces_len
);
1935 html_special_form(struct part
*part
, struct form
*form
)
1939 assert(part
&& form
);
1940 assert(form
->form_num
> 0);
1941 assert(form
->form_end
== INT_MAX
);
1942 if_assert_failed
return;
1944 if (!part
->document
) {
1949 /* Make a fake form with form_num == 0 so that there is
1950 * something to use if form controls appear above the first
1951 * actual FORM element. There can never be a real form with
1952 * form_num == 0 because the form_num is the position after the
1953 * "<form" characters and that's already five characters. The
1954 * fake form does not have a name, and it gets a form_view and
1955 * becomes visible to ECMAScript only if it actually has
1956 * controls in it. */
1957 if (list_empty(part
->document
->forms
)) {
1958 nform
= init_form();
1963 nform
->form_num
= 0;
1964 add_to_list(part
->document
->forms
, nform
);
1967 /* Make sure the new form ``claims'' its slice of the form range
1968 * maintained in the form_num and form_end variables. */
1969 foreach (nform
, part
->document
->forms
) {
1970 if (form
->form_num
< nform
->form_num
1971 || nform
->form_end
< form
->form_num
)
1974 /* First check if the form has identical form numbers.
1975 * That should only be the case when the form being
1976 * added is in fact the same form in which case it
1977 * should be dropped. The fact that this can happen
1978 * suggests that the table renderering can be confused.
1979 * See bug 647 for a test case.
1980 * Do not compare form->form_end here because it is
1981 * normally set by this function and that has obviously
1982 * not yet been done. */
1983 if (nform
->form_num
== form
->form_num
) {
1988 /* The form start is inside an already added form, so
1989 * partition the space of the existing form and get
1991 form
->form_end
= nform
->form_end
;
1992 nform
->form_end
= form
->form_num
- 1;
1993 assertm(nform
->form_num
<= nform
->form_end
,
1994 "[%d:%d] [%d:%d]", nform
->form_num
, nform
->form_end
,
1995 form
->form_num
, form
->form_end
);
1996 add_to_list(part
->document
->forms
, form
);
2000 ERROR("hole between forms");
2006 html_special_form_control(struct part
*part
, struct form_control
*fc
)
2011 if_assert_failed
return;
2013 if (!part
->document
) {
2014 done_form_control(fc
);
2019 fc
->g_ctrl_num
= renderer_context
.g_ctrl_num
++;
2021 if (list_empty(part
->document
->forms
)) {
2022 /* No forms encountered yet, that means a homeless form
2023 * control. Generate a dummy form for those Flying
2027 add_to_list(part
->document
->forms
, form
);
2029 /* Attach this form control to the last form encountered. */
2030 form
= part
->document
->forms
.next
;
2032 add_to_list(form
->items
, fc
);
2036 /** Assert that each form in the list has a different form.form_num
2037 * ... form.form_end range and that the ranges are contiguous and
2038 * together cover all numbers from 0 to INT_MAX. Alternatively, the
2039 * whole list may be empty. This function can be called from a
2040 * debugger, or automatically from some places.
2042 * This function may leave assert_failed = 1; the caller must use
2043 * if_assert_failed. */
2045 assert_forms_list_ok(LIST_OF(struct form
) *forms
)
2047 int saw_form_num_0
= 0;
2050 if (list_empty(*forms
)) return;
2052 /* O(n^2) algorithm, but it's only for debugging. */
2053 foreach (outer
, *forms
) {
2057 if (outer
->form_num
== 0)
2060 foreach (inner
, *forms
) {
2061 assert(inner
== outer
2062 || inner
->form_num
> outer
->form_end
2063 || outer
->form_num
> inner
->form_end
);
2064 if (outer
->form_end
== inner
->form_num
- 1)
2068 if (outer
->form_end
== INT_MAX
)
2069 assert(followers
== 0);
2071 assert(followers
== 1);
2074 assert(saw_form_num_0
== 1);
2076 #else /* !CONFIG_DEBUG */
2077 # define assert_forms_list_ok(forms) ((void) 0)
2078 #endif /* !CONFIG_DEBUG */
2080 /* Reparents form items based on position in the source. */
2082 check_html_form_hierarchy(struct part
*part
)
2084 struct document
*document
= part
->document
;
2085 INIT_LIST_OF(struct form_control
, form_controls
);
2087 struct form_control
*fc
, *next
;
2089 if (list_empty(document
->forms
))
2092 assert_forms_list_ok(&document
->forms
);
2095 /* Take out all badly placed form items. */
2097 foreach (form
, document
->forms
) {
2099 assertm(form
->form_num
<= form
->form_end
,
2100 "%p [%d : %d]", form
, form
->form_num
, form
->form_end
);
2102 foreachsafe (fc
, next
, form
->items
) {
2103 if (form
->form_num
<= fc
->position
2104 && fc
->position
<= form
->form_end
)
2107 move_to_top_of_list(form_controls
, fc
);
2111 /* Re-insert the form items the correct places. */
2113 foreachsafe (fc
, next
, form_controls
) {
2115 foreach (form
, document
->forms
) {
2116 if (fc
->position
< form
->form_num
2117 || form
->form_end
< fc
->position
)
2121 move_to_top_of_list(form
->items
, fc
);
2126 assert(list_empty(form_controls
));
2130 color_link_lines(struct html_context
*html_context
)
2132 struct document
*document
= html_context
->part
->document
;
2133 struct color_pair colors
= INIT_COLOR_PAIR(par_format
.color
.background
, 0x0);
2134 enum color_mode color_mode
= document
->options
.color_mode
;
2135 enum color_flags color_flags
= document
->options
.color_flags
;
2138 for (y
= 0; y
< document
->height
; y
++) {
2141 for (x
= 0; x
< document
->data
[y
].length
; x
++) {
2142 struct screen_char
*schar
= &document
->data
[y
].chars
[x
];
2144 set_term_color(schar
, &colors
, color_flags
, color_mode
);
2146 /* XXX: Entering hack zone! Change to clink color after
2147 * link text has been recolored. */
2148 if (schar
->data
== ':' && colors
.foreground
== 0x0)
2149 colors
.foreground
= format
.color
.clink
;
2152 colors
.foreground
= 0x0;
2157 html_special(struct html_context
*html_context
, enum html_special_type c
, ...)
2161 struct document
*document
;
2162 void *ret_val
= NULL
;
2164 assert(html_context
);
2165 if_assert_failed
return NULL
;
2167 part
= html_context
->part
;
2170 if_assert_failed
return NULL
;
2172 document
= part
->document
;
2178 unsigned char *t
= va_arg(l
, unsigned char *);
2180 html_special_tag(document
, t
, X(part
->cx
), Y(part
->cy
));
2185 struct form
*form
= va_arg(l
, struct form
*);
2187 html_special_form(part
, form
);
2192 struct form_control
*fc
= va_arg(l
, struct form_control
*);
2194 html_special_form_control(part
, fc
);
2198 ret_val
= renderer_context
.convert_table
;
2201 ret_val
= (void *) (long) !!document
;
2203 case SP_CACHE_CONTROL
:
2205 struct cache_entry
*cached
= renderer_context
.cached
;
2207 cached
->cache_mode
= CACHE_MODE_NEVER
;
2211 case SP_CACHE_EXPIRES
:
2213 time_t expires
= va_arg(l
, time_t);
2214 struct cache_entry
*cached
= renderer_context
.cached
;
2216 if (!expires
|| cached
->cache_mode
== CACHE_MODE_NEVER
)
2219 timeval_from_seconds(&cached
->max_age
, expires
);
2225 struct frameset_param
*fsp
= va_arg(l
, struct frameset_param
*);
2226 struct frameset_desc
*frameset_desc
;
2228 if (!fsp
->parent
&& document
->frame_desc
)
2231 frameset_desc
= create_frameset(fsp
);
2232 if (!fsp
->parent
&& !document
->frame_desc
)
2233 document
->frame_desc
= frameset_desc
;
2235 ret_val
= frameset_desc
;
2240 struct frameset_desc
*parent
= va_arg(l
, struct frameset_desc
*);
2241 unsigned char *name
= va_arg(l
, unsigned char *);
2242 unsigned char *url
= va_arg(l
, unsigned char *);
2244 add_frameset_entry(parent
, NULL
, name
, url
);
2248 renderer_context
.nowrap
= !!va_arg(l
, int);
2252 unsigned long seconds
= va_arg(l
, unsigned long);
2253 unsigned char *t
= va_arg(l
, unsigned char *);
2256 if (document
->refresh
)
2257 done_document_refresh(document
->refresh
);
2258 document
->refresh
= init_document_refresh(t
, seconds
);
2262 case SP_COLOR_LINK_LINES
:
2263 if (document
&& use_document_bg_colors(&document
->options
))
2264 color_link_lines(html_context
);
2269 struct uri
*uri
= va_arg(l
, struct uri
*);
2271 add_to_uri_list(&document
->css_imports
, uri
);
2276 #ifdef CONFIG_ECMASCRIPT
2278 struct uri
*uri
= va_arg(l
, struct uri
*);
2280 add_to_uri_list(&document
->ecmascript_imports
, uri
);
2292 free_table_cache(void)
2295 struct hash_item
*item
;
2298 /* We do not free key here. */
2299 foreach_hash_item (item
, *table_cache
, i
) {
2300 mem_free_if(item
->value
);
2303 free_hash(&table_cache
);
2304 table_cache_entries
= 0;
2309 format_html_part(struct html_context
*html_context
,
2310 unsigned char *start
, unsigned char *end
,
2311 int align
, int margin
, int width
, struct document
*document
,
2312 int x
, int y
, unsigned char *head
,
2317 struct tag
*saved_last_tag_to_move
= renderer_context
.last_tag_to_move
;
2318 int saved_empty_format
= renderer_context
.empty_format
;
2319 int saved_margin
= html_context
->margin
;
2320 int saved_last_link_to_move
= renderer_context
.last_link_to_move
;
2322 /* Hash creation if needed. */
2324 table_cache
= init_hash8();
2325 } else if (!document
) {
2326 /* Search for cached entry. */
2327 struct table_cache_entry_key key
;
2328 struct hash_item
*item
;
2330 /* Clear key to prevent potential alignment problem
2331 * when keys are compared. */
2332 memset(&key
, 0, sizeof(key
));
2337 key
.margin
= margin
;
2340 key
.link_num
= link_num
;
2342 item
= get_hash_item(table_cache
,
2343 (unsigned char *) &key
,
2345 if (item
) { /* We found it in cache, so just copy and return. */
2346 part
= mem_alloc(sizeof(*part
));
2348 copy_struct(part
, &((struct table_cache_entry
*)
2349 item
->value
)->part
);
2355 assertm(y
>= 0, "format_html_part: y == %d", y
);
2356 if_assert_failed
return NULL
;
2359 struct node
*node
= mem_alloc(sizeof(*node
));
2362 int node_width
= !html_context
->table_level
? INT_MAX
: width
;
2364 set_box(&node
->box
, x
, y
, node_width
, 1);
2365 add_to_list(document
->nodes
, node
);
2368 renderer_context
.last_link_to_move
= document
->nlinks
;
2369 renderer_context
.last_tag_to_move
= (struct tag
*) &document
->tags
;
2370 renderer_context
.last_tag_for_newline
= (struct tag
*) &document
->tags
;
2372 renderer_context
.last_link_to_move
= 0;
2373 renderer_context
.last_tag_to_move
= (struct tag
*) NULL
;
2374 renderer_context
.last_tag_for_newline
= (struct tag
*) NULL
;
2377 html_context
->margin
= margin
;
2378 renderer_context
.empty_format
= !document
;
2380 done_link_state_info();
2381 renderer_context
.nobreak
= 1;
2383 part
= mem_calloc(1, sizeof(*part
));
2384 if (!part
) goto ret
;
2386 part
->document
= document
;
2391 part
->link_num
= link_num
;
2393 html_state
= init_html_parser_state(html_context
, ELEMENT_IMMORTAL
, align
, margin
, width
);
2395 parse_html(start
, end
, part
, head
, html_context
);
2397 done_html_parser_state(html_context
, html_state
);
2399 int_lower_bound(&part
->max_width
, part
->box
.width
);
2401 renderer_context
.nobreak
= 0;
2403 done_link_state_info();
2404 mem_free_if(part
->spaces
);
2406 mem_free_if(part
->char_width
);
2410 struct node
*node
= document
->nodes
.next
;
2412 node
->box
.height
= y
- node
->box
.y
+ part
->box
.height
;
2416 renderer_context
.last_link_to_move
= saved_last_link_to_move
;
2417 renderer_context
.last_tag_to_move
= saved_last_tag_to_move
;
2418 renderer_context
.empty_format
= saved_empty_format
;
2420 html_context
->margin
= saved_margin
;
2422 if (html_context
->table_level
> 1 && !document
2424 && table_cache_entries
< MAX_TABLE_CACHE_ENTRIES
) {
2425 /* Create a new entry. */
2426 /* Clear memory to prevent bad key comparaison due to alignment
2428 struct table_cache_entry
*tce
= mem_calloc(1, sizeof(*tce
));
2431 tce
->key
.start
= start
;
2433 tce
->key
.align
= align
;
2434 tce
->key
.margin
= margin
;
2435 tce
->key
.width
= width
;
2437 tce
->key
.link_num
= link_num
;
2438 copy_struct(&tce
->part
, part
);
2440 if (!add_hash_item(table_cache
,
2441 (unsigned char *) &tce
->key
,
2442 sizeof(tce
->key
), tce
)) {
2445 table_cache_entries
++;
2454 render_html_document(struct cache_entry
*cached
, struct document
*document
,
2455 struct string
*buffer
)
2457 struct html_context
*html_context
;
2459 unsigned char *start
;
2461 struct string title
;
2464 assert(cached
&& document
);
2465 if_assert_failed
return;
2467 if (!init_string(&head
)) return;
2469 if (cached
->head
) add_to_string(&head
, cached
->head
);
2471 start
= buffer
->source
;
2472 end
= buffer
->source
+ buffer
->length
;
2474 html_context
= init_html_parser(cached
->uri
, &document
->options
,
2475 start
, end
, &head
, &title
,
2476 put_chars_conv
, line_break
,
2478 if (!html_context
) return;
2480 renderer_context
.g_ctrl_num
= 0;
2481 renderer_context
.cached
= cached
;
2482 renderer_context
.convert_table
= get_convert_table(head
.source
,
2483 document
->options
.cp
,
2484 document
->options
.assume_cp
,
2486 &document
->cp_status
,
2487 document
->options
.hard_assume
);
2489 html_context
->options
->utf8
= is_cp_utf8(document
->options
.cp
);
2490 #endif /* CONFIG_UTF8 */
2491 html_context
->doc_cp
= document
->cp
;
2494 /* CSM_DEFAULT because init_html_parser() did not
2495 * decode entities in the title. */
2496 document
->title
= convert_string(renderer_context
.convert_table
,
2497 title
.source
, title
.length
,
2498 document
->options
.cp
,
2499 CSM_DEFAULT
, NULL
, NULL
, NULL
);
2501 done_string(&title
);
2503 part
= format_html_part(html_context
, start
, end
, par_format
.align
,
2504 par_format
.leftmargin
,
2505 document
->options
.box
.width
, document
,
2506 0, 0, head
.source
, 1);
2508 /* Drop empty allocated lines at end of document if any
2509 * and adjust document height. */
2510 while (document
->height
&& !document
->data
[document
->height
- 1].length
)
2511 mem_free_if(document
->data
[--document
->height
].chars
);
2513 /* Calculate document width. */
2517 document
->width
= 0;
2518 for (i
= 0; i
< document
->height
; i
++)
2519 int_lower_bound(&document
->width
, document
->data
[i
].length
);
2523 document
->options
.needs_width
= 1;
2525 /* FIXME: This needs more tuning since if we are centering stuff it
2527 document
->options
.needs_width
=
2528 (document
->width
+ (document
->options
.margin
2529 >= document
->options
.width
));
2532 document
->color
.background
= par_format
.color
.background
;
2534 done_html_parser(html_context
);
2536 /* Drop forms which has been serving as a placeholder for form items
2537 * added in the wrong order due to the ordering of table rendering. */
2541 foreach (form
, document
->forms
) {
2545 if (list_empty(form
->items
))
2552 /* @part was residing in html_context so it has to stay alive until
2553 * done_html_parser(). */
2557 #if 0 /* debug purpose */
2559 FILE *f
= fopen("forms", "ab");
2560 struct form_control
*form
;
2562 fprintf(f
,"FORM:\n");
2563 foreach (form
, document
->forms
) {
2564 fprintf(f
, "g=%d f=%d c=%d t:%d\n",
2565 form
->g_ctrl_num
, form
->form_num
,
2566 form
->ctrl_num
, form
->type
);
2568 fprintf(f
,"fragment: \n");
2569 for (qq
= start
; qq
< end
; qq
++) fprintf(f
, "%c", *qq
);
2570 fprintf(f
,"----------\n\n");