reportlab borderRadius is not working - python

I'm using reportlab 3.2 with python 2.7.
I'm trying to make put some text in a rounded panel like so:
but I'm getting:
I've tried using borderRadius but it is not making the corners round:
ps_title2 = ParagraphStyle(styles['Normal'],\
fontName=helper.REG_FONT, fontSize=18, textColor=DARK_BLUE,\
leading=22, backColor=LIGHT_BLUE, borderRadius=15)
pr2 = Paragraph('Interpretation summary<br/>something<br/>another thing', ps_title2)
pr2 = Paragraph('my paragraph<br/>something<br/>another thing', ps_title2)
elements.append(pr2)
I know another approach is using a table and know how to make a table an put the info in a table but do not know how to make the table corners round.
Drawing canvas is not a good idea because I don't know the coordinates of the section because depending on the content length it would change.
Any ideas how to make the corners round?

For borderRadius to work, you need to set the following values as well:
borderWidth
borderPadding
borderColor
borderRadius

I looked through the code and figured out that when border color is not set, border radius will not work; also you need to set borderWidth and borderPadding.
Here is what works:
ps_title2 = ParagraphStyle(styles['Normal'],\
fontName=helper.REG_FONT, fontSize=18, textColor=DARK_BLUE,\
leading=22, backColor=LIGHT_BLUE, borderRadius=15, borderColor=LIGHT_BLUE, borderWidth=1, borderPadding=5)

Related

Pre-existing high-contrast palettes from the ANSI color set, to use in a terminal app?

Looking to convey more information in a Rich Table by using colors (specifically to track which modules given classes come from).
On the web, it's fairly easy on find color palettes that are optimized for contrast, rather than esthetics. Here's a 6 color example. Then it's just question of using the RGB/HSL specs to drive your CSS.
Rich has a nice list of ANSI colors in rich.colors.ANSI_COLOR_NAMES. But there is no indication of what colors would constitute a high-contrast 10-12 color palette.
Are there such lists available, for ANSI colors to be used in terminal apps? Or should I just find a web palette and use rich.colors.Color.from_rgb() to build myself such a palette?
Consider using Rich's themes to give names to your colors. That way you won't have to reference your list. You can do print("Hello [color3]World![/]")
OK, well, that was simple, no need to mess around with from_rgb, because of what styles support.
And, after reading Will's answer, I've modified my original solution to use saved themes. Thanks again for an awesome library, Will!
Alternatively you can use a CSS-like syntax to specify a color with a “#” followed by three pairs of hex characters, or in RGB form with three decimal integers. The following two lines both print “Hello” in the same color (purple):
console.print("Hello", style="#af00ff")
Found a 20-element list here (last 2 are black and white which I am skipping).
hivis.theme.ini
[styles]
; High constrast palette from
; https://sashamaps.net/docs/resources/20-colors/
hivis0 = #e6194b
hivis1 = #3cb44b
hivis2 = #ffe119
hivis3 = #4363d8
hivis4 = #f58231
hivis5 = #911eb4
hivis6 = #46f0f0
hivis7 = #f032e6
hivis8 = #bcf60c
hivis9 = #fabebe
hivis10 = #008080
hivis11 = #e6beff
hivis12 = #9a6324
hivis13 = #fffac8
hivis14 = #800000
hivis15 = #aaffc3
hivis16 = #808000
hivis17 = #ffd8b1
hivis18 = #000075
hivis19 = #808080
;hivis20 = #ffffff
;hivis21 = #000000
script.py
from rich.console import Console
from rich.theme import Theme
with open("hivis.theme.ini") as fi:
theme = Theme.from_file(fi)
palette = [name for name in theme.styles.keys() if name.startswith("hivis")]
console = Console(theme=theme)
console.print("[hivis1] First Hivis. [hivis2] Second Hivis.")
for ix, rgb in enumerate(palette):
console.print(f"[{palette[ix]}] {rgb}")

matplotlib.widgets.TextBox: change color

