gdkdrawable-fb2.c 33.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2
#include "gdkprivate-fb.h"
#include "mi.h"
Elliot Lee's avatar
Elliot Lee committed
3 4 5 6 7 8 9 10 11

/* #define USE_FTGRAYS */
#define USE_AA
#include <freetype/ftglyph.h>

#include <endian.h>
#ifndef __BYTE_ORDER
#error "endian.h needs to #define __BYTE_ORDER"
#endif
Elliot Lee's avatar
Elliot Lee committed
12 13 14 15 16

#ifndef g_alloca
#define g_alloca alloca
#endif

Elliot Lee's avatar
Elliot Lee committed
17 18
static void gdk_fb_drawable_set_pixel(GdkDrawable *drawable, GdkGC *gc, int x, int y, GdkColor *spot, gboolean abs_coords);

Elliot Lee's avatar
Elliot Lee committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
static void gdk_fb_drawable_destroy   (GdkDrawable     *drawable);
void gdk_fb_draw_rectangle (GdkDrawable    *drawable,
			    GdkGC          *gc,
			    gint            filled,
			    gint            x,
			    gint            y,
			    gint            width,
			    gint            height);
static void gdk_fb_draw_arc       (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   gint            filled,
				   gint            x,
				   gint            y,
				   gint            width,
				   gint            height,
				   gint            angle1,
				   gint            angle2);
static void gdk_fb_draw_polygon   (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   gint            filled,
				   GdkPoint       *points,
				   gint            npoints);
static void gdk_fb_draw_text      (GdkDrawable    *drawable,
				   GdkFont        *font,
				   GdkGC          *gc,
				   gint            x,
				   gint            y,
				   const gchar    *text,
				   gint            text_length);
static void gdk_fb_draw_text_wc   (GdkDrawable    *drawable,
				   GdkFont        *font,
				   GdkGC          *gc,
				   gint            x,
				   gint            y,
				   const GdkWChar *text,
				   gint            text_length);
Elliot Lee's avatar
Elliot Lee committed
55 56 57 58 59 60
static void gdk_fb_draw_glyphs(GdkDrawable      *drawable,
			       GdkGC	           *gc,
			       PangoFont        *font,
			       gint              x,
			       gint              y,
			       PangoGlyphString *glyphs);
Elliot Lee's avatar
Elliot Lee committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
void gdk_fb_draw_drawable  (GdkDrawable    *drawable,
			    GdkGC          *gc,
			    GdkPixmap      *src,
			    gint            xsrc,
			    gint            ysrc,
			    gint            xdest,
			    gint            ydest,
			    gint            width,
			    gint            height);
static void gdk_fb_draw_points    (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkPoint       *points,
				   gint            npoints);
static void gdk_fb_draw_segments  (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkSegment     *segs,
				   gint            nsegs);
static void gdk_fb_draw_lines     (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkPoint       *points,
				   gint            npoints);

GdkDrawableClass _gdk_fb_drawable_class = {
  gdk_fb_drawable_destroy,
  (gpointer)_gdk_fb_gc_new,
  gdk_fb_draw_rectangle,
  gdk_fb_draw_arc,
  gdk_fb_draw_polygon,
  gdk_fb_draw_text,
  gdk_fb_draw_text_wc,
  gdk_fb_draw_drawable,
  gdk_fb_draw_points,
  gdk_fb_draw_segments,
Elliot Lee's avatar
Elliot Lee committed
94 95
  gdk_fb_draw_lines,
  gdk_fb_draw_glyphs
Elliot Lee's avatar
Elliot Lee committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
};

/*****************************************************
 * FB specific implementations of generic functions *
 *****************************************************/

GdkColormap*
gdk_drawable_get_colormap (GdkDrawable *drawable)
{
  GdkColormap *retval = GDK_DRAWABLE_P(drawable)->colormap;

  if(!retval)
    retval = gdk_colormap_get_system();

  return retval;
}

void
gdk_drawable_set_colormap (GdkDrawable *drawable,
			   GdkColormap *colormap)
{
  GdkColormap *old_cmap;
  old_cmap = GDK_DRAWABLE_P(drawable)->colormap;
  GDK_DRAWABLE_P(drawable)->colormap = gdk_colormap_ref(colormap);
  gdk_colormap_unref(old_cmap);
}

/* Drawing
 */
static void 
gdk_fb_drawable_destroy (GdkDrawable *drawable)
{
}

static GdkRegion *
gdk_fb_clip_region(GdkDrawable *drawable, GdkGC *gc, gboolean do_clipping)
{
  GdkRectangle draw_rect;
  GdkRegion *real_clip_region, *tmpreg;

  g_assert(!GDK_IS_WINDOW(drawable) || !GDK_WINDOW_P(drawable)->input_only);

  draw_rect.x = GDK_DRAWABLE_FBDATA(drawable)->llim_x;
  draw_rect.y = GDK_DRAWABLE_FBDATA(drawable)->llim_y;
  draw_rect.width = GDK_DRAWABLE_FBDATA(drawable)->lim_x - draw_rect.x;
  draw_rect.height = GDK_DRAWABLE_FBDATA(drawable)->lim_y - draw_rect.y;
  real_clip_region = gdk_region_rectangle(&draw_rect);

  if(do_clipping && GDK_IS_WINDOW(drawable) && GDK_WINDOW_P(drawable)->mapped && !GDK_WINDOW_P(drawable)->input_only)
    {
      GdkWindow *parentwin, *lastwin;

      for(parentwin = lastwin = ((GdkWindow *)drawable);
	  parentwin; lastwin = parentwin, parentwin = GDK_WINDOW_P(parentwin)->parent)
	{
	  GList *cur;

	  for(cur = GDK_WINDOW_P(parentwin)->children; cur && cur->data != lastwin; cur = cur->next)
	    {
	      if(!GDK_WINDOW_P(cur->data)->mapped || GDK_WINDOW_P(cur->data)->input_only)
		continue;

	      draw_rect.x = GDK_DRAWABLE_FBDATA(cur->data)->llim_x;
	      draw_rect.y = GDK_DRAWABLE_FBDATA(cur->data)->llim_y;
	      draw_rect.width = GDK_DRAWABLE_FBDATA(cur->data)->lim_x - draw_rect.x;
	      draw_rect.height = GDK_DRAWABLE_FBDATA(cur->data)->lim_y - draw_rect.y;

	      tmpreg = gdk_region_rectangle(&draw_rect);
	      gdk_region_subtract(real_clip_region, tmpreg);
	      gdk_region_destroy(tmpreg);
	    }
	}
    }

  if(gc)
    {
      if(GDK_GC_FBDATA(gc)->clip_region)
	{
	  tmpreg = gdk_region_copy(GDK_GC_FBDATA(gc)->clip_region);
	  gdk_region_offset(tmpreg, GDK_DRAWABLE_FBDATA(drawable)->abs_x + GDK_GC_P(gc)->clip_x_origin,
			    GDK_DRAWABLE_FBDATA(drawable)->abs_y + GDK_GC_P(gc)->clip_y_origin);
	  gdk_region_intersect(real_clip_region, tmpreg);
	  gdk_region_destroy(tmpreg);
	}

      if(GDK_GC_FBDATA(gc)->values.clip_mask)
	{
	  GdkDrawable *cmask = GDK_GC_FBDATA(gc)->values.clip_mask;

	  g_assert(GDK_DRAWABLE_P(cmask)->depth == 1);
	  g_assert(GDK_DRAWABLE_FBDATA(cmask)->abs_x == 0
		   && GDK_DRAWABLE_FBDATA(cmask)->abs_y == 0);

	  draw_rect.x = GDK_DRAWABLE_FBDATA(drawable)->abs_x + GDK_DRAWABLE_FBDATA(cmask)->llim_x + GDK_GC_FBDATA(gc)->values.clip_x_origin;
	  draw_rect.y = GDK_DRAWABLE_FBDATA(drawable)->abs_y + GDK_DRAWABLE_FBDATA(cmask)->llim_y + GDK_GC_FBDATA(gc)->values.clip_y_origin;
	  draw_rect.width = GDK_DRAWABLE_P(cmask)->width;
	  draw_rect.height = GDK_DRAWABLE_P(cmask)->height;

	  tmpreg = gdk_region_rectangle(&draw_rect);
	  gdk_region_intersect(real_clip_region, tmpreg);
	  gdk_region_destroy(tmpreg);
	}
    }

  return real_clip_region;
}

