#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define integer long
#include "dvi2html.h"

static char *font[256];
static int   size[256];
static int    mag[256];
static int  topmargin = 150;
static int  leftmargin = 106;
static int  curfont = -1;

static int scalex(int x)
{
  return (x/440)+leftmargin;
}

static int scaley(int y)
{
  return (y/400)+topmargin;
}

void DRAW_startjob(void)
{
  fprintf(stdout, "<HTML>");
  fprintf(stdout, "<HEAD>");
  fprintf(stdout, "</HEAD>");
  fprintf(stdout, "<BODY>");
}

static int thisfont(char *f)
{
//fprintf(stderr,"'%s' vs %d '%s'?\n", f, curfont, font[curfont]);
   return (curfont >= 0) && (font[curfont] != NULL) && (strcmp(font[curfont], f) == 0);
}

static void HTML_onechar(int c)
{
  if (c == '<') fprintf(stdout, "&lt;");
  else if (c == '&') fprintf(stdout, "&amp;");
  else if (c == '\'') fprintf(stdout, "&rsquo;");
  else if (c == '`') fprintf(stdout, "&lsquo;");
  else {
      // fonst-specific tweaks can go here...
    fputc(c, stdout);
  }
}
static void HTML_char(int c)
{

  if (thisfont("caecilia")) {
  } else if (thisfont("caeciliab")) {
    fprintf(stdout, "<B>");
  } else if (thisfont("caeciliai")) {
    fprintf(stdout, "<I>");
  } else if (thisfont("caeciliabi")) {
    fprintf(stdout, "<B><I>");
  }
  HTML_onechar(c);
  if (thisfont("caecilia")) {
  } else if (thisfont("caeciliab")) {
    fprintf(stdout, "</B>");
  } else if (thisfont("caeciliai")) {
    fprintf(stdout, "</I>");
  } else if (thisfont("caeciliabi")) {
    fprintf(stdout, "</I></B>");
  }
}

static void HTML_output(char *s)
{
  if (thisfont("caecilia")) {
  } else if (thisfont("caeciliab")) {
    fprintf(stdout, "<B>");
  } else if (thisfont("caeciliai")) {
    fprintf(stdout, "<I>");
  } else if (thisfont("caeciliabi")) {
    fprintf(stdout, "<B><I>");
  }
  for (;;) {
    int c = *s++;
    if (c == '\0') break;
    HTML_onechar(c);
  }
  if (thisfont("caecilia")) {
  } else if (thisfont("caeciliab")) {
    fprintf(stdout, "</B>");
  } else if (thisfont("caeciliai")) {
    fprintf(stdout, "</I>");
  } else if (thisfont("caeciliabi")) {
    fprintf(stdout, "</I></B>");
  }
}

void DRAW_text(int oldhh, int oldvv, int hh_plus_font_charwidth, int vv, char *text)
{
  // fprintf(stdout, "text: %d,%d %d %d %s\n", oldhh, oldvv, hh_plus_font_charwidth, vv, text);
  if (strchr(text, ' ') == NULL) {// not stretchable?
    fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx\">", scaley(oldvv), scalex(oldhh));
  } else { // WE SHOULDN'T GET THESE ON THE KINDLE. IT DOESN'T SUPPORT JUSTIFICATION.
    fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx; width: %dpx;"
                    " text-align: justify; text-justify: distribute; text-align-last: justify\">",
                    scaley(oldvv), scalex(oldhh), scalex(hh_plus_font_charwidth-oldhh)/*-8*/);
  }
  HTML_output(text);
  fprintf(stdout, "</DIV\n>");
}