Is there a way to change TextBox text color at any moment? I tried Google already; my question looks too trivial, but I am still at a loss.
TextBox methods:
: dir(matplotlib.widgets.TextBox)
Out[63]:
[
...
'active',
'begin_typing',
'connect_event',
'disconnect',
'disconnect_events',
'drawon',
'eventson',
'get_active',
'ignore',
'on_submit',
'on_text_change',
'position_cursor',
'set_active',
'set_val',
'stop_typing']
AxesWidget superclass methods:
: dir(matplotlib.widgets.AxesWidget)
Out[64]:
[
...
'active',
'connect_event',
'disconnect_events',
'drawon',
'eventson',
'get_active',
'ignore',
'set_active']
Nothing suggestive. At least, to my eye.
A partial answer only - without knowing the fuller application it isn't obvious whether this helps you. There are 2 pieces of text that you can change the color of: the label and the edit box. Below shows how to change each, once.
import matplotlib.widgets
import matplotlib.pyplot as plt
plt.figure(1)
ax = plt.subplot(111)
tb = matplotlib.widgets.TextBox(ax, "Name:", initial="Jane Doe")
tb.label.set_color('red') # label color
tb.text_disp.set_color('blue') # text inside the edit box
If you just want the label text to be different, that persists. But whenever the text inside the edit box (text_disp) is changed, it will be in black again.
This is because widget recreates the text
(by removing and then re-generating and it will be in black again.
The source for the text create method does not have any arguments that the user could modify (color, font size/weight etc) or include as a TextBox instance attribute.
You could write your own subclass that overrides this method. Or perhaps simply setting it after text has been entered is enough for you?

Testing in Python whether one glyph is a reflection of another in the same font

Inspired by List of all unicode's open/close brackets? I'm trying to find a list of all unicode glyphs in a given font that are reflections of each other. First I just need to be able to test whether one glyph is a reflection of another. Below I have two different attempts (two different implementations of my render_char function) but I'm not able to identify '(' and ')' as mirror images using either one. How can I do this?
from PIL import Image,ImageDraw,ImageFont
import freetype
import numpy as np
def render_char0(c):
# Based on https://github.com/rougier/freetype-py/blob/master/examples/hello-world.py
# Needs numpy (blech) and the image comes out the inverse of the way I expect
face = freetype.Face("/Library/Fonts/Verdana.ttf")
face.set_char_size( 48*64 )
face.load_char(c)
bitmap = face.glyph.bitmap
w,h = bitmap.width, bitmap.rows
Z = np.array(bitmap.buffer, dtype=np.ubyte).reshape(h,w)
return Image.fromarray(Z, mode='L').convert('1')
def render_char1(c):
# Based on https://stackoverflow.com/a/14446201/2829764
verdana_font = ImageFont.truetype("/Library/Fonts/Verdana.ttf", 20, encoding="unic")
text_width, text_height = verdana_font.getsize(c)
canvas = Image.new('RGB', (text_width+10, text_height+10), (255, 255, 255))
draw = ImageDraw.Draw(canvas)
draw.text((5,5), c, font = verdana_font, fill = "#000000")
return canvas
for render_char in [render_char0, render_char1]:
lparen = render_char('(')
rparen = render_char(')')
mirror = lparen.transpose(Image.FLIP_LEFT_RIGHT)
mirror.show()
rparen.show()
print mirror.tobytes() == rparen.tobytes() # False
There is a text file called BidiMirroring.txt in the Unicode plain-text database with a list of all mirrored characters. That file is easy to parse by programs.
Current url is http://www.unicode.org/Public/UNIDATA/BidiMirroring.txt
I don't think using the rendered glyphs can work reliably. There's a lot of reasons why eg. ( and ) are no exact mirror images, like spacing around the character, hinting and anti-aliasing, maybe the font is slightly slanted, or maybe the font designer has just make the two brackets a bit different etc. Other characters are rotated, rather than mirrored, like “ and ” in some fonts, and the Chinese quotation marks 「 and 」.
I think rendering is the wrong aproach. It depends on the font and wether the font knows how to render this. I heard that unicode characters have a specification for this symmetry. Maybe it is encoded in their name. "LEFT" and "RIGHT" "SUBSCRIPT". Have a look at http://xahlee.info/comp/unicode_matching_brackets.html

Images in wx.ListControl are cut off

I have a wx.ListCtrl in REPORT mode and i use an image list to display icons which are 50x50 pixels with SetItemColumnImage. The problem now is that the text I display in the column right of the icon is less than 50 pixels high and the parts of the icons that are higher than the text are cut off.
Is there a way to tell ListCtrl to adjust the row height to the height of the icons? Last refuge would be to change the fontsize of the text, but there should be a better way.
Update:
Here is some of my code:
self.list = util.ListCtrl(nb, style=wx.LC_REPORT|
wx.LC_SINGLE_SEL|wx.LC_NO_HEADER|wx.LC_ALIGN_LEFT)
self.list.InsertColumn(0, 'Avatar', width=-1)
self.list.InsertColumn(1, 'Name', width=-1)
self.list.SetColumnWidth(0, 50)
self.imagelist = wx.ImageList(50, 50, 255, 20)
self.list.SetImageList(self.imagelist, wx.IMAGE_LIST_SMALL)
i = 0
for user in self.users:
self.list.Append(['', user['name']])
if user['avatar']:
bitmap = wx.BitmapFromImage(user['avatar'])
imageidx = self.imagelist.Add(bitmap)
self.list.SetItemColumnImage(i, 0, imageidx)
i += 1
When I remove the LC_REPORT flag the images are completely visible but they are all displayed in one row and the names aren't visible anymore.
Since the images are 50x50, I don't think they qualify as "small" any more. Try using the wx.IMAGE_LIST_NORMAL instead of wx.IMAGE_LIST_SMALL. I can't find anything about manually setting row height, so I'm guessing that's not possible. However, I did find a bug report on this topic that says it was resolved in wx2.9. Are you using 2.9?
Alternatively, you could use the UltimateListCtrl which is pure Python and if it doesn't have that ability, you can probably get it patched quickly as the author is very responsive.
Took me a couple cups of coffee to figure it out.
The call to ImageList.Add should precede ListCtrl.Append (or ListCtrl.InsertItem) in order for the ListCtrl to change the height of its rows according to the height of images in ImageList.
So instead of
for user in self.users:
self.list.Append(['', user['name']])
if user['avatar']:
bitmap = wx.BitmapFromImage(user['avatar'])
imageidx = self.imagelist.Add(bitmap)
self.list.SetItemColumnImage(i, 0, imageidx)
You should go with something like this
for user in self.users:
if user['avatar']:
bitmap = wx.BitmapFromImage(user['avatar'])
imageidx = self.imagelist.Add(bitmap)
self.list.Append(['', user['name']])
if user['avatar']:
self.list.SetItemColumnImage(i, 0, imageidx)
Which looks ugly, until you implement a default avatar:
def_avatar = 'default_avatar.jpg'
for user in self.users:
bitmap = wx.BitmapFromImage(user['avatar'] if user['avatar'] else def_avatar)
imageidx = self.imagelist.Add(bitmap)
self.list.Append(['', user['name']])
self.list.SetItemColumnImage(i, 0, imageidx)

Dynamic Spacer in ReportLab

I'm automatically generating a PDF-file with Platypus that has dynamic content.
This means that it might happen that the length of the text content (which is directly at the bottom of the pdf-file) may vary.
However, it might happen that a page break is done in cases where the content is too long.
This is because i use a "static" spacer:
s = Spacer(width=0, height=23.5*cm)
as i always want to have only one page, I somehow need to dynamically set the height of the Spacer, so that the "rest" of the space that is left on the page is taken by the Spacer as its height.
Now, how do i get the "rest" of height that is left on my page?
I sniffed around in the reportlab library a bit and found the following:
Basically, I decided to use a frame into which the flowables will be printed. f._aH returns the height of the Frame (we could also calculate this by hand). Subtracting the heights of the other two flowables, which we get through wrap, we get the remaining height which is the height of the Spacer.
elements.append(Flowable1)
elements.append(Flowable2)
c = Canvas(path)
f = Frame(fx, fy,fw,fh,showBoundary=0)
# compute the available height for the spacer
sheight = f._aH - (Flowable1.wrap(f._aW,f._aH)[1] + Flowable2.wrap(f._aW,f._aH)[1])
# create spacer
s = Spacer(width=0, height=sheight)
# insert the spacer between the two flowables
elements.insert(1,s)
# create a frame from the list of elements
f.addFromList(elements,c)
c.save()
tested and works fine.
As far as i can see you want to have footer, right?
Then you should do it like:
def _laterPages(canvas, doc):
canvas.drawImage(os.path.join(settings.PROJECT_ROOT, 'templates/documents/pics/footer.png'), left_margin, bottom_margin - 0.5*cm, frame_width, 0.5*cm)
doc = BaseDocTemplate(filename,showBoundary=False)
doc.multiBuild(flowble elements, _firstPage, _laterPages)

Categories