<commit_before>#include <cstdlib>
#include <sstream>
#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>

void textInfo(osgText::Text* text)
{
    const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap();

    const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin();


    osgText::String& s = text->getText();

    for(unsigned int i = 0; i < s.size(); i++)
    {
        osg::Vec2 ul; text->getCoord(0 + (i * 4), ul); // upperLeft
        osg::Vec2 ll; text->getCoord(1 + (i * 4), ll); // lowerLeft
        osg::Vec2 lr; text->getCoord(2 + (i * 4), lr); // lowerRight
        osg::Vec2 ur; text->getCoord(3 + (i * 4), ur); // upperRight

        osg::notify(osg::NOTICE)
            << "'" << static_cast<char>(s[i]) << "':"
            << " width(" << lr.x() - ll.x() << ")"
            << " height(" << ul.y() - ll.y() << ")" << std::endl << "\t"
            << "ul(" << ul << "), "
            << "ll(" << ll << "), "
            << "lr(" << lr << "), "
            << "ur(" << ur << ")"
            << std::endl
        ;
    }
}

osg::Camera* createOrthoCamera(double width, double height)
{
    osg::Camera* camera = new osg::Camera();

    camera->getOrCreateStateSet()->setMode(
        GL_LIGHTING,
        osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
    );

    osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);

    camera->setProjectionMatrix(m);
    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera->setViewMatrix(osg::Matrix::identity());
    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
    camera->setRenderOrder(osg::Camera::POST_RENDER);

    return camera;
}

typedef std::list<unsigned int> Sizes;

struct TextSettings
{
    TextSettings():
        fontFilename("fonts/arial.ttf"),
        glyphImageMargin(1),
        glyphImageMarginRatio(0.02),
        glyphInterval(1),
        textColor(1.0f, 1.0f, 1.0f, 1.0f),
        backdropType(osgText::Text::NONE),
        backdropOffset(0.04f, 0.04f),
        backdropColor(0.0f, 0.0f, 0.0f, 1.0f)
    {
    }

    void read(osg::ArgumentParser& arguments)
    {
        if (arguments.read("--test"))
        {
            backgroundColor = osg::Vec4(1.0, 1.0, 1.0, 1.0);

            fontFilename = "fonts/arialbd.ttf";
            backdropType = osgText::Text::OUTLINE;

            sizes.clear();
            sizes.push_back(8);
            sizes.push_back(16);
            sizes.push_back(32);
            sizes.push_back(64);
            sizes.push_back(128);
        }

        if (arguments.read("--font",fontFilename)) {}


        if (arguments.read("--margin", glyphImageMargin)) {}
        if (arguments.read("--margin-ratio", glyphImageMarginRatio)) {}
        if (arguments.read("--interval", glyphInterval)) {}


        if (arguments.read("--outline")) backdropType = osgText::Text::OUTLINE;
        if (arguments.read("--shadow")) backdropType = osgText::Text::DROP_SHADOW_BOTTOM_RIGHT;

        float offset;
        if (arguments.read("--offset", offset)) backdropOffset.set(offset, offset);

        if (arguments.read("--text-color", textColor.r(), textColor.g(), textColor.b(), textColor.a())) {}
        if (arguments.read("--bd-color", backdropColor.r(), backdropColor.g(), backdropColor.b(), backdropColor.a())) {}
        if (arguments.read("--bg-color", backgroundColor.r(), backgroundColor.g(), backgroundColor.b(), backgroundColor.a())) {}

    }

    void setText(osgText::Text& text)
    {
        OSG_NOTICE<<"Settings::setText()"<<std::endl;
        text.setFont(fontFilename);
        text.setColor(textColor);
        text.setBackdropType(backdropType);
        text.setBackdropOffset(backdropOffset.x(), backdropOffset.y());
        text.setBackdropColor(backdropColor);
    }

    std::string                 fontFilename;
    unsigned int                glyphImageMargin;
    float                       glyphImageMarginRatio;
    int                         glyphInterval;

