-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3318fda
commit c6ea9ec
Showing
277 changed files
with
1,237 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
borb version 2.0.16 | ||
borb version 2.0.17 | ||
Joris Schellekens |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
This file is part of the borb (R) project. | ||
Copyright (c) 2020-2040 borb Group NV | ||
Authors: Joris Schellekens, et al. | ||
This program is free software; you can redistribute it and/or modify | ||
it under the terms of the GNU Affero General Public License version 3 | ||
as published by the Free Software Foundation with the addition of the | ||
following permission added to Section 15 as permitted in Section 7(a): | ||
FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY | ||
BORB GROUP. BORB GROUP DISCLAIMS THE WARRANTY OF NON INFRINGEMENT | ||
OF THIRD PARTY RIGHTS | ||
This program is distributed in the hope that it will be useful, but | ||
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | ||
or FITNESS FOR A PARTICULAR PURPOSE. | ||
See the GNU Affero General Public License for more details. | ||
You should have received a copy of the GNU Affero General Public License | ||
along with this program; if not, see http://www.gnu.org/licenses or write to | ||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
Boston, MA, 02110-1301 USA. | ||
The interactive user interfaces in modified source and object code versions | ||
of this program must display Appropriate Legal Notices, as required under | ||
Section 5 of the GNU Affero General Public License. | ||
In accordance with Section 7(b) of the GNU Affero General Public License, | ||
a covered work must retain the producer line in every PDF that is created | ||
or manipulated using borb. | ||
You can be released from the requirements of the license by purchasing | ||
a commercial license. Buying such a license is mandatory as soon as you | ||
develop commercial activities involving the borb software without | ||
disclosing the source code of your own applications. | ||
These activities include: offering paid services to customers as an ASP, | ||
serving PDFs on the fly in a web application, shipping borb with a closed | ||
source product. | ||
For more information, please contact borb Software Corp. at this | ||
address: [email protected] | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
This class represents a generic disjoint shape (specified by a List of lines). | ||
It has convenience methods to calculate width and height, perform scaling, etc | ||
""" | ||
|
||
from decimal import Decimal | ||
|
||
import typing | ||
|
||
from borb.pdf.canvas.color.color import HexColor, Color, X11Color | ||
from borb.pdf.canvas.geometry.rectangle import Rectangle | ||
from borb.pdf.canvas.layout.layout_element import Alignment, LayoutElement | ||
from borb.pdf.page.page import Page | ||
|
||
|
||
class DisjointShape(LayoutElement): | ||
""" | ||
This class represents a generic disjoint shape (specified by a List of lines). | ||
It has convenience methods to calculate width and height, perform scaling, etc | ||
""" | ||
|
||
def __init__( | ||
self, | ||
lines: typing.List[ | ||
typing.Tuple[typing.Tuple[Decimal, Decimal], typing.Tuple[Decimal, Decimal]] | ||
], | ||
stroke_color: Color = HexColor("000000"), | ||
line_width: Decimal = Decimal(0), | ||
horizontal_alignment: Alignment = Alignment.LEFT, | ||
vertical_alignment: Alignment = Alignment.TOP, | ||
preserve_aspect_ratio: bool = True, | ||
): | ||
super(DisjointShape, self).__init__( | ||
horizontal_alignment=horizontal_alignment, | ||
vertical_alignment=vertical_alignment, | ||
) | ||
assert len(lines) > 0 | ||
self._lines = lines | ||
self._stroke_color = stroke_color | ||
self._line_width = line_width | ||
self._preserve_aspect_ratio = preserve_aspect_ratio | ||
|
||
def get_width(self) -> Decimal: | ||
""" | ||
This function returns the width of this DisjointShape | ||
""" | ||
min_x = min([min(x[0][0], x[1][0]) for x in self._lines]) | ||
max_x = max([max(x[0][0], x[1][0]) for x in self._lines]) | ||
return max_x - min_x | ||
|
||
def get_height(self) -> Decimal: | ||
""" | ||
This function returns the height of this DisjointShape | ||
""" | ||
min_y = min([min(x[0][1], x[1][1]) for x in self._lines]) | ||
max_y = max([max(x[0][1], x[1][1]) for x in self._lines]) | ||
return max_y - min_y | ||
|
||
def scale_to_fit(self, max_width: Decimal, max_height: Decimal) -> "DisjointShape": | ||
""" | ||
This method scales this DisjointShape to fit a given max. width / height | ||
""" | ||
w_scale = max_width / self.get_width() | ||
h_scale = max_height / self.get_height() | ||
if self._preserve_aspect_ratio: | ||
w_scale = min(w_scale, h_scale) | ||
h_scale = w_scale | ||
if w_scale < 1: | ||
self._lines = [ | ||
((x[0][0] * w_scale, x[0][1]), (x[1][0] * w_scale, x[1][1])) | ||
for x in self._lines | ||
] | ||
if h_scale < 1: | ||
self._lines = [ | ||
((x[0][0], x[0][1] * h_scale), (x[1][0], x[1][1] * h_scale)) | ||
for x in self._lines | ||
] | ||
return self | ||
|
||
def translate_to_align( | ||
self, lower_left_x: Decimal, lower_left_y: Decimal | ||
) -> "DisjointShape": | ||
""" | ||
This method translates this DisjointShape so its lower left corner aligns with the given coordinates | ||
""" | ||
min_x = min([min(x[0][0], x[1][0]) for x in self._lines]) | ||
min_y = min([min(x[0][1], x[1][1]) for x in self._lines]) | ||
delta_x = lower_left_x - min_x | ||
delta_y = lower_left_y - min_y | ||
self._lines = [ | ||
( | ||
(x[0][0] + delta_x, x[0][1] + delta_y), | ||
(x[1][0] + delta_x, x[1][1] + delta_y), | ||
) | ||
for x in self._lines | ||
] | ||
return self | ||
|
||
def _do_layout_without_padding( | ||
self, page: Page, bounding_box: Rectangle | ||
) -> Rectangle: | ||
|
||
# scale to fit | ||
self.scale_to_fit(bounding_box.width, bounding_box.height) | ||
|
||
# translate points to fit in box | ||
self.translate_to_align( | ||
bounding_box.x, bounding_box.y + bounding_box.height - self.get_height() | ||
) | ||
|
||
# write content | ||
stroke_rgb = (self._stroke_color or X11Color("Black")).to_rgb() | ||
content = "q %f %f %f RG %d w " % ( | ||
Decimal(stroke_rgb.red), | ||
Decimal(stroke_rgb.green), | ||
Decimal(stroke_rgb.blue), | ||
self._line_width, | ||
) | ||
for l in self._lines: | ||
content += " %f %f m %f %f l " % (l[0][0], l[0][1], l[1][0], l[1][1]) | ||
|
||
# stroke | ||
content += " S Q" | ||
|
||
# append to page | ||
self._append_to_content_stream(page, content) | ||
|
||
# calculate bounding box | ||
layout_rect = Rectangle( | ||
bounding_box.x, | ||
bounding_box.y + bounding_box.height - self.get_height(), | ||
self.get_width(), | ||
self.get_height(), | ||
) | ||
|
||
# set bounding box | ||
self.set_bounding_box(layout_rect) | ||
|
||
# return | ||
return layout_rect |
Oops, something went wrong.