2 * line formatting buffer for line adjustment and hyphenation
4 * The line formatting buffer does two main functions: breaking
5 * words into lines (possibly after breaking them at their
6 * hyphenation points), and, if requested, adjusting the space
7 * between words in a line. In this file the first step is
8 * referred to as filling.
10 * Functions like fmt_word() return nonzero on failure, which
11 * means the call should be repeated after fetching previously
12 * formatted lines via fmt_nextline().
19 #define FMT_LLEN(f) MAX(0, (f)->ll - (f)->li)
20 #define FMT_FILL(f) (!n_ce && n_u)
21 #define FMT_ADJ(f) (n_u && !n_na && !n_ce && (n_j & AD_B) == AD_B)
25 int wid
; /* word's width */
26 int elsn
, elsp
; /* els_neg and els_pos */
27 int gap
; /* the space before this word */
28 int hy
; /* hyphen width if inserted after this word */
29 int str
; /* does the space before it stretch */
40 struct word words
[NWORDS
];
43 struct line lines
[NLINES
];
45 /* for paragraph adjustment */
50 int gap
; /* space before the next word */
51 int nls
; /* newlines before the next word */
52 int nls_sup
; /* suppressed newlines */
53 int li
, ll
; /* current line indentation and length */
54 int filled
; /* filled all words in the last fmt_fill() */
55 int eos
; /* last word ends a sentence */
56 int fillreq
; /* fill after the last word (\p) */
59 /* .ll, .in and .ti are delayed until the partial line is output */
60 static void fmt_confupdate(struct fmt
*f
)
63 f
->li
= n_ti
>= 0 ? n_ti
: n_i
;
67 static int fmt_confchanged(struct fmt
*f
)
69 return f
->ll
!= n_l
|| f
->li
!= (n_ti
>= 0 ? n_ti
: n_i
);
72 /* move words inside an fmt struct */
73 static void fmt_movewords(struct fmt
*a
, int dst
, int src
, int len
)
75 memmove(a
->words
+ dst
, a
->words
+ src
, len
* sizeof(a
->words
[0]));
78 /* move words from the buffer to s */
79 static int fmt_wordscopy(struct fmt
*f
, int beg
, int end
,
80 struct sbuf
*s
, int *els_neg
, int *els_pos
)
87 for (i
= beg
; i
< end
; i
++) {
89 sbuf_printf(s
, "%ch'%du'", c_ec
, wcur
->gap
);
90 sbuf_append(s
, wcur
->s
);
91 w
+= wcur
->wid
+ wcur
->gap
;
92 if (wcur
->elsn
< *els_neg
)
93 *els_neg
= wcur
->elsn
;
94 if (wcur
->elsp
> *els_pos
)
95 *els_pos
= wcur
->elsp
;
99 wcur
= &f
->words
[end
- 1];
101 sbuf_append(s
, "\\(hy");
107 static int fmt_nlines(struct fmt
*f
)
109 if (f
->l_tail
<= f
->l_head
)
110 return f
->l_head
- f
->l_tail
;
111 return NLINES
- f
->l_tail
+ f
->l_head
;
114 /* the total width of the specified words in f->words[] */
115 static int fmt_wordslen(struct fmt
*f
, int beg
, int end
)
118 for (i
= beg
; i
< end
; i
++)
119 w
+= f
->words
[i
].wid
+ f
->words
[i
].gap
;
120 return beg
< end
? w
+ f
->words
[end
- 1].hy
: 0;
123 /* the number of stretchable spaces in f */
124 static int fmt_spaces(struct fmt
*f
, int beg
, int end
)
127 for (i
= beg
+ 1; i
< end
; i
++)
133 /* the amount of stretchable spaces in f */
134 static int fmt_spacessum(struct fmt
*f
, int beg
, int end
)
137 for (i
= beg
+ 1; i
< end
; i
++)
139 n
+= f
->words
[i
].gap
;
143 /* return the next line in the buffer */
144 int fmt_nextline(struct fmt
*f
, struct sbuf
*sbuf
, int *w
,
145 int *li
, int *ll
, int *els_neg
, int *els_pos
)
148 l
= &f
->lines
[f
->l_tail
];
149 if (f
->l_head
== f
->l_tail
)
156 sbuf_append(sbuf
, sbuf_buf(&l
->sbuf
));
158 f
->l_tail
= (f
->l_tail
+ 1) % NLINES
;
162 static struct line
*fmt_mkline(struct fmt
*f
)
164 struct line
*l
= &f
->lines
[f
->l_head
];
165 if ((f
->l_head
+ 1) % NLINES
== f
->l_tail
)
167 f
->l_head
= (f
->l_head
+ 1) % NLINES
;
174 static int fmt_sp(struct fmt
*f
)
185 l
->wid
= fmt_wordscopy(f
, 0, f
->nwords
, &l
->sbuf
, &l
->elsn
, &l
->elsp
);
191 int fmt_br(struct fmt
*f
)
201 void fmt_space(struct fmt
*fmt
)
203 fmt
->gap
+= N_SS(n_f
, n_s
);
206 int fmt_newline(struct fmt
*f
)
217 if (f
->nls
== 0 && !f
->filled
&& !f
->nwords
)
223 /* format the paragraph after the next word (\p) */
224 int fmt_fillreq(struct fmt
*f
)
229 f
->fillreq
= f
->nwords
+ 1;
233 static void fmt_wb2word(struct fmt
*f
, struct word
*word
, struct wb
*wb
,
234 int hy
, int str
, int gap
)
236 int len
= strlen(wb_buf(wb
));
237 word
->s
= xmalloc(len
+ 1);
238 memcpy(word
->s
, wb_buf(wb
), len
+ 1);
239 word
->wid
= wb_wid(wb
);
240 word
->elsn
= wb
->els_neg
;
241 word
->elsp
= wb
->els_pos
;
242 word
->hy
= hy
? wb_dashwid(wb
) : 0;
247 /* find explicit hyphenation positions: dashes, \: and \% */
248 static int fmt_hyphmarks(char *word
, int *hyidx
, int *hyins
)
253 while ((c
= escread(&s
, d
)) > 0)
255 if (c
< 0 || !strcmp(c_hc
, d
))
257 while ((c
= escread(&s
, d
)) >= 0 && n
< NHYPHSWORD
) {
258 if (!c
&& !strcmp(c_hc
, d
)) {
260 hyidx
[n
++] = s
- word
;
262 if (!c
&& (!strcmp(c_bp
, d
) || c_isdash(d
))) {
264 hyidx
[n
++] = s
- word
;
270 static void fmt_insertword(struct fmt
*f
, struct wb
*wb
, int gap
)
272 int hyidx
[NHYPHSWORD
];
273 int hyins
[NHYPHSWORD
] = {0};
274 char *src
= wb_buf(wb
);
280 n
= fmt_hyphmarks(src
, hyidx
, hyins
);
282 fmt_wb2word(f
, &f
->words
[f
->nwords
++], wb
, 0, 1, gap
);
286 for (i
= 0; i
<= n
; i
++) {
287 beg
= src
+ (i
> 0 ? hyidx
[i
- 1] : 0);
288 end
= src
+ (i
< n
? hyidx
[i
] : strlen(src
));
289 wb_catstr(&wbc
, beg
, end
);
290 fmt_wb2word(f
, &f
->words
[f
->nwords
++], &wbc
,
291 i
< n
&& hyins
[i
], i
== 0, i
== 0 ? gap
: 0);
293 wb_fnszget(&wbc
, &cs
, &cf
, &cm
);
295 wb_fnszset(&wbc
, cs
, cf
, cm
);
300 /* the amount of space necessary before the next word */
301 static int fmt_wordgap(struct fmt
*f
)
303 int nls
= f
->nls
|| f
->nls_sup
;
304 if (f
->eos
&& f
->nwords
)
305 if ((nls
&& !f
->gap
) || (!nls
&& f
->gap
== 2 * N_SS(n_f
, n_s
)))
306 return N_SS(n_f
, n_s
) + N_SSS(n_f
, n_s
);
307 return (nls
&& !f
->gap
&& f
->nwords
) ? N_SS(n_f
, n_s
) : f
->gap
;
310 /* insert wb into fmt */
311 int fmt_word(struct fmt
*f
, struct wb
*wb
)
315 if (f
->nwords
+ NHYPHSWORD
>= NWORDS
|| fmt_confchanged(f
))
318 if (FMT_FILL(f
) && f
->nls
&& f
->gap
)
321 if (!f
->nwords
) /* apply the new .l and .i */
323 f
->gap
= fmt_wordgap(f
);
325 fmt_insertword(f
, wb
, f
->filled
? 0 : f
->gap
);
333 /* assuming an empty line has cost 10000; take care of integer overflow */
334 #define POW2(x) ((x) * (x))
335 #define FMT_COST(lwid, llen, pen) (POW2(((llen) - (lwid)) * 1000l / (llen)) / 100l + (pen) * 10l)
337 /* the cost of putting a line break before word pos */
338 static long fmt_findcost(struct fmt
*f
, int pos
)
342 int lwid
= 0; /* current line length */
343 int swid
= 0; /* amount of spaces */
344 int llen
= MAX(1, FMT_LLEN(f
));
347 if (f
->best_pos
[pos
] >= 0)
351 if (f
->words
[i
].hy
) /* the last word is hyphenated */
352 lwid
+= f
->words
[i
].hy
;
356 lwid
+= f
->words
[i
].wid
;
358 lwid
+= f
->words
[i
+ 1].gap
;
359 if (i
+ 1 < pos
&& f
->words
[i
+ 1].str
)
360 swid
+= f
->words
[i
+ 1].gap
;
361 if (lwid
- (swid
* n_ssh
/ 100) > llen
)
364 cur
= fmt_findcost(f
, i
) + FMT_COST(lwid
, llen
, pen
);
365 if (f
->best_pos
[pos
] < 0 || cur
< f
->best
[pos
]) {
366 f
->best_pos
[pos
] = i
;
367 f
->best_dep
[pos
] = f
->best_dep
[i
] + 1;
375 static int fmt_bestpos(struct fmt
*f
, int pos
)
377 fmt_findcost(f
, pos
);
378 return MAX(0, f
->best_pos
[pos
]);
381 static int fmt_bestdep(struct fmt
*f
, int pos
)
383 fmt_findcost(f
, pos
);
384 return MAX(0, f
->best_dep
[pos
]);
387 /* return the last filled word */
388 static int fmt_breakparagraph(struct fmt
*f
, int pos
)
392 int llen
= FMT_LLEN(f
);
394 if (f
->fillreq
> 0 && f
->fillreq
<= f
->nwords
) {
395 fmt_findcost(f
, f
->fillreq
);
398 if (pos
> 0 && f
->words
[pos
- 1].wid
>= llen
) {
399 fmt_findcost(f
, pos
);
404 if (f
->words
[i
].hy
) /* the last word is hyphenated */
405 lwid
+= f
->words
[i
].hy
;
407 lwid
+= f
->words
[i
].wid
;
409 lwid
+= f
->words
[i
+ 1].gap
;
410 if (lwid
> llen
&& i
+ 1 < pos
)
412 if (best
< 0 || fmt_findcost(f
, i
) < fmt_findcost(f
, best
))
419 /* extract the first nreq formatted lines before the word at pos */
420 static int fmt_head(struct fmt
*f
, int nreq
, int pos
)
422 int best
= pos
; /* best line break for nreq-th line */
423 int prev
, next
; /* best line breaks without hyphenation */
424 if (nreq
<= 0 || fmt_bestdep(f
, pos
) < nreq
)
426 /* finding the optimal line break for nreq-th line */
427 while (best
> 0 && fmt_bestdep(f
, best
) > nreq
)
428 best
= fmt_bestpos(f
, best
);
431 /* finding closest line breaks without hyphenation */
432 while (prev
> 1 && f
->words
[prev
- 1].hy
&&
433 fmt_bestdep(f
, prev
- 1) == nreq
)
435 while (next
< pos
&& f
->words
[next
- 1].hy
&&
436 fmt_bestdep(f
, next
+ 1) == nreq
)
438 /* choosing the best of them */
439 if (!f
->words
[prev
- 1].hy
&& !f
->words
[next
- 1].hy
)
440 return fmt_findcost(f
, prev
) <= fmt_findcost(f
, next
) ? prev
: next
;
441 if (!f
->words
[prev
- 1].hy
)
443 if (!f
->words
[next
- 1].hy
)
448 /* break f->words[0..end] into lines according to fmt_bestpos() */
449 static int fmt_break(struct fmt
*f
, int end
)
451 int llen
, fmt_div
, fmt_rem
, beg
;
455 beg
= fmt_bestpos(f
, end
);
457 ret
+= fmt_break(f
, beg
);
462 f
->words
[beg
].gap
= 0;
463 w
= fmt_wordslen(f
, beg
, end
);
464 nspc
= fmt_spaces(f
, beg
, end
);
465 if (FMT_ADJ(f
) && nspc
) {
466 fmt_div
= (llen
- w
) / nspc
;
467 fmt_rem
= (llen
- w
) % nspc
;
472 for (i
= beg
+ 1; i
< end
; i
++)
474 f
->words
[i
].gap
+= fmt_div
+ (fmt_rem
-- > 0);
476 l
->wid
= fmt_wordscopy(f
, beg
, end
, &l
->sbuf
, &l
->elsn
, &l
->elsp
);
479 return ret
+ (end
- beg
);
482 /* estimated number of lines until traps or the end of a page */
483 static int fmt_safelines(void)
485 int lnht
= MAX(1, n_L
) * n_v
;
486 return (f_nexttrap() + lnht
- 1) / lnht
;
489 /* fill the words collected in the buffer */
490 int fmt_fill(struct fmt
*f
)
492 int nreq
; /* the number of lines until a trap */
493 int end
; /* the final line ends before this word */
494 int end_head
; /* like end, but only the first nreq lines included */
495 int head
= 0; /* only nreq first lines have been formatted */
496 int llen
; /* line length, taking shrinkable spaces into account */
500 llen
= fmt_wordslen(f
, 0, f
->nwords
) -
501 fmt_spacessum(f
, 0, f
->nwords
) * n_ssh
/ 100;
502 /* not enough words to fill */
503 if ((f
->fillreq
<= 0 || f
->nwords
< f
->fillreq
) && llen
<= FMT_LLEN(f
))
505 nreq
= (n_hy
& HY_LAST
) ? fmt_safelines() : 0;
506 if (nreq
> 0 && nreq
<= fmt_nlines(f
))
508 /* resetting positions */
509 for (i
= 0; i
< f
->nwords
+ 1; i
++)
511 end
= fmt_breakparagraph(f
, f
->nwords
);
513 end_head
= fmt_head(f
, nreq
- fmt_nlines(f
), end
);
514 head
= end_head
< end
;
517 /* recursively add lines */
518 n
= fmt_break(f
, end
);
521 fmt_movewords(f
, 0, n
, f
->nwords
);
522 f
->filled
= n
&& !f
->nwords
;
525 if (f
->nwords
) /* apply the new .l and .i */
527 return head
|| n
!= end
;
530 struct fmt
*fmt_alloc(void)
532 struct fmt
*fmt
= xmalloc(sizeof(*fmt
));
533 memset(fmt
, 0, sizeof(*fmt
));
537 void fmt_free(struct fmt
*fmt
)
542 int fmt_wid(struct fmt
*fmt
)
544 return fmt_wordslen(fmt
, 0, fmt
->nwords
) + fmt_wordgap(fmt
);
547 int fmt_morewords(struct fmt
*fmt
)
549 return fmt_morelines(fmt
) || fmt
->nwords
;
552 int fmt_morelines(struct fmt
*fmt
)
554 return fmt
->l_head
!= fmt
->l_tail
;
557 /* suppress the last newline */
558 void fmt_suppressnl(struct fmt
*fmt
)