1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libodfgen
3  * Version: MPL 2.0 / LGPLv2.1+
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * Major Contributor(s):
10  * Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
11  * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
12  *
13  * For minor contributions see the git repository.
14  *
15  * Alternatively, the contents of this file may be used under the terms
16  * of the GNU Lesser General Public License Version 2.1 or later
17  * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
18  * applicable instead of those above.
19  *
20  * For further information visit http://libwpd.sourceforge.net
21  */
22 
23 /* "This product is not manufactured, approved, or supported by
24  * Corel Corporation or Corel Corporation Limited."
25  */
26 
27 #include <stdio.h>
28 
29 #include <iostream>
30 #include <fstream>
31 
32 #include <librevenge/librevenge.h>
33 #include <libodfgen/libodfgen.hxx>
34 
35 #include "StringDocumentHandler.hxx"
36 
37 // must draw 3 red shapes in layer Red, 2 shapes in layer Blue and 1 black shape with not layer
38 template <class Generator>
sendGraphic(Generator & generator,void (Generator::* SetStyle)(const librevenge::RVNGPropertyList &),double xDiff=0,double yDiff=0)39 static void sendGraphic(Generator &generator, void (Generator::*SetStyle)(const librevenge::RVNGPropertyList &), double xDiff=0, double yDiff=0)
40 {
41 	// the Red layer
42 	librevenge::RVNGPropertyList style1;
43 	style1.insert("draw:stroke", "solid");
44 	style1.insert("svg:stroke-color", "#FF0000");
45 	style1.insert("svg:stroke-width", 3, librevenge::RVNG_POINT);
46 	style1.insert("draw:fill", "none");
47 	(generator.*SetStyle)(style1);
48 
49 	librevenge::RVNGPropertyList layer;
50 	layer.insert("draw:layer", "Red");
51 	generator.startLayer(layer);
52 
53 	librevenge::RVNGPropertyList shape, point;
54 	librevenge::RVNGPropertyListVector vertices;
55 	point.insert("svg:x", 20+xDiff, librevenge::RVNG_POINT);
56 	point.insert("svg:y", 40+yDiff, librevenge::RVNG_POINT);
57 	vertices.append(point);
58 	point.insert("svg:x", 200+xDiff, librevenge::RVNG_POINT);
59 	point.insert("svg:y", 40+yDiff, librevenge::RVNG_POINT);
60 	vertices.append(point);
61 	shape.insert("svg:points", vertices);
62 	generator.drawPolyline(shape);
63 
64 	librevenge::RVNGPropertyList style2;
65 	style2.clear();
66 	style2.insert("draw:stroke", "dash");
67 	style2.insert("draw:dots1", 1);
68 	style2.insert("draw:dots1-length", librevenge::RVNG_POINT);
69 	style2.insert("svg:stroke-color", "#00FF00");
70 	style2.insert("svg:stroke-width", 3, librevenge::RVNG_POINT);
71 	style2.insert("draw:fill", "solid");
72 	style2.insert("draw:fill-color", "#FF0000");
73 	(generator.*SetStyle)(style2);
74 
75 	shape.clear();
76 	shape.insert("svg:x", 70+xDiff, librevenge::RVNG_POINT);
77 	shape.insert("svg:y", 80+yDiff, librevenge::RVNG_POINT);
78 	shape.insert("svg:width", 100, librevenge::RVNG_POINT);
79 	shape.insert("svg:height", 30, librevenge::RVNG_POINT);
80 	generator.drawRectangle(shape);
81 	generator.endLayer();
82 
83 	//
84 	// end of the Red layer
85 	//
86 
87 	// shape outside any layer
88 
89 	style1.insert("svg:stroke-color", "#000000");
90 	(generator.*SetStyle)(style1);
91 	shape.clear();
92 	vertices.clear();
93 	point.insert("svg:x", 20+xDiff, librevenge::RVNG_POINT);
94 	point.insert("svg:y", 100+yDiff, librevenge::RVNG_POINT);
95 	vertices.append(point);
96 	point.insert("svg:x", 200+xDiff, librevenge::RVNG_POINT);
97 	point.insert("svg:y", 100+yDiff, librevenge::RVNG_POINT);
98 	vertices.append(point);
99 	shape.insert("svg:points", vertices);
100 	generator.drawPolyline(shape);
101 
102 	//
103 	// the Blue layer
104 	//
105 	layer.insert("draw:layer", "Blue");
106 	generator.startLayer(layer);
107 
108 	librevenge::RVNGPropertyList style(style1);
109 	style.insert("draw:marker-start-path", "m10 0-10 30h20z");
110 	style.insert("draw:marker-start-viewbox", "0 0 20 30");
111 	style.insert("draw:marker-start-center", "false");
112 	style.insert("draw:marker-start-width", "5pt");
113 	style.insert("svg:stroke-color", "#0000FF");
114 	(generator.*SetStyle)(style);
115 
116 	shape.clear();
117 	vertices.clear();
118 	point.insert("svg:x", 20+xDiff, librevenge::RVNG_POINT);
119 	point.insert("svg:y", 200+yDiff, librevenge::RVNG_POINT);
120 	vertices.append(point);
121 	point.insert("svg:x", 200+xDiff, librevenge::RVNG_POINT);
122 	point.insert("svg:y", 200+yDiff, librevenge::RVNG_POINT);
123 	vertices.append(point);
124 	shape.insert("svg:points", vertices);
125 	generator.drawPolyline(shape);
126 
127 	style=style2;
128 	style.insert("draw:fill", "gradient");
129 	style.insert("draw:style", "axial");
130 	style.insert("draw:start-color", "#FF0000");
131 	style.insert("draw:start-opacity", 1., librevenge::RVNG_PERCENT);
132 	style.insert("draw:end-color", "#700070");
133 	style.insert("draw:end-opacity", 1., librevenge::RVNG_PERCENT);
134 	(generator.*SetStyle)(style);
135 
136 	// we want this one to be in red layer, so we insert "draw:layer"="Red"
137 	shape.clear();
138 	shape.insert("svg:cx", 3+xDiff/72., librevenge::RVNG_INCH);
139 	shape.insert("svg:cy", 1.2+yDiff/72., librevenge::RVNG_INCH);
140 	shape.insert("svg:rx", 0.8, librevenge::RVNG_INCH);
141 	shape.insert("svg:ry", 0.4, librevenge::RVNG_INCH);
142 	shape.insert("draw:layer", "Red");
143 	generator.drawEllipse(shape);
144 
145 	style=style2;
146 	style.insert("draw:fill", "gradient");
147 	style.insert("draw:style", "axial");
148 	style.insert("draw:start-color", "#0000FF");
149 	style.insert("draw:start-opacity", 1., librevenge::RVNG_PERCENT);
150 	style.insert("draw:end-color", "#700070");
151 	style.insert("draw:end-opacity", 1., librevenge::RVNG_PERCENT);
152 	(generator.*SetStyle)(style);
153 
154 	// this shape is inserted on a not-existing layer: "Green", so it will appear in the current layer "Blue"
155 	shape.remove("draw:layer");
156 	style.insert("draw:stroke", "solid");
157 	style.insert("draw:shadow", "visible");
158 	style.insert("draw:shadow-color", "#000020");
159 	style.insert("draw:shadow-opacity", 1, librevenge::RVNG_PERCENT);
160 	style.insert("draw:shadow-offset-x", "30");
161 	style.insert("draw:shadow-offset-y", "30");
162 	style.insert("draw:angle", 30);
163 	shape.insert("draw:layer", "Green");
164 	(generator.*SetStyle)(style);
165 	shape.insert("svg:cy", 1.8+yDiff/72., librevenge::RVNG_INCH);
166 	shape.insert("librevenge:rotate", 30);
167 	generator.drawEllipse(shape);
168 
169 	generator.endLayer();
170 
171 	//
172 	// end of the Blue layer
173 	//
174 }
175 
createOdg()176 static void createOdg()
177 {
178 	StringDocumentHandler content;
179 	OdgGenerator generator;
180 	generator.addDocumentHandler(&content, ODF_FLAT_XML);
181 
182 	generator.startDocument(librevenge::RVNGPropertyList());
183 	librevenge::RVNGPropertyList page;
184 	page.insert("draw:name", "Basic");
185 	page.insert("svg:width", 9, librevenge::RVNG_INCH);
186 	page.insert("svg:height", 11, librevenge::RVNG_INCH);
187 	page.insert("librevenge:enforce-frame",true);
188 	generator.startPage(page);
189 
190 	sendGraphic(generator, &OdgGenerator::setStyle);
191 
192 	generator.endPage();
193 
194 	/*
195 	  now let's try what happens if we reuse the same layer names:
196 
197 	  Red will become Red#0, Blue will become Blue#0
198 	*/
199 	page.insert("draw:name", "Reusing same layer names is bad");
200 	generator.startPage(page);
201 	sendGraphic(generator, &OdgGenerator::setStyle);
202 	generator.endPage();
203 
204 	generator.endDocument();
205 
206 	std::ofstream file("testLayer1.odg");
207 	file << content.cstr();
208 }
209 
main()210 int main()
211 {
212 	try
213 	{
214 		createOdg();
215 	}
216 	catch (...)
217 	{
218 		fprintf(stderr, "ERROR: parse exception\n");
219 		return 1;
220 	}
221 	return 0;
222 }
223 
224 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
225 
226