Ejemplo n.º 1
0
static GtkCssImage *
gtk_css_image_builtin_compute (GtkCssImage             *image,
                               guint                    property_id,
                               GtkStyleProviderPrivate *provider,
                               GtkCssStyle             *style,
                               GtkCssStyle             *parent_style)
{
  GtkCssImageBuiltin *result;
  GtkBorderStyle border_style;

  result = g_object_new (GTK_TYPE_CSS_IMAGE_BUILTIN, NULL);

  border_style = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
  if (border_style == GTK_BORDER_STYLE_SOLID)
    {
      GtkBorder border;

      border.top = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100);
      border.right = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100);
      border.bottom = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH), 100);
      border.left = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH), 100);

      result->border_width = MIN (MIN (border.top, border.bottom),
                                  MIN (border.left, border.right));
    }

  result->fg_color = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_COLOR));
  result->bg_color = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
  result->border_color = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_COLOR));

  return GTK_CSS_IMAGE (result);
}
Ejemplo n.º 2
0
void
gtk_css_style_render_border (GtkCssStyle      *style,
                             cairo_t          *cr,
                             gdouble           x,
                             gdouble           y,
                             gdouble           width,
                             gdouble           height,
                             guint             hidden_side,
                             GtkJunctionSides  junction)
{
  GtkBorderImage border_image;
  double border_width[4];

  border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_WIDTH), 100);
  border_width[1] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_WIDTH), 100);
  border_width[2] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_WIDTH), 100);
  border_width[3] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_WIDTH), 100);

  if (gtk_border_image_init (&border_image, style))
    {
      gtk_border_image_render (&border_image, border_width, cr, x, y, width, height);
    }
  else
    {
      GtkBorderStyle border_style[4];
      GtkRoundedBox border_box;
      GdkRGBA colors[4];

      /* Optimize the most common case of "This widget has no border" */
      if (border_width[0] == 0 &&
          border_width[1] == 0 &&
          border_width[2] == 0 &&
          border_width[3] == 0)
        return;

      border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_STYLE));
      border_style[1] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_STYLE));
      border_style[2] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_STYLE));
      border_style[3] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_STYLE));

      hide_border_sides (border_width, border_style, hidden_side);

      colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_TOP_COLOR));
      colors[1] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_RIGHT_COLOR));
      colors[2] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_BOTTOM_COLOR));
      colors[3] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_BORDER_LEFT_COLOR));

      _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
      _gtk_rounded_box_apply_border_radius_for_style (&border_box, style, junction);

      render_border (cr, &border_box, border_width, hidden_side, colors, border_style);
    }
}
Ejemplo n.º 3
0
void
gtk_css_style_render_outline (GtkCssStyle *style,
                              cairo_t     *cr,
                              gdouble      x,
                              gdouble      y,
                              gdouble      width,
                              gdouble      height)
{
  GtkBorderStyle border_style[4];
  GtkRoundedBox border_box;
  double border_width[4];
  GdkRGBA colors[4];

  border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_STYLE));
  if (border_style[0] != GTK_BORDER_STYLE_NONE)
    {
      cairo_rectangle_t rect;

      compute_outline_rect (style, x, y, width, height, &rect);

      border_style[1] = border_style[2] = border_style[3] = border_style[0];
      border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_WIDTH), 100);
      border_width[3] = border_width[2] = border_width[1] = border_width[0];
      colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_COLOR));
      colors[3] = colors[2] = colors[1] = colors[0];

      _gtk_rounded_box_init_rect (&border_box, rect.x, rect.y, rect.width, rect.height);
      _gtk_rounded_box_apply_outline_radius_for_style (&border_box, style, GTK_JUNCTION_NONE);

      render_border (cr, &border_box, border_width, 0, colors, border_style);
    }
}
Ejemplo n.º 4
0
/**
 * gtk_symbolic_color_resolve:
 * @color: a #GtkSymbolicColor
 * @props: (allow-none): #GtkStyleProperties to use when resolving
 *    named colors, or %NULL
 * @resolved_color: (out): return location for the resolved color
 *
 * If @color is resolvable, @resolved_color will be filled in
 * with the resolved color, and %TRUE will be returned. Generally,
 * if @color can't be resolved, it is due to it being defined on
 * top of a named color that doesn't exist in @props.
 *
 * When @props is %NULL, resolving of named colors will fail, so if
 * your @color is or references such a color, this function will
 * return %FALSE.
 *
 * Returns: %TRUE if the color has been resolved
 *
 * Since: 3.0
 *
 * Deprecated: 3.8: #GtkSymbolicColor is deprecated.
 **/
