% Feta (not the Font-En-Tja) music font -- draw flags
% This file is part of LilyPond, the GNU music typesetter.
%
% Copyright (C) 1997--2023 Han-Wen Nienhuys <hanwen@xs4all.nl>
%
% The LilyPond font is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version, or under the SIL Open Font License.
%
% LilyPond is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.

fet_begingroup ("flags");

save outer_path, stem_length;
path outer_path;

%
% Flags on a down stem overlap with the notehead (in x-direction), so
% they can be wider.
%

upflag_width# = .65 black_notehead_width# + stemthickness# / 2;
downflag_width# = .833 black_notehead_width# + stemthickness# / 2;

%
% Flags on a down stem cannot overlap with the notehead in y-direction,
% so they have less slant.
%
% Because of optical illusion, the utmost flag (bottom for
% stem-down, top for stem-up) should be smaller than the other
% flags.  Adobe Sonata doesn't do this correctly.  (Instead they have
% an extension flag, which looks less elegant.)
%

save hip_thickness, foot_thickness;

hip_thickness# = 1.0 linethickness# + .069 staff_space#;
foot_thickness# = 1.2055 linethickness# + .06 staff_space#;

define_pixels (hip_thickness, foot_thickness);

%
% Extend the stem a little bit.
%

save stem_extra;

stem_extra# = .5 staff_space#;

define_pixels (stem_extra);

%
% Inspired by Adobe Sonata and [Wanske].
% For example, see POSTSCRIPT Language -- program design,
% page 119, and [Wanske], p 41, 42.
%

% The overall size of a flag is given by `dims`; `hip_depth` and `foot_width`
% are factors that control the vertical position of the flag's 'hip' and the
% horizontal position of the flag's 'foot', respectively.
%
% This function sets `outer_path`, corresponding to the right-side outline of
% the flag.
def draw_flag (expr center, flare, dims, hip_depth, foot_wid,
                    hip_thickness, foot_thickness, show_labels) =
  save c;

  clearxy;

  c := .7;

  penpos1 (flare, 90);
  penpos2 (whatever, 0);

  x2r - x2l = hround (hip_thickness);

  penpos3 (foot_thickness, -20);

  z1r = center;
  z2r = center + (xpart (dims), -ypart (dims) * hip_depth);
  z3r = center + (xpart (dims) * foot_wid, -ypart (dims));

  x2r := hround x2r;
  y2r := vround y2r;

  outer_path := z3r{curl c}
                .. z2r{up}
                .. {up}z1r;

  if show_labels = true:
    penlabels (1, 2, 3);
  fi;

  fill z1l{curl 0}
       ..tension 1.1.. z2l{down}
       .. {curl c}simple_serif (z3l, z3r, 80)
       & outer_path
       -- cycle;
enddef;

%
% TODO: calculate intersectpoint (see TeX book, p. 137)
% TODO: calculate incision_depth
%

def add_flag (expr yoff, flare, hip_wid_multiplier, hip_dep_multiplier,
                   intersectpoint, hip_thickness, foot_thickness) =
  begingroup
    save prev_center, prev_xextreme, prev_yextreme;
    save rel_foot, ip, center, incision_depth;
    save prev_hipwid, prev_footdep, prev_hipdep, wid, dep, hip_dep;
    save hip_dep_ratio, foot_wid_ratio;
    pair prev_center, center, foot, prev_xextreme, prev_yextreme;
    pair ip, rel_foot;

    % Use `outer_path` of previous flag element to construct a new one.
    incision_depth = 1.013;
    prev_center = point 2 of outer_path;
    prev_xextreme = point 1 of outer_path;
    prev_yextreme = point 0 of outer_path;
    prev_hipwid = xpart (prev_xextreme - prev_center);
    prev_footdep = -ypart (prev_yextreme - prev_center);
    prev_hipdep = -ypart (prev_xextreme - prev_center);
    ip = point intersectpoint of outer_path;

    wid = prev_hipwid * hip_wid_multiplier;
    hip_dep = prev_hipdep * hip_dep_multiplier;

    center = prev_center + (0, yoff);
    rel_foot = incision_depth [(wid, hip_dep), ip - center];
    dep = -ypart (rel_foot);
    foot_wid_ratio = xpart (rel_foot) / wid;
    hip_dep_ratio = hip_dep / dep;

    draw_flag (center, flare, (wid, dep),
               hip_dep_ratio, foot_wid_ratio,
               hip_thickness, foot_thickness, false);

    stem_length := stem_length + ypart (center - prev_center);
  endgroup