static void
Elliot Lee's avatar
Elliot Lee committed
204
gdk_fb_fill_span(GdkDrawable *drawable, GdkGC *gc, GdkSegment *cur, GdkColor *color, GdkVisual *visual)
Elliot Lee's avatar
Elliot Lee committed
205 206
{
  int curx, cury;
Elliot Lee's avatar
Elliot Lee committed
207
  GdkColor spot = *color;
Elliot Lee's avatar
Elliot Lee committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

  if(gc
     && (GDK_GC_FBDATA(gc)->values.clip_mask
	 || GDK_GC_FBDATA(gc)->values.tile
	 || GDK_GC_FBDATA(gc)->values.stipple))
    {
      int clipxoff, clipyoff; /* Amounts to add to curx & cury to get x & y in clip mask */
      int tsxoff, tsyoff;
      GdkDrawable *cmask;
      guchar *clipmem;
      guint mask_rowstride;
      GdkPixmap *ts = NULL;
      gboolean solid_stipple;

      cmask = GDK_GC_FBDATA(gc)->values.clip_mask;
      if(cmask)
	{
	  clipmem = GDK_DRAWABLE_FBDATA(cmask)->mem;
	  clipxoff = GDK_DRAWABLE_FBDATA(cmask)->abs_x - GDK_GC_FBDATA(gc)->values.clip_x_origin - GDK_DRAWABLE_FBDATA(drawable)->abs_x;
	  clipyoff = GDK_DRAWABLE_FBDATA(cmask)->abs_y - GDK_GC_FBDATA(gc)->values.clip_y_origin - GDK_DRAWABLE_FBDATA(drawable)->abs_y;
	  mask_rowstride = GDK_DRAWABLE_FBDATA(cmask)->rowstride;
	}

      if(GDK_GC_FBDATA(gc)->values.fill == GDK_TILED
	 && GDK_GC_FBDATA(gc)->values.tile)
	{
	  gint xstep, ystep;
	  gint relx, rely;
Elliot Lee's avatar
Elliot Lee committed
236 237 238 239
	  GdkFBDrawingContext *dc, dc_data;
	  dc = &dc_data;

	  gdk_fb_drawing_context_init(dc, drawable, gc, FALSE, TRUE);
Elliot Lee's avatar
Elliot Lee committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	  ts = GDK_GC_FBDATA(gc)->values.tile;
	  for(cury = cur->y1; cury < cur->y2; cury += ystep)
	    {
	      int drawh;
	      
	      rely = cury - GDK_DRAWABLE_FBDATA(drawable)->abs_y;
	      drawh = (rely + GDK_GC_FBDATA(gc)->values.ts_y_origin) % GDK_DRAWABLE_P(ts)->height;
	      if(drawh < 0)
		drawh += GDK_DRAWABLE_P(ts)->height;

	      ystep = MIN(GDK_DRAWABLE_P(ts)->height - drawh, cur->y2 - rely);

	      for(curx = cur->x1; curx < cur->x2; curx += xstep)
		{
		  int draww;

		  relx = curx - GDK_DRAWABLE_FBDATA(drawable)->abs_x;

		  draww = (relx + GDK_GC_FBDATA(gc)->values.ts_x_origin) % GDK_DRAWABLE_P(ts)->width;
		  if(draww < 0)
		    draww += GDK_DRAWABLE_P(ts)->width;

		  xstep = MIN(GDK_DRAWABLE_P(ts)->width - draww, cur->x2 - relx);

Elliot Lee's avatar
Elliot Lee committed
265 266
		  gdk_fb_draw_drawable_3(drawable, gc, ts,
					 dc,
Elliot Lee's avatar
Elliot Lee committed
267 268
					 draww, drawh,
					 relx, rely,
Elliot Lee's avatar
Elliot Lee committed
269
					 xstep, ystep);
Elliot Lee's avatar
Elliot Lee committed
270 271 272
		}
	    }

Elliot Lee's avatar
Elliot Lee committed
273 274
	  gdk_fb_drawing_context_finalize(dc);

Elliot Lee's avatar
Elliot Lee committed
275 276 277 278 279 280 281 282 283 284 285 286
	  return;
	}
      else if((GDK_GC_FBDATA(gc)->values.fill == GDK_STIPPLED
	      || GDK_GC_FBDATA(gc)->values.fill == GDK_OPAQUE_STIPPLED)
	      && GDK_GC_FBDATA(gc)->values.stipple)
	{
	  ts = GDK_GC_FBDATA(gc)->values.stipple;
	  tsxoff = GDK_DRAWABLE_FBDATA(ts)->abs_x - GDK_GC_FBDATA(gc)->values.ts_x_origin - GDK_DRAWABLE_FBDATA(drawable)->abs_x;
	  tsyoff = GDK_DRAWABLE_FBDATA(ts)->abs_y - GDK_GC_FBDATA(gc)->values.ts_y_origin - GDK_DRAWABLE_FBDATA(drawable)->abs_y;
	  solid_stipple = (GDK_GC_FBDATA(gc)->values.fill == GDK_OPAQUE_STIPPLED);
	}

Elliot Lee's avatar
Elliot Lee committed
287
      for(cury = cur->y1; cury < cur->y2; cury++)
Elliot Lee's avatar
Elliot Lee committed
288
	{
Elliot Lee's avatar
Elliot Lee committed
289
	  for(curx = cur->x1; curx < cur->x2; curx++)
Elliot Lee's avatar
Elliot Lee committed
290
	    {
Elliot Lee's avatar
Elliot Lee committed
291 292
	      int maskx = curx+clipxoff, masky = cury + clipyoff;
	      guchar foo;
Elliot Lee's avatar
Elliot Lee committed
293

Elliot Lee's avatar
Elliot Lee committed
294
	      if(cmask)
Elliot Lee's avatar
Elliot Lee committed
295
		{
Elliot Lee's avatar
Elliot Lee committed
296
		  foo = clipmem[masky*mask_rowstride + (maskx >> 3)];
Elliot Lee's avatar
Elliot Lee committed
297

Elliot Lee's avatar
Elliot Lee committed
298 299
		  if(!(foo & (1 << (maskx % 8))))
		    continue;
Elliot Lee's avatar
Elliot Lee committed
300 301
		}

Elliot Lee's avatar
Elliot Lee committed
302
	      if(ts)
Elliot Lee's avatar
Elliot Lee committed
303
		{
Elliot Lee's avatar
Elliot Lee committed
304
		  int wid = GDK_DRAWABLE_P(ts)->width, hih = GDK_DRAWABLE_P(ts)->height;
Elliot Lee's avatar
Elliot Lee committed
305

Elliot Lee's avatar
Elliot Lee committed
306 307 308 309 310 311
		  maskx = (curx+tsxoff)%wid;
		  masky = (cury+tsyoff)%hih;
		  if(maskx < 0)
		    maskx += wid;
		  if(masky < 0)
		    masky += hih;
Elliot Lee's avatar
Elliot Lee committed
312

Elliot Lee's avatar
Elliot Lee committed
313 314 315 316
		  foo = GDK_DRAWABLE_FBDATA(ts)->mem[(maskx >> 3) + GDK_DRAWABLE_FBDATA(ts)->rowstride*masky];
		  if(foo & (1 << (maskx % 8)))
		    {
		      spot = GDK_GC_FBDATA(gc)->values.foreground;
Elliot Lee's avatar
Elliot Lee committed
317
		    }
Elliot Lee's avatar
Elliot Lee committed
318
		  else if(solid_stipple)
Elliot Lee's avatar
Elliot Lee committed
319
		    {
Elliot Lee's avatar
Elliot Lee committed
320
		      spot = GDK_GC_FBDATA(gc)->values.background;
Elliot Lee's avatar
Elliot Lee committed
321
		    }
Elliot Lee's avatar
Elliot Lee committed
322 323
		  else
		    continue;
Elliot Lee's avatar
Elliot Lee committed
324
		}
Elliot Lee's avatar
Elliot Lee committed
325 326

	      gdk_fb_drawable_set_pixel(drawable, gc, curx, cury, &spot, TRUE);
Elliot Lee's avatar
Elliot Lee committed
327 328 329 330 331
	    }
	}
    }
  else
    {
Elliot Lee's avatar
Elliot Lee committed
332 333 334 335 336
      guchar *mem = GDK_DRAWABLE_FBDATA(drawable)->mem, *ptr;
      guint rowstride = GDK_DRAWABLE_FBDATA(drawable)->rowstride;
      int n;

      switch(GDK_DRAWABLE_P(drawable)->depth)
Elliot Lee's avatar
Elliot Lee committed
337 338
	{
	case 1:
Elliot Lee's avatar
Elliot Lee committed
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	  {
	    int fromx = MIN((cur->x1+7)&(~7), cur->x2);
	    int begn = fromx - cur->x1, begoff = cur->x1 % 8, endn;
	    guchar begmask, endmask;
	    int body_end = cur->x2 & ~7;
	    int body_len = (body_end - fromx)/8;

	    begmask = ((1 << (begn + begoff)) - 1)
	      & ~((1 << (begoff)) - 1);
	    endn = cur->x2 - body_end;
	    endmask = (1 << endn) - 1;

	    for(cury = cur->y1; cury < cur->y2; cury++)
	      {
		ptr = mem + cury*rowstride + (cur->x1 >> 3);

		if(spot.pixel)
		  *ptr |= begmask;
		else
		  *ptr &= ~begmask;

		curx = fromx;

		if(curx < cur->x2)
		  {
		    ptr = mem + cury*rowstride + (curx >> 3);
		    memset(ptr, spot.pixel?0xFF:0, body_len);

		    if(endn)
		      {
			ptr = mem + cury*rowstride + (body_end >> 3);
			if(spot.pixel)
			  *ptr |= endmask;
			else
			  *ptr &= ~endmask;
		      }
		  }
	      }
	  }
Elliot Lee's avatar
Elliot Lee committed
378 379 380 381
	  break;
	case 8:
	  for(cury = cur->y1; cury < cur->y2; cury++)
	    {
Elliot Lee's avatar
Elliot Lee committed
382 383
	      ptr = mem + cury*rowstride + cur->x1;
	      memset(ptr, spot.pixel, cur->x2 - cur->x1);
Elliot Lee's avatar
Elliot Lee committed
384 385 386
	    }
	  break;
	case 16:
Elliot Lee's avatar
Elliot Lee committed
387 388 389 390 391 392 393 394 395 396 397
	  {
	    int i;
	    n = cur->x2 - cur->x1;
	    for(cury = cur->y1; cury < cur->y2; cury++)
	      {
		guint16 *p16 = (guint16 *)(mem + cury * rowstride + cur->x1*2);
		for(i = 0; i < n; i++)
		  *(p16++) = spot.pixel;
	      }
	  }
	  break;
Elliot Lee's avatar
Elliot Lee committed
398
	case 24:
Elliot Lee's avatar
Elliot Lee committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	  {
	    guchar redval = spot.red>>8, greenval=spot.green>>8, blueval=spot.blue>>8;
	    guchar *firstline, *ptr_end;

	    if((cur->y2 - cur->y1) <= 0)
	      break;

	    n = (cur->x2 - cur->x1)*3;

	    firstline = ptr = mem + cur->y1 * rowstride + cur->x1*3;
	    ptr_end = ptr+n;
	    while(ptr < ptr_end)
	      {
		ptr[gdk_display->red_byte] = redval;
		ptr[gdk_display->green_byte] = greenval;
		ptr[gdk_display->blue_byte] = blueval;
		ptr += 3;
	      }
	    for(cury = cur->y1 + 1, ptr = mem + cury * rowstride + cur->x1*3; cury < cur->y2; cury++, ptr += rowstride)
	      {
		memcpy(ptr, firstline, n);
	      }
	  }
	  break;
Elliot Lee's avatar
Elliot Lee committed
423
	case 32:
Elliot Lee's avatar
Elliot Lee committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437
	  {
	    int i;
	    n = cur->x2 - cur->x1;
	    for(cury = cur->y1; cury < cur->y2; cury++)
	      {
		guint32 *p32 = (guint32 *)(mem + cury * rowstride + cur->x1*4);
		for(i = 0; i < n; i++)
		  *(p32++) = spot.pixel;
	      }
	  }
	  break;
	default:
	  g_assert_not_reached();
#if 0
Elliot Lee's avatar
Elliot Lee committed
438 439 440 441
	  for(cury = cur->y1; cury < cur->y2; cury++)
	    {
	      for(curx = cur->x1; curx < cur->x2; curx++)
		{
Elliot Lee's avatar
Elliot Lee committed
442
		  gdk_fb_drawable_set_pixel(drawable, gc, curx, cury, &spot, TRUE);
Elliot Lee's avatar
Elliot Lee committed
443 444
		}
	    }
Elliot Lee's avatar
Elliot Lee committed
445
#endif
Elliot Lee's avatar
Elliot Lee committed
446 447 448 449 450 451 452 453 454 455 456
	  break;
	}
    }
}

