From 1969599b5f61d43d353e39e5b1cc1854a35a1684 Mon Sep 17 00:00:00 2001
From: Han-Wen Nienhuys <hanwen@xs4all.nl>
Date: Tue, 2 Jan 2007 16:13:20 +0100
Subject: [PATCH] Fix #190.

Add a hint-direction-penalty detail for Beam.  A slight push into
making slanted patterns use slanted beams.
---
 .../regression/beam-flat-retain-direction.ly  | 16 +++++++++++++
 lily/beam-quanting.cc                         | 23 ++++++++++++++-----
 lily/include/beam.hh                          |  1 +
 scm/define-grobs.scm                          |  3 ++-
 scm/layout-beam.scm                           |  2 +-
 5 files changed, 37 insertions(+), 8 deletions(-)
 create mode 100644 input/regression/beam-flat-retain-direction.ly

diff --git a/input/regression/beam-flat-retain-direction.ly b/input/regression/beam-flat-retain-direction.ly
new file mode 100644
index 0000000000..ba03734854
--- /dev/null
+++ b/input/regression/beam-flat-retain-direction.ly
@@ -0,0 +1,16 @@
+\header
+{
+  texidoc = "Even very flat but slanted patterns should give slanted beams. "
+}
+
+\version "2.10.7"
+\layout{
+  line-width = 15\cm
+  debug-beam-scoring = ##t
+}
+
+\relative c'''{
+  \time 2/4
+  \assertBeamQuant #'(0  . 1) #'(0 . 0) 
+  fis16[ dis b ais] cis4
+}
diff --git a/lily/beam-quanting.cc b/lily/beam-quanting.cc
index e150a51594..fc60f8def0 100644
--- a/lily/beam-quanting.cc
+++ b/lily/beam-quanting.cc
@@ -37,15 +37,17 @@ Beam_quant_parameters::fill (Grob *him)
 {
   SCM details = him->get_property ("details");
 
+  /*
+    TODO: put in define-grobs.scm
+   */
   INTER_QUANT_PENALTY = get_detail (details, ly_symbol2scm ("inter-quant-penalty"), 1000.0);
   SECONDARY_BEAM_DEMERIT = get_detail (details, ly_symbol2scm ("secondary-beam-demerit"), 10.0);
   STEM_LENGTH_DEMERIT_FACTOR = get_detail (details, ly_symbol2scm ("stem-length-demerit-factor"), 5);
   REGION_SIZE = get_detail (details, ly_symbol2scm ("region-size"), 2);
   BEAM_EPS = get_detail (details, ly_symbol2scm ("beam-eps"), 1e-3);
-
-  // possibly ridiculous, but too short stems just won't do
   STEM_LENGTH_LIMIT_PENALTY = get_detail (details, ly_symbol2scm ("stem-length-limit-penalty"), 5000);
   DAMPING_DIRECTION_PENALTY = get_detail (details, ly_symbol2scm ("damping-direction-penalty"), 800);
+  HINT_DIRECTION_PENALTY = get_detail (details, ly_symbol2scm ("hint-direction-penalty"), 20);
   MUSICAL_DIRECTION_FACTOR = get_detail (details, ly_symbol2scm ("musical-direction-factor"), 400);
   IDEAL_SLOPE_FACTOR = get_detail (details, ly_symbol2scm ("ideal-slope-factor"), 10);
   ROUND_TO_ZERO_SLOPE = get_detail (details, ly_symbol2scm ("round-to-zero-slope"), 0.02);
@@ -401,10 +403,19 @@ Beam::score_slopes_dy (Real yl, Real yr,
     TODO: find a way to incorporate the complexity of the beam in this
     penalty.
   */
-  if (fabs (dy / dx) > parameters->ROUND_TO_ZERO_SLOPE
-      && sign (dy_damp) != sign (dy))
-    dem += parameters->DAMPING_DIRECTION_PENALTY;
-
+  if (sign (dy_damp) != sign (dy))
+    {
+      if (!dy)
+	{
+	  if (fabs (dy_damp / dx) > parameters->ROUND_TO_ZERO_SLOPE)
+	    dem += parameters->DAMPING_DIRECTION_PENALTY;
+	  else
+	    dem += parameters->HINT_DIRECTION_PENALTY;
+	}
+      else
+	dem += parameters->DAMPING_DIRECTION_PENALTY;
+    }
+  
   dem += parameters->MUSICAL_DIRECTION_FACTOR
     * max (0.0, (fabs (dy) - fabs (dy_mus)));
 
diff --git a/lily/include/beam.hh b/lily/include/beam.hh
index 67daab8de8..3eb8027f49 100644
--- a/lily/include/beam.hh
+++ b/lily/include/beam.hh
@@ -34,6 +34,7 @@ struct Beam_quant_parameters
   Real STEM_LENGTH_LIMIT_PENALTY;
   Real DAMPING_DIRECTION_PENALTY;
   Real MUSICAL_DIRECTION_FACTOR;
+  Real HINT_DIRECTION_PENALTY;
   Real IDEAL_SLOPE_FACTOR;
   Real ROUND_TO_ZERO_SLOPE;
 
diff --git a/scm/define-grobs.scm b/scm/define-grobs.scm
index c4246b6589..91803b3afa 100644
--- a/scm/define-grobs.scm
+++ b/scm/define-grobs.scm
@@ -322,7 +322,8 @@
 	(beaming . ,ly:beam::calc-beaming)
 	(stencil . ,ly:beam::print)
 	(clip-edges . #t)
-	
+
+	(details .  ((hint-direction-penalty . 20)))
 	;; TODO: should be in SLT.
 	(thickness . 0.48) ; in staff-space
 	(neutral-direction . ,DOWN)
diff --git a/scm/layout-beam.scm b/scm/layout-beam.scm
index d946890049..1ee840aa0b 100644
--- a/scm/layout-beam.scm
+++ b/scm/layout-beam.scm
@@ -59,7 +59,7 @@
 	ly:beam::quanting
 	(check-beam-quant l r)
 	))
-			
+
 
 (define-public (check-slope-callbacks comparison)
   (list ly:beam::calc-least-squares-positions
-- 
2.39.5