Frame Shapes
This module provides frame shape generators for Phase 4 QR code rendering.
Frame Shape Generator
Frame shape generators for QR codes.
This module provides generators for creating SVG clipping paths and masks for various frame shapes including circles, rounded rectangles, and squircles.
- class segnomms.shapes.frames.FrameShapeGenerator[source]
Bases:
objectGenerates SVG paths and definitions for frame shapes.
This class provides static methods to generate various frame shapes as SVG elements for use in clipping paths and masks.
- static generate_rounded_rect_clip(width, height, border, corner_radius)[source]
Generate rounded rectangle clipping path.
- static generate_squircle_clip(width, height, border=0)[source]
Generate squircle (superellipse) clipping path.
A squircle is a mathematical shape between a square and a circle, defined by the superellipse equation with n=4.
- static generate_fade_mask(width, height, shape, fade_distance, corner_radius=0.0, custom_path=None)[source]
Generate fade mask for soft edges.
Creates a gradient mask that provides a soft fade effect at the edges of the frame shape using SVG gradients and masks.
- Parameters:
width (
int) – Total SVG width in pixelsheight (
int) – Total SVG height in pixelsshape (
str) – Frame shape type (‘circle’, ‘rounded-rect’, ‘squircle’, ‘custom’)fade_distance (
float) – Fade distance in pixelscorner_radius (
float) – Corner radius for rounded-rect (0.0-1.0)custom_path (
Optional[str]) – Custom SVG path for custom shapes
- Return type:
- Returns:
SVG gradient and mask elements as string
- static generate_scale_transform(width, height, shape, scale_distance, corner_radius=0.0, custom_path=None)[source]
Generate scale transform for modules near frame edges.
Creates transform groups that gradually scale down modules as they approach the frame edge, creating a smooth transition effect.
- Parameters:
width (
int) – Total SVG width in pixelsheight (
int) – Total SVG height in pixelsshape (
str) – Frame shape type (‘circle’, ‘rounded-rect’, ‘squircle’, ‘custom’)scale_distance (
float) – Distance in pixels from edge where scaling beginscorner_radius (
float) – Corner radius for rounded-rect (0.0-1.0)custom_path (
Optional[str]) – Custom SVG path for custom shapes
- Return type:
- Returns:
Tuple of (clip_path_url, scale_group_transform)
- static validate_custom_path(path, width, height)[source]
Validate a custom SVG path.
Performs basic validation on custom SVG path strings to ensure they are likely to work as clipping paths.
- static scale_path_to_fit(path, current_box, target_width, target_height)[source]
Scale an SVG path to fit target dimensions.
This is a placeholder for path scaling functionality. In practice, this would parse the path and transform coordinates.
The FrameShapeGenerator class provides static methods for generating
SVG clipping paths and masks for various frame shapes.
Available Frame Shapes
- class segnomms.shapes.frames.FrameShapeGenerator[source]
Bases:
objectGenerates SVG paths and definitions for frame shapes.
This class provides static methods to generate various frame shapes as SVG elements for use in clipping paths and masks.
- static generate_rounded_rect_clip(width, height, border, corner_radius)[source]
Generate rounded rectangle clipping path.
- static generate_squircle_clip(width, height, border=0)[source]
Generate squircle (superellipse) clipping path.
A squircle is a mathematical shape between a square and a circle, defined by the superellipse equation with n=4.
- static generate_fade_mask(width, height, shape, fade_distance, corner_radius=0.0, custom_path=None)[source]
Generate fade mask for soft edges.
Creates a gradient mask that provides a soft fade effect at the edges of the frame shape using SVG gradients and masks.
- Parameters:
width (
int) – Total SVG width in pixelsheight (
int) – Total SVG height in pixelsshape (
str) – Frame shape type (‘circle’, ‘rounded-rect’, ‘squircle’, ‘custom’)fade_distance (
float) – Fade distance in pixelscorner_radius (
float) – Corner radius for rounded-rect (0.0-1.0)custom_path (
Optional[str]) – Custom SVG path for custom shapes
- Return type:
- Returns:
SVG gradient and mask elements as string
- static generate_scale_transform(width, height, shape, scale_distance, corner_radius=0.0, custom_path=None)[source]
Generate scale transform for modules near frame edges.
Creates transform groups that gradually scale down modules as they approach the frame edge, creating a smooth transition effect.
- Parameters:
width (
int) – Total SVG width in pixelsheight (
int) – Total SVG height in pixelsshape (
str) – Frame shape type (‘circle’, ‘rounded-rect’, ‘squircle’, ‘custom’)scale_distance (
float) – Distance in pixels from edge where scaling beginscorner_radius (
float) – Corner radius for rounded-rect (0.0-1.0)custom_path (
Optional[str]) – Custom SVG path for custom shapes
- Return type:
- Returns:
Tuple of (clip_path_url, scale_group_transform)
- static validate_custom_path(path, width, height)[source]
Validate a custom SVG path.
Performs basic validation on custom SVG path strings to ensure they are likely to work as clipping paths.
- static scale_path_to_fit(path, current_box, target_width, target_height)[source]
Scale an SVG path to fit target dimensions.
This is a placeholder for path scaling functionality. In practice, this would parse the path and transform coordinates.
Supported frame shapes include:
square: Standard rectangular QR code (default)
circle: Circular boundary with equal radius
rounded-rect: Rectangle with customizable corner radius
squircle: Modern superellipse shape (between square and circle)
custom: User-defined SVG path
Examples
Circular Frame:
from segnomms.shapes.frames import FrameShapeGenerator
# Generate circular clipping path
circle_clip = FrameShapeGenerator.generate_circle_clip(200, 200)
print(circle_clip) # <circle cx="100" cy="100" r="100"/>
Rounded Rectangle:
# Generate rounded rectangle with 20% corner radius
rounded_clip = FrameShapeGenerator.generate_rounded_rect_clip(
200, 200, border=10, corner_radius=0.2
)
Custom Frame Shape:
# Use with a custom SVG path via the main write() API
diamond_path = "M 100 0 L 200 100 L 100 200 L 0 100 Z"
# See usage with QR codes below for passing frame_custom_path
Usage with QR Codes
Frame shapes are typically used through the main write function:
import segno
from segnomms import write
qr = segno.make("https://example.com", error='h')
# Circle frame
with open('circle_frame.svg', 'w') as f:
write(qr, f, frame_shape='circle', border=6)
# Rounded rectangle frame
with open('rounded_frame.svg', 'w') as f:
write(qr, f,
frame_shape='rounded-rect',
frame_corner_radius=0.3,
border=5)
# Custom diamond frame
with open('diamond_frame.svg', 'w') as f:
write(qr, f,
frame_shape='custom',
frame_custom_path="M 100 0 L 200 100 L 100 200 L 0 100 Z",
border=8)