+ fe->drawstatus = NOTHING;
+}
+
+static void win_line_width(void *handle, float width)
+{
+ frontend *fe = (frontend *)handle;
+
+ assert(fe->drawstatus != DRAWING);
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ fe->linewidth = (int)(width * fe->printpixelscale);
+}
+
+static void win_begin_doc(void *handle, int pages)
+{
+ frontend *fe = (frontend *)handle;
+
+ assert(fe->drawstatus != DRAWING);
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ if (StartDoc(fe->hdc, &fe->di) <= 0) {
+ char *e = geterrstr();
+ MessageBox(fe->hwnd, e, "Error starting to print",
+ MB_ICONERROR | MB_OK);
+ sfree(e);
+ fe->drawstatus = NOTHING;
+ }
+
+ /*
+ * Push a marker on the font stack so that we won't use the
+ * same fonts for printing and drawing. (This is because
+ * drawing seems to look generally better in bold, but printing
+ * is better not in bold.)
+ */
+ fe->fontstart = fe->nfonts;
+}
+
+static void win_begin_page(void *handle, int number)
+{
+ frontend *fe = (frontend *)handle;
+
+ assert(fe->drawstatus != DRAWING);
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ if (StartPage(fe->hdc) <= 0) {
+ char *e = geterrstr();
+ MessageBox(fe->hwnd, e, "Error starting a page",
+ MB_ICONERROR | MB_OK);
+ sfree(e);
+ fe->drawstatus = NOTHING;
+ }
+}
+
+static void win_begin_puzzle(void *handle, float xm, float xc,
+ float ym, float yc, int pw, int ph, float wmm)
+{
+ frontend *fe = (frontend *)handle;
+ int ppw, pph, pox, poy;
+ float mmpw, mmph, mmox, mmoy;
+ float scale;
+
+ assert(fe->drawstatus != DRAWING);
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ ppw = GetDeviceCaps(fe->hdc, HORZRES);
+ pph = GetDeviceCaps(fe->hdc, VERTRES);
+ mmpw = (float)GetDeviceCaps(fe->hdc, HORZSIZE);
+ mmph = (float)GetDeviceCaps(fe->hdc, VERTSIZE);
+
+ /*
+ * Compute the puzzle's position on the logical page.
+ */
+ mmox = xm * mmpw + xc;
+ mmoy = ym * mmph + yc;
+
+ /*
+ * Work out what that comes to in pixels.
+ */
+ pox = (int)(mmox * (float)ppw / mmpw);
+ poy = (int)(mmoy * (float)ppw / mmpw);
+
+ /*
+ * And determine the scale.
+ *
+ * I need a scale such that the maximum puzzle-coordinate
+ * extent of the rectangle (pw * scale) is equal to the pixel
+ * equivalent of the puzzle's millimetre width (wmm * ppw /
+ * mmpw).
+ */
+ scale = (wmm * ppw) / (mmpw * pw);
+
+ /*
+ * Now store pox, poy and scale for use in the main drawing
+ * functions.
+ */
+ fe->printoffsetx = pox;
+ fe->printoffsety = poy;
+ fe->printpixelscale = scale;
+
+ fe->linewidth = 1;
+}
+
+static void win_end_puzzle(void *handle)
+{
+ /* Nothing needs to be done here. */
+}
+
+static void win_end_page(void *handle, int number)
+{
+ frontend *fe = (frontend *)handle;
+
+ assert(fe->drawstatus != DRAWING);
+
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ if (EndPage(fe->hdc) <= 0) {
+ char *e = geterrstr();
+ MessageBox(fe->hwnd, e, "Error finishing a page",
+ MB_ICONERROR | MB_OK);
+ sfree(e);
+ fe->drawstatus = NOTHING;
+ }
+}
+
+static void win_end_doc(void *handle)
+{
+ frontend *fe = (frontend *)handle;
+
+ assert(fe->drawstatus != DRAWING);
+
+ /*
+ * Free all the fonts created since we began printing.
+ */
+ while (fe->nfonts > fe->fontstart) {
+ fe->nfonts--;
+ DeleteObject(fe->fonts[fe->nfonts].font);
+ }
+ fe->fontstart = 0;
+
+ /*
+ * The MSDN web site sample code doesn't bother to call EndDoc
+ * if an error occurs half way through printing. I expect doing
+ * so would cause the erroneous document to actually be
+ * printed, or something equally undesirable.
+ */
+ if (fe->drawstatus == NOTHING)
+ return;
+
+ if (EndDoc(fe->hdc) <= 0) {
+ char *e = geterrstr();
+ MessageBox(fe->hwnd, e, "Error finishing printing",
+ MB_ICONERROR | MB_OK);
+ sfree(e);
+ fe->drawstatus = NOTHING;
+ }