Tweak a conditional expression in pearl.c to work around a display bug
[sgt/puzzles] / windows.c
index 6bcf895..7bdb5e2 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -154,11 +154,17 @@ void debug_printf(char *fmt, ...)
 {
     char buf[4096];
     va_list ap;
+    static int debugging = -1;
 
-    va_start(ap, fmt);
-    _vsnprintf(buf, 4095, fmt, ap);
-    dputs(buf);
-    va_end(ap);
+    if (debugging == -1)
+        debugging = getenv("DEBUG_PUZZLES") ? 1 : 0;
+
+    if (debugging) {
+        va_start(ap, fmt);
+        _vsnprintf(buf, 4095, fmt, ap);
+       dputs(buf);
+        va_end(ap);
+    }
 }
 #endif
 
@@ -225,7 +231,7 @@ struct frontend {
     int printoffsetx, printoffsety;
     float printpixelscale;
     int fontstart;
-    int linewidth;
+    int linewidth, linedotted;
     drawing *dr;
     int xmin, ymin;
     float puzz_scale;
@@ -239,6 +245,7 @@ void frontend_free(frontend *fe)
     sfree(fe->brushes);
     sfree(fe->pens);
     sfree(fe->presets);
+    sfree(fe->fonts);
 
     sfree(fe);
 }
@@ -493,12 +500,16 @@ static void win_set_pen(frontend *fe, int colour, int thin)
        float r, g, b;
        int width = thin ? 0 : fe->linewidth;
 
+       if (fe->linedotted)
+           width = 0;
+
        print_get_colour(fe->dr, colour, fe->printcolour, &hatch, &r, &g, &b);
        /*
         * Stroking in hatched colours is not permitted.
         */
        assert(hatch < 0);
-       pen = CreatePen(PS_SOLID, width, RGB(r * 255, g * 255, b * 255));
+       pen = CreatePen(fe->linedotted ? PS_DOT : PS_SOLID,
+                       width, RGB(r * 255, g * 255, b * 255));
     } else {
        pen = fe->pens[colour];
     }
@@ -597,10 +608,8 @@ static void win_draw_text(void *handle, int x, int y, int fonttype,
        HFONT oldfont;
        TEXTMETRIC tm;
        SIZE size;
-#ifdef _WIN32_WCE
        TCHAR wText[256];
-       MultiByteToWideChar (CP_ACP, 0, text, -1, wText, 256);
-#endif
+       MultiByteToWideChar (CP_UTF8, 0, text, -1, wText, 256);
 
        oldfont = SelectObject(fe->hdc, fe->fonts[i].font);
        if (GetTextMetrics(fe->hdc, &tm)) {
@@ -609,11 +618,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype,
            else
                xy.y -= tm.tmAscent;
        }
-#ifndef _WIN32_WCE
-       if (GetTextExtentPoint32(fe->hdc, text, strlen(text), &size))
-#else
-       if (GetTextExtentPoint32(fe->hdc, wText, wcslen(wText), &size))
-#endif
+       if (GetTextExtentPoint32W(fe->hdc, wText, wcslen(wText), &size))
        {
            if (align & ALIGN_HCENTRE)
                xy.x -= size.cx / 2;
@@ -622,11 +627,7 @@ static void win_draw_text(void *handle, int x, int y, int fonttype,
        }
        SetBkMode(fe->hdc, TRANSPARENT);
        win_text_colour(fe, colour);
-#ifndef _WIN32_WCE
-       TextOut(fe->hdc, xy.x, xy.y, text, strlen(text));
-#else
-       ExtTextOut(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
-#endif
+       ExtTextOutW(fe->hdc, xy.x, xy.y, 0, NULL, wText, wcslen(wText), NULL);
        SelectObject(fe->hdc, oldfont);
     }
 }
@@ -792,6 +793,17 @@ static void win_line_width(void *handle, float width)
     fe->linewidth = (int)(width * fe->printpixelscale);
 }
 
+static void win_line_dotted(void *handle, int dotted)
+{
+    frontend *fe = (frontend *)handle;
+
+    assert(fe->drawstatus != DRAWING);
+    if (fe->drawstatus == NOTHING)
+       return;
+
+    fe->linedotted = dotted;
+}
+
 static void win_begin_doc(void *handle, int pages)
 {
     frontend *fe = (frontend *)handle;
@@ -882,6 +894,7 @@ static void win_begin_puzzle(void *handle, float xm, float xc,
     fe->printpixelscale = scale;
 
     fe->linewidth = 1;
+    fe->linedotted = FALSE;
 }
 
 static void win_end_puzzle(void *handle)
@@ -940,6 +953,15 @@ static void win_end_doc(void *handle)
     }
 }
 
+char *win_text_fallback(void *handle, const char *const *strings, int nstrings)
+{
+    /*
+     * We assume Windows can cope with any UTF-8 likely to be
+     * emitted by a puzzle.
+     */
+    return dupstr(strings[0]);
+}
+
 const struct drawing_api win_drawing = {
     win_draw_text,
     win_draw_rect,
@@ -963,6 +985,8 @@ const struct drawing_api win_drawing = {
     win_end_page,
     win_end_doc,
     win_line_width,
+    win_line_dotted,
+    win_text_fallback,
 };
 
 void print(frontend *fe)
@@ -3380,6 +3404,7 @@ int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show)
        DispatchMessage(&msg);
     }
 
+    DestroyWindow(fe->hwnd);
     cleanup_help();
 
     return msg.wParam;