Parcourir la source

* Exploded cucul/export.c

tags/v0.99.beta14
Jean-Yves Lamoureux jylam il y a 18 ans
Parent
révision
1971191c09
4 fichiers modifiés avec 235 ajouts et 165 suppressions
  1. +3
    -1
      cucul/Makefile.am
  2. +106
    -0
      cucul/exporter_ansi.c
  3. +5
    -164
      cucul/exporter_html.c
  4. +121
    -0
      cucul/exporter_irc.c

+ 3
- 1
cucul/Makefile.am Voir le fichier

@@ -16,7 +16,9 @@ libcucul_la_SOURCES = \
triangle.c \
sprite.c \
bitmap.c \
export.c \
exporter_irc.c \
exporter_ansi.c \
exporter_html.c \
$(NULL)
libcucul_la_LDFLAGS = -no-undefined
libcucul_la_LIBADD = @CUCUL_LIBS@


+ 106
- 0
cucul/exporter_ansi.c Voir le fichier

@@ -0,0 +1,106 @@
/*
* libcucul Unicode canvas library
* Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/** \file export.c
* \version \$Id: export.c 361 2006-03-09 13:24:06Z jylam $
* \author Sam Hocevar <sam@zoy.org>
* \author Jean-Yves Lamoureux <jylam@lnxscene.org>
* \brief Export function
*
* This file contains export functions for ANSI
*/

#include "config.h"

#if !defined(__KERNEL__)
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
#endif

#include "cucul.h"
#include "cucul_internals.h"



/** \brief Generate ANSI representation of current image.
*
* This function generates and returns an ANSI representation of
* the current image.
* \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
* able to cut/paste the result to a function like printf
* \return buffer containing generated ANSI codes as a big string
*/
char * cucul_get_ansi(cucul_t *qq, int trailing, int *size)
{
static int const palette[] =
{
30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
};

char *cur;
unsigned int x, y;

/* 20 bytes assumed for max length per pixel.
* Add height*9 to that (zeroes color at the end and jump to next line) */
if(qq->ansi_buffer)
free(qq->ansi_buffer);
qq->ansi_buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
if(qq->ansi_buffer == NULL)
return NULL;

cur = qq->ansi_buffer;

// *cur++ = '';

for(y = 0; y < qq->height; y++)
{
uint8_t *lineattr = qq->attr + y * qq->width;
uint32_t *linechar = qq->chars + y * qq->width;

uint8_t prevfg = -1;
uint8_t prevbg = -1;

for(x = 0; x < qq->width; x++)
{
uint8_t fg = palette[lineattr[x] & 0x0f];
uint8_t bg = (palette[lineattr[x] >> 4])+10;
uint32_t c = linechar[x];

if(!trailing)
cur += sprintf(cur, "\033[");
else
cur += sprintf(cur, "\\033[");

if(fg > 7)
cur += sprintf(cur, "1;%d;%dm",fg,bg);
else
cur += sprintf(cur, "0;%d;%dm",fg,bg);
*cur++ = c & 0x7f;
if((c == '%') && trailing)
*cur++ = c & 0x7f;
prevfg = fg;
prevbg = bg;
}
if(!trailing)
cur += sprintf(cur, "\033[0m\n\r");
else
cur += sprintf(cur, "\\033[0m\\n\n");
}

/* Crop to really used size */
*size = (strlen(qq->ansi_buffer) + 1)* sizeof(char);
qq->ansi_buffer = realloc(qq->ansi_buffer, *size);

return qq->ansi_buffer;
}


cucul/export.c → cucul/exporter_html.c Voir le fichier

@@ -10,13 +10,12 @@
*/

/** \file export.c
* \version \$Id$
* \version \$Id: export.c 361 2006-03-09 13:24:06Z jylam $
* \author Sam Hocevar <sam@zoy.org>
* \author Jean-Yves Lamoureux <jylam@lnxscene.org>
* \brief Export function
*
* This file contains export functions for various file formats such
* as HTML or IRC.
* This file contains export functions for HTML and HTML3
*/

