diff options
author | Behdad Esfahbod <behdad@gnome.org> | 2006-02-01 22:10:11 +0000 |
---|---|---|
committer | Behdad Esfahbod <behdad@src.gnome.org> | 2006-02-01 22:10:11 +0000 |
commit | 9ca400bee42e2ad658681c8d0161ec18492964ef (patch) | |
tree | 350c697c7ee55429830a84933d58f7ad6b4d29ca /examples | |
parent | eda8c1ea592c855cc675d7a37dd5b6937c5e996f (diff) | |
download | pango-9ca400bee42e2ad658681c8d0161ec18492964ef.tar.gz |
Driver for X-based viewer. Used by pangocairo-view and pangoxft-view
2006-02-01 Behdad Esfahbod <behdad@gnome.org>
* examples/viewer-x.c, examples/viewer-x.h: Driver for X-based
viewer. Used by pangocairo-view and pangoxft-view currently.
* examples/Makefile.am, examples/cairoview.c, examples/xftview.c,
examples/pangoft2topgm.c, examples/renderdemo.h,
examples/renderdemo.c: Adjust to the above change.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/Makefile.am | 18 | ||||
-rw-r--r-- | examples/cairoview.c | 232 | ||||
-rw-r--r-- | examples/pangoft2topgm.c | 8 | ||||
-rw-r--r-- | examples/renderdemo.c | 58 | ||||
-rw-r--r-- | examples/renderdemo.h | 7 | ||||
-rw-r--r-- | examples/viewer-x.c | 169 | ||||
-rw-r--r-- | examples/viewer-x.h | 43 | ||||
-rw-r--r-- | examples/xftview.c | 159 |
8 files changed, 368 insertions, 326 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am index b9c1420f..0d2dc42d 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -36,8 +36,8 @@ endif pangoft2_view_SOURCES = \ pangoft2topgm.c \ - renderdemo.c \ - renderdemo.h + renderdemo.h \ + renderdemo.c pangoft2_view_LDADD = \ ../pango/libpango-$(PANGO_API_VERSION).la \ ../pango/libpangoft2-$(PANGO_API_VERSION).la \ @@ -51,8 +51,11 @@ noinst_PROGRAMS += pangoxft-view endif pangoxft_view_SOURCES = \ - xftview.c \ - renderdemo.c + renderdemo.h \ + renderdemo.c \ + viewer-x.h \ + viewer-x.c \ + xftview.c pangoxft_view_LDADD = \ ../pango/libpango-$(PANGO_API_VERSION).la \ ../pango/libpangoft2-$(PANGO_API_VERSION).la \ @@ -72,8 +75,11 @@ endif endif pangocairo_view_SOURCES = \ - cairoview.c \ - renderdemo.c + renderdemo.h \ + renderdemo.c \ + viewer-x.h \ + viewer-x.c \ + cairoview.c pangocairo_view_LDADD = \ ../pango/libpango-$(PANGO_API_VERSION).la \ ../pango/libpangoft2-$(PANGO_API_VERSION).la \ diff --git a/examples/cairoview.c b/examples/cairoview.c index b58a74ed..061c1abb 100644 --- a/examples/cairoview.c +++ b/examples/cairoview.c @@ -1,8 +1,8 @@ /* Pango * cairoview.c: Example program to view a UTF-8 encoding file - * using Cairo to render result + * using PangoCairo to render result * - * Copyright (C) 2005 Red Hat, Inc. + * Copyright (C) 1999,2004,2005 Red Hat, Inc. * Copyright (C) 2001 Sun Microsystems * * This library is free software; you can redistribute it and/or @@ -21,41 +21,20 @@ * Boston, MA 02111-1307, USA. */ -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include <X11/Xlib.h> -#include <X11/Xutil.h> - #include "renderdemo.h" +#include "viewer-x.h" #include <pango/pangocairo.h> #include <cairo-xlib.h> -static Region update_region = NULL; -static PangoContext *context; -static Display *display; -int screen; -static Window window; -gboolean show_borders; - -typedef struct -{ - cairo_t *cr; - int x_offset; - int y_offset; -} RenderData; - static void -do_cairo_render (PangoLayout *layout, +render_callback (PangoLayout *layout, int x, int y, - gpointer data) + gpointer data, + gboolean show_borders) { - RenderData *render_data = data; - cairo_t *cr = render_data->cr; + cairo_t *cr = (cairo_t *)data; cairo_move_to (cr, x, y); pango_cairo_show_layout (cr, layout); @@ -91,11 +70,11 @@ do_cairo_render (PangoLayout *layout, } static void -do_cairo_transform (PangoContext *context, +transform_callback (PangoContext *context, PangoMatrix *matrix, gpointer data) { - RenderData *render_data = data; + cairo_t *cr = (cairo_t *)data; cairo_matrix_t cairo_matrix; if (matrix) @@ -112,182 +91,65 @@ do_cairo_transform (PangoContext *context, cairo_matrix_init_identity (&cairo_matrix); } - cairo_matrix_translate (&cairo_matrix, - render_data->x_offset, - render_data->y_offset); - - cairo_set_matrix (render_data->cr, &cairo_matrix); + cairo_set_matrix (cr, &cairo_matrix); pango_context_set_matrix (context, matrix); - pango_cairo_update_context (render_data->cr, context); + pango_cairo_update_context (cr, context); } void -update (void) +do_init (Display *display, + int screen, + /* output */ + PangoContext **context, + int *width, + int *height) { - RenderData render_data; - cairo_surface_t *surface; + PangoFontMap *fontmap; cairo_t *cr; - Pixmap pixmap; - GC gc; - XRectangle extents; - int width, height; - - /* Create a temporary pixmap and a Cairo context pointing to it */ - XClipBox (update_region, &extents); + cairo_surface_t *surface; + fontmap = pango_cairo_font_map_get_default (); + pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (fontmap), opt_dpi); + *context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap)); - width = extents.width; - height = extents.height; + /* This is annoying ... we have to create a temporary surface just to + * get the extents of the text. + */ + surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1); + cr = cairo_create (surface); + cairo_surface_destroy (surface); + do_output (*context, NULL, transform_callback, cr, width, height, FALSE); + cairo_destroy (cr); +} - pixmap = XCreatePixmap (display, window, width, height, - DefaultDepth (display, screen)); +void +do_render (Display *display, + int screen, + Window window, + Pixmap pixmap, + PangoContext *context, + int width, + int height, + gboolean show_borders) +{ + cairo_t *cr; + cairo_surface_t *surface; surface = cairo_xlib_surface_create (display, pixmap, DefaultVisual (display, screen), width, height); - - cr = render_data.cr = cairo_create (surface); + cr = cairo_create (surface); cairo_surface_destroy (surface); - render_data.x_offset = - extents.x; - render_data.y_offset = - extents.y; - - do_cairo_transform (context, NULL, &render_data); + transform_callback (context, NULL, cr); - /* Clip to the current update region and fill with white */ - cairo_rectangle (cr, extents.x, extents.y, extents.width, extents.height); - - cairo_clip (cr); cairo_set_source_rgb (cr, 1.0, 1.0, 1.0); cairo_paint (cr); /* Draw the text in black */ cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); - do_output (context, do_cairo_render, do_cairo_transform, &render_data, NULL, NULL); - cairo_destroy (cr); - - /* Copy the updated area onto the window */ - gc = XCreateGC (display, pixmap, 0, NULL); - - XCopyArea (display, pixmap, window, gc, - 0, 0, - extents.width, extents.height, extents.x, extents.y); - - XFreeGC (display, gc); - XFreePixmap (display, pixmap); - - XDestroyRegion (update_region); - update_region = NULL; -} - -static void -expose (XExposeEvent *xev) -{ - XRectangle r; - if (!update_region) - update_region = XCreateRegion (); - - r.x = xev->x; - r.y = xev->y; - r.width = xev->width; - r.height = xev->height; - XUnionRectWithRegion (&r, update_region, update_region); -} - -int main (int argc, char **argv) -{ - PangoFontMap *fontmap; - XEvent xev; - unsigned long bg; - int width, height; - XSizeHints size_hints; - RenderData render_data; - unsigned int quit_keycode; - unsigned int borders_keycode; - cairo_surface_t *surface; - - g_type_init(); - - parse_options (argc, argv); + do_output (context, render_callback, transform_callback, cr, NULL, NULL, show_borders); - display = XOpenDisplay (NULL); - if (!display) - fail ("Cannot open display %s\n", XDisplayName (NULL)); - screen = DefaultScreen (display); - - fontmap = pango_cairo_font_map_get_default (); - pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (fontmap), opt_dpi); - context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (fontmap)); - - /* This is annoying ... we have to create a temporary surface just to - * get the extents of the text. - */ - surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1, 1); - render_data.cr = cairo_create (surface); - cairo_surface_destroy (surface); - render_data.x_offset = 0; - render_data.y_offset = 0; - do_output (context, NULL, do_cairo_transform, &render_data, &width, &height); - cairo_destroy (render_data.cr); - - bg = WhitePixel (display, screen); - - window = XCreateSimpleWindow (display, DefaultRootWindow (display), - 0, 0, width, height, 0, - bg, bg); - XSelectInput (display, window, ExposureMask | KeyPressMask); - - XMapWindow (display, window); - XmbSetWMProperties (display, window, - get_options_string (), - NULL, NULL, 0, NULL, NULL, NULL); - - memset ((char *)&size_hints, 0, sizeof (XSizeHints)); - size_hints.flags = PSize | PMaxSize; - size_hints.width = width; size_hints.height = height; /* for compat only */ - size_hints.max_width = width; size_hints.max_height = height; - - XSetWMNormalHints (display, window, &size_hints); - - borders_keycode = XKeysymToKeycode(display, 'B'); - quit_keycode = XKeysymToKeycode(display, 'Q'); - - while (1) - { - if (!XPending (display) && update_region) - update (); - - XNextEvent (display, &xev); - switch (xev.xany.type) { - case KeyPress: - if (xev.xkey.keycode == quit_keycode) - goto done; - else if (xev.xkey.keycode == borders_keycode) - { - XRectangle r; - show_borders = !show_borders; - - if (!update_region) - update_region = XCreateRegion (); - - r.x = 0; - r.y = 0; - r.width = width; - r.height = height; - XUnionRectWithRegion (&r, update_region, update_region); - } - break; - case Expose: - expose (&xev.xexpose); - break; - } - } - -done: - - g_object_unref (context); - finalize (); - - return 0; + cairo_destroy (cr); } diff --git a/examples/pangoft2topgm.c b/examples/pangoft2topgm.c index f47e3a22..85da627c 100644 --- a/examples/pangoft2topgm.c +++ b/examples/pangoft2topgm.c @@ -1,8 +1,8 @@ /* Pango * pangoft2topgm.c: Example program to view a UTF-8 encoding file - * using Pango to render result. + * using PangoFT2 to render result. * - * Copyright (C) 1999 Red Hat Software + * Copyright (C) 1999,2004,2005 Red Hat, Inc. * Copyright (C) 2001 Sun Microsystems * * This library is free software; you can redistribute it and/or @@ -102,7 +102,7 @@ main(int argc, char *argv[]) int width, height; int run; - do_output (context, NULL, NULL, NULL, &width, &height); + do_output (context, NULL, NULL, NULL, &width, &height, FALSE); bitmap.width = width; bitmap.pitch = (bitmap.width + 3) & ~3; @@ -113,7 +113,7 @@ main(int argc, char *argv[]) memset (buf, 0x00, bitmap.pitch * bitmap.rows); for (run = 0; run < opt_runs; run++) - do_output (context, ft2_render, NULL, &bitmap, &width, &height); + do_output (context, ft2_render, NULL, &bitmap, &width, &height, FALSE); if (gen_output) { diff --git a/examples/renderdemo.c b/examples/renderdemo.c index 0cf1b14f..6e3ff981 100644 --- a/examples/renderdemo.c +++ b/examples/renderdemo.c @@ -39,7 +39,7 @@ const char *prog_name; -gboolean opt_display = FALSE; +gboolean opt_display = TRUE; int opt_dpi = 96; const char *opt_font = MAKE_FONT_NAME (DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE); gboolean opt_header = FALSE; @@ -165,7 +165,8 @@ output_body (PangoContext *context, RenderCallback render_cb, gpointer cb_data, int *width, - int *height) + int *height, + gboolean show_borders) { PangoLayout *layout; PangoRectangle logical_rect; @@ -193,11 +194,16 @@ output_body (PangoContext *context, layout = make_layout (context, text, size); pango_layout_get_extents (layout, NULL, &logical_rect); + /* TODO: instead of these two calls, we really should call + * pango_layout_get_effective_width when that's implemented. + */ + *width = MAX (*width, PANGO_PIXELS (pango_layout_get_width (layout))); *width = MAX (*width, PANGO_PIXELS (logical_rect.width)); + *height += PANGO_PIXELS (logical_rect.height); if (render_cb) - (*render_cb) (layout, 0, dy, cb_data); + (*render_cb) (layout, 0, dy, cb_data, show_borders); dy += PANGO_PIXELS (logical_rect.height); @@ -223,7 +229,8 @@ do_output (PangoContext *context, TransformCallback transform_cb, gpointer cb_data, int *width_out, - int *height_out) + int *height_out, + gboolean show_borders) { PangoLayout *layout; PangoRectangle logical_rect; @@ -258,7 +265,7 @@ do_output (PangoContext *context, height += PANGO_PIXELS (logical_rect.height); if (render_cb) - (*render_cb) (layout, x, y, cb_data); + (*render_cb) (layout, x, y, cb_data, show_borders); y += PANGO_PIXELS (logical_rect.height); @@ -270,7 +277,7 @@ do_output (PangoContext *context, set_transform (context, transform_cb, cb_data, &matrix); - output_body (context, text, NULL, NULL, &rotated_width, &rotated_height); + output_body (context, text, NULL, NULL, &rotated_width, &rotated_height, show_borders); transform_point (&matrix, 0, 0, &p1x, &p1y); transform_point (&matrix, rotated_width, 0, &p2x, &p2y); @@ -289,7 +296,7 @@ do_output (PangoContext *context, set_transform (context, transform_cb, cb_data, &matrix); if (render_cb) - output_body (context, text, render_cb, cb_data, &rotated_width, &rotated_height); + output_body (context, text, render_cb, cb_data, &rotated_width, &rotated_height, show_borders); width = MAX (width, maxx - minx); height += maxy - miny; @@ -380,39 +387,39 @@ parse_options (int argc, char *argv[]) { {"no-auto-dir", 0, G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_auto_dir, "No layout direction according to contents", NULL }, - {"display", 0, 0, G_OPTION_ARG_NONE, &opt_display, - "Show output using ImageMagick", NULL }, + {"no-display", 'q', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &opt_display, + "Do not display (just save to file or whatever)", NULL }, {"dpi", 0, 0, G_OPTION_ARG_INT, &opt_dpi, - "Set the dpi", NULL }, + "Set the resolution", NULL }, {"ellipsize", 0, 0, G_OPTION_ARG_CALLBACK, &parse_ellipsis, "Ellipsization mode", "start/middle/end" }, - {"font", 0, 0,G_OPTION_ARG_STRING, &opt_font, - "Set the font name", NULL }, + {"font", 0, 0, G_OPTION_ARG_STRING, &opt_font, + "Set the font description", NULL }, {"header", 0, 0, G_OPTION_ARG_NONE, &opt_header, "Display the options in the output", NULL }, - {"hinting", 0, 0, G_OPTION_ARG_CALLBACK, &parse_hinting, - "Hinting style", "none/auto/full" }, + {"hinting", 0, 0, G_OPTION_ARG_CALLBACK, &parse_hinting, + "Hinting style", "none/auto/full" }, {"indent", 0, 0, G_OPTION_ARG_INT, &opt_indent, "Width in points to indent paragraphs", NULL }, {"margin", 0, 0, G_OPTION_ARG_INT, &opt_margin, "Set the margin on the output in pixels", NULL }, - {"makup", 0, 0, G_OPTION_ARG_NONE, &opt_markup, - "Interpret contents as Pango markup", NULL }, - {"output", 0, 0,G_OPTION_ARG_STRING, &opt_output, - "Name of output file", NULL }, - {"pangorc", 0, 0,G_OPTION_ARG_STRING, &opt_pangorc, + {"markup", 0, 0, G_OPTION_ARG_NONE, &opt_markup, + "Interpret text as Pango markup", NULL }, + {"output", 'o', 0,G_OPTION_ARG_STRING, &opt_output, + "Save rendered image to output file", NULL }, + {"pangorc", 0, 0, G_OPTION_ARG_STRING, &opt_pangorc, "pangorc file to use (default is ./pangorc)", NULL }, {"rtl", 0, 0, G_OPTION_ARG_NONE, &opt_rtl, - "Set base dir to RTL", NULL }, + "Set base direction to right-to-left", NULL }, {"rotate", 0, 0, G_OPTION_ARG_INT, &opt_rotate, "Angle at which to rotate results", NULL }, - {"runs", 0, 0, G_OPTION_ARG_INT, &opt_runs, - "Render text this many times", NULL }, - {"text", 0, 0,G_OPTION_ARG_STRING, &opt_text, + {"runs", 'n', 0, G_OPTION_ARG_INT, &opt_runs, + "Run Pango layout engine this many times", NULL }, + {"text", 't', 0, G_OPTION_ARG_STRING, &opt_text, "Text to display (instead of a file)", NULL }, {"waterfall", 0, 0, G_OPTION_ARG_NONE, &opt_waterfall, "Create a waterfall display", NULL }, - {"width", 0, 0, G_OPTION_ARG_INT, &opt_width, + {"width", 'w', 0, G_OPTION_ARG_INT, &opt_width, "Width in points to which to wrap output", NULL }, {NULL } }; @@ -447,9 +454,6 @@ parse_options (int argc, char *argv[]) g_printerr ("Usage: %s [OPTION...] FILE\n", prog_name); exit (1); } - - if (!opt_output) - opt_display = TRUE; /* Get the text */ diff --git a/examples/renderdemo.h b/examples/renderdemo.h index d79df1a6..f18636a2 100644 --- a/examples/renderdemo.h +++ b/examples/renderdemo.h @@ -32,7 +32,8 @@ typedef enum { typedef void (*RenderCallback) (PangoLayout *layout, int x, int y, - gpointer data); + gpointer data, + gboolean show_borders); typedef void (*TransformCallback) (PangoContext *context, PangoMatrix *transform, gpointer data); @@ -46,8 +47,8 @@ void do_output (PangoContext *context, TransformCallback transform_cb, gpointer cb_data, int *width, - int *height); -void update (void); + int *height, + gboolean show_borders); void finalize (void); void fc_substitute_func (FcPattern *pattern, gpointer data); diff --git a/examples/viewer-x.c b/examples/viewer-x.c new file mode 100644 index 00000000..6546612b --- /dev/null +++ b/examples/viewer-x.c @@ -0,0 +1,169 @@ +/* viewer-x.c: Common code X-based rendering demos + * + * Copyright (C) 1999,2004,2005 Red Hat, Inc. + * Copyright (C) 2001 Sun Microsystems + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <errno.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "viewer-x.h" +#include "renderdemo.h" + +/* initialized by main() */ +static Display *display; +static int screen; +static Window window; +static Pixmap pixmap; + +/* initialized by do_init() */ +static PangoContext *context; +static int width, height; + +/* runtime stuff */ +static Region update_region = NULL; +static gboolean show_borders; + +static void +update (void) +{ + GC gc; + XRectangle extents; + int width, height; + + XClipBox (update_region, &extents); + + width = extents.width; + height = extents.height; + + gc = XCreateGC (display, pixmap, 0, NULL); + + XCopyArea (display, pixmap, window, gc, + extents.x, extents.y, + extents.width, extents.height, + extents.x, extents.y); + + XFreeGC (display, gc); + + XDestroyRegion (update_region); + update_region = NULL; +} + +static void +expose (XExposeEvent *xev) +{ + XRectangle r; + + if (!update_region) + update_region = XCreateRegion (); + + r.x = xev->x; + r.y = xev->y; + r.width = xev->width; + r.height = xev->height; + + XUnionRectWithRegion (&r, update_region, update_region); +} + +int +main (int argc, char **argv) +{ + XEvent xev; + unsigned long bg; + XSizeHints size_hints; + unsigned int quit_keycode; + unsigned int borders_keycode; + + g_type_init(); + + parse_options (argc, argv); + + display = XOpenDisplay (NULL); + if (!display) + fail ("Cannot open display %s\n", XDisplayName (NULL)); + screen = DefaultScreen (display); + + do_init (display, screen, &context, &width, &height); + + bg = WhitePixel (display, screen); + window = XCreateSimpleWindow (display, DefaultRootWindow (display), + 0, 0, width, height, 0, + bg, bg); + pixmap = XCreatePixmap (display, window, width, height, + DefaultDepth (display, screen)); + XSelectInput (display, window, ExposureMask | KeyPressMask); + + XMapWindow (display, window); + XmbSetWMProperties (display, window, + get_options_string (), + NULL, NULL, 0, NULL, NULL, NULL); + + memset ((char *)&size_hints, 0, sizeof (XSizeHints)); + size_hints.flags = PSize | PMaxSize; + size_hints.width = width; size_hints.height = height; /* for compat only */ + size_hints.max_width = width; size_hints.max_height = height; + + XSetWMNormalHints (display, window, &size_hints); + + borders_keycode = XKeysymToKeycode(display, 'B'); + quit_keycode = XKeysymToKeycode(display, 'Q'); + + do_render (display, screen, window, pixmap, context, width, height, show_borders); + + while (1) + { + if (!XPending (display) && update_region) + update (); + + XNextEvent (display, &xev); + switch (xev.xany.type) { + case KeyPress: + if (xev.xkey.keycode == quit_keycode) + goto done; + else if (xev.xkey.keycode == borders_keycode) + { + XRectangle r; + show_borders = !show_borders; + do_render (display, screen, window, pixmap, context, width, height, show_borders); + + if (!update_region) + update_region = XCreateRegion (); + + r.x = 0; + r.y = 0; + r.width = width; + r.height = height; + XUnionRectWithRegion (&r, update_region, update_region); + } + break; + case Expose: + expose (&xev.xexpose); + break; + } + } + +done: + + g_object_unref (context); + XFreePixmap (display, pixmap); + finalize (); + + return 0; +} diff --git a/examples/viewer-x.h b/examples/viewer-x.h new file mode 100644 index 00000000..6ff528bb --- /dev/null +++ b/examples/viewer-x.h @@ -0,0 +1,43 @@ +/* viewer-x.h: Common code X-based rendering demos + * + * Copyright (C) 1999,2004,2005 Red Hat, Inc. + * Copyright (C) 2001 Sun Microsystems + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include <pango/pango.h> + +#include <X11/Xlib.h> +#include <X11/Xutil.h> + +/* to be implemented by the backend-specific part */ + +void do_init (Display *display, + int screen, + /* output */ + PangoContext **context, + int *width, + int *height); + +void do_render (Display *display, + int screen, + Window window, + Pixmap pixmap, + PangoContext *context, + int width, + int height, + gboolean show_borders); diff --git a/examples/xftview.c b/examples/xftview.c index c050b6a3..f9ba6f6a 100644 --- a/examples/xftview.c +++ b/examples/xftview.c @@ -1,8 +1,8 @@ /* Pango - * pangoft2topgm.c: Example program to view a UTF-8 encoding file - * using Pango to render result. + * xftview.c: Example program to view a UTF-8 encoding file + * using PangoXft to render result * - * Copyright (C) 1999,2004 Red Hat, Inc. + * Copyright (C) 1999,2004,2005 Red Hat, Inc. * Copyright (C) 2001 Sun Microsystems * * This library is free software; you can redistribute it and/or @@ -21,25 +21,19 @@ * Boston, MA 02111-1307, USA. */ -#include <errno.h> -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - #include "renderdemo.h" +#include "viewer-x.h" #include <pango/pangoxft.h> -static Region update_region = NULL; -static XftDraw *draw; -static PangoContext *context; - static void -xft_render (PangoLayout *layout, - int x, - int y, - gpointer data) +render_callback (PangoLayout *layout, + int x, + int y, + gpointer data, + gboolean show_borders) { + XftDraw *draw = (XftDraw *)data; XftColor color; color.color.red = 0x0; @@ -48,103 +42,66 @@ xft_render (PangoLayout *layout, color.color.alpha = 0xffff; pango_xft_render_layout (draw, &color, layout, x, y); + + /* + if (show_borders) + { + PangoContext *context; + PangoXftFontMap *fontmap; + PangoRenderer *renderer; + PangoRectangle ink, logical; + + pango_layout_get_extents (layout, &ink, &logical); + + context = pango_layout_get_context (layout); + fontmap = (PangoXftFontMap *)pango_context_get_font_map (context); + + humm, we cannot go on, as the following is private api. + need to implement pango_font_map_get_renderer... + + renderer = _pango_xft_font_map_get_renderer (fontmap); + } + */ } void -update (void) +do_init (Display *display, + int screen, + /* output */ + PangoContext **context, + int *width, + int *height) { - XRectangle area; + XftInit (NULL); + *context = pango_xft_get_context (display, screen); + do_output (*context, NULL, NULL, NULL, width, height, FALSE); +} + +void +do_render (Display *display, + int screen, + Window window, + Pixmap pixmap, + PangoContext *context, + int width, + int height, + gboolean show_borders) +{ + XftDraw *draw; XftColor color; - XClipBox (update_region, &area); - XftDrawSetClip (draw, update_region); - XDestroyRegion (update_region); - update_region = NULL; + draw = XftDrawCreate (display, pixmap, + DefaultVisual (display, screen), + DefaultColormap (display, screen)); color.color.red = 0xffff; color.color.blue = 0xffff; color.color.green = 0xffff; color.color.alpha = 0xffff; - XftDrawRect (draw, &color, - area.x, area.y, area.width, area.height); + XftDrawRect (draw, &color, 0, 0, width, height); - do_output (context, xft_render, NULL, draw, NULL, NULL); -} - -static void -expose (XExposeEvent *xev) -{ - XRectangle area; - - if (!update_region) - update_region = XCreateRegion (); - - area.x = xev->x; - area.y = xev->y; - area.width = xev->width; - area.height = xev->height; - - XUnionRectWithRegion (&area, update_region, update_region); -} + do_output (context, render_callback, NULL, draw, NULL, NULL, show_borders); -int main (int argc, char **argv) -{ - Display *display; - int screen; - Window window; - XEvent xev; - unsigned long bg; - int width, height; - XSizeHints size_hints; - - g_type_init(); - - XftInit (NULL); - - parse_options (argc, argv); - - display = XOpenDisplay (NULL); - if (!display) - fail ("Cannot open display %s\n", XDisplayName (NULL)); - - screen = DefaultScreen (display); - bg = WhitePixel (display, screen); - - context = pango_xft_get_context (display, screen); - do_output (context, NULL, NULL, NULL, &width, &height); - - window = XCreateSimpleWindow (display, DefaultRootWindow (display), - 0, 0, width, height, 0, - bg, bg); - XSelectInput (display, window, ExposureMask); - - XMapWindow (display, window); - draw = XftDrawCreate (display, window, - DefaultVisual (display, screen), - DefaultColormap (display, screen)); - XmbSetWMProperties (display, window, - get_options_string (), - NULL, NULL, 0, NULL, NULL, NULL); - - memset ((char *)&size_hints, 0, sizeof (XSizeHints)); - size_hints.flags = PSize | PMaxSize; - size_hints.width = width; size_hints.height = height; /* for compat only */ - size_hints.max_width = width; size_hints.max_height = height; - - XSetWMNormalHints (display, window, &size_hints); - - while (1) - { - if (!XPending (display) && update_region) - update (); - - XNextEvent (display, &xev); - if (xev.xany.type == Expose) - { - expose (&xev.xexpose); - } - } - - return 0; + XftDrawDestroy (draw); } |