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 <stdarg.h>
22 #include <stdlib.h>
23
24 #include "libpspp/cast.h"
25 #include "libpspp/pool.h"
26 #include "output/driver.h"
27 #include "output/output-item-provider.h"
28 #include "output/table.h"
29 #include "output/table-item.h"
30 #include "output/table-provider.h"
31
32 #include "gl/xalloc.h"
33 #include "gl/xvasprintf.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37
38 const char *
text_item_type_to_string(enum text_item_type type)39 text_item_type_to_string (enum text_item_type type)
40 {
41 switch (type)
42 {
43 case TEXT_ITEM_PAGE_TITLE:
44 return _("Page Title");
45
46 case TEXT_ITEM_TITLE:
47 return _("Title");
48
49 case TEXT_ITEM_SYNTAX:
50 case TEXT_ITEM_LOG:
51 return _("Log");
52
53 case TEXT_ITEM_EJECT_PAGE:
54 return _("Page Break");
55
56 default:
57 return _("Text");
58 }
59 }
60
61 /* Creates and returns a new text item containing TEXT and the specified TYPE.
62 The new text item takes ownership of TEXT. */
63 struct text_item *
text_item_create_nocopy(enum text_item_type type,char * text)64 text_item_create_nocopy (enum text_item_type type, char *text)
65 {
66 struct text_item *item = xzalloc (sizeof *item);
67 output_item_init (&item->output_item, &text_item_class);
68 item->text = text;
69 item->type = type;
70 return item;
71 }
72
73 /* Creates and returns a new text item containing a copy of TEXT and the
74 specified TYPE. The caller retains ownership of TEXT. */
75 struct text_item *
text_item_create(enum text_item_type type,const char * text)76 text_item_create (enum text_item_type type, const char *text)
77 {
78 return text_item_create_nocopy (type, xstrdup (text));
79 }
80
81 /* Creates and returns a new text item containing a copy of FORMAT, which is
82 formatted as if by printf(), and the specified TYPE. The caller retains
83 ownership of FORMAT. */
84 struct text_item *
text_item_create_format(enum text_item_type type,const char * format,...)85 text_item_create_format (enum text_item_type type, const char *format, ...)
86 {
87 struct text_item *item;
88 va_list args;
89
90 va_start (args, format);
91 item = text_item_create_nocopy (type, xvasprintf (format, args));
92 va_end (args);
93
94 return item;
95 }
96
97 /* Returns ITEM's type. */
98 enum text_item_type
text_item_get_type(const struct text_item * item)99 text_item_get_type (const struct text_item *item)
100 {
101 return item->type;
102 }
103
104 /* Returns ITEM's text, which the caller may not modify or free. */
105 const char *
text_item_get_text(const struct text_item * item)106 text_item_get_text (const struct text_item *item)
107 {
108 return item->text;
109 }
110
111 /* Submits ITEM to the configured output drivers, and transfers ownership to
112 the output subsystem. */
113 void
text_item_submit(struct text_item * item)114 text_item_submit (struct text_item *item)
115 {
116 output_submit (&item->output_item);
117 }
118
119 struct table_item *
text_item_to_table_item(struct text_item * text_item)120 text_item_to_table_item (struct text_item *text_item)
121 {
122 struct table *tab = table_create (1, 1, 0, 0, 0, 0);
123
124 struct area_style *style = pool_alloc (tab->container, sizeof *style);
125 *style = (struct area_style) { AREA_STYLE_INITIALIZER__,
126 .cell_style.halign = TABLE_HALIGN_LEFT };
127 struct font_style *font_style = &style->font_style;
128 if (text_item->typeface)
129 font_style->typeface = pool_strdup (tab->container, text_item->typeface);
130 font_style->size = text_item->size;
131 font_style->bold = text_item->bold;
132 font_style->italic = text_item->italic;
133 font_style->underline = text_item->underline;
134 font_style->markup = text_item->markup;
135 tab->styles[0] = style;
136
137 int opts = 0;
138 if (text_item->markup)
139 opts |= TAB_MARKUP;
140 if (text_item->type == TEXT_ITEM_SYNTAX || text_item->type == TEXT_ITEM_LOG)
141 opts |= TAB_FIX;
142 table_text (tab, 0, 0, opts, text_item_get_text (text_item));
143 struct table_item *table_item = table_item_create (tab, NULL, NULL);
144 text_item_unref (text_item);
145 return table_item;
146 }
147
148 static void
text_item_destroy(struct output_item * output_item)149 text_item_destroy (struct output_item *output_item)
150 {
151 struct text_item *item = to_text_item (output_item);
152 free (item->text);
153 free (item->typeface);
154 free (item);
155 }
156
157 const struct output_item_class text_item_class =
158 {
159 "text",
160 text_item_destroy,
161 };
162