commit b7780b26192cf95943aa85d736ea24af73197ab3
parent 363db8ac933bf0edddf320e7a999abaed7fe6756
Author: nibo <nibo@relim.de>
Date: Thu, 24 Oct 2024 12:03:24 +0200
Improve 'cho_image_tag_parse'
Diffstat:
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -2386,6 +2386,7 @@ static struct ChoImage *
cho_image_tag_parse(struct Attr **attrs)
{
struct ChoImage *image = cho_image_new();
+ struct ChoImage *asset;
struct Size *size;
int a;
for (a = 0; attrs[a]; a++) {
@@ -2508,6 +2509,24 @@ cho_image_tag_parse(struct Attr **attrs)
return NULL;
}
}
+ if (image->id) {
+ if (image->src) {
+ cho_log(LOG_ERR, "'img' tag can't have both attributes 'id' and 'src' at the same time.");
+ return NULL;
+ } else {
+ asset = cho_image_find_asset(image->id);
+ if (!asset) {
+ cho_log(LOG_ERR, "There is no image asset with the id '%s'.", image->id);
+ return NULL;
+ }
+ image->src = strdup(asset->src);
+ }
+ } else {
+ if (!image->src) {
+ cho_log(LOG_ERR, "'img' tag has to have at least either the attribute 'id' or 'src'.");
+ return NULL;
+ }
+ }
return image;
}
@@ -3273,7 +3292,6 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
if (songs[so]->sections[se]->lines[li]->items[ly]->is_text) {
songs[so]->sections[se]->lines[li]->items[ly]->u.text->text = realloc(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text, (te+1) * sizeof(char));
songs[so]->sections[se]->lines[li]->items[ly]->u.text->text[te] = 0;
- te = 0;
if (strlen(songs[so]->sections[se]->lines[li]->items[ly]->u.text->text) == 0) {
cho_line_item_free(songs[so]->sections[se]->lines[li]->items[ly]);
} else {
@@ -3282,6 +3300,7 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
} else {
ly++;
}
+ te = 0;
songs[so]->sections[se]->lines[li]->items = realloc(songs[so]->sections[se]->lines[li]->items, (ly+1) * sizeof(struct ChoLineItem *));
songs[so]->sections[se]->lines[li]->items[ly] = cho_line_item_new();
prev_state = STATE_LYRICS;
@@ -3817,7 +3836,6 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
cho_text_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text);
songs[so]->sections[se]->lines[li]->items[ly]->is_text = false;
songs[so]->sections[se]->lines[li]->items[ly]->u.image = image;
- // cho_debug_image_print(image);
}
break;
case DT_PREAMBLE:
@@ -4135,8 +4153,11 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
cho_text_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text);
songs[so]->sections[se]->lines[li]->items[ly]->is_text = false;
image = cho_image_tag_parse(tags[ta]->attrs);
+ if (!image) {
+ LOG_DEBUG("cho_image_tag_parse failed.");
+ return NULL;
+ }
songs[so]->sections[se]->lines[li]->items[ly]->u.image = image;
- printf("img tag with attr.\n");
} else {
tag_style = cho_style_parse(tag_start, tags[ta]->attrs, cho_tag_style_inherit(tags, ta-1));
if (!tag_style) {
@@ -4219,8 +4240,11 @@ cho_songs_parse(FILE *fp, const char *chordpro_filepath, struct Config *config)
cho_text_free(songs[so]->sections[se]->lines[li]->items[ly]->u.text);
songs[so]->sections[se]->lines[li]->items[ly]->is_text = false;
image = cho_image_tag_parse(tags[ta]->attrs);
+ if (!image) {
+ LOG_DEBUG("cho_image_tag_parse failed.");
+ return NULL;
+ }
songs[so]->sections[se]->lines[li]->items[ly]->u.image = image;
- printf("img tag with attr.\n");
} else {
tag_style = cho_style_parse(tag_start, tags[ta]->attrs, cho_tag_style_inherit(tags, ta-1));
if (!tag_style) {
diff --git a/out_pdf.c b/out_pdf.c
@@ -1055,7 +1055,7 @@ text_create(struct ChoSong **songs, struct Config *config)
text[t]->lines[tl]->items = NULL;
for (ly = 0; lines[li]->items[ly]; ly++) {
if (!lines[li]->items[ly]->is_text) {
- // printf("src: %s\n", lines[li]->items[ly]->u.image->src);
+ util_log(LOG_INFO, "image with src '%s'.", lines[li]->items[ly]->u.image->src);
continue;
}
// out_pdf_text_find_fitting_length(lines[li]->lyrics[ly]);