Skip to content

Instantly share code, notes, and snippets.

@reffort
Created July 31, 2015 00:59
Show Gist options
  • Save reffort/60288c3bb2e42d678ef7 to your computer and use it in GitHub Desktop.
Save reffort/60288c3bb2e42d678ef7 to your computer and use it in GitHub Desktop.
A penalty function to experiment with different windows and calculation methods
// experimental penalty() function for n7.c
// Rename the existing function to x_penalty() or something similar so that it won't be executed
// and then add the following function
static double
penalty(int k, int s, int h, int h2, int h3)
{
double t, d, p1, p2, p3 ;
int scaletype, calctype ;
// Change the values of scaletype and calctype to select the range of acceptable space sizes and the demerits calculation you want,
// then recompile troff. The scaletype and calctype can be matched as desired.
//
// The hyphenation penalty should be adjusted externally to reasonably compare the calctypes, although there is no direct match.
// If hypp 50 is assumed to be the standard number, the hypp value used with the Tex calculation (2) should be about 25, and
// for the two-stage curve (4) hypp should be about 35.
//
// In Heirloom's calculations, hypp 50 applies a penalty of the same value as a marginally bad word space at the edge of the
// acceptable range (0 or 19.2 troff space units, with the desired space size of 12 units). Compare with TeX's value 50,
// which applies a penalty similar to that of a word space that is a little worse than the "normal" range (10 to 15 units).
scaletype = 1 ; // 1=Heirloom (0-19.2), 2=tex (8-18), 3=symmetrical (6-18), 4=intermediate (7-18)
calctype = 1 ; // 1=Heirloom, 2=tex14, 3=kp81, 4=two-stage quadratic, 5=initial proposal
// ----------------
t = nel - k;
if (scaletype <= 1 || scaletype > 4)
t = t >= 0 ? t * 5 / 3 : -t ; // Heirloom's original line
else if (scaletype == 2)
t = t >= 0 ? t * 2 : t * -3 ; // TeX
else if (scaletype == 3)
t = t >= 0 ? t * 2 : t * -2 ; // symmetrical
else if (scaletype == 4)
t = t >= 0 ? t * 2 : t * -2.5 ; // intermediate
// -----------------
if (ad && !admod) {
d = s;
// if (k - s && (letsps || lshmin || lshmax))
// d += (double)(k - s) / 100;
if (d)
t /= d; // rj
} else
t /= nel / 10;
p1 = p2 = p3 = 0 ;
if (h && hypp)
p1 = hypp ;
if (h2 && hypp2)
p2 = hypp2 ;
if (h3 && hypp3)
p3 = hypp3 ;
// demerits calculations ------
if (calctype <= 1 || calctype > 5) { // heirloom
t = t + p1 + p2 + p3 ;
t = t * t * t ;
} else if (calctype == 2) { // TeX14 (after 1982) (adapted)
t = 1 + 100 * t * t * t ;
t = t * t ;
t /= 10000 ;
t += p1 * p1 + p2 * p2 + p3 * p3 ;
} else if (calctype == 3) { // TeX from KP-81 (adapted)
t = 1 + 100 * t * t * t + p1 ;
t = t * t ;
t /= 10000 ;
t += p2 * p2 + p3 * p3 ;
} else if (calctype == 4) { // two-stage curve
t = t * t ; // quadratic in-band
if (t > 1)
t = t * t * t ; // but same out-of-band as the TeX14 curve
t += p1 * p1 + p2 * p2 + p3 * p3 ;
} else if (calctype == 5) { // initial proposal
t = t * t * t ;
t = t * t ;
t += (p1 + p2 + p3) * (p1 + p2 * p3) ;
}
// -----------------
if (t > MAXPENALTY)
t = MAXPENALTY;
return t;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment