235 lines
7.9 KiB
C++
235 lines
7.9 KiB
C++
#include "Canvas.h"
|
|
#include "Blend2D_Convert.h"
|
|
#include "Font_Face_p.h"
|
|
#include "Image_p.h"
|
|
#include "blend2d/blend2d.h"
|
|
namespace renderive {
|
|
struct Canvas::Impl {
|
|
Image& image;
|
|
BLContext ctx;
|
|
Font current_font;
|
|
bool antialias = true;
|
|
Pen current_pen;
|
|
Brush current_brush;
|
|
Impl(Image& img) : image(img) {
|
|
if (!img.impl_->image.is_empty()) {
|
|
ctx.begin(img.impl_->image);
|
|
if (ctx)
|
|
ctx.set_comp_op(BL_COMP_OP_SRC_OVER);
|
|
}
|
|
}
|
|
~Impl() {
|
|
if (ctx)
|
|
ctx.end();
|
|
}
|
|
void apply_brush() {
|
|
if (!current_brush.enabled()) {
|
|
ctx.set_fill_style(BLRgba(0.0, 0.0, 0.0, 0.0));
|
|
return;
|
|
}
|
|
ctx.set_fill_style(Convert::to_bl(current_brush.color));
|
|
}
|
|
void apply_pen(double stroke_width = -1.0) {
|
|
if (!current_pen.enabled()) {
|
|
ctx.set_stroke_style(BLRgba(0.0, 0.0, 0.0, 0.0));
|
|
return;
|
|
}
|
|
ctx.set_stroke_style(Convert::to_bl(current_pen.color));
|
|
double w = stroke_width > 0.0 ? stroke_width : current_pen.width;
|
|
ctx.set_stroke_width(w);
|
|
if (current_pen.style == Line_Style::Dash || current_pen.style == Line_Style::Dot) {
|
|
BLArray<double> pattern;
|
|
if (current_pen.style == Line_Style::Dash) {
|
|
pattern.append(w * 4.0);
|
|
pattern.append(w * 2.0);
|
|
}
|
|
else {
|
|
pattern.append(w * 1.0);
|
|
pattern.append(w * 2.0);
|
|
}
|
|
ctx.set_stroke_dash_array(pattern);
|
|
}
|
|
else {
|
|
ctx.set_stroke_dash_array(BLArray<double>());
|
|
}
|
|
ctx.set_stroke_caps(static_cast<BLStrokeCap>(Convert::blend2d_line_cap(current_pen.cap)));
|
|
ctx.set_stroke_join(static_cast<BLStrokeJoin>(Convert::blend2d_line_join(current_pen.join)));
|
|
}
|
|
};
|
|
Canvas::Canvas(Image& image) : impl_(std::make_unique<Impl>(image)) {}
|
|
Canvas::~Canvas() = default;
|
|
void Canvas::save() {
|
|
impl_->ctx.save();
|
|
}
|
|
void Canvas::restore() {
|
|
impl_->ctx.restore();
|
|
}
|
|
void Canvas::set_pen(const Pen& pen) {
|
|
impl_->current_pen = pen;
|
|
}
|
|
void Canvas::set_brush(const Brush& brush) {
|
|
impl_->current_brush = brush;
|
|
}
|
|
void Canvas::set_font(const Font& font) {
|
|
impl_->current_font = font;
|
|
}
|
|
void Canvas::set_antialias(bool enabled) {
|
|
impl_->antialias = enabled;
|
|
}
|
|
void Canvas::set_image_interpolation(Image_Interpolation_Mode mode) {
|
|
switch (mode) {
|
|
case Image_Interpolation_Mode::Nearest:
|
|
impl_->ctx.set_pattern_quality(BL_PATTERN_QUALITY_NEAREST);
|
|
break;
|
|
case Image_Interpolation_Mode::Bilinear:
|
|
impl_->ctx.set_pattern_quality(BL_PATTERN_QUALITY_BILINEAR);
|
|
break;
|
|
case Image_Interpolation_Mode::Bicubic:
|
|
impl_->ctx.set_pattern_quality(BL_PATTERN_QUALITY_BILINEAR);
|
|
break;
|
|
}
|
|
}
|
|
void Canvas::translate(double x, double y) {
|
|
impl_->ctx.translate(x, y);
|
|
}
|
|
void Canvas::scale(double x, double y) {
|
|
impl_->ctx.scale(x, y);
|
|
}
|
|
void Canvas::rotate(double radians) {
|
|
impl_->ctx.rotate(radians);
|
|
}
|
|
void Canvas::transform(double m00, double m01, double m10, double m11, double m20, double m21) {
|
|
impl_->ctx.apply_transform(BLMatrix2D(m00, m01, m10, m11, m20, m21));
|
|
}
|
|
void Canvas::reset_transform() {
|
|
impl_->ctx.reset_transform();
|
|
}
|
|
void Canvas::set_clip_rect(const RectF& rect) {
|
|
impl_->ctx.clip_to_rect(Convert::to_bl(rect));
|
|
}
|
|
void Canvas::reset_clip() {
|
|
impl_->ctx.restore_clipping();
|
|
}
|
|
void Canvas::draw_line(PointF first, PointF second) {
|
|
impl_->apply_pen();
|
|
impl_->ctx.stroke_line(first.x, first.y, second.x, second.y);
|
|
}
|
|
void Canvas::draw_polyline(std::span<const PointF> points) {
|
|
if (points.size() < 2)
|
|
return;
|
|
impl_->apply_pen();
|
|
BLPath path;
|
|
path.move_to(points[0].x, points[0].y);
|
|
for (std::size_t i = 1; i < points.size(); ++i)
|
|
path.line_to(points[i].x, points[i].y);
|
|
impl_->ctx.stroke_path(path);
|
|
}
|
|
void Canvas::draw_point(PointF point) {
|
|
impl_->apply_pen();
|
|
impl_->ctx.stroke_line(point.x, point.y, point.x + 0.001, point.y);
|
|
}
|
|
void Canvas::draw_points(std::span<const PointF> points) {
|
|
for (auto& p : points)
|
|
draw_point(p);
|
|
}
|
|
void Canvas::draw_rect(const RectF& rect) {
|
|
impl_->apply_pen();
|
|
auto r = Convert::to_bl(rect);
|
|
impl_->ctx.stroke_rect(r);
|
|
}
|
|
void Canvas::fill_rect(const RectF& rect) {
|
|
impl_->apply_brush();
|
|
auto r = Convert::to_bl(rect);
|
|
impl_->ctx.fill_rect(r);
|
|
}
|
|
void Canvas::draw_ellipse(PointF center, double radius_x, double radius_y) {
|
|
impl_->apply_pen();
|
|
BLPath path;
|
|
path.add_ellipse(BLEllipse(center.x, center.y, radius_x, radius_y));
|
|
impl_->ctx.stroke_path(path);
|
|
}
|
|
void Canvas::draw_polygon(std::span<const PointF> points, bool fill) {
|
|
if (points.size() < 3)
|
|
return;
|
|
BLPath path;
|
|
path.move_to(points[0].x, points[0].y);
|
|
for (std::size_t i = 1; i < points.size(); ++i)
|
|
path.line_to(points[i].x, points[i].y);
|
|
path.close();
|
|
if (fill) {
|
|
impl_->apply_brush();
|
|
impl_->ctx.fill_path(path);
|
|
}
|
|
impl_->apply_pen();
|
|
impl_->ctx.stroke_path(path);
|
|
}
|
|
void Canvas::fill_polygon(std::span<const PointF> points) {
|
|
draw_polygon(points, true);
|
|
}
|
|
void Canvas::draw_image(const RectF& target, const Image& image) {
|
|
auto src = RectF{0, 0, static_cast<double>(image.width()), static_cast<double>(image.height())};
|
|
draw_image(target, image, src);
|
|
}
|
|
void Canvas::draw_image(const RectF& target, const Image& image, const RectF& source) {
|
|
if (image.empty())
|
|
return;
|
|
impl_->ctx.blit_image(
|
|
BLRect(target.x, target.y, target.width, target.height),
|
|
image.impl_->image,
|
|
BLRectI(static_cast<int>(source.x), static_cast<int>(source.y),
|
|
static_cast<int>(source.width), static_cast<int>(source.height)));
|
|
}
|
|
void Canvas::draw_text(PointF position, std::string_view text) {
|
|
if (text.empty() || !impl_->current_font.valid())
|
|
return;
|
|
BLFont bl_font;
|
|
bl_font.create_from_face(impl_->current_font.face->impl_->face, static_cast<float>(impl_->current_font.size));
|
|
impl_->apply_brush();
|
|
impl_->ctx.set_stroke_style(BLRgba(0.0, 0.0, 0.0, 0.0));
|
|
impl_->ctx.fill_utf8_text(BLPoint(position.x, position.y + impl_->current_font.size), bl_font, text.data(), text.size());
|
|
}
|
|
void Canvas::draw_text(const RectF& rect, Text_Align align, std::string_view text) {
|
|
if (text.empty() || !impl_->current_font.valid())
|
|
return;
|
|
auto metrics = measure_text(text);
|
|
double x = rect.x;
|
|
double y = rect.y;
|
|
if ((align & Text_Align::HCenter) == Text_Align::HCenter)
|
|
x += (rect.width - metrics.width) / 2.0;
|
|
else if ((align & Text_Align::Right) == Text_Align::Right)
|
|
x += rect.width - metrics.width;
|
|
if ((align & Text_Align::VCenter) == Text_Align::VCenter)
|
|
y += (rect.height - metrics.height) / 2.0 + metrics.ascent;
|
|
else if ((align & Text_Align::Bottom) == Text_Align::Bottom)
|
|
y += rect.height - metrics.descent;
|
|
else
|
|
y += metrics.ascent;
|
|
draw_text(PointF{x, y}, text);
|
|
}
|
|
Text_Metrics Canvas::measure_text(std::string_view text) const {
|
|
return measure_text(impl_->current_font, text);
|
|
}
|
|
Text_Metrics Canvas::measure_text(const Font& font, std::string_view text) const {
|
|
if (text.empty() || !font.valid())
|
|
return {};
|
|
BLFont bl_font;
|
|
bl_font.create_from_face(font.face->impl_->face, static_cast<float>(font.size));
|
|
BLGlyphBuffer gb;
|
|
gb.set_utf8_text(text.data(), text.size());
|
|
bl_font.shape(gb);
|
|
BLTextMetrics tm{};
|
|
bl_font.get_text_metrics(gb, tm);
|
|
const BLFontMetrics& fm = bl_font.metrics();
|
|
return {
|
|
static_cast<double>(tm.bounding_box.x1 - tm.bounding_box.x0),
|
|
static_cast<double>(fm.ascent + fm.descent),
|
|
static_cast<double>(fm.ascent),
|
|
static_cast<double>(fm.descent),
|
|
static_cast<double>(fm.line_gap)
|
|
};
|
|
}
|
|
Image& Canvas::image() const {
|
|
return impl_->image;
|
|
}
|
|
} // namespace renderive
|