+
+ /*
+ * Compute the cost of this line. The method of doing
+ * this differs hugely depending on whether
+ * natural_space is nonzero or not.
+ */
+ if (natural_space) {
+ if (!nspaces && linelen > thiswidth) {
+ /*
+ * Special case: if there are no spaces at all
+ * on the line because one single word is too
+ * long for its line, cost is zero because
+ * there's nothing we can do about it anyway.
+ */
+ cost = 0;
+ } else {
+ int shortfall = thiswidth - linelen;
+ int spaceextra = shortfall / (nspaces ? nspaces : 1);
+ int spaceshortfall = natural_space -
+ (minspacewidth + spaceextra);
+
+ if (i+j == nwords && spaceshortfall < 0) {
+ /*
+ * Special case: on the very last line of
+ * the paragraph, we don't score penalty
+ * points for having to _stretch_ the line,
+ * since we won't stretch it anyway.
+ * However, we score penalties as normal
+ * for having to squeeze it.
+ */
+ cost = 0;
+ } else {
+ /*
+ * Squaring this number is tricky since
+ * it's liable to be quite big. Let's
+ * divide it through by 256.
+ */
+ int x = spaceshortfall >> 8;
+ int xf = spaceshortfall & 0xFF;
+
+ /*
+ * Not counting strange variable-fixed-
+ * point oddities, we are computing
+ *
+ * (x+xf)^2 = x^2 + 2*x*xf + xf*xf
+ *
+ * except that _our_ xf is 256 times the
+ * one listed there.
+ */
+
+ cost = x * x;
+ cost += (2 * x * xf) >> 8;
+ }
+ }