コード例 #1
0
    def __init__(self, board, filename):
        """Save a board as an image.

        Arguments:
        board -- the board
        filename -- the filename

        """
        self.numbers = board.numbers
        self.cellsize = board.cellsize
        self.boardsize = board.boardsize
        self.filename = filename
        if options.get("image", "format"):
            self.format = options.get("image", "format")
        else:
            self.format = re.sub(".*\.", "", filename)

        # 10% for borders
        self.square_width = options.getint("image", "width") / \
                            (self.boardsize + 1)
        self.square_height = options.getint("image", "height") / \
                             (self.boardsize + 1)

        self.create()

        self.draw_board()
        self.draw_numbers()

        self.save()
コード例 #2
0
    def draw_numbers(self):
        """Draw the numbers."""
        font = PIL.ImageFont.truetype(options.get("image", "font"),
                                      options.getint("image", "font_size"))

        # 5% margin + half square
        x = options.getint("image", "width") / 20 + self.square_width / 2
        y = options.getint("image", "height") / 20 + self.square_height / 2
        for i in xrange(self.boardsize):
            for j in xrange(self.boardsize):
                if self.numbers[j][i] != 0:
                    if options.getboolean("sudoku", "use_letters"):
                        text = str(Value(self.numbers[j][i]))
                    else:
                        text = str(Value(self.numbers[j][i]).integer())
                    size = self.draw.textsize(text, font=font)
                    # this should be:
                    #self.draw.text((x + i * self.square_width - size[0] / 2,
                    #                y + j * self.square_height - size[1] / 2),
                    #                text)
                    # but it's not centered without the + 2 in the y coord
                    self.draw.text((x + i * self.square_width - size[0] / 2,
                                    y + j * self.square_height - size[1] / 2 +
                                    2),
                                   text,
                                   fill=options.get("image", "font_colour"),
                                   font=font)
コード例 #3
0
ファイル: pdf.py プロジェクト: ternus/arcnet
    def draw_numbers(self):
        """Draw the numbers."""
        self.c.setFillColor(options.get("pdf", "font_colour"))
        self.c.setFont(options.get("pdf", "font"),
                       options.getint("pdf", "font_size"))

        face = reportlab.pdfbase.pdfmetrics.getFont("Helvetica").face
        height = (face.ascent - face.descent) * \
                 options.getint("pdf", "font_size") / 1000.0
        if self.page_size[0] < self.page_size[1]:
            square_length = self.page_size[0] / (self.boardsize) * 0.9
            x = self.page_size[0] / 20 + square_length / 2
            y = (self.page_size[1] - self.page_size[0] * 0.9 + \
                 square_length - height) / 2
        else:
            square_length = self.page_size[1] / (self.boardsize) * 0.9
            y = self.page_size[1] / 20 + square_length / 2
            x = (self.page_size[0] - self.page_size[1] * 0.9 + \
                 square_length - height) / 2

        for i in xrange(self.boardsize):
            for j in xrange(self.boardsize):
                if self.numbers[self.boardsize - 1 - j][i] != 0:
                    if options.getboolean("sudoku", "use_letters"):
                        text = str(Value(self.numbers[self.boardsize - 1 - j][i]))
                    else:
                        text = str(Value(self.numbers[self.boardsize - 1 - j][i]).integer())
                    self.c.drawCentredString(x + i * square_length,
                                             y + j * square_length,
                                             text)
コード例 #4
0
ファイル: text.py プロジェクト: ternus/arcnet
def create_sudoku(filename):
    """Create a sudoku with handicap and save it to filename.

    The handicap are the extra numbers given.

    Arguments:
    filename -- the file name

    """
    while True:
        print _(u"Creating sudoku..."),
        sys.stdout.flush()

        sudoku = Sudoku(Board((options.getint("sudoku", "region_width"),
                               options.getint("sudoku", "region_height"))),
                        difficulty=options.get("sudoku", "difficulty"))
        sudoku.create(options.getint("sudoku", "handicap"))

        if options.getboolean("sudoku", "force") and \
           (difficulty(sudoku.to_board()) != options.get("sudoku",
                                                       "difficulty")):
            print _(u"sudoku with wrong difficulty!")
        else:
            sudoku.to_board().save(filename)
            print _(u"success!")
            break

    draw_board(sudoku.to_board())

    return True