gboolean
gtk_symbolic_color_resolve (GtkSymbolicColor   *color,
			    GtkStyleProperties *props,
			    GdkRGBA            *resolved_color)
{
  GdkRGBA pink = { 1.0, 0.5, 0.5, 1.0 };
  GtkCssValue *v, *current;

  g_return_val_if_fail (color != NULL, FALSE);
  g_return_val_if_fail (resolved_color != NULL, FALSE);
  g_return_val_if_fail (props == NULL || GTK_IS_STYLE_PROPERTIES (props), FALSE);

  current = _gtk_css_rgba_value_new_from_rgba (&pink);
  v = _gtk_css_color_value_resolve (color->value,
                                    GTK_STYLE_PROVIDER_PRIVATE (props),
                                    current,
                                    0,
                                    NULL);
  _gtk_css_value_unref (current);
  if (v == NULL)
    return FALSE;

  *resolved_color = *_gtk_css_rgba_value_get_rgba (v);
  _gtk_css_value_unref (v);
  return TRUE;
}
Ejemplo n.º 5
0
Archivo: gtkrender.c Proyecto: Vort/gtk
static void
gtk_do_render_line (GtkStyleContext *context,
                    cairo_t         *cr,
                    gdouble          x0,
                    gdouble          y0,
                    gdouble          x1,
                    gdouble          y1)
{
  const GdkRGBA *color;

  cairo_save (cr);

  color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));

  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);
  cairo_set_line_width (cr, 1);

  cairo_move_to (cr, x0 + 0.5, y0 + 0.5);
  cairo_line_to (cr, x1 + 0.5, y1 + 0.5);

  gdk_cairo_set_source_rgba (cr, color);
  cairo_stroke (cr);

  cairo_restore (cr);
}
Ejemplo n.º 6
0
static GtkCssImage *
gtk_css_image_icon_theme_compute (GtkCssImage             *image,
                                  guint                    property_id,
                                  GtkStyleProviderPrivate *provider,
                                  int                      scale,
                                  GtkCssComputedValues    *values,
                                  GtkCssComputedValues    *parent_values,
                                  GtkCssDependencies      *dependencies)
{
  GtkCssImageIconTheme *icon_theme = GTK_CSS_IMAGE_ICON_THEME (image);
  GtkCssImageIconTheme *copy;
  GtkSettings *settings;
  GdkScreen *screen;

  settings = _gtk_style_provider_private_get_settings (provider);
  if (settings == NULL)
    screen = gdk_screen_get_default ();
  else
    screen = _gtk_settings_get_screen (settings);

  copy = g_object_new (GTK_TYPE_CSS_IMAGE_ICON_THEME, NULL);
  copy->name = g_strdup (icon_theme->name);
  copy->icon_theme = gtk_icon_theme_get_for_screen (screen);
  copy->scale = scale;
  copy->color = *_gtk_css_rgba_value_get_rgba (_gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_COLOR));

  *dependencies = GTK_CSS_DEPENDS_ON_COLOR;

  return GTK_CSS_IMAGE (copy);
}
Ejemplo n.º 7
0
cairo_pattern_t *
_gtk_gradient_resolve_full (GtkGradient             *gradient,
                            GtkStyleProviderPrivate *provider,
                            GtkCssComputedValues    *values,
                            GtkCssComputedValues    *parent_values,
                            GtkCssDependencies      *dependencies)
{
  cairo_pattern_t *pattern;
  guint i;

  g_return_val_if_fail (gradient != NULL, NULL);
  g_return_val_if_fail (GTK_IS_STYLE_PROVIDER (provider), NULL);
  g_return_val_if_fail (GTK_IS_CSS_COMPUTED_VALUES (values), NULL);
  g_return_val_if_fail (parent_values == NULL || GTK_IS_CSS_COMPUTED_VALUES (parent_values), NULL);
  g_return_val_if_fail (*dependencies == 0, NULL);

  if (gradient->radius0 == 0 && gradient->radius1 == 0)
    pattern = cairo_pattern_create_linear (gradient->x0, gradient->y0,
                                           gradient->x1, gradient->y1);
  else
    pattern = cairo_pattern_create_radial (gradient->x0, gradient->y0,
                                           gradient->radius0,
                                           gradient->x1, gradient->y1,
                                           gradient->radius1);

  for (i = 0; i < gradient->stops->len; i++)
    {
      ColorStop *stop;
      GtkCssValue *val;
      GdkRGBA rgba;
      GtkCssDependencies stop_deps;

      stop = &g_array_index (gradient->stops, ColorStop, i);

      /* if color resolving fails, assume transparency */
      val = _gtk_css_color_value_resolve (_gtk_symbolic_color_get_css_value (stop->color),
                                          provider,
                                          _gtk_css_computed_values_get_value (values, GTK_CSS_PROPERTY_COLOR),
                                          GTK_CSS_DEPENDS_ON_COLOR,
                                          &stop_deps,
                                          NULL);
      if (val)
        {
          rgba = *_gtk_css_rgba_value_get_rgba (val);
          *dependencies = _gtk_css_dependencies_union (*dependencies, stop_deps);
          _gtk_css_value_unref (val);
        }
      else
        {
          rgba.red = rgba.green = rgba.blue = rgba.alpha = 0.0;
        }

      cairo_pattern_add_color_stop_rgba (pattern, stop->offset,
                                         rgba.red, rgba.green,
                                         rgba.blue, rgba.alpha);
    }

  return pattern;
}
Ejemplo n.º 8
0
void
gtk_theming_background_render (GtkStyleContext      *context,
                               cairo_t              *cr,
                               gdouble               x,
                               gdouble               y,
                               gdouble               width,
                               gdouble               height,
                               GtkJunctionSides      junction)
{
  GtkThemingBackground bg;
  gint idx;
  GtkCssValue *background_image;
  GtkCssValue *box_shadow;
  const GdkRGBA *bg_color;

  background_image = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BACKGROUND_IMAGE);
  bg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BACKGROUND_COLOR));
  box_shadow = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_BOX_SHADOW);

  /* This is the common default case of no background */
  if (gtk_rgba_is_clear (bg_color) &&
      _gtk_css_array_value_get_n_values (background_image) == 1 &&
      _gtk_css_image_value_get_image (_gtk_css_array_value_get_nth (background_image, 0)) == NULL &&
      _gtk_css_shadows_value_is_none (box_shadow))
    return;

  bg.context = context;
  _gtk_theming_background_init_context (&bg, width, height, junction);

  cairo_save (cr);
  cairo_translate (cr, x, y);

  /* Outset shadows */
  _gtk_css_shadows_value_paint_box (box_shadow,
                                    cr,
                                    &bg.boxes[GTK_CSS_AREA_BORDER_BOX],
                                    FALSE);

  _gtk_theming_background_paint_color (&bg, cr, bg_color, background_image);

  for (idx = _gtk_css_array_value_get_n_values (background_image) - 1; idx >= 0; idx--)
    {
      _gtk_theming_background_paint_layer (&bg, idx, cr);
    }

  /* Inset shadows */
  _gtk_css_shadows_value_paint_box (box_shadow,
                                    cr,
                                    &bg.boxes[GTK_CSS_AREA_PADDING_BOX],
                                    TRUE);

  cairo_restore (cr);
}
Ejemplo n.º 9
0
static void
gtk_css_image_fallback_draw (GtkCssImage *image,
                             cairo_t     *cr,
                             double       width,
                             double       height)
{
  GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);

  if (fallback->used < 0)
    {
      if (fallback->color)
        gdk_cairo_set_source_rgba (cr, _gtk_css_rgba_value_get_rgba (fallback->color));
      else
        cairo_set_source_rgb (cr, 1, 0, 9);

      cairo_rectangle (cr, 0, 0, width, height);
      cairo_fill (cr);
    }
  else
    _gtk_css_image_draw (fallback->images[fallback->used], cr, width, height);
}
Ejemplo n.º 10
0
/**
 * gtk_snapshot_render_layout:
 * @snapshot: a #GtkSnapshot
 * @context: the #GtkStyleContext to use
 * @x: X origin of the rectangle
 * @y: Y origin of the rectangle
 * @layout: the #PangoLayout to render
 *
 * Creates a render node for rendering @layout according to the style
 * information in @context, and appends it to the current node of @snapshot,
 * without changing the current node.
 *
 * Since: 3.90
 */
