improve text_line bounding box, calculate scale_multiplier once + update icu_shaper

This commit is contained in:
artemp 2015-07-09 16:51:41 +02:00
parent ebbaf49896
commit d75400b983
5 changed files with 30 additions and 18 deletions

View file

@ -128,6 +128,9 @@ static void shape_text(text_line & line,
double ymin = 0.0;
double ymax = 0.0;
double units_per_EM = face->get_face()->units_per_EM;
double scale_multiplier = (units_per_EM > 0) ? (size / units_per_EM) : 1.0 ;
for (unsigned i = 0; i < num_glyphs; ++i)
{
auto const& gpos = positions[i];
@ -138,7 +141,7 @@ static void shape_text(text_line & line,
if (face->glyph_dimensions(g))
{
g.face = face;
g.scale_multiplier = size / face->get_face()->units_per_EM;
g.scale_multiplier = scale_multiplier;//size / face->get_face()->units_per_EM;
//Overwrite default advance with better value provided by HarfBuzz
g.unscaled_advance = gpos.x_advance;
g.offset.set(gpos.x_offset * g.scale_multiplier, gpos.y_offset * g.scale_multiplier);
@ -148,7 +151,7 @@ static void shape_text(text_line & line,
ymax = std::max(g.ymax(), ymax);
}
}
std::cerr << "update y min/max " << ymin << "," << ymax << std::endl;
std::cerr << "Harfbuzz shaper: update y min/max " << ymin << "," << ymax << std::endl;
line.set_y_minmax(ymin, ymax);
break; //When we reach this point the current font had all glyphs.
}

View file

@ -90,7 +90,9 @@ static void shape_text(text_line & line,
std::size_t num_chars = static_cast<std::size_t>(num_char);
shaped.releaseBuffer(length);
bool shaped_status = true;
double max_glyph_height = 0;
double ymin = 0.0;
double ymax = 0.0;
double scale_multiplier = size / face->get_face()->units_per_EM;
if (U_SUCCESS(err) && (num_chars == length))
{
unsigned char_index = 0;
@ -109,16 +111,17 @@ static void shape_text(text_line & line,
if (face->glyph_dimensions(g))
{
g.face = face;
g.scale_multiplier = size / face->get_face()->units_per_EM;
double tmp_height = g.height();
if (tmp_height > max_glyph_height) max_glyph_height = tmp_height;
g.scale_multiplier = scale_multiplier;
width_map[char_index++] += g.advance();
line.add_glyph(std::move(g), scale_factor);
ymin = std::min(g.ymin(), ymin);
ymax = std::max(g.ymax(), ymax);
}
}
}
if (!shaped_status) continue;
line.update_max_char_height(max_glyph_height);
std::cerr << "ICU shaper: update y min/max " << ymin << "," << ymax << std::endl;
line.set_y_minmax(ymin, ymax);
return;
}
}

View file

@ -268,6 +268,7 @@ void agg_renderer<T0,T1>::process(debug_symbolizer const& sym,
for (auto const& n : *common_.detector_)
{
auto const& label = n.get();
std::cerr << "label.box=>" << label.box << std::endl;
draw_rotated_rect(pixmap_, *ras_ptr, label.box, label.angle);
}
}

View file

@ -118,7 +118,7 @@ text_upright_e placement_finder::simplify_upright(text_upright_e upright, double
bool placement_finder::find_point_placement(pixel_position const& pos)
{
glyph_positions_ptr glyphs = std::make_shared<glyph_positions>();
glyph_positions_ptr glyphs = std::make_unique<glyph_positions>();
std::vector<std::tuple<box2d<double>,double> > labels;
glyphs->reserve(layouts_.glyphs_count());
@ -140,7 +140,9 @@ bool placement_finder::find_point_placement(pixel_position const& pos)
}
box2d<double> bbox = layout.bounds();
bbox.re_center(layout_center.x, layout_center.y);
std::cerr << "before recenter -->" << bbox << std::endl;
auto box_center_y = bbox.center().y;
bbox.re_center(layout_center.x, layout_center.y + box_center_y);
// TODO
double margin = (text_props_->margin != 0 ? text_props_->margin : text_props_->minimum_distance) * scale_factor_;
@ -197,6 +199,7 @@ bool placement_finder::find_point_placement(pixel_position const& pos)
{
label_box.expand_to_include(box);
}
std::cerr << box << std::endl;
detector_.insert(box, layouts_.text(), std::get<1>(label));
}
// do not render text off the canvas
@ -305,7 +308,7 @@ bool placement_finder::single_line_placement(vertex_cache &pp, text_upright_e or
pixel_position pos = off_pp.current_position() + cluster_offset;
// Center the text on the line
double char_height = line.ymax() - line.ymin();//line.max_char_height();
double char_height = line.ymax() - line.ymin(); //line.max_char_height();
pos.y = -pos.y - char_height/2.0*rot.cos;
pos.x = pos.x + char_height/2.0*rot.sin;

View file

@ -37,12 +37,15 @@ namespace mapnik
{
// Output is centered around (0,0)
static void rotated_box2d(box2d<double> & box, rotation const& rot, pixel_position const& center, double width, double ymin, double ymax)
void rotated_box2d(box2d<double> & box, rotation const& rot, pixel_position const& center, double width, double height, double vertical_displacement)
{
double half_width, half_height;
half_width = width / 2.;
half_height = (ymax - ymin) / 2.;
box.init(center.x - half_width, center.y - half_height + ymin, center.x + half_width, center.y + half_height + ymin);
half_width = 0.5 * width ;
half_height = 0.5 * height;
box.init(center.x - half_width,
center.y - half_height - vertical_displacement,
center.x + half_width,
center.y + half_height - vertical_displacement);
}
pixel_position evaluate_displacement(double dx, double dy, directions_e dir)
@ -192,11 +195,10 @@ void text_layout::layout()
init_auto_alignment();
// Find text origin.
std::cerr << "ymin=" << ymin_ << " ymax=" << ymax_ << std::endl;
displacement_ = scale_factor_ * displacement_ + alignment_offset();
if (rotate_displacement_) displacement_ = displacement_.rotate(!orientation_);
// Find layout bounds, expanded for rotation
rotated_box2d(bounds_, orientation_, displacement_, width_, ymin_, ymax_);
rotated_box2d(bounds_, orientation_, displacement_, width_, height_, ymin_);
}
// In the Unicode string characters are always stored in logical order.
@ -409,8 +411,8 @@ void text_layout::add_line(text_line && line)
line.set_first_line(true);
}
height_ += line.height();
ymin_ += line.ymin();
ymax_ += line.ymax();
ymin_ = std::min(ymin_, line.ymin());
ymax_ = std::max(ymax_, line.ymax());
glyphs_count_ += line.size();
width_ = std::max(width_, line.width());
lines_.emplace_back(std::move(line));