コード例 #5
0
 def create(self):
     """Create a blank image widthxheight."""
     if not options.get("image", "background"):
         mode = "RGBA"
     elif self.in_greyscale(options.get("image", "background")) and \
          self.in_greyscale(options.get("image", "lines_colour")) and \
          self.in_greyscale(options.get("image", "font_colour")):
         mode = "L"
     else:
         mode = "RGB"
     self.im = PIL.Image.new(mode, (options.getint("image", "width"),
                                    options.getint("image", "height")),
                             options.get("image", "background"))
     self.draw = PIL.ImageDraw.Draw(self.im)
コード例 #6
0
ファイル: pdf.py プロジェクト: ternus/arcnet
 def draw_filename(self):
     """Draw the filename."""
     if options.getint("pdf", "filename_size") > 0 and self.filename:
         self.c.setFillColor(options.get("pdf", "filename_colour"))
         self.c.setFont(options.get("pdf", "filename_font"),
                        options.getint("pdf", "filename_size"))
         face = reportlab.pdfbase.pdfmetrics.getFont("Helvetica").face
         height = (face.ascent - face.descent) * \
                  options.getint("pdf", "filename_size") / 1000.0
         self.c.drawCentredString(self.page_size[0] / 2,
                                  (self.page_size[1] - \
                                   self.page_size[0] * 0.9) / 2 + \
                                  self.page_size[0] * 0.9 + \
                                  height / 2,
                                  "(" + str(self.filename) + ")")
コード例 #7
0
ファイル: pdf.py プロジェクト: ternus/arcnet
 def draw_title(self):
     """Draw the title "sudou"."""
     if options.getint("pdf", "title_size") > 0:
         self.c.setFillColor(options.get("pdf", "title_colour"))
         self.c.setFont(options.get("pdf", "title_font"),
                        options.getint("pdf", "title_size"))
         face = reportlab.pdfbase.pdfmetrics.getFont("Helvetica").face
         height = (face.ascent - face.descent) * \
                  options.getint("pdf", "title_size") / 1000.0
         self.c.drawCentredString(self.page_size[0] / 2,
                                  (3 * self.page_size[1] + \
                                   0.9 * self.page_size[0]) / 4 - \
                                  options.getint("pdf", "title_size") / 2 + \
                                  height / 2,
                                  "sudoku")
コード例 #8
0
    def draw_board(self):
        """Draw the board.

        Only the board, to draw numbers draw_numbers it is used.

        """
        # margins
        x = options.getint("image", "width") / 20 # 5% left margin
        y = options.getint("image", "height") / 20 # 5% top margin

        self.draw.rectangle(((x, y), (options.getint("image", "width") - x,
                                      options.getint("image", "height") - y)),
                            outline=options.get("image", "lines_colour"),
                            fill=None)

        linewidth = 0

        # horizontal lines
        for i in xrange(self.boardsize):
            if i > 0 and i % self.cellsize[1] == 0:
                linewidth = 2
            else:
                linewidth = 1
            # PIL <= 1.1.4
            for offset in range(linewidth):
                self.draw.line(((x, y + i * self.square_height + offset),
                                (options.getint("image", "width") - x,
                                 y + i * self.square_height + offset)),
                               options.get("image", "lines_colour"))
            # PIL > 1.1.5
            #self.draw.line(((x, y + i * self.square_height),
            #                (options.getint("image", "width") - x,
            #                 y + i * self.square_height)),
            #               fill=options.get("image", "lines_colour"),
            #               width=linewidth)

        # vertical lines
        for i in xrange(self.boardsize):
            if i > 0 and i % self.cellsize[0] == 0:
                linewidth = 2
            else:
                linewidth = 1
            # PIL <= 1.1.4
            for offset in range(linewidth):
                self.draw.line(((x + i * self.square_width + offset, y),
                                (x + i * self.square_width + offset,
                                 options.getint("image", "height") - y)),
                               options.get("image", "lines_colour"))