<commit_before>package org.liquidengine.legui.system.renderer.nvg.component;

import static org.liquidengine.legui.style.color.ColorUtil.oppositeBlackOrWhite;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgColorUtil.rgba;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.alignTextInBox;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.calculateTextBoundsRect;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.createScissor;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.intersectScissor;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.resetScissor;
import static org.lwjgl.nanovg.NanoVG.nnvgTextGlyphPositions;
import static org.lwjgl.nanovg.NanoVG.nvgFillColor;
import static org.lwjgl.nanovg.NanoVG.nvgFontFace;
import static org.lwjgl.nanovg.NanoVG.nvgFontSize;
import static org.lwjgl.system.MemoryUtil.memAddress;
import static org.lwjgl.system.MemoryUtil.memFree;
import static org.lwjgl.system.MemoryUtil.memUTF8;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.joml.Vector2f;
import org.joml.Vector4f;
import org.liquidengine.legui.component.TextArea;
import org.liquidengine.legui.component.optional.TextState;
import org.liquidengine.legui.component.optional.align.HorizontalAlign;
import org.liquidengine.legui.component.optional.align.VerticalAlign;
import org.liquidengine.legui.input.Mouse;
import org.liquidengine.legui.system.context.Context;
import org.liquidengine.legui.system.renderer.nvg.util.NvgShapes;
import org.liquidengine.legui.system.renderer.nvg.util.NvgText;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.nanovg.NVGGlyphPosition;

/**
 * NanoVG Text area renderer.
 */
public class NvgTextAreaRenderer extends NvgDefaultComponentRenderer<TextArea> {

    public static final String NEWLINE = "\n";
    private static final String PRATIO = "pratio";
    private static final String POFFSETX = "poffsetx";
    private static final String PHALIGN = "phalign";
    private static final String PVALIGN = "pvalign";
    private static final String POFFSETY = "poffsety";
    private static final String TABS = "\t";
    private static final String SPACES = " ";
    private static final char SPACEC = ' ';
    private final Vector4f caretColor = new Vector4f(0, 0, 0, 0.5f);
    private final int maxGlyphCount = 2048;

    @Override
    public void renderSelf(TextArea component, Context context, long nanovg) {
        createScissor(nanovg, component);
        {
            Vector2f pos = component.getAbsolutePosition();
            Vector2f size = component.getSize();
            Vector4f bc = new Vector4f(component.getStyle().getBackground().getColor());

            renderBackground(component, context, nanovg);

            TextState textState = component.getTextState();
            Vector4f p = new Vector4f(textState.getPadding()).add(2, 2, 2, 2);
            Vector4f intersectRect = new Vector4f(pos.x + p.x, pos.y + p.y, size.x - p.x - p.z, size.y - p.y - p.w);

            intersectScissor(nanovg, new Vector4f(intersectRect).sub(1, 1, -2, -2));
            if (component.getTextState().getText() != null && !component.getTextState().getText().isEmpty()) {
                renderText(context, nanovg, component, size, intersectRect, bc);
            }
        }
        resetScissor(nanovg);
    }

    private void renderText(Context leguiContext, long context, TextArea gui, Vector2f size, Vector4f rect, Vector4f bc) {

        NVGGlyphPosition.Buffer glyphs = NVGGlyphPosition.calloc(maxGlyphCount);

        TextState textState = gui.getTextState();
        String text = textState.getText();
        String font = textState.getFont();
        float fontSize = textState.getFontSize();
        HorizontalAlign halign = textState.getHorizontalAlign();
        VerticalAlign valign = textState.getVerticalAlign();
        Vector4f textColor = textState.getTextColor();<commit_msg>fixed caret rendering in text area<commit_after>package org.liquidengine.legui.system.renderer.nvg.component;

import static org.liquidengine.legui.style.color.ColorUtil.oppositeBlackOrWhite;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgColorUtil.rgba;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.alignTextInBox;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.calculateTextBoundsRect;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.createScissor;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.intersectScissor;
import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.resetScissor;
import static org.lwjgl.nanovg.NanoVG.nnvgTextGlyphPositions;
import static org.lwjgl.nanovg.NanoVG.nvgFillColor;
import static org.lwjgl.nanovg.NanoVG.nvgFontFace;
import static org.lwjgl.nanovg.NanoVG.nvgFontSize;
import static org.lwjgl.system.MemoryUtil.memAddress;
import static org.lwjgl.system.MemoryUtil.memFree;
import static org.lwjgl.system.MemoryUtil.memUTF8;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.joml.Vector2f;
import org.joml.Vector4f;
import org.liquidengine.legui.component.TextArea;
import org.liquidengine.legui.component.optional.TextState;
import org.liquidengine.legui.component.optional.align.HorizontalAlign;
import org.liquidengine.legui.component.optional.align.VerticalAlign;
import org.liquidengine.legui.input.Mouse;
import org.liquidengine.legui.system.context.Context;
import org.liquidengine.legui.system.renderer.nvg.util.NvgShapes;
import org.liquidengine.legui.system.renderer.nvg.util.NvgText;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.nanovg.NVGColor;
import org.lwjgl.nanovg.NVGGlyphPosition;

/**
 * NanoVG Text area renderer.
 */
public class NvgTextAreaRenderer extends NvgDefaultComponentRenderer<TextArea> {

    public static final String NEWLINE = "\n";
    private static final String PRATIO = "pratio";
    private static final String POFFSETX = "poffsetx";
    private static final String PHALIGN = "phalign";
    private static final String PVALIGN = "pvalign";
    private static final String POFFSETY = "poffsety";
    private static final String TABS = "\t";
    private static final String SPACES = " ";
    private static final char SPACEC = ' ';
    private final Vector4f caretColor = new Vector4f(0, 0, 0, 0.5f);
    private final int maxGlyphCount = 2048;

    @Override
    public void renderSelf(TextArea component, Context context, long nanovg) {
        createScissor(nanovg, component);
        {
            Vector2f pos = component.getAbsolutePosition();
            Vector2f size = component.getSize();
            Vector4f bc = new Vector4f(component.getStyle().getBackground().getColor());

            renderBackground(component, context, nanovg);

            TextState textState = component.getTextState();
            Vector4f p = new Vector4f(textState.getPadding()).add(2, 2, 2, 2);
            Vector4f intersectRect = new Vector4f(pos.x + p.x, pos.y + p.y, size.x - p.x - p.z, size.y - p.y - p.w);

            intersectScissor(nanovg, new Vector4f(intersectRect).sub(1, 1, -2, -2));
            if (component.getTextState().getText() != null) {
                renderText(context, nanovg, component, size, intersectRect, bc);
            }
        }
        resetScissor(nanovg);
    }

    private void renderText(Context leguiContext, long context, TextArea gui, Vector2f size, Vector4f rect, Vector4f bc) {

        NVGGlyphPosition.Buffer glyphs = NVGGlyphPosition.calloc(maxGlyphCount);

        TextState textState = gui.getTextState();
        String text = textState.getText();
        String font = textState.getFont();
        float fontSize = textState.getFontSize();
        HorizontalAlign halign = textState.getHorizontalAlign();
        VerticalAlign valign = textState.getVerticalAlign();
        Vector4f textColor = textState.getTextColor();
{'fraction_non_alphanumeric': 0.10319590143937546, 'fraction_numerical': 0.008538667967797023, 'mean_word_length': 4.064237183446572, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4158853', 'n_tokens_mistral': 2705, 'n_tokens_neox': 2587, 'n_words': 517}