Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
Additions declaration mode for FontPage class.
Additions test_hex_color method on TestFontPreview class.
Additions test_hex_color method on TestFontPreview class.
Additions test_declarative_object method on TestFontPreview class.
Additions test_other_color_system method on TestFontPreview class.
Fix TestFontPreview.test_font_size method and FontPage.__compose.
method.Fix test_text_position method on TestFontPreview class.
  • Loading branch information
MatteoGuadrini committed Dec 12, 2020
2 parents 65259e7 + e99385d commit ea1288c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 17 deletions.
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Release notes

## 1.1.0
Dec 12, 2020

- Additions declaration mode for *FontPage* class
- Additions *test_hex_color* method on TestFontPreview class
- Additions *test_hex_color* method on TestFontPreview class
- Additions *test_declarative_object* method on TestFontPreview class
- Additions *test_other_color_system* method on TestFontPreview class
- Fix TestFontPreview.test_font_size method and FontPage.__compose method
- Fix test_text_position method on TestFontPreview class

## 1.0.0
Dec 5, 2020

Expand Down
13 changes: 8 additions & 5 deletions docs/source/example.rst
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ The units (default 6) are equal parts divided across the height of the page.
Declarative object creation
***************************

Each FontPreview based object in this module has a declarative instance implementation.
Each *FontPreview* and *FontPage* based object in this module has a declarative instance implementation.

