1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2011 ~ 2018 Deepin, Inc.
* 2011 ~ 2018 Wang Yong
*
* Author: Wang Yong <wangyong@deepin.com>
* Maintainer: Wang Yong <wangyong@deepin.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using Cairo;
namespace Draw {
public void draw_surface(Cairo.Context cr, ImageSurface surface, int x = 0, int y = 0, int width=0, int height=0) {
if (surface != null) {
cr.set_source_surface(surface, x + int.max(0, (int)(width - surface.get_width() / Utils.get_default_monitor_scale()) / 2),
y + int.max(0, (int)(height - surface.get_height() / Utils.get_default_monitor_scale()) / 2));
cr.paint_with_alpha(1.0);
}
}
public Pango.Layout create_layout_from_context_for_text(Cairo.Context cr, string text, int font_size, int layout_width,
Pango.Alignment horizontal_alignment = Pango.Alignment.LEFT,
string vertical_align = "middle", int? wrap_width=null) {
var font_description = new Pango.FontDescription();
font_description.set_size((int)(font_size * Pango.SCALE));
var layout = Pango.cairo_create_layout(cr);
layout.set_font_description(font_description);
layout.set_markup(text, text.length);
layout.set_alignment(horizontal_alignment);
if (wrap_width == null) {
layout.set_single_paragraph_mode(true);
layout.set_width(layout_width * Pango.SCALE);
layout.set_ellipsize(Pango.EllipsizeMode.END);
} else {
layout.set_width(wrap_width * Pango.SCALE);
layout.set_wrap(Pango.WrapMode.WORD);
}
return layout;
}
public void draw_text(Cairo.Context cr, string text, int x, int y, int width, int height, int size,
Pango.Alignment horizontal_alignment=Pango.Alignment.LEFT,
string vertical_align = "middle",
int? wrap_width=null) {
cr.save();
var layout = create_layout_from_context_for_text(cr, text, size, width, horizontal_alignment, vertical_align, wrap_width);
int text_width, text_height;
layout.get_pixel_size(out text_width, out text_height);
int render_y;
if (vertical_align == "top") {
render_y = y;
} else if (vertical_align == "middle") {
render_y = y + int.max(0, (height - text_height) / 2);
} else {
render_y = y + int.max(0, height - text_height);
}
cr.move_to(x, render_y);
Pango.cairo_update_layout(cr, layout);
Pango.cairo_show_layout(cr, layout);
cr.restore();
}
public void draw_layout(Cairo.Context cr, Pango.Layout layout, int x, int y) {
cr.move_to(x, y);
Pango.cairo_update_layout(cr, layout);
Pango.cairo_show_layout(cr, layout);
}
public Pango.Layout create_layout_from_widget_for_text(Gtk.Widget widget, string text, int font_size, int layout_width,
Pango.Alignment horizontal_alignment = Pango.Alignment.LEFT,
string vertical_align = "middle", int? wrap_width=null) {
var font_description = new Pango.FontDescription();
font_description.set_size((int)(font_size * Pango.SCALE));
var layout = widget.create_pango_layout(null);
layout.set_font_description(font_description);
layout.set_text(text, text.length);
layout.set_alignment(horizontal_alignment);
if (wrap_width == null) {
layout.set_single_paragraph_mode(true);
layout.set_width(layout_width * Pango.SCALE);
layout.set_ellipsize(Pango.EllipsizeMode.END);
} else {
layout.set_width(wrap_width * Pango.SCALE);
layout.set_wrap(Pango.WrapMode.WORD);
}
return layout;
}
public void get_text_bounding_rect_from_widget(Gtk.Widget widget, string text, int font_size, int layout_width,
out int bounding_width, out int bounding_height,
Pango.Alignment horizontal_alignment = Pango.Alignment.LEFT,
string vertical_align = "middle", int? wrap_width=null) {
var layout = create_layout_from_widget_for_text(widget, text, font_size, layout_width, horizontal_alignment, vertical_align, wrap_width);
layout.get_pixel_size(out bounding_width, out bounding_height);
}
public void get_text_bounding_rect_from_context(Cairo.Context cr, string text, int font_size, int layout_width,
out int bounding_width, out int bounding_height,
Pango.Alignment horizontal_alignment = Pango.Alignment.LEFT,
string vertical_align = "middle", int? wrap_width=null) {
var layout = create_layout_from_context_for_text(cr, text, font_size, layout_width, horizontal_alignment, vertical_align, wrap_width);
layout.get_pixel_size(out bounding_width, out bounding_height);
}
public int get_text_render_height(Gtk.Widget widget, string text, int width, int height, int size,
Pango.Alignment horizontal_alignment=Pango.Alignment.LEFT,
string vertical_align = "middle",
int? wrap_width=null) {
int text_width, text_height;
get_text_bounding_rect_from_widget(widget, text, size, width, out text_width, out text_height, horizontal_alignment, vertical_align, wrap_width);
return text_height;
}
public int get_text_render_width(Cairo.Context cr, string text, int width, int height, int size,
Pango.Alignment horizontal_alignment=Pango.Alignment.LEFT,
string vertical_align = "middle",
int? wrap_width=null) {
int text_width, text_height;
get_text_bounding_rect_from_context(cr, text, size, width, out text_width, out text_height, horizontal_alignment, vertical_align, wrap_width);
return text_width;
}
public void set_context_source_color(Cairo.Context cr, Gdk.RGBA rgba) {
cr.set_source_rgba(rgba.red, rgba.green, rgba.blue, rgba.alpha);
}
public void draw_rectangle(Cairo.Context cr, int x, int y, int w, int h, bool fill=true) {
cr.rectangle(x, y, w, h);
if (fill) {
cr.fill();
} else {
cr.stroke();
}
}
public void fill_rounded_rectangle(Context cr, int x, int y, int width, int height, double r) {
cr.new_sub_path();
cr.arc(x + width - r, y + r, r, Math.PI * 3 / 2, Math.PI * 2);
cr.arc(x + width - r, y + height - r, r, 0, Math.PI / 2);
cr.arc(x + r, y + height - r, r, Math.PI / 2, Math.PI);
cr.arc(x + r, y + r, r, Math.PI, Math.PI * 3 / 2);
cr.close_path();
cr.fill();
}
public void stroke_rounded_rectangle(Context cr, int x, int y, int width, int height, double r, Gdk.RGBA frame_color, Gdk.RGBA background_color, int line_width=1) {
cr.set_source_rgba(frame_color.red, frame_color.green, frame_color.blue, frame_color.alpha);
Draw.fill_rounded_rectangle(cr, x, y, width, height, r);
cr.set_source_rgba(background_color.red, background_color.green, background_color.blue, background_color.alpha);
Draw.fill_rounded_rectangle(cr, x + line_width, y + line_width, width - line_width * 2, height - line_width * 2, r);
}
public void draw_search_rectangle(Context cr, int x, int y, int width, int height, double r, bool fill=true) {
// Top side.
cr.move_to(x, y);
cr.line_to(x + width, y);
// Right side.
cr.line_to(x + width, y + height);
// Bottom side.
cr.line_to(x + r, y + height);
// Bottom-left corner.
cr.arc(x + r, y + height - r, r, Math.PI / 2, Math.PI);
// Left side.
cr.line_to(x, y);
// Close path.
cr.close_path();
if (fill) {
cr.fill();
} else {
cr.stroke();
}
}
public void draw_radial(Cairo.Context cr, int x, int width, int height, Gdk.RGBA center_color, Gdk.RGBA edge_color) {
Cairo.Pattern pattern = new Cairo.Pattern.radial(x + width / 2, height, width / 2, x + width / 2, height, 0);
pattern.add_color_stop_rgba(1, center_color.red, center_color.green, center_color.blue, center_color.alpha);
pattern.add_color_stop_rgba(0, edge_color.red, edge_color.green, edge_color.blue, edge_color.alpha);
cr.set_source(pattern);
cr.paint();
}
public void clip_rectangle(Cairo.Context cr, int x, int y, int w, int h) {
cr.rectangle(x, y, w, h);
cr.clip();
}
public void clip_rounded_rectangle(Context cr, int x, int y, int width, int height, double r) {
cr.new_sub_path();
cr.arc(x + width - r, y + r, r, Math.PI * 3 / 2, Math.PI * 2);
cr.arc(x + width - r, y + height - r, r, 0, Math.PI / 2);
cr.arc(x + r, y + height - r, r, Math.PI / 2, Math.PI);
cr.arc(x + r, y + r, r, Math.PI, Math.PI * 3 / 2);
cr.close_path();
cr.clip();
}
}
|