+static gint util_clip_line(gdouble clip_x, gdouble clip_y, gdouble clip_w, gdouble clip_h,
+ gdouble x1, gdouble y1, gdouble x2, gdouble y2,
+ gdouble *rx1, gdouble *ry1, gdouble *rx2, gdouble *ry2)
+{
+ gint flip = FALSE;
+ gdouble d;
+
+ if (x1 > x2)
+ {
+ gdouble t;
+
+ t = x1;
+ x1 = x2;
+ x2 = t;
+
+ t = y1;
+ y1 = y2;
+ y2 = t;
+
+ flip = TRUE;
+ }
+
+ if (x2 < clip_x || x1 > clip_x + clip_w) return FALSE;
+
+ if (y1 < y2)
+ {
+ if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
+ }
+ else
+ {
+ if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
+ }
+
+#if 0
+ if (x1 >= clip_x && x2 <= clip_x + clip_w)
+ {
+ if (y1 < y2)
+ {
+ if (y1 >= clip_y && y2 <= clip_y + clip_h) return TRUE;
+ }
+ else
+ {
+ if (y2 >= clip_y && y1 <= clip_y + clip_h) return TRUE;
+ }
+ }
+#endif
+
+ d = x2 - x1;
+ if (d > 0.0)
+ {
+ gdouble slope;
+
+ slope = (y2 - y1) / d;
+ if (x1 < clip_x)
+ {
+ y1 = y1 + slope * (clip_x - x1);
+ x1 = clip_x;
+ }
+ if (x2 > clip_x + clip_w)
+ {
+ y2 = y2 + slope * (clip_x + clip_w - x2);
+ x2 = clip_x + clip_w;
+ }
+ }
+
+ if (y1 < y2)
+ {
+ if (y2 < clip_y || y1 > clip_y + clip_h) return FALSE;
+ }
+ else
+ {
+ gdouble t;
+
+ if (y1 < clip_y || y2 > clip_y + clip_h) return FALSE;
+
+ t = x1;
+ x1 = x2;
+ x2 = t;
+
+ t = y1;
+ y1 = y2;
+ y2 = t;
+
+ flip = !flip;
+ }
+
+ d = y2 - y1;
+ if (d > 0.0)
+ {
+ gdouble slope;
+
+ slope = (x2 - x1) / d;
+ if (y1 < clip_y)
+ {
+ x1 = x1 + slope * (clip_y - y1);
+ y1 = clip_y;
+ }
+ if (y2 > clip_y + clip_h)
+ {
+ x2 = x2 + slope * (clip_y + clip_h - y2);
+ y2 = clip_y + clip_h;
+ }
+ }
+
+ if (flip)
+ {
+ *rx1 = x2;
+ *ry1 = y2;
+ *rx2 = x1;
+ *ry2 = y1;
+ }
+ else
+ {
+ *rx1 = x1;
+ *ry1 = y1;
+ *rx2 = x2;
+ *ry2 = y2;
+ }
+
+ return TRUE;
+}
+