Metadata-Version: 2.5
Name: brother-bridge
Version: 1.0.0
Summary: Parse, render and print Brother P-touch (.lbx) label templates on Windows (b-PAC) and Linux/macOS (Brother QL raster, CUPS).
Project-URL: Homepage, https://github.com/Baromir19/brother
Project-URL: Issues, https://github.com/Baromir19/brother/issues
Project-URL: Changelog, https://github.com/Baromir19/brother/blob/main/CHANGELOG.md
Project-URL: Documentation, https://github.com/Baromir19/brother/blob/main/docs/user-guide.md
Author: brother-bridge contributors
License: GPL-3.0-or-later
License-File: LICENSE
Keywords: b-pac,brother,label,lbx,p-touch,printing,ql,raster
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Printing
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: fonttools>=4.40
Requires-Dist: pillow>=10.1
Provides-Extra: all
Requires-Dist: brother-ql>=0.9.4; extra == 'all'
Requires-Dist: pywin32>=306; (sys_platform == 'win32') and extra == 'all'
Provides-Extra: brother
Requires-Dist: brother-ql>=0.9.4; extra == 'brother'
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: mypy>=1.11; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: twine>=5.0; extra == 'dev'
Provides-Extra: windows
Requires-Dist: pywin32>=306; (sys_platform == 'win32') and extra == 'windows'
Description-Content-Type: text/markdown

# brother-bridge

Parse, fill and print Brother P-touch (`.lbx`) label templates from Python.

The package reads the template archive that P-touch Editor produces, exposes its
text fields, renders the design to a monochrome bitmap and sends it to the
printer. Windows uses the native Brother **b-PAC** COM SDK; Linux and macOS use
the in-memory renderer with either direct Brother raster output or CUPS. The
same code path is used for previews, tests and production printing.

```python
from pathlib import Path

from brother_bridge import LabelData, LabelPrinterService, LabelTemplate, PrintRequest

request = PrintRequest(
    label=LabelTemplate("address", "address.lbx", ("title", "address")),
    data=LabelData({"title": "Ada Lovelace", "address": "London"}),
)

LabelPrinterService(Path("templates")).print_label(request)
```

## Features

- **Template parsing** - reads `label.xml` from the `.lbx` ZIP container and
  builds an in-memory layout of paper, text, barcode and image objects.
- **Rendering without hardware** - converts a layout to a 300 DPI monochrome
  `PIL.Image`, honouring fonts, alignment, word wrap, auto line feed, shrink-to-fit
  and continuous or die-cut tape geometry.
- **Automatic tape resolution** - the `brother_ql` tape identifier is derived
  from the paper geometry, so no format table has to be maintained.
- **Pluggable transport** - direct Brother raster over TCP, CUPS queues or plain
  image files. The backend is an abstract interface and can be replaced.
- **Platform isolation** - the b-PAC (COM) and raster implementations are loaded
  lazily, so the package imports on any OS and degrades with clear errors.
- **Dependency injection** - documents, renderers, parsers, tape resolvers and
  backends are all injectable; nothing is bound to global state.
- **Typed and tested** - `py.typed`, strict MyPy on the package, unit and
  integration tests, CI for Linux and Windows.

## Requirements

| Platform | Printing requires |
| --- | --- |
| Windows | Brother b-PAC SDK (COM) and `pywin32` |
| Linux / macOS | `brother_ql` and a reachable printer (direct raster), or CUPS with a working queue |
| Any | Pillow is the only mandatory runtime dependency |

Rendering, parsing and previews work on every platform with just Pillow;
`fonttools` (installed by default) is used for better font family detection.

## Installation

```bash
pip install brother-bridge            # core: parse and render
pip install "brother-bridge[brother]" # Linux/macOS raster printing
pip install "brother-bridge[windows]" # Windows b-PAC printing (pywin32)
pip install "brother-bridge[all]"     # everything
```

The `brother` extra installs the `brother_ql` release available on PyPI. Models
and labels introduced after that release (for example the QL-1110NWB) require the
upstream build, so install it before the package:

```bash
pip install "brother-ql @ git+https://github.com/pklaus/brother_ql.git"
pip install brother-bridge
```

Windows printing does not need `brother_ql` at all.

Development install:

```bash
git clone https://github.com/Baromir19/brother
cd brother
python -m venv .venv
.venv/Scripts/activate    # Windows
pip install -e ".[all,dev]"
```

## Quick start

### Print a label

```python
from pathlib import Path

from brother_bridge import LabelData, LabelPrinterService, LabelTemplate, PrintRequest

service = LabelPrinterService(Path("templates"))
request = PrintRequest(
    label=LabelTemplate(
        name="address",
        template="address.lbx",
        fields=("title", "address"),
    ),
    data=LabelData(
        {
            "title": "Ada Lovelace",
            "address": "12 rue de la Paix\n75002 Paris",
        }
    ),
    copies=2,
    printer="",  # empty: platform default or the printer stored in the template
)
service.print_label(request)
```

### Render a preview

```python
from pathlib import Path

from brother_bridge import FileBackend, LbxParser, LbxRenderer

layout = LbxParser().parse_file(Path("templates/address.lbx"))
layout.text_objects["title"].set_text("Ada Lovelace")

image = LbxRenderer().render(layout)
image.save("preview.png")
FileBackend(Path("output")).print(image, printer="", copies=1)
```

### Drive a platform document directly

