steenzout.barcode.writer module

class Base(initialize=None, paint_module=None, paint_text=None, finish=None)[source]

Bases: steenzout.object.Object, steenzout.barcode.writer.Interface

Base class for all writers.

Initializes the basic writer options. Sub-classes can add more attributes and can set them directly or using self.set_options(option=value).

Parameters:
  • initialize (function) – Callback for initializing the inheriting writer. Is called: callback_initialize(raw_code)
  • paint_module (function) – Callback for painting one barcode module. Is called: callback_paint_module(xpos, ypos, width, color)
  • paint_text (function) – Callback for painting the text under the barcode. Is called: callback_paint_text(xpos, ypos) using self.text as text.
  • finish (function) – Callback for doing something with the completely rendered output. Is called: return callback_finish() and must return the rendered output.
calculate_size(modules_per_line, number_of_lines, dpi=300)[source]

Calculates the size of the barcode in pixel.

Parameters:
  • modules_per_line (int) – number of modules in one line.
  • number_of_lines (int) – number of lines of the barcode.
  • dpi (int) – DPI to calculate.
Returns:

Width and height of the barcode in pixel.

Return type:

(tuple[int, int])

register_callback(action, callback)[source]

Register one of the three callbacks if not given at instance creation.

Parameters:
  • action (str) – One of ‘initialize’, ‘paint_module’, ‘paint_text’, ‘finish’.
  • callback (function) – The callback function for the given action.
render(code)[source]

Renders the barcode to whatever the inheriting writer provides, using the registered callbacks.

Parameters:code (list[str]) – list of strings matching the writer spec (only contain 0 or 1).
save(filename, output)[source]

See Interface.save().

set_options(options)[source]

Sets the given options as instance attributes (only if they are known).

Parameters:options (dict) – All known instance attributes and more if the child class has defined them before this call.
DEFAULT_WRITER

alias of SVG

class ImageWriter[source]

Bases: steenzout.barcode.writer.Base

save(filename, output)[source]

See Interface.save().

class Interface[source]

Bases: object

Writer interface.

save(filename, content)[source]

Saves contents to filename.

Parameters:
  • filename (str) – filename without extension.
  • content (str) – output of rendering process.
Returns:

the filename.

Return type:

(str)

class SVG[source]

Bases: steenzout.barcode.writer.Base

save(filename, content)[source]

See Interface.save().

create_svg_object()[source]

Returns a blank SVG document.

Returns:XML document.
Return type:(xml.dom.minidom.DocumentType)
mm2px(value, dpi=300)[source]

Converts the given value in millimeters into dots per inch.

Parameters:
  • value (float) – value, in millimeters.
  • dpi (int) – resolution, in dots per inch.
Returns:

value, in dots per inch.

Return type:

(float)

pt2mm(value)[source]

Converts given value in points to millimeters.

Parameters:value (int) – value in points.
Returns:value, in millimeters.
Return type:(float)