void
gdk_fb_fill_spans(GdkDrawable *drawable,
		  GdkGC *gc,
		  GdkRectangle *rects, int nrects)
{
  int i;
Elliot Lee's avatar
Elliot Lee committed
457
  GdkColor color;
Elliot Lee's avatar
Elliot Lee committed
458 459 460 461 462 463 464 465 466 467
  GdkRegion *real_clip_region, *tmpreg;
  GdkRectangle draw_rect, cursor_rect;
  GdkVisual *visual = gdk_visual_get_system();
  gboolean handle_cursor = FALSE;

  if(GDK_IS_WINDOW(drawable) && !GDK_WINDOW_P(drawable)->mapped)
    return;
  if(GDK_IS_WINDOW(drawable) && GDK_WINDOW_P(drawable)->input_only)
    g_error("Drawing on the evil input-only!");

Elliot Lee's avatar
Elliot Lee committed
468 469
  if(gc && (GDK_GC_FBDATA(gc)->values_mask | GDK_GC_FOREGROUND))
    color = GDK_GC_FBDATA(gc)->values.foreground;
Elliot Lee's avatar
Elliot Lee committed
470
  else if(GDK_IS_WINDOW(drawable))
Elliot Lee's avatar
Elliot Lee committed
471
    color = GDK_WINDOW_P(drawable)->bg_color;
Elliot Lee's avatar
Elliot Lee committed
472
  else
Elliot Lee's avatar
Elliot Lee committed
473
    gdk_color_black(GDK_DRAWABLE_P(drawable)->colormap, &color);
Elliot Lee's avatar
Elliot Lee committed
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

  real_clip_region = gdk_fb_clip_region(drawable, gc, TRUE);

  gdk_fb_get_cursor_rect(&cursor_rect);
  if(GDK_DRAWABLE_FBDATA(drawable)->mem == GDK_DRAWABLE_FBDATA(gdk_parent_root)->mem
     && cursor_rect.x >= 0
     && gdk_region_rect_in(real_clip_region, &cursor_rect) != GDK_OVERLAP_RECTANGLE_OUT)
    {
      handle_cursor = TRUE;
      gdk_fb_cursor_hide();
    }
    
  for(i = 0; i < nrects; i++)
    {
      GdkSegment cur;
      int j;

      cur.x1 = rects[i].x;
      cur.y1 = rects[i].y;
      cur.x2 = cur.x1 + rects[i].width;
      cur.y2 = cur.y1 + rects[i].height;
      g_assert(cur.x2 >= cur.x1);
      g_assert(cur.y2 >= cur.y1);

      cur.x1 += GDK_DRAWABLE_FBDATA(drawable)->abs_x;
      cur.x1 = MAX(cur.x1, GDK_DRAWABLE_FBDATA(drawable)->llim_x);

      cur.x2 += GDK_DRAWABLE_FBDATA(drawable)->abs_x;
      cur.x2 = MIN(cur.x2, GDK_DRAWABLE_FBDATA(drawable)->lim_x);
      cur.x1 = MIN(cur.x1, cur.x2);

      cur.y1 += GDK_DRAWABLE_FBDATA(drawable)->abs_y;
      cur.y1 = MAX(cur.y1, GDK_DRAWABLE_FBDATA(drawable)->llim_y);

      cur.y2 += GDK_DRAWABLE_FBDATA(drawable)->abs_y;
      cur.y2 = MIN(cur.y2, GDK_DRAWABLE_FBDATA(drawable)->lim_y);
      cur.y1 = MIN(cur.y1, cur.y2);

      draw_rect.x = cur.x1;
      draw_rect.y = cur.y1;
      draw_rect.width = cur.x2 - cur.x1;
      draw_rect.height = cur.y2 - cur.y1;

      switch(gdk_region_rect_in(real_clip_region, &draw_rect))
	{
	case GDK_OVERLAP_RECTANGLE_PART:
	  tmpreg = gdk_region_rectangle(&draw_rect);
	  gdk_region_intersect(tmpreg, real_clip_region);
	  for(j = 0; j < tmpreg->numRects; j++)
Elliot Lee's avatar
Elliot Lee committed
523 524 525 526
	    {
	      cur = tmpreg->rects[j];
	      gdk_fb_fill_span(drawable, gc, &cur, &color, visual);
	    }
Elliot Lee's avatar
Elliot Lee committed
527 528 529
	  gdk_region_destroy(tmpreg);
	  break;
	case GDK_OVERLAP_RECTANGLE_IN:
Elliot Lee's avatar
Elliot Lee committed
530
	  gdk_fb_fill_span(drawable, gc, &cur, &color, visual);
Elliot Lee's avatar
Elliot Lee committed
531 532 533 534 535 536 537 538 539 540 541
	  break;
	default:
	  break;
	}
    }

  gdk_region_destroy(real_clip_region);
  if(handle_cursor)
    gdk_fb_cursor_unhide();
}

