1 /**
2  * @file   importer.c
3  * @brief
4  *
5  * Copyright (C) 2009 Gummi Developers
6  * All Rights reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use,
12  * copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following
15  * conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 #include "importer.h"
31 
32 #include <string.h>
33 
34 #include <glib.h>
35 
36 #include "editor.h"
37 #include "environment.h"
38 #include "utils.h"
39 
40 extern Gummi* gummi;
41 
42 const gchar align_type[][4] = { "l", "c", "r" };
43 const gchar bracket_type[][16] = { "matrix", "pmatrix", "bmatrix",
44                                   "Bmatrix", "vmatrix", "Vmatrix" };
45 
importer_generate_table(gint rows,gint cols,gint borders,gint alignment)46 const gchar* importer_generate_table (gint rows, gint cols, gint borders,
47         gint alignment) {
48     gint i = 0, j = 0;
49     static gchar result[BUFSIZ * 2] = { 0 };
50     gchar table[BUFSIZ * 2] = { 0 },
51           begin_tabular[BUFSIZ] = "\\begin{tabular}{",
52           end_tabular[] = "\n\\end{tabular}\n",
53           line[] = "\n\\hline",
54           tmp[BUFSIZ / 8];
55 
56     /* clear previous data */
57     result[0] = 0;
58 
59     if (borders)
60         strncat (begin_tabular, "|", BUFSIZ - strlen (begin_tabular) -1);
61     for (i = 0; i < cols; ++i) {
62         strncat (begin_tabular, align_type[alignment], BUFSIZ
63                 -strlen (begin_tabular) -1);
64         if (borders == 2 || (borders == 1 && i == cols -1))
65             strncat (begin_tabular, "|", BUFSIZ -strlen (begin_tabular) -1);
66     }
67     strncat (begin_tabular, "}", BUFSIZ -strlen (begin_tabular) -1);
68     if (borders)
69         strncat (table, line, BUFSIZ * 2 -strlen (table) -1);
70     for (i = 0; i < rows; ++i) {
71         strncat (table, "\n\t", BUFSIZ * 2 -strlen (table) -1);
72         for (j = 0; j < cols; ++j) {
73             snprintf (tmp, BUFSIZ/8, "%d%d", i + 1, j + 1);
74             strncat (table, tmp, BUFSIZ * 2 -strlen (table) -1);
75             if (j != cols -1)
76                 strncat (table, " & ", BUFSIZ * 2 -strlen (table) -1);
77             else
78                 strncat (table, "\\\\", BUFSIZ * 2 -strlen (table) -1);
79         }
80         if (borders == 2 || (borders == 1 && i == rows -1))
81             strncat (table, line, BUFSIZ * 2 -strlen (table) -1);
82     }
83     strncat (result, begin_tabular, BUFSIZ *2 -strlen (result) -1);
84     strncat (result, table, BUFSIZ *2 -strlen (result) -1);
85     strncat (result, end_tabular, BUFSIZ *2 -strlen (result) -1);
86     return result;
87 }
88 
importer_generate_matrix(gint bracket,gint rows,gint cols)89 const gchar* importer_generate_matrix (gint bracket, gint rows, gint cols) {
90     gint i = 0, j = 0;
91     static gchar result[BUFSIZ * 2] = { 0 };
92     gchar tmp[BUFSIZ / 8];
93 
94     /* clear previous data */
95     result[0] = 0;
96 
97     strncat (result, "$\\begin{", BUFSIZ * 2 -strlen (result) -1);
98     strncat (result, bracket_type[bracket], BUFSIZ * 2 -strlen (result) -1);
99     strncat (result, "}", BUFSIZ * 2 - strlen (result) -1);
100 
101     for (i = 0; i < rows; ++i) {
102         strncat (result, "\n\t", BUFSIZ * 2 -strlen (result) -1);
103         for (j = 0; j < cols; ++j) {
104             snprintf (tmp, BUFSIZ/8, "%d%d", i + 1, j + 1);
105             strncat (result, tmp, BUFSIZ * 2 -strlen (result) -1);
106             if (j != cols -1)
107                 strncat (result, " & ", BUFSIZ * 2 -strlen (result) -1);
108             else
109                 strncat (result, "\\\\", BUFSIZ * 2 -strlen (result) -1);
110         }
111     }
112     strncat (result, "\n\\end{", BUFSIZ * 2 -strlen (result) -1);
113     strncat (result, bracket_type[bracket], BUFSIZ * 2 -strlen (result) -1);
114     strncat (result, "}$\n", BUFSIZ * 2 -strlen (result) -1);
115     return result;
116 }
117 
importer_generate_image(const gchar * filepath,const gchar * caption,const gchar * label,gdouble scale)118 const gchar* importer_generate_image (const gchar* filepath, const gchar* caption,
119                                       const gchar* label, gdouble scale) {
120     static gchar result[BUFSIZ] = { 0 };
121     gchar scale_str[16] = { 0 };
122     gchar* loc = NULL;
123 
124     /* clear previous data */
125     result[0] = 0;
126 
127     // Filepath notation corrections for Windows systems:
128     #ifdef WIN32
129     gchar* path = g_strjoinv("/", g_strsplit(filepath, "\\", -1));
130     if (utils_subinstr (" ", filepath, FALSE)) {
131         editor_insert_package (g_active_editor, "grffile", "space");
132     }
133     #endif
134 
135     snprintf (scale_str, 16, "%.2f", scale);
136 
137     /* some locales use ',' as seperator, replace them as '.' */
138     if ( (loc = strstr (scale_str, ",")))
139         *loc = '.';
140 
141     snprintf (result, BUFSIZ, "\\begin{figure}[htp]\n\\centering\n"
142         "\\includegraphics[scale=%s]{%s}\n\\caption{%s}\n\\label{%s}\n"
143         "\\end{figure}", scale_str, filepath, caption, label);
144     return result;
145 }
146