.. code-block:: python
from fontpreview import FontPreview, FontBanner, FontLogo
from fontpreview import FontPreview, FontBanner, FontLogo, FontPage
# FontPreview object
fp = FontPreview('/tmp/noto.ttf',
font_size=50,
Expand All @@ -275,10 +275,13 @@ Each FontPreview based object in this module has a declarative instance implemen
font_size=70,
color_system='RGB')
# FontLogo object
fb = FontLogo('/tmp/noto.ttf',
fl = FontLogo('/tmp/noto.ttf',
'Fl',
size=(170, 170),
bg_color='blue',
fg_color='yellow',
bg_color='yellow',
fg_color='blue',
font_size=50,
color_system='RGB')
# FontPage object
page = FontPage(header=fb, logo=fl, body=fb, footer=fb)
page.draw()
42 changes: 31 additions & 11 deletions fontpreview/fontpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,44 @@ class FontPage:
Class that represents the page of a font banners
"""

def __init__(self, template=None, dimension=(2480, 3508)):
def __init__(self, template=None, dimension=(2480, 3508), header=None, logo=None, body=None, footer=None):
"""
Object that represents the page of a font banners
:param template: template used to build the page
:param dimension: dimension of page. Default A4 in pixels.
:param header: header of fontpage object
:param logo: logo of fontpage object on header part
:param body: body of fontpage object
:param footer: footer of fontpage object
"""
self.logo = None
self.header = None
self.body = None
self.footer = None
self.template = template
if self.template:
self.dimension = (dimension[0], self.template.page_height)
else:
self.dimension = dimension
self.color_system = 'RGB'
self.page = Image.new(self.color_system, self.dimension, color='white')
# Set header
if header:
self.set_header(header)
else:
self.header = None
# Set logo
if logo:
self.set_logo(logo)
else:
self.logo = None
# Set body
if body:
self.set_body(body)
else:
self.body = None
# Set footer
if footer:
self.set_footer(footer)
else:
self.footer = None

def __str__(self):
"""
Expand Down Expand Up @@ -80,20 +100,20 @@ def __compose(self):
self.set_header(self.header)
if self.header.image.height != self.template.header_units:
self.header.dimension = (self.page.width, self.template.header_units)
self.header.set_font_size(self.template.header_font_size)
self.header.set_text_position(self.template.header_text_position)
self.header.set_font_size(self.template.header_font_size)
self.header.set_text_position(self.template.header_text_position)
# Compose body
self.set_body(self.body)
if self.body.image.height != self.template.body_units:
self.body.dimension = (self.page.width, self.template.body_units)
self.body.set_font_size(self.template.body_font_size)
self.body.set_text_position(self.template.body_text_position)
self.body.set_font_size(self.template.body_font_size)
self.body.set_text_position(self.template.body_text_position)
# Compose footer
self.set_footer(self.footer)
if self.footer.image.height != self.template.footer_units:
self.footer.dimension = (self.page.width, self.template.footer_units)
self.footer.set_font_size(self.template.footer_font_size)
self.footer.set_text_position(self.template.footer_text_position)
self.footer.set_font_size(self.template.footer_font_size)
self.footer.set_text_position(self.template.footer_text_position)
except AttributeError:
raise AttributeError('header, body and footer is mandatory object')

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from setuptools import setup
__version__ = '1.0.0'
__version__ = '1.1.0'

with open("README.md", "r") as fh:
long_description = fh.read()
Expand Down
87 changes: 87 additions & 0 deletions test_fp.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ def test_font_size(self):
# Test FontBanner font size
self.fl.set_font_size(50)
self.assertEqual(self.fl.font.size, 50)
# Test FontPage and FontPageTemplate font size
template = FontPageTemplate()
template.set_header(90, 1, 'lcenter')
template.set_body(90, 3, 'lcenter')
template.set_footer(90, 2, 'lcenter')
fpage = FontPage(template=template)
fpage.set_header(self.fb)
fpage.set_body(self.fb)
fpage.set_footer(self.fb)
fpage.draw()
# FontPageTemplate font size
self.assertEqual(template.header_font_size, 90)
self.assertEqual(template.body_font_size, 90)
self.assertEqual(template.footer_font_size, 90)
# FontPage font size
self.assertEqual(fpage.header.font.size, 90)
self.assertEqual(fpage.body.font.size, 90)
self.assertEqual(fpage.footer.font.size, 90)

def test_text_position(self):
# Test FontPreview font size
Expand All @@ -96,6 +114,12 @@ def test_text_position(self):
self.fb.set_text_position('rcenter')
# Test FontBanner font size
self.fl.set_text_position('center')
# Test FontPreview font size
self.fp.set_text_position((100, 100))
# Test FontBanner font size
self.fb.set_text_position((100, 100))
# Test FontBanner font size
self.fl.set_text_position((100, 100))

def test_set_text(self):
# Test FontPreview font size
Expand Down Expand Up @@ -147,6 +171,69 @@ def test_template_page(self):
self.assertEqual(self.fpage_t.footer_font_size, 100)
self.assertEqual(self.fpage_t.footer_units, self.fpage_t.unit * 2)

def test_declarative_object(self):
# FontPreview object
fp = FontPreview(font,
font_size=50,
font_text='some text',
color_system='RGB',
bg_color='blue',
fg_color='yellow',
dimension=(800, 400))
# FontBanner object
fb = FontBanner(font,
orientation='portrait',
bg_color='blue',
fg_color='yellow',
mode='paragraph',
font_size=70,
color_system='RGB')
# FontLogo object
fl = FontLogo(font,
'Fl',
size=(170, 170),
bg_color='yellow',
fg_color='blue',
font_size=50,
color_system='RGB')
# FontPage object
fpage = FontPage(header=fb, logo=fl, body=fb, footer=fb)
# test if instance has been created
self.assertIsInstance(fp, FontPreview)
self.assertIsInstance(fb, FontBanner)
self.assertIsInstance(fl, FontLogo)
self.assertIsInstance(fpage, FontPage)

def test_other_color_system(self):
fp = self.fp
fb = self.fb
fl = self.fl
# define new color system
fp.color_system = 'CMYK'
# change background color
fp.bg_color = fb.bg_color = fl.bg_color = (51, 153, 193)
# change background color
fp.fg_color = fb.fg_color = fl.fg_color = (253, 194, 45)
# test draw it
fp.draw()
fb.draw()
fl.draw()

def test_hex_color(self):
fp = self.fp
fb = self.fb
fl = self.fl
# define new color system
fp.color_system = 'CMYK'
# change background color
fp.bg_color = fb.bg_color = fl.bg_color = "#269cc3"
# change background color
fp.fg_color = fb.fg_color = fl.fg_color = "#ff0000"
# test draw it
fp.draw()
fb.draw()
fl.draw()


if __name__ == '__main__':
unittest.main()

0 comments on commit ea1288c

Please sign in to comment.