Elliot Lee's avatar
Elliot Lee committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
typedef enum { GPR_USED_BG, GPR_AA_GRAYVAL, GPR_NONE, GPR_ERR_BOUNDS } GetPixelRet;
static GetPixelRet
gdk_fb_drawable_get_pixel(GdkDrawable *drawable, GdkGC *gc, int x, int y, GdkColor *spot,
			  gboolean abs_coords, GdkDrawable *bg_relto, GdkDrawable *bgpm)
{
  GetPixelRet retval = GPR_NONE;
  guchar *mem = GDK_DRAWABLE_FBDATA(drawable)->mem;
  guint rowstride = GDK_DRAWABLE_FBDATA(drawable)->rowstride;

  if(!abs_coords)
    {
      x += GDK_DRAWABLE_FBDATA(drawable)->abs_x;
      y += GDK_DRAWABLE_FBDATA(drawable)->abs_y;
    }

  switch(GDK_DRAWABLE_P(drawable)->depth)
    {
    case 1:
      {
	guchar foo = mem[(x >> 3) + y * rowstride];
	if(foo & (1 << (x % 8)))
	  *spot = GDK_GC_FBDATA(gc)->values.foreground;
	else
	  {
	    retval = GPR_USED_BG;

	    if(bgpm)
	      {
		int bgx, bgy;

		bgx = (x - GDK_DRAWABLE_FBDATA(bg_relto)->abs_x) % GDK_DRAWABLE_P(bgpm)->width;
		bgy = (y - GDK_DRAWABLE_FBDATA(bg_relto)->abs_y) % GDK_DRAWABLE_P(bgpm)->height;

		gdk_fb_drawable_get_pixel(bgpm, gc, bgx, bgy, spot, FALSE, NULL, NULL);
		retval = GPR_USED_BG;
	      }
	    else
	      {
		*spot = GDK_GC_FBDATA(gc)->values.background;
	      }
	  }
      }
      break;
    case 71:
      if(mem[x + y * rowstride])
	*spot = GDK_GC_FBDATA(gc)->values.foreground;
      else
	*spot = GDK_GC_FBDATA(gc)->values.background;
      break;
    case 77:
      retval = GPR_AA_GRAYVAL;
      spot->pixel = mem[x + y * rowstride] << 1;
      spot->red = spot->green = spot->blue = spot->pixel << 8;
      break;
    case 78: /* AA mode */
      retval = GPR_AA_GRAYVAL;
      spot->pixel = mem[x + y * rowstride];
      spot->red = spot->green = spot->blue = spot->pixel << 8;
      break;
    case 8:
      spot->pixel = mem[x + y * rowstride];
      *spot = GDK_DRAWABLE_P(drawable)->colormap->colors[spot->pixel];
      break;
    case 16:
      {
	guint16 val16 = *((guint16 *)&mem[x*2 + y*rowstride]);

	spot->red = (((1<<gdk_display->modeinfo.red.length) - 1) & (val16 >> gdk_display->modeinfo.red.offset)) << (16 - gdk_display->modeinfo.red.length);
	spot->green = (((1<<gdk_display->modeinfo.green.length) - 1) & (val16 >> gdk_display->modeinfo.green.offset)) << (16 - gdk_display->modeinfo.green.length);
	spot->blue = (((1<<gdk_display->modeinfo.blue.length) - 1) & (val16 >> gdk_display->modeinfo.blue.offset)) << (16 - gdk_display->modeinfo.blue.length);

	spot->pixel = val16;
      }
      break;
    case 24:
      {
	guchar *smem = &mem[x*3 + y*rowstride];
	spot->red = smem[gdk_display->red_byte] << 8;
	spot->green = smem[gdk_display->green_byte] << 8;
	spot->blue = smem[gdk_display->blue_byte] << 8;
#if (__BYTE_ORDER == __BIG_ENDIAN)
	spot->pixel = (smem[0]<<16)|(smem[1]<<8)|smem[2];
#else
	spot->pixel = smem[0]|(smem[1]<<8)|(smem[2]<<16);
#endif
      }
      break;
    case 32:
      {
	guchar *smem = &mem[x*4 + y*rowstride];
	spot->red = smem[gdk_display->red_byte] << 8;
	spot->green = smem[gdk_display->green_byte] << 8;
	spot->blue = smem[gdk_display->blue_byte] << 8;
	spot->pixel = *(guint32 *)smem;
      }
      break;
    }

  return retval;
}