#include "config.h"
@@ -30,6 +29,8 @@
#include "cucul.h"
#include "cucul_internals.h"



/* HTML */

/** \brief Generate HTML representation of current image.
@@ -109,6 +110,7 @@ char* cucul_get_html(cucul_t *qq, int *size)
return qq->html_buffer;
}


/** \brief Generate HTML3 representation of current image.
*
* This function generates and returns the HTML3 representation of
@@ -193,165 +195,4 @@ char* cucul_get_html3(cucul_t *qq, int *size)
return qq->html3_buffer;
}

/** \brief Generate IRC representation of current image.
*
* This function generates and returns an IRC representation of
* the current image.
*/
char* cucul_get_irc(cucul_t *qq, int *size)
{
static int const palette[] =
{
1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
14, 12, 9, 11, 4, 13, 8, 0, /* Light */
};

char *cur;
unsigned int x, y;

/* 11 bytes assumed for max length per pixel. Worst case scenario:
* ^Cxx,yy 6 bytes
* ^B^B 2 bytes
* c 1 byte
* \r\n 2 bytes
* In real life, the average bytes per pixel value will be around 5.
*/

if(qq->irc_buffer)
free(qq->irc_buffer);

qq->irc_buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char));
if(qq->irc_buffer == NULL)
return NULL;

cur = qq->irc_buffer;

*cur++ = '\x0f';

for(y = 0; y < qq->height; y++)
{
uint8_t *lineattr = qq->attr + y * qq->width;
uint32_t *linechar = qq->chars + y * qq->width;

uint8_t prevfg = -1;
uint8_t prevbg = -1;

for(x = 0; x < qq->width; x++)
{
uint8_t fg = palette[lineattr[x] & 0x0f];
uint8_t bg = palette[lineattr[x] >> 4];
uint32_t c = linechar[x];

if(bg == prevbg)
{
if(fg == prevfg)
; /* Same fg/bg, do nothing */
else if(c == (uint32_t)' ')
fg = prevfg; /* Hackety hack */
else
{
cur += sprintf(cur, "\x03%d", fg);
if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
cur += sprintf(cur, "\x02\x02");
}
}
else
{
if(fg == prevfg)
cur += sprintf(cur, "\x03,%d", bg);
else
cur += sprintf(cur, "\x03%d,%d", fg, bg);

if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
cur += sprintf(cur, "\x02\x02");
}
*cur++ = c & 0x7f;
prevfg = fg;
prevbg = bg;
}
*cur++ = '\r';
*cur++ = '\n';
}

*cur++ = '\x0f';

/* Crop to really used size */
*size = (strlen(qq->irc_buffer) + 1) * sizeof(char);
qq->irc_buffer = realloc(qq->irc_buffer, *size);
return qq->irc_buffer;
}