void
gtk_snapshot_render_layout (GtkSnapshot     *snapshot,
                            GtkStyleContext *context,
                            gdouble          x,
                            gdouble          y,
                            PangoLayout     *layout)
{
  const GdkRGBA *fg_color;
  graphene_rect_t bounds;
  GtkBorder shadow_extents;
  PangoRectangle ink_rect;
  GtkCssValue *shadow;
  cairo_t *cr;

  g_return_if_fail (snapshot != NULL);
  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
  g_return_if_fail (PANGO_IS_LAYOUT (layout));

  fg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));
  shadow = _gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_TEXT_SHADOW);
  pango_layout_get_pixel_extents (layout, &ink_rect, NULL);
  _gtk_css_shadows_value_get_extents (shadow, &shadow_extents);
  graphene_rect_init (&bounds,
                      ink_rect.x - shadow_extents.left,
                      ink_rect.y - shadow_extents.top,
                      ink_rect.width + shadow_extents.left + shadow_extents.right,
                      ink_rect.height + shadow_extents.top + shadow_extents.bottom);

  gtk_snapshot_translate_2d (snapshot, x, y);

  cr = gtk_snapshot_append_cairo_node (snapshot, &bounds, "Text<%dchars>", pango_layout_get_character_count (layout));

  _gtk_css_shadows_value_paint_layout (shadow, cr, layout);

  gdk_cairo_set_source_rgba (cr, fg_color);
  pango_cairo_show_layout (cr, layout);

  cairo_destroy (cr);
  gtk_snapshot_translate_2d (snapshot, -x, -y);
}
Ejemplo n.º 11
0
Archivo: gtkrender.c Proyecto: Vort/gtk
static void
gtk_do_render_layout (GtkStyleContext *context,
                      cairo_t         *cr,
                      gdouble          x,
                      gdouble          y,
                      PangoLayout     *layout)
{
  const GdkRGBA *fg_color;

  cairo_save (cr);
  fg_color = _gtk_css_rgba_value_get_rgba (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_COLOR));

  prepare_context_for_layout (cr, x, y, layout);

  _gtk_css_shadows_value_paint_layout (_gtk_style_context_peek_property (context, GTK_CSS_PROPERTY_TEXT_SHADOW),
                                       cr, layout);

  gdk_cairo_set_source_rgba (cr, fg_color);
  pango_cairo_show_layout (cr, layout);

  cairo_restore (cr);
}
Ejemplo n.º 12
0
static void
gtk_css_image_fallback_snapshot (GtkCssImage *image,
                                 GtkSnapshot *snapshot,
                                 double       width,
                                 double       height)
{
  GtkCssImageFallback *fallback = GTK_CSS_IMAGE_FALLBACK (image);

  if (fallback->used < 0)
    {
      GdkRGBA red = { 1, 0, 0, 1 };
      const GdkRGBA *color;

      if (fallback->color)
        color = _gtk_css_rgba_value_get_rgba (fallback->color);
      else
        color = &red;

      gtk_snapshot_append_color (snapshot, color, &GRAPHENE_RECT_INIT (0, 0, width, height));
    }
  else
    gtk_css_image_snapshot (fallback->images[fallback->used], snapshot, width, height);
}
Ejemplo n.º 13
0
void
gtk_css_style_render_outline (GtkCssStyle *style,
                              cairo_t     *cr,
                              gdouble      x,
                              gdouble      y,
                              gdouble      width,
                              gdouble      height)
{
  GtkBorderStyle border_style[4];
  GtkRoundedBox border_box;
  double border_width[4];
  GdkRGBA colors[4];

  border_style[0] = _gtk_css_border_style_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_STYLE));
  if (border_style[0] != GTK_BORDER_STYLE_NONE)
    {
      int offset;

      border_style[1] = border_style[2] = border_style[3] = border_style[0];
      border_width[0] = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_WIDTH), 100);
      border_width[3] = border_width[2] = border_width[1] = border_width[0];
      colors[0] = *_gtk_css_rgba_value_get_rgba (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_COLOR));
      colors[3] = colors[2] = colors[1] = colors[0];
      offset = _gtk_css_number_value_get (gtk_css_style_get_value (style, GTK_CSS_PROPERTY_OUTLINE_OFFSET), 100);

      _gtk_rounded_box_init_rect (&border_box, x, y, width, height);
      _gtk_rounded_box_shrink (&border_box,
                               - border_width[GTK_CSS_TOP] - offset,
                               - border_width[GTK_CSS_RIGHT] - offset,
                               - border_width[GTK_CSS_LEFT] - offset,
                               - border_width[GTK_CSS_BOTTOM] - offset);
      _gtk_rounded_box_apply_outline_radius_for_style (&border_box, style, GTK_JUNCTION_NONE);

      render_border (cr, &border_box, border_width, 0, colors, border_style);
    }
}
Ejemplo n.º 14
0
static void
gtk_css_image_radial_draw (GtkCssImage *image,
                           cairo_t     *cr,
                           double       width,
                           double       height)
{
    GtkCssImageRadial *radial = GTK_CSS_IMAGE_RADIAL (image);
    cairo_pattern_t *pattern;
    cairo_matrix_t matrix;
    double x, y;
    double radius, yscale;
    double start, end;
    double r1, r2, r3, r4, r;
    double offset;
    int i, last;

    x = _gtk_css_position_value_get_x (radial->position, width);
    y = _gtk_css_position_value_get_y (radial->position, height);

    if (radial->circle)
    {
        switch (radial->size)
        {
        case GTK_CSS_EXPLICIT_SIZE:
            radius = _gtk_css_number_value_get (radial->sizes[0], width);
            break;
        case GTK_CSS_CLOSEST_SIDE:
            radius = MIN (MIN (x, width - x), MIN (y, height - y));
            break;
        case GTK_CSS_FARTHEST_SIDE:
            radius = MAX (MAX (x, width - x), MAX (y, height - y));
            break;
        case GTK_CSS_CLOSEST_CORNER:
        case GTK_CSS_FARTHEST_CORNER:
            r1 = x*x + y*y;
            r2 = x*x + (height - y)*(height - y);
            r3 = (width - x)*(width - x) + y*y;
            r4 = (width - x)*(width - x) + (height - y)*(height - y);
            if (radial->size == GTK_CSS_CLOSEST_CORNER)
                r = MIN ( MIN (r1, r2), MIN (r3, r4));
            else
                r = MAX ( MAX (r1, r2), MAX (r3, r4));
            radius = sqrt (r);
            break;
        default:
            g_assert_not_reached ();
        }

        radius = MAX (1.0, radius);
        yscale = 1.0;
    }
    else
    {
        double hradius, vradius;

        switch (radial->size)
        {
        case GTK_CSS_EXPLICIT_SIZE:
            hradius = _gtk_css_number_value_get (radial->sizes[0], width);
            vradius = _gtk_css_number_value_get (radial->sizes[1], height);
            break;
        case GTK_CSS_CLOSEST_SIDE:
            hradius = MIN (x, width - x);
            vradius = MIN (y, height - y);
            break;
        case GTK_CSS_FARTHEST_SIDE:
            hradius = MAX (x, width - x);
            vradius = MAX (y, height - y);
            break;
        case GTK_CSS_CLOSEST_CORNER:
            hradius = M_SQRT2 * MIN (x, width - x);
            vradius = M_SQRT2 * MIN (y, height - y);
            break;
        case GTK_CSS_FARTHEST_CORNER:
            hradius = M_SQRT2 * MAX (x, width - x);
            vradius = M_SQRT2 * MAX (y, height - y);
            break;
        default:
            g_assert_not_reached ();
        }

        hradius = MAX (1.0, hradius);
        vradius = MAX (1.0, vradius);

        radius = hradius;
        yscale = vradius / hradius;
    }

    gtk_css_image_radial_get_start_end (radial, radius, &start, &end);

    pattern = cairo_pattern_create_radial (0, 0, 0, 0, 0, radius);
    if (yscale != 1.0)
    {
        cairo_matrix_init_scale (&matrix, 1.0, 1.0 / yscale);
        cairo_pattern_set_matrix (pattern, &matrix);
    }

    if (radial->repeating)
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
    else
        cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);

    offset = start;
    last = -1;
    for (i = 0; i < radial->stops->len; i++)
    {
        GtkCssImageRadialColorStop *stop;
        double pos, step;

        stop = &g_array_index (radial->stops, GtkCssImageRadialColorStop, i);

        if (stop->offset == NULL)
        {
            if (i == 0)
                pos = 0.0;
            else if (i + 1 == radial->stops->len)
                pos = 1.0;
            else
                continue;
        }
        else
            pos = _gtk_css_number_value_get (stop->offset, radius) / radius;

        pos = MAX (pos, 0);
        step = pos / (i - last);
        for (last = last + 1; last <= i; last++)
        {
            const GdkRGBA *rgba;

            stop = &g_array_index (radial->stops, GtkCssImageRadialColorStop, last);

            rgba = _gtk_css_rgba_value_get_rgba (stop->color);
            offset += step;

            cairo_pattern_add_color_stop_rgba (pattern,
                                               (offset - start) / (end - start),
                                               rgba->red,
                                               rgba->green,
                                               rgba->blue,
                                               rgba->alpha);
        }

        offset = pos;
        last = i;
    }

    cairo_rectangle (cr, 0, 0, width, height);
    cairo_translate (cr, x, y);
    cairo_set_source (cr, pattern);
    cairo_fill (cr);

    cairo_pattern_destroy (pattern);
}
Ejemplo n.º 15
0
static void
gtk_css_image_linear_snapshot (GtkCssImage        *image,
                               GtkSnapshot        *snapshot,
                               double              width,
                               double              height)
{
  GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
  GskColorStop *stops;
  GskRenderNode *node;
  double off_x, off_y; /* snapshot offset */
  double angle; /* actual angle of the gradiant line in degrees */
  double x, y; /* coordinates of start point */
  double length; /* distance in pixels for 100% */
  double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
  double offset;
  int i, last;
  char *name;

  if (linear->side)
    {
      /* special casing the regular cases here so we don't get rounding errors */
      switch (linear->side)
      {
        case 1 << GTK_CSS_RIGHT:
          angle = 90;
          break;
        case 1 << GTK_CSS_LEFT:
          angle = 270;
          break;
        case 1 << GTK_CSS_TOP:
          angle = 0;
          break;
        case 1 << GTK_CSS_BOTTOM:
          angle = 180;
          break;
        default:
          angle = atan2 (linear->side & 1 << GTK_CSS_TOP ? -width : width,
                         linear->side & 1 << GTK_CSS_LEFT ? -height : height);
          angle = 180 * angle / G_PI + 90;
          break;
      }
    }
  else
    {
      angle = _gtk_css_number_value_get (linear->angle, 100);
    }

  gtk_css_image_linear_compute_start_point (angle,
                                            width, height,
                                            &x, &y);

  length = sqrt (x * x + y * y);
  gtk_css_image_linear_get_start_end (linear, length, &start, &end);

  if (start == end)
    {
      /* repeating gradients with all color stops sharing the same offset
       * get the color of the last color stop */
      GtkCssImageLinearColorStop *stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, linear->stops->len - 1);

      gtk_snapshot_append_color_node (snapshot,
                                      _gtk_css_rgba_value_get_rgba (stop->color),
                                      &GRAPHENE_RECT_INIT (0, 0, width, height),
                                      "RepeatingLinearGradient<degenerate>");
      return;
    }

  offset = start;
  last = -1;
  stops = g_newa (GskColorStop, linear->stops->len);

  for (i = 0; i < linear->stops->len; i++)
    {
      GtkCssImageLinearColorStop *stop;
      double pos, step;
      
      stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);

      if (stop->offset == NULL)
        {
          if (i == 0)
            pos = 0.0;
          else if (i + 1 == linear->stops->len)
            pos = 1.0;
          else
            continue;
        }
      else
        pos = _gtk_css_number_value_get (stop->offset, length) / length;

      pos = MAX (pos, offset);
      step = (pos - offset) / (i - last);
      for (last = last + 1; last <= i; last++)
        {
          stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);

          offset += step;

          stops[last].offset = (offset - start) / (end - start);
          stops[last].color = *_gtk_css_rgba_value_get_rgba (stop->color);
        }

      offset = pos;
      last = i;
    }

  gtk_snapshot_get_offset (snapshot, &off_x, &off_y);

  if (linear->repeating)
    {
      node = gsk_repeating_linear_gradient_node_new (
          &GRAPHENE_RECT_INIT (off_x, off_y, width, height),
          &GRAPHENE_POINT_INIT (off_x + width / 2 + x * (start - 0.5), off_y + height / 2 + y * (start - 0.5)),
          &GRAPHENE_POINT_INIT (off_x + width / 2 + x * (end - 0.5),   off_y + height / 2 + y * (end - 0.5)),
          stops,
          linear->stops->len);
    }
  else
    {
      node = gsk_linear_gradient_node_new (
          &GRAPHENE_RECT_INIT (off_x, off_y, width, height),
          &GRAPHENE_POINT_INIT (off_x + width / 2 + x * (start - 0.5), off_y + height / 2 + y * (start - 0.5)),
          &GRAPHENE_POINT_INIT (off_x + width / 2 + x * (end - 0.5),   off_y + height / 2 + y * (end - 0.5)),
          stops,
          linear->stops->len);
    }

  name = g_strdup_printf ("%sLinearGradient<%ustops>", linear->repeating ? "Repeating" : "", linear->stops->len);
  gsk_render_node_set_name (node, name);
  g_free (name);

  gtk_snapshot_append_node (snapshot, node);

  gsk_render_node_unref (node);
}
Ejemplo n.º 16
0
static void
gtk_css_image_linear_draw (GtkCssImage        *image,
                           cairo_t            *cr,
                           double              width,
                           double              height)
{
  GtkCssImageLinear *linear = GTK_CSS_IMAGE_LINEAR (image);
  cairo_pattern_t *pattern;
  double angle; /* actual angle of the gradiant line in degrees */
  double x, y; /* coordinates of start point */
  double length; /* distance in pixels for 100% */
  double start, end; /* position of first/last point on gradient line - with gradient line being [0, 1] */
  double offset;
  int i, last;

  if (linear->side)
    {
      /* special casing the regular cases here so we don't get rounding errors */
      switch (linear->side)
      {
        case 1 << GTK_CSS_RIGHT:
          angle = 90;
          break;
        case 1 << GTK_CSS_LEFT:
          angle = 270;
          break;
        case 1 << GTK_CSS_TOP:
          angle = 0;
          break;
        case 1 << GTK_CSS_BOTTOM:
          angle = 180;
          break;
        default:
          angle = atan2 (linear->side & 1 << GTK_CSS_TOP ? -width : width,
                         linear->side & 1 << GTK_CSS_LEFT ? -height : height);
          angle = 180 * angle / G_PI + 90;
          break;
      }
    }
  else
    {
      angle = _gtk_css_number_value_get (linear->angle, 100);
    }

  gtk_css_image_linear_compute_start_point (angle,
                                            width, height,
                                            &x, &y);

  length = sqrt (x * x + y * y);
  gtk_css_image_linear_get_start_end (linear, length, &start, &end);
  pattern = cairo_pattern_create_linear (x * (start - 0.5), y * (start - 0.5),
                                         x * (end - 0.5),   y * (end - 0.5));
  if (linear->repeating)
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
  else
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_PAD);

  offset = start;
  last = -1;
  for (i = 0; i < linear->stops->len; i++)
    {
      GtkCssImageLinearColorStop *stop;
      double pos, step;
      
      stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, i);

      if (stop->offset == NULL)
        {
          if (i == 0)
            pos = 0.0;
          else if (i + 1 == linear->stops->len)
            pos = 1.0;
          else
            continue;
        }
      else
        pos = _gtk_css_number_value_get (stop->offset, length) / length;

      pos = MAX (pos, offset);
      step = (pos - offset) / (i - last);
      for (last = last + 1; last <= i; last++)
        {
          const GdkRGBA *rgba;

          stop = &g_array_index (linear->stops, GtkCssImageLinearColorStop, last);

          rgba = _gtk_css_rgba_value_get_rgba (stop->color);
          offset += step;

          cairo_pattern_add_color_stop_rgba (pattern,
                                             (offset - start) / (end - start),
                                             rgba->red,
                                             rgba->green,
                                             rgba->blue,
                                             rgba->alpha);
        }

      offset = pos;
      last = i;
    }

  cairo_rectangle (cr, 0, 0, width, height);
  cairo_translate (cr, width / 2, height / 2);
  cairo_set_source (cr, pattern);
  cairo_fill (cr);

  cairo_pattern_destroy (pattern);
}