+void
+Output_def::normalize ()
+{
+ Real paper_width;
+ SCM scm_paper_width = c_variable ("paper-width");
+
+ bool twosided = to_boolean (c_variable ("two-sided"));
+ // We don't distinguish between outer-margin / left-margin and so on
+ // until page-stencil positioning in page.scm
+ Real left_margin, left_margin_default;
+ SCM scm_left_margin_default = (twosided
+ ? c_variable ("outer-margin-default-scaled")
+ : c_variable ("left-margin-default-scaled"));
+ SCM scm_left_margin = (twosided
+ ? c_variable ("outer-margin")
+ : c_variable ("left-margin"));
+
+ Real right_margin, right_margin_default;
+ SCM scm_right_margin_default = (twosided
+ ? c_variable ("inner-margin-default-scaled")
+ : c_variable ("right-margin-default-scaled"));
+ SCM scm_right_margin = (twosided
+ ? c_variable ("inner-margin")
+ : c_variable ("right-margin"));
+
+ if (SCM_UNBNDP (scm_paper_width)
+ || SCM_UNBNDP (scm_left_margin_default)
+ || SCM_UNBNDP (scm_right_margin_default))
+ {
+ programming_error ("called normalize () on paper with missing settings");
+ return;
+ }
+ else
+ {
+ paper_width = scm_to_double (scm_paper_width);
+ left_margin_default = scm_to_double (scm_left_margin_default);
+ right_margin_default = scm_to_double (scm_right_margin_default);
+ }
+
+ Real line_width;
+ Real line_width_default
+ = paper_width - left_margin_default - right_margin_default;
+ SCM scm_line_width = c_variable ("line-width");
+
+ Real binding_offset = 0;
+ if (twosided)
+ binding_offset = robust_scm2double (c_variable ("binding-offset"), 0);
+
+ if (SCM_UNBNDP (scm_line_width))
+ {
+ left_margin = (SCM_UNBNDP (scm_left_margin)
+ ? left_margin_default
+ : scm_to_double (scm_left_margin));
+ right_margin = (SCM_UNBNDP (scm_right_margin)
+ ? right_margin_default
+ : scm_to_double (scm_right_margin)) + binding_offset;
+ line_width = paper_width - left_margin - right_margin;
+ }
+ else
+ {
+ line_width = scm_to_double (scm_line_width);
+ if (SCM_UNBNDP (scm_left_margin))
+ {
+ // Vertically center systems if only line-width is given
+ if (SCM_UNBNDP (scm_right_margin))
+ {
+ left_margin = (paper_width - line_width) / 2;
+ right_margin = left_margin;
+ }
+ else
+ {
+ right_margin = scm_to_double (scm_right_margin) + binding_offset;
+ left_margin = paper_width - line_width - right_margin;
+ }
+ }
+ else
+ {
+ left_margin = scm_to_double (scm_left_margin);
+ right_margin = (SCM_UNBNDP (scm_right_margin)
+ ? (paper_width - line_width - left_margin)
+ : scm_to_double (scm_right_margin)) + binding_offset;
+ }
+ }
+
+ if (to_boolean (c_variable ("check-consistency")))
+ {
+ // Consistency checks. If values don't match, set defaults.
+ if (abs (paper_width - line_width - left_margin - right_margin) > 1e-6)
+ {
+ line_width = line_width_default;
+ left_margin = left_margin_default;
+ right_margin = right_margin_default;
+ warning (_ ("margins do not fit with line-width, setting default values"));
+ }
+ else if ((left_margin < 0) || (right_margin < 0))
+ {
+ line_width = line_width_default;
+ left_margin = left_margin_default;
+ right_margin = right_margin_default;
+ warning (_ ("systems run off the page due to improper paper settings, setting default values"));
+ }
+ }
+
+ set_variable (ly_symbol2scm ("left-margin"), scm_from_double (left_margin));
+ set_variable (ly_symbol2scm ("right-margin"), scm_from_double (right_margin));
+ set_variable (ly_symbol2scm ("line-width"), scm_from_double (line_width));
+}
+