// An example for illustrating a bug in the pango layout justify option
// for Herbew.
//
// compile with:
//    cc `pkg-config --cflags --libs pangocairo pango` -o pango-justify-bug pango-justify-bug.c
//
// Dov Grobgeld <dov.grobgeld@gmail.com>
// Sunday 2012-10-28 23:10 

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <pango/pango.h>
#include <pango/pangocairo.h>

//const char *text_no_nikud = "כל אחד";
const char *text_no_nikud = "kl ahd";

const int WIDTH = 50;
const int HEIGHT = 250;
const int MARGIN = 10;
  
// Render through cairo though the backend
// is unimportant for this bug.
int main(int argc, char **argv)
{
  cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, WIDTH, HEIGHT);
  cairo_t *cr = cairo_create(surface);
  PangoFontDescription *desc;
  
  cairo_set_source_rgb(cr,1,1,1);
  cairo_paint(cr);
  cairo_set_source_rgb(cr,0,0,0);
  PangoLayout *layout = pango_cairo_create_layout(cr);
  desc = pango_font_description_from_string ("Serif 20");
  pango_layout_set_font_description (layout, desc);
  pango_layout_set_justify(layout, TRUE);
  pango_layout_set_width(layout, (WIDTH-2*MARGIN)*PANGO_SCALE);
  pango_font_description_free (desc);

  // Draw layout without nikud
  pango_layout_set_text(layout, text_no_nikud,-1);
  cairo_move_to(cr, MARGIN, 20);
  pango_cairo_show_layout(cr, layout);

  // Without justify
  pango_layout_set_text(layout, text_no_nikud,-1);
  pango_layout_set_justify(layout, FALSE);
  cairo_move_to(cr, MARGIN, HEIGHT/2);
  pango_cairo_show_layout(cr, layout);

  cairo_show_page(cr);
  cairo_destroy(cr);
  cairo_surface_write_to_png(surface, "justify-bug.png");
  cairo_surface_destroy(surface);
    
  return(0);
}
