All pastes #459885 Raw Edit

Anonymous

public text v1 · immutable
#459885 ·published 2007-04-26 19:07 UTC
rendered paste body
/*======================================================================
//  caps.c - A paps like program
//
//  Dov Grobgeld <dov.grobgeld@gmail.com>
//  Tue Sep  5 21:06:50 2006
//----------------------------------------------------------------------
*/

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>
#include <cairo-ps.h>
#include <cairo-svg.h>
#include <cairo-pdf.h>

static int
draw_text(cairo_t *cr,
          const gchar *text);
cairo_status_t caps_cairo_write(void		  *closure,
                                const unsigned char *data,
                                unsigned int	   length);

static void die(const char *fmt, ...)
{
    va_list ap;
    va_start(ap,fmt); 
    
    vfprintf(stderr, fmt, ap);
    exit(-1);
}

int main(int argc, char **argv)
{
  cairo_surface_t *surface;
  cairo_t *cr;
  int width = 595;
  int height = 842;
  const gchar *output_file = "-";
  FILE *CAPS_OUT = NULL;
  gchar *text = NULL;
  GError *error = NULL;

  /* Prerequisite when using glib. */
  g_type_init();

  if (argc > 1)
    {
      gchar *filename = argv[1];
      gsize length;
      g_file_get_contents(filename,
                          &text,
                          &length,
                          &error);
      if (error)
        die("Error: %s\n", error->message);
    }
  else
    text = g_strdup ("&#20320;&#22909;&#65292;&#36825;&#26159;&#20013;&#25991;&#31446;&#25490;&#27979;&#35797;&#12290;\n"
		     "&#27426;&#36814;&#26469;&#21040;&#20013;&#22269;&#21271;&#20140;&#12290;\n"
		     "&#30333;&#26085;&#20381;&#23665;&#23613;&#65292;\n"
		     "Roses are Red,\n"
		     "Grass is Green. 2006\n"
		     "Arabic is &#1711;&#1604;&#8204;&#1607;&#1575; &#1602;&#1585;&#1605;&#1586;&#1606;&#1583;&#1548;&#8207;\n"
		     "&#1670;&#1605;&#1606; &#1587;&#1576;&#1586;. &#1778;&#1776;&#1776;&#1782;\n"
		     "&#30333;&#26085;&#20381;&#23665;&#23613;&#65292; 2006\n");

  if (strcmp(output_file, "-") == 0)
      CAPS_OUT = stdout;
  else
      CAPS_OUT = fopen(output_file, "w");
  
  surface = cairo_pdf_surface_create_for_stream (caps_cairo_write,
						 CAPS_OUT,
						 width,
						 height);

  cr = cairo_create (surface);
  cairo_rotate (cr, G_PI / 2);

  cairo_move_to (cr, 0, -500);
  draw_text(cr, text);

  cairo_destroy (cr);
  cairo_surface_destroy (surface);
  
  exit(0);
  return(0);
}

static int
draw_text(cairo_t *cr,
          const gchar *text)
{
    PangoFontDescription *font_description;
    PangoLayout *layout;
    PangoContext *context;
    
    /* Create a PangoLayout, set the font and text */
    layout = pango_cairo_create_layout (cr);

    context = pango_layout_get_context (layout);
    pango_context_set_base_gravity (context, PANGO_GRAVITY_AUTO);

    pango_layout_set_markup (layout, text, -1);
    pango_layout_set_width (layout, (int)(500 * PANGO_SCALE));

    pango_cairo_show_layout(cr, layout);

    cairo_show_page(cr);

    return 0;
}

cairo_status_t caps_cairo_write(void		  *closure,
                                const unsigned char *data,
                                unsigned int	   length)
{
    FILE *CAPS_OUT = (FILE*)closure;
    fwrite(data,length,1,CAPS_OUT);
    return CAIRO_STATUS_SUCCESS;
}