static void
gdk_fb_drawable_set_pixel(GdkDrawable *drawable, GdkGC *gc, int x, int y, GdkColor *spot,
			  gboolean abs_coords)
{
  guchar *mem = GDK_DRAWABLE_FBDATA(drawable)->mem;
  guint rowstride = GDK_DRAWABLE_FBDATA(drawable)->rowstride;

  if(!abs_coords)
    {
      x += GDK_DRAWABLE_FBDATA(drawable)->abs_x;
      y += GDK_DRAWABLE_FBDATA(drawable)->abs_y;
    }

  switch(GDK_DRAWABLE_P(drawable)->depth)
    {
    case 1:
      {
	guchar *foo = mem + (y*rowstride) + (x >> 3);

	if(spot->pixel)
	  *foo |= (1 << (x % 8));
	else
	  *foo &= ~(1 << (x % 8));
      }
      break;
    case 8:
      mem[x + y*rowstride] = spot->pixel;
      break;
    case 16:
      {
	guint16 *p16 = (guint16 *)&mem[x*2 + y*rowstride];
	*p16 = spot->pixel;
      }
      break;
    case 24:
      {
	guchar *smem = &mem[x*3 + y*rowstride];
	smem[gdk_display->red_byte] = spot->red >> 8;
	smem[gdk_display->green_byte] = spot->green >> 8;
	smem[gdk_display->blue_byte] = spot->blue >> 8;
      };
      break;
    case 32:
      {
	guint32 *smem = (guint32 *)&mem[x*4 + y*rowstride];
	*smem = spot->pixel;
      }
      break;
    default:
      g_assert_not_reached();
      break;
    }

}

void
gdk_fb_drawing_context_init(GdkFBDrawingContext *dc,
			    GdkDrawable *drawable,
			    GdkGC *gc,
			    gboolean draw_bg,
			    gboolean do_clipping)
{
  dc->mem = GDK_DRAWABLE_FBDATA(drawable)->mem;
  dc->rowstride = GDK_DRAWABLE_FBDATA(drawable)->rowstride;
  dc->handle_cursor = FALSE;
  dc->bgpm = NULL;
  dc->bg_relto = drawable;
  dc->draw_bg = draw_bg;

  if(GDK_IS_WINDOW(drawable))
    {
      dc->bgpm = GDK_WINDOW_P(drawable)->bg_pixmap;
      if(dc->bgpm == GDK_PARENT_RELATIVE_BG)
	{
	  for(; dc->bgpm == GDK_PARENT_RELATIVE_BG && dc->bg_relto; dc->bg_relto = GDK_WINDOW_P(dc->bg_relto)->parent)
	    dc->bgpm = GDK_WINDOW_P(dc->bg_relto)->bg_pixmap;
	}

      if(dc->bgpm == GDK_NO_BG)
	dc->bgpm = NULL;
    }
  dc->clipxoff = - GDK_DRAWABLE_FBDATA(drawable)->abs_x;
  dc->clipyoff = - GDK_DRAWABLE_FBDATA(drawable)->abs_y;

  dc->real_clip_region = gdk_fb_clip_region(drawable, gc, do_clipping);

  if(gc)
    {
      dc->clipxoff -= GDK_GC_FBDATA(gc)->values.clip_x_origin;
      dc->clipyoff -= GDK_GC_FBDATA(gc)->values.clip_y_origin;

      if(GDK_GC_FBDATA(gc)->values.clip_mask)
	{
	  dc->clipmem = GDK_DRAWABLE_FBDATA(GDK_GC_FBDATA(gc)->values.clip_mask)->mem;
	  dc->clip_rowstride = GDK_DRAWABLE_FBDATA(GDK_GC_FBDATA(gc)->values.clip_mask)->rowstride;
	}
    }

  if(do_clipping)
    {
      GdkRectangle cursor_rect;

      gdk_fb_get_cursor_rect(&cursor_rect);

      if(GDK_DRAWABLE_FBDATA(drawable)->mem == GDK_DRAWABLE_FBDATA(gdk_parent_root)->mem
	 && cursor_rect.x >= 0
	 && gdk_region_rect_in(dc->real_clip_region, &cursor_rect) != GDK_OVERLAP_RECTANGLE_OUT)
	{
	  dc->handle_cursor = TRUE;
	  gdk_fb_cursor_hide();
	}
    }
}

