commit 730d90a1914d1ae9f136a5a2f7ae403e8ef58e2e
parent 6b6cb82f00af07b909daf787245dd4cea3e76d34
Author: nibo <nibo@relim.de>
Date: Sun, 30 Jun 2024 20:39:40 +0200
Further work
Diffstat:
5 files changed, 93 insertions(+), 30 deletions(-)
diff --git a/chordpro.c b/chordpro.c
@@ -344,7 +344,7 @@ struct RGBColor *cho_color_parse(const char *str)
return color;
}
-static struct Font *cho_font_new(void)
+struct Font *cho_font_new(void)
{
struct Font *font = malloc(sizeof(struct Font));
font->name = NULL;
@@ -389,6 +389,16 @@ void cho_font_free(struct Font *font)
free(font);
}
+void cho_fonts_free(struct Font **fonts)
+{
+ int i = 0;
+ while (fonts[i] != NULL) {
+ cho_font_free(fonts[i]);
+ i++;
+ }
+ free(fonts);
+}
+
enum FontStyle cho_font_style_parse(const char *str)
{
if (strcasecmp(str, "italic") == 0) {
@@ -421,11 +431,6 @@ struct Style *cho_style_new(void)
{
struct Style *style = malloc(sizeof(struct Style));
style->font = cho_font_new();
- /* style->font = NULL;
- style->font_family = FF_NORMAL;
- style->font_size = DEFAULT_FONT_SIZE;
- style->font_style = FS_NORMAL;
- style->font_weight = FW_NORMAL; */
style->foreground_color = cho_rgbcolor_new(20, 20, 20);
style->background_color = cho_rgbcolor_new(255, 255, 255);
style->underline_style = LS_NONE;
@@ -444,7 +449,6 @@ struct Style *cho_style_new(void)
void cho_style_free(struct Style *style)
{
cho_font_free(style->font);
- // free(style->font);
free(style->foreground_color);
free(style->background_color);
free(style->underline_color);
@@ -459,11 +463,6 @@ struct Style *cho_style_duplicate(struct Style *style)
{
struct Style *copy = malloc(sizeof(struct Style));
copy->font = cho_font_duplicate(style->font);
- /* copy->font = style->font ? strdup(style->font) : NULL;
- copy->font_family = style->font_family;
- copy->font_size = style->font_size;
- copy->font_style = style->font_style;
- copy->font_weight = style->font_weight; */
copy->foreground_color = cho_rgbcolor_duplicate(style->foreground_color);
copy->background_color = cho_rgbcolor_duplicate(style->background_color);
copy->underline_style = style->underline_style;
diff --git a/chordpro.h b/chordpro.h
@@ -162,7 +162,9 @@ void cho_songs_free(struct ChoSong **song);
int cho_line_item_count(struct ChoLineItem **items);
int cho_chord_count(struct ChoChord **chords);
struct Font *cho_style_font_desc_parse(const char *str);
+struct Font *cho_font_new(void);
void cho_font_free(struct Font *font);
+void cho_fonts_free(struct Font **fonts);
enum FontStyle cho_font_style_parse(const char *str);
enum FontWeight cho_font_weight_parse(const char *str);
diff --git a/lorid.c b/lorid.c
@@ -7,7 +7,6 @@
#include "chordpro.h"
#include "out_pdf.h"
#include "config.h"
-#include <fontconfig/fontconfig.h>
int main(int argc, char *argv[])
{
diff --git a/out_pdf.c b/out_pdf.c
@@ -1,6 +1,6 @@
-#include <string.h>
#include <stdbool.h>
#include <stdint.h>
+#include <string.h>
#include <pdfio.h>
#include <pdfio-content.h>
#include "chordpro.h"
@@ -115,7 +115,46 @@ char *string_trim(const char *text)
return trimmed_text;
}
-static bool out_pdf_add_if_not_in(struct Font *font, struct Font ***array)
+/* static void out_pdf_font_find(struct Font *font)
+{
+ FcInit();
+ FcObjectSet *obj = FcObjectSetBuild(FC_FAMILY, FC_STYLE, FC_WEIGHT, FC_FILE);
+ FcPattern *pattern = FcPatternCreate();
+ FcValue value = { .type = FcTypeString, .u.s = (unsigned char *)font->name };
+ FcPatternAdd(pattern, FC_FAMILY, value, FcFalse);
+ FcFontSet *set = FcFontList(NULL, pattern, obj);
+ for (int i=0; i<set->nfont; i++) {
+ FcChar8 *style, *filepath;
+ if (FcPatternGetString(set->fonts[i], FC_STYLE, 0, &style) == FcResultMatch) {
+ printf("style: %s\n", style);
+ if (FcPatternGetString(set->fonts[i], FC_FILE, 0, &filepath) == FcResultMatch) {
+ printf("filepath: %s\n", filepath);
+ }
+ }
+ }
+ FcFontSetDestroy(set);
+ FcObjectSetDestroy(obj);
+ FcPatternDestroy(pattern);
+ FcFini();
+} */
+
+static void out_pdf_font_add(struct Font *font, struct Font ***array)
+{
+ int a = 0;
+ if (!*array) {
+ *array = realloc(*array, 2 * sizeof(struct Font *));
+ (*array)[0] = font;
+ (*array)[1] = NULL;
+ } else {
+ while ((*array)[a] != NULL)
+ a++;
+ *array = realloc(*array, (a+2) * sizeof(struct Font *));
+ (*array)[a] = font;
+ (*array)[a+1] = NULL;
+ }
+}
+
+static bool out_pdf_font_add_if_not_in(struct Font *font, struct Font ***array)
{
int a = 0;
if (!*array) {
@@ -159,7 +198,7 @@ static void out_pdf_add_fonts(struct Font *font, struct Font ***fonts)
new_font->style = font->style;
new_font->weight = font->weight;
new_font->size = font->size;
- added = out_pdf_add_if_not_in(new_font, fonts);
+ added = out_pdf_font_add_if_not_in(new_font, fonts);
if (!added) {
cho_font_free(new_font);
trimmed = NULL;
@@ -180,14 +219,35 @@ static void out_pdf_add_fonts(struct Font *font, struct Font ***fonts)
new_font->style = font->style;
new_font->weight = font->weight;
new_font->size = font->size;
- added = out_pdf_add_if_not_in(new_font, fonts);
+ added = out_pdf_font_add_if_not_in(new_font, fonts);
if (!added)
cho_font_free(new_font);
}
-static void out_pdf_font_get_all(struct ChoSong **songs)
+static void out_pdf_fonts_add_default(struct Font ***fonts)
+{
+ struct Font *font_regular = cho_font_new();
+ font_regular->name = strdup("Inter");
+ out_pdf_font_add(font_regular, fonts);
+ struct Font *font_bold = cho_font_new();
+ font_bold->name = strdup("Inter");
+ font_bold->weight = FW_BOLD;
+ out_pdf_font_add(font_bold, fonts);
+ struct Font *font_italic = cho_font_new();
+ font_italic->name = strdup("Inter");
+ font_italic->style = FS_ITALIC;
+ out_pdf_font_add(font_italic, fonts);
+ struct Font *font_italic_bold = cho_font_new();
+ font_italic_bold->name = strdup("Inter");
+ font_italic_bold->style = FS_ITALIC;
+ font_italic_bold->weight = FW_BOLD;
+ out_pdf_font_add(font_italic_bold, fonts);
+}
+
+static struct Font **out_pdf_font_get_all(struct ChoSong **songs)
{
struct Font **fonts = NULL;
+ out_pdf_fonts_add_default(&fonts);
int so = 0;
int se = 0;
int li = 0;
@@ -212,15 +272,7 @@ static void out_pdf_font_get_all(struct ChoSong **songs)
se = 0;
so++;
}
- int f = 0;
- if (fonts) {
- while (fonts[f] != NULL) {
- // cho_font_print(fonts[f]);
- cho_font_free(fonts[f]);
- f++;
- }
- free(fonts);
- }
+ return fonts;
}
static bool out_pdf_chord_show(pdfio_stream_t *stream, const char *lyrics_line, struct ChoChord *chord, bool linebreak)
@@ -347,14 +399,21 @@ bool out_pdf_new(const char *cho_filename, struct ChoSong **songs)
pdfio_rect_t crop_box = { 36.0, 36.0, MEDIABOX_WIDTH, MEDIABOX_HEIGHT };
pdfio_file_t *pdf = pdfioFileCreate(pdf_filename, "2.0", &media_box_a4, &crop_box, NULL, NULL);
free(pdf_filename);
- out_pdf_font_get_all(songs);
+ pdfio_dict_t *page1_dict = pdfioDictCreate(pdf);
+ struct Font **fonts = out_pdf_font_get_all(songs);
+ int f = 0;
+ while (fonts[f] != NULL) {
+ cho_font_print(fonts[f]);
+ // out_pdf_font_find(fonts[f]);
+ f++;
+ }
+ cho_fonts_free(fonts);
inter_regular = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-Regular.ttf", true);
inter_bold = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-Bold.ttf", true);
inter_italic = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-Italic.ttf", true);
inter_bold_italic = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-BoldItalic.ttf", true);
inter_light = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-Light.ttf", true);
inter_light_italic = pdfioFileCreateFontObjFromFile(pdf, "./fonts/Inter-LightItalic.ttf", true);
- pdfio_dict_t *page1_dict = pdfioDictCreate(pdf);
if (!pdfioPageDictAddFont(page1_dict, "Inter-Regular", inter_regular)) {
fprintf(stderr, "pdfioPageDictAddFont failed.\n");
return false;
diff --git a/out_pdf.h b/out_pdf.h
@@ -2,7 +2,6 @@
#define MEDIABOX_WIDTH 595.0
#define PAGE_HEIGHT MEDIABOX_HEIGHT - 36.0
#define PAGE_WIDTH MEDIABOX_WIDTH - 36.0
-// #define PADDING 50.0
#define PADDING 80.0
#define LINE_LEN MEDIABOX_WIDTH - PADDING * 2
@@ -12,4 +11,9 @@ enum Alignment {
CONTINUE
};
+struct FontObj {
+ char *name;
+ // pdfio_obj_t
+};
+
bool out_pdf_new(const char *cho_filename, struct ChoSong **songs);