Documents and Elements¶
A novasvg.Document is loaded from a file or from a string, can be queried
with CSS selectors, styled with a stylesheet and rendered. Its tree is made of
novasvg.Element and novasvg.TextNode objects, both of which derive
from novasvg.Node.
import novasvg
doc = novasvg.Document.load_from_file("artwork.svg")
for rect in doc.query_selector_all("rect"):
print(rect)
PDF¶
render_to_pdf() returns a one-page vector PDF as bytes and save_pdf() writes
it to a file. Shapes and text stay vector at any zoom, <mask> and clip paths are PDF
soft masks, the text can be selected, searched and copied, and the document’s <title>
(novasvg.Document.title) is the PDF’s title. PdfMode.RASTER makes the page one
bitmap at a given dpi instead.
doc = novasvg.Document.load_from_file("artwork.svg")
data = doc.render_to_pdf() # PdfMode.VECTOR, 192 dpi for what must be a bitmap
doc.save_pdf("artwork.pdf", novasvg.PdfMode.VECTOR)
A PDF luminosity mask weighs red, green and blue as 0.30 / 0.59 / 0.11, an SVG luminance
mask as 0.2125 / 0.7154 / 0.0721: grey and gradient masks are identical, a saturated colour
in a mask is not (use PdfMode.RASTER if that matters).
Reference¶
- class Document¶
- apply_stylesheet(self, content: str) None¶
Apply a CSS stylesheet to the document.
- bounding_box(self) novasvg.Box¶
Get the bounding box of the document content.
- document_element(self) novasvg.Element¶
Get the root element of the document.
- element_from_point(self, x: float, y: float) novasvg.Element¶
Get the topmost element at a specific point.
- force_layout(self) None¶
Force an immediate layout update.
- get_element_by_id(self, id: str) novasvg.Element¶
Get an element by its ID.
- query_selector_all(self, content: str) list[novasvg.Element]¶
Select elements matching a CSS selector.
- render(self, bitmap: novasvg.Bitmap, matrix: novasvg.Matrix = <novasvg.Matrix object at 0x74e4ae071110>) None¶
Render the document onto a bitmap.
- render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: novasvg.Color = <novasvg.Color object at 0x74e4ae1d17b0>) novasvg.Bitmap¶
- render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: int = 0) novasvg.Bitmap
Overloaded function.
render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: novasvg.Color = <novasvg.Color object at 0x74e4ae1d17b0>) -> novasvg.Bitmap
Render the document to a new Bitmap.
render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: int = 0) -> novasvg.Bitmap
Render the document using a packed 0xRRGGBBAA background color.
- render_to_pdf(self, mode: novasvg.PdfMode = PdfMode.VECTOR, dpi: float = 192.0, text: novasvg.PdfText = PdfText.OUTLINES) bytes¶
Render the document as a one-page PDF and return its bytes (empty when the document has no size). PdfMode.VECTOR keeps shapes and text vector, PdfMode.RASTER makes the page one bitmap at dpi. text (PdfText, vector mode): OUTLINES draws letters as shapes (with an invisible text layer for selecting and searching), EMBED writes real text with an embedded subset of the font (a font whose license forbids it, or that cannot be subset, stays outlines, with a warning), EMBED_IGNORE_LICENSE embeds without reading the license: you answer for it.
- save_pdf(self, filename: str, mode: novasvg.PdfMode = PdfMode.VECTOR, dpi: float = 192.0, text: novasvg.PdfText = PdfText.OUTLINES) bool¶
Write render_to_pdf() to a file; False when nothing could be rendered or written.
- update_layout(self) None¶
Update the layout of the document if needed.
- property height¶
Get the intrinsic height of the document.
- load_from_data = <nanobind.nb_func object>¶
- load_from_file = <nanobind.nb_func object>¶
- property title¶
The text of the root <svg>’s first <title> child, ‘’ when there is none. It is the PDF’s /Title.
- property width¶
Get the intrinsic width of the document.
- class PdfMode(value)¶
- class Element(*args, **kwargs)¶
- children(self) list[novasvg.Node]¶
Get child nodes.
- get_attribute(self, name: str) str¶
Get an attribute value.
- get_bounding_box(self) novasvg.Box¶
Get the bounding box without transformations.
- get_global_bounding_box(self) novasvg.Box¶
Get the global bounding box.
- get_global_matrix(self) novasvg.Matrix¶
Get the global transformation matrix.
- get_local_bounding_box(self) novasvg.Box¶
Get the local bounding box.
- get_local_matrix(self) novasvg.Matrix¶
Get the local transformation matrix.
- has_attribute(self, name: str) bool¶
Check if an attribute exists.
- render(self, bitmap: novasvg.Bitmap, matrix: novasvg.Matrix = <novasvg.Matrix object at 0x74e4ae070570>) None¶
Render the element onto a bitmap.
- render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: novasvg.Color = <novasvg.Color object at 0x74e4ae1d1570>) novasvg.Bitmap¶
- render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: int = 0) novasvg.Bitmap
Overloaded function.
render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: novasvg.Color = <novasvg.Color object at 0x74e4ae1d1570>) -> novasvg.Bitmap
Render the element to a new Bitmap.
render_to_bitmap(self, width: int = -1, height: int = -1, backgroundColor: int = 0) -> novasvg.Bitmap
Render the element using a packed 0xRRGGBBAA background color.
- set_attribute(self, name: str, value: str) None¶
Set an attribute value.
- class Node¶
- is_element(self) bool¶
Check if the node is an element node.
- is_null(self) bool¶
Check if the node is null.
- is_text_node(self) bool¶
Check if the node is a text node.
- parent_element(self) novasvg.Element¶
Get the parent element.
- to_element(self) novasvg.Element¶
Convert the node to an Element.
- to_text_node(self) novasvg.TextNode¶
Convert the node to a TextNode.