void
gdk_fb_drawing_context_finalize(GdkFBDrawingContext *dc)
{
  gdk_region_destroy(dc->real_clip_region);

  if(dc->handle_cursor)
    gdk_fb_cursor_unhide();
}

Elliot Lee's avatar
Elliot Lee committed
766 767 768 769 770 771 772 773 774 775 776 777 778
void
gdk_fb_draw_drawable_2 (GdkDrawable *drawable,
			GdkGC       *gc,
			GdkPixmap   *src,
			gint         xsrc,
			gint         ysrc,
			gint         xdest,
			gint         ydest,
			gint         width,
			gint         height,
			gboolean     draw_bg,
			gboolean     do_clipping)
{
Elliot Lee's avatar
Elliot Lee committed
779 780
  GdkFBDrawingContext *dc, dc_data;
  dc = &dc_data;
Elliot Lee's avatar
Elliot Lee committed
781

Elliot Lee's avatar
Elliot Lee committed
782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
  gdk_fb_drawing_context_init(dc, drawable, gc, draw_bg, do_clipping);
  gdk_fb_draw_drawable_3(drawable, gc, src, dc, xsrc, ysrc, xdest, ydest, width, height);
  gdk_fb_drawing_context_finalize(dc);
}

void
gdk_fb_draw_drawable_3 (GdkDrawable *drawable,
			GdkGC       *gc,
			GdkPixmap   *src,
			GdkFBDrawingContext *dc,
			gint         xsrc,
			gint         ysrc,
			gint         xdest,
			gint         ydest,
			gint         width,
			gint         height)
{
  GdkRectangle rect;
  guchar *srcmem = GDK_DRAWABLE_FBDATA(src)->mem;
  int src_x_off, src_y_off;
  GdkRegion *tmpreg, *real_clip_region;
  int i;
Elliot Lee's avatar
Elliot Lee committed
804 805 806

  if(GDK_IS_WINDOW(drawable))
    {
Elliot Lee's avatar
Elliot Lee committed
807 808 809 810
      if(!GDK_WINDOW_P(drawable)->mapped)
	return;
      if(GDK_WINDOW_P(drawable)->input_only)
	g_error("Drawing on the evil input-only!");
Elliot Lee's avatar
Elliot Lee committed
811 812 813 814 815
    }

  if(drawable == src)
    {
      GdkDrawableFBData *fbd = GDK_DRAWABLE_FBDATA(src);
Elliot Lee's avatar
Elliot Lee committed
816

Elliot Lee's avatar
Elliot Lee committed
817 818
      /* One lame hack deserves another ;-) */
      srcmem = g_alloca(fbd->rowstride * fbd->lim_y);
Elliot Lee's avatar
Elliot Lee committed
819
      memcpy(srcmem, dc->mem, fbd->rowstride * fbd->lim_y);
Elliot Lee's avatar
Elliot Lee committed
820 821
    }

Elliot Lee's avatar
Elliot Lee committed
822 823 824
  /* Do some magic to avoid creating extra regions unnecessarily */
  tmpreg = dc->real_clip_region;

Elliot Lee's avatar
Elliot Lee committed
825 826 827 828
  rect.x = xdest + GDK_DRAWABLE_FBDATA(drawable)->abs_x;
  rect.y = ydest + GDK_DRAWABLE_FBDATA(drawable)->abs_y;
  rect.width = width;
  rect.height = height;
Elliot Lee's avatar
Elliot Lee committed
829
  real_clip_region = gdk_region_rectangle(&rect);
Elliot Lee's avatar
Elliot Lee committed
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
  gdk_region_intersect(real_clip_region, tmpreg);

  rect.x = xdest + GDK_DRAWABLE_FBDATA(drawable)->abs_x;
  rect.y = ydest + GDK_DRAWABLE_FBDATA(drawable)->abs_y;
  rect.width = MAX(GDK_DRAWABLE_P(src)->width - xsrc, 0);
  rect.height = MAX(GDK_DRAWABLE_P(src)->height - ysrc, 0);
  if(!rect.width || !rect.height)
    goto out;
  tmpreg = gdk_region_rectangle(&rect);
  gdk_region_intersect(real_clip_region, tmpreg);
  gdk_region_destroy(tmpreg);

  src_x_off = (GDK_DRAWABLE_FBDATA(src)->abs_x + xsrc) - (GDK_DRAWABLE_FBDATA(drawable)->abs_x + xdest);
  src_y_off = (GDK_DRAWABLE_FBDATA(src)->abs_y + ysrc) - (GDK_DRAWABLE_FBDATA(drawable)->abs_y + ydest);

  for(i = 0; i < real_clip_region->numRects; i++)
    {
      GdkRegionBox *cur = &real_clip_region->rects[i];
      int cur_y;

      if(GDK_DRAWABLE_P(src)->depth == GDK_DRAWABLE_P(drawable)->depth
Elliot Lee's avatar
Elliot Lee committed
851 852
	 && GDK_DRAWABLE_P(src)->depth >= 8
	 && GDK_DRAWABLE_P(src)->depth <= 32
Elliot Lee's avatar
Elliot Lee committed
853 854 855
	 && (!gc || !GDK_GC_FBDATA(gc)->values.clip_mask))
	{
	  guint depth = GDK_DRAWABLE_P(src)->depth;
Elliot Lee's avatar
Elliot Lee committed
856 857
	  guint src_rowstride = GDK_DRAWABLE_FBDATA(src)->rowstride;
	  int linelen = (cur->x2 - cur->x1)*(depth>>3);
Elliot Lee's avatar
Elliot Lee committed
858 859 860

	  for(cur_y = cur->y1; cur_y < cur->y2; cur_y++)
	    {
Elliot Lee's avatar
Elliot Lee committed
861
	      memcpy(dc->mem + (cur_y * dc->rowstride) + cur->x1*(depth>>3),
Elliot Lee's avatar
Elliot Lee committed
862
		     srcmem + ((cur_y + src_y_off)*src_rowstride) + (cur->x1 + src_x_off)*(depth>>3),
Elliot Lee's avatar
Elliot Lee committed
863
		     linelen);
Elliot Lee's avatar
Elliot Lee committed
864 865 866 867 868 869 870 871 872 873
	    }
	}
      else
	{
	  int cur_x;

	  for(cur_y = cur->y1; cur_y < cur->y2; cur_y++)
	    {
	      for(cur_x = cur->x1; cur_x < cur->x2; cur_x++)
		{
Elliot Lee's avatar
Elliot Lee committed
874
		  GdkColor spot;
Elliot Lee's avatar
Elliot Lee committed
875 876 877

		  if(gc && GDK_GC_FBDATA(gc)->values.clip_mask)
		    {
Elliot Lee's avatar
Elliot Lee committed
878
		      int maskx = cur_x+dc->clipxoff, masky = cur_y + dc->clipyoff;
Elliot Lee's avatar
Elliot Lee committed
879 880
		      guchar foo;

Elliot Lee's avatar
Elliot Lee committed
881
		      foo = dc->clipmem[masky*dc->clip_rowstride + (maskx >> 3)];
Elliot Lee's avatar
Elliot Lee committed
882 883 884 885 886

		      if(!(foo & (1 << (maskx % 8))))
			continue;
		    }

Elliot Lee's avatar
Elliot Lee committed
887
		  switch(gdk_fb_drawable_get_pixel(src, gc, cur_x + src_x_off, cur_y + src_y_off, &spot, TRUE, NULL, NULL))
Elliot Lee's avatar
Elliot Lee committed
888
		    {
Elliot Lee's avatar
Elliot Lee committed
889
		    case GPR_AA_GRAYVAL:
Elliot Lee's avatar
Elliot Lee committed
890
		      {
Elliot Lee's avatar
Elliot Lee committed
891 892 893 894 895 896 897 898 899 900 901 902
			GdkColor realspot, fg;
			guint graylevel = spot.pixel;
			gint tmp;

			if(GDK_DRAWABLE_P(drawable)->depth == 1)
			  {
			    if(spot.pixel > 192)
			      spot = GDK_GC_FBDATA(gc)->values.foreground;
			    else
			     spot = GDK_GC_FBDATA(gc)->values.background;
			    break;
			  }
Elliot Lee's avatar
Elliot Lee committed
903
			else
Elliot Lee's avatar
Elliot Lee committed
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
			  {
			    if(graylevel >= 254)
			      {
				spot = GDK_GC_FBDATA(gc)->values.foreground;
			      }
			    else if(graylevel <= 2)
			      {
				if(!dc->draw_bg)
				  continue;

				spot = GDK_GC_FBDATA(gc)->values.background;
			      }
			    else
			      {
				switch(gdk_fb_drawable_get_pixel(drawable, gc, cur_x, cur_y, &realspot, TRUE, dc->bg_relto, dc->bgpm))
				  {
				  case GPR_NONE:
				  case GPR_USED_BG:
				    break;
				  default:
				    g_assert_not_reached();
				    break;
				  }

				fg = GDK_GC_FBDATA(gc)->values.foreground;
				/* Now figure out what 'spot' should actually look like */
				fg.red >>= 8;
				fg.green >>= 8;
				fg.blue >>= 8;
				realspot.red >>= 8;
				realspot.green >>= 8;
				realspot.blue >>= 8;

				tmp = (fg.red - realspot.red) * graylevel;
				spot.red = realspot.red + ((tmp + (tmp >> 8) + 0x80) >> 8);
				spot.red <<= 8;

				tmp = (fg.green - realspot.green) * graylevel;
				spot.green = realspot.green + ((tmp + (tmp >> 8) + 0x80) >> 8);
				spot.green <<= 8;

				tmp = (fg.blue - realspot.blue) * graylevel;
				spot.blue = realspot.blue + ((tmp + (tmp >> 8) + 0x80) >> 8);
				spot.blue <<= 8;

				/* Now find the pixel for this thingie */
				switch(GDK_DRAWABLE_P(drawable)->depth)
				  {
				  case 8:
				    if(!gdk_colormap_alloc_color(GDK_DRAWABLE_P(drawable)->colormap, &spot, FALSE, TRUE))
				      {
					g_error("Can't allocate AA color!");
				      }
				    break;
				  case 16:
				    spot.pixel = (spot.red >> (16 - gdk_display->modeinfo.red.length)) << gdk_display->modeinfo.red.offset;
				    spot.pixel |= (spot.green >> (16 - gdk_display->modeinfo.green.length)) << gdk_display->modeinfo.green.offset;
				    spot.pixel |= (spot.blue >> (16 - gdk_display->modeinfo.blue.length)) << gdk_display->modeinfo.blue.offset;
				    break;
				  case 24:
				  case 32:
				    spot.pixel = ((spot.red & 0xFF00) << (gdk_display->modeinfo.red.offset - 8))
				      | ((spot.green & 0xFF00) << (gdk_display->modeinfo.green.offset - 8))
				      | ((spot.blue & 0xFF00) << (gdk_display->modeinfo.blue.offset - 8));
				    break;
				  }
			      }
			  }
Elliot Lee's avatar
Elliot Lee committed
972 973
		      }
		      break;
Elliot Lee's avatar
Elliot Lee committed
974 975 976
		    case GPR_USED_BG:
		      if(!dc->draw_bg)
			continue;
Elliot Lee's avatar
Elliot Lee committed
977
		      break;
Elliot Lee's avatar
Elliot Lee committed
978
		    case GPR_NONE:
Elliot Lee's avatar
Elliot Lee committed
979 980 981 982 983
		      break;
		    default:
		      g_assert_not_reached();
		      break;
		    }
Elliot Lee's avatar
Elliot Lee committed
984 985

		  gdk_fb_drawable_set_pixel(drawable, gc, cur_x, cur_y, &spot, GDK_DRAWABLE_P(src)->depth);
Elliot Lee's avatar
Elliot Lee committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
		}
	    }
	}
    }

 out:
  gdk_region_destroy(real_clip_region);
}

