diff --git a/src/ImgManip.py b/src/ImgManip.py index 2a2b7efb3..cc7369f99 100644 --- a/src/ImgManip.py +++ b/src/ImgManip.py @@ -185,24 +185,38 @@ def image_actual_size(x_cm, y_cm, x, y): # resize_to_jpeg_buffer # #------------------------------------------------------------------------- -def resize_to_jpeg_buffer(source, width, height): +def resize_to_jpeg_buffer(source, size, crop=None): """ Loads the image, converting the file to JPEG, and resizing it. Instead of saving the file, the data is returned in a buffer. :param source: source image file, in any format that gtk recognizes :type source: unicode - :param width: desired width of the destination image - :type width: int - :param height: desired height of the destination image - :type height: int + :param size: desired size of the destination image ([width, height]) + :type size: list + :param crop: cropping coordinates + :type crop: array of integers ([start_x, start_y, end_x, end_y]) :rtype: buffer of data :returns: jpeg image as raw data """ import gtk filed, dest = tempfile.mkstemp() img = gtk.gdk.pixbuf_new_from_file(source) - scaled = img.scale_simple(int(width), int(height), gtk.gdk.INTERP_BILINEAR) + + if crop: + # Gramps cropping coorinates are [0, 100], so we need to convert to pixels + start_x = int((crop[0]/100.0)*img.get_width()) + start_y = int((crop[1]/100.0)*img.get_height()) + end_x = int((crop[2]/100.0)*img.get_width()) + end_y = int((crop[3]/100.0)*img.get_height()) + + img = img.subpixbuf(start_x, start_y, end_x-start_x, end_y-start_y) + + # Need to keep the ratio intact, otherwise scaled images look stretched + # if the dimensions aren't close in size + (size[0], size[1]) = image_actual_size(size[0], size[1], img.get_width(), img.get_height()) + + scaled = img.scale_simple(int(size[0]), int(size[1]), gtk.gdk.INTERP_BILINEAR) os.close(filed) dest = Utils.get_unicode_path_from_env_var(dest) scaled.save(dest, 'jpeg') diff --git a/src/plugins/docgen/RTFDoc.py b/src/plugins/docgen/RTFDoc.py index ceebf0742..deee0a21f 100644 --- a/src/plugins/docgen/RTFDoc.py +++ b/src/plugins/docgen/RTFDoc.py @@ -37,11 +37,19 @@ from gen.ggettext import gettext as _ # #------------------------------------------------------------------------ from gen.plug.docgen import (BaseDoc, TextDoc, FONT_SERIF, PARA_ALIGN_RIGHT, - PARA_ALIGN_CENTER, PARA_ALIGN_JUSTIFY) + PARA_ALIGN_CENTER, PARA_ALIGN_JUSTIFY, + URL_PATTERN) import ImgManip import Errors import Utils +#------------------------------------------------------------------------ +# +# Set up to make links clickable +# +#------------------------------------------------------------------------ +_CLICKABLE = r'''{\\field{\\*\\fldinst HYPERLINK "\1"}{\\fldrslt \1}}''' + #------------------------------------------------------------------------ # # RTF uses a unit called "twips" for its measurements. According to the @@ -380,24 +388,16 @@ class RTFDoc(BaseDoc,TextDoc): if (nx, ny) == (0,0): return - if (nx, ny) == (0,0): - return - - ratio = float(x_cm)*float(ny)/(float(y_cm)*float(nx)) - - if ratio < 1: - act_width = x_cm - act_height = y_cm*ratio - else: - act_height = y_cm - act_width = x_cm/ratio - - buf = ImgManip.resize_to_jpeg_buffer(name, int(act_width*40), - int(act_height*40)) + (act_width, act_height) = ImgManip.image_actual_size(x_cm, y_cm, nx, ny) act_width = twips(act_width) act_height = twips(act_height) + size = [act_width, act_height] + buf = ImgManip.resize_to_jpeg_buffer(name, size, crop=crop) + act_width = size[0] # In case it changed because of cropping or keeping the ratio + act_height = size[1] + self.f.write('{\*\shppict{\\pict\\jpegblip') self.f.write('\\picwgoal%d\\pichgoal%d\n' % (act_width,act_height)) index = 1 @@ -408,6 +408,9 @@ class RTFDoc(BaseDoc,TextDoc): index = index+1 self.f.write('}}\\par\n') + if len(alt): + self.f.write('%s\n\\par\n' % alt) + def write_styled_note(self, styledtext, format, style_name, contains_html=False, links=False): """ @@ -419,6 +422,7 @@ class RTFDoc(BaseDoc,TextDoc): If contains_html=True, then the textdoc is free to handle that in some way. Eg, a textdoc could remove all tags, or could make sure a link is clickable. RTFDoc prints the html without handling it + links: bool, make URLs clickable if True """ text = str(styledtext) self.start_paragraph(style_name) @@ -435,7 +439,7 @@ class RTFDoc(BaseDoc,TextDoc): else: if ( linenb > 1 ): self.write_text('\\line ') - self.write_text(line) + self.write_text(line, links=links) linenb += 1 # FIXME: I don't understand why these newlines are necessary. # It may be related to the behaviour of end_paragraph inside tables, and @@ -476,6 +480,10 @@ class RTFDoc(BaseDoc,TextDoc): else: self.text += i + if links == True: + import re + self.text = re.sub(URL_PATTERN, _CLICKABLE, self.text) + def process_spaces(line, format): """ Function to process spaces in text lines for flowed and pre-formatted notes. diff --git a/src/plugins/lib/libcairodoc.py b/src/plugins/lib/libcairodoc.py index 1ca563b0d..734749ebe 100644 --- a/src/plugins/lib/libcairodoc.py +++ b/src/plugins/lib/libcairodoc.py @@ -1369,7 +1369,8 @@ class CairoDoc(BaseDoc, TextDoc, DrawDoc): markuptext = self._backend.add_markup_from_styled(text, s_tags) self.__write_text(markuptext, markup=True) - def add_media_object(self, name, pos, x_cm, y_cm, alt=''): + def add_media_object(self, name, pos, x_cm, y_cm, alt='', + style_name=None, crop=None): new_image = GtkDocPicture(pos, name, x_cm, y_cm) self._active_element.add_child(new_image) diff --git a/src/plugins/webreport/NarrativeWeb.py b/src/plugins/webreport/NarrativeWeb.py index 20a8c9be0..256e93eeb 100644 --- a/src/plugins/webreport/NarrativeWeb.py +++ b/src/plugins/webreport/NarrativeWeb.py @@ -3014,8 +3014,10 @@ class MediaPage(BasePage): if scale < 0.8: # scale factor is significant enough to warrant making a smaller image initial_image_path = '%s_init.jpg' % os.path.splitext(newpath)[0] - initial_image_data = ImgManip.resize_to_jpeg_buffer(orig_image_path, - new_width, new_height) + size = [new_width, new_height] + initial_image_data = ImgManip.resize_to_jpeg_buffer(orig_image_path, size) + new_width = size[0] # In case it changed because of keeping the ratio + new_height = size[1] if self.report.archive: filed, dest = tempfile.mkstemp() os.write(filed, initial_image_data)