1
0
mirror of https://git.familie-radermacher.ch/linux/ptouch-print.git synced 2026-03-07 06:34:18 +00:00

Add support for using \n for new line (thanks to github.com/probonopd/)

This commit is contained in:
Dominic Radermacher
2026-03-06 08:20:32 +01:00
parent bdd724fef5
commit 76c419f952
5 changed files with 156 additions and 110 deletions

View File

@@ -1,7 +1,7 @@
/*
ptouch-print - Print labels with images or text on a Brother P-Touch
Copyright (C) 2015-2025 Dominic Radermacher <dominic@familie-radermacher.ch>
Copyright (C) 2015-2026 Dominic Radermacher <dominic@familie-radermacher.ch>
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
@@ -18,6 +18,7 @@
*/
#include <argp.h>
#include <errno.h>
#include <stdio.h> /* printf() */
#include <stdlib.h> /* exit(), malloc() */
#include <stdbool.h>
@@ -75,6 +76,7 @@ gdImage *img_cutmark(int print_width);
gdImage *render_text(char *font, char *line[], int lines, int print_width);
void unsupported_printer(ptouch_dev ptdev);
void add_job(job_type_t type, int n, char *line);
void add_text(struct argp_state *state, char *arg, bool new_job);
static error_t parse_opt(int key, char *arg, struct argp_state *state);
const char *argp_program_version = P_NAME " " VERSION;
@@ -94,12 +96,12 @@ static struct argp_option options[] = {
{ 0, 0, 0, 0, "print commands:", 2},
{ "image", 'i', "<file>", 0, "Print the given image which must be a 2 color (black/white) png", 2},
{ "text", 't', "<text>", 0, "Print line of <text>. If the text contains spaces, use quotation marks taround it", 2},
{ "text", 't', "<text>", 0, "Print line of <text>. If the text contains spaces, use quotation marks around it. \\n will be replaced by a newline", 2},
{ "cutmark", 'c', 0, 0, "Print a mark where the tape should be cut", 2},
{ "pad", 'p', "<n>", 0, "Add n pixels padding (blank tape)", 2},
{ "chain", 10, 0, 0, "Skip final feed of label and any automatic cut", 2},
{ "precut", 11, 0, 0, "Add a cut before the label (useful in chain mode for cuts with minimal waste)", 2},
{ "newline", 'n', "<text>", 0, "Add text in a new line (up to 4 lines)", 2},
{ "newline", 'n', "<text>", 0, "Add text in a new line (up to 4 lines). \\n will be replaced by a newline", 2},
{ "align", 'a', "<l|c|r>", 0, "Align text (when printing multiple lines)", 2},
{ 0, 0, 0, 0, "other commands:", 3},
@@ -525,6 +527,58 @@ void add_job(job_type_t type, int n, char *line)
last_added_job = new_job;
}
void add_text(struct argp_state *state, char *arg, bool new_job)
{
char *p = arg;
bool first_part = true;
do {
char *next1 = strstr(p, "\\n");
char *next2 = strchr(p, '\n');
char *next = NULL;
char *p_next = NULL;
int skip = 0;
if (next1 && next2) {
if (next1 < next2) {
next = next1;
skip = 2;
} else {
next = next2;
skip = 1;
}
} else if (next1) {
next = next1;
skip = 2;
} else if (next2) {
next = next2;
skip = 1;
}
if (next) {
*next = '\0';
p_next = next + skip;
} else {
p_next = NULL;
}
if (new_job && first_part) {
add_job(JOB_TEXT, 1, p);
} else {
if (!last_added_job || last_added_job->type != JOB_TEXT) {
add_job(JOB_TEXT, 1, p);
} else {
if (last_added_job->n >= MAX_LINES) {
argp_failure(state, 1, EINVAL, _("Only up to %d lines are supported"), MAX_LINES);
return;
}
last_added_job->lines[last_added_job->n++] = p;
}
}
p = p_next;
first_part = false;
} while (p);
}
static error_t parse_opt(int key, char *arg, struct argp_state *state)
{
struct arguments *arguments = (struct arguments *)state->input;
@@ -556,7 +610,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
break;
case 't': // text
//printf("adding text job with alignment %i\n", arguments->align);
add_job(JOB_TEXT, 1, arg);
add_text(state, arg, true);
break;
case 'c': // cutmark
add_job(JOB_CUTMARK, 0, NULL);
@@ -583,17 +637,7 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
}
break;
case 'n': // newline
if (!last_added_job || last_added_job->type != JOB_TEXT) {
add_job(JOB_TEXT, 1, arg);
break;
}
if (last_added_job->n >= MAX_LINES) { // max number of lines reached
argp_failure(state, 1, EINVAL, _("Only up to %d lines are supported"), MAX_LINES);
break;
}
last_added_job->lines[last_added_job->n++] = arg;
add_text(state, arg, false);
break;
case 20: // info
arguments->info = true;