void
gdk_fb_draw_drawable (GdkDrawable *drawable,
		      GdkGC       *gc,
		      GdkPixmap   *src,
		      gint         xsrc,
		      gint         ysrc,
		      gint         xdest,
		      gint         ydest,
		      gint         width,
		      gint         height)
{
  gdk_fb_draw_drawable_2(drawable, gc, src, xsrc, ysrc, xdest, ydest, width, height, TRUE, TRUE);
}

static void
gdk_fb_draw_text(GdkDrawable    *drawable,
		 GdkFont        *font,
		 GdkGC          *gc,
		 gint            x,
		 gint            y,
		 const gchar    *text,
		 gint            text_length)
{
Elliot Lee's avatar
Elliot Lee committed
1018
  g_warning("gdk_fb_draw_text NYI");
Elliot Lee's avatar
Elliot Lee committed
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
}

static void
gdk_fb_draw_text_wc (GdkDrawable    *drawable,
		     GdkFont	     *font,
		     GdkGC	     *gc,
		     gint	      x,
		     gint	      y,
		     const GdkWChar *text,
		     gint	      text_length)
{
Elliot Lee's avatar
Elliot Lee committed
1030
  g_warning("gdk_fb_draw_text_wc NYI");
Elliot Lee's avatar
Elliot Lee committed
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
}

void
gdk_fb_draw_rectangle (GdkDrawable    *drawable,
		       GdkGC          *gc,
		       gint            filled,
		       gint            x,
		       gint            y,
		       gint            width,
		       gint            height)
{
  GdkRectangle rect;

  if(filled)
    {
      rect.x = x;
      rect.y = y;
      rect.width = width;
      rect.height = height;
      gdk_fb_fill_spans(drawable, gc, &rect, 1);
    }
  else
    {
      GdkPoint pts[5];
      pts[0].x = pts[4].x = x;
      pts[0].y = pts[4].y = y;
      pts[1].x = x + width;
      pts[1].y = y;
      pts[2].x = x + width;
      pts[2].y = y + height;
      pts[3].x = x;
      pts[3].y = y + height;
      gdk_fb_draw_lines(drawable, gc, pts, 5);
    }
}

