author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年07月08日 16:59:02 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年07月08日 16:59:02 +0200 |
commit | 3115c6cf96e60f59b3c5d8cdc9d6edc3be2a410d (patch) | |
tree | b1c167a4e8f24b9ef433aaa9d01263223dab7f3b /agg-plot/text-shape.h | |
parent | 47b9fe48be2408ebfeb4c2ad9d852cbd15530fc2 (diff) | |
download | gsl-shell-3115c6cf96e60f59b3c5d8cdc9d6edc3be2a410d.tar.gz |
-rw-r--r-- | agg-plot/text-shape.h | 111 |
diff --git a/agg-plot/text-shape.h b/agg-plot/text-shape.h index a1a55edf..77afb0af 100644 --- a/agg-plot/text-shape.h +++ b/agg-plot/text-shape.h @@ -1,104 +1,85 @@ #ifndef AGGPLOT_TEXT_SHAPE_H #define AGGPLOT_TEXT_SHAPE_H -#include "agg_gsv_text.h" - +#include "text_label.h" #include "sg_object.h" -typedef sg_object_gen<agg::gsv_text> sg_text_base; - namespace draw { class text_shape : public sg_object { public: text_shape(double x, double y, const char* text, double _size = 10.0, double hjustif = 0.0, double vjustif = 0.0): - m_sg_text(), m_x(x), m_y(y), m_text(text), m_size(_size), - m_scaling(0), m_trans(m_sg_text, identity_matrix), m_stroke(m_trans) + m_text_label(text, _size), m_scaling(0), m_size(_size) { - agg::gsv_text& t = m_sg_text.self(); - t.text(text); - t.size(_size); - - const double text_width = t.text_width(), text_height = _size; - m_x -= hjustif * text_width; - m_y -= vjustif * text_height; - - t.start_point(m_x, m_y); - - m_stroke.line_cap(agg::round_cap); - m_stroke.line_join(agg::round_join); + m_matrix.tx = x; + m_matrix.ty = y; + m_text_label.model_mtx(m_matrix); } virtual void rewind(unsigned path_id) { - m_sg_text.self().start_point(m_x, m_y); - m_stroke.rewind(path_id); + m_text_label.rewind(0.0, 0.0); } virtual unsigned vertex(double* x, double* y) { - return m_stroke.vertex(x, y); + unsigned cmd = m_text_label.vertex(x, y); + if (m_scaling && agg::is_vertex(cmd)) + m_scaling->transform(x, y); + return cmd; } virtual void bounding_box(double *x1, double *y1, double *x2, double *y2) { - agg::bounding_rect_single(m_sg_text, 0, x1, y1, x2, y2); - const double pad = 1.0; - *x1 -= pad; - *y1 -= pad; - *x2 += pad; - *y2 += pad; + const double pad = 1.0; + const double tx = m_matrix.tx, ty = m_matrix.ty; + *x1 = tx - pad; + *y1 = ty - pad; + *x2 = tx + m_text_label.get_text_width() + pad; + *y2 = ty + m_text_label.get_text_height() + pad; } virtual str write_svg(int id, agg::rgba8 c, double h) { - const char* text = m_text.cstr(); - int txt_size = (int)(m_size * 1.5); - - const agg::trans_affine& m = *m_scaling; - - double x = m_x, y = -m_y; - const double dx = m.tx, dy = svg_y_coord(m.ty, h); - - if (is_unit_matrix(m)) - { - x += dx; - y += dy; - } - - str svgtext = str::print("<text x=\"%g\" y=\"%g\" id=\"text%i\"" \ - " style=\"font-size:%i\">" \ - " <tspan id=\"tspan%i\">%s</tspan>" \ - "</text>", - x, y, id, txt_size, id, text); - - str s; - if (is_unit_matrix(m)) - s = svgtext; - else - s = str::print("<g transform=\"matrix(%g,%g,%g,%g,%g,%g)\">%s</g>", - m.sx, m.shx, m.shy, m.sy, dx, dy, svgtext.cstr()); - - return s; + const str& text = m_text_label.text(); + int txt_size = m_size; + + const agg::trans_affine* m = m_scaling; + + double x = m_matrix.tx, y = -m_matrix.ty; + const double dx = (m ? m->tx : 0.0), dy = svg_y_coord(m ? m->ty : 0.0, h); + + x += dx; + y += dy; + + str svgtext = str::print("<text x=\"%g\" y=\"%g\" id=\"text%i\"" \ + " style=\"font-size:%i\">" \ + " <tspan id=\"tspan%i\">%s</tspan>" \ + "</text>", + x, y, id, txt_size, id, text.cstr()); + + str s; + if (!m || is_unit_matrix(*m)) + s = svgtext; + else + s = str::print("<g transform=\"matrix(%g,%g,%g,%g,%g,%g)\">%s</g>", + m->sx, m->shx, m->shy, m->sy, dx, dy, svgtext.cstr()); + + return s; } virtual void apply_transform(const agg::trans_affine& m, double as) { - m_stroke.width(1.5 * m.scale()); - m_trans.transformer(m); - m_scaling = &m; + m_scaling = &m; + m_text_label.approximation_scale(m.scale()); } private: - sg_text_base m_sg_text; - double m_x, m_y; - str m_text; - double m_size; - + text_label m_text_label; + agg::trans_affine m_matrix; const agg::trans_affine* m_scaling; - agg::conv_transform<sg_object> m_trans; - agg::conv_stroke<agg::conv_transform<sg_object> > m_stroke; + double m_size; }; } |