1
0
mirror of https://github.com/gryf/wmdocklib.git synced 2025-12-18 20:10:23 +01:00

Make add_string/add_char methods of base class.

This commit is contained in:
2022-04-18 19:22:39 +02:00
parent ac17fe8b24
commit ec39f9628c
2 changed files with 35 additions and 46 deletions

View File

@@ -185,5 +185,40 @@ class DockApp:
pywmgeneral.include_pixmap(xpm)
def add_char(self, ch, x, y, drawable=None):
"""Paint the character ch at position x, y in the window.
if the character being painted falls partly out of the boundary, it
will be clipped without causing an exception. this works even if the
character starts out of the boundary.
"""
# linelength is the amount of bits the character set uses on each row.
linelength = self.charset_width - (self.charset_width %
self.char_width)
# pos is the horizontal index of the box containing ch.
pos = (ord(ch)-32) * self.char_width
# translate pos into ch_x, ch_y, rolling back and down each linelength
# bits. character definition start at row 64, column 0.
ch_y = int((pos / linelength)) * self.char_height + self.charset_start
ch_x = pos % linelength
target_x = x + self.x_offset
target_y = y + self.y_offset
if drawable is None:
pywmgeneral.copy_xpm_area(ch_x, ch_y, self.char_width,
self.char_height, target_x, target_y)
else:
drawable.xCopyAreaFromWindow(ch_x, ch_y, self.char_width,
self.char_height, target_x, target_y)
def add_string(self, string, x, y, drawable=None):
"""Add a string at the given x and y positions.
Call add_char repeatedely, so the same exception rules apply."""
last_width = 0
for letter in string:
self.add_char(letter, x + last_width, y, drawable)
last_width += self.char_width
def redraw(self):
pywmgeneral.redraw_window()

View File

@@ -64,52 +64,6 @@ def get_center_start_pos(string, areaWidth, offset):
return (text_area - w) / 2
def add_char(ch, x, y, x_offset, y_offset, width, height, drawable=None):
"""Paint the character ch at position x, y in the window.
Return the (width, height) of the character painted. (will be useful if
we implement proportional char sets)
the library only supports lower ascii: 32-127. any other will cause a
ValueError exception.
if the character being painted falls partly out of the boundary, it will
be clipped without causing an exception. this works even if the
character starts out of the boundary.
"""
# linelength is the amount of bits the character set uses on each row.
linelength = charset_width - (charset_width % char_width)
# pos is the horizontal index of the box containing ch.
pos = (ord(ch)-32) * char_width
# translate pos into ch_x, ch_y, rolling back and down each linelength
# bits. character definition start at row 64, column 0.
ch_y = int((pos / linelength)) * char_height + charset_start
ch_x = pos % linelength
target_x = x + x_offset
target_y = y + y_offset
char_width
if drawable is None:
pywmgeneral.copy_xpm_area(ch_x, ch_y, char_width, char_height,
target_x, target_y)
else:
drawable.xCopyAreaFromWindow(ch_x, ch_y, char_width, char_height,
target_x, target_y)
return char_width
def add_string(string, x, y, x_offset=0, y_offset=0, width=None, height=None,
drawable=None):
"""Add a string at the given x and y positions.
Call add_char repeatedely, so the same exception rules apply."""
last_width = 0
for letter in string:
width = add_char(letter, x + last_width, y, x_offset, y_offset,
width, height, drawable)
last_width += width
def get_vertical_spacing(num_lines, margin, height, y_offset):
"""Return the optimal spacing between a number of lines.