gimpdrawable-stroke.c 5.71 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3 4 5 6
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpdrawable-stroke.c
 * Copyright (C) 2003 Simon Budig  <simon@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <cairo.h>
24
#include <gdk-pixbuf/gdk-pixbuf.h>
25
#include <gegl.h>
26

27
#include "libgimpbase/gimpbase.h"
28 29 30

#include "core-types.h"

31
#include "gimpchannel.h"
32
#include "gimpdrawable-fill.h"
33
#include "gimpdrawable-stroke.h"
34
#include "gimperror.h"
35
#include "gimpimage.h"
36 37 38 39
#include "gimpscanconvert.h"
#include "gimpstrokeoptions.h"

#include "vectors/gimpvectors.h"
40 41 42 43

#include "gimp-intl.h"


Michael Natterer's avatar
Michael Natterer committed
44 45 46
/*  public functions  */

void
47 48 49 50 51 52 53
gimp_drawable_stroke_boundary (GimpDrawable       *drawable,
                               GimpStrokeOptions  *options,
                               const GimpBoundSeg *bound_segs,
                               gint                n_bound_segs,
                               gint                offset_x,
                               gint                offset_y,
                               gboolean            push_undo)
Michael Natterer's avatar
Michael Natterer committed
54
{
55
  GimpScanConvert *scan_convert;
56 57 58 59 60

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
  g_return_if_fail (GIMP_IS_STROKE_OPTIONS (options));
  g_return_if_fail (bound_segs == NULL || n_bound_segs != 0);
61 62
  g_return_if_fail (gimp_fill_options_get_style (GIMP_FILL_OPTIONS (options)) !=
                    GIMP_FILL_STYLE_PATTERN ||
63 64
                    gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL);

65 66
  scan_convert = gimp_scan_convert_new_from_boundary (bound_segs, n_bound_segs,
                                                      offset_x, offset_y);
67 68 69 70

  if (scan_convert)
    {
      gimp_drawable_stroke_scan_convert (drawable, options,
71
                                         scan_convert, push_undo);
72 73 74 75
      gimp_scan_convert_free (scan_convert);
    }
}

76 77 78 79 80 81
gboolean
gimp_drawable_stroke_vectors (GimpDrawable       *drawable,
                              GimpStrokeOptions  *options,
                              GimpVectors        *vectors,
                              gboolean            push_undo,
                              GError            **error)
82
{
83
  const GimpBezierDesc *bezier;
84

85 86 87 88
  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), FALSE);
  g_return_val_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)), FALSE);
  g_return_val_if_fail (GIMP_IS_STROKE_OPTIONS (options), FALSE);
  g_return_val_if_fail (GIMP_IS_VECTORS (vectors), FALSE);
89 90
  g_return_val_if_fail (gimp_fill_options_get_style (GIMP_FILL_OPTIONS (options)) !=
                        GIMP_FILL_STYLE_PATTERN ||
91 92 93
                        gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL,
                        FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
94

95
  bezier = gimp_vectors_get_bezier (vectors);
96

97
  if (bezier && bezier->num_data >= 2)
98
    {
99
      GimpScanConvert *scan_convert = gimp_scan_convert_new ();
100

101
      gimp_scan_convert_add_bezier (scan_convert, bezier);
102 103
      gimp_drawable_stroke_scan_convert (drawable, options,
                                         scan_convert, push_undo);
104

105 106 107
      gimp_scan_convert_free (scan_convert);

      return TRUE;
108 109
    }

110
  g_set_error_literal (error, GIMP_ERROR, GIMP_FAILED,
111
                       _("Not enough points to stroke"));
112

113
  return FALSE;
114
}
115

116 117 118 119 120
void
gimp_drawable_stroke_scan_convert (GimpDrawable      *drawable,
                                   GimpStrokeOptions *options,
                                   GimpScanConvert   *scan_convert,
                                   gboolean           push_undo)
121
{
122 123
  gdouble  width;
  GimpUnit unit;
124 125 126 127 128 129 130 131 132

  g_return_if_fail (GIMP_IS_DRAWABLE (drawable));
  g_return_if_fail (gimp_item_is_attached (GIMP_ITEM (drawable)));
  g_return_if_fail (GIMP_IS_STROKE_OPTIONS (options));
  g_return_if_fail (scan_convert != NULL);
  g_return_if_fail (gimp_fill_options_get_style (GIMP_FILL_OPTIONS (options)) !=
                    GIMP_FILL_STYLE_PATTERN ||
                    gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL);

133 134
  if (! gimp_item_mask_intersect (GIMP_ITEM (drawable), NULL, NULL, NULL, NULL))
    return;
135

136 137
  width = gimp_stroke_options_get_width (options);
  unit  = gimp_stroke_options_get_unit (options);
138

139 140
  if (unit != GIMP_UNIT_PIXEL)
    {
141 142 143
      GimpImage *image = gimp_item_get_image (GIMP_ITEM (drawable));
      gdouble    xres;
      gdouble    yres;
144

145
      gimp_image_get_resolution (image, &xres, &yres);
146

147
      gimp_scan_convert_set_pixel_ratio (scan_convert, yres / xres);
148

149
      width = gimp_units_to_pixels (width, unit, yres);
150
    }
151

152 153 154 155 156 157
  gimp_scan_convert_stroke (scan_convert, width,
                            gimp_stroke_options_get_join_style (options),
                            gimp_stroke_options_get_cap_style (options),
                            gimp_stroke_options_get_miter_limit (options),
                            gimp_stroke_options_get_dash_offset (options),
                            gimp_stroke_options_get_dash_info (options));
158

159 160
  gimp_drawable_fill_scan_convert (drawable, GIMP_FILL_OPTIONS (options),
                                   scan_convert, push_undo);
161
}