Shared types¶
Shared typing helpers for pyStrich encoders and renderers.
- class BarcodeRenderOptions[source]¶
Bases:
TypedDictOptional render-time tweaks for label-bearing 1D barcodes.
Currently used by
pystrich.code128.Code128Encoderandpystrich.code39.Code39Encoder. All keys are optional; omitted keys fall back to library defaults.- show_label: bool¶
Whether to render the human-readable label underneath the bars. Defaults to
True; set toFalseto suppress it.
- ttf_font: str¶
Absolute path to a TrueType font file used for the label in PNG output. Defaults to a bundled bitmap font if unset. SVG and EPS output always render the label using the bundled Courier Prime glyph paths and ignore this option.
- ttf_fontsize: int¶
Font size in points (PNG output only — SVG/EPS use the bundled Courier Prime glyph paths).
Matrix mark extraction shared by vector renderers (SVG, EPS, DXF).
- class SymbolMarks(marks: tuple[MatrixMark, ...], width: int, height: int)[source]¶
Bases:
NamedTupleA rendered symbol as dark rectangles in a unit grid.
marksare the dark regions as(x, y, width, height)rectangles, with a top-left origin and y pointing down (matching the matrix, PIL and SVG).widthandheightare the full extent of the grid the marks live in – the same canvas every other output format draws, so quiet zones, finder pattern and (for 1D) the bearer bar and the space reserved for the human-readable label are all included – and a consumer can fit[0, width] x [0, height]into any target box at any scale. The label glyphs themselves are never marks. For 2D symbols one unit is one module; for 1D symbols the x unit is one narrow bar and the y unit is one pixel of the bar layout.
- class MarkShape(*values)[source]¶
Bases:
EnumHow marked cells are grouped and drawn in vector output.
Each value selects a grouping (one
MatrixMarkper cell, or one per horizontal run) and – where the renderer supports it – the drawing primitive used per mark.- HORIZONTAL_RUNS = 1¶
Maximal horizontal runs of matched cells, drawn as filled rectangles.
- SQUARE_CELLS = 2¶
One 1x1 region per matched cell, drawn as a filled rectangle.
- CIRCULAR_CELLS = 3¶
One 1x1 region per matched cell, drawn as a filled circle inscribed in the cell.
- iter_horizontal_runs(matrix: Sequence[Sequence[int | None]], *, mark_values_when: bool) Iterator[tuple[int, int, int, int]][source]¶
Yield each maximal horizontal run of cells whose truthiness equals
mark_values_when.mark_values_when=Truemarks the dark (truthy) cells;Falsemarks the light cells (0orNone). Each yielded mark hasheight=1.
- iter_cells(matrix: Sequence[Sequence[int | None]], *, mark_values_when: bool) Iterator[tuple[int, int, int, int]][source]¶
Yield a 1x1 mark for every cell whose truthiness equals
mark_values_when.
- iter_marks(matrix: Sequence[Sequence[int | None]], *, mark_values_when: bool, mark_shape: MarkShape) Iterator[tuple[int, int, int, int]][source]¶
Yield
MatrixMarkregions for the chosenmark_shape.
- class TextLabel(text: str, x: float, y: float, font_size: int, anchor: str = 'start')[source]¶
Bases:
NamedTupleA run of text to render below the bars in vector output.
Coordinates are in pixels (= user units for SVG/EPS at default DPI), and
yis the top edge of the text — matching the convention used byPIL.ImageDraw.text(xy, ...)for the corresponding raster path.anchorcontrols howxrelates to the text run:"start"is the left edge,"middle"the centre,"end"the right edge.
- class BarLayout(heights: Sequence[int], bar_width: int, quiet_left: int = 0, quiet_right: int = 0, quiet_top: int = 0, quiet_bottom: int = 0, labels: Sequence[TextLabel] = (), bearer_width: int = 0)[source]¶
Bases:
NamedTuplePixel-precise layout of a 1D barcode for any output format.
All values are in pixels (= user units for SVG/EPS at default DPI).
heights[i]is the bar’s pixel height at columni(0is a gap). Each column isbar_widthpixels wide. The four quiet zones frame the symbol;quiet_leftandquiet_topshift the bars, whilequiet_rightandquiet_bottomonly enlarge the canvas.labelscarries the human-readable text drawn beneath the bars, rendered identically by the PNG, SVG and EPS paths.bearer_width, when positive, draws a bearer bar of that pixel thickness bordering the bars (as used by ITF-14); it must be folded into the quiet zones, with the label placed in the bottom quiet zone outside the frame.
- iter_bar_marks(heights: Sequence[int], bar_width: int, *, quiet_left: int = 0, quiet_top: int = 0) Iterator[tuple[int, int, int, int]][source]¶
Yield a
MatrixMarkper maximal run of equal positive heights.Coordinates and dimensions are in pixels.
heights[i]is the bar’s pixel height at columni(0is a gap; positive values are bars sharing a top edge aty = quiet_top). Each column isbar_widthpixels wide. Adjacent columns with the same positive height collapse into one mark.Only
quiet_leftandquiet_topare accepted because they are the only offsets that affect mark coordinates; the right and bottom quiet zones are a renderer concern (canvas / viewBox sizing).
- iter_bearer_marks(layout: BarLayout) Iterator[tuple[int, int, int, int]][source]¶
Yield the four rectangles of a full-frame bearer bar.
Nothing is yielded when
layout.bearer_widthis zero. The frame borders the bars only – the top and bottom rules abut the bars, and the label (in the bottom quiet zone) sits outside the frame.quiet_topholds the top rule, so the bearer thickness must be folded into the quiet zones.
- iter_barcode_marks(layout: BarLayout) Iterator[tuple[int, int, int, int]][source]¶
Yield every dark mark of a 1D barcode: its bars, then its bearer bar.
The single entry point the PNG, SVG and EPS paths render from, composing the raw
iter_bar_marks()primitive withiter_bearer_marks().
User-friendly composition of GS1 Application Identifier payloads.
Pair GS1Fixed and GS1Variable instances with the matching
gs1() classmethod on pystrich.code128.Code128Data or
pystrich.datamatrix.DataMatrixData to emit a GS1-128 or GS1 Data
Matrix payload without managing FNC1 separators by hand.
The library does not ship an Application Identifier registry: callers tell us whether each field is fixed- or variable-length by choosing the wrapper class. FNC1 separators are inserted after variable-length fields that are not the final element of the payload, and once at the very start of the message.
Added in version 0.15.
- class GS1Fixed(application_identifier: str, value: str)[source]¶
Bases:
_GS1FieldA fixed-length GS1 Application Identifier field.
The reader knows the data length from the Application Identifier alone, so no FNC1 separator is needed after the field. Use for Application Identifiers such as
(01)GTIN-14,(17)expiry date, or(11)production date.- Parameters:
application_identifier – The 2-4 digit Application Identifier.
value – The data string. Must be non-empty printable ASCII.
- class GS1Variable(application_identifier: str, value: str)[source]¶
Bases:
_GS1FieldA variable-length GS1 Application Identifier field.
The reader cannot determine where the data ends from the Application Identifier alone, so an FNC1 separator follows unless this is the last field of the payload. Use for Application Identifiers such as
(10)batch,(21)serial number, or(240)additional product identification.- Parameters:
application_identifier – The 2-4 digit Application Identifier.
value – The data string. Must be non-empty printable ASCII.