```python
from brother_bridge import PrinterConfig, document_factory

config = PrinterConfig(host="192.168.1.50", model="QL-1110NWB")
with document_factory(config) as document:
    document.open("templates/address.lbx")
    document.set_fields({"title": "Ada Lovelace", "address": "London"})
    document.print(copies=1)
```

## Configuration

`PrinterConfig` is an immutable dataclass; it can be built explicitly or from
the environment. Malformed values raise `ConfigurationError` instead of failing
later at print time.

| Variable | Default | Meaning |
| --- | --- | --- |
| `BROTHER_BRIDGE_HOST` | `10.10.4.44` | Printer address for the raster backend |
| `BROTHER_BRIDGE_MODEL` | `QL-1110NWB` | Model name understood by `brother_ql` |
| `BROTHER_BRIDGE_LABEL` | auto | Explicit tape identifier; empty means auto-detect |
| `BROTHER_BRIDGE_PORT` | `9100` | Raw TCP port |
| `BROTHER_BRIDGE_CUT` | `true` | Cut after each copy |
| `BROTHER_BRIDGE_THRESHOLD` | `70` | Black/white threshold in percent |
| `BROTHER_BRIDGE_DITHER` | `false` | Dither instead of thresholding |
| `BROTHER_BRIDGE_DPI` | `300` | Rendering resolution |
| `BROTHER_BRIDGE_TEMPLATE_DIR` | unset | Default template directory |
| `BROTHER_BRIDGE_LOCALE` | `en` | Locale of exception messages (`en`, `fr`) |

## Public API

The stable surface is exported from the package root and listed in
`brother_bridge.__all__`:

- Models: `LabelTemplate`, `LabelData`, `PrintRequest`, `PrintMode`
- Pipeline: `LbxParser`, `LbxRenderer`, `FontResolver`, `LabelLayout`,
  `TextObject`, `PaperSpec`
- Documents: `document_factory`, `LabelDocument`
- Backends: `PrinterBackend`, `BrotherQlRasterBackend`, `CupsPngBackend`,
  `FileBackend`, `BrotherQlLabelResolver`
- Services: `LabelPrinterService`, `LabelTemplateService`
- Support: `PrinterConfig`, `LbxUtils`, the exception hierarchy

See [`docs/api.md`](docs/api.md) for signatures and [`docs/user-guide.md`](docs/user-guide.md)
for task-oriented guidance.

## Platform behaviour

| | Windows | Linux / macOS |
| --- | --- | --- |
| Document class | `BpacDocument` (COM, b-PAC) | `LinuxBrotherDocument` (in memory) |
| Template handling | Native b-PAC template | Parsed and rendered by this package |
| Transport | b-PAC print job | `BrotherQlRasterBackend` (TCP) or `CupsPngBackend` (`lp`) |
| Tape resolution | Provided by b-PAC | `BrotherQlLabelResolver` from paper geometry |

`document_factory()` picks the implementation for the current platform. Pass
`platform_name="Linux"` or `platform_name="Windows"` to force one, for example in
tests.

## Extending

- **New transport**: implement `PrinterBackend.print(image, printer, copies)` and
  pass an instance to `LinuxBrotherDocument(backend=...)` or
  `LabelPrinterService(..., document_factory=...)`.
- **New document manager**: subclass `LabelDocument` and implement `_open`,
  `_set_field` and `print`.
- **Custom fonts**: `FontResolver(extra_dirs=[...], substitutions={...})`.
- **Custom geometry**: a `tape_spec_provider` callable can replace the
  `brother_ql` catalogue lookup.

## Errors, logging and locale

All package exceptions derive from `PrinterError`. Messages are translated
(English and French ship in the box), while structured attributes such as
`LabelFieldNotFoundError.name` remain available for programmatic handling.

The package never configures the root logger. Add a handler when needed:

```python
from brother_bridge.logging import configure_logging

configure_logging("DEBUG")
```

Set `BROTHER_BRIDGE_LOCALE=fr` (or call `brother_bridge.i18n.set_locale("fr")`) to
localise exception messages.

## Thread safety

`LbxParser`, `LbxRenderer`, `FontResolver`, `BrotherQlLabelResolver` and the
print backends are safe to share between threads. `LabelDocument` instances are
not: one document belongs to one thread and one job, and b-PAC in particular is
COM apartment-bound. Do not share a document between threads.

## Documentation

| Document | Content |
| --- | --- |
| [`docs/user-guide.md`](docs/user-guide.md) | Installation, templates, printing, troubleshooting |
| [`docs/architecture.md`](docs/architecture.md) | Layers, data flow, design decisions, extension points |
| [`docs/developer-guide.md`](docs/developer-guide.md) | Development setup, tests, typing, build, release |
| [`docs/api.md`](docs/api.md) | Public API reference |
| [`CHANGELOG.md`](CHANGELOG.md) | Version history |

## Versioning

The package follows [Semantic Versioning](https://semver.org/). The public API is
the set of names exported from `brother_bridge`; everything else (module-internal
helpers prefixed with `_`) may change in a minor release. Deprecations are
introduced with a warning and removed only in the next major version.

## License

GNU General Public License v3.0 or later. See [`LICENSE`](LICENSE).
`brother_ql` (installed as an optional dependency) is distributed under GPLv3 as
well; Pillow, fonttools and pywin32 keep their own licenses. No third-party code
is vendored into this repository.
