output: Support decimal and mixed alignment,
[pspp] / src / output / text-item.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "output/text-item.h"
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdlib.h>
24
25 #include "libpspp/cast.h"
26 #include "libpspp/pool.h"
27 #include "output/driver.h"
28 #include "output/output-item-provider.h"
29 #include "output/tab.h"
30 #include "output/table-item.h"
31 #include "output/table-provider.h"
32
33 #include "gl/xalloc.h"
34 #include "gl/xvasprintf.h"
35
36 #include "gettext.h"
37 #define _(msgid) gettext (msgid)
38
39 /* Creates and returns a new text item containing TEXT and the specified TYPE.
40    The new text item takes ownership of TEXT. */
41 struct text_item *
42 text_item_create_nocopy (enum text_item_type type, char *text)
43 {
44   struct text_item *item = xzalloc (sizeof *item);
45   output_item_init (&item->output_item, &text_item_class);
46   item->text = text;
47   item->type = type;
48   return item;
49 }
50
51 /* Creates and returns a new text item containing a copy of TEXT and the
52    specified TYPE.  The caller retains ownership of TEXT. */
53 struct text_item *
54 text_item_create (enum text_item_type type, const char *text)
55 {
56   return text_item_create_nocopy (type, xstrdup (text));
57 }
58
59 /* Creates and returns a new text item containing a copy of FORMAT, which is
60    formatted as if by printf(), and the specified TYPE.  The caller retains
61    ownership of FORMAT. */
62 struct text_item *
63 text_item_create_format (enum text_item_type type, const char *format, ...)
64 {
65   struct text_item *item;
66   va_list args;
67
68   va_start (args, format);
69   item = text_item_create_nocopy (type, xvasprintf (format, args));
70   va_end (args);
71
72   return item;
73 }
74
75 /* Returns ITEM's type. */
76 enum text_item_type
77 text_item_get_type (const struct text_item *item)
78 {
79   return item->type;
80 }
81
82 /* Returns ITEM's text, which the caller may not modify or free. */
83 const char *
84 text_item_get_text (const struct text_item *item)
85 {
86   return item->text;
87 }
88
89 /* Submits ITEM to the configured output drivers, and transfers ownership to
90    the output subsystem. */
91 void
92 text_item_submit (struct text_item *item)
93 {
94   output_submit (&item->output_item);
95 }
96
97 struct table_item *
98 text_item_to_table_item (struct text_item *text_item)
99 {
100   struct tab_table *tab = tab_create (1, 1);
101
102   struct area_style *style = pool_alloc (tab->container, sizeof *style);
103   *style = (struct area_style) AREA_STYLE_INITIALIZER;
104   struct font_style *font_style = &style->font_style;
105   if (text_item->typeface)
106     font_style->typeface = pool_strdup (tab->container, text_item->typeface);
107   font_style->size = text_item->size;
108   font_style->bold = text_item->bold;
109   font_style->italic = text_item->italic;
110   font_style->underline = text_item->underline;
111   font_style->markup = text_item->markup;
112   tab->styles[0] = style;
113
114   int opts = TAB_LEFT;
115   if (text_item->markup)
116     opts |= TAB_MARKUP;
117   if (text_item->type == TEXT_ITEM_SYNTAX || text_item->type == TEXT_ITEM_LOG)
118     opts |= TAB_FIX;
119   tab_text (tab, 0, 0, opts, text_item_get_text (text_item));
120   struct table_item *table_item = table_item_create (&tab->table, NULL, NULL);
121   text_item_unref (text_item);
122   return table_item;
123 }
124 \f
125 static void
126 text_item_destroy (struct output_item *output_item)
127 {
128   struct text_item *item = to_text_item (output_item);
129   free (item->text);
130   free (item->typeface);
131   free (item);
132 }
133
134 const struct output_item_class text_item_class =
135   {
136     text_item_destroy,
137   };