    osg::Vec4                   textColor;
    osgText::Text::BackdropType backdropType;
    osg::Vec2                   backdropOffset;
    osg::Vec4                   backdropColor;
    osg::Vec4                   backgroundColor;
    Sizes                       sizes;
};

osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigned int size)
{
    static osg::Vec3 pos(10.0f, 10.0f, 0.0f);

    osgText::Text* label = new osgText::Text();
    osg::ref_ptr<osgText::Font> font  = osgText::readRefFontFile(settings.fontFilename);

    font->setGlyphImageMargin(settings.glyphImageMargin);
    font->setGlyphImageMarginRatio(settings.glyphImageMarginRatio);

    settings.setText(*label);

    label->setCharacterSize(size);
    label->setFontResolution(size, size);
    label->setPosition(pos);
    label->setAlignment(osgText::Text::LEFT_BOTTOM);


    // It seems to be important we do this last to get best results?
    label->setText(l);


    // textInfo(label);

    pos.y() += size + 10.0f;<commit_msg>To control the GlyphTexture Min/MagFilter values Added --min <value> and --mag <value> filter with LINEAR, NEAREST and LINEAR_MIPMAP_LINER options for values<commit_after>#include <cstdlib>
#include <sstream>
#include <osg/io_utils>
#include <osg/ArgumentParser>
#include <osg/Geode>
#include <osg/MatrixTransform>
#include <osgDB/ReadFile>
#include <osgDB/WriteFile>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <osgGA/StateSetManipulator>

void textInfo(osgText::Text* text)
{
    const osgText::Text::TextureGlyphQuadMap& tgqm = text->getTextureGlyphQuadMap();

    const osgText::Text::TextureGlyphQuadMap::const_iterator tgqmi = tgqm.begin();


    osgText::String& s = text->getText();

    for(unsigned int i = 0; i < s.size(); i++)
    {
        osg::Vec2 ul; text->getCoord(0 + (i * 4), ul); // upperLeft
        osg::Vec2 ll; text->getCoord(1 + (i * 4), ll); // lowerLeft
        osg::Vec2 lr; text->getCoord(2 + (i * 4), lr); // lowerRight
        osg::Vec2 ur; text->getCoord(3 + (i * 4), ur); // upperRight

        osg::notify(osg::NOTICE)
            << "'" << static_cast<char>(s[i]) << "':"
            << " width(" << lr.x() - ll.x() << ")"
            << " height(" << ul.y() - ll.y() << ")" << std::endl << "\t"
            << "ul(" << ul << "), "
            << "ll(" << ll << "), "
            << "lr(" << lr << "), "
            << "ur(" << ur << ")"
            << std::endl
        ;
    }
}

osg::Camera* createOrthoCamera(double width, double height)
{
    osg::Camera* camera = new osg::Camera();

    camera->getOrCreateStateSet()->setMode(
        GL_LIGHTING,
        osg::StateAttribute::PROTECTED | osg::StateAttribute::OFF
    );

    osg::Matrix m = osg::Matrix::ortho2D(0.0f, width, 0.0f, height);

    camera->setProjectionMatrix(m);
    camera->setReferenceFrame(osg::Transform::ABSOLUTE_RF);
    camera->setViewMatrix(osg::Matrix::identity());
    camera->setClearMask(GL_DEPTH_BUFFER_BIT);
    camera->setRenderOrder(osg::Camera::POST_RENDER);

    return camera;
}

typedef std::list<unsigned int> Sizes;

struct TextSettings
{
    TextSettings():
        fontFilename("fonts/arial.ttf"),
        minFilter(osg::Texture::LINEAR_MIPMAP_LINEAR),
        magFilter(osg::Texture::LINEAR),
        glyphImageMargin(1),
        glyphImageMarginRatio(0.02),
        glyphInterval(1),
        textColor(1.0f, 1.0f, 1.0f, 1.0f),
        backdropType(osgText::Text::NONE),
        backdropOffset(0.04f, 0.04f),
        backdropColor(0.0f, 0.0f, 0.0f, 1.0f)
    {
    }


    void readFilterMode(const std::string& value, osg::Texture::FilterMode& filterMode)
    {
        if (value=="LINEAR") filterMode = osg::Texture::LINEAR;
        if (value=="NEAREST") filterMode = osg::Texture::NEAREST;
        if (value=="LINEAR_MIPMAP_LINEAR") filterMode = osg::Texture::LINEAR_MIPMAP_LINEAR;
    }

    void read(osg::ArgumentParser& arguments)
    {
        if (arguments.read("--test"))
        {
            backgroundColor = osg::Vec4(1.0, 1.0, 1.0, 1.0);

            fontFilename = "fonts/arialbd.ttf";
            backdropType = osgText::Text::OUTLINE;

            sizes.clear();
            sizes.push_back(8);
            sizes.push_back(16);
            sizes.push_back(32);
            sizes.push_back(64);
            sizes.push_back(128);
        }

        if (arguments.read("--font",fontFilename)) {}

        std::string value;
        if (arguments.read("--min", value)) { readFilterMode(value, minFilter); }
        if (arguments.read("--mag", value)) { readFilterMode(value, magFilter); }


        if (arguments.read("--margin", glyphImageMargin)) {}
        if (arguments.read("--margin-ratio", glyphImageMarginRatio)) {}
        if (arguments.read("--interval", glyphInterval)) {}


        if (arguments.read("--outline")) backdropType = osgText::Text::OUTLINE;
        if (arguments.read("--shadow")) backdropType = osgText::Text::DROP_SHADOW_BOTTOM_RIGHT;

        float offset;
        if (arguments.read("--offset", offset)) backdropOffset.set(offset, offset);

        if (arguments.read("--text-color", textColor.r(), textColor.g(), textColor.b(), textColor.a())) {}
        if (arguments.read("--bd-color", backdropColor.r(), backdropColor.g(), backdropColor.b(), backdropColor.a())) {}
        if (arguments.read("--bg-color", backgroundColor.r(), backgroundColor.g(), backgroundColor.b(), backgroundColor.a())) {}

    }

    void setText(osgText::Text& text)
    {
        OSG_NOTICE<<"Settings::setText()"<<std::endl;
        text.setFont(fontFilename);
        text.setColor(textColor);
        text.setBackdropType(backdropType);
        text.setBackdropOffset(backdropOffset.x(), backdropOffset.y());
        text.setBackdropColor(backdropColor);
    }

    std::string                 fontFilename;
    osg::Texture::FilterMode    minFilter;
    osg::Texture::FilterMode    magFilter;
    unsigned int                glyphImageMargin;
    float                       glyphImageMarginRatio;
    int                         glyphInterval;

    osg::Vec4                   textColor;
    osgText::Text::BackdropType backdropType;
    osg::Vec2                   backdropOffset;
    osg::Vec4                   backdropColor;
    osg::Vec4                   backgroundColor;
    Sizes                       sizes;
};

osgText::Text* createLabel(const std::string& l, TextSettings& settings, unsigned int size)
{
    static osg::Vec3 pos(10.0f, 10.0f, 0.0f);

    osgText::Text* label = new osgText::Text();
    osg::ref_ptr<osgText::Font> font  = osgText::readRefFontFile(settings.fontFilename);

    font->setGlyphImageMargin(settings.glyphImageMargin);
    font->setGlyphImageMarginRatio(settings.glyphImageMarginRatio);
    font->setMinFilterHint(settings.minFilter);
    font->setMagFilterHint(settings.magFilter);

    settings.setText(*label);

    label->setCharacterSize(size);
    label->setFontResolution(size, size);
    label->setPosition(pos);
    label->setAlignment(osgText::Text::LEFT_BOTTOM);


    // It seems to be important we do this last to get best results?
    label->setText(l);


    // textInfo(label);

    pos.y() += size + 10.0f;
{'fraction_non_alphanumeric': 0.16288478452066843, 'fraction_numerical': 0.014072119613016711, 'mean_word_length': 2.772727272727273, 'pattern_counts': {'":': 0, '<': 139, '<?xml version=': 0, '>': 75, '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': '5872581', 'n_tokens_mistral': 3874, 'n_tokens_neox': 3692, 'n_words': 617}