From: <md...@us...> - 2007-12-05 16:21:07
|
Revision: 4618 http://matplotlib.svn.sourceforge.net/matplotlib/?rev=4618&view=rev Author: mdboom Date: 2007-12-05 08:20:54 -0800 (Wed, 05 Dec 2007) Log Message: ----------- Support arbitrary rotation of usetex text. Modified Paths: -------------- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py trunk/matplotlib/lib/matplotlib/texmanager.py trunk/matplotlib/src/_backend_agg.cpp trunk/matplotlib/src/_image.cpp Modified: trunk/matplotlib/lib/matplotlib/backends/backend_agg.py =================================================================== --- trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2007-12-05 15:40:46 UTC (rev 4617) +++ trunk/matplotlib/lib/matplotlib/backends/backend_agg.py 2007-12-05 16:20:54 UTC (rev 4618) @@ -27,8 +27,8 @@ REQUIREMENTs python2.3+ - numpy 1.0 + - + numpy 1.0 + + agg2 (see below) freetype 2 libpng @@ -126,7 +126,7 @@ self.restore_region = self._renderer.restore_region self.mathtext_parser = MathTextParser('Agg') self._fontd = {} - + self.bbox = lbwh_to_bbox(0,0, self.width, self.height) if __debug__: verbose.report('RendererAgg.__init__ done', 'debug-annoying') @@ -175,7 +175,7 @@ 'debug-annoying') ox, oy, width, height, descent, font_image, used_characters = \ self.mathtext_parser.parse(s, self.dpi.get(), prop) - + x = int(x) + ox y = int(y) - oy self._renderer.draw_text_image(font_image, x, y + 1, angle, gc) @@ -209,7 +209,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 @@ -222,8 +222,8 @@ # todo: handle props size = prop.get_size_in_points() texmanager = self.get_texmanager() - Z = texmanager.get_rgba(s, size, self.dpi.get(), rgb) - m,n,tmp = Z.shape + Z = texmanager.get_grey(s, size, self.dpi.get()) + m,n = Z.shape # TODO: descent of TeX text (I am imitating backend_ps here -JKS) return n, m, 0 @@ -242,45 +242,19 @@ 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.get() - 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 = lbwh_to_bbox(*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 @@ -413,8 +387,8 @@ self.draw() self.get_renderer()._renderer.write_rgba(str(filename)) print_rgba = print_raw - + def print_png(self, filename, *args, **kwargs): self.draw() self.get_renderer()._renderer.write_png(filename, self.figure.dpi.get()) - + Modified: trunk/matplotlib/lib/matplotlib/texmanager.py =================================================================== --- trunk/matplotlib/lib/matplotlib/texmanager.py 2007-12-05 15:40:46 UTC (rev 4617) +++ trunk/matplotlib/lib/matplotlib/texmanager.py 2007-12-05 16:20:54 UTC (rev 4618) @@ -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: trunk/matplotlib/src/_backend_agg.cpp =================================================================== --- trunk/matplotlib/src/_backend_agg.cpp 2007-12-05 15:40:46 UTC (rev 4617) +++ trunk/matplotlib/src/_backend_agg.cpp 2007-12-05 16:20:54 UTC (rev 4618) @@ -1003,7 +1003,7 @@ for(k = firstRow; k <= lastRow; k++) { numCol = inPolygon(k, xs, ys, col); - + if (numCol >= 2) rendererBase->copy_hline(col[0], k, col[1] - 1, color); if (numCol == 4) rendererBase->copy_hline(col[2], k, col[3] - 1, color); } @@ -2137,9 +2137,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 { @@ -2157,22 +2173,19 @@ set_clipbox_rasterizer(gc.cliprect); - 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, -(int)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: trunk/matplotlib/src/_image.cpp =================================================================== --- trunk/matplotlib/src/_image.cpp 2007-12-05 15:40:46 UTC (rev 4617) +++ trunk/matplotlib/src/_image.cpp 2007-12-05 16:20:54 UTC (rev 4618) @@ -850,7 +850,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. |