Skip to content

Commit b386261

Browse files
bruno-j-nicolettibjn
and
bjn
authored
Useful code brought over from before the flip to cairo. (#415)
* Test * Useful code brought over from the skia branch --------- Co-authored-by: bjn <[email protected]>
1 parent 721ecc1 commit b386261

File tree

5 files changed

+54
-1
lines changed

5 files changed

+54
-1
lines changed

lib/include/elements/support/canvas.hpp

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ namespace cycfi { namespace elements
163163
point size;
164164
};
165165

166+
struct font_metrics
167+
{
168+
float ascent;
169+
float descent;
170+
float height;
171+
float leading;
172+
};
173+
166174
[[deprecated("Use fill_text(utf8, p) instead following artist API.")]]
167175
void fill_text(point p, char const* utf8);
168176
void fill_text(std::string_view utf8, point p);
@@ -174,6 +182,8 @@ namespace cycfi { namespace elements
174182
text_metrics measure_text(char const* utf8);
175183
void text_align(int align);
176184

185+
font_metrics measure_font();
186+
177187
///////////////////////////////////////////////////////////////////////////////////
178188
// Pixmaps
179189

lib/include/elements/support/color.hpp

+20
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ namespace cycfi { namespace elements
100100
return r;
101101
}
102102

103+
constexpr color operator+(color const& a, color const& b)
104+
{
105+
return color(a.red + b.red, a.green + b.green, a.blue + b.blue, a.alpha + b.alpha*(1.0-a.alpha));
106+
}
107+
108+
constexpr color operator-(color const& a, color const& b)
109+
{
110+
return color(a.red - b.red, a.green - b.green, a.blue - b.blue, a.alpha + b.alpha*(1.0-a.alpha));
111+
}
112+
113+
constexpr color operator*(color const& a, float b)
114+
{
115+
return color(a.red * b, a.green * b, a.blue * b, a.alpha);
116+
}
117+
118+
constexpr color operator*(float a, color const& b)
119+
{
120+
return color(a * b.red, a * b.green, a * b.blue, b.alpha);
121+
}
122+
103123
////////////////////////////////////////////////////////////////////////////
104124
// Common colors
105125
////////////////////////////////////////////////////////////////////////////

lib/include/elements/support/rect.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace cycfi { namespace elements
6565
constexpr bool is_valid(rect r);
6666
constexpr bool is_same_size(rect a, rect b);
6767
bool intersects(rect a, rect b);
68+
rect intersection(rect const& a, rect const& b);
6869

6970
constexpr point center_point(rect r);
7071
constexpr float area(rect r);

lib/src/support/canvas.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,19 @@ namespace cycfi { namespace elements
417417
};
418418
}
419419

420+
canvas::font_metrics canvas::measure_font()
421+
{
422+
cairo_font_extents_t font_extents;
423+
cairo_scaled_font_extents(cairo_get_scaled_font(&_context), &font_extents);
424+
425+
return {
426+
/*ascent=*/ float(font_extents.ascent),
427+
/*descent=*/ float(font_extents.descent),
428+
/*height=*/ float(font_extents.height),
429+
/*leading=*/ float(font_extents.height-(font_extents.ascent+font_extents.descent))
430+
};
431+
}
432+
420433
void canvas::draw(pixmap const& pm, elements::rect src, elements::rect dest)
421434
{
422435
auto state = new_state();
@@ -443,4 +456,3 @@ namespace cycfi { namespace elements
443456
cairo_restore(&_context);
444457
}
445458
}}
446-

lib/src/support/rect.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ namespace cycfi { namespace elements
1818
return true;
1919
}
2020

21+
rect intersection(rect const& a, rect const& b)
22+
{
23+
return {
24+
std::max(a.left, b.left),
25+
std::max(a.top, b.top),
26+
std::min(a.right, b.right),
27+
std::min(a.bottom, b.bottom)
28+
};
29+
}
30+
2131
rect max(rect a, rect b)
2232
{
2333
return {

0 commit comments

Comments
 (0)