<commit_before>#include <Elementary.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

#include "private.h"
#include "miniview.h"
#include "col.h"
#include "termpty.h"
#include "termio.h"
#include "utils.h"
#include "main.h"

/* specific log domain to help debug only miniview */
int _miniview_log_dom = -1;

#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG

#define CRIT(...)     EINA_LOG_DOM_CRIT(_miniview_log_dom, __VA_ARGS__)
#define ERR(...)      EINA_LOG_DOM_ERR(_miniview_log_dom, __VA_ARGS__)
#define WRN(...)      EINA_LOG_DOM_WARN(_miniview_log_dom, __VA_ARGS__)
#define INF(...)      EINA_LOG_DOM_INFO(_miniview_log_dom, __VA_ARGS__)
#define DBG(...)      EINA_LOG_DOM_DBG(_miniview_log_dom, __VA_ARGS__)

static Eina_Bool _deferred_renderer(void *data);

void
miniview_init(void)
{
   if (_miniview_log_dom >= 0) return;

   _miniview_log_dom = eina_log_domain_register("miniview", NULL);
   if (_miniview_log_dom < 0)
     EINA_LOG_CRIT(_("Could not create logging domain '%s'."), "miniview");
}

void
miniview_shutdown(void)
{
   if (_miniview_log_dom < 0) return;
   eina_log_domain_unregister(_miniview_log_dom);
   _miniview_log_dom = -1;
}

typedef struct _Miniview Miniview;

struct _Miniview
{
   Evas_Object *self;
   Evas_Object *base;
   Evas_Object *img;
   Evas_Object *termio;

   int img_hist; /* history rendered is between img_hist (<0) and
                    img_hist + img_h */
   unsigned img_h;
   unsigned rows;
   unsigned cols;

   Ecore_Timer *deferred_renderer;

   unsigned int is_shown : 1;
   unsigned int to_render : 1;
   unsigned int initial_pos : 1;

   Eina_Bool fits_to_img;

   struct _screen {
      double size;
      double pos_val;
   }screen;
};

static Evas_Smart *_smart = NULL;

static void
_draw_cell(const Termpty *ty, unsigned int *pixel, const Termcell *cell, unsigned int *colors)
{
   int fg, bg, fgext, bgext;
   int inv = ty->state.reverse;
   Eina_Unicode codepoint;

   codepoint = cell->codepoint;
   if ((codepoint == 0) || (cell->att.newline) || (cell->att.invisible))
     {
        *pixel = 0;
        return;
     }
   // colors
   fg = cell->att.fg;
   bg = cell->att.bg;
   fgext = cell->att.fg256;
   bgext = cell->att.bg256;

   if ((fg == COL_DEF) && (cell->att.inverse ^ inv)) fg = COL_INVERSEBG;
   if (bg == COL_DEF)
     {
        if (cell->att.inverse ^ inv) bg = COL_INVERSE;
        else if (!bgext) bg = COL_INVIS;
     }
   if ((cell->att.fgintense) && (!fgext)) fg += 48;
   if ((cell->att.bgintense) && (!bgext)) bg += 48;
   if (cell->att.inverse ^ inv)
     {
        int t;
        t = fgext; fgext = bgext; bgext = t;
        t = fg; fg = bg; bg = t;
     }
   if ((cell->att.bold) && (!fgext)) fg += 12;
   if ((cell->att.faint) && (!fgext)) fg += 24;
   
   if (bgext) *pixel = colors[bg + 256];
   else if (bg && ((bg % 12) != COL_INVIS)) *pixel = colors[bg];
   else if ((codepoint > 32) && (codepoint < 0x00110000) &&
            (!isspace(codepoint)))
     {
        if (fgext) *pixel = colors[fg + 256];
        else *pixel = colors[fg];
     }
   else
     *pixel = 0;
}