enddef;


def upstemsingleflag (expr shortening) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  flagspace_factor := 1;

  flag_count = 1;
  total_depth# = (3 - shortening) * staff_space# - blot_diameter# / 2;
  flare = staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                foot_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemdoubleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .8;
  else:
    flagspace_factor := .85;
  fi;

  flag_count = 2;
  total_depth# = (3.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemtripleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .82;
  else:
    flagspace_factor := .87;
  fi;

  flag_count = 3;
  total_depth# = (4.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemquadrupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .87;
  else:
    flagspace_factor := .9;
  fi;

  flag_count = 4;
  total_depth# = (5.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.3,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemquintupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .9;
  else:
    flagspace_factor := .93;
  fi;

  flag_count = 5;
  total_depth# = (6.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.3,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemsextupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .93;
  else:
    flagspace_factor := .93;
  fi;

  flag_count = 6;
  total_depth# = (7.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.3,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemseptupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .93;
  else:
    flagspace_factor := .93;
  fi;

  flag_count = 7;
  total_depth# = (8.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.3,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def upstemoctupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .93;
  else:
    flagspace_factor := .93;
  fi;

  flag_count = 8;
  total_depth# = (9.25 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .85 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .72;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_width_ratio = .8;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, true);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .97, 1, 1.3,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.05, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));
enddef;


def downstemsingleflag (expr shortening) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  flagspace_factor := .9;

  flag_count = 1;
  total_depth# = (2.75 - shortening) * staff_space#;
  flare = staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .74;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .85;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2)

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemdoubleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .8;
  else:
    flagspace_factor := .9;
  fi;

  flag_count = 2;
  total_depth# = (3 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .82 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .85;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .95;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.25,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .95, 1, 1.25,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemtripleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .8;
  else:
    flagspace_factor := .88;
  fi;

  flag_count = 3;
  total_depth# = (3.75 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .82 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .87;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .965;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.22,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .95, 1.02, 1.22,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemquadrupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .75;
  else:
    flagspace_factor := .9;
  fi;

  flag_count = 4;
  total_depth# = (4.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .8 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .83;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .975;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.22,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1.1, 1.19,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .98, 1.08, 1.19,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .94, 1.04, 1.21,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemquintupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .78;
  else:
    flagspace_factor := .92;
  fi;

  flag_count = 5;
  total_depth# = (5.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .8 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .85;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .98;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1.1, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .98, 1.08, 1.185,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .97, 1.06, 1.2,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .93, 1.04, 1.22,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemsextupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .8;
  else:
    flagspace_factor := .94;
  fi;

  flag_count = 6;
  total_depth# = (6.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .8 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .86;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .98;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1.1, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .98, 1.08, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1.06, 1.2,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .97, 1.04, 1.2,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .93, 1, 1.2,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemseptupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .82;
  else:
    flagspace_factor := .95;
  fi;

  flag_count = 7;
  total_depth# = (7.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .8 staff_space;
  flagspace# = .flagspace_factor * staff_space#;
  hip_depth_ratio = .87;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .98;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1.1, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .98, 1.08, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1.06, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1.04, 1.2,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .97, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .93, 1, 1.22,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


