# include "Ximage.h"

void PositionPicture (Layout *layout) {

  int fontsize, bump, Nc;
  double PADx, PADy, Dx, Dy;
  double PXm, PXp, PYm, PYp;
  double X0, Y0, dX, dY;
  char string[64], *fontname;

  fontname = GetRotFont (&fontsize);

  /* window has outer bounding box with margins Wx, Wy 
     section commands subdivide the internal box (Dx,Dy)
   */

  PADx = MAX (graphic.dx / 20.0, fontsize);
  PADy = MAX (graphic.dy / 20.0, fontsize);
  Dx = graphic.dx - 2*PADx;
  Dy = graphic.dy - 2*PADy;

  /* each graph has a padding PXm, PXp, PYm, PYp */
  PXm = (layout[0].axis[1].islabel) ? 4*fontsize : 0;
  PXp = (layout[0].axis[3].islabel) ? 4*fontsize : 0;
  PYm = (layout[0].axis[0].islabel) ? 4*fontsize : 0;
  PYp = (layout[0].axis[2].islabel) ? 4*fontsize : 0;
 
  X0 = PADx + PXm + (Dx * layout[0].x);
  Y0 = PADy + PYm + (Dy * layout[0].y);
  dX = (Dx * layout[0].dx) - PXp - PXm;
  dY = (Dy * layout[0].dy) - PYp - PYm;

  /* define locations of coordinate axes */
  layout[0].axis[0].fx  = X0;
  layout[0].axis[0].fy  = graphic.dy - Y0;
  layout[0].axis[0].dfx = dX;
  layout[0].axis[0].dfy = 0;
  
  layout[0].axis[1].fx  = X0;
  layout[0].axis[1].fy  = graphic.dy - Y0;
  layout[0].axis[1].dfx = 0;
  layout[0].axis[1].dfy = -dY;

  layout[0].axis[2].fx  = X0;
  layout[0].axis[2].fy  = graphic.dy - Y0 - dY;
  layout[0].axis[2].dfx = dX;
  layout[0].axis[2].dfy = 0;

  layout[0].axis[3].fx  = X0 + dX;
  layout[0].axis[3].fy  = graphic.dy - Y0;
  layout[0].axis[3].dfx = 0;
  layout[0].axis[3].dfy = -dY;

  PADx = 0.8*fontsize + 2;
  PADy = 3.0*fontsize + 4;

  /* define locations of axis labels */
  layout[0].label[LABELX0].x = layout[0].axis[0].fx + 0.5*layout[0].axis[0].dfx;
  bump = (layout[0].axis[0].islabel) ? PADy : PADx;
  layout[0].label[LABELX0].y = layout[0].axis[0].fy + bump;

  layout[0].label[LABELX1].x = layout[0].axis[2].fx + 0.5*layout[0].axis[2].dfx;
  bump = (layout[0].axis[2].islabel) ? PADy : PADx;
  layout[0].label[LABELX1].y = layout[0].axis[2].fy - bump;

  sprintf (string, "%4g", layout[0].axis[1].min);
  Nc = strlen (string);
  sprintf (string, "%4g", layout[0].axis[1].max);
  Nc = MAX (Nc, strlen (string));

  layout[0].label[LABELY0].y = layout[0].axis[1].fy + 0.5*layout[0].axis[1].dfy;
  bump = (layout[0].axis[1].islabel) ? (0.8*Nc*fontsize + 1) : PADx;
  layout[0].label[LABELY0].x = layout[0].axis[1].fx - bump;

  sprintf (string, "%4g", layout[0].axis[1].min);
  Nc = strlen (string);
  sprintf (string, "%4g", layout[0].axis[1].max);
  Nc = MAX (Nc, strlen (string));

  layout[0].label[LABELY1].y = layout[0].axis[3].fy + 0.5*layout[0].axis[3].dfy;
  bump = (layout[0].axis[3].islabel) ? (0.8*Nc*fontsize + 1) : PADx;
  layout[0].label[LABELY1].x = layout[0].axis[3].fx + bump;
  
  /* these are wrong and have to be adjusted to sit in the corners */

  layout[0].label[LABELUL].x = layout[0].axis[2].fx - PADx;
  layout[0].label[LABELUL].y = layout[0].axis[2].fy - PADx;
  layout[0].label[LABELUR].x = layout[0].axis[2].fx + layout[0].axis[2].dfx + PADx;
  layout[0].label[LABELUR].y = layout[0].axis[2].fy - PADx;
  layout[0].label[LABELLL].x = layout[0].axis[0].fx - PADx;
  layout[0].label[LABELLL].y = layout[0].axis[0].fy + PADx;
  layout[0].label[LABELLR].x = layout[0].axis[0].fx + layout[0].axis[0].dfx + PADx;
  layout[0].label[LABELLR].y = layout[0].axis[0].fy + PADx;

}
