Allow writing text on pieces
[xboard.git] / draw.c
diff --git a/draw.c b/draw.c
index badd12b..080fd82 100644 (file)
--- a/draw.c
+++ b/draw.c
@@ -181,7 +181,9 @@ InitDrawingSizes (BoardSize boardSize, int flags)
 
     if(boardSize == -2 && gameInfo.variant != oldVariant
                        && oldNrOfFiles && oldNrOfFiles != BOARD_WIDTH) { // called because variant switch changed board format
-       squareSize = ((squareSize + lineGap) * oldNrOfFiles + 0.5*BOARD_WIDTH) / BOARD_WIDTH - lineGap; // keep total width fixed
+       squareSize = ((squareSize + lineGap) * oldNrOfFiles + 0.5*BOARD_WIDTH) / BOARD_WIDTH; // keep total width fixed
+       if(appData.overrideLineGap < 0) lineGap = squareSize < 37 ? 1 : squareSize < 59 ? 2 : squareSize < 116 ? 3 : 4;
+        squareSize -= lineGap;
        CreatePNGPieces();
         CreateGrid();
     }
@@ -706,7 +708,7 @@ DrawText (char *string, int x, int y, int align)
                    CAIRO_FONT_SLANT_NORMAL,
                    CAIRO_FONT_WEIGHT_BOLD);
 
-       cairo_set_font_size (cr, squareSize/4);
+       cairo_set_font_size (cr, align < 0 ? 2*squareSize/3 : squareSize/4);
        // calculate where it goes
        cairo_text_extents (cr, string, &te);
 
@@ -720,6 +722,8 @@ DrawText (char *string, int x, int y, int align)
            yy += -te.y_bearing + 3;
        } else if (align == 4) {
            xx += te.x_bearing + 1, yy += -te.y_bearing + 3;
+       } else if (align < 0) {
+           xx += squareSize/2 - te.width/2, yy += 9*squareSize/16 - te.y_bearing/2;
        }
 
        cairo_move_to (cr, xx-1, yy);
@@ -730,6 +734,31 @@ DrawText (char *string, int x, int y, int align)
 }
 
 void
+InscribeKanji (ChessSquare piece, int x, int y)
+{
+    char *p, *q, buf[10];
+    int n;
+    if(piece == EmptySquare) return;
+    if(piece >= BlackPawn) piece = BLACK_TO_WHITE piece;
+    p = appData.inscriptions;
+    n = piece;
+    while(piece > WhitePawn) {
+      if(*p++ == NULLCHAR) {
+        if(n != WhiteKing) return;
+        p = q;
+        break;
+      }
+      q = p - 1;
+      while((*p & 0xC0) == 0x80) p++; // skip UTF-8 continuation bytes
+      piece--;
+    }
+    strncpy(buf, p, 10);
+    for(q=buf; (*++q & 0xC0) == 0x80;);
+    *q = NULLCHAR;
+    DrawText(buf, x, y, -1);
+}
+
+void
 DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, char *tString, char *bString, int align)
 {   // basic front-end board-draw function: takes care of everything that can be in square:
     // piece, background, coordinate/count, marker dot
@@ -738,6 +767,7 @@ DrawOneSquare (int x, int y, ChessSquare piece, int square_color, int marker, ch
        BlankSquare(csBoardWindow, x, y, square_color, piece, 1);
     } else {
        pngDrawPiece(csBoardWindow, piece, square_color, x, y);
+        InscribeKanji(piece, x, y);
     }
 
     if(align) { // square carries inscription (coord or piece count)