Back to index

texmacs  1.0.7.15
imlib2.cpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : imlib2.cpp
00004 * DESCRIPTION: interface with Imlib2
00005 * COPYRIGHT  : (C) 2005  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #include "Imlib2/imlib2.hpp"
00013 #include "dyn_link.hpp"
00014 #include "hashmap.hpp"
00015 
00016 #ifdef USE_IMLIB2
00017 
00018 #include <Imlib2.h>
00019 #include <stdio.h>
00020 #include <string.h>
00021 
00022 /******************************************************************************
00023 * Routines used from Imlib2
00024 ******************************************************************************/
00025 
00026 Imlib_Image (*IMLIB2_load_image) (const char *file);
00027 int (*IMLIB2_image_get_width) (void);
00028 int (*IMLIB2_image_get_height) (void);
00029 void (*IMLIB2_context_set_image) (Imlib_Image image);
00030 void (*IMLIB2_render_image_part_on_drawable_at_size)
00031   (int source_x, int source_y, int source_width, int source_height,
00032    int x, int y, int width, int height);
00033 void (*IMLIB2_free_image) (void);
00034 void (*IMLIB2_context_set_display) (Display * display);
00035 void (*IMLIB2_context_set_visual) (Visual * visual);
00036 void (*IMLIB2_context_set_colormap) (Colormap colormap);
00037 void (*IMLIB2_context_set_drawable) (Drawable drawable);
00038 
00039 /******************************************************************************
00040 * Initialization
00041 ******************************************************************************/
00042 
00043 static bool imlib2_initialized= false;
00044 static bool imlib2_error      = false;
00045 
00046 #ifdef LINKED_IMLIB2
00047 #define imlib2_bind(orig,tm) \
00048   tm= orig;
00049 #else
00050 #define imlib2_bind(orig,tm) \
00051   (void) symbol_install ("libImlib2.so", #orig, (pointer&) tm); \
00052   if (tm == NULL) return;
00053 #endif
00054 
00055 void
00056 imlib2_initialize () {
00057   imlib2_initialized= true;
00058   imlib2_error      = true;
00059 
00060   int status= debug_off ();
00061   imlib2_bind (imlib_load_image, IMLIB2_load_image);
00062   imlib2_bind (imlib_image_get_width, IMLIB2_image_get_width);
00063   imlib2_bind (imlib_image_get_height, IMLIB2_image_get_height);
00064   imlib2_bind (imlib_context_set_image, IMLIB2_context_set_image);
00065   imlib2_bind (imlib_render_image_part_on_drawable_at_size,
00066               IMLIB2_render_image_part_on_drawable_at_size);
00067   imlib2_bind (imlib_free_image, IMLIB2_free_image);
00068   imlib2_bind (imlib_context_set_display, IMLIB2_context_set_display);
00069   imlib2_bind (imlib_context_set_visual, IMLIB2_context_set_visual);
00070   imlib2_bind (imlib_context_set_colormap, IMLIB2_context_set_colormap);
00071   imlib2_bind (imlib_context_set_drawable, IMLIB2_context_set_drawable);
00072   debug_on (status);
00073 
00074 #ifdef LINKED_IMLIB2
00075   if (DEBUG_AUTO) cout << "TeXmacs] With linked Imlib2 support\n";
00076 #else
00077   if (DEBUG_AUTO) cout << "TeXmacs] Installed Imlib2 support\n";
00078 #endif
00079 
00080   imlib2_error= false;
00081 }
00082 
00083 /******************************************************************************
00084 * Functionality provided by the plug-in
00085 ******************************************************************************/
00086 
00087 hashmap<tree,tree> imlib2_size_cache ("");
00088 
00089 bool
00090 imlib2_present () {
00091   if (!imlib2_initialized)
00092     imlib2_initialize ();
00093   return !imlib2_error;
00094 }
00095 
00096 bool
00097 imlib2_supports (url u) {
00098   string s= suffix (u);
00099   if (s == "ps" || s == "eps" || s == "pdf" ||
00100       s == "xpm" || s == "tif") return false;
00101   return imlib2_present ();
00102 }
00103 
00104 static Imlib_Image
00105 imlib2_load_image (url u) {
00106   url name= resolve (u);
00107   if (is_none (name))
00108     name= resolve ("$TEXMACS_PIXMAP_PATH/TeXmacs-gnu.xpm");
00109   char *_name= as_charp (as_string (name));
00110   Imlib_Image image;
00111   image = IMLIB2_load_image (_name);
00112   tm_delete_array (_name);
00113   return image;
00114 }
00115 
00116 void
00117 imlib2_image_size (url u, int& w, int& h) {
00118   if (imlib2_size_cache->contains (u->t)) {
00119     w= as_int (imlib2_size_cache[u->t][0]);
00120     h= as_int (imlib2_size_cache[u->t][1]);
00121   }
00122   else {
00123     Imlib_Image image= imlib2_load_image (u);
00124     if (image) {
00125       IMLIB2_context_set_image (image);
00126       w= IMLIB2_image_get_width ();
00127       h= IMLIB2_image_get_height ();
00128       IMLIB2_free_image ();
00129       imlib2_size_cache (u->t)= tuple (as_string (w), as_string (h));
00130     }
00131   }
00132 }
00133 
00134 void
00135 imlib2_display (Display* dpy, Pixmap pm, url u, SI w, SI h,
00136               double cx1, double cy1, double cx2, double cy2)
00137 {
00138   Imlib_Image image= imlib2_load_image (u);
00139   if (image) {
00140     Visual *vis= DefaultVisual (dpy, DefaultScreen (dpy));
00141     Colormap cm= DefaultColormap (dpy, DefaultScreen (dpy));
00142     IMLIB2_context_set_display (dpy);
00143     IMLIB2_context_set_visual (vis);
00144     IMLIB2_context_set_colormap (cm);
00145     IMLIB2_context_set_drawable (pm);
00146 
00147     IMLIB2_context_set_image (image);
00148     int iw= IMLIB2_image_get_width ();
00149     int ih= IMLIB2_image_get_height ();
00150     int x1= as_int (cx1 * iw);
00151     int y1= as_int (cy1 * ih);
00152     int x2= as_int (cx2 * iw);
00153     int y2= as_int (cy2 * ih);
00154     int ww= x2 - x1;
00155     int hh= y2 - y1;
00156     IMLIB2_render_image_part_on_drawable_at_size
00157       (x1, hh-y2, ww, hh, 0, 0, w, h);
00158     IMLIB2_free_image ();
00159   }
00160 }
00161 
00162 #else // USE_IMLIB2
00163 
00164 /******************************************************************************
00165 * If Imlib2 is not present...
00166 ******************************************************************************/
00167 
00168 bool imlib2_present () { return false; }
00169 bool imlib2_supports (url u) { (void) u; return false; }
00170 
00171 void
00172 imlib2_image_size (url u, int& w, int& h) {
00173   (void) u; (void) w; (void) h;
00174   FAILED ("imlib2 is not present");
00175 }
00176 
00177 #ifdef X11TEXMACS
00178 void
00179 imlib2_display (Display* dpy, Pixmap pm, url u, SI w, SI h,
00180               double cx1, double cy1, double cx2, double cy2)
00181 {
00182   (void) dpy; (void) pm;
00183   (void) u; (void) w; (void) h;
00184   (void) cx1; (void) cy1; (void) cx2; (void) cy2;
00185   FAILED ("imlib2 is not present");
00186 }
00187 #endif
00188 
00189 #endif // USE_IMLIB2