|
|
b8976b |
|
|
|
b8976b |
#include <algorithm></algorithm>
|
|
|
b8976b |
#include <iostream></iostream>
|
|
|
b8976b |
|
|
|
b8976b |
#include <glibmm.h></glibmm.h>
|
|
|
b8976b |
|
|
|
71e4f2 |
#include "labpangorenderer.h"
|
|
|
b8976b |
|
|
|
b8976b |
#include "surface.h"
|
|
|
b8976b |
#include "log.h"
|
|
|
b8976b |
|
|
|
b8976b |
#include "freetypeview.h"
|
|
|
b8976b |
|
|
|
b8976b |
|
|
|
b8976b |
FreeTypeView::FreeTypeView() {
|
|
|
b8976b |
params.family = "Ani";
|
|
|
b8976b |
params.bold = false;
|
|
|
b8976b |
params.italic = false;
|
|
|
b8976b |
params.size = 12;
|
|
|
b8976b |
|
|
|
71e4f2 |
params.matrix = Matrix().translation(Vector2(15.5, 18.4));
|
|
|
b8976b |
params.color = Color(1, 1, 0, 1);
|
|
|
b8976b |
|
|
|
b8976b |
params.text = "Hello World! 123456";
|
|
|
b8976b |
|
|
|
b8976b |
Glib::signal_timeout().connect(
|
|
|
b8976b |
sigc::mem_fun(*this, &FreeTypeView::on_timeout),
|
|
|
b8976b |
20,
|
|
|
b8976b |
Glib::PRIORITY_DEFAULT_IDLE );
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
FreeTypeView::~FreeTypeView()
|
|
|
b8976b |
{ }
|
|
|
b8976b |
|
|
|
b8976b |
bool
|
|
|
b8976b |
FreeTypeView::on_timeout() {
|
|
|
b8976b |
if (!is_visible()) return false;
|
|
|
71e4f2 |
params.matrix.m20 += 0.01;
|
|
|
b8976b |
update_surface();
|
|
|
b8976b |
return true;
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
void
|
|
|
b8976b |
FreeTypeView::update_surface() {
|
|
|
b8976b |
this->surface.clear();
|
|
|
b8976b |
const int width = get_allocated_width();
|
|
|
b8976b |
const int height = get_allocated_height();
|
|
|
b8976b |
if (width <= 0 || height <= 0)
|
|
|
b8976b |
return;
|
|
|
b8976b |
|
|
|
71e4f2 |
Matrix matrix = params.matrix;
|
|
|
71e4f2 |
Real det2 = matrix.m00*matrix.m11 - matrix.m01*matrix.m10;
|
|
|
71e4f2 |
if (det2 <= real_precision_sqr)
|
|
|
71e4f2 |
return;
|
|
|
71e4f2 |
Real scale = sqrt(det2);
|
|
|
71e4f2 |
matrix.row_x() /= scale;
|
|
|
71e4f2 |
matrix.row_y() /= scale;
|
|
|
71e4f2 |
Real size = params.size*scale;
|
|
|
71e4f2 |
|
|
|
b8976b |
// init
|
|
|
71e4f2 |
PangoFontMap *font_map = lab_pango_font_map_new(params.hinting);
|
|
|
b8976b |
PangoContext *context = pango_font_map_create_context(font_map);
|
|
|
71e4f2 |
|
|
|
b8976b |
// load font
|
|
|
b8976b |
PangoFontDescription *font_desc = pango_font_description_new();
|
|
|
b8976b |
pango_font_description_set_family(font_desc, params.family.c_str());
|
|
|
b8976b |
pango_font_description_set_weight(font_desc, params.bold ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL);
|
|
|
b8976b |
pango_font_description_set_style(font_desc, params.italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
|
|
|
71e4f2 |
pango_font_description_set_absolute_size(font_desc, size * PANGO_SCALE);
|
|
|
71e4f2 |
|
|
|
71e4f2 |
PangoFont *font = pango_context_load_font(context, font_desc);
|
|
|
b8976b |
|
|
|
b8976b |
// create layout
|
|
|
b8976b |
PangoLayout *layout = pango_layout_new(context);
|
|
|
b8976b |
pango_layout_set_font_description(layout, font_desc);
|
|
|
b8976b |
pango_layout_set_text(layout, params.text.c_str(), (int)params.text.size());
|
|
|
b8976b |
|
|
|
b8976b |
// reneder
|
|
|
b8976b |
DataSurface surface(width, height);
|
|
|
71e4f2 |
PangoRenderer *renderer = lab_pango_renderer_new();
|
|
|
71e4f2 |
lab_pango_renderer_set_surface((LabPangoRenderer*)renderer, &surface);
|
|
|
71e4f2 |
lab_pango_renderer_set_matrix((LabPangoRenderer*)renderer, ¶ms.matrix);
|
|
|
71e4f2 |
lab_pango_renderer_set_color((LabPangoRenderer*)renderer, ¶ms.color);
|
|
|
71e4f2 |
pango_renderer_draw_layout(renderer, layout, 0, 0);
|
|
|
71e4f2 |
|
|
|
b8976b |
// free pango
|
|
|
b8976b |
g_object_unref(renderer);
|
|
|
b8976b |
pango_font_description_free(font_desc);
|
|
|
b8976b |
g_object_unref(layout);
|
|
|
b8976b |
g_object_unref(font);
|
|
|
b8976b |
g_object_unref(context);
|
|
|
b8976b |
g_object_unref(font_map);
|
|
|
b8976b |
|
|
|
b8976b |
this->surface = surface.to_cairo_surface(true);
|
|
|
b8976b |
|
|
|
b8976b |
queue_draw();
|
|
|
b8976b |
}
|
|
|
b8976b |
|
|
|
b8976b |
void
|
|
|
b8976b |
FreeTypeView::on_draw_view(const Cairo::RefPtr<cairo::context> &context) {</cairo::context>
|
|
|
b8976b |
if (surface) {
|
|
|
b8976b |
context->save();
|
|
|
b8976b |
context->transform(transform_from_pixels().to_cairo());
|
|
|
b8976b |
context->set_source(surface, 0, 0);
|
|
|
b8976b |
context->paint();
|
|
|
b8976b |
context->restore();
|
|
|
b8976b |
}
|
|
|
b8976b |
}
|
|
|
b8976b |
|