! draw_glyph.MBS
! Draw a Hershey glyph, optionally in a box.
! Everything else is just a way of getting here.

local drawing module draw_glyph(
    int    glyph_number;
    int    x;   ! origin, in center of box
    int    y;
    string shortname*128;
    int    box_radius;      ! half-width of bounding box
    int    box_visibility;  ! 0 = plain = invisible (no boxes); 
                            ! 1 = boxed = visible
    int    numpairs;
var int    pairs(300);
    int    verbosity);

int ix, iy;
int bv;
int xmin, xmax;
int glyph_number_width;

beginmodule

! box_visibility is the inverse of the blank attribute
if box_visibility = 1 then
   bv := 0;
else 
   bv := 1;
endif;

! optionally, draw the surrounding box
if box_visibility = 1 then
   
   ! horizontal lines of bounding box
   for iy := (y - (box_radius + 1)) to (y + (box_radius + 1)) 
       step ((box_radius * 2) + 2) do
      for ix := (x - box_radius) to (x + box_radius) do
         lin_free (#10, vec(ix, iy - 0.1),vec(ix, iy + 0.1): blank=bv);
         if (trunc(ix/10) = (ix/10)) then
            lin_free(#11, vec(ix, iy - 0.5), vec(ix, iy + 0.5): blank=bv);
         endif;
         if (ix = 0) then
            lin_free(#12, vec(ix, iy - 1.0), vec(ix, iy + 1.0): blank=bv);
         endif;
      endfor;
   endfor;
   
   ! vertical lines of bounding box
   for ix := (x - (box_radius + 1)) to (x + (box_radius + 1)) 
       step ((box_radius * 2) + 2) do
      for iy := (y - box_radius) to (y + box_radius) do
         lin_free (#20, vec(ix - 0.1, iy),vec(ix + 0.1, iy): blank=bv);
         if (trunc(iy/10) = (iy/10)) then
            lin_free(#21, vec(ix - 0.5, iy), vec(ix + 0.5, iy): blank=bv);
         endif;
         if (iy = 0) then
            lin_free(#22, vec(ix - 1.0, iy), vec(ix + 1.0, iy): blank=bv);
         endif;
      endfor;
   endfor;

   ! label it
   ! there must be a better way
   if glyph_number <= 9 then
      glyph_number_width := 1;
   elif glyph_number <= 99 then
      glyph_number_width := 2;
   elif glyph_number <= 999 then
      glyph_number_width := 3;
   else
      glyph_number_width := 4;
   endif;
   text(#30,vec((x - box_radius), (y + box_radius - 2)),0,
                str(glyph_number,glyph_number_width,0): tsize=2, blank=bv);
   text(#31,vec((x - (textl(shortname)/2)), (y - (box_radius + 10))),0,
            shortname: blank=bv);

   ! indicate the glyph width
   xmin := x + pairs(1) - ascii("R");
   xmax := x + pairs(2) - ascii("R");
   lin_free(#40, vec(xmin,y - 1), vec(xmin,y+1): pen=2);
   lin_free(#41, vec(xmax,y - 1), vec(xmax,y+1): pen=2);

endif; ! box_visibility

! draw the glyph
! trace out the Hershey polylines in the Hurt encoding
ix := 3;
drawloop:
   if (pairs(ix + 2) - ascii("R") = -50) and 
      (pairs(ix + 3) - ascii("R") =   0) then

      ix := ix + 2;

   else
      lin_free(#50,vec(x + pairs(ix + 0) - ascii("R"),
                        y - (pairs(ix + 1) - ascii("R"))),
                   vec(x + pairs(ix + 2) - ascii("R"),
                        y -  (pairs(ix + 3) - ascii("R"))));
   endif;

if (ix < ((numpairs - 1) * 2) - 2) then
   ix := ix + 2;
   goto drawloop;
endif;  ! draw the glyph


endmodule

! Copyright 2003, 2004 by David M. MacMillan

! This work 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 of the License, or
! (at your option) any later version.

! NOTICE OF DISCLAIMER OF WARRANTY AND LIABILITY:

! This work is distributed 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.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for damages,
! including but not limited to any general, special, incidental
! or consequential damages arising out of your use of or inability to use this
! work or the information contained in it, even if you have been advised
! of the possibility of such damages.

! In no event will the author(s), editor(s), or publisher(s) of this work
! be liable to you or to any other party for any injury, death,
! disfigurement, or other personal damage arising out of your use of
! or inability to use this work or the information
! contained in it, even if you have been advised of the
! possibility of such injury, death, disfigurement, or other
! personal damage.

! You should have received a copy of the GNU General Public License
! along with this work; if not, write to the
! Free Software Foundation, Inc., 59 Temple Place - Suite 330,
! Boston, MA  02111-1307, USA.

