1 /* imposter (OO.org Impress viewer)
2 ** Copyright (C) 2003-2005 Gurer Ozen
3 ** This code is free software; you can redistribute it and/or
4 ** modify it under the terms of GNU General Public License.
25 struct Span *last_span;
35 struct Line *last_line;
40 add_line(struct Layout *lay)
44 line = iks_stack_alloc(lay->s, sizeof(struct Line));
45 memset(line, 0, sizeof(struct Line));
47 if (!lay->lines) lay->lines = line;
48 if (lay->last_line) lay->last_line->next = line;
49 lay->last_line = line;
55 add_span(struct Layout *lay, char *text, int len, int size, int styles)
60 span = iks_stack_alloc(lay->s, sizeof(struct Span));
61 memset(span, 0, sizeof(struct Span));
65 span->styles = styles;
67 line = lay->last_line;
68 if (!line) line = add_line(lay);
70 span->x = line->last_span->x + line->last_span->w;
71 span->y = line->last_span->y;
77 if (!line->spans) line->spans = span;
78 if (line->last_span) line->last_span->next = span;
79 line->last_span = span;
85 calc_sizes(ImpRenderCtx *ctx, void *drw_data, struct Layout *lay)
90 for (line = lay->lines; line; line = line->next) {
91 for (span = line->spans; span; span = span->next) {
92 ctx->drw->get_text_size(drw_data,
93 span->text, span->len,
94 span->size, span->styles,
98 if (span->h > line->h) line->h = span->h;
100 if (line->w > lay->tw) lay->tw = line->w;
106 calc_pos(ImpRenderCtx *ctx, struct Layout *lay)
114 for (line = lay->lines; line; line = line->next) {
119 for (span = line->spans; span; span = span->next) {
128 _imp_draw_layout(ImpRenderCtx *ctx, void *drw_data, struct Layout *lay)
133 for (line = lay->lines; line; line = line->next) {
134 for (span = line->spans; span; span = span->next) {
135 ctx->drw->set_fg_color(drw_data, &span->fg);
136 ctx->drw->draw_text(drw_data,
138 span->text, span->len,
147 text_span(ImpRenderCtx *ctx, struct Layout *lay, iks *node, char *text, size_t len)
152 int px = 0, cont = 1;
153 int styles = IMP_NORMAL;
155 attr = r_get_style(ctx, node, "fo:font-size");
158 if (strstr(attr, "pt")) cm = cm * 2.54 / 102;
159 px = cm * ctx->fact_y;
161 attr = r_get_style(ctx, node, "fo:font-weight");
162 if (attr && strcmp(attr, "bold") == 0) styles |= IMP_BOLD;
163 attr = r_get_style(ctx, node, "style:text-underline");
164 if (attr && strcmp(attr, "single") == 0) styles |= IMP_UNDERLINE;
165 attr = r_get_style(ctx, node, "fo:font-style");
166 if (attr && strcmp(attr, "italic") == 0) styles |= IMP_ITALIC;
173 span = add_span(lay, t, len2, px, styles);
178 span = add_span(lay, text, len, px, styles);
181 r_get_color(ctx, node, "fo:color", &span->fg);
186 text_p(ImpRenderCtx *ctx, struct Layout *lay, iks *node)
191 for (n = iks_child(node); n; n = iks_next(n)) {
192 if (iks_type(n) == IKS_CDATA) {
193 text_span(ctx, lay, node, iks_cdata(n), iks_cdata_size(n));
194 } else if (iks_strcmp(iks_name(n), "text:span") == 0) {
195 for (n2 = iks_child(n); n2; n2 = iks_next(n2)) {
196 if (iks_type(n2) == IKS_CDATA) {
197 text_span(ctx, lay, n2, iks_cdata(n2), iks_cdata_size(n2));
198 } else if (iks_strcmp(iks_name(n2), "text:s") == 0) {
201 attr = iks_find_attrib(n2, "text:c");
202 if (attr) c = atoi(attr);
207 text_span(ctx, lay, n, lay->spaces, c);
208 } else if (iks_strcmp(iks_name(n2), "text:a") == 0) {
209 text_span(ctx, lay, n, iks_cdata(iks_child(n2)), iks_cdata_size(iks_child(n2)));
210 } else if (iks_strcmp(iks_name(n2), "text:tab-stop") == 0) {
211 text_span(ctx, lay, n, "\t", 1);
212 } else if (iks_strcmp(iks_name(n2), "text:page-number") == 0) {
214 sprintf(buf, "%d", ctx->page->nr);
215 text_span(ctx, lay, n, iks_stack_strdup(lay->s, buf, 0), strlen(buf));
218 } else if (iks_strcmp(iks_name(n), "text:line-break") == 0) {
220 } else if (iks_strcmp(iks_name(n), "text:a") == 0) {
221 text_span(ctx, lay, n, iks_cdata(iks_child(n)), iks_cdata_size(iks_child(n)));
222 } else if (iks_strcmp(iks_name(n), "text:page-number") == 0) {
224 sprintf(buf, "%d", ctx->page->nr);
225 text_span(ctx, lay, n, iks_stack_strdup(lay->s, buf, 0), strlen(buf));
231 text_list(ImpRenderCtx *ctx, struct Layout *lay, iks *node)
235 for (n = iks_first_tag(node); n; n = iks_next_tag(n)) {
236 for (n2 = iks_first_tag(n); n2; n2 = iks_next_tag(n2)) {
237 if (strcmp(iks_name(n2), "text:p") == 0) {
238 text_p(ctx, lay, n2);
239 } else if (strcmp(iks_name(n2), "text:ordered-list") == 0) {
240 text_list(ctx, lay, n2);
241 } else if (strcmp(iks_name(n2), "text:unordered-list") == 0) {
242 text_list(ctx, lay, n2);
243 } else if (strcmp(iks_name(n2), "text:list") == 0) {
244 text_list(ctx, lay, n2);
251 r_text(ImpRenderCtx *ctx, void *drw_data, iks *node)
256 memset(&lay, 0, sizeof(struct Layout));
257 memset(&lay.spaces, ' ', 128);
258 lay.s = iks_stack_new(sizeof(struct Span) * 16, 0);
259 lay.x = r_get_x(ctx, node, "svg:x");
260 lay.y = r_get_y(ctx, node, "svg:y");
261 lay.w = r_get_y(ctx, node, "svg:width");
262 lay.h = r_get_y(ctx, node, "svg:height");
264 for (n = iks_first_tag(node); n; n = iks_next_tag(n)) {
265 if (strcmp(iks_name(n), "text:p") == 0) {
266 text_p(ctx, &lay, n);
267 } else if (strcmp(iks_name(n), "text:ordered-list") == 0) {
268 text_list(ctx, &lay, n);
269 } else if (strcmp(iks_name(n), "text:unordered-list") == 0) {
270 text_list(ctx, &lay, n);
271 } else if (strcmp(iks_name(n), "text:list") == 0) {
272 text_list(ctx, &lay, n);
276 calc_sizes(ctx, drw_data, &lay);
278 _imp_draw_layout(ctx, drw_data, &lay);
280 iks_stack_delete(lay.s);
284 text_span (render_ctx *ctx, text_ctx *tc, struct layout_s *lout, iks *node, char *text, int len)
286 if (tc->bullet_flag && tc->bullet_sz) size = tc->bullet_sz; else size = r_get_font_size (ctx, tc, node);
290 is_animated (render_ctx *ctx, text_ctx *tc, iks *node)
292 if (!ctx->step_mode) return 0;
293 if (!tc->id) return 0;
294 while (strcmp (iks_name (node), "draw:page") != 0
295 && strcmp (iks_name (node), "style:master-page") != 0)
296 node = iks_parent (node);
297 node = iks_find (node, "presentation:animations");
299 if (iks_find_with_attrib (node, "presentation:show-text", "draw:shape-id", tc->id)) return 1;
304 text_p (render_ctx *ctx, text_ctx *tc, iks *node)
306 if (is_animated (ctx, tc, node) && ctx->step_cnt >= ctx->step) lout->flag = 0;
309 attr = r_get_style (ctx, node, "text:enable-numbering");
310 if (attr && strcmp (attr, "true") == 0) {
311 if (iks_child (node) && tc->bullet) {
313 text_span (ctx, tc, lout, node, tc->bullet, strlen (tc->bullet));
314 text_span (ctx, tc, lout, node, " ", 1);
321 attr = r_get_style (ctx, node, "fo:line-height");
323 int ratio = atoi (attr);
328 tc->layouts = g_list_append (tc->layouts, lout);
329 // g_object_unref (lout->play);
330 // iks_stack_delete (s);
334 attr = r_get_style (ctx, node, "fo:text-align");
336 if (strcmp (attr, "center") == 0)
337 pango_layout_set_alignment (lout->play, PANGO_ALIGN_CENTER);
338 else if (strcmp (attr, "end") == 0)
339 pango_layout_set_alignment (lout->play, PANGO_ALIGN_RIGHT);
341 pango_layout_set_width (lout->play, tc->w * PANGO_SCALE);
342 pango_layout_set_markup (lout->play, lout->text, lout->text_len);
343 pango_layout_get_pixel_size (lout->play, &lout->w, &lout->h);
344 attr = r_get_style (ctx, node, "fo:line-height");
346 int ratio = atoi (attr);
351 tc->layouts = g_list_append (tc->layouts, lout);
355 find_bullet (render_ctx *ctx, text_ctx *tc, iks *node)
359 x = r_get_bullet (ctx, node, "text:list-level-style-bullet");
360 x = iks_find (x, "text:list-level-style-bullet");
361 t = iks_find_attrib (x, "text:bullet-char");
362 if (t) tc->bullet = t; else tc->bullet = "*";
363 x = iks_find (x, "style:properties");
364 t = iks_find_attrib (x, "fo:font-size");
365 if (t) tc->bullet_sz = tc->last_sz * atoi (t) / 100;
366 else tc->bullet_sz = 0;
370 r_text (render_ctx *ctx, iks *node)
372 tc.id = iks_find_attrib (node, "draw:id");
374 for (n = iks_first_tag (node); n; n = iks_next_tag (n)) {
375 if (strcmp (iks_name (n), "text:p") == 0) {
376 text_p (ctx, &tc, n);
377 } else if (strcmp (iks_name (n), "text:ordered-list") == 0) {
378 text_list (ctx, &tc, n);
379 } else if (strcmp (iks_name (n), "text:unordered-list") == 0) {
380 find_bullet (ctx, &tc, n);
381 text_list (ctx, &tc, n);