From 176c51d3caaa77daddcb4c6a0be11b877bc61637 Mon Sep 17 00:00:00 2001 From: Brian Matherly Date: Fri, 26 Jan 2007 15:06:52 +0000 Subject: [PATCH] 0000876: Calendar would print beyond a day's borders if too many, or too long svn: r7992 --- ChangeLog | 3 +++ src/FontScale.py | 60 +++++++++++++++++++++++++++++++++++++++++ src/plugins/Calendar.py | 8 ++++++ 3 files changed, 71 insertions(+) diff --git a/ChangeLog b/ChangeLog index 6578899d1..fd5265b64 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,9 @@ 2007-01-26 Douglas Blank * src/ReportBase/ReportUtils.py: 0000875: _ReportUtils.py has incorrect conversion cm2pt + * src/FontScale.py: add string_trim() + * src/plugins/Calendar.py: 0000876: Calendar would print beyond a day's + borders if too many, or too long 2007-01-26 Don Allingham * src/DataViews/_MediaView.py: fix accelerator entries diff --git a/src/FontScale.py b/src/FontScale.py index c7cdbe409..3233dad26 100644 --- a/src/FontScale.py +++ b/src/FontScale.py @@ -261,3 +261,63 @@ def string_width(font,text): except: r = r + l[ord('n')] return (r+1)*s + +def string_trim(font, text, width, ellipses = "..."): + """ + Like string_width, but this makes sure the length of the + string is <= width. Optionally, add ellipses (...). + """ + i = font.get_type_face() + j = font.get_bold() + font.get_italic()*2 + s = font.get_size() + l = _font_array[i][j] + ellipses_length = 0 + # get length of each letter + for c in ellipses: + try: + ellipses_length += l[ord(c)] + except: + ellipses_length += l[ord('n')] + # find the part that is < width + retval = "" + sumlen = 0 + length = 0 + for i in range(len(text)): + c = text[i] + try: + length = l[ord(c)] + except: + length = l[ord('n')] + # too long: + if (sumlen + length + 1) * s > width: + if ellipses_length > 0: + # try again with ellipses + retval += c + sumlen += length + break + else: + # return just this so far + return retval + retval += c + sumlen += length + # if exited out the bottom: + if (sumlen + 1) * s <= width: + return text + # too long; try again with ellipses + retval = "" + sumlen = 0 + length = 0 + for i in range(len(text)): + c = text[i] + try: + length = l[ord(c)] + except: + length = l[ord('n')] + if (sumlen + length + 1) * s > width: + return retval + if (sumlen + length + ellipses_length + 1) * s > width: + return retval + ellipses + retval += c + sumlen += length + # should not exit out the bottom! + return text diff --git a/src/plugins/Calendar.py b/src/plugins/Calendar.py index 6730a8bda..3f67f2209 100644 --- a/src/plugins/Calendar.py +++ b/src/plugins/Calendar.py @@ -41,10 +41,12 @@ from PluginUtils import register_report from ReportBase import Report, ReportUtils, ReportOptions, \ CATEGORY_DRAW, CATEGORY_TEXT, MODE_GUI, MODE_BKI, MODE_CLI pt2cm = ReportUtils.pt2cm +cm2pt = ReportUtils.cm2pt from Filters import GenericFilter, ParamFilter, Rules import GrampsLocale import RelLib from Utils import probably_alive +from FontScale import string_trim, string_width #------------------------------------------------------------------------ # @@ -238,6 +240,12 @@ class Calendar(Report): position += (lines * spacing) current = 0 for line in p.split("\n"): + # make sure text will fit: + numpos = pt2cm(self["CAL-Numbers"].get_size()) + if position + (current * spacing) - 0.1 >= cell_height - numpos: # font daynums + continue + font = self["CAL-Text"] + line = string_trim(font, line, cm2pt(cell_width + 0.2)) self.doc.draw_text("CAL-Text", line, day_col * cell_width + 0.1, header + (week_row + 1) * cell_height - position + (current * spacing) - 0.1)