X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2Fxo-file.c;h=91044b0e491202286720fd52005ed4b7682448d0;hb=2e5ff0bff41fa53d4941b6f0fe49c8914bf29cbc;hp=bff53a1a206caf18dc2b979dd3923c8f914921dd;hpb=fd008f43f9e0848369972b0bc0e52129380e229b;p=xournal.git diff --git a/src/xo-file.c b/src/xo-file.c index bff53a1..91044b0 100644 --- a/src/xo-file.c +++ b/src/xo-file.c @@ -21,8 +21,9 @@ #include "xo-callbacks.h" #include "xo-misc.h" #include "xo-file.h" +#include "xo-paint.h" -const char *tool_names[NUM_TOOLS] = {"pen", "eraser", "highlighter", "", "", "selectrect", "vertspace", "hand"}; +const char *tool_names[NUM_TOOLS] = {"pen", "eraser", "highlighter", "text", "", "selectrect", "vertspace", "hand"}; const char *color_names[COLOR_MAX] = {"black", "blue", "red", "green", "gray", "lightblue", "lightgreen", "magenta", "orange", "yellow", "white"}; const char *bgtype_names[3] = {"solid", "pixmap", "pdf"}; @@ -73,7 +74,7 @@ gboolean save_journal(const char *filename) struct Layer *layer; struct Item *item; int i, is_clone; - char *tmpfn; + char *tmpfn, *tmpstr; gchar *pdfbuf; gsize pdflen; gboolean success; @@ -88,8 +89,8 @@ gboolean save_journal(const char *filename) setlocale(LC_NUMERIC, "C"); gzprintf(f, "\n" - "Xournal document - see http://math.mit.edu/~auroux/software/xournal/\n" - "\n"); + "\n" + "Xournal document - see http://math.mit.edu/~auroux/software/xournal/\n"); for (pagelist = journal.pages; pagelist!=NULL; pagelist = pagelist->next) { pg = (struct Page *)pagelist->data; gzprintf(f, "\n", pg->width, pg->height); @@ -178,11 +179,24 @@ gboolean save_journal(const char *filename) gzprintf(f, "%.2f ", item->path->coords[i]); gzprintf(f, "\n\n"); } + if (item->type == ITEM_TEXT) { + gzprintf(f, "font_name, item->font_size, item->bbox.left, item->bbox.top); + if (item->brush.color_no >= 0) + gzputs(f, color_names[item->brush.color_no]); + else + gzprintf(f, "#%08x", item->brush.color_rgba); + tmpstr = g_markup_escape_text(item->text, -1); + gzprintf(f, "\">%s\n", tmpstr); + g_free(tmpstr); + + } } gzprintf(f, "\n"); } gzprintf(f, "\n"); } + gzprintf(f, "\n"); gzclose(f); setlocale(LC_NUMERIC, ""); @@ -471,9 +485,69 @@ void xoj_parser_start_element(GMarkupParseContext *context, tmpItem->brush.tool_options = 0; // who cares ? if (tmpItem->brush.tool_type == TOOL_HIGHLIGHTER) { if (tmpItem->brush.color_no >= 0) - tmpItem->brush.color_rgba &= HILITER_ALPHA_MASK; + tmpItem->brush.color_rgba &= ui.hiliter_alpha_mask; } } + else if (!strcmp(element_name, "text")) { // start of a text item + if (tmpLayer == NULL || tmpItem != NULL) { + *error = xoj_invalid(); + return; + } + tmpItem = (struct Item *)g_malloc0(sizeof(struct Item)); + tmpItem->type = ITEM_TEXT; + tmpItem->canvas_item = NULL; + tmpLayer->items = g_list_append(tmpLayer->items, tmpItem); + tmpLayer->nitems++; + // scan for font, size, x, y, and color attributes + has_attr = 0; + while (*attribute_names!=NULL) { + if (!strcmp(*attribute_names, "font")) { + if (has_attr & 1) *error = xoj_invalid(); + tmpItem->font_name = g_strdup(*attribute_values); + has_attr |= 1; + } + else if (!strcmp(*attribute_names, "size")) { + if (has_attr & 2) *error = xoj_invalid(); + cleanup_numeric((gchar *)*attribute_values); + tmpItem->font_size = g_ascii_strtod(*attribute_values, &ptr); + if (ptr == *attribute_values) *error = xoj_invalid(); + has_attr |= 2; + } + else if (!strcmp(*attribute_names, "x")) { + if (has_attr & 4) *error = xoj_invalid(); + cleanup_numeric((gchar *)*attribute_values); + tmpItem->bbox.left = g_ascii_strtod(*attribute_values, &ptr); + if (ptr == *attribute_values) *error = xoj_invalid(); + has_attr |= 4; + } + else if (!strcmp(*attribute_names, "y")) { + if (has_attr & 8) *error = xoj_invalid(); + cleanup_numeric((gchar *)*attribute_values); + tmpItem->bbox.top = g_ascii_strtod(*attribute_values, &ptr); + if (ptr == *attribute_values) *error = xoj_invalid(); + has_attr |= 8; + } + else if (!strcmp(*attribute_names, "color")) { + if (has_attr & 16) *error = xoj_invalid(); + tmpItem->brush.color_no = COLOR_OTHER; + for (i=0; ibrush.color_no = i; + tmpItem->brush.color_rgba = predef_colors_rgba[i]; + } + // there's also the case of hex (#rrggbbaa) colors + if (tmpItem->brush.color_no == COLOR_OTHER && **attribute_values == '#') { + tmpItem->brush.color_rgba = strtol(*attribute_values + 1, &ptr, 16); + if (*ptr!=0) *error = xoj_invalid(); + } + has_attr |= 16; + } + else *error = xoj_invalid(); + attribute_names++; + attribute_values++; + } + if (has_attr!=31) *error = xoj_invalid(); + } } void xoj_parser_end_element(GMarkupParseContext *context, @@ -502,6 +576,13 @@ void xoj_parser_end_element(GMarkupParseContext *context, update_item_bbox(tmpItem); tmpItem = NULL; } + if (!strcmp(element_name, "text")) { + if (tmpItem == NULL) { + *error = xoj_invalid(); + return; + } + tmpItem = NULL; + } } void xoj_parser_text(GMarkupParseContext *context, @@ -528,6 +609,11 @@ void xoj_parser_text(GMarkupParseContext *context, tmpItem->path = gnome_canvas_points_new(n/2); g_memmove(tmpItem->path->coords, ui.cur_path.coords, n*sizeof(double)); } + if (!strcmp(element_name, "text")) { + tmpItem->text = g_malloc(text_len+1); + g_memmove(tmpItem->text, text, text_len); + tmpItem->text[text_len]=0; + } } gboolean user_wants_second_chance(char **filename) @@ -619,7 +705,7 @@ gboolean open_journal(char *filename) close_journal(); while (bgpdf.status != STATUS_NOT_INIT) gtk_main_iteration(); new_journal(); - ui.zoom = DEFAULT_ZOOM; + ui.zoom = ui.startup_zoom; gnome_canvas_set_pixels_per_unit(canvas, ui.zoom); update_page_stuff(); return init_bgpdf(filename, TRUE, DOMAIN_ABSOLUTE); @@ -664,7 +750,7 @@ gboolean open_journal(char *filename) ui.layerno = ui.cur_page->nlayers-1; ui.cur_layer = (struct Layer *)(g_list_last(ui.cur_page->layers)->data); ui.saved = TRUE; - ui.zoom = DEFAULT_ZOOM; + ui.zoom = ui.startup_zoom; update_file_name(g_strdup(filename)); gnome_canvas_set_pixels_per_unit(canvas, ui.zoom); make_canvas_items(); @@ -711,8 +797,9 @@ GList *attempt_load_gv_bg(char *filename) char *pipename; int buflen, remnlen, file_pageno; - buf = g_malloc(BUFSIZE); // a reasonable buffer size f = fopen(filename, "r"); + if (f == NULL) return NULL; + buf = g_malloc(BUFSIZE); // a reasonable buffer size if (fread(buf, 1, 4, f) !=4 || (strncmp((char *)buf, "%!PS", 4) && strncmp((char *)buf, "%PDF", 4))) { fclose(f); @@ -1066,7 +1153,7 @@ gboolean init_bgpdf(char *pdfname, gboolean create_pages, int file_domain) bgpdf.requests = NULL; bgpdf.create_pages = create_pages; bgpdf.has_failed = FALSE; - add_bgpdf_request(-1, DEFAULT_ZOOM, FALSE); // request all pages + add_bgpdf_request(-1, ui.startup_zoom, FALSE); // request all pages return TRUE; } @@ -1091,7 +1178,7 @@ void bgpdf_create_page_with_bg(int pageno, struct BgPdfPage *bgpg) bg->filename = refstring_ref(bgpdf.filename); bg->file_domain = bgpdf.file_domain; bg->file_page_seq = pageno; - bg->pixbuf_scale = DEFAULT_ZOOM; + bg->pixbuf_scale = ui.startup_zoom; bg->pixbuf_dpi = bgpg->dpi; if (journal.npages < pageno) { @@ -1165,11 +1252,14 @@ void update_mru_menu(void) { int i; gboolean anyone = FALSE; + gchar *tmp; for (i=0; itype = BG_SOLID; @@ -1231,6 +1321,7 @@ void init_config_default(void) ui.default_page.bg->ruling = RULING_LINED; ui.view_continuous = TRUE; ui.allow_xinput = TRUE; + ui.discard_corepointer = FALSE; ui.bg_apply_all_pages = FALSE; ui.use_erasertip = FALSE; ui.window_default_width = 720; @@ -1244,6 +1335,20 @@ void init_config_default(void) ui.progressive_bg = TRUE; ui.print_ruling = TRUE; ui.default_unit = UNIT_CM; + ui.default_path = NULL; + ui.default_font_name = g_strdup(DEFAULT_FONT); + ui.default_font_size = DEFAULT_FONT_SIZE; + + // the default UI vertical order + ui.vertical_order[0][0] = 1; + ui.vertical_order[0][1] = 2; + ui.vertical_order[0][2] = 3; + ui.vertical_order[0][3] = 0; + ui.vertical_order[0][4] = 4; + ui.vertical_order[1][0] = 2; + ui.vertical_order[1][1] = 3; + ui.vertical_order[1][2] = 0; + ui.vertical_order[1][3] = ui.vertical_order[1][4] = -1; ui.toolno[0] = ui.startuptool = TOOL_PEN; ui.ruler[0] = ui.startupruler = FALSE; @@ -1265,9 +1370,10 @@ void init_config_default(void) g_memmove(&(ui.brushes[j][i]), &(ui.brushes[0][i]), sizeof(struct Brush)); // predef_thickness is already initialized as a global variable - GS_BITMAP_DPI = 144; PDFTOPPM_PRINTING_DPI = 150; + + ui.hiliter_opacity = 0.5; } #if GLIB_CHECK_VERSION(2,6,0) @@ -1284,6 +1390,23 @@ void update_keyval(const gchar *group_name, const gchar *key, #endif +const char *vorder_usernames[VBOX_MAIN_NITEMS+1] = + {"drawarea", "menu", "main_toolbar", "pen_toolbar", "statusbar", NULL}; + +gchar *verbose_vertical_order(int *order) +{ + gchar buf[80], *p; // longer than needed + int i; + + p = buf; + for (i=0; i=VBOX_MAIN_NITEMS) continue; + if (p!=buf) *(p++) = ' '; + p = g_stpcpy(p, vorder_usernames[order[i]]); + } + return g_strdup(buf); +} + void save_config_to_file(void) { gchar *buf; @@ -1332,9 +1455,24 @@ void save_config_to_file(void) update_keyval("general", "use_xinput", " use XInput extensions (true/false)", g_strdup(ui.allow_xinput?"true":"false")); + update_keyval("general", "discard_corepointer", + " discard Core Pointer events in XInput mode (true/false)", + g_strdup(ui.discard_corepointer?"true":"false")); update_keyval("general", "use_erasertip", " always map eraser tip to eraser (true/false)", g_strdup(ui.use_erasertip?"true":"false")); + update_keyval("general", "default_path", + " default path for open/save (leave blank for current directory)", + g_strdup((ui.default_path!=NULL)?ui.default_path:"")); + update_keyval("general", "interface_order", + " interface components from top to bottom\n valid values: drawarea menu main_toolbar pen_toolbar statusbar", + verbose_vertical_order(ui.vertical_order[0])); + update_keyval("general", "interface_fullscreen", + " interface components in fullscreen mode, from top to bottom", + verbose_vertical_order(ui.vertical_order[1])); + update_keyval("general", "highlighter_opacity", + " highlighter opacity (0 to 1, default 0.5)\n warning: opacity level is not saved in xoj files!", + g_strdup_printf("%.2f", ui.hiliter_opacity)); update_keyval("paper", "width", " the default page width, in points (1/72 in)", @@ -1395,7 +1533,7 @@ void save_config_to_file(void) " default highlighter thickness (fine = 1, medium = 2, thick = 3)", g_strdup_printf("%d", ui.default_brushes[TOOL_HIGHLIGHTER].thickness_no)); update_keyval("tools", "btn2_tool", - " button 2 tool (pen, eraser, highlighter, selectrect, vertspace, hand)", + " button 2 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)", g_strdup(tool_names[ui.toolno[1]])); update_keyval("tools", "btn2_linked", " button 2 brush linked to primary brush (true/false) (overrides all other settings)", @@ -1415,7 +1553,7 @@ void save_config_to_file(void) " button 2 eraser mode (eraser only)", g_strdup_printf("%d", ui.brushes[1][TOOL_ERASER].tool_options)); update_keyval("tools", "btn3_tool", - " button 3 tool (pen, eraser, highlighter, selectrect, vertspace, hand)", + " button 3 tool (pen, eraser, highlighter, text, selectrect, vertspace, hand)", g_strdup(tool_names[ui.toolno[2]])); update_keyval("tools", "btn3_linked", " button 3 brush linked to primary brush (true/false) (overrides all other settings)", @@ -1451,8 +1589,12 @@ void save_config_to_file(void) g_strdup_printf("%.2f;%.2f;%.2f", predef_thickness[TOOL_HIGHLIGHTER][1], predef_thickness[TOOL_HIGHLIGHTER][2], predef_thickness[TOOL_HIGHLIGHTER][3])); - // set comments for keys / groups that don't exist - // set keyvals to current options + update_keyval("tools", "default_font", + " name of the default font", + g_strdup(ui.default_font_name)); + update_keyval("tools", "default_font_size", + " default font size", + g_strdup_printf("%.1f", ui.default_font_size)); buf = g_key_file_to_data(ui.config_data, NULL, NULL); if (buf == NULL) return; @@ -1545,6 +1687,50 @@ gboolean parse_keyval_boolean(const gchar *group, const gchar *key, gboolean *va return FALSE; } +gboolean parse_keyval_string(const gchar *group, const gchar *key, gchar **val) +{ + gchar *ret; + + ret = g_key_file_get_value(ui.config_data, group, key, NULL); + if (ret==NULL) return FALSE; + if (strlen(ret) == 0) { + *val = NULL; + g_free(ret); + } + else *val = ret; + return TRUE; +} + +gboolean parse_keyval_vorderlist(const gchar *group, const gchar *key, int *order) +{ + gchar *ret, *p; + int tmp[VBOX_MAIN_NITEMS]; + int i, n, found, l; + + ret = g_key_file_get_value(ui.config_data, group, key, NULL); + if (ret==NULL) return FALSE; + + for (i=0; iVBOX_MAIN_NITEMS) return FALSE; // too many items + for (i=0; i=VBOX_MAIN_NITEMS) { g_free(ret); return FALSE; } // parse error + // we found item #i + tmp[n++] = i; + while (*p==' ') p++; + } + + for (n=0; ncolor_no), bgcolor_names, COLOR_MAX); @@ -1646,5 +1838,8 @@ void load_config_from_file(void) parse_keyval_floatlist("tools", "pen_thicknesses", predef_thickness[TOOL_PEN], 5, 0.01, 1000.0); parse_keyval_floatlist("tools", "eraser_thicknesses", predef_thickness[TOOL_ERASER]+1, 3, 0.01, 1000.0); parse_keyval_floatlist("tools", "highlighter_thicknesses", predef_thickness[TOOL_HIGHLIGHTER]+1, 3, 0.01, 1000.0); + if (parse_keyval_string("tools", "default_font", &str)) + if (str!=NULL) { g_free(ui.default_font_name); ui.default_font_name = str; } + parse_keyval_float("tools", "default_font_size", &ui.default_font_size, 1., 200.); #endif }