void DRAW_selfont(int p)
{
   int x = size[p];
// int x = size[p]*mag[p]/1590;

  if (p >> 8) fprintf(stderr, "BAD FONT NUMBER %d\n", p);
  // fprintf(stderr, "sel font: %d ('%s' ?)\n", p, font[p]);
  fprintf(stdout, "<!-- selfont: %d (%s at %dpts magstep %d)\n-->",
                                  p, font[p], size[p], mag[p]);

  fprintf(stdout, "</font><font face=\"Courier New\" size=%d>",
#ifdef NEVER
      x < 75 ? 1
    : x < 82 ? 2
    : x < 84 ? 3
    : x < 93 ? 4
    : x < 109 ? 5
    : x < 130 ? 6
    : 7);
#else

// 122 1
// 129 2
// 137 2
// 152 3
// 175 4
// 206 5
// 229 6
// 282 7

      x < 125 ? 1
    : x < 144 ? 2
    : x < 163 ? 3
    : x < 190 ? 4
    : x < 212 ? 5
    : x < 254 ? 6
    : 7);

#endif
  curfont = p;
}

void DRAW_deffont(int p, char *font_name, int font_size, int scale)
{
  if (p >> 8) fprintf(stderr, "BAD FONT NUMBER %d\n", p);
  // fprintf(stderr, "def font[%d] as '%s'\n", p, font_name);
  font[p] = strdup(font_name);
  size[p] = font_size;
  mag[p] = scale;
  fprintf(stdout, "<!-- deffont: %d %s %d %d\n-->", p, font_name, font_size, scale);
}

void DRAW_startpage(void)
{
  fprintf(stdout, "<font face=\"Courier New\" size=\"1\">");
  fprintf(stdout, "<!-- startpage: -->\n");
}

void DRAW_char(int hh, int vv, int p)
{
  // fprintf(stdout, "char: %d,%d %c\n", hh, vv, p);
  fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx\"\n>", scaley(vv), scalex(hh));

  // THESE ARE NOT SUPPORTED ON THE KINDLE

#ifdef NEVER
  if (p == 28) {
    HTML_output("fi");
  } else if (p == 29) {
    HTML_output("fl");
  } else if (p == 30) {
    HTML_output("ffi");
  } else if (p == 27) {
    HTML_output("ff");
  } else if (p == 15) {
    fprintf(stdout, "&bull;");
  } else if (p == 16) {
    fprintf(stdout, "&ldquo;");
  } else if (p == 17) {
    fprintf(stdout, "&rdquo;");
  } else if (p == 10) {
    fprintf(stdout, "&Omega;");
  } else if (p == 21) {
    fprintf(stdout, "&ndash;");
  } else if (p == 22) {
    fprintf(stdout, "&mdash;");
  } else if (p == 2) {
    fprintf(stdout, "&times;");
  } else {
    fprintf(stdout, "<FONT SIZE=-2><SUP>%d</SUP></FONT>", p); // Debugging aid
  }
#endif
  fprintf(stdout, "BUG!");
  fprintf(stdout, "</DIV>");
}

void DRAW_endpage(int page_width, int page_height, long *page_counters)
{
  fprintf(stdout, "</font>");
  fprintf(stdout, "<!-- endpage: %d %d -->\n", page_width, page_height);
  topmargin = scaley(page_height/3200);
  fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx\"><HR></DIV>",
                  topmargin, 0);
}

void DRAW_rule(int hh, int vv, long p, long q)
{
  fprintf(stdout, "<!-- rule: %d,%d %d %d\n-->", hh, vv, p, q);
}

void DRAW_special(char *s)
{
  static int href;
  if (strncmp(s, "html:<a href=", 13) == 0) {
    href = 1;
//    fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx\"\n>%s</DIV>",
//                    topmargin, 0, s+5);
    fprintf(stdout, "%s", s+5);
  } else if (strncmp(s, "html:<a name=", 13) == 0) {
    href = 0;
    fprintf(stdout, "%s</a>", s+5);
  } else if (strcmp(s, "html:</a>") == 0) {
    if (href == 1) fprintf(stdout, "%s", s+5);
  } else if (strncmp(s, "html:", 5) == 0) {
    fprintf(stdout, "<DIV style=\"position: absolute; top:%dpx; left:%dpx\"\n>%s</DIV>",
                    topmargin, 0, s+5);
  } else {
    fprintf(stdout, "<!-- special: %s -->\n", s);
  }
}

void DRAW_endjob(void)
{
  fprintf(stdout, "</BODY>");
  fprintf(stdout, "</HTML>\n");
}