% bzrsetup.mf: interesting and/or necessary things for bzrto's Metafont output.
% 
% Copyright (C) 1992 Free Software Foundation, Inc.
%
% This program 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 2, or (at your option)
% any later version.
%
% This program 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 this program; if not, write to the Free Software
% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.


% |true_design_size| must be set before this file is input.  It is
% supposed to be the design size of the original font.  If the user does
% not set |designsize| on the command line, we default to the following.
default_design_size# := 10pt#;
if designsize = 0: designsize := default_design_size#; fi;

% It's pointless to actually produce different TFM files for different
% design sizes: we have only one set of outlines, after all.  So we
% adjust the resolution to account for different sizes, by setting
% |mag|.  Then we reset |designsize| to the default.
if unknown mag: mag := 1; fi;
mag := mag * designsize / default_design_size#;

% So we can produce output at varying sizes, all our dimensions are
% output in terms of the ad hoc parameter |u|.
% 
u# := default_design_size# / true_design_size#;

designsize := default_design_size#;

% Macros from page 289 of {\sl The Metafontbook} for short-circuit evaluation.
% 
def cand (text q) = startif true q else: false fi enddef;
def cor (text q) = startif true true else: q fi enddef;
tertiarydef p startif true = if p: enddef;

% The proof resolution of 2601.72 pixels per inch established by plain
% \MF\ does not always work, because the characters might be too large
% (or small, for that matter) for the paper size.  So, here we change
% the proof resolution to fit the design size of the font into
% |proof_size| inches.  To do this, we must know the resolution of the
% device the proof output will be printed on; we make |proof_resolution|
% default to 300.
% 
if unknown mode cor ((mode = proof) or (mode = smoke)):
  % Given in inches:
  if unknown proof_size: proof_size := 7; fi;
  
  % Given in pixels per inch:
  if unknown proof_resolution: proof_resolution := 300; fi;
  
  % Set up the new mode to be the same as the old one, except for the
  % resolution.
  original_mode := if known mode: mode else: proof fi;
  mode_def bzr_proof_mode =
    if original_mode = proof:
      proof_;
    else:
      smoke_;
    fi;
    pixels_per_inch
      := (proof_size * proof_resolution)
         * (72.27 / proof_resolution)
         * (72.27 / designsize);
  enddef;
  mode := bzr_proof_mode;
fi;

% Set up the device values for this run.  Since we allow a nonstandard
% value of |proofing| (see below), we must save its value, if it has
% already been assigned.
%
if (known proofing) cand (proofing > 2):
  save_proofing := proofing;
fi

mode_setup;

if known save_proofing:
  proofing := save_proofing;
fi;


% We can't define the pixel-oriented version of |u| until after
% |mode_setup| has been called.
define_pixels (u);


% Use a font that blends better with the \MF\ logo for the title line.
% 
special "titlefont cmss8";

% If we are doing smoke proofs, put the character at its actual size on
% the output; if we're doing gray proofs, use a smaller font for the
% labels.
% 
if proofing <= 1:
  special "labelfont " & jobname;
  extra_beginchar := extra_beginchar &
    "makelabel.lft.nodot (decimal charcode, (w + 1in#, 0));";
else:
  special "labelfont cmtt8";
fi;

% Produce a |makelabel| command for each |z| point in |point_list| and
% each suffix in |suffixes|.
% 
def general_label (text point_list, suffixes) =
  forsuffixes point_suffix = ,suffixes:
    forsuffixes point = point_list:
      % Unfortunately, the way in which the |str| operator formats its
      % argument (which is an arbitrary suffix) cannot be changed, so
      % the proofsheets end up showing "0 0" for the variable |z[0][0]|.
      makelabel (str point.point_suffix, z.point.point_suffix);
    endfor
  endfor
enddef;

% Define our labels for the proofsheets.  The person running Metafont
% can also see the control points on each spline, by setting
% |proofing>2| at the beginning.  (The {\tt plain.mf} macros only
% distinguish between |proofing=0|, |proofing>0| and |proofing>1|, so we
% are not disturbing them.)
% 
def proof_labels (text t) =
  % We need to delimit the arguments here, so \MF\ knows where the
  % second |text| argument begins.  Our output uses the suffix |"s"| for
  % startpoints of segments, and the suffixes |"c1"| and |"c2"| for
  % control points.
  if proofing > 1: general_label (t) (s); fi;
  if proofing > 2: general_label (t) (c1,c2); fi;
enddef;


% We define our characters as filled outlines; page 206 of {\sl The
% Metafontbook} suggests disabling \MF's |autorounding| and
% |smoothing| features in that case.
% 
autorounding := 0;
smoothing := 0;

% Sometimes we want to fill our paths, other times unfill them,
% depending on whether the winding number is positive or negative.  We
% could be absolutely safe here, and declare |temp_path| inside a group,
% at every invocation of |fill_or_unfill|; but this would waste time and
% space to no advantage, since we know exactly what kind of \MF\
% programs we output, and the variable |temp_path| is not used in them.
% 
% Incidentally, we need a variable in the first place only because it is more
% efficient to avoid evaluating long paths more than once.
% 
path temp_path;

def fill_or_unfill expr p =
  temp_path := p;
  if turningnumber temp_path > 0: fill else: unfill fi temp_path;
enddef;

% Since we can assume the font fills all cycles counterclockwise
% (because PostScript requires this), we want to disable the
% |turningnumber| checking which generates the `Strange path' errors.
% See p.112 and p.119 of the Metafontbook.
% 
% (By the way, setting |turningcheck := 0| makes Metafont always use the
% filling rule for positive turning number, i.e., always |fill| and
% never |unfill|.  When would this be useful?)
% 
turningcheck := 1;

% When we don't know the dimensions of the character we're defining
% before we define it, we have to use these to begin it:
def begin_no_dimen_char (expr c) =
  begingroup
  transform save_currenttransform;
  save_currenttransform := currenttransform;
  charcode:=if known c: byte c else: 0 fi;
  charic:=0; clearxy; clearit; clearpen; scantokens extra_beginchar;
enddef;

def end_no_dimen_char =
  currenttransform := save_currenttransform;
  endchar;
enddef;

% When use the above to start a character, must call this before `endchar'.
% 
def set_char_dimens (expr w_sharp,h_sharp,d_sharp) =
 charwd:=w_sharp;      charht:=h_sharp;       chardp:=d_sharp;
 w:=hround(charwd*hppp); h:=vround(charht*hppp); d:=vround(chardp*hppp);
enddef;

% Move by shifting the current transform matrix.
% 
def move (expr h_amount, v_amount) =
  currenttransform := currenttransform shifted (h_amount, v_amount);
enddef;

def hmove (expr amount) = move (amount, 0); enddef;
def vmove (expr amount) = move (0, amount); enddef;
