+static para_data *make_para_data(int ptype, int paux, int indent, int rmargin,
+ word *pkwtext, word *pkwtext2, word *pwords,
+ paper_conf *conf)
+{
+ para_data *pdata;
+ line_data *ldata;
+ int extra_indent, firstline_indent, aux_indent;
+ word *aux, *aux2;
+
+ pdata = mknew(para_data);
+ pdata->outline_level = -1;
+ pdata->outline_title = NULL;
+ pdata->rect_type = RECT_NONE;
+ pdata->contents_entry = NULL;
+ pdata->justification = JUST;
+
+ /*
+ * Choose fonts for this paragraph.
+ *
+ * FIXME: All of this ought to be completely
+ * user-configurable.
+ */
+ switch (ptype) {
+ case para_Title:
+ pdata->fonts[FONT_NORMAL] = conf->hr;
+ pdata->sizes[FONT_NORMAL] = 24;
+ pdata->fonts[FONT_EMPH] = conf->hi;
+ pdata->sizes[FONT_EMPH] = 24;
+ pdata->fonts[FONT_CODE] = conf->cb;
+ pdata->sizes[FONT_CODE] = 24;
+ pdata->outline_level = 0;
+ break;
+
+ case para_Chapter:
+ case para_Appendix:
+ case para_UnnumberedChapter:
+ pdata->fonts[FONT_NORMAL] = conf->hr;
+ pdata->sizes[FONT_NORMAL] = 20;
+ pdata->fonts[FONT_EMPH] = conf->hi;
+ pdata->sizes[FONT_EMPH] = 20;
+ pdata->fonts[FONT_CODE] = conf->cb;
+ pdata->sizes[FONT_CODE] = 20;
+ pdata->outline_level = 1;
+ break;
+
+ case para_Heading:
+ case para_Subsect:
+ pdata->fonts[FONT_NORMAL] = conf->hr;
+ pdata->fonts[FONT_EMPH] = conf->hi;
+ pdata->fonts[FONT_CODE] = conf->cb;
+ pdata->sizes[FONT_NORMAL] =
+ pdata->sizes[FONT_EMPH] =
+ pdata->sizes[FONT_CODE] =
+ (paux == 0 ? 16 : paux == 1 ? 14 : 13);
+ pdata->outline_level = 2 + paux;
+ break;
+
+ case para_Normal:
+ case para_BiblioCited:
+ case para_Bullet:
+ case para_NumberedList:
+ case para_DescribedThing:
+ case para_Description:
+ case para_Copyright:
+ pdata->fonts[FONT_NORMAL] = conf->tr;
+ pdata->sizes[FONT_NORMAL] = 12;
+ pdata->fonts[FONT_EMPH] = conf->ti;
+ pdata->sizes[FONT_EMPH] = 12;
+ pdata->fonts[FONT_CODE] = conf->cr;
+ pdata->sizes[FONT_CODE] = 12;
+ break;
+ }
+
+ /*
+ * Also select an indentation level depending on the
+ * paragraph type (list paragraphs other than
+ * para_DescribedThing need extra indent).
+ *
+ * (FIXME: Perhaps at some point we might even arrange
+ * for the user to be able to request indented first
+ * lines in paragraphs.)
+ */
+ if (ptype == para_Bullet ||
+ ptype == para_NumberedList ||
+ ptype == para_Description) {
+ extra_indent = firstline_indent = conf->indent_list;
+ } else {
+ extra_indent = firstline_indent = 0;
+ }
+
+ /*
+ * Find the auxiliary text for this paragraph.
+ */
+ aux = aux2 = NULL;
+ aux_indent = 0;
+
+ switch (ptype) {
+ case para_Chapter:
+ case para_Appendix:
+ case para_Heading:
+ case para_Subsect:
+ /*
+ * For some heading styles (FIXME: be able to
+ * configure which), the auxiliary text contains
+ * the chapter number and is arranged to be
+ * right-aligned a few points left of the primary
+ * margin. For other styles, the auxiliary text is
+ * the full chapter _name_ and takes up space
+ * within the (wrapped) chapter title, meaning that
+ * we must move the first line indent over to make
+ * space for it.
+ */
+ if (ptype == para_Heading || ptype == para_Subsect) {
+ int len;
+
+ aux = pkwtext2;
+ len = paper_width_simple(pdata, pkwtext2);
+ aux_indent = -len - conf->sect_num_left_space;
+
+ pdata->outline_title =
+ prepare_outline_title(pkwtext2, L" ", pwords);
+ } else {
+ aux = pkwtext;
+ aux2 = fake_word(L": ");
+ aux_indent = 0;
+
+ firstline_indent += paper_width_simple(pdata, aux);
+ firstline_indent += paper_width_simple(pdata, aux2);
+
+ pdata->outline_title =
+ prepare_outline_title(pkwtext, L": ", pwords);
+ }
+ break;
+
+ case para_Bullet:
+ /*
+ * Auxiliary text consisting of a bullet. (FIXME:
+ * configurable bullet.)
+ */
+ aux = fake_word(L"\x2022");
+ aux_indent = indent + conf->indent_list_bullet;
+ break;
+
+ case para_NumberedList:
+ /*
+ * Auxiliary text consisting of the number followed
+ * by a (FIXME: configurable) full stop.
+ */
+ aux = pkwtext;
+ aux2 = fake_word(L".");
+ aux_indent = indent + conf->indent_list_bullet;
+ break;
+
+ case para_BiblioCited:
+ /*
+ * Auxiliary text consisting of the bibliography
+ * reference text, and a trailing space.
+ */
+ aux = pkwtext;
+ aux2 = fake_word(L" ");
+ aux_indent = indent;
+ firstline_indent += paper_width_simple(pdata, aux);
+ firstline_indent += paper_width_simple(pdata, aux2);
+ break;
+ }
+
+ if (pdata->outline_level >= 0 && !pdata->outline_title) {
+ pdata->outline_title =
+ prepare_outline_title(NULL, NULL, pwords);
+ }
+
+ wrap_paragraph(pdata, pwords, conf->base_width - rmargin,
+ indent + firstline_indent,
+ indent + extra_indent);
+
+ pdata->first->aux_text = aux;
+ pdata->first->aux_text_2 = aux2;
+ pdata->first->aux_left_indent = aux_indent;
+
+ /*
+ * Line breaking penalties.
+ */
+ switch (ptype) {
+ case para_Chapter:
+ case para_Appendix:
+ case para_Heading:
+ case para_Subsect:
+ case para_UnnumberedChapter:
+ /*
+ * Fixed and large penalty for breaking straight
+ * after a heading; corresponding bonus for
+ * breaking straight before.
+ */
+ pdata->first->penalty_before = -500000;
+ pdata->last->penalty_after = 500000;
+ for (ldata = pdata->first; ldata; ldata = ldata->next)
+ ldata->penalty_after = 500000;
+ break;
+
+ case para_DescribedThing:
+ /*
+ * This is treated a bit like a small heading:
+ * there's a penalty for breaking after it (i.e.
+ * between it and its description), and a bonus for
+ * breaking before it (actually _between_ list
+ * items).
+ */
+ pdata->first->penalty_before = -200000;
+ pdata->last->penalty_after = 200000;
+ break;
+
+ default:
+ /*
+ * Most paragraph types: widow/orphan control by
+ * discouraging breaking one line from the end of
+ * any paragraph.
+ */
+ if (pdata->first != pdata->last) {
+ pdata->first->penalty_after = 100000;
+ pdata->last->penalty_before = 100000;
+ }
+ break;
+ }
+
+ standard_line_spacing(pdata, conf);
+
+ /*
+ * Some kinds of section heading require a page break before
+ * them and an underline after.
+ */
+ if (ptype == para_Title ||
+ ptype == para_Chapter ||
+ ptype == para_Appendix ||
+ ptype == para_UnnumberedChapter) {
+ pdata->first->page_break = TRUE;
+ pdata->first->space_before = conf->chapter_top_space;
+ pdata->last->space_after +=
+ (conf->chapter_underline_depth +
+ conf->chapter_underline_thickness);
+ pdata->rect_type = RECT_CHAPTER_UNDERLINE;
+ }
+
+ return pdata;
+}
+
+static void standard_line_spacing(para_data *pdata, paper_conf *conf)
+{
+ line_data *ldata;
+
+ /*
+ * Set the line spacing for each line in this paragraph.
+ */
+ for (ldata = pdata->first; ldata; ldata = ldata->next) {
+ if (ldata == pdata->first)
+ ldata->space_before = conf->base_para_spacing / 2;
+ else
+ ldata->space_before = conf->base_leading / 2;
+ if (ldata == pdata->last)
+ ldata->space_after = conf->base_para_spacing / 2;
+ else
+ ldata->space_after = conf->base_leading / 2;
+ ldata->page_break = FALSE;
+ }
+}
+