Patch by Adam Stein <adam@csh.rit.edu > - Continued work on "0002513: Using section/region on media_ref as thumbnail on reports"

svn: r17971
This commit is contained in:
Brian Matherly 2011-07-27 03:26:12 +00:00
parent 030d675b72
commit 9bc3d725cb
4 changed files with 50 additions and 25 deletions

View File

@ -185,24 +185,38 @@ def image_actual_size(x_cm, y_cm, x, y):
# resize_to_jpeg_buffer # 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 Loads the image, converting the file to JPEG, and resizing it. Instead of
saving the file, the data is returned in a buffer. saving the file, the data is returned in a buffer.
:param source: source image file, in any format that gtk recognizes :param source: source image file, in any format that gtk recognizes
:type source: unicode :type source: unicode
:param width: desired width of the destination image :param size: desired size of the destination image ([width, height])
:type width: int :type size: list
:param height: desired height of the destination image :param crop: cropping coordinates
:type height: int :type crop: array of integers ([start_x, start_y, end_x, end_y])
:rtype: buffer of data :rtype: buffer of data
:returns: jpeg image as raw data :returns: jpeg image as raw data
""" """
import gtk import gtk
filed, dest = tempfile.mkstemp() filed, dest = tempfile.mkstemp()
img = gtk.gdk.pixbuf_new_from_file(source) 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) os.close(filed)
dest = Utils.get_unicode_path_from_env_var(dest) dest = Utils.get_unicode_path_from_env_var(dest)
scaled.save(dest, 'jpeg') scaled.save(dest, 'jpeg')

View File

@ -37,11 +37,19 @@ from gen.ggettext import gettext as _
# #
#------------------------------------------------------------------------ #------------------------------------------------------------------------
from gen.plug.docgen import (BaseDoc, TextDoc, FONT_SERIF, PARA_ALIGN_RIGHT, 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 ImgManip
import Errors import Errors
import Utils 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 # 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): if (nx, ny) == (0,0):
return return
if (nx, ny) == (0,0): (act_width, act_height) = ImgManip.image_actual_size(x_cm, y_cm, nx, ny)
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 = twips(act_width) act_width = twips(act_width)
act_height = twips(act_height) 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('{\*\shppict{\\pict\\jpegblip')
self.f.write('\\picwgoal%d\\pichgoal%d\n' % (act_width,act_height)) self.f.write('\\picwgoal%d\\pichgoal%d\n' % (act_width,act_height))
index = 1 index = 1
@ -408,6 +408,9 @@ class RTFDoc(BaseDoc,TextDoc):
index = index+1 index = index+1
self.f.write('}}\\par\n') 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, def write_styled_note(self, styledtext, format, style_name,
contains_html=False, links=False): 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 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 some way. Eg, a textdoc could remove all tags, or could make sure
a link is clickable. RTFDoc prints the html without handling it a link is clickable. RTFDoc prints the html without handling it
links: bool, make URLs clickable if True
""" """
text = str(styledtext) text = str(styledtext)
self.start_paragraph(style_name) self.start_paragraph(style_name)
@ -435,7 +439,7 @@ class RTFDoc(BaseDoc,TextDoc):
else: else:
if ( linenb > 1 ): if ( linenb > 1 ):
self.write_text('\\line ') self.write_text('\\line ')
self.write_text(line) self.write_text(line, links=links)
linenb += 1 linenb += 1
# FIXME: I don't understand why these newlines are necessary. # FIXME: I don't understand why these newlines are necessary.
# It may be related to the behaviour of end_paragraph inside tables, and # It may be related to the behaviour of end_paragraph inside tables, and
@ -476,6 +480,10 @@ class RTFDoc(BaseDoc,TextDoc):
else: else:
self.text += i self.text += i
if links == True:
import re
self.text = re.sub(URL_PATTERN, _CLICKABLE, self.text)
def process_spaces(line, format): def process_spaces(line, format):
""" """
Function to process spaces in text lines for flowed and pre-formatted notes. Function to process spaces in text lines for flowed and pre-formatted notes.

View File

@ -1369,7 +1369,8 @@ class CairoDoc(BaseDoc, TextDoc, DrawDoc):
markuptext = self._backend.add_markup_from_styled(text, s_tags) markuptext = self._backend.add_markup_from_styled(text, s_tags)
self.__write_text(markuptext, markup=True) 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) new_image = GtkDocPicture(pos, name, x_cm, y_cm)
self._active_element.add_child(new_image) self._active_element.add_child(new_image)

View File

@ -3014,8 +3014,10 @@ class MediaPage(BasePage):
if scale < 0.8: if scale < 0.8:
# scale factor is significant enough to warrant making a smaller image # scale factor is significant enough to warrant making a smaller image
initial_image_path = '%s_init.jpg' % os.path.splitext(newpath)[0] initial_image_path = '%s_init.jpg' % os.path.splitext(newpath)[0]
initial_image_data = ImgManip.resize_to_jpeg_buffer(orig_image_path, size = [new_width, new_height]
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: if self.report.archive:
filed, dest = tempfile.mkstemp() filed, dest = tempfile.mkstemp()
os.write(filed, initial_image_data) os.write(filed, initial_image_data)