From 5ff8ac2dfe6acb3c27d22b0bc344bad06919d919 Mon Sep 17 00:00:00 2001 From: Ali Gholami Rudi Date: Fri, 12 Dec 2014 16:16:07 +0330 Subject: [PATCH] fmt: specify the cost of short lines Now the cost of short lines is 100 or the value specified as the second argument of .pmll. --- fmt.c | 25 +++++++++---------------- reg.c | 4 ++-- roff.h | 5 +++-- tr.c | 3 ++- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/fmt.c b/fmt.c index b51ed4d..17894ed 100644 --- a/fmt.c +++ b/fmt.c @@ -173,7 +173,7 @@ static struct line *fmt_mkline(struct fmt *f) return l; } -static int fmt_extractline(struct fmt *f, int beg, int end, int llen, int spread) +static int fmt_extractline(struct fmt *f, int beg, int end, int llen) { int fmt_div, fmt_rem; int w, i, nspc; @@ -183,7 +183,7 @@ static int fmt_extractline(struct fmt *f, int beg, int end, int llen, int spread w = fmt_wordslen(f, beg, end); nspc = fmt_spaces(f, beg, end); /* stretch if (spread & 1) and shrink if (spread & 2) */ - if (nspc && ((spread & 1 && w < llen) || (spread & 2 && w > llen))) { + if (nspc && llen) { fmt_div = (llen - w) / nspc; fmt_rem = (llen - w) % nspc; if (fmt_rem < 0) { @@ -202,8 +202,7 @@ static int fmt_sp(struct fmt *f) { if (fmt_fillwords(f, 1)) return 1; - if (fmt_extractline(f, 0, f->nwords, FMT_LLEN(f) * n_pmll / 100, - FMT_ADJ(f) && n_j & AD_P)) + if (fmt_extractline(f, 0, f->nwords, 0)) return 1; f->filled = 0; f->nls--; @@ -385,14 +384,6 @@ static long FMT_COST(int llen, int lwid, int swid, int nspc) return ratio * ratio / 100l * (nspc ? nspc : 1); } -/* the cost of formatting last lines; should prevent widows */ -static long FMT_LCOST(int llen, int lwid, int swid, int nspc) -{ - if (!n_pmll || lwid >= llen * n_pmll / 100) - return 0; - return FMT_COST(llen * n_pmll / 100, lwid, swid, nspc); -} - /* the cost of putting a line break before word pos */ static long fmt_findcost(struct fmt *f, int pos) { @@ -411,7 +402,7 @@ static long fmt_findcost(struct fmt *f, int pos) if (f->words[i].hy) /* the last word is hyphenated */ lwid += f->words[i].hy; if (f->words[i].hy) - pen = n_hyp; + pen = n_hycost; while (i >= 0) { lwid += f->words[i].wid; if (i + 1 < pos) @@ -479,8 +470,10 @@ static int fmt_breakparagraph(struct fmt *f, int pos, int br) } if (lwid > llen && i + 1 < pos) break; - cost = fmt_findcost(f, i) + - (br ? FMT_LCOST(llen, lwid, swid, nspc) : 0); + cost = fmt_findcost(f, i); + /* the cost of formatting short lines; should prevent widows */ + if (br && n_pmll && lwid < llen * n_pmll / 100) + cost += n_pmllcost; if (best < 0 || cost < best_cost) { best = i; best_cost = cost; @@ -527,7 +520,7 @@ static int fmt_break(struct fmt *f, int end) if (beg > 0) ret += fmt_break(f, beg); f->words[beg].gap = 0; - if (fmt_extractline(f, beg, end, FMT_LLEN(f), FMT_ADJ(f) ? 3 : 0)) + if (fmt_extractline(f, beg, end, FMT_ADJ(f) ? FMT_LLEN(f) : 0)) return ret; if (beg > 0) fmt_confupdate(f); diff --git a/reg.c b/reg.c index 46e0dc1..2ee34f0 100644 --- a/reg.c +++ b/reg.c @@ -37,8 +37,8 @@ static char *eregs[] = { /* environment-specific number registers */ ".L", ".nI", ".nm", ".nM", ".nn", ".nS", ".m", ".s", ".u", ".v", ".it", ".itn", ".mc", ".mcn", - ".ce", ".f0", ".hy", ".hyp", ".i0", ".l0", - ".L0", ".m0", ".n0", ".s0", ".ss", ".ssh", ".sss", ".pmll", + ".ce", ".f0", ".hy", ".hycost", ".i0", ".l0", + ".L0", ".m0", ".n0", ".s0", ".ss", ".ssh", ".sss", ".pmll", ".pmllcost", ".ti", ".lt", ".lt0", ".v0", }; diff --git a/roff.h b/roff.h index 1973add..08e386b 100644 --- a/roff.h +++ b/roff.h @@ -463,7 +463,7 @@ int clr_get(char *s); #define n_f0 (*nreg(map(".f0"))) /* last .f */ #define n_lg (*nreg(map(".lg"))) /* .lg mode */ #define n_hy (*nreg(map(".hy"))) /* .hy mode */ -#define n_hyp (*nreg(map(".hyp"))) /* hyphenation penalty */ +#define n_hycost (*nreg(map(".hycost"))) /* hyphenation cost */ #define n_i0 (*nreg(map(".i0"))) /* last .i */ #define n_ti (*nreg(map(".ti"))) /* pending .ti */ #define n_kn (*nreg(map(".kn"))) /* .kn mode */ @@ -474,7 +474,8 @@ int clr_get(char *s); #define n_na (*nreg(map(".na"))) /* .na mode */ #define n_ns (*nreg(map(".ns"))) /* .ns mode */ #define n_o0 (*nreg(map(".o0"))) /* last .o */ -#define n_pmll (*nreg(map(".pmll"))) /* minimum last line (.pmll) */ +#define n_pmll (*nreg(map(".pmll"))) /* minimum line length (.pmll) */ +#define n_pmllcost (*nreg(map(".pmllcost"))) /* short line cost */ #define n_ss (*nreg(map(".ss"))) /* word space (.ss) */ #define n_sss (*nreg(map(".sss"))) /* sentence space (.ss) */ #define n_ssh (*nreg(map(".ssh"))) /* word space compression (.ssh) */ diff --git a/tr.c b/tr.c index 2fdc6e0..cbd21e7 100644 --- a/tr.c +++ b/tr.c @@ -455,12 +455,13 @@ static void tr_hy(char **args) static void tr_hyp(char **args) { - n_hyp = args[1] ? atoi(args[1]) : 0; + n_hycost = args[1] ? atoi(args[1]) : 0; } static void tr_pmll(char **args) { n_pmll = args[1] ? atoi(args[1]) : 0; + n_pmllcost = args[2] ? atoi(args[2]) : 100; } static void tr_lg(char **args) -- 2.11.4.GIT