def downstemoctupleflag (expr shortening, stacked) =
  save flare, hip_depth_ratio, hip_width, foot_depth, foot_width_ratio;
  save flagspace, total_depth, flag_count;
  save flagspace_factor;

  if stacked:
    flagspace_factor := .83;
  else:
    flagspace_factor := .96;
  fi;

  flag_count = 8;
  total_depth# = (8.5 - shortening) * staff_space# - blot_diameter# / 2;
  flare = .8 staff_space;
  flagspace# = flagspace_factor * staff_space#;
  hip_depth_ratio = .88;
  hip_width# = downflag_width# - hip_thickness# / 2;
  foot_width_ratio = .98;

  (flag_count - 1) * flagspace# + foot_depth# = total_depth#;

  define_pixels (hip_width, foot_depth);
  define_whole_vertical_pixels (flagspace);

  set_char_box (0, hip_width#,
                total_depth# + foot_thickness# / 2, stemthickness# / 2);

  stem_length := flare;

  draw_flag ((0, -(flag_count - 1) * flagspace),
             flare, (hip_width, foot_depth),
             hip_depth_ratio, foot_width_ratio,
             hip_thickness, foot_thickness, false);

  if stacked:
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
  else:
    add_flag (flagspace, flare, .98, 1.1, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, .98, 1.08, 1.185,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1.06, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1.04, 1.2,
              hip_thickness, foot_thickness);
    add_flag (flagspace, flare, 1, 1, 1.2,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .97, 1, 1.22,
              hip_thickness, foot_thickness);
    add_flag (.98 flagspace, flare, .93, 1, 1.22,
              hip_thickness, foot_thickness);
  fi;

  draw_square_block ((-.5 stemthickness_rounded, 0),
                     (0, vround (-stem_length - stem_extra)));

  y_mirror_char;
enddef;


fet_beginchar ("8th flag (up)", "u3");
  upstemsingleflag (0);
fet_endchar;


fet_beginchar ("16th flag (up)", "u4");
  upstemdoubleflag (0, false);
fet_endchar;


fet_beginchar ("32nd flag (up)", "u5");
  upstemtripleflag (0, false);
fet_endchar;


fet_beginchar ("64th flag (up)", "u6");
  upstemquadrupleflag (0, false);
fet_endchar;


fet_beginchar ("128th flag (up)", "u7");
  upstemquintupleflag (0, false);
fet_endchar;


fet_beginchar ("256th flag (up)", "u8");
  upstemsextupleflag (0, false);
fet_endchar;


fet_beginchar ("512th flag (up)", "u9");
  upstemseptupleflag (0, false);
fet_endchar;


fet_beginchar ("1024th flag (up)", "u10");
  upstemoctupleflag (0, false);
fet_endchar;


fet_beginchar ("8th flag (down)", "d3");
  downstemsingleflag (0);
fet_endchar;


fet_beginchar ("16th flag (down)", "d4");
  downstemdoubleflag (0, false);
fet_endchar;


fet_beginchar ("32nd flag (down)", "d5");
  downstemtripleflag (0, false);
fet_endchar;


fet_beginchar ("64th flag (down)", "d6");
  downstemquadrupleflag (0, false);
fet_endchar;


fet_beginchar ("128th flag (down)", "d7");
  downstemquintupleflag (0, false);
fet_endchar;


fet_beginchar ("256th flag (down)", "d8");
  downstemsextupleflag (0, false);
fet_endchar;


fet_beginchar ("512th flag (down)", "d9");
  downstemseptupleflag (0, false);
fet_endchar;


fet_beginchar ("1024th flag (down)", "d10");
  downstemoctupleflag (0, false);
fet_endchar;


fet_beginchar ("8th stacked flag (up)", "stackedu3");
  upstemsingleflag (.4);
fet_endchar;


fet_beginchar ("16th stacked flag (up)", "stackedu4");
  upstemdoubleflag (.4, true);
fet_endchar;


fet_beginchar ("32nd stacked flag (up)", "stackedu5");
  upstemtripleflag (.4, true);
fet_endchar;


fet_beginchar ("64th stacked flag (up)", "stackedu6");
  upstemquadrupleflag (.4, true);
fet_endchar;


fet_beginchar ("128th stacked flag (up)", "stackedu7");
  upstemquintupleflag (.4, true);
fet_endchar;


fet_beginchar ("256th stacked flag (up)", "stackedu8");
  upstemsextupleflag (.4, true);
fet_endchar;


fet_beginchar ("512th stacked flag (up)", "stackedu9");
  upstemseptupleflag (.4, true);
fet_endchar;


fet_beginchar ("1024th stacked flag (up)", "stackedu10");
  upstemoctupleflag (.4, true);
fet_endchar;


fet_beginchar ("8th stacked flag (down)", "stackedd3");
  downstemsingleflag (.4);
fet_endchar;


fet_beginchar ("16th stacked flag (down)", "stackedd4");
  downstemdoubleflag (.4, true);
fet_endchar;


fet_beginchar ("32nd stacked flag (down)", "stackedd5");
  downstemtripleflag (.4, true);
fet_endchar;


fet_beginchar ("64th stacked flag (down)", "stackedd6");
  downstemquadrupleflag (.4, true);
fet_endchar;


fet_beginchar ("128th stacked flag (down)", "stackedd7");
  downstemquintupleflag (.5, true);
fet_endchar;


fet_beginchar ("256th stacked flag (down)", "stackedd8");
  downstemsextupleflag (.6, true);
fet_endchar;


fet_beginchar ("512th stacked flag (down)", "stackedd9");
  downstemseptupleflag (.7, true);
fet_endchar;


fet_beginchar ("1024th stacked flag (down)", "stackedd10");
  downstemoctupleflag (.8, true);
fet_endchar;


%%%%%%%%
%
% Single Stroke for Short Appoggiatura
%

fet_beginchar ("grace dash (up)", "ugrace");
  save flare, hip_depth_ratio, hip_width, foot_depth;

  hip_depth_ratio = .72;
  flare# = staff_space#;
  hip_width# = upflag_width# - hip_thickness# / 2;
  foot_depth# = 3 staff_space#;

  define_pixels (hip_width, foot_depth);

  set_char_box (hip_width# * hip_depth_ratio, hip_width#,
                foot_depth# * hip_depth_ratio, -flare#)

  pickup pencircle scaled 1.5 stemthickness;

  z1 = (-b, -d);
  z2 = (w, h);

  penpos1 (1.5 stemthickness, angle (z2 - z1) - 90);
  penpos2 (1.5 stemthickness, angle (z2 - z1) - 90);

  fill z1l
       -- z2l
       .. top z2
       .. rt z2
       .. z2r
       -- z1r
       .. bot z1
       .. lft z1
       .. cycle;

  penlabels (1, 2);
fet_endchar;


fet_beginchar ("grace dash (down)", "dgrace");
  save flare, hip_depth_ratio, hip_width, foot_depth;
  save total_depth;

  hip_depth_ratio = .72 ;
  flare# = .99 staff_space#;
  hip_width# = downflag_width# - hip_thickness# / 2;
  total_depth# = 2.85 staff_space#;
  foot_depth# = total_depth#;
  foot_width_ratio = .8;

  define_pixels (hip_width, foot_depth);

  set_char_box (hip_width# * hip_depth_ratio, hip_width#,
                foot_depth# * hip_depth_ratio, -flare#)

  pickup pencircle scaled 1.5 stemthickness;

  z1 = (-b, -d);
  z2 = (w, h);

  penpos1 (1.5 stemthickness, angle (z2 - z1) - 90);
  penpos2 (1.5 stemthickness, angle (z2 - z1) - 90);

  fill z1l
       -- z2l
       .. top z2
       .. rt z2
       .. z2r
       -- z1r
       .. bot z1
       .. lft z1
       .. cycle;

  y_mirror_char;
fet_endchar;

fet_endgroup ("flags");
