1 | /* |
1 | /* |
2 | generate_page.c - Part of psiconv, a PSION 5 file formats converter |
2 | generate_page.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 2000-2014 Frodo Looijaard <frodo@frodo.looijaard.name> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
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 |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
24 | #include <string.h> |
24 | #include <string.h> |
25 | |
25 | |
26 | #include "generate_routines.h" |
26 | #include "generate_routines.h" |
27 | #include "error.h" |
27 | #include "error.h" |
28 | |
28 | |
|
|
29 | #ifdef DMALLOC |
|
|
30 | #include <dmalloc.h> |
|
|
31 | #endif |
29 | |
32 | |
|
|
33 | |
|
|
34 | |
30 | int psiconv_write_page_header(psiconv_buffer buf, |
35 | int psiconv_write_page_header(const psiconv_config config, |
|
|
36 | psiconv_buffer buf, int lev, |
31 | const psiconv_page_header value) |
37 | const psiconv_page_header value, |
|
|
38 | psiconv_buffer *extra_buf) |
32 | { |
39 | { |
33 | int res; |
40 | int res; |
34 | psiconv_paragraph_layout basepara; |
41 | psiconv_paragraph_layout basepara; |
35 | psiconv_character_layout basechar; |
42 | psiconv_character_layout basechar; |
36 | |
43 | |
|
|
44 | psiconv_progress(config,lev,0,"Writing page header"); |
|
|
45 | |
37 | if (!value) { |
46 | if (!value) { |
38 | psiconv_warn(0,psiconv_buffer_length(buf),"Null page header"); |
47 | psiconv_error(config,lev,0,"Null page header"); |
39 | res = -PSICONV_E_GENERATE; |
48 | res = -PSICONV_E_GENERATE; |
40 | goto ERROR1; |
49 | goto ERROR1; |
41 | } |
50 | } |
42 | |
51 | |
43 | if (!(basepara=psiconv_basic_paragraph_layout())) { |
52 | if (!(basepara=psiconv_basic_paragraph_layout())) { |
|
|
53 | psiconv_error(config,lev+1,0,"Out of memory error"); |
44 | res = -PSICONV_E_NOMEM; |
54 | res = -PSICONV_E_NOMEM; |
45 | goto ERROR1; |
55 | goto ERROR1; |
46 | } |
56 | } |
47 | if (!(basechar=psiconv_basic_character_layout())) { |
57 | if (!(basechar=psiconv_basic_character_layout())) { |
|
|
58 | psiconv_error(config,lev+1,0,"Out of memory error"); |
48 | res = -PSICONV_E_NOMEM; |
59 | res = -PSICONV_E_NOMEM; |
49 | goto ERROR2; |
60 | goto ERROR2; |
50 | } |
61 | } |
51 | |
62 | |
52 | /* Unknown byte */ |
63 | /* Unknown byte */ |
53 | if ((res = psiconv_write_u8(buf,0x01))) |
64 | if ((res = psiconv_write_u8(config,buf,lev+1,0x01))) |
54 | goto ERROR3; |
65 | goto ERROR3; |
55 | if ((res = psiconv_write_bool(buf,value->on_first_page))) |
66 | if ((res = psiconv_write_bool(config,buf,lev+1,value->on_first_page))) |
56 | goto ERROR3; |
67 | goto ERROR3; |
57 | /* Three unknown bytes */ |
68 | /* Three unknown bytes */ |
58 | if ((res = psiconv_write_u8(buf,0x00))) |
69 | if ((res = psiconv_write_u8(config,buf,lev+1,0x00))) |
59 | goto ERROR3; |
70 | goto ERROR3; |
60 | if ((res = psiconv_write_u8(buf,0x00))) |
71 | if ((res = psiconv_write_u8(config,buf,lev+1,0x00))) |
61 | goto ERROR3; |
72 | goto ERROR3; |
62 | if ((res = psiconv_write_u8(buf,0x00))) |
73 | if ((res = psiconv_write_u8(config,buf,lev+1,0x00))) |
63 | goto ERROR3; |
74 | goto ERROR3; |
64 | if ((res = psiconv_write_paragraph_layout_list(buf, |
75 | if ((res = psiconv_write_paragraph_layout_list(config,buf,lev+1, |
65 | value->base_paragraph_layout,basepara))) |
76 | value->base_paragraph_layout,basepara))) |
66 | goto ERROR3; |
77 | goto ERROR3; |
67 | if ((res = psiconv_write_character_layout_list(buf, |
78 | if ((res = psiconv_write_character_layout_list(config,buf,lev+1, |
68 | value->base_character_layout,basechar))) |
79 | value->base_character_layout,basechar))) |
69 | goto ERROR3; |
80 | goto ERROR3; |
70 | res = psiconv_write_texted_section(buf,value->text, |
81 | res = psiconv_write_texted_section(config,buf,lev+1,value->text, |
71 | value->base_character_layout, |
82 | value->base_character_layout, |
72 | value->base_paragraph_layout); |
83 | value->base_paragraph_layout,extra_buf); |
73 | ERROR3: |
84 | ERROR3: |
74 | psiconv_free_character_layout(basechar); |
85 | psiconv_free_character_layout(basechar); |
75 | ERROR2: |
86 | ERROR2: |
76 | psiconv_free_paragraph_layout(basepara); |
87 | psiconv_free_paragraph_layout(basepara); |
77 | ERROR1: |
88 | ERROR1: |
|
|
89 | if (res) |
|
|
90 | psiconv_error(config,lev,0,"Writing of page header failed"); |
|
|
91 | else |
|
|
92 | psiconv_progress(config,lev,0,"End of page header"); |
78 | return res; |
93 | return res; |
79 | } |
94 | } |
80 | |
95 | |
81 | int psiconv_write_page_layout_section(psiconv_buffer buf, |
96 | int psiconv_write_page_layout_section(const psiconv_config config, |
|
|
97 | psiconv_buffer buf, int lev, |
82 | const psiconv_page_layout_section value) |
98 | const psiconv_page_layout_section value) |
83 | { |
99 | { |
84 | int res; |
100 | int res; |
|
|
101 | psiconv_buffer header_buf,footer_buf; |
|
|
102 | |
|
|
103 | psiconv_progress(config,lev,0,"Writing page layout section"); |
85 | |
104 | |
86 | if (!value) { |
105 | if (!value) { |
87 | psiconv_warn(0,psiconv_buffer_length(buf),"Null page section"); |
106 | psiconv_error(config,lev,0,"Null page section"); |
88 | return -PSICONV_E_GENERATE; |
107 | res = -PSICONV_E_GENERATE; |
|
|
108 | goto ERROR1; |
89 | } |
109 | } |
90 | |
110 | |
91 | if ((res = psiconv_write_u32(buf,value->first_page_nr))) |
111 | if ((res = psiconv_write_u32(config,buf,lev+1,value->first_page_nr))) |
|
|
112 | goto ERROR1; |
|
|
113 | if ((res = psiconv_write_length(config,buf,lev+1,value->header_dist))) |
|
|
114 | goto ERROR1; |
|
|
115 | if ((res = psiconv_write_length(config,buf,lev+1,value->footer_dist))) |
|
|
116 | goto ERROR1; |
|
|
117 | if ((res = psiconv_write_length(config,buf,lev+1,value->left_margin))) |
|
|
118 | goto ERROR1; |
|
|
119 | if ((res = psiconv_write_length(config,buf,lev+1,value->right_margin))) |
|
|
120 | goto ERROR1; |
|
|
121 | if ((res = psiconv_write_length(config,buf,lev+1,value->top_margin))) |
|
|
122 | goto ERROR1; |
|
|
123 | if ((res = psiconv_write_length(config,buf,lev+1,value->bottom_margin))) |
|
|
124 | goto ERROR1; |
|
|
125 | if ((res = psiconv_write_page_header(config,buf,lev+1,value->header,&header_buf))) |
|
|
126 | goto ERROR1; |
|
|
127 | if ((res = psiconv_write_page_header(config,buf,lev+1,value->footer,&footer_buf))) |
|
|
128 | goto ERROR2; |
|
|
129 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_PAGE_DIMENSIONS2))) |
|
|
130 | goto ERROR3; |
|
|
131 | if ((res = psiconv_write_length(config,buf,lev+1,value->page_width))) |
|
|
132 | goto ERROR3; |
|
|
133 | if ((res = psiconv_write_length(config,buf,lev+1,value->page_height))) |
|
|
134 | goto ERROR3; |
|
|
135 | if ((res = psiconv_write_bool(config,buf,lev+1,value->landscape))) |
|
|
136 | goto ERROR3; |
|
|
137 | if ((res = psiconv_buffer_concat(buf,header_buf))) { |
|
|
138 | psiconv_error(config,lev+1,0,"Out of memory error"); |
|
|
139 | goto ERROR3; |
|
|
140 | } |
|
|
141 | if ((res = psiconv_buffer_concat(buf,footer_buf))) { |
|
|
142 | psiconv_error(config,lev+1,0,"Out of memory error"); |
|
|
143 | goto ERROR3; |
|
|
144 | } |
|
|
145 | |
|
|
146 | |
|
|
147 | ERROR3: |
|
|
148 | psiconv_buffer_free(footer_buf); |
|
|
149 | ERROR2: |
|
|
150 | psiconv_buffer_free(header_buf); |
|
|
151 | ERROR1: |
|
|
152 | if (res) |
|
|
153 | psiconv_error(config,lev,0,"Writing of page layout section failed"); |
|
|
154 | else |
|
|
155 | psiconv_progress(config,lev,0,"End of page layout section"); |
92 | return res; |
156 | return res; |
93 | if ((res = psiconv_write_length(buf,value->header_dist))) |
|
|
94 | return res; |
|
|
95 | if ((res = psiconv_write_length(buf,value->footer_dist))) |
|
|
96 | return res; |
|
|
97 | if ((res = psiconv_write_length(buf,value->left_margin))) |
|
|
98 | return res; |
|
|
99 | if ((res = psiconv_write_length(buf,value->right_margin))) |
|
|
100 | return res; |
|
|
101 | if ((res = psiconv_write_length(buf,value->top_margin))) |
|
|
102 | return res; |
|
|
103 | if ((res = psiconv_write_length(buf,value->bottom_margin))) |
|
|
104 | return res; |
|
|
105 | if ((res = psiconv_write_page_header(buf,value->header))) |
|
|
106 | return res; |
|
|
107 | if ((res = psiconv_write_page_header(buf,value->footer))) |
|
|
108 | return res; |
|
|
109 | if ((res = psiconv_write_u32(buf,PSICONV_ID_PAGE_DIMENSIONS2))) |
|
|
110 | return res; |
|
|
111 | if ((res = psiconv_write_length(buf,value->page_width))) |
|
|
112 | return res; |
|
|
113 | if ((res = psiconv_write_length(buf,value->page_height))) |
|
|
114 | return res; |
|
|
115 | return psiconv_write_bool(buf,value->landscape); |
|
|
116 | } |
157 | } |