mirror of
https://git.familie-radermacher.ch/linux/ptouch-print.git
synced 2025-11-21 04:42:04 +00:00
Initial commit
This commit is contained in:
287
src/libptouch.c
Normal file
287
src/libptouch.c
Normal file
@@ -0,0 +1,287 @@
|
||||
/*
|
||||
libptouch - functions to help accessing a brother ptouch
|
||||
|
||||
Copyright (C) 2013 Dominic Radermacher <dominic.radermacher@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License version 3 as
|
||||
published by the Free Software Foundation
|
||||
|
||||
This program 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 this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h> /* malloc() */
|
||||
#include <string.h> /* memcmp() */
|
||||
#include <sys/types.h> /* open() */
|
||||
#include <sys/stat.h> /* open() */
|
||||
#include <fcntl.h> /* open() */
|
||||
#include <time.h> /* nanosleep(), struct timespec */
|
||||
#include "config.h"
|
||||
#include "gettext.h" /* gettext(), ngettext() */
|
||||
#include "ptouch.h"
|
||||
|
||||
#define _(s) gettext(s)
|
||||
|
||||
struct _pt_tape_info tape_info[5]= {
|
||||
{9, 52}, /* 9mm tape is 52px wide? works for me ;-) */
|
||||
{12,76}, /* and 76px work for me on a 12mm tape - maybe its only 64px */
|
||||
{18,120},
|
||||
{24,128},
|
||||
{0,0} /* terminating entry */
|
||||
};
|
||||
|
||||
struct _pt_dev_info ptdevs[] = {
|
||||
{0x04f9, 0x202d, "PT-2430PC", 128, 0}, /* 180dpi, maximum 128px */
|
||||
{0x04f9, 0x202c, "PT-1230PC", 76, 0}, /* 180dpi, supports tapes up to 12mm - I don't know how much pixels it can print! */
|
||||
{0,0,"",0,0}
|
||||
};
|
||||
|
||||
void ptouch_rawstatus(uint8_t raw[32]);
|
||||
|
||||
int ptouch_open(ptouch_dev *ptdev)
|
||||
{
|
||||
libusb_device **devs;
|
||||
libusb_device *dev;
|
||||
libusb_device_handle *handle = NULL;
|
||||
struct libusb_device_descriptor desc;
|
||||
ssize_t cnt;
|
||||
int r,i=0;
|
||||
|
||||
if ((*ptdev=malloc(sizeof(struct _ptouch_dev))) == NULL) {
|
||||
fprintf(stderr, _("out of memory\n"));
|
||||
return -1;
|
||||
}
|
||||
if ((libusb_init(NULL)) < 0) {
|
||||
fprintf(stderr, _("libusb_init() failed\n"));
|
||||
return -1;
|
||||
}
|
||||
// libusb_set_debug(NULL, 3);
|
||||
if ((cnt=libusb_get_device_list(NULL, &devs)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
while ((dev=devs[i++]) != NULL) {
|
||||
if ((r=libusb_get_device_descriptor(dev, &desc)) < 0) {
|
||||
fprintf(stderr, _("failed to get device descriptor"));
|
||||
libusb_free_device_list(devs, 1);
|
||||
return -1;
|
||||
}
|
||||
for (int k=0; ptdevs[k].vid > 0; k++) {
|
||||
if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
|
||||
fprintf(stderr, _("%s found on USB bus %d, device %d\n"),
|
||||
ptdevs[k].name,
|
||||
libusb_get_bus_number(dev),
|
||||
libusb_get_device_address(dev));
|
||||
if ((r=libusb_open(dev, &handle)) != 0) {
|
||||
fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
|
||||
return -1;
|
||||
}
|
||||
libusb_free_device_list(devs, 1);
|
||||
if ((r=libusb_kernel_driver_active(handle, 0)) == 1) {
|
||||
if ((r=libusb_detach_kernel_driver(handle, 0)) != 0) {
|
||||
fprintf(stderr, _("error while detaching kernel driver: %s\n"), libusb_error_name(r));
|
||||
}
|
||||
}
|
||||
if ((r=libusb_claim_interface(handle, 0)) != 0) {
|
||||
fprintf(stderr, _("interface claim error: %s\n"), libusb_error_name(r));
|
||||
return -1;
|
||||
}
|
||||
(*ptdev)->h=handle;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
|
||||
libusb_free_device_list(devs, 1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ptouch_close(ptouch_dev ptdev)
|
||||
{
|
||||
libusb_release_interface(ptdev->h, 0);
|
||||
libusb_close(ptdev->h);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ptouch_send(ptouch_dev ptdev, uint8_t *data, int len)
|
||||
{
|
||||
int r,tx;
|
||||
|
||||
if (ptdev == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if ((r=libusb_bulk_transfer(ptdev->h, 0x02, data, len, &tx, 0)) != 0) {
|
||||
fprintf(stderr, _("write error: %s\n"), libusb_error_name(r));
|
||||
return -1;
|
||||
}
|
||||
if (tx != len) {
|
||||
fprintf(stderr, _("write error: could send only %i of %i bytes\n"), tx, len);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ptouch_init(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x1b\x40"; /* 1B 40 = ESC @ = INIT */
|
||||
return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
}
|
||||
|
||||
int ptouch_rasterstart(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x1b\x69\x52\x01"; /* 1B 69 52 01 = RASTER DATA */
|
||||
return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
}
|
||||
|
||||
/* print an empty line */
|
||||
int ptouch_lf(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x5a";
|
||||
return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
}
|
||||
|
||||
/* print and advance tape, but do not cut */
|
||||
int ptouch_ff(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x0c";
|
||||
return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
}
|
||||
|
||||
/* print and cut tape */
|
||||
int ptouch_eject(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x1a";
|
||||
return ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
}
|
||||
|
||||
/* print a "cut here" mark (it's just a dashed line) */
|
||||
#define CUTMARK_SPACING 5
|
||||
int ptouch_cutmark(ptouch_dev ptdev)
|
||||
{
|
||||
uint8_t buf[32];
|
||||
int i,len=16;
|
||||
|
||||
for (i=0; i<CUTMARK_SPACING; i++) {
|
||||
ptouch_lf(ptdev);
|
||||
}
|
||||
ptouch_rasterstart(ptdev);
|
||||
buf[0]=0x47;
|
||||
buf[1]=len;
|
||||
buf[2]=0;
|
||||
memset(buf+3, 0, len);
|
||||
int offset=(64-ptouch_getmaxwidth(ptdev)/2);
|
||||
for (i=0; i<ptouch_getmaxwidth(ptdev); i++) {
|
||||
if ((i%8) <= 3) { /* pixels 0-3 get set, 4-7 are unset */
|
||||
buf[3+15-((offset+i)/8)] |= 1<<((offset+i)%8);
|
||||
}
|
||||
}
|
||||
ptouch_send(ptdev, buf, len+3);
|
||||
for (i=0; i<CUTMARK_SPACING; i++) {
|
||||
ptouch_lf(ptdev);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ptouch_rawstatus(uint8_t raw[32])
|
||||
{
|
||||
fprintf(stderr, _("debug: dumping raw status bytes\n"));
|
||||
for (int i=0; i<32; i++) {
|
||||
fprintf(stderr, "%02x ", raw[i]);
|
||||
if (((i+1) % 16) == 0) {
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
return;
|
||||
}
|
||||
|
||||
int ptouch_getstatus(ptouch_dev ptdev)
|
||||
{
|
||||
char cmd[]="\x1b\x69\x53";
|
||||
uint8_t buf[32];
|
||||
int i, r, tx=0, tries=0;
|
||||
struct timespec w;
|
||||
|
||||
ptouch_send(ptdev, (uint8_t *)cmd, strlen(cmd));
|
||||
while (tx == 0) {
|
||||
w.tv_sec=0;
|
||||
w.tv_nsec=100000000; /* 0.1 sec */
|
||||
r=nanosleep(&w, NULL);
|
||||
if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
|
||||
fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
|
||||
return -1;
|
||||
}
|
||||
tries++;
|
||||
if (tries > 10) {
|
||||
fprintf(stderr, _("timeout while waiting for status response\n"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
if (tx == 32) {
|
||||
if (buf[0]==0x80 && buf[1]==0x20) {
|
||||
memcpy(ptdev->raw, buf, 32);
|
||||
if (buf[8] != 0) {
|
||||
fprintf(stderr, _("Error 1 = %02x\n"), buf[8]);
|
||||
}
|
||||
if (buf[9] != 0) {
|
||||
fprintf(stderr, _("Error 2 = %02x\n"), buf[9]);
|
||||
}
|
||||
ptdev->tape_width_mm=buf[10];
|
||||
ptdev->tape_width_px=0;
|
||||
for (i=0; tape_info[i].mm > 0; i++) {
|
||||
if (tape_info[i].mm == buf[10]) {
|
||||
ptdev->tape_width_px=tape_info[i].px;
|
||||
}
|
||||
}
|
||||
if (ptdev->tape_width_px == 0) {
|
||||
fprintf(stderr, _("unknown tape width of %imm, please report this.\n"), buf[10]);
|
||||
}
|
||||
ptdev->media_type=buf[11];
|
||||
ptdev->status=buf[18];
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if (tx == 16) {
|
||||
fprintf(stderr, _("got only 16 bytes... wondering what they are:\n"));
|
||||
ptouch_rawstatus(buf);
|
||||
}
|
||||
if (tx != 32) {
|
||||
fprintf(stderr, _("read error: got %i instead of 32 bytes\n"), tx);
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, _("strange status:\n"));
|
||||
ptouch_rawstatus(buf);
|
||||
fprintf(stderr, _("trying to flush junk\n"));
|
||||
if ((r=libusb_bulk_transfer(ptdev->h, 0x81, buf, 32, &tx, 0)) != 0) {
|
||||
fprintf(stderr, _("read error: %s\n"), libusb_error_name(r));
|
||||
return -1;
|
||||
}
|
||||
fprintf(stderr, _("got another %i bytes. now try again\n"), tx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ptouch_getmaxwidth(ptouch_dev ptdev)
|
||||
{
|
||||
return ptdev->tape_width_px;
|
||||
}
|
||||
|
||||
int ptouch_sendraster(ptouch_dev ptdev, uint8_t *data, int len)
|
||||
{
|
||||
uint8_t buf[32];
|
||||
|
||||
if (len > 16) { /* PT-2430PC can not print more than 128 px */
|
||||
return -1; /* as we support more devices, we need to check */
|
||||
} /* how much pixels each device support */
|
||||
buf[0]=0x47;
|
||||
buf[1]=len;
|
||||
buf[2]=0;
|
||||
memcpy(buf+3, data, len);
|
||||
return ptouch_send(ptdev, buf, len+3);
|
||||
}
|
||||
119
src/ptouch-gtk.c
Normal file
119
src/ptouch-gtk.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
ptouch-gtk - Simple GTK+ UI to print labels on a Brother P-Touch
|
||||
|
||||
Copyright (C) 2015 Dominic Radermacher <dominic.radermacher@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License version 3 as
|
||||
published by the Free Software Foundation
|
||||
|
||||
This program 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 this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <gtk/gtk.h>
|
||||
|
||||
#define BUILDER_XML_FILE "data/ptouch.ui"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
GtkWidget *window;
|
||||
GtkWidget *statusbar;
|
||||
GtkWidget *text_view;
|
||||
guint statusbar_context_id;
|
||||
} PTouchEditor;
|
||||
|
||||
/* prototypes */
|
||||
void error_message(const gchar *message);
|
||||
void on_window_destroy(GtkWidget *object, PTouchEditor *editor);
|
||||
gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event,
|
||||
PTouchEditor *editor);
|
||||
gboolean init_app(PTouchEditor *editor);
|
||||
|
||||
void error_message(const gchar *message)
|
||||
{
|
||||
GtkWidget *dialog;
|
||||
g_warning(message); /* log to terminal window */
|
||||
/* create an error message dialog and display modally to the user */
|
||||
dialog = gtk_message_dialog_new(NULL,
|
||||
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
|
||||
GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, message);
|
||||
gtk_window_set_title(GTK_WINDOW(dialog), "Error!");
|
||||
gtk_dialog_run(GTK_DIALOG(dialog));
|
||||
gtk_widget_destroy(dialog);
|
||||
}
|
||||
|
||||
void on_window_destroy(GtkWidget *object, PTouchEditor *editor)
|
||||
{
|
||||
gtk_main_quit();
|
||||
}
|
||||
|
||||
gboolean on_window_delete_event(GtkWidget *widget, GdkEvent *event, PTouchEditor *editor)
|
||||
{
|
||||
return FALSE; /* propogate event */
|
||||
}
|
||||
|
||||
void show_about(PTouchEditor *editor)
|
||||
{
|
||||
static const gchar * const authors[] = {
|
||||
"Dominic Radermacher <dominic.radermacher@gmail.com>",
|
||||
NULL
|
||||
};
|
||||
static const gchar copyright[] = "Copyright \xc2\xa9 2015 Dominic Radermacher";
|
||||
static const gchar comments[] = "PTouch Print";
|
||||
|
||||
gtk_show_about_dialog(GTK_WINDOW(editor->window), "authors", authors,
|
||||
"comments", comments, "copyright", copyright,
|
||||
"version", "0.1",
|
||||
"website", "http://mockmoon-cybernetics.ch/",
|
||||
"program-name", "ptouch-gtk",
|
||||
"logo-icon-name", GTK_STOCK_EDIT, NULL);
|
||||
}
|
||||
|
||||
gboolean init_app(PTouchEditor *editor)
|
||||
{
|
||||
GtkBuilder *builder;
|
||||
GError *err=NULL;
|
||||
guint id;
|
||||
|
||||
/* use GtkBuilder to build our interface from the XML file */
|
||||
builder = gtk_builder_new();
|
||||
if (gtk_builder_add_from_file(builder, BUILDER_XML_FILE, &err) == 0) {
|
||||
error_message(err->message);
|
||||
g_error_free(err);
|
||||
return FALSE;
|
||||
}
|
||||
/* get the widgets which will be referenced in callbacks */
|
||||
editor->window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
|
||||
editor->statusbar = GTK_WIDGET(gtk_builder_get_object(builder, "statusbar"));
|
||||
editor->text_view = GTK_WIDGET(gtk_builder_get_object(builder, "text_view"));
|
||||
gtk_builder_connect_signals(builder, editor);
|
||||
/* free memory used by GtkBuilder object */
|
||||
g_object_unref(G_OBJECT(builder));
|
||||
gtk_window_set_default_icon_name(GTK_STOCK_EDIT);
|
||||
/* setup and initialize our statusbar */
|
||||
id = gtk_statusbar_get_context_id(GTK_STATUSBAR(editor->statusbar),
|
||||
"PTouch Print GTK+");
|
||||
editor->statusbar_context_id = id;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
PTouchEditor *editor;
|
||||
|
||||
editor = g_slice_new(PTouchEditor);
|
||||
gtk_init(&argc, &argv);
|
||||
if (init_app(editor) == FALSE) {
|
||||
return 1; /* error loading UI */
|
||||
}
|
||||
gtk_widget_show(editor->window);
|
||||
gtk_main();
|
||||
g_slice_free(PTouchEditor, editor);
|
||||
}
|
||||
395
src/ptouch-print.c
Normal file
395
src/ptouch-print.c
Normal file
@@ -0,0 +1,395 @@
|
||||
/*
|
||||
ptouch-print - Print labels with images or text on a Brother P-Touch
|
||||
|
||||
Copyright (C) 2015 Dominic Radermacher <dominic.radermacher@gmail.com>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify it
|
||||
under the terms of the GNU General Public License version 3 as
|
||||
published by the Free Software Foundation
|
||||
|
||||
This program 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 this program; if not, write to the Free Software Foundation,
|
||||
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* printf() */
|
||||
#include <stdlib.h> /* exit(), malloc() */
|
||||
#include <string.h> /* strcmp(), memcmp() */
|
||||
#include <sys/types.h> /* open() */
|
||||
#include <sys/stat.h> /* open() */
|
||||
#include <fcntl.h> /* open() */
|
||||
#include <gd.h>
|
||||
#include "config.h"
|
||||
#include "gettext.h" /* gettext(), ngettext() */
|
||||
#include "ptouch.h"
|
||||
|
||||
#define _(s) gettext(s)
|
||||
|
||||
#define MAX_LINES 4 /* maybe this should depend on tape size */
|
||||
|
||||
gdImage *image_load(const char *file);
|
||||
void rasterline_setpixel(uint8_t rasterline[16], int pixel);
|
||||
int get_baselineoffset(char *text, char *font, int fsz);
|
||||
int find_fontsize(int want_px, char *font, char *text);
|
||||
int needed_width(char *text, char *font, int fsz);
|
||||
int print_img(ptouch_dev ptdev, gdImage *im);
|
||||
int write_png(gdImage *im, const char *file);
|
||||
gdImage *render_text(char *font, char *line[], int lines, int tape_width);
|
||||
void usage(char *progname);
|
||||
int parse_args(int argc, char **argv);
|
||||
|
||||
// char *font_file="/usr/share/fonts/TTF/Ubuntu-M.ttf";
|
||||
// char *font_file="Ubuntu:medium";
|
||||
char *font_file="DejaVuSans";
|
||||
char *save_png=NULL;
|
||||
int verbose=0;
|
||||
int fontsize=0;
|
||||
|
||||
/* --------------------------------------------------------------------
|
||||
-------------------------------------------------------------------- */
|
||||
|
||||
void rasterline_setpixel(uint8_t rasterline[16], int pixel)
|
||||
{
|
||||
rasterline[15-(pixel/8)] |= 1<<(pixel%8);
|
||||
return;
|
||||
}
|
||||
|
||||
int print_img(ptouch_dev ptdev, gdImage *im)
|
||||
{
|
||||
int d,i,k,offset,tape_width;
|
||||
uint8_t rasterline[16];
|
||||
|
||||
tape_width=ptouch_getmaxwidth(ptdev);
|
||||
/* find out whether color 0 or color 1 is darker */
|
||||
d=(gdImageRed(im,1)+gdImageGreen(im,1)+gdImageBlue(im,1) < gdImageRed(im,0)+gdImageGreen(im,0)+gdImageBlue(im,0))?1:0;
|
||||
if (gdImageSY(im) > tape_width) {
|
||||
printf(_("image is too large (%ipx x %ipx)\n"), gdImageSX(im), gdImageSY(im));
|
||||
printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
|
||||
return -1;
|
||||
}
|
||||
offset=64-(gdImageSY(im)/2); /* always print centered */
|
||||
if (ptouch_rasterstart(ptdev) != 0) {
|
||||
printf(_("ptouch_rasterstart() failed\n"));
|
||||
return -1;
|
||||
}
|
||||
for (k=0; k<gdImageSX(im); k+=1) {
|
||||
memset(rasterline, 0, sizeof(rasterline));
|
||||
for (i=0; i<gdImageSY(im); i+=1) {
|
||||
if (gdImageGetPixel(im, k, gdImageSY(im)-1-i) == d) {
|
||||
rasterline_setpixel(rasterline, offset+i);
|
||||
}
|
||||
}
|
||||
if (ptouch_sendraster(ptdev, rasterline, 16) != 0) {
|
||||
printf(_("ptouch_send() failed\n"));
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------
|
||||
Function image_load()
|
||||
Description detect the type of a image and try to load it
|
||||
Last update 2005-10-16
|
||||
Status Working, should add debug info
|
||||
-------------------------------------------------------------------- */
|
||||
|
||||
gdImage *image_load(const char *file)
|
||||
{
|
||||
const uint8_t png[8]={0x89,'P','N','G',0x0d,0x0a,0x1a,0x0a};
|
||||
char d[10];
|
||||
FILE *f;
|
||||
gdImage *img=NULL;
|
||||
|
||||
if ((f = fopen(file, "rb")) == NULL) { /* error cant open file */
|
||||
return NULL;
|
||||
}
|
||||
if (fread(d, sizeof(d), 1, f) != 1) {
|
||||
return NULL;
|
||||
}
|
||||
rewind(f);
|
||||
if (memcmp(d, png, 8) == 0) {
|
||||
img=gdImageCreateFromPng(f);
|
||||
}
|
||||
fclose(f);
|
||||
return img;
|
||||
}
|
||||
|
||||
int write_png(gdImage *im, const char *file)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
if ((f = fopen(file, "wb")) == NULL) {
|
||||
printf(_("writing image '%s' failed\n"), file);
|
||||
return -1;
|
||||
}
|
||||
gdImagePng(im, f);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------
|
||||
Find out the difference in pixels between a "normal" char and one
|
||||
that goes below the font baseline
|
||||
-------------------------------------------------------------------- */
|
||||
int get_baselineoffset(char *text, char *font, int fsz)
|
||||
{
|
||||
int brect[8];
|
||||
|
||||
if (strpbrk(text, "QgjpqyQ") == NULL) { /* if we have none of these */
|
||||
return 0; /* we don't need an baseline offset */
|
||||
} /* else we need to calculate it */
|
||||
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "o");
|
||||
int tmp=brect[1]-brect[5];
|
||||
gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, "g");
|
||||
return (brect[1]-brect[5])-tmp;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------
|
||||
Find out which fontsize we need for a given font to get a
|
||||
specified pixel size
|
||||
-------------------------------------------------------------------- */
|
||||
int find_fontsize(int want_px, char *font, char *text)
|
||||
{
|
||||
int save=0;
|
||||
int brect[8];
|
||||
|
||||
for (int i=4; ; i++) {
|
||||
if (gdImageStringFT(NULL, &brect[0], -1, font, i, 0.0, 0, 0, text) != NULL) {
|
||||
break;
|
||||
}
|
||||
if (brect[1]-brect[5] <= want_px) {
|
||||
save=i;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (save == 0) {
|
||||
return -1;
|
||||
}
|
||||
return save;
|
||||
}
|
||||
|
||||
int needed_width(char *text, char *font, int fsz)
|
||||
{
|
||||
int brect[8];
|
||||
|
||||
if (gdImageStringFT(NULL, &brect[0], -1, font, fsz, 0.0, 0, 0, text) != NULL) {
|
||||
return -1;
|
||||
}
|
||||
return brect[2]-brect[0];
|
||||
}
|
||||
|
||||
gdImage *render_text(char *font, char *line[], int lines, int tape_width)
|
||||
{
|
||||
int brect[8];
|
||||
int i, black, x=0, tmp, fsz=0, ofs;
|
||||
char *p;
|
||||
gdImage *im=NULL;
|
||||
|
||||
// printf(_("%i lines, font = '%s'\n"), lines, font);
|
||||
if (gdFTUseFontConfig(1) != GD_TRUE) {
|
||||
printf(_("warning: font config not available\n"));
|
||||
}
|
||||
if (fontsize > 0) {
|
||||
fsz=fontsize;
|
||||
printf(_("setting font size=%i\n"), fsz);
|
||||
} else {
|
||||
for (i=0; i<lines; i++) {
|
||||
if ((tmp=find_fontsize(tape_width/lines, font, line[i])) < 0) {
|
||||
printf(_("could not estimate needed font size\n"));
|
||||
return NULL;
|
||||
}
|
||||
if ((fsz == 0) || (tmp < fsz)) {
|
||||
fsz=tmp;
|
||||
}
|
||||
}
|
||||
printf(_("choosing font size=%i\n"), fsz);
|
||||
}
|
||||
for(i=0; i<lines; i++) {
|
||||
tmp=needed_width(line[i], font_file, fsz);
|
||||
if (tmp > x) {
|
||||
x=tmp;
|
||||
}
|
||||
}
|
||||
im=gdImageCreatePalette(x, tape_width);
|
||||
gdImageColorAllocate(im, 255, 255, 255);
|
||||
black=gdImageColorAllocate(im, 0, 0, 0);
|
||||
/* gdImageStringFT(im,brect,fg,fontlist,size,angle,x,y,string) */
|
||||
for (i=0; i<lines; i++) {
|
||||
if ((p=gdImageStringFT(NULL, &brect[0], -black, font, fsz, 0.0, 0, 0, line[i])) != NULL) {
|
||||
printf(_("error in gdImageStringFT: %s\n"), p);
|
||||
}
|
||||
tmp=brect[1]-brect[5];
|
||||
ofs=get_baselineoffset(line[i], font_file, fsz);
|
||||
// printf("line %i height = %ipx, pos = %i\n", i+1, tmp, i*(tape_width/lines)+tmp-ofs-1);
|
||||
if ((p=gdImageStringFT(im, &brect[0], -black, font, fsz, 0.0, 0, i*(tape_width/lines)+tmp-ofs-1, line[i])) != NULL) {
|
||||
printf(_("error in gdImageStringFT: %s\n"), p);
|
||||
}
|
||||
}
|
||||
return im;
|
||||
}
|
||||
|
||||
void usage(char *progname)
|
||||
{
|
||||
printf("usage: %s [options] <print-command(s)>\n", progname);
|
||||
printf("options:\n");
|
||||
printf("\t--font <file>\t\tuse font <file> or <name>\n");
|
||||
printf("\t--writepng <file>\tinstead of printing, write output to png file\n");
|
||||
printf("\t\t\t\tThis currently works only when using\n\t\t\t\tEXACTLY ONE --text statement\n");
|
||||
printf("print-commands:\n");
|
||||
printf("\t--image <file>\t\tprint the given image which must be a 2 color\n");
|
||||
printf("\t\t\t\t(black/white) png\n");
|
||||
printf("\t--text <text>\t\tPrint 1-4 lines of text.\n");
|
||||
printf("\t\t\t\tIf the text contains spaces, use quotation marks\n\t\t\t\taround it.\n");
|
||||
printf("\t--cutmark\t\tPrint a mark where the tape should be cut\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* here we don't print anything, but just try to catch syntax errors */
|
||||
int parse_args(int argc, char **argv)
|
||||
{
|
||||
int lines, i;
|
||||
|
||||
for (i=1; i<argc; i++) {
|
||||
if (*argv[i] != '-') {
|
||||
break;
|
||||
}
|
||||
if (strcmp(&argv[i][1], "-font") == 0) {
|
||||
if (i+1<argc) {
|
||||
font_file=argv[++i];
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-fontsize") == 0) {
|
||||
if (i+1<argc) {
|
||||
i++;
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-writepng") == 0) {
|
||||
if (i+1<argc) {
|
||||
save_png=argv[++i];
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-cutmark") == 0) {
|
||||
continue; /* not done here */
|
||||
} else if (strcmp(&argv[i][1], "-info") == 0) {
|
||||
continue; /* not done here */
|
||||
} else if (strcmp(&argv[i][1], "-image") == 0) {
|
||||
if (i+1<argc) {
|
||||
i++;
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-text") == 0) {
|
||||
for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
|
||||
if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-version") == 0) {
|
||||
printf(_("ptouch-print version %s by Dominic Radermacher\n"), VERSION);
|
||||
exit(0);
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int i, lines, tape_width;
|
||||
char *line[MAX_LINES];
|
||||
gdImage *im=NULL;
|
||||
ptouch_dev ptdev=NULL;
|
||||
|
||||
setlocale(LC_ALL, "");
|
||||
bindtextdomain(PACKAGE, LOCALEDIR);
|
||||
textdomain(PACKAGE);
|
||||
i=parse_args(argc, argv);
|
||||
if (i != argc) {
|
||||
usage(argv[0]);
|
||||
}
|
||||
if ((ptouch_open(&ptdev)) < 0) {
|
||||
return 5;
|
||||
}
|
||||
if (ptouch_init(ptdev) != 0) {
|
||||
printf(_("ptouch_init() failed\n"));
|
||||
}
|
||||
if (ptouch_getstatus(ptdev) != 0) {
|
||||
printf(_("ptouch_getstatus() failed\n"));
|
||||
return 1;
|
||||
}
|
||||
tape_width=ptouch_getmaxwidth(ptdev);
|
||||
for (i=1; i<argc; i++) {
|
||||
if (*argv[i] != '-') {
|
||||
break;
|
||||
}
|
||||
if (strcmp(&argv[i][1], "-font") == 0) {
|
||||
if (i+1<argc) {
|
||||
font_file=argv[++i];
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-fontsize") == 0) {
|
||||
if (i+1<argc) {
|
||||
fontsize=strtol(argv[++i], NULL, 10);
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-writepng") == 0) {
|
||||
if (i+1<argc) {
|
||||
save_png=argv[++i];
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-info") == 0) {
|
||||
printf(_("maximum printing width for this tape is %ipx\n"), tape_width);
|
||||
exit(0);
|
||||
} else if (strcmp(&argv[i][1], "-image") == 0) {
|
||||
im=image_load(argv[++i]);
|
||||
if (im != NULL) {
|
||||
print_img(ptdev, im);
|
||||
gdImageDestroy(im);
|
||||
}
|
||||
} else if (strcmp(&argv[i][1], "-text") == 0) {
|
||||
for (lines=0; (lines < MAX_LINES) && (i < argc); lines++) {
|
||||
if ((i+1 >= argc) || (argv[i+1][0] == '-')) {
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
line[lines]=argv[i];
|
||||
}
|
||||
if ((im=render_text(font_file, line, lines, tape_width)) == NULL) {
|
||||
printf(_("could not render text\n"));
|
||||
return 1;
|
||||
}
|
||||
if (save_png != NULL) {
|
||||
write_png(im, save_png);
|
||||
} else {
|
||||
print_img(ptdev, im);
|
||||
}
|
||||
gdImageDestroy(im);
|
||||
} else if (strcmp(&argv[i][1], "-cutmark") == 0) {
|
||||
ptouch_cutmark(ptdev);
|
||||
} else {
|
||||
usage(argv[0]);
|
||||
}
|
||||
}
|
||||
if (ptouch_eject(ptdev) != 0) {
|
||||
printf(_("ptouch_eject() failed\n"));
|
||||
return -1;
|
||||
}
|
||||
ptouch_close(ptdev);
|
||||
libusb_exit(NULL);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user