Skip to content

Supporting <text> tags from SVG files #1029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fpdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
from .fpdf import (
FPDF,
TitleStyle,
FPDF_FONT_DIR as _FPDF_FONT_DIR,
FPDF_VERSION as _FPDF_VERSION,
)
from .html import HTMLMixin, HTML2FPDF
from .prefs import ViewerPreferences
from .template import Template, FlexTemplate
from .text_renderer import FPDF_FONT_DIR as _FPDF_FONT_DIR
from .util import get_scale_factor

try:
Expand Down
54 changes: 27 additions & 27 deletions fpdf/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3104,13 +3104,13 @@ class DrawingContext:
def __init__(self):
self._subitems = []

def add_item(self, item, _copy=True):
def add_item(self, item, clone=True):
"""
Append an item to this drawing context

Args:
item (GraphicsContext, PaintedPath): the item to be appended.
_copy (bool): if true (the default), the item will be copied before being
clone (bool): if true (the default), the item will be copied before being
appended. This prevents modifications to a referenced object from
"retroactively" altering its style/shape and should be disabled with
caution.
Expand All @@ -3119,7 +3119,7 @@ def add_item(self, item, _copy=True):
if not isinstance(item, (GraphicsContext, PaintedPath)):
raise TypeError(f"{item} doesn't belong in a DrawingContext")

if _copy:
if clone:
item = copy.deepcopy(item)

self._subitems.append(item)
Expand Down Expand Up @@ -3358,24 +3358,24 @@ def transform_group(self, transform):
ctxt.transform = transform
yield self

def add_path_element(self, item, _copy=True):
def add_path_element(self, item, clone=True):
"""
Add the given element as a path item of this path.

Args:
item: the item to add to this path.
_copy (bool): if true (the default), the item will be copied before being
clone (bool): if true (the default), the item will be copied before being
appended. This prevents modifications to a referenced object from
"retroactively" altering its style/shape and should be disabled with
caution.
"""
if self._starter_move is not None:
self._closed = False
self._graphics_context.add_item(self._starter_move, _copy=False)
self._graphics_context.add_item(self._starter_move, clone=False)
self._close_context = self._graphics_context
self._starter_move = None

self._graphics_context.add_item(item, _copy=_copy)
self._graphics_context.add_item(item, clone=clone)

def remove_last_path_element(self):
self._graphics_context.remove_last_item()
Expand Down Expand Up @@ -3405,7 +3405,7 @@ def rectangle(self, x, y, w, h, rx=0, ry=0):

self._insert_implicit_close_if_open()
self.add_path_element(
RoundedRectangle(Point(x, y), Point(w, h), Point(rx, ry)), _copy=False
RoundedRectangle(Point(x, y), Point(w, h), Point(rx, ry)), clone=False
)
self._closed = True
self.move_to(x, y)
Expand Down Expand Up @@ -3440,7 +3440,7 @@ def ellipse(self, cx, cy, rx, ry):
The path, to allow chaining method calls.
"""
self._insert_implicit_close_if_open()
self.add_path_element(Ellipse(Point(rx, ry), Point(cx, cy)), _copy=False)
self.add_path_element(Ellipse(Point(rx, ry), Point(cx, cy)), clone=False)
self._closed = True
self.move_to(cx, cy)

Expand Down Expand Up @@ -3484,7 +3484,7 @@ def move_relative(self, x, y):
self._insert_implicit_close_if_open()
if self._starter_move is not None:
self._closed = False
self._graphics_context.add_item(self._starter_move, _copy=False)
self._graphics_context.add_item(self._starter_move, clone=False)
self._close_context = self._graphics_context
self._starter_move = RelativeMove(Point(x, y))
return self
Expand All @@ -3500,7 +3500,7 @@ def line_to(self, x, y):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(Line(Point(x, y)), _copy=False)
self.add_path_element(Line(Point(x, y)), clone=False)
return self

def line_relative(self, dx, dy):
Expand All @@ -3517,7 +3517,7 @@ def line_relative(self, dx, dy):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(RelativeLine(Point(dx, dy)), _copy=False)
self.add_path_element(RelativeLine(Point(dx, dy)), clone=False)
return self

def horizontal_line_to(self, x):
Expand All @@ -3531,7 +3531,7 @@ def horizontal_line_to(self, x):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(HorizontalLine(x), _copy=False)
self.add_path_element(HorizontalLine(x), clone=False)
return self

def horizontal_line_relative(self, dx):
Expand All @@ -3547,7 +3547,7 @@ def horizontal_line_relative(self, dx):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(RelativeHorizontalLine(dx), _copy=False)
self.add_path_element(RelativeHorizontalLine(dx), clone=False)
return self

def vertical_line_to(self, y):
Expand All @@ -3561,7 +3561,7 @@ def vertical_line_to(self, y):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(VerticalLine(y), _copy=False)
self.add_path_element(VerticalLine(y), clone=False)
return self

