1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/* utils.c
* Functions useful across the different modules of
* drawing and non-drawing code.
*
* for Denemo, a gtk+ frontend to GNU Lilypond
* (c) 1999, 2000, 2001 Matthew Hiller
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include "accwidths.h"
#include "datastructures.h"
#include "notewidths.h"
#include "utils.h"
void
drawbitmapinverse (GdkPixmap * pixmap, GdkGC * gc, GdkBitmap * mask, gint x,
gint y, gint width, gint height)
{
gdk_gc_set_clip_mask (gc, mask);
gdk_gc_set_clip_origin (gc, x, y);
gdk_draw_rectangle (pixmap, gc, TRUE, x, y, width, height);
gdk_gc_set_clip_mask (gc, NULL); /* Removes clip mask */
}
void
set_tuplefied_numticks (mudelaobject * theobj, gint numerator,
gint denominator)
{
theobj->durinticks = theobj->basic_durinticks * numerator / denominator;
/* Though WHOLENUMTICKS is chosen strategically so that in common
* cases, this division works out evenly, things should also work
* out properly if it doesn't; i.e., durinticks_untupletized is
* rounded down */
}
void
set_grace_numticks (mudelaobject * theobj, gint multiplier)
{
theobj->durinticks = theobj->basic_durinticks / multiplier;
}
void
set_basic_numticks (mudelaobject * theobj)
{
gint power;
gint withoutdots;
gint addperdot, i;
switch (theobj->type)
{
case CHORD:
power = 1 << theobj->u.chordval.baseduration;
withoutdots = WHOLE_NUMTICKS / power;
addperdot = withoutdots / 2;
theobj->basic_durinticks = withoutdots;
for (i = 0; i < theobj->u.chordval.numdots; addperdot /= 2, i++)
theobj->basic_durinticks += addperdot;
break;
default:
theobj->basic_durinticks = 0;
theobj->durinticks = 0;
/* There's no reason not to set that as well */
break;
}
}
/* Returns the amount of space to be left after a note or rest, only
* taking the width of the measure into consideration */
gint
space_after (gint numticks, gint wholenotewidth)
{
return MAX (numticks * wholenotewidth / WHOLE_NUMTICKS, 0);
}
#define EXTRAWIDTH 5
/* Sets the minimum space that needs to be allocated for drawing a mudela
* object */
void
setpixelmin (mudelaobject * theobj)
{
gint i, baseduration, headtype;
chord chordval;
GList *tnode;
note *thetone;
/* And these static declaration are copied right out of drawnotes.c
* and drawaccidentals.c */
switch (theobj->type)
{
case CHORD:
chordval = theobj->u.chordval;
baseduration = chordval.baseduration;
headtype = MIN (baseduration, 2);
if (chordval.tones)
{
/* if (theobj->isstart_beamgroup && theobj->isend_beamgroup &&
chordval.is_stemup)
theobj->minpixelsalloted = headwidths[headtype] + STEM_WIDTH;
else
theobj->minpixelsalloted = headwidths[headtype]; */
/* We can get away with that because the stems are narrower
* than even the narrowest notes; upstemmed notes are
* the unusual case here */
/* The above code will allow extra space for the stem of
* stemup notes. It's commented out 'cause we no longer want
* that behavior */
theobj->minpixelsalloted = headwidths[headtype];
}
else /* a rest */
theobj->minpixelsalloted = restwidths[baseduration];
/* 12 pixels for the first dot, 6 for each dot thereafter */
if (chordval.numdots)
theobj->minpixelsalloted += 6;
for (i = 0; i < chordval.numdots; i++)
theobj->minpixelsalloted += 6;
theobj->space_before = 0;
if (chordval.hasanacc)
for (tnode = chordval.tones; tnode; tnode = tnode->next)
{
thetone = tnode->data;
if (thetone->showaccidental)
theobj->space_before =
MAX (theobj->space_before, thetone->position_of_accidental);
}
if (chordval.is_reversealigned)
if (chordval.is_stemup)
theobj->minpixelsalloted += headwidths[headtype];
else if (!chordval.hasanacc)
/* Accidental positioning already accounts for the extra leading
space that we need for reverse-aligned noteheads, provided
the chord has an accidental in it somewhere. We only have to
remark upon noteheads to the left of the stem if there weren't
any accidentals to position. */
theobj->space_before += headwidths[headtype];
theobj->minpixelsalloted += EXTRAWIDTH;
break;
case TUPOPEN:
case TUPCLOSE:
/* The real way do this will be with a gdk_string_width. Until
* then, though: */
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
case CLEF:
theobj->minpixelsalloted = 35;
theobj->space_before = 0;
break;
case KEYSIG:
theobj->space_before = 0;
break;
case TIMESIG:
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
case STEMDIRECTIVE:
/* The real way do this will be with a gdk_string_width. Until
* then, though: */
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
case LILYDIRECTIVE:
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
case DYNAMIC:
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
case GRACE_START:
case GRACE_END:
theobj->minpixelsalloted = 40;
theobj->space_before = 0;
break;
default:
break;
}
}
/* Height of a tone based on its mid_c_offset and the clef that it's in */
gint calculateheight (gint mid_c_offset, gint dclef)
{
switch (dclef)
{
case TREBLE:
return 5 * LINE_SPACE - HALF_LINE_SPACE * mid_c_offset;
break; /* Probably gratuitous */
case ALTO:
return 2 * LINE_SPACE - HALF_LINE_SPACE * mid_c_offset;
break;
case G_8:
return LINE_SPACE - HALF_LINE_SPACE * (mid_c_offset - 1);
break;
case BASS:
return -LINE_SPACE - HALF_LINE_SPACE * mid_c_offset;
break;
case TENOR:
return LINE_SPACE - HALF_LINE_SPACE * mid_c_offset;
break;
case SOPRANO:
return LINE_SPACE - HALF_LINE_SPACE * (mid_c_offset - 6);
break;
}
return (0);
}
gint offsettonumber (gint n)
{
if (n >= 0)
return n % 7;
else
return (7 - (-n % 7)) % 7;
/* Not all C implementations conform to the more recent standard on how %
should operate on negative operands. */
}
gchar mid_c_offsettoname (gint mid_c_offset)
{
int otn = offsettonumber (mid_c_offset);
return ((otn + 2) % 7) + 'a';
}
gint mid_c_offsettooctave (gint mid_c_offset)
{
if (mid_c_offset < 0)
return -((-mid_c_offset + 6) / 7) + 1;
else
return (mid_c_offset / 7) + 1;
}
void
freeit (gpointer data, gpointer user_data)
{
g_free (data);
}
|