static void
_draw_line(const Termpty *ty, unsigned int *pixels,
           const Termcell *cells, int length, unsigned int *colors)
{
   int x;

   for (x = 0 ; x < length; x++)
     {
        _draw_cell(ty, pixels + x, cells + x, colors);<commit_msg>fix segfault in miniview due to large codepoints<commit_after>#include <Elementary.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>

#include "private.h"
#include "miniview.h"
#include "col.h"
#include "termpty.h"
#include "termio.h"
#include "utils.h"
#include "main.h"

/* specific log domain to help debug only miniview */
int _miniview_log_dom = -1;

#undef CRITICAL
#undef ERR
#undef WRN
#undef INF
#undef DBG

#define CRIT(...)     EINA_LOG_DOM_CRIT(_miniview_log_dom, __VA_ARGS__)
#define ERR(...)      EINA_LOG_DOM_ERR(_miniview_log_dom, __VA_ARGS__)
#define WRN(...)      EINA_LOG_DOM_WARN(_miniview_log_dom, __VA_ARGS__)
#define INF(...)      EINA_LOG_DOM_INFO(_miniview_log_dom, __VA_ARGS__)
#define DBG(...)      EINA_LOG_DOM_DBG(_miniview_log_dom, __VA_ARGS__)

static Eina_Bool _deferred_renderer(void *data);

void
miniview_init(void)
{
   if (_miniview_log_dom >= 0) return;

   _miniview_log_dom = eina_log_domain_register("miniview", NULL);
   if (_miniview_log_dom < 0)
     EINA_LOG_CRIT(_("Could not create logging domain '%s'."), "miniview");
}

void
miniview_shutdown(void)
{
   if (_miniview_log_dom < 0) return;
   eina_log_domain_unregister(_miniview_log_dom);
   _miniview_log_dom = -1;
}

typedef struct _Miniview Miniview;

struct _Miniview
{
   Evas_Object *self;
   Evas_Object *base;
   Evas_Object *img;
   Evas_Object *termio;

   int img_hist; /* history rendered is between img_hist (<0) and
                    img_hist + img_h */
   unsigned img_h;
   unsigned rows;
   unsigned cols;

   Ecore_Timer *deferred_renderer;

   unsigned int is_shown : 1;
   unsigned int to_render : 1;
   unsigned int initial_pos : 1;

   Eina_Bool fits_to_img;

   struct _screen {
      double size;
      double pos_val;
   }screen;
};

static Evas_Smart *_smart = NULL;

static void
_draw_cell(const Termpty *ty, unsigned int *pixel, const Termcell *cell, unsigned int *colors)
{
   int fg, bg, fgext, bgext;
   int inv = ty->state.reverse;
   Eina_Unicode codepoint;

   codepoint = cell->codepoint;
   if ((codepoint == 0) || (cell->att.newline) || (cell->att.invisible))
     {
        *pixel = 0;
        return;
     }
   // colors
   fg = cell->att.fg;
   bg = cell->att.bg;
   fgext = cell->att.fg256;
   bgext = cell->att.bg256;

   if ((fg == COL_DEF) && (cell->att.inverse ^ inv)) fg = COL_INVERSEBG;
   if (bg == COL_DEF)
     {
        if (cell->att.inverse ^ inv) bg = COL_INVERSE;
        else if (!bgext) bg = COL_INVIS;
     }
   if ((cell->att.fgintense) && (!fgext)) fg += 48;
   if ((cell->att.bgintense) && (!bgext)) bg += 48;
   if (cell->att.inverse ^ inv)
     {
        int t;
        t = fgext; fgext = bgext; bgext = t;
        t = fg; fg = bg; bg = t;
     }
   if ((cell->att.bold) && (!fgext)) fg += 12;
   if ((cell->att.faint) && (!fgext)) fg += 24;
   
   if (bgext) *pixel = colors[bg + 256];
   else if (bg && ((bg % 12) != COL_INVIS)) *pixel = colors[bg];
   else if ((codepoint > 32) && (codepoint < 0x00110000))
     {
        if (fgext) *pixel = colors[fg + 256];
        else *pixel = colors[fg];
     }
   else
     *pixel = 0;
}

static void
_draw_line(const Termpty *ty, unsigned int *pixels,
           const Termcell *cells, int length, unsigned int *colors)
{
   int x;

   for (x = 0 ; x < length; x++)
     {
        _draw_cell(ty, pixels + x, cells + x, colors);
{'fraction_non_alphanumeric': 0.14035874439461885, 'fraction_numerical': 0.013751868460388639, 'mean_word_length': 3.0210336538461537, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 45, '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': '2532800', 'n_tokens_mistral': 2713, 'n_tokens_neox': 2604, 'n_words': 633}