def vertical_line_relative(self, dy):
Expand All @@ -3577,7 +3577,7 @@ def vertical_line_relative(self, dy):
Returns:
The path, to allow chaining method calls.
"""
self.add_path_element(RelativeVerticalLine(dy), _copy=False)
self.add_path_element(RelativeVerticalLine(dy), clone=False)
return self

def curve_to(self, x1, y1, x2, y2, x3, y3):
Expand All @@ -3599,7 +3599,7 @@ def curve_to(self, x1, y1, x2, y2, x3, y3):
ctrl2 = Point(x2, y2)
end = Point(x3, y3)

self.add_path_element(BezierCurve(ctrl1, ctrl2, end), _copy=False)
self.add_path_element(BezierCurve(ctrl1, ctrl2, end), clone=False)
return self

def curve_relative(self, dx1, dy1, dx2, dy2, dx3, dy3):
Expand Down Expand Up @@ -3633,7 +3633,7 @@ def curve_relative(self, dx1, dy1, dx2, dy2, dx3, dy3):
c2d = Point(dx2, dy2)
end = Point(dx3, dy3)

self.add_path_element(RelativeBezierCurve(c1d, c2d, end), _copy=False)
self.add_path_element(RelativeBezierCurve(c1d, c2d, end), clone=False)
return self

def quadratic_curve_to(self, x1, y1, x2, y2):
Expand All @@ -3651,7 +3651,7 @@ def quadratic_curve_to(self, x1, y1, x2, y2):
"""
ctrl = Point(x1, y1)
end = Point(x2, y2)
self.add_path_element(QuadraticBezierCurve(ctrl, end), _copy=False)
self.add_path_element(QuadraticBezierCurve(ctrl, end), clone=False)
return self

def quadratic_curve_relative(self, dx1, dy1, dx2, dy2):
Expand All @@ -3673,7 +3673,7 @@ def quadratic_curve_relative(self, dx1, dy1, dx2, dy2):
"""
ctrl = Point(dx1, dy1)
end = Point(dx2, dy2)
self.add_path_element(RelativeQuadraticBezierCurve(ctrl, end), _copy=False)
self.add_path_element(RelativeQuadraticBezierCurve(ctrl, end), clone=False)
return self

def arc_to(self, rx, ry, rotation, large_arc, positive_sweep, x, y):
Expand Down Expand Up @@ -3720,7 +3720,7 @@ def arc_to(self, rx, ry, rotation, large_arc, positive_sweep, x, y):
end = Point(x, y)

self.add_path_element(
Arc(radii, rotation, large_arc, positive_sweep, end), _copy=False
Arc(radii, rotation, large_arc, positive_sweep, end), clone=False
)
return self

Expand Down Expand Up @@ -3769,21 +3769,21 @@ def arc_relative(self, rx, ry, rotation, large_arc, positive_sweep, dx, dy):
end = Point(dx, dy)

self.add_path_element(
RelativeArc(radii, rotation, large_arc, positive_sweep, end), _copy=False
RelativeArc(radii, rotation, large_arc, positive_sweep, end), clone=False
)
return self

def close(self):
"""
Explicitly close the current (sub)path.
"""
self.add_path_element(Close(), _copy=False)
self.add_path_element(Close(), clone=False)
self._closed = True
self.move_relative(0, 0)

def _insert_implicit_close_if_open(self):
if not self._closed:
self._close_context.add_item(ImplicitClose(), _copy=False)
self._close_context.add_item(ImplicitClose(), clone=False)
self._close_context = self._graphics_context
self._closed = True

Expand Down Expand Up @@ -3970,19 +3970,19 @@ def clipping_path(self):
def clipping_path(self, new_clipath):
self._clipping_path = new_clipath

def add_item(self, item, _copy=True):
def add_item(self, item, clone=True):
"""
Add a path element to this graphics context.

Args:
item: the path element to add. May be a primitive element or another
`GraphicsContext` or a `PaintedPath`.
_copy (bool): if true (the default), the item will be copied before being
clone (bool): if true (the default), the item will be copied before being
appended. This prevents modifications to a referenced object from
"retroactively" altering its style/shape and should be disabled with
caution.
"""
if _copy:
if clone:
item = copy.deepcopy(item)

self.path_items.append(item)
Expand Down
6 changes: 3 additions & 3 deletions fpdf/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __deepcopy__(self, _memo):

from .deprecation import get_stack_level
from .drawing import convert_to_device_color, DeviceGray, DeviceRGB
from .enums import FontDescriptorFlags, TextEmphasis, Align
from .enums import Align, FontDescriptorFlags, TextEmphasis
from .syntax import Name, PDFObject
from .util import escape_parens

Expand Down Expand Up @@ -220,8 +220,8 @@ class CoreFont:
"emphasis",
)

def __init__(self, fpdf, fontkey, style):
self.i = len(fpdf.fonts) + 1
def __init__(self, i, fontkey, style):
self.i = i
self.type = "core"
self.name = CORE_FONTS[fontkey]
self.sp = 250 # strikethrough horizontal position
Expand Down
Loading