]> Pileus Git - grits/blob - src/objects/gis-marker.c
Add outline to markers
[grits] / src / objects / gis-marker.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gis-marker
20  * @short_description: Single point markers
21  *
22  * Each #GisMarker represents some point on the earth with some form of
23  * content. Commonly this is used to mark geographic features such as cities or
24  * states.
25  * 
26  * While markers represent a place in three dimensions somewhere on, below, or
27  * above the surface of the earth, they are drawn in 2 dimensions so that they
28  * look normal and readable by the user. Due to this, GisObjects should almost
29  * always be added to the GIS_LEVEL_OVERLAY level so they are drawn "above" the
30  * actual earth.
31  */
32
33 #include <config.h>
34 #include "gis-marker.h"
35
36 /*************
37  * GisMarker *
38  *************/
39 /**
40  * gis_marker_new:
41  * @label: a short description of the marker
42  *
43  * Create a new GisMarker which shows the given label when drawn.
44  *
45  * Returns: the new #GisMarker.
46  */
47 GisMarker *gis_marker_new(const gchar *label)
48 {
49         //g_debug("GisMarker: new - %s", label);
50         static const gint OUTLINE =   2;
51         static const gint RADIUS  =   4;
52         static const gint WIDTH   = 120;
53         static const gint HEIGHT  =  40;
54
55         GisMarker *marker = g_object_new(GIS_TYPE_MARKER, NULL);
56         marker->xoff  = RADIUS+OUTLINE;
57         marker->yoff  = HEIGHT-(RADIUS+OUTLINE);
58         marker->label = g_strdup(label);
59         marker->cairo = cairo_create(cairo_image_surface_create(
60                                 CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT));
61
62         cairo_select_font_face(marker->cairo, "sans-serif",
63                         CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
64         cairo_set_font_size(marker->cairo, 14);
65
66         /* Draw outline */
67         cairo_set_source_rgba(marker->cairo, 0, 0, 0, 1);
68         cairo_set_line_width(marker->cairo, OUTLINE*2);
69
70         cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
71         cairo_stroke(marker->cairo);
72
73         cairo_move_to(marker->cairo, marker->xoff+4, marker->yoff-8);
74         cairo_text_path(marker->cairo, marker->label);
75         cairo_stroke(marker->cairo);
76
77         /* Draw filler */
78         cairo_set_source_rgba(marker->cairo, 1, 1, 1, 1);
79
80         cairo_arc(marker->cairo, marker->xoff, marker->yoff, RADIUS, 0, 2*G_PI);
81         cairo_fill(marker->cairo);
82
83         cairo_move_to(marker->cairo, marker->xoff+4, marker->yoff-8);
84         cairo_show_text(marker->cairo, marker->label);
85         return marker;
86 }
87
88 G_DEFINE_TYPE(GisMarker, gis_marker, GIS_TYPE_OBJECT);
89 static void gis_marker_init(GisMarker *marker)
90 {
91 }
92
93 static void gis_marker_finalize(GObject *_marker)
94 {
95         GisMarker *marker = GIS_MARKER(_marker);
96         //g_debug("GisMarker: finalize - %s", marker->label);
97         cairo_surface_t *surface = cairo_get_target(marker->cairo);
98         cairo_surface_destroy(surface);
99         cairo_destroy(marker->cairo);
100         g_free(marker->label);
101 }
102
103 static void gis_marker_class_init(GisMarkerClass *klass)
104 {
105         G_OBJECT_CLASS(klass)->finalize = gis_marker_finalize;
106 }