Debian Codegraph @

lib/fontconfig/font-manager-xml-writer.c

raw
1 /* font-manager-xml-writer.c
2 *
3 * Copyright (C) 2009-2024 Jerry Casiano
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 #include "font-manager-xml-writer.h"
22
23 /**
24 * SECTION: font-manager-xml-writer
25 * @short_description: Convenience class for generating Fontconfig xml files
26 * @title: Xml Writer
27 * @include: font-manager-xml-writer.h
28 * @see_also: https://www.freedesktop.org/software/fontconfig/fontconfig-user.html
29 *
30 * Convenience class for generating fontconfig configuration files.
31 */
32
33 struct _FontManagerXmlWriter
34 {
35 GObject parent;
36
37 gchar *filepath;
38 xmlTextWriter *writer;
39 };
40
41 G_DEFINE_TYPE(FontManagerXmlWriter, font_manager_xml_writer, G_TYPE_OBJECT)
42
43 enum
44 {
45 PROP_RESERVED,
46 PROP_FILEPATH,
47 N_PROPERTIES
48 };
49
50 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
51
52 static const gchar *DOCTYPE = "<!DOCTYPE fontconfig SYSTEM \"urn:fontconfig:fonts.dtd\">\n";
53 static const gchar *DOCGEN = " Generated by Font Manager. Do NOT edit this file. ";
54 static const gchar *DOCROOT = "fontconfig";
55
56 static void
57 font_manager_xml_writer_set_default_options (FontManagerXmlWriter *self)
58 {
59 xmlTextWriterSetIndent(self->writer, TRUE);
60 xmlTextWriterSetIndentString(self->writer, (const xmlChar *) " ");
61 xmlTextWriterStartDocument(self->writer, NULL, NULL, NULL);
62 xmlTextWriterWriteString(self->writer, (xmlChar *) DOCTYPE);
63 xmlTextWriterWriteComment(self->writer, (xmlChar *) DOCGEN);
64 xmlTextWriterStartElement(self->writer, (xmlChar *) DOCROOT);
65 return;
66 }
67
68 static void
69 font_manager_xml_writer_reset (FontManagerXmlWriter *self)
70 {
71 g_clear_pointer(&self->writer, xmlFreeTextWriter);
72 g_clear_pointer(&self->filepath, g_free);
73 return;
74 }
75
76 static void
77 font_manager_xml_writer_dispose (GObject *gobject)
78 {
79 g_return_if_fail(gobject != NULL);
80 font_manager_xml_writer_reset(FONT_MANAGER_XML_WRITER(gobject));
81 G_OBJECT_CLASS(font_manager_xml_writer_parent_class)->dispose(gobject);
82 return;
83 }
84
85 static void
86 font_manager_xml_writer_get_property (GObject *gobject,
87 guint property_id,
88 GValue *value,
89 GParamSpec *pspec)
90 {
91 g_return_if_fail(gobject != NULL);
92 switch (property_id)
93 {
94 case PROP_FILEPATH:
95 g_value_set_string(value, FONT_MANAGER_XML_WRITER(gobject)->filepath);
96 break;
97 default:
98 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
99 break;
100 }
101 return;
102 }
103
104 static void
105 font_manager_xml_writer_class_init (FontManagerXmlWriterClass *klass)
106 {
107 GObjectClass *object_class = G_OBJECT_CLASS(klass);
108 object_class->dispose = font_manager_xml_writer_dispose;
109 object_class->get_property = font_manager_xml_writer_get_property;
110
111 /**
112 * FontManagerXmlWriter:filepath:
113 *
114 * Path to file.
115 */
116 obj_properties[PROP_FILEPATH] = g_param_spec_string("filepath",
117 NULL,
118 "Filepath",
119 NULL,
120 G_PARAM_STATIC_STRINGS |
121 G_PARAM_READABLE |
122 G_PARAM_EXPLICIT_NOTIFY);
123
124 g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
125 return;
126 }
127
128 static void
129 font_manager_xml_writer_init (FontManagerXmlWriter *self)
130 {
131 self->writer = NULL;
132 self->filepath = NULL;
133 return;
134 }
135
136 /**
137 * font_manager_xml_writer_open:
138 * @self: #FontManagerXmlWriter
139 * @filepath: filepath to open for editing
140 *
141 * Returns: %TRUE on success
142 */
143 gboolean
144 font_manager_xml_writer_open (FontManagerXmlWriter *self, const gchar *filepath)
145 {
146 g_return_val_if_fail(self != NULL, FALSE);
147 g_return_val_if_fail(self->writer == NULL && self->filepath == NULL, FALSE);
148 self->writer = xmlNewTextWriterFilename(filepath, FALSE);
149 if (self->writer == NULL) {
150 g_critical(G_STRLOC ": Error opening %s", filepath);
151 return FALSE;
152 }
153 self->filepath = g_strdup(filepath);
154 font_manager_xml_writer_set_default_options(self);
155 return TRUE;
156 }
157
158 /**
159 * font_manager_xml_writer_close:
160 * @self: #FontManagerXmlWriter
161 *
162 * Save and close current document
163 *
164 * Returns: %TRUE if document was successfully saved
165 */
166 gboolean
167 font_manager_xml_writer_close (FontManagerXmlWriter *self)
168 {
169 g_return_val_if_fail(self != NULL, FALSE);
170 g_return_val_if_fail(self->writer != NULL, FALSE);
171 if (xmlTextWriterEndDocument(self->writer) < 0) {
172 g_critical(G_STRLOC ": Error closing %s", self->filepath);
173 return FALSE;
174 }
175 font_manager_xml_writer_reset(self);
176 return TRUE;
177 }
178
179 /**
180 * font_manager_xml_writer_discard:
181 * @self: #FontManagerXmlWriter
182 *
183 * Close current document without saving.
184 */
185 void
186 font_manager_xml_writer_discard (FontManagerXmlWriter *self)
187 {
188 g_return_if_fail(self != NULL);
189 font_manager_xml_writer_reset(self);
190 return;
191 }
192
193 /**
194 * font_manager_xml_writer_start_element:
195 * @self: #FontManagerXmlWriter
196 * @name: element name
197 *
198 * Start an xml element.
199 *
200 * Returns: The number of bytes written (may be 0 because of buffering) or -1 in case of error.
201 */
202 gint
203 font_manager_xml_writer_start_element (FontManagerXmlWriter *self, const gchar *name)
204 {
205 g_return_val_if_fail(self != NULL, -1);
206 g_return_val_if_fail(self->writer != NULL, -1);
207 g_return_val_if_fail(name != NULL, -1);
208 return xmlTextWriterStartElement(self->writer, (const xmlChar *) name);
209 }
210
211 /**
212 * font_manager_xml_writer_end_element:
213 * @self: #FontManagerXmlWriter
214 *
215 * End the current xml element.
216 *
217 * Returns: The number of bytes written (may be 0 because of buffering) or -1 in case of error.
218 */
219 gint
220 font_manager_xml_writer_end_element (FontManagerXmlWriter *self)
221 {
222 g_return_val_if_fail(self != NULL, -1);
223 g_return_val_if_fail(self->writer != NULL, -1);
224 return xmlTextWriterEndElement(self->writer);
225 }
226
227 /**
228 * font_manager_xml_writer_write_element:
229 * @self: #FontManagerXmlWriter
230 * @name: element name
231 * @content: element content
232 *
233 * Returns: The number of bytes written (may be 0 because of buffering) or -1 in case of error.
234 */
235 gint
236 font_manager_xml_writer_write_element (FontManagerXmlWriter *self,
237 const gchar *name,
238 const gchar *content)
239 {
240 g_return_val_if_fail(self != NULL, -1);
241 g_return_val_if_fail(self->writer != NULL, -1);
242 g_return_val_if_fail(name != NULL && content != NULL, -1);
243 return xmlTextWriterWriteElement(self->writer, (const xmlChar *) name, (const xmlChar *) content);
244 }
245
246 /**
247 * font_manager_xml_writer_write_attribute:
248 * @self: #FontManagerXmlWriter
249 * @name: attribute name
250 * @content: attribute content
251 *
252 * Returns: The number of bytes written (may be 0 because of buffering) or -1 in case of error.
253 */
254 gint
255 font_manager_xml_writer_write_attribute (FontManagerXmlWriter *self,
256 const gchar *name,
257 const gchar *content)
258 {
259 g_return_val_if_fail(self != NULL, -1);
260 g_return_val_if_fail(self->writer != NULL, -1);
261 g_return_val_if_fail(name != NULL && content != NULL, -1);
262 return xmlTextWriterWriteAttribute(self->writer, (const xmlChar *) name, (const xmlChar *) content);
263 }
264
265 /**
266 * font_manager_xml_writer_add_assignment:
267 * @self: #FontManagerXmlWriter
268 * @a_name: name of property to edit
269 * @a_type: type of the property
270 * @a_val: new value to assign
271 *
272 * Assign a new value to a Fontconfig property.
273 * Valid types are int, double, bool and string.
274 */
275 void
276 font_manager_xml_writer_add_assignment (FontManagerXmlWriter *self,
277 const gchar *a_name,
278 const gchar *a_type,
279 const gchar *a_val)
280 {
281 g_return_if_fail(self != NULL);
282 g_return_if_fail(self->writer != NULL);
283 g_return_if_fail(a_name != NULL && a_type != NULL && a_val != NULL);
284 xmlTextWriterStartElement(self->writer, (xmlChar *) "edit");
285 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "name", (xmlChar *) a_name);
286 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "mode", (xmlChar *) "assign");
287 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "binding", (xmlChar *) "strong");
288 xmlTextWriterWriteElement(self->writer, (xmlChar *) a_type, (xmlChar *) a_val);
289 xmlTextWriterEndElement(self->writer);
290 return;
291 }
292
293 gchar *
294 xml_escaped_text (char *str) {
295 g_autofree gchar *lt = font_manager_str_replace(str, "<", "&lt;");
296 g_autofree gchar *gt = font_manager_str_replace(lt, ">", "&gt;");
297 return font_manager_str_replace(gt, "&", "&amp;");
298 }
299
300 /**
301 * font_manager_xml_writer_add_elements:
302 * @self: #FontManagerXmlWriter
303 * @e_type: element type
304 * @elements: (element-type utf8) (transfer none): #GList
305 *
306 * Add simple elements to a fontconfig configuration file.
307 */
308 void
309 font_manager_xml_writer_add_elements (FontManagerXmlWriter *self,
310 const gchar *e_type,
311 GList *elements)
312 {
313 g_return_if_fail(self != NULL);
314 g_return_if_fail(self->writer != NULL);
315 g_return_if_fail(e_type != NULL);
316 GList *iter;
317 for (iter = elements; iter != NULL; iter = iter->next) {
318 g_autofree gchar *element = xml_escaped_text(g_strstrip(iter->data));
319 xmlTextWriterWriteElement(self->writer, (xmlChar *) e_type, (xmlChar *) element);
320 }
321 return;
322 }
323
324 /**
325 * font_manager_xml_writer_add_patelt:
326 * @self: #FontManagerXmlWriter
327 * @p_name: patelt name
328 * @p_type: patelt type
329 * @p_val: patelt value
330 *
331 * Write a valid fontconfig pattern elt.
332 * Valid patelt types are int, double, string, bool and const.
333 */
334 void
335 font_manager_xml_writer_add_patelt (FontManagerXmlWriter *self,
336 const gchar *p_name,
337 const gchar *p_type,
338 const gchar *p_val)
339 {
340 g_return_if_fail(self != NULL);
341 g_return_if_fail(self->writer != NULL);
342 g_return_if_fail(p_name != NULL && p_type != NULL && p_val != NULL);
343 xmlTextWriterStartElement(self->writer, (xmlChar *) "pattern");
344 xmlTextWriterStartElement(self->writer, (xmlChar *) "patelt");
345 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "name", (xmlChar *) p_name);
346 xmlTextWriterWriteElement(self->writer, (xmlChar *) p_type, (xmlChar *) p_val);
347 xmlTextWriterEndElement(self->writer);
348 xmlTextWriterEndElement(self->writer);
349 return;
350 }
351
352 /**
353 * font_manager_xml_writer_add_selections:
354 * @self: #FontManagerXmlWriter
355 * @selection_type: acceptfont or rejectfont
356 * @selections: (element-type utf8) (transfer none): #GList containing font family names
357 *
358 * Whitelist or blacklist a #GList of font families.
359 */
360 void
361 font_manager_xml_writer_add_selections (FontManagerXmlWriter *self,
362 const gchar *selection_type,
363 GList *selections)
364 {
365 g_return_if_fail(self != NULL);
366 g_return_if_fail(self->writer != NULL);
367 g_return_if_fail(selection_type != NULL);
368 xmlTextWriterStartElement(self->writer, (xmlChar *) "selectfont");
369 xmlTextWriterStartElement(self->writer, (xmlChar *) selection_type);
370 GList *iter;
371 for (iter = selections; iter != NULL; iter = iter->next) {
372 g_autofree gchar *element = xml_escaped_text(g_strstrip(iter->data));
373 font_manager_xml_writer_add_patelt(self, "family", "string", element);
374 }
375 xmlTextWriterEndElement(self->writer);
376 xmlTextWriterEndElement(self->writer);
377 return;
378 }
379
380 /**
381 * font_manager_xml_writer_add_test_element:
382 * @self: #FontManagerXmlWriter
383 * @t_name: fontconfig property to test
384 * @t_test: valid comparison operator
385 * @t_type: value type
386 * @t_val: value
387 *
388 * Valid comparison operators can be one of eq, not_eq, less, less_eq,
389 * more, more_eq, contains or not_contains.
390 * Valid value types are int, double, string, bool and const.
391 */
392 void
393 font_manager_xml_writer_add_test_element (FontManagerXmlWriter *self,
394 const gchar *t_name,
395 const gchar *t_test,
396 const gchar *t_type,
397 const gchar *t_val)
398 {
399 g_return_if_fail(self != NULL);
400 g_return_if_fail(self->writer != NULL);
401 g_return_if_fail(t_name != NULL && t_test != NULL && t_type != NULL && t_val != NULL);
402 xmlTextWriterStartElement(self->writer, (xmlChar *) "test");
403 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "name", (xmlChar *) t_name);
404 xmlTextWriterWriteAttribute(self->writer, (xmlChar *) "compare", (xmlChar *) t_test);
405 xmlTextWriterWriteElement(self->writer, (xmlChar *) t_type, (xmlChar *) t_val);
406 xmlTextWriterEndElement(self->writer);
407 return;
408 }
409
410 /**
411 * font_manager_xml_writer_new:
412 *
413 * Returns: (transfer full): A newly created #FontManagerXmlWriter.
414 * Free the returned object using #g_object_unref().
415 **/
416 FontManagerXmlWriter *
417 font_manager_xml_writer_new (void)
418 {
419 return g_object_new(FONT_MANAGER_TYPE_XML_WRITER, NULL);
420 }
421