From: <md...@us...> - 2007-12-05 16:28:14
|
Revision: 4619 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4619&view=rev Author: mdboom Date: 2007-12-05 08:28:05 -0800 (Wed, 05 Dec 2007) Log Message: ----------- Merged revisions 4616-4618 via svnmerge from http://matplotlib.svn.sf.net/svnroot/matplotlib/trunk/matplotlib ........ r4618 | mdboom | 2007-12-05 11:20:54 -0500 (Wed, 05 Dec 2007) | 2 lines Support arbitrary rotation of usetex text. ........ Modified Paths: -------------- branches/transforms/lib/matplotlib/backends/backend_agg.py branches/transforms/lib/matplotlib/texmanager.py branches/transforms/src/_backend_agg.cpp branches/transforms/src/_image.cpp Property Changed: ---------------- branches/transforms/ Property changes on: branches/transforms ___________________________________________________________________ Name: svnmerge-integrated - /trunk/matplotlib:1-4615 + /trunk/matplotlib:1-4618 Modified: branches/transforms/lib/matplotlib/backends/backend_agg.py =================================================================== --- branches/transforms/lib/matplotlib/backends/backend_agg.py 2007-12-05 16:20:54 UTC (rev 4618) +++ branches/transforms/lib/matplotlib/backends/backend_agg.py 2007-12-05 16:28:05 UTC (rev 4619) @@ -71,7 +71,7 @@ self.tostring_rgba_minimized = self._renderer.tostring_rgba_minimized self.mathtext_parser = MathTextParser('Agg') self._fontd = {} - + self.bbox = Bbox.from_bounds(0, 0, self.width, self.height) if __debug__: verbose.report('RendererAgg.__init__ done', 'debug-annoying') @@ -84,7 +84,7 @@ 'debug-annoying') ox, oy, width, height, descent, font_image, used_characters = \ self.mathtext_parser.parse(s, self.dpi, prop) - + x = int(x) + ox y = int(y) - oy self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) @@ -112,7 +112,7 @@ self._renderer.draw_text_image(font.get_image(), int(x), int(y) + 1, angle, gc) - def get_text_width_height_descent(self, s, prop, ismath, rgb=(0,0,0)): + def get_text_width_height_descent(self, s, prop, ismath): """ get the width and height in display coords of the string s with FontPropertry prop @@ -125,8 +125,8 @@ # todo: handle props size = prop.get_size_in_points() texmanager = self.get_texmanager() - Z = texmanager.get_rgba(s, size, self.dpi, rgb) - m,n,tmp = Z.shape + Z = texmanager.get_grey(s, size, self.dpi) + m,n = Z.shape # TODO: descent of TeX text (I am imitating backend_ps here -JKS) return n, m, 0 @@ -145,45 +145,18 @@ def draw_tex(self, gc, x, y, s, prop, angle): # todo, handle props, angle, origins - rgb = gc.get_rgb() size = prop.get_size_in_points() dpi = self.dpi - flip = angle==90 - w,h,d = self.get_text_width_height_descent(s, prop, 'TeX', rgb) - if flip: - w,h = h,w - x -= w - texmanager = self.get_texmanager() - key = s, size, dpi, rgb, angle, texmanager.get_font_config() + key = s, size, dpi, angle, texmanager.get_font_config() im = self.texd.get(key) if im is None: - Z = texmanager.get_rgba(s, size, dpi, rgb) - if flip: - r = Z[:,:,0] - g = Z[:,:,1] - b = Z[:,:,2] - a = Z[:,:,3] - m,n,tmp = Z.shape + Z = texmanager.get_grey(s, size, dpi) + Z = npy.array(Z * 255.0, npy.uint8) - def func(x): - return npy.transpose(npy.fliplr(x)) + self._renderer.draw_text_image(Z, x, y, angle, gc) - Z = npy.zeros((n,m,4), float) - Z[:,:,0] = func(r) - Z[:,:,1] = func(g) - Z[:,:,2] = func(b) - Z[:,:,3] = func(a) - im = fromarray(Z, 1) - im.flipud_out() - self.texd[key] = im - - cliprect = gc.get_clip_rectangle() - if cliprect is None: bbox = None - else: bbox = Bbox.from_bounds(*cliprect) - self.draw_image(x, self.height-y, im, bbox) - def get_canvas_width_height(self): 'return the canvas width and height in display coords' return self.width, self.height @@ -217,7 +190,7 @@ if __debug__: verbose.report('RendererAgg.points_to_pixels', 'debug-annoying') return points*self.dpi/72.0 - + def tostring_rgb(self): if __debug__: verbose.report('RendererAgg.tostring_rgb', 'debug-annoying') @@ -314,8 +287,8 @@ FigureCanvasAgg.draw(self) self.get_renderer()._renderer.write_rgba(str(filename)) print_rgba = print_raw - + def print_png(self, filename, *args, **kwargs): FigureCanvasAgg.draw(self) self.get_renderer()._renderer.write_png(filename, self.figure.dpi) - + Modified: branches/transforms/lib/matplotlib/texmanager.py =================================================================== --- branches/transforms/lib/matplotlib/texmanager.py 2007-12-05 16:20:54 UTC (rev 4618) +++ branches/transforms/lib/matplotlib/texmanager.py 2007-12-05 16:28:05 UTC (rev 4619) @@ -79,7 +79,8 @@ dvipngVersion = get_dvipng_version() # mappable cache of - arrayd = {} + rgba_arrayd = {} + grey_arrayd = {} postscriptd = {} pscnt = 0 @@ -131,7 +132,7 @@ found_font = self.font_info[font.lower()] setattr(self, font_family_attr, self.font_info[font.lower()]) - if DEBUG: + if DEBUG: print 'family: %s, font: %s, info: %s'%(font_family, font, self.font_info[font.lower()]) break @@ -323,6 +324,24 @@ return [int(val) for val in line.split()[1:]] raise RuntimeError('Could not parse %s'%psfile) + def get_grey(self, tex, fontsize=None, dpi=None): + key = tex, self.get_font_config(), fontsize, dpi + alpha = self.grey_arrayd.get(key) + + if alpha is None: + pngfile = self.make_png(tex, fontsize, dpi) + X = readpng(os.path.join(self.texcache, pngfile)) + + if (self.dvipngVersion < '1.6') or rcParams['text.dvipnghack']: + # hack the alpha channel as described in comment above + alpha = npy.sqrt(1-X[:,:,0]) + else: + alpha = X[:,:,-1] + + self.grey_arrayd[key] = alpha + return alpha + + def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0,0,0)): """ Return tex string as an rgba array @@ -351,23 +370,16 @@ if not dpi: dpi = rcParams['savefig.dpi'] r,g,b = rgb key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb) - Z = self.arrayd.get(key) + Z = self.rgba_arrayd.get(key) if Z is None: - pngfile = self.make_png(tex, fontsize, dpi) - X = readpng(os.path.join(self.texcache, pngfile)) + alpha = self.get_grey(tex, fontsize, dpi) - if (self.dvipngVersion < '1.6') or rcParams['text.dvipnghack']: - # hack the alpha channel as described in comment above - alpha = npy.sqrt(1-X[:,:,0]) - else: - alpha = X[:,:,-1] - - Z = npy.zeros(X.shape, npy.float) + Z = npy.zeros((X.shape[0], X.shape[1], 4), npy.float) Z[:,:,0] = r Z[:,:,1] = g Z[:,:,2] = b Z[:,:,3] = alpha - self.arrayd[key] = Z + self.rgba_arrayd[key] = Z return Z Modified: branches/transforms/src/_backend_agg.cpp =================================================================== --- branches/transforms/src/_backend_agg.cpp 2007-12-05 16:20:54 UTC (rev 4618) +++ branches/transforms/src/_backend_agg.cpp 2007-12-05 16:28:05 UTC (rev 4619) @@ -658,9 +658,25 @@ args.verify_length(5); - FT2Image *image = static_cast<FT2Image*>(args[0].ptr()); - if (!image->get_buffer()) - return Py::Object(); + const unsigned char* buffer = NULL; + int width, height; + Py::Object image_obj = args[0]; + if (PyArray_Check(image_obj.ptr())) { + PyArrayObject* image_array = NULL; + image_array = (PyArrayObject*)PyArray_FromObject(image_obj.ptr(), PyArray_UBYTE, 2, 2); + if (!image_array) + throw Py::ValueError("First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array."); + buffer = (unsigned char *)PyArray_DATA(image_array); + width = PyArray_DIM(image_array, 1); + height = PyArray_DIM(image_array, 0); + } else { + FT2Image *image = static_cast<FT2Image*>(args[0].ptr()); + if (!image->get_buffer()) + throw Py::ValueError("First argument to draw_text_image must be a FT2Font.Image object or a Nx2 uint8 numpy array."); + buffer = image->get_buffer(); + width = image->get_width(); + height = image->get_height(); + } int x(0),y(0); try { @@ -680,22 +696,19 @@ rendererBase->reset_clipping(true); set_clipbox(gc.cliprect, theRasterizer); - const unsigned char* const buffer = image->get_buffer(); - agg::rendering_buffer srcbuf - ((agg::int8u*)buffer, image->get_width(), - image->get_height(), image->get_width()); + agg::rendering_buffer srcbuf((agg::int8u*)buffer, width, height, width); agg::pixfmt_gray8 pixf_img(srcbuf); agg::trans_affine mtx; - mtx *= agg::trans_affine_translation(0, -(int)image->get_height()); + mtx *= agg::trans_affine_translation(0, -height); mtx *= agg::trans_affine_rotation(-angle * agg::pi / 180.0); mtx *= agg::trans_affine_translation(x, y); agg::path_storage rect; rect.move_to(0, 0); - rect.line_to(image->get_width(), 0); - rect.line_to(image->get_width(), image->get_height()); - rect.line_to(0, image->get_height()); + rect.line_to(width, 0); + rect.line_to(width, height); + rect.line_to(0, height); rect.line_to(0, 0); agg::conv_transform<agg::path_storage> rect2(rect, mtx); Modified: branches/transforms/src/_image.cpp =================================================================== --- branches/transforms/src/_image.cpp 2007-12-05 16:20:54 UTC (rev 4618) +++ branches/transforms/src/_image.cpp 2007-12-05 16:28:05 UTC (rev 4619) @@ -805,7 +805,7 @@ char _image_module_readpng__doc__[] = "readpng(fname)\n" "\n" -"Load an image from png file into a numerix array of MxNx4 uint8"; +"Load an image from png file into a numerix array of MxNx4 float"; Py::Object _image_module::readpng(const Py::Tuple& args) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |