vaseboot/util/render-label.c

216 lines
5.5 KiB
C

/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <VasEBoot/util/misc.h>
#include <VasEBoot/util/install.h>
#include <VasEBoot/i18n.h>
#include <VasEBoot/term.h>
#include <VasEBoot/font.h>
#include <VasEBoot/gfxmenu_view.h>
#include <VasEBoot/color.h>
#include <VasEBoot/emu/hostdisk.h>
#define _GNU_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
struct header
{
VasEBoot_uint8_t magic;
VasEBoot_uint16_t width;
VasEBoot_uint16_t height;
} VasEBoot_PACKED;
static struct VasEBoot_video_palette_data ieee1275_palette[256];
void
VasEBoot_util_render_label (const char *label_font,
const char *label_bgcolor,
const char *label_color,
const char *text,
const char *output)
{
struct header head;
FILE *out;
int i, j, k, cptr = 0;
VasEBoot_font_t font;
char *fontfull;
const VasEBoot_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
const VasEBoot_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
0x77, 0x66, 0x3f, 0x27 };
int width, height;
VasEBoot_uint8_t bg, fg;
struct VasEBoot_video_mode_info mode_info;
VasEBoot_err_t err;
VasEBoot_video_rgba_color_t fgcolor;
VasEBoot_video_rgba_color_t bgcolor;
if (output)
out = VasEBoot_util_fopen (output, "wb");
else
out = stdout;
if (!out)
{
VasEBoot_util_error (_("cannot open `%s': %s"), output ? : "stdout",
strerror (errno));
}
if (label_color)
{
err = VasEBoot_video_parse_color (label_color, &fgcolor);
if (err)
VasEBoot_util_error (_("invalid color specification `%s'"), label_color);
}
else
{
fgcolor.red = 0x00;
fgcolor.green = 0x00;
fgcolor.blue = 0x00;
fgcolor.alpha = 0xff;
}
if (label_bgcolor)
{
err = VasEBoot_video_parse_color (label_bgcolor, &bgcolor);
if (err)
VasEBoot_util_error (_("invalid color specification `%s'"), label_bgcolor);
}
else
{
bgcolor.red = 0xff;
bgcolor.green = 0xff;
bgcolor.blue = 0xff;
bgcolor.alpha = 0xff;
}
for (i = 0; i < 256; i++)
ieee1275_palette[i].a = 0xff;
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
for (k = 0; k < 6; k++)
{
ieee1275_palette[cptr].r = vals[i];
ieee1275_palette[cptr].g = vals[j];
ieee1275_palette[cptr].b = vals[k];
ieee1275_palette[cptr].a = 0xff;
cptr++;
}
cptr--;
for (i = 0; i < 10; i++)
{
ieee1275_palette[cptr].r = vals2[i];
ieee1275_palette[cptr].g = 0;
ieee1275_palette[cptr].b = 0;
ieee1275_palette[cptr].a = 0xff;
cptr++;
}
for (i = 0; i < 10; i++)
{
ieee1275_palette[cptr].r = 0;
ieee1275_palette[cptr].g = vals2[i];
ieee1275_palette[cptr].b = 0;
ieee1275_palette[cptr].a = 0xff;
cptr++;
}
for (i = 0; i < 10; i++)
{
ieee1275_palette[cptr].r = 0;
ieee1275_palette[cptr].g = 0;
ieee1275_palette[cptr].b = vals2[i];
ieee1275_palette[cptr].a = 0xff;
cptr++;
}
for (i = 0; i < 10; i++)
{
ieee1275_palette[cptr].r = vals2[i];
ieee1275_palette[cptr].g = vals2[i];
ieee1275_palette[cptr].b = vals2[i];
ieee1275_palette[cptr].a = 0xff;
cptr++;
}
ieee1275_palette[cptr].r = 0;
ieee1275_palette[cptr].g = 0;
ieee1275_palette[cptr].b = 0;
ieee1275_palette[cptr].a = 0xff;
char * t;
t = VasEBoot_canonicalize_file_name (label_font);
if (!t)
{
VasEBoot_util_error (_("cannot open `%s': %s"), label_font,
strerror (errno));
}
fontfull = xasprintf ("(host)/%s", t);
free (t);
VasEBoot_font_loader_init ();
font = VasEBoot_font_load (fontfull);
if (!font)
{
VasEBoot_util_error (_("cannot open `%s': %s"), label_font,
VasEBoot_errmsg);
}
width = VasEBoot_font_get_string_width (font, text) + 10;
height = VasEBoot_font_get_height (font);
mode_info.width = width;
mode_info.height = height;
mode_info.pitch = width;
mode_info.mode_type = VasEBoot_VIDEO_MODE_TYPE_INDEX_COLOR;
mode_info.bpp = 8;
mode_info.bytes_per_pixel = 1;
mode_info.number_of_colors = 256;
VasEBoot_video_capture_start (&mode_info, ieee1275_palette,
ARRAY_SIZE (ieee1275_palette));
fg = VasEBoot_video_map_rgb (fgcolor.red,
fgcolor.green,
fgcolor.blue);
bg = VasEBoot_video_map_rgb (bgcolor.red,
bgcolor.green,
bgcolor.blue);
VasEBoot_memset (VasEBoot_video_capture_get_framebuffer (), bg, height * width);
VasEBoot_font_draw_string (text, font, fg,
5, VasEBoot_font_get_ascent (font));
head.magic = 1;
head.width = VasEBoot_cpu_to_be16 (width);
head.height = VasEBoot_cpu_to_be16 (height);
fwrite (&head, 1, sizeof (head), out);
fwrite (VasEBoot_video_capture_get_framebuffer (), 1, width * height, out);
VasEBoot_video_capture_end ();
if (out != stdout)
fclose (out);
free (fontfull);
}