Главная \ Словарь терминов

Словарь терминов

 

 

from reportlab.lib.pagesizes import A4, landscape
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.lib import colors
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import math
import os

# Optional: register a clean sans font if available
def safe_register_font():
    try:
        # Try NotoSans if installed
        pdfmetrics.registerFont(TTFont("NotoSans", "NotoSans-Regular.ttf"))
        return "NotoSans"
    except:
        try:
            pdfmetrics.registerFont(TTFont("DejaVuSans", "DejaVuSans.ttf"))
            return "DejaVuSans"
        except:
            return "Helvetica"

FONT = safe_register_font()

# Settings
filename = "map_grid_A4_landscape.pdf"
page_width, page_height = landscape(A4)  # 297 x 210 mm
margin = 1.5 * cm
rows = 3
cols = 5

# Derived drawing area
w = page_width - 2 * margin
h = page_height - 2 * margin

# Reserve top area for title
title_space = 2.8 * cm  # room for title + subtitle
grid_top = page_height - margin - title_space
grid_bottom = margin
grid_height = grid_top - grid_bottom
grid_width = w

cell_w = grid_width / cols
cell_h = grid_height / rows

# Circle for points
circle_r = min(cell_w, cell_h) * 0.18

# Positions for numbers 1–15 in a simple left-to-right, top-to-bottom fill
# Row 1 is top, Row 3 is bottom
positions = []
for r in range(rows):
    for c in range(cols):
        positions.append((r, c))

# Create canvas
c = canvas.Canvas(filename, pagesize=landscape(A4))

# Background
c.setFillColor(colors.white)
c.rect(0, 0, page_width, page_height, stroke=0, fill=1)

# Title and subtitle
title = "Карта приключений царевны"
subtitle = "Маршрут и точки 1–15"

c.setFont(FONT, 24)
c.setFillColor(colors.black)
title_y = page_height - margin - 0.8 * cm
c.drawString(margin, title_y, title)

c.setFont(FONT, 12)
c.setFillColor(colors.black)
c.drawString(margin, title_y - 0.9 * cm, subtitle)

# Draw outer grid rectangle (optional subtle stroke)
c.setStrokeColor(colors.HexColor("#666666"))
c.setLineWidth(1)

# Grid origin (top-left of grid area)
grid_left = margin
grid_right = margin + grid_width

# Horizontal lines
c.setStrokeColor(colors.HexColor("#BBBBBB"))
c.setLineWidth(1)
for r in range(rows + 1):
    y = grid_top - r * cell_h
    c.line(grid_left, y, grid_right, y)

# Vertical lines
for col in range(cols + 1):
    x = grid_left + col * cell_w
    c.line(x, grid_top, x, grid_bottom)

# Draw numbered circles
c.setLineWidth(1.4)
c.setStrokeColor(colors.HexColor("#444444"))
c.setFillColor(colors.white)

num_font_size = 14
for idx in range(15):
    r, ccol = positions[idx]  # r: 0..2 from top, ccol: 0..4 from left
    # Center of the cell
    cx = grid_left + ccol * cell_w + cell_w / 2.0
    cy = grid_top - r * cell_h - cell_h / 2.0
    # Circle
    c.circle(cx, cy, circle_r, stroke=1, fill=1)
    # Number
    label = str(idx + 1)
    c.setFont(FONT, num_font_size)
    c.setFillColor(colors.black)
    # Center text
    tw = c.stringWidth(label, FONT, num_font_size)
    c.drawString(cx - tw / 2.0, cy - num_font_size * 0.35, label)

# Footer small note
c.setFont(FONT, 8)
c.setFillColor(colors.HexColor("#888888"))
c.drawRightString(page_width - margin, margin * 0.7, "Формат: A4 горизонтально • Поля 1.5 см • Сетка 3×5")

c.showPage()
c.save()

print(f"Saved: {os.path.abspath(filename)}")