<%@ language="javascript" %>
<%
/*  $Revision$
    $Id$

    File:               Print.asp
 */
%>
<!-- #INCLUDE FILE="../shared/common.inc" -->
<!-- #INCLUDE FILE="./SLNKDWF.inc" -->
<%
    var paperSizeX  = parseFloat(_getParam("paperSizeX", 165));
    var paperSizeY  = parseFloat(_getParam("paperSizeY", 260));
    var mustrotate  = _getParam("mustrotate", 0) == 1; // Moet de bitmap echt geroteerd worden
    var sizeX       = parseFloat(_getParam("sizeX", 165));
    var sizeY       = parseFloat(_getParam("sizeY", 260));
    var centerX     = parseFloat(_getParam("centerX", 0));
    var centerY     = parseFloat(_getParam("centerY", 0));
    var dwgScale    = parseFloat(_getParam("dwgScale", 0));
    var forceBW     = parseInt(_getParam("forceBW", 0));
    var sheetsX     = parseInt(_getParam("sheetsX", 1));
    var sheetsY     = parseInt(_getParam("sheetsY", 1));
    var img         = _getParam('img');
%>
<html>
<head>
  <style type="text/css">
    html { margin:0; padding:0; border-style: none; }
    body { margin:0; padding:0; border-style: none; }
    img  { margin:0; padding:0; border-style: none; }
    .noprint { display: none; }
    @media screen
    {
        table { border: 3px solid #999; zoom: 34%; }
        td { border: 1px solid #f0f0f0; }
        .noprint { display: block;
                   color: #888;
                   font-family: Verdana;
                   font-size: 0.75em;
                 }
    }
  </style>
</head>
<body onload="print()">
<%
function _RotatePoint(pt, rot, center)
{
    pt.x -= center.x;
    pt.y -= center.y;
    var newx = pt.x * Math.cos(rot) - pt.y * Math.sin(rot);
    var newy = pt.y * Math.cos(rot) + pt.x * Math.sin(rot);
    return { x : newx + center.x, y : newy + center.y };
}

function onepage(psx, sx, sy, cx, cy, scl)
{
       var tiles = 1 + Math.floor(Math.max(sx, sy) / 2000); // max 2000px per tile
       if (mustrotate)
            Response.Write("<p class='noprint'>height="+psx+"mm width="+Math.round(psx/sx*sy)+"mm </p>");
       else
            Response.Write("<p class='noprint'>height="+Math.round(psx/sx*sy)+"mm width=" + psx +"mm </p>");

       Response.Write("\n<!-- psx="+psx+", sx="+sx+", sy="+sy
                      +",\n      cx="+cx+", cy="+cy
                      +",\n      scl="+scl +"-->");
       Response.Write("\n<table cellpadding='0' cellspacing='0' border='0' style='border-collapse: collapse;'>");
       for (yy = 0; yy < tiles; yy++)
       {
          Response.Write("\n          <tr>");
          for (xx = 0; xx < tiles; xx++)
          {
            var tsx = (sx / tiles);
            var tsy = (sy / tiles);
            if (mustrotate)
            {
                var center = { x: (cx - scl * (0.5 + yy - tiles/2) * tsx),
                               y: (cy - scl * (0.5 + xx - tiles/2) * tsy)
                             };
            }
            else
            {
                var center = { x: (cx + scl * (0.5 + xx - tiles/2) * tsx),
                               y: (cy - scl * (0.5 + yy - tiles/2) * tsy)
                             };
            }
            //center = _RotatePoint(center, -90, { x: cx, y: cy});
            url = img
                   + "&sizeX=" + Math.round(tsx)
                   + "&sizeY=" + Math.round(tsy) // Pixels
                   + "&centerX=" + center.x
                   + "&centerY=" + center.y
                   + "&dwgScale=" + scl;
            if (mustrotate)
                url += "&rotation=90";

            if (forceBW)
                url += "&forceBW=1";
            %><td>
                <img alt=""
                    style="<%=mustrotate?"height":"width"%>:<%=toJsNumber(psx / tiles)%>mm; border:0; padding:0; margin:0;"
                    src="<%=safe.htmlattr(url)%>"></td>
          <%
          }
          Response.Write("</tr>");
      }
      Response.Write("</table>");
}
   for (sheetY = 0; sheetY < sheetsY; sheetY++)
   {
      for (sheetX = 0; sheetX < sheetsX; sheetX++)
      {
        if (sheetY > 0 || sheetX > 0)
            Response.Write("\n\n\n<div style='page-break-before:always'>&#160;</div>");
        onepage(paperSizeX / sheetsX, sizeX / sheetsX, sizeY / sheetsY,
                centerX + dwgScale * (0.5 + sheetX - sheetsX / 2) * sizeX / sheetsX,
                centerY - dwgScale * (0.5 + sheetY - sheetsY / 2) * sizeY / sheetsY,
                dwgScale);
      }
   }
%>
</body>
</html>
