<commit_before>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include <mapnik/debug.hpp>
#include <mapnik/view_transform.hpp>
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/text/point_layout.hpp>
#include <mapnik/text/placements/base.hpp>
#include <mapnik/text/text_layout.hpp>
#include <mapnik/text/glyph_info.hpp>
#include <mapnik/text/text_properties.hpp>
#include <mapnik/text/glyph_positions.hpp>
#include <mapnik/text/glyph_bbox.hpp>
#include <mapnik/vertex_cache.hpp>
#include <mapnik/util/math.hpp>
#include <mapnik/symbolizer.hpp>
#include <mapnik/marker.hpp>

#include <vector>

namespace mapnik { namespace text {

point_layout::point_layout(params_type const & params)
    : params_(params),
      collision_cache_insert_(parse_collision_detector_keys(
          params.get_optional<std::string, keys::collision_cache_insert>()))
{
}

bool point_layout::try_placement(
    text_layout_generator & layout_generator,
    pixel_position const & pos)
{
    glyph_positions_ptr glyphs = std::make_unique<glyph_positions>();
    layout_container & layouts = *layout_generator.layouts_;
    evaluated_text_properties const & text_props = layout_generator.get_text_props();
    detector_type & detector = layout_generator.detector_;

    if (try_placement(layouts, detector, text_props, pos, *glyphs))
    {
        process_bboxes(detector, layouts, glyphs);
        return true;
    }

    return false;
}

bool point_layout::try_placement(
    text_layout_generator & layout_generator,
    point_position const & pos)
{
    // TODO: angle
    return try_placement(layout_generator, pos.coords);
}

bool point_layout::try_placement(
    layout_container const & layouts,
    detector_type & detector,
    evaluated_text_properties const & text_props,
    pixel_position const & pos,
    glyph_positions & glyphs)
{
    bool base_point_set = false;
    for (auto const& layout_wrapper : layouts)
    {
        text_layout const& layout = layout_wrapper.get();
        rotation const& orientation = layout.orientation();

        // Find text origin.
        pixel_position layout_center = pos + layout.displacement();

        if (!base_point_set)
        {
            glyphs.set_base_point(layout_center);
            base_point_set = true;
        }

        pixel_position layout_offset = layout_center - glyphs.get_base_point();
        layout_offset.y = -layout_offset.y;
        layout_center.y = -layout_center.y;

        // IMPORTANT NOTE:
        //   x and y are relative to the center of the text
        //   coordinate system:
        //   x: grows from left to right
        //   y: grows from bottom to top (opposite of normal computer graphics)

        double x, y;

        // set for upper left corner of text envelope for the first line, top left of first character
        y = layout.height() / 2.0;

        for ( auto const& line : layout)
        {
            y -= line.height(); //Automatically handles first line differently
            x = layout.jalign_offset(line.width());

            for (auto const& glyph : line)
            {
                pixel_position glyph_pos(x, y);
                glyph_pos = glyph_pos.rotate(orientation);
                box2d<double> bbox(get_bbox(layout, glyph, glyph_pos + layout_center, orientation));
                if (collision(detector, text_props, bbox, layouts.text()))
                {
                    return false;
                }

                // place the character relative to the center of the string envelope
                glyphs.emplace_back(glyph, glyph_pos + layout_offset, orientation, bbox);
                if (glyph.advance())
                {
                    //Only advance if glyph is not part of a multiple glyph sequence
                    x += glyph.advance() + glyph.format->character_spacing * params_.scale_factor;
                }
            }
        }
    }
    return true;
}<commit_msg>Align the advance for monochrome font<commit_after>/*****************************************************************************
 *
 * This file is part of Mapnik (c++ mapping toolkit)
 *
 * Copyright (C) 2015 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

#include <mapnik/debug.hpp>
#include <mapnik/view_transform.hpp>
#include <mapnik/expression_evaluator.hpp>
#include <mapnik/text/point_layout.hpp>
#include <mapnik/text/placements/base.hpp>
#include <mapnik/text/text_layout.hpp>
#include <mapnik/text/glyph_info.hpp>
#include <mapnik/text/text_properties.hpp>
#include <mapnik/text/glyph_positions.hpp>
#include <mapnik/text/glyph_bbox.hpp>
#include <mapnik/vertex_cache.hpp>
#include <mapnik/util/math.hpp>
#include <mapnik/symbolizer.hpp>
#include <mapnik/marker.hpp>

#include <vector>

namespace mapnik { namespace text {

point_layout::point_layout(params_type const & params)
    : params_(params),
      collision_cache_insert_(parse_collision_detector_keys(
          params.get_optional<std::string, keys::collision_cache_insert>()))
{
}

bool point_layout::try_placement(
    text_layout_generator & layout_generator,
    pixel_position const & pos)
{
    glyph_positions_ptr glyphs = std::make_unique<glyph_positions>();
    layout_container & layouts = *layout_generator.layouts_;
    evaluated_text_properties const & text_props = layout_generator.get_text_props();
    detector_type & detector = layout_generator.detector_;

    if (try_placement(layouts, detector, text_props, pos, *glyphs))
    {
        process_bboxes(detector, layouts, glyphs);
        return true;
    }

    return false;
}

bool point_layout::try_placement(
    text_layout_generator & layout_generator,
    point_position const & pos)
{
    // TODO: angle
    return try_placement(layout_generator, pos.coords);
}

bool point_layout::try_placement(
    layout_container const & layouts,
    detector_type & detector,
    evaluated_text_properties const & text_props,
    pixel_position const & pos,
    glyph_positions & glyphs)
{
    bool base_point_set = false;
    for (auto const& layout_wrapper : layouts)
    {
        text_layout const& layout = layout_wrapper.get();
        rotation const& orientation = layout.orientation();

        // Find text origin.
        pixel_position layout_center = pos + layout.displacement();

        if (!base_point_set)
        {
            glyphs.set_base_point(layout_center);
            base_point_set = true;
        }

        pixel_position layout_offset = layout_center - glyphs.get_base_point();
        layout_offset.y = -layout_offset.y;
        layout_center.y = -layout_center.y;

        // IMPORTANT NOTE:
        //   x and y are relative to the center of the text
        //   coordinate system:
        //   x: grows from left to right
        //   y: grows from bottom to top (opposite of normal computer graphics)

        double x, y;

        // set for upper left corner of text envelope for the first line, top left of first character
        y = layout.height() / 2.0;

        for ( auto const& line : layout)
        {
            y -= line.height(); //Automatically handles first line differently
            x = layout.jalign_offset(line.width());

            for (auto const& glyph : line)
            {
                pixel_position glyph_pos(x, y);
                glyph_pos = glyph_pos.rotate(orientation);
                box2d<double> bbox(get_bbox(layout, glyph, glyph_pos + layout_center, orientation));
                if (collision(detector, text_props, bbox, layouts.text()))
                {
                    return false;
                }

                // place the character relative to the center of the string envelope
                glyphs.emplace_back(glyph, glyph_pos + layout_offset, orientation, bbox);
                if (glyph.advance())
                {
                    //Only advance if glyph is not part of a multiple glyph sequence
                    x += glyph.advance() + glyph.format->character_spacing * params_.scale_factor;
                }
                if (glyph.format->text_mode == TEXT_MODE_MONO)
                {
                    x = std::round(x);
                }
            }
        }
    }
    return true;
}
{'fraction_non_alphanumeric': 0.11760549162123965, 'fraction_numerical': 0.004037956793862305, 'mean_word_length': 2.805993084902036, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3918487', 'n_tokens_mistral': 2830, 'n_tokens_neox': 2663, 'n_words': 800}