NetSurf
save_draw.c
Go to the documentation of this file.
1/*
2 * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
3 * Copyright 2004-2008 John Tytgat <joty@netsurf-browser.org>
4 * Copyright 2007 James Bursa <bursa@users.sourceforge.net>
5 *
6 * This file is part of NetSurf, http://www.netsurf-browser.org/
7 *
8 * NetSurf is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
11 *
12 * NetSurf is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21/**
22 * \file
23 * Export a content as a DrawFile (implementation).
24 */
25
26#ifdef WITH_DRAW_EXPORT
27
28#include <assert.h>
29#include <limits.h>
30#include <oslib/draw.h>
31#include <oslib/osfile.h>
32#include <pencil.h>
33
34#include "utils/log.h"
35#include "netsurf/plotters.h"
36#include "netsurf/content.h"
37
38#include "riscos/bitmap.h"
39#include "riscos/gui.h"
40#include "riscos/save_draw.h"
41#include "riscos/font.h"
42
43
44static struct pencil_diagram *ro_save_draw_diagram;
45static int ro_save_draw_width;
46static int ro_save_draw_height;
47
48
49/**
50 * Report an error from pencil.
51 *
52 * \param code error code
53 * \return false
54 */
55static nserror ro_save_draw_error(pencil_code code)
56{
57 NSLOG(netsurf, INFO, "code %i", code);
58
59 switch (code) {
60 case pencil_OK:
61 assert(0);
62 break;
63
64 case pencil_OUT_OF_MEMORY:
65 ro_warn_user("NoMemory", 0);
66 break;
67
68 case pencil_FONT_MANAGER_ERROR:
69 ro_warn_user("SaveError", rufl_fm_error->errmess);
70 break;
71
72 case pencil_FONT_NOT_FOUND:
73 case pencil_IO_ERROR:
74 case pencil_IO_EOF:
75 ro_warn_user("SaveError", "generating the DrawFile failed");
76 break;
77 }
78
79 return NSERROR_INVALID;
80}
81
82/**
83 * \brief Sets a clip rectangle for subsequent plot operations.
84 *
85 * \param ctx The current redraw context.
86 * \param clip The rectangle to limit all subsequent plot
87 * operations within.
88 * \return NSERROR_OK on success else error code.
89 */
90static nserror
91ro_save_draw_clip(const struct redraw_context *ctx, const struct rect *clip)
92{
93 return NSERROR_OK;
94}
95
96
97/**
98 * Plots an arc
99 *
100 * plot an arc segment around (x,y), anticlockwise from angle1
101 * to angle2. Angles are measured anticlockwise from
102 * horizontal, in degrees.
103 *
104 * \param ctx The current redraw context.
105 * \param pstyle Style controlling the arc plot.
106 * \param x The x coordinate of the arc.
107 * \param y The y coordinate of the arc.
108 * \param radius The radius of the arc.
109 * \param angle1 The start angle of the arc.
110 * \param angle2 The finish angle of the arc.
111 * \return NSERROR_OK on success else error code.
112 */
113static nserror
114ro_save_draw_arc(const struct redraw_context *ctx,
115 const plot_style_t *style,
116 int x, int y, int radius, int angle1, int angle2)
117{
118 return NSERROR_OK;
119}
120
121
122/**
123 * Plots a circle
124 *
125 * Plot a circle centered on (x,y), which is optionally filled.
126 *
127 * \param ctx The current redraw context.
128 * \param pstyle Style controlling the circle plot.
129 * \param x The x coordinate of the circle.
130 * \param y The y coordinate of the circle.
131 * \param radius The radius of the circle.
132 * \return NSERROR_OK on success else error code.
133 */
134static nserror
135ro_save_draw_disc(const struct redraw_context *ctx,
136 const plot_style_t *style,
137 int x, int y, int radius)
138{
139 return NSERROR_OK;
140}
141
142
143/**
144 * Plots a line
145 *
146 * plot a line from (x0,y0) to (x1,y1). Coordinates are at
147 * centre of line width/thickness.
148 *
149 * \param ctx The current redraw context.
150 * \param pstyle Style controlling the line plot.
151 * \param line A rectangle defining the line to be drawn
152 * \return NSERROR_OK on success else error code.
153 */
154static nserror
155ro_save_draw_line(const struct redraw_context *ctx,
156 const plot_style_t *style,
157 const struct rect *line)
158{
159 pencil_code code;
160 const int path[] = {
161 draw_MOVE_TO, line->x0 * 2, -line->y0 * 2 - 1,
162 draw_LINE_TO, line->x1 * 2, -line->y1 * 2 - 1,
163 draw_END_PATH
164 };
165
166 code = pencil_path(ro_save_draw_diagram,
167 path,
168 sizeof path / sizeof path[0],
169 pencil_TRANSPARENT,
170 style->stroke_colour << 8,
172 pencil_JOIN_MITRED,
173 pencil_CAP_BUTT,
174 pencil_CAP_BUTT,
175 0, 0, false,
176 pencil_SOLID);
177 if (code != pencil_OK)
178 return ro_save_draw_error(code);
179
180 return NSERROR_OK;
181}
182
183
184/**
185 * Plots a rectangle.
186 *
187 * The rectangle can be filled an outline or both controlled
188 * by the plot style The line can be solid, dotted or
189 * dashed. Top left corner at (x0,y0) and rectangle has given
190 * width and height.
191 *
192 * \param ctx The current redraw context.
193 * \param pstyle Style controlling the rectangle plot.
194 * \param rect A rectangle defining the line to be drawn
195 * \return NSERROR_OK on success else error code.
196 */
197static nserror
198ro_save_draw_rectangle(const struct redraw_context *ctx,
199 const plot_style_t *style,
200 const struct rect *rect)
201{
202 pencil_code code;
203 const int path[] = {
204 draw_MOVE_TO, rect->x0 * 2, -rect->y0 * 2 - 1,
205 draw_LINE_TO, rect->x1 * 2, -rect->y0 * 2 - 1,
206 draw_LINE_TO, rect->x1 * 2, -rect->y1 * 2 - 1,
207 draw_LINE_TO, rect->x0 * 2, -rect->y1 * 2 - 1,
208 draw_CLOSE_LINE,
209 draw_END_PATH
210 };
211
212 if (style->fill_type != PLOT_OP_TYPE_NONE) {
213
214 code = pencil_path(ro_save_draw_diagram,
215 path,
216 sizeof path / sizeof path[0],
217 style->fill_colour << 8,
218 pencil_TRANSPARENT,
219 0,
220 pencil_JOIN_MITRED,
221 pencil_CAP_BUTT,
222 pencil_CAP_BUTT,
223 0,
224 0,
225 false,
226 pencil_SOLID);
227 if (code != pencil_OK)
228 return ro_save_draw_error(code);
229 }
230
231 if (style->stroke_type != PLOT_OP_TYPE_NONE) {
232
233 code = pencil_path(ro_save_draw_diagram,
234 path,
235 sizeof path / sizeof path[0],
236 pencil_TRANSPARENT,
237 style->stroke_colour << 8,
239 pencil_JOIN_MITRED,
240 pencil_CAP_BUTT,
241 pencil_CAP_BUTT,
242 0,
243 0,
244 false,
245 pencil_SOLID);
246
247 if (code != pencil_OK)
248 return ro_save_draw_error(code);
249 }
250 return NSERROR_OK;
251}
252
253
254/**
255 * Plot a polygon
256 *
257 * Plots a filled polygon with straight lines between
258 * points. The lines around the edge of the ploygon are not
259 * plotted. The polygon is filled with the non-zero winding
260 * rule.
261 *
262 * \param ctx The current redraw context.
263 * \param pstyle Style controlling the polygon plot.
264 * \param p verticies of polygon
265 * \param n number of verticies.
266 * \return NSERROR_OK on success else error code.
267 */
268static nserror
269ro_save_draw_polygon(const struct redraw_context *ctx,
270 const plot_style_t *style,
271 const int *p,
272 unsigned int n)
273{
274 pencil_code code;
275 int path[n * 3 + 1];
276 unsigned int i;
277
278 for (i = 0; i != n; i++) {
279 path[i * 3 + 0] = draw_LINE_TO;
280 path[i * 3 + 1] = p[i * 2 + 0] * 2;
281 path[i * 3 + 2] = -p[i * 2 + 1] * 2;
282 }
283 path[0] = draw_MOVE_TO;
284 path[n * 3] = draw_END_PATH;
285
286 code = pencil_path(ro_save_draw_diagram,
287 path, n * 3 + 1,
288 style->fill_colour << 8,
289 pencil_TRANSPARENT,
290 0,
291 pencil_JOIN_MITRED,
292 pencil_CAP_BUTT,
293 pencil_CAP_BUTT,
294 0,
295 0,
296 false,
297 pencil_SOLID);
298 if (code != pencil_OK)
299 return ro_save_draw_error(code);
300
301 return NSERROR_OK;
302}
303
304
305/**
306 * Plots a path.
307 *
308 * Path plot consisting of cubic Bezier curves. Line and fill colour is
309 * controlled by the plot style.
310 *
311 * \param ctx The current redraw context.
312 * \param pstyle Style controlling the path plot.
313 * \param p elements of path
314 * \param n nunber of elements on path
315 * \param transform A transform to apply to the path.
316 * \return NSERROR_OK on success else error code.
317 */
318static nserror
319ro_save_draw_path(const struct redraw_context *ctx,
320 const plot_style_t *pstyle,
321 const float *p,
322 unsigned int n,
323 const float transform[6])
324{
325 pencil_code code;
326 int *path;
327 unsigned int i;
328 bool empty_path = true;
329
330 if (n == 0)
331 return NSERROR_OK;
332
333 if (p[0] != PLOTTER_PATH_MOVE) {
334 NSLOG(netsurf, INFO, "path doesn't start with a move");
335 return NSERROR_INVALID;
336 }
337
338 path = malloc(sizeof *path * (n + 10));
339 if (!path) {
340 NSLOG(netsurf, INFO, "out of memory");
341 return NSERROR_INVALID;
342 }
343
344 for (i = 0; i < n; ) {
345 if (p[i] == PLOTTER_PATH_MOVE) {
346 path[i] = draw_MOVE_TO;
347 path[i + 1] = (transform[0] * p[i + 1] +
348 transform[2] * -p[i + 2] +
349 transform[4]) * 2;
350 path[i + 2] = (transform[1] * p[i + 1] +
351 transform[3] * -p[i + 2] +
352 -transform[5]) * 2;
353 i += 3;
354 } else if (p[i] == PLOTTER_PATH_CLOSE) {
355 path[i] = draw_CLOSE_LINE;
356 i++;
357 } else if (p[i] == PLOTTER_PATH_LINE) {
358 path[i] = draw_LINE_TO;
359 path[i + 1] = (transform[0] * p[i + 1] +
360 transform[2] * -p[i + 2] +
361 transform[4]) * 2;
362 path[i + 2] = (transform[1] * p[i + 1] +
363 transform[3] * -p[i + 2] +
364 -transform[5]) * 2;
365 i += 3;
366 empty_path = false;
367 } else if (p[i] == PLOTTER_PATH_BEZIER) {
368 path[i] = draw_BEZIER_TO;
369 path[i + 1] = (transform[0] * p[i + 1] +
370 transform[2] * -p[i + 2] +
371 transform[4]) * 2;
372 path[i + 2] = (transform[1] * p[i + 1] +
373 transform[3] * -p[i + 2] +
374 -transform[5]) * 2;
375 path[i + 3] = (transform[0] * p[i + 3] +
376 transform[2] * -p[i + 4] +
377 transform[4]) * 2;
378 path[i + 4] = (transform[1] * p[i + 3] +
379 transform[3] * -p[i + 4] +
380 -transform[5]) * 2;
381 path[i + 5] = (transform[0] * p[i + 5] +
382 transform[2] * -p[i + 6] +
383 transform[4]) * 2;
384 path[i + 6] = (transform[1] * p[i + 5] +
385 transform[3] * -p[i + 6] +
386 -transform[5]) * 2;
387 i += 7;
388 empty_path = false;
389 } else {
390 NSLOG(netsurf, INFO, "bad path command %f", p[i]);
391 free(path);
392 return NSERROR_INVALID;
393 }
394 }
395 path[i] = draw_END_PATH;
396
397 if (empty_path) {
398 free(path);
399 return NSERROR_OK;
400 }
401
402 code = pencil_path(ro_save_draw_diagram,
403 path, i + 1,
404 pstyle->fill_colour == NS_TRANSPARENT ?
405 pencil_TRANSPARENT :
406 pstyle->fill_colour << 8,
407 pstyle->stroke_colour == NS_TRANSPARENT ?
408 pencil_TRANSPARENT :
409 pstyle->stroke_colour << 8,
411 pencil_JOIN_MITRED,
412 pencil_CAP_BUTT,
413 pencil_CAP_BUTT,
414 0,
415 0,
416 false,
417 pencil_SOLID);
418 free(path);
419 if (code != pencil_OK)
420 return ro_save_draw_error(code);
421
422 return NSERROR_OK;
423}
424
425
426/**
427 * Plot a bitmap
428 *
429 * Tiled plot of a bitmap image. (x,y) gives the top left
430 * coordinate of an explicitly placed tile. From this tile the
431 * image can repeat in all four directions -- up, down, left
432 * and right -- to the extents given by the current clip
433 * rectangle.
434 *
435 * The bitmap_flags say whether to tile in the x and y
436 * directions. If not tiling in x or y directions, the single
437 * image is plotted. The width and height give the dimensions
438 * the image is to be scaled to.
439 *
440 * \param ctx The current redraw context.
441 * \param bitmap The bitmap to plot
442 * \param x The x coordinate to plot the bitmap
443 * \param y The y coordiante to plot the bitmap
444 * \param width The width of area to plot the bitmap into
445 * \param height The height of area to plot the bitmap into
446 * \param bg the background colour to alpha blend into
447 * \param flags the flags controlling the type of plot operation
448 * \return NSERROR_OK on success else error code.
449 */
450static nserror
451ro_save_draw_bitmap(const struct redraw_context *ctx,
452 struct bitmap *bitmap,
453 int x, int y,
454 int width,
455 int height,
456 colour bg,
457 bitmap_flags_t flags)
458{
459 pencil_code code;
460 const uint8_t *buffer;
461
463 if (!buffer) {
464 ro_warn_user("NoMemory", 0);
465 return NSERROR_INVALID;
466 }
467
468 code = pencil_sprite(ro_save_draw_diagram,
469 x * 2, (-y - height) * 2,
470 width * 2, height * 2,
471 ((char *) bitmap->sprite_area) +
472 bitmap->sprite_area->first);
473 if (code != pencil_OK)
474 return ro_save_draw_error(code);
475
476 return NSERROR_OK;
477}
478
479
480/**
481 * Text plotting.
482 *
483 * \param ctx The current redraw context.
484 * \param fstyle plot style for this text
485 * \param x x coordinate
486 * \param y y coordinate
487 * \param text UTF-8 string to plot
488 * \param length length of string, in bytes
489 * \return NSERROR_OK on success else error code.
490 */
491static nserror
492ro_save_draw_text(const struct redraw_context *ctx,
493 const struct plot_font_style *fstyle,
494 int x,
495 int y,
496 const char *text,
497 size_t length)
498{
499 pencil_code code;
500 const char *font_family;
501 unsigned int font_size;
502 rufl_style font_style;
503
504 nsfont_read_style(fstyle, &font_family, &font_size, &font_style);
505
506 code = pencil_text(ro_save_draw_diagram, x * 2, -y * 2, font_family,
507 font_style, font_size, text, length,
508 fstyle->foreground << 8);
509 if (code != pencil_OK)
510 return ro_save_draw_error(code);
511
512 return NSERROR_OK;
513}
514
515
516/**
517 * Start of a group of objects.
518 *
519 * \param ctx The current redraw context.
520 * \return NSERROR_OK on success else error code.
521 */
522static nserror
523ro_save_draw_group_start(const struct redraw_context *ctx, const char *name)
524{
525 pencil_code code;
526
527 code = pencil_group_start(ro_save_draw_diagram, name);
528 if (code != pencil_OK)
529 return ro_save_draw_error(code);
530
531 return NSERROR_OK;
532}
533
534
535/**
536 * End of the most recently started group.
537 *
538 * \param ctx The current redraw context.
539 * \return NSERROR_OK on success else error code.
540 */
541static nserror
542ro_save_draw_group_end(const struct redraw_context *ctx)
543{
544 pencil_code code;
545
546 code = pencil_group_end(ro_save_draw_diagram);
547 if (code != pencil_OK)
548 return ro_save_draw_error(code);
549
550 return NSERROR_OK;
551}
552
553
554static const struct plotter_table ro_save_draw_plotters = {
555 .rectangle = ro_save_draw_rectangle,
556 .line = ro_save_draw_line,
557 .polygon = ro_save_draw_polygon,
558 .clip = ro_save_draw_clip,
559 .text = ro_save_draw_text,
560 .disc = ro_save_draw_disc,
561 .arc = ro_save_draw_arc,
562 .bitmap = ro_save_draw_bitmap,
563 .group_start = ro_save_draw_group_start,
564 .group_end = ro_save_draw_group_end,
565 .path = ro_save_draw_path,
566 .option_knockout = false,
567};
568
569
570/* exported interface documented in save_draw.h */
571bool save_as_draw(struct hlcache_handle *h, const char *path)
572{
573 pencil_code code;
574 char *drawfile_buffer;
575 struct rect clip;
576 struct content_redraw_data data;
577 size_t drawfile_size;
578 os_error *error;
579 struct redraw_context ctx = {
580 .interactive = false,
581 .background_images = true,
582 .plot = &ro_save_draw_plotters
583 };
584
585 ro_save_draw_diagram = pencil_create();
586 if (!ro_save_draw_diagram) {
587 ro_warn_user("NoMemory", 0);
588 return false;
589 }
590
591 ro_save_draw_width = content_get_width(h);
592 ro_save_draw_height = content_get_height(h);
593
594 clip.x0 = clip.y0 = INT_MIN;
595 clip.x1 = clip.y1 = INT_MAX;
596
597 data.x = 0;
598 data.y = -ro_save_draw_height;
599 data.width = ro_save_draw_width;
600 data.height = ro_save_draw_height;
601 data.background_colour = 0xFFFFFF;
602 data.scale = 1;
603 data.repeat_x = false;
604 data.repeat_y = false;
605
606 if (!content_redraw(h, &data, &clip, &ctx)) {
607 pencil_free(ro_save_draw_diagram);
608 return false;
609 }
610
611 /*pencil_dump(ro_save_draw_diagram);*/
612
613 code = pencil_save_drawfile(ro_save_draw_diagram, "NetSurf",
614 &drawfile_buffer, &drawfile_size);
615 if (code != pencil_OK) {
616 ro_warn_user("SaveError", 0);
617 pencil_free(ro_save_draw_diagram);
618 return false;
619 }
620 assert(drawfile_buffer);
621
622 error = xosfile_save_stamped(path, osfile_TYPE_DRAW,
623 (byte *) drawfile_buffer,
624 (byte *) drawfile_buffer + drawfile_size);
625 if (error) {
626 NSLOG(netsurf, INFO, "xosfile_save_stamped failed: 0x%x: %s",
627 error->errnum, error->errmess);
628 ro_warn_user("SaveError", error->errmess);
629 pencil_free(ro_save_draw_diagram);
630 return false;
631 }
632
633 pencil_free(ro_save_draw_diagram);
634
635 return true;
636}
637
638#endif
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_INVALID
Invalid data.
Definition: errors.h:49
@ NSERROR_OK
No error.
Definition: errors.h:30
unsigned char * riscos_bitmap_get_buffer(void *vbitmap)
Return a pointer to the pixel data in a bitmap.
Definition: bitmap.c:145
void nsfont_read_style(const plot_font_style_t *fstyle, const char **font_family, unsigned int *font_size, rufl_style *font_style)
Convert a font style to a font family, size and rufl_style.
Definition: font.c:456
RISC OS font interface.
Public content interface.
bool content_redraw(struct hlcache_handle *h, struct content_redraw_data *data, const struct rect *clip, const struct redraw_context *ctx)
Display content on screen with optional tiling.
Definition: content.c:558
int content_get_height(struct hlcache_handle *h)
Retrieve height of content.
Definition: content.c:1175
int content_get_width(struct hlcache_handle *h)
Retrieve width of content.
Definition: content.c:1158
Target independent plotting interface.
unsigned long bitmap_flags_t
Definition: plotters.h:36
@ PLOTTER_PATH_MOVE
Definition: plotters.h:42
@ PLOTTER_PATH_CLOSE
Definition: plotters.h:43
@ PLOTTER_PATH_LINE
Definition: plotters.h:44
@ PLOTTER_PATH_BEZIER
Definition: plotters.h:45
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
#define plot_style_fixed_to_int(v)
Definition: plot_style.h:54
@ PLOT_OP_TYPE_NONE
No operation.
Definition: plot_style.h:66
#define NS_TRANSPARENT
Transparent colour value.
Definition: plot_style.h:39
int width
Definition: gui.c:160
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2077
int height
Definition: gui.c:161
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
struct osspriteop_area * sprite_area
Uncompressed data, or NULL.
Definition: bitmap.h:45
parameters to content redraw
Definition: content.h:40
High-level cache handle.
Definition: hlcache.c:66
Font style for plotting.
Definition: plot_style.h:111
colour foreground
Colour of text.
Definition: plot_style.h:123
Plot style for stroke/fill plotters.
Definition: plot_style.h:76
colour fill_colour
Colour of fill.
Definition: plot_style.h:81
plot_style_fixed stroke_width
Width of stroke, in pixels.
Definition: plot_style.h:78
plot_operation_type_t fill_type
Fill plot type.
Definition: plot_style.h:80
colour stroke_colour
Colour of stroke.
Definition: plot_style.h:79
plot_operation_type_t stroke_type
Stroke plot type.
Definition: plot_style.h:77
Plotter operations table.
Definition: plotters.h:102
nserror(* rectangle)(const struct redraw_context *ctx, const plot_style_t *pstyle, const struct rect *rectangle)
Plots a rectangle.
Definition: plotters.h:188
Rectangle coordinates.
Definition: types.h:40
int x0
Definition: types.h:41
int y0
Top left.
Definition: types.h:41
int x1
Definition: types.h:42
int y1
Bottom right.
Definition: types.h:42
Redraw context.
Definition: plotters.h:51
bool interactive
Redraw to show interactive features.
Definition: plotters.h:59
uint32_t colour
Colour type: XBGR.
Definition: types.h:35
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821
static nserror line(const struct redraw_context *ctx, const plot_style_t *style, const struct rect *line)
Plots a line.
Definition: plot.c:579
static nserror text(const struct redraw_context *ctx, const struct plot_font_style *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
Definition: plot.c:978
static nserror clip(const struct redraw_context *ctx, const struct rect *clip)
Sets a clip rectangle for subsequent plot operations.
Definition: plot.c:357