static void gdk_fb_draw_points    (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkPoint       *points,
				   gint            npoints)
{
Elliot Lee's avatar
Elliot Lee committed
1072
  GdkRectangle *rects = g_alloca(npoints * sizeof(GdkRectangle));
Elliot Lee's avatar
Elliot Lee committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
  int i;

  for(i = 0; i < npoints; i++)
    {
      rects[i].x = points[i].x;
      rects[i].y = points[i].y;
      rects[i].width = rects[i].height = 1;
    }

  gdk_fb_fill_spans(drawable, gc, rects, npoints);
}

static void gdk_fb_draw_arc       (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   gint            filled,
				   gint            x,
				   gint            y,
				   gint            width,
				   gint            height,
				   gint            angle1,
				   gint            angle2)
{
  miArc arc;

  arc.x = x;
  arc.y = y;
  arc.width = width;
  arc.height = height;
  arc.angle1 = angle1;
  arc.angle2 = angle2;

  if(filled)
    miPolyFillArc(drawable, gc, 1, &arc);
  else
    miPolyArc(drawable, gc, 1, &arc);
}

static void gdk_fb_draw_polygon   (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   gint            filled,
				   GdkPoint       *points,
				   gint            npoints)
{
  if(filled)
    miFillPolygon(drawable, gc, 0, 0, npoints, points);
  else
    {
Elliot Lee's avatar
Elliot Lee committed
1120
      GdkPoint *realpts = g_alloca(sizeof(GdkPoint) * (npoints + 1));
Elliot Lee's avatar
Elliot Lee committed
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

      memcpy(realpts, points, sizeof(GdkPoint) * npoints);
      realpts[npoints] = points[0];
      gdk_fb_draw_lines(drawable, gc, points, npoints);
    }
}

static void gdk_fb_draw_lines     (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkPoint       *points,
				   gint            npoints)
{
  if(GDK_GC_FBDATA(gc)->values.line_width > 0)
    miWideLine(drawable, gc, 0, npoints, points);
  else
    miZeroLine(drawable, gc, 0, npoints, points);
}

static void gdk_fb_draw_segments  (GdkDrawable    *drawable,
				   GdkGC          *gc,
				   GdkSegment     *segs,
				   gint            nsegs)
{
  GdkPoint pts[2];
  int i;

  for(i = 0; i < nsegs; i++)
    {
      pts[0].x = segs[i].x1;
      pts[0].y = segs[i].y1;
      pts[1].x = segs[i].x2;
      pts[1].y = segs[i].y2;

      gdk_fb_draw_lines(drawable, gc, pts, 2);
    }
}

void
gdk_fb_drawable_clear(GdkDrawable *d)
{
Elliot Lee's avatar
Elliot Lee committed
1161 1162 1163 1164 1165 1166 1167
  extern void
    _gdk_windowing_window_clear_area (GdkWindow *window,
				      gint       x,
				      gint       y,
				      gint       width,
				      gint       height);

Elliot Lee's avatar
Elliot Lee committed
1168 1169
  _gdk_windowing_window_clear_area(d, 0, 0, GDK_DRAWABLE_P(d)->width, GDK_DRAWABLE_P(d)->height);
}
Elliot Lee's avatar
Elliot Lee committed
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269

extern FT_Library gdk_fb_ft_lib;

extern void pango_fb_font_set_size(PangoFont *font);

static void gdk_fb_draw_glyphs(GdkDrawable      *drawable,
			       GdkGC	        *gc,
			       PangoFont        *font,
			       gint              x,
			       gint              y,
			       PangoGlyphString *glyphs)
{
  int i;
  GdkPixmapFBData fbd;
  GdkDrawablePrivate tmp_foo;
  FT_Face ftf;
  int xpos;
  GdkFBDrawingContext fbdc;

  g_return_if_fail(font);

  gdk_fb_drawing_context_init(&fbdc, drawable, gc, FALSE, TRUE);

  ftf = PANGO_FB_FONT(font)->ftf;

  /* Fake its existence as a pixmap */
  memset(&tmp_foo, 0, sizeof(tmp_foo));
  memset(&fbd, 0, sizeof(fbd));
  tmp_foo.klass = &_gdk_fb_drawable_class;
  tmp_foo.klass_data = &fbd;
  tmp_foo.window_type = GDK_DRAWABLE_PIXMAP;

  pango_fb_font_set_size(font);
  for(i = xpos = 0; i < glyphs->num_glyphs; i++)
    {
      FT_GlyphSlot g;
      FT_Bitmap *renderme;
      int this_wid;

      FT_Load_Glyph(ftf, glyphs->glyphs[i].glyph, FT_LOAD_DEFAULT);
      g = ftf->glyph;

      if(g->format != ft_glyph_format_bitmap)
	{
	  FT_BitmapGlyph bgy;
	  int bdepth;
#ifdef USE_AA
#ifdef USE_FTGRAYS
	  bdepth = 256;
#else
	  bdepth = 128;
#endif
#else
	  bdepth = 0;
#endif

	  if(FT_Get_Glyph_Bitmap(ftf, glyphs->glyphs[i].glyph, 0, bdepth, NULL, &bgy))
	    continue;

	  renderme = &bgy->bitmap;
	}
      else
	renderme = &g->bitmap;

      fbd.drawable_data.mem = renderme->buffer;
      fbd.drawable_data.rowstride = renderme->pitch;
      tmp_foo.width = fbd.drawable_data.lim_x = renderme->width;
      tmp_foo.height = fbd.drawable_data.lim_y = renderme->rows;

      switch(renderme->pixel_mode)
	{
	case ft_pixel_mode_mono:
	  tmp_foo.depth = 1;
	  break;
	case ft_pixel_mode_grays:
#if defined(USE_FTGRAYS)
	  tmp_foo.depth = 78;
#else
	  tmp_foo.depth = 77;
#endif
	  break;
	default:
	  g_assert_not_reached();
	  break;
	}

      this_wid = (xpos + glyphs->glyphs[i].geometry.width)/PANGO_SCALE;
      gdk_fb_draw_drawable_3(drawable, gc, (GdkDrawable *)&tmp_foo,
			     &fbdc,
			     0, 0,
			     x + (xpos + glyphs->glyphs[i].geometry.x_offset)/PANGO_SCALE,
			     y + glyphs->glyphs[i].geometry.y_offset / PANGO_SCALE
			     + ((-ftf->glyph->metrics.horiBearingY) >> 6),
			     this_wid, renderme->rows);

      xpos += glyphs->glyphs[i].geometry.width;
    }

  gdk_fb_drawing_context_finalize(&fbdc);
}