2 * draw.c - draw functions
4 * Copyright © 2007 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <cairo-xlib.h>
31 drawtext(Display
*disp
, int screen
, int x
, int y
, int w
, int h
, Drawable drawable
, int dw
, int dh
, XftFont
*font
, const char *text
, XColor color
[ColLast
])
36 cairo_font_face_t
*font_face
;
37 cairo_surface_t
*surface
;
40 drawrectangle(disp
, screen
, x
, y
, w
, h
, drawable
, dw
, dh
, True
, color
[ColBG
]);
44 surface
= cairo_xlib_surface_create(disp
, drawable
, DefaultVisual(disp
, screen
), dw
, dh
);
45 cr
= cairo_create(surface
);
46 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
47 cairo_set_font_face(cr
, font_face
);
48 cairo_set_font_size(cr
, font
->height
);
49 cairo_set_source_rgb(cr
, color
[ColFG
].red
/ 65535.0, color
[ColFG
].green
/ 65535.0, color
[ColFG
].blue
/ 65535.0);
51 olen
= len
= a_strlen(text
);
52 if(len
>= sizeof(buf
))
53 len
= sizeof(buf
) - 1;
54 memcpy(buf
, text
, len
);
56 while(len
&& (nw
= textwidth(disp
, font
, buf
)) > w
)
59 return; /* too long */
70 cairo_move_to(cr
, x
+ font
->height
/ 2, y
+ font
->ascent
+ (dh
- font
->height
) / 2);
71 cairo_show_text(cr
, buf
);
73 cairo_font_face_destroy(font_face
);
75 cairo_surface_destroy(surface
);
79 drawrectangle(Display
*disp
, int screen
, int x
, int y
, int w
, int h
, Drawable drawable
, int dw
, int dh
, Bool filled
, XColor color
)
81 cairo_surface_t
*surface
;
84 surface
= cairo_xlib_surface_create(disp
, drawable
, DefaultVisual(disp
, screen
), dw
, dh
);
85 cr
= cairo_create (surface
);
87 cairo_set_antialias(cr
, CAIRO_ANTIALIAS_NONE
);
88 cairo_set_line_width(cr
, 1.0);
89 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
92 cairo_rectangle(cr
, x
, y
, w
+ 1, h
+ 1);
96 cairo_rectangle(cr
, x
+ 1, y
, w
, h
);
101 cairo_surface_destroy(surface
);
105 drawcircle(Display
*disp
, int screen
, int x
, int y
, int r
, Drawable drawable
, int dw
, int dh
, Bool filled
, XColor color
)
107 cairo_surface_t
*surface
;
110 surface
= cairo_xlib_surface_create(disp
, drawable
, DefaultVisual(disp
, screen
), dw
, dh
);
111 cr
= cairo_create (surface
);
112 cairo_set_line_width(cr
, 1.0);
113 cairo_set_source_rgb(cr
, color
.red
/ 65535.0, color
.green
/ 65535.0, color
.blue
/ 65535.0);
116 cairo_arc (cr
, x
+ r
, y
+ r
, r
, 0, 2 * M_PI
);
120 cairo_arc (cr
, x
+ r
, y
+ r
, r
- 1, 0, 2 * M_PI
);
125 cairo_surface_destroy(surface
);
129 textwidth(Display
*disp
, XftFont
*font
, char *text
)
131 cairo_surface_t
*surface
;
133 cairo_font_face_t
*font_face
;
134 cairo_text_extents_t te
;
136 surface
= cairo_xlib_surface_create(disp
, DefaultScreen(disp
),
137 DefaultVisual(disp
, DefaultScreen(disp
)),
138 DisplayWidth(disp
, DefaultScreen(disp
)),
139 DisplayHeight(disp
, DefaultScreen(disp
)));
140 cr
= cairo_create(surface
);
141 font_face
= cairo_ft_font_face_create_for_pattern(font
->pattern
);
142 cairo_set_font_face(cr
, font_face
);
143 cairo_set_font_size(cr
, font
->height
);
144 cairo_text_extents(cr
, text
, &te
);
146 cairo_surface_destroy(surface
);
147 cairo_font_face_destroy(font_face
);
149 return MAX(te
.x_advance
, te
.width
) + font
->height
;
152 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99