/** \brief Generate ANSI representation of current image.
*
* This function generates and returns an ANSI representation of
* the current image.
* \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
* able to cut/paste the result to a function like printf
* \return buffer containing generated ANSI codes as a big string
*/
char * cucul_get_ansi(cucul_t *qq, int trailing, int *size)
{
static int const palette[] =
{
30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
};

char *cur;
unsigned int x, y;

/* 20 bytes assumed for max length per pixel.
* Add height*9 to that (zeroes color at the end and jump to next line) */
if(qq->ansi_buffer)
free(qq->ansi_buffer);
qq->ansi_buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
if(qq->ansi_buffer == NULL)
return NULL;

cur = qq->ansi_buffer;

// *cur++ = '';

for(y = 0; y < qq->height; y++)
{
uint8_t *lineattr = qq->attr + y * qq->width;
uint32_t *linechar = qq->chars + y * qq->width;

uint8_t prevfg = -1;
uint8_t prevbg = -1;

for(x = 0; x < qq->width; x++)
{
uint8_t fg = palette[lineattr[x] & 0x0f];
uint8_t bg = (palette[lineattr[x] >> 4])+10;
uint32_t c = linechar[x];

if(!trailing)
cur += sprintf(cur, "\033[");
else
cur += sprintf(cur, "\\033[");

if(fg > 7)
cur += sprintf(cur, "1;%d;%dm",fg,bg);
else
cur += sprintf(cur, "0;%d;%dm",fg,bg);
*cur++ = c & 0x7f;
if((c == '%') && trailing)
*cur++ = c & 0x7f;
prevfg = fg;
prevbg = bg;
}
if(!trailing)
cur += sprintf(cur, "\033[0m\n\r");
else
cur += sprintf(cur, "\\033[0m\\n\n");
}

/* Crop to really used size */
*size = (strlen(qq->ansi_buffer) + 1)* sizeof(char);
qq->ansi_buffer = realloc(qq->ansi_buffer, *size);

return qq->ansi_buffer;
}


+ 121
- 0
cucul/exporter_irc.c Voir le fichier

@@ -0,0 +1,121 @@
/*
* libcucul Unicode canvas library
* Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Do What The Fuck You Want To
* Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/

/** \file export.c
* \version \$Id: export.c 361 2006-03-09 13:24:06Z jylam $
* \author Sam Hocevar <sam@zoy.org>
* \author Jean-Yves Lamoureux <jylam@lnxscene.org>
* \brief Export function
*
* This file contains export functions for IRC
*/

#include "config.h"

#if !defined(__KERNEL__)
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
#endif

#include "cucul.h"
#include "cucul_internals.h"



/** \brief Generate IRC representation of current image.
*
* This function generates and returns an IRC representation of
* the current image.
*/
char* cucul_get_irc(cucul_t *qq, int *size)
{
static int const palette[] =
{
1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
14, 12, 9, 11, 4, 13, 8, 0, /* Light */
};

char *cur;
unsigned int x, y;

/* 11 bytes assumed for max length per pixel. Worst case scenario:
* ^Cxx,yy 6 bytes
* ^B^B 2 bytes
* c 1 byte
* \r\n 2 bytes
* In real life, the average bytes per pixel value will be around 5.
*/

if(qq->irc_buffer)
free(qq->irc_buffer);

qq->irc_buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char));
if(qq->irc_buffer == NULL)
return NULL;

cur = qq->irc_buffer;

*cur++ = '\x0f';

for(y = 0; y < qq->height; y++)
{
uint8_t *lineattr = qq->attr + y * qq->width;
uint32_t *linechar = qq->chars + y * qq->width;

uint8_t prevfg = -1;
uint8_t prevbg = -1;

for(x = 0; x < qq->width; x++)
{
uint8_t fg = palette[lineattr[x] & 0x0f];
uint8_t bg = palette[lineattr[x] >> 4];
uint32_t c = linechar[x];

if(bg == prevbg)
{
if(fg == prevfg)
; /* Same fg/bg, do nothing */
else if(c == (uint32_t)' ')
fg = prevfg; /* Hackety hack */
else
{
cur += sprintf(cur, "\x03%d", fg);
if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
cur += sprintf(cur, "\x02\x02");
}
}
else
{
if(fg == prevfg)
cur += sprintf(cur, "\x03,%d", bg);
else
cur += sprintf(cur, "\x03%d,%d", fg, bg);

if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
cur += sprintf(cur, "\x02\x02");
}
*cur++ = c & 0x7f;
prevfg = fg;
prevbg = bg;
}
*cur++ = '\r';
*cur++ = '\n';
}

*cur++ = '\x0f';

/* Crop to really used size */
*size = (strlen(qq->irc_buffer) + 1) * sizeof(char);
qq->irc_buffer = realloc(qq->irc_buffer, *size);
return qq->irc_buffer;
}

Chargement…
Annuler
Enregistrer