Exemplo n.º 1
0
/** Draw a TextLine object.
 * @param object The renderer object to use for transform and output
 * @param text_line The TextLine to render, including font and height.
 * @param pos The position to render it at.
 * @param color The color to render it with.
 */
static void 
draw_text_line (DiaRenderer *object, TextLine *text_line,
		Point *pos, Alignment alignment, Color *color)
{
  DiaGdkRenderer *renderer = DIA_GDK_RENDERER (object);
  GdkColor gdkcolor;
  int x,y;
  Point start_pos;
  PangoLayout* layout = NULL;
  const gchar *text = text_line_get_string(text_line);
  int height_pixels;
  real font_height = text_line_get_height(text_line);
  real scale = dia_transform_length(renderer->transform, 1.0);

  if (text == NULL || *text == '\0') return; /* Don't render empty strings. */

  point_copy(&start_pos,pos);

  renderer_color_convert(renderer, color, &gdkcolor);
 
  height_pixels = dia_transform_length(renderer->transform, font_height);
  if (height_pixels < 2) { /* "Greeking" instead of making tiny font */
    int width_pixels = dia_transform_length(renderer->transform,
					    text_line_get_width(text_line));
    gdk_gc_set_foreground(renderer->gc, &gdkcolor);
    gdk_gc_set_dashes(renderer->gc, 0, (gint8*)"\1\2", 2);
    dia_transform_coords(renderer->transform, start_pos.x, start_pos.y, &x, &y);
    gdk_draw_line(renderer->pixmap, renderer->gc, x, y, x + width_pixels, y);
    return;
  } else {
    start_pos.y -= text_line_get_ascent(text_line);
    start_pos.x -= text_line_get_alignment_adjustment (text_line, alignment);
  
    dia_transform_coords(renderer->transform, 
			 start_pos.x, start_pos.y, &x, &y);

    layout = dia_font_build_layout(text, text_line->font,
				   dia_transform_length(renderer->transform, text_line->height)/20.0);
#if defined(PANGO_VERSION_ENCODE)
#  if (PANGO_VERSION >= PANGO_VERSION_ENCODE(1,16,0))
    /* I'd say the former Pango API was broken, i.e. leaky */
#    define HAVE_pango_layout_get_line_readonly
#   endif
#endif
    text_line_adjust_layout_line (text_line, 
#if defined(HAVE_pango_layout_get_line_readonly)
                                  pango_layout_get_line_readonly(layout, 0),
#else
                                  pango_layout_get_line(layout, 0),
#endif
				  scale/20.0);

    if (renderer->highlight_color != NULL) {
      draw_highlighted_string(renderer, layout, x, y, &gdkcolor);
    } else {
#if defined HAVE_FREETYPE
      {
	FT_Bitmap ftbitmap;
	int width, height;
	GdkPixbuf *rgba = NULL;
	
	width = dia_transform_length(renderer->transform,
				     text_line_get_width(text_line));
	height = dia_transform_length(renderer->transform, 
				      text_line_get_height(text_line));
	
	if (width > 0) {
	  int stride;
	  guchar* pixels;
	  int i,j;
	  guint8 *graybitmap;

	  initialize_ft_bitmap(&ftbitmap, width, height);
	  pango_ft2_render_layout(&ftbitmap, layout, 0, 0);
	  
	  graybitmap = ftbitmap.buffer;
	  
	  rgba = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height);
	  stride = gdk_pixbuf_get_rowstride(rgba);
	  pixels = gdk_pixbuf_get_pixels(rgba);
	  for (i = 0; i < height; i++) {
	    for (j = 0; j < width; j++) {
	      pixels[i*stride+j*4] = gdkcolor.red>>8;
	      pixels[i*stride+j*4+1] = gdkcolor.green>>8;
	      pixels[i*stride+j*4+2] = gdkcolor.blue>>8;
	      pixels[i*stride+j*4+3] = graybitmap[i*ftbitmap.pitch+j];
	    }
	  }
	  g_free(graybitmap);

	  gdk_draw_pixbuf(renderer->pixmap, renderer->gc, rgba, 0, 0, x, y, width, height, GDK_RGB_DITHER_NONE, 0, 0);

	  g_object_unref(G_OBJECT(rgba));
	}
      }
#else
      gdk_gc_set_foreground(renderer->gc, &gdkcolor);
	
      gdk_draw_layout(renderer->pixmap, renderer->gc, x, y, layout);
#endif
    } /* !higlight_color */
    g_object_unref(G_OBJECT(layout));
  } /* !greeking */
Exemplo n.º 2
0
static void
draw_text_line(DiaRenderer *self,
	       TextLine *text_line,
	       Point *pos, Alignment alignment, Color *color)
{
  DiaPsFt2Renderer *renderer = DIA_PS_FT2_RENDERER(self);
  PangoLayout *layout;
  int line, linecount;
  double xpos = pos->x, ypos = pos->y;
  char *text = text_line_get_string(text_line);
  /* TODO: we could probably pass the alignment down to the PS file? */
  xpos -= text_line_get_alignment_adjustment (text_line, alignment);

/* Using the global PangoContext does not allow to have renderer specific 
 * different ones. Or it implies the push/pop _context mess. Anyway just 
 * get rid of warnings for now. But the local code may be resurreted 
 * sooner or later...                                               --hb
 */
#define USE_GLOBAL_CONTEXT
#ifndef USE_GLOBAL_CONTEXT
  PangoAttrList* list;
  PangoAttribute* attr;
  guint length;
#endif

  if ((!text)||(text == (const char *)(1))) return;

  lazy_setcolor(DIA_PS_RENDERER(renderer),color);

#define ANNOYING_SCALE_FACTOR 5.9

  /* Make sure the letters aren't too wide. */
#ifdef USE_GLOBAL_CONTEXT
  layout = dia_font_build_layout(text, text_line_get_font(text_line),
				 text_line_get_height(text_line)*ANNOYING_SCALE_FACTOR);
#else
  /* approximately what would be required but w/o dia_font_get_context() */
  dia_font_set_height(text_line_get_font(text_line),
		      text_line_get_height(text_line));
  layout = pango_layout_new(dia_font_get_context());

  length = text ? strlen(text) : 0;
  pango_layout_set_text(layout,text,length);
        
  list = pango_attr_list_new();

  attr = pango_attr_font_desc_new(dia_font_get_description(text_line_get_font(text_line)));
  attr->start_index = 0;
  attr->end_index = length;
  pango_attr_list_insert(list,attr);
    
  pango_layout_set_attributes(layout,list);
  pango_attr_list_unref(list);

  pango_layout_set_indent(layout,0);
  pango_layout_set_justify(layout,FALSE);
#endif

  pango_layout_set_alignment(layout, PANGO_ALIGN_LEFT);
    
  linecount = pango_layout_get_line_count(layout);
  for (line = 0; line < linecount; line++) {
    PangoLayoutLine *layoutline = pango_layout_get_line(layout, line);

    /* Not sure scale is the right one here.  */
    text_line_adjust_layout_line(text_line, layoutline, ANNOYING_SCALE_FACTOR);

    postscript_draw_contour(DIA_PS_RENDERER(renderer),
			    DPI, /* dpi_x */
			    layoutline, xpos, ypos);
    ypos += 10;/* Some line height thing??? */
  }
}