xref: /haiku/src/libs/icon/transformer/StrokeTransformer.cpp (revision b2537f99cdffc40818b65879120ecae67b29d7d9)
1 /*
2  * Copyright 2006-2007, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Stephan Aßmus <superstippi@gmx.de>
7  */
8 
9 
10 #include "StrokeTransformer.h"
11 
12 #ifdef ICON_O_MATIC
13 # include <Message.h>
14 
15 # include "CommonPropertyIDs.h"
16 # include "OptionProperty.h"
17 # include "Property.h"
18 # include "PropertyObject.h"
19 #endif // ICON_O_MATIC
20 
21 #include <new>
22 
23 using namespace BPrivate::Icon;
24 using std::nothrow;
25 
26 
27 // constructor
28 StrokeTransformer::StrokeTransformer(VertexSource& source)
29 	: Transformer(source, "Stroke"),
30 	  Stroke(source)
31 {
32 }
33 
34 #ifdef ICON_O_MATIC
35 // constructor
36 StrokeTransformer::StrokeTransformer(VertexSource& source,
37 									 BMessage* archive)
38 	: Transformer(source, archive),
39 	  Stroke(source)
40 {
41 	if (!archive)
42 		return;
43 
44 	int32 mode;
45 	if (archive->FindInt32("line cap", &mode) == B_OK)
46 		line_cap((agg::line_cap_e)mode);
47 
48 	if (archive->FindInt32("line join", &mode) == B_OK)
49 		line_join((agg::line_join_e)mode);
50 
51 	if (archive->FindInt32("inner join", &mode) == B_OK)
52 		inner_join((agg::inner_join_e)mode);
53 
54 	double value;
55 	if (archive->FindDouble("width", &value) == B_OK)
56 		width(value);
57 
58 	if (archive->FindDouble("miter limit", &value) == B_OK)
59 		miter_limit(value);
60 
61 	if (archive->FindDouble("inner miter limit", &value) == B_OK)
62 		inner_miter_limit(value);
63 
64 	if (archive->FindDouble("shorten", &value) == B_OK)
65 		shorten(value);
66 }
67 #endif // ICON_O_MATIC
68 
69 // destructor
70 StrokeTransformer::~StrokeTransformer()
71 {
72 }
73 
74 // Clone
75 Transformer*
76 StrokeTransformer::Clone(VertexSource& source) const
77 {
78 	StrokeTransformer* clone = new (nothrow) StrokeTransformer(source);
79 	if (clone) {
80 		clone->line_cap(line_cap());
81 		clone->line_join(line_join());
82 		clone->inner_join(inner_join());
83 		clone->width(width());
84 		clone->miter_limit(miter_limit());
85 		clone->inner_miter_limit(inner_miter_limit());
86 		clone->shorten(shorten());
87 	}
88 	return clone;
89 }
90 
91 // rewind
92 void
93 StrokeTransformer::rewind(unsigned path_id)
94 {
95 	Stroke::rewind(path_id);
96 }
97 
98 // vertex
99 unsigned
100 StrokeTransformer::vertex(double* x, double* y)
101 {
102 	return Stroke::vertex(x, y);
103 }
104 
105 // SetSource
106 void
107 StrokeTransformer::SetSource(VertexSource& source)
108 {
109 	Transformer::SetSource(source);
110 	Stroke::attach(source);
111 }
112 
113 // WantsOpenPaths
114 bool
115 StrokeTransformer::WantsOpenPaths() const
116 {
117 	return true;
118 }
119 
120 // ApproximationScale
121 double
122 StrokeTransformer::ApproximationScale() const
123 {
124 	return fSource.ApproximationScale() * width();
125 }
126 
127 // #pragma mark -
128 
129 #ifdef ICON_O_MATIC
130 
131 // Archive
132 status_t
133 StrokeTransformer::Archive(BMessage* into, bool deep) const
134 {
135 	status_t ret = Transformer::Archive(into, deep);
136 
137 	if (ret == B_OK)
138 		into->what = archive_code;
139 
140 	if (ret == B_OK)
141 		ret = into->AddInt32("line cap", line_cap());
142 
143 	if (ret == B_OK)
144 		ret = into->AddInt32("line join", line_join());
145 
146 	if (ret == B_OK)
147 		ret = into->AddInt32("inner join", inner_join());
148 
149 	if (ret == B_OK)
150 		ret = into->AddDouble("width", width());
151 
152 	if (ret == B_OK)
153 		ret = into->AddDouble("miter limit", miter_limit());
154 
155 	if (ret == B_OK)
156 		ret = into->AddDouble("inner miter limit", inner_miter_limit());
157 
158 	if (ret == B_OK)
159 		ret = into->AddDouble("shorten",shorten());
160 
161 	return ret;
162 }
163 
164 // MakePropertyObject
165 PropertyObject*
166 StrokeTransformer::MakePropertyObject() const
167 {
168 	PropertyObject* object = Transformer::MakePropertyObject();
169 	if (!object)
170 		return NULL;
171 
172 	// width
173 	object->AddProperty(new FloatProperty(PROPERTY_WIDTH, width()));
174 
175 	// cap mode
176 	OptionProperty* property = new OptionProperty(PROPERTY_CAP_MODE);
177 	property->AddOption(agg::butt_cap, "Butt");
178 	property->AddOption(agg::square_cap, "Square");
179 	property->AddOption(agg::round_cap, "Round");
180 	property->SetCurrentOptionID(line_cap());
181 
182 	object->AddProperty(property);
183 
184 	// join mode
185 	property = new OptionProperty(PROPERTY_JOIN_MODE);
186 	property->AddOption(agg::miter_join, "Miter");
187 	property->AddOption(agg::round_join, "Round");
188 	property->AddOption(agg::bevel_join, "Bevel");
189 	property->SetCurrentOptionID(line_join());
190 
191 	object->AddProperty(property);
192 
193 	// miter limit
194 	if (line_join() == agg::miter_join) {
195 		object->AddProperty(new FloatProperty(PROPERTY_MITER_LIMIT,
196 											  miter_limit()));
197 	}
198 
199 //	// shorten
200 //	object->AddProperty(new FloatProperty(PROPERTY_STROKE_SHORTEN,
201 //										  shorten()));
202 
203 	return object;
204 }
205 
206 // SetToPropertyObject
207 bool
208 StrokeTransformer::SetToPropertyObject(const PropertyObject* object)
209 {
210 	AutoNotificationSuspender _(this);
211 	Transformer::SetToPropertyObject(object);
212 
213 	// width
214 	float w = object->Value(PROPERTY_WIDTH, (float)width());
215 	if (w != width()) {
216 		width(w);
217 		Notify();
218 	}
219 
220 	// cap mode
221 	OptionProperty* property = dynamic_cast<OptionProperty*>(
222 			object->FindProperty(PROPERTY_CAP_MODE));
223 	if (property && line_cap() != property->CurrentOptionID()) {
224 		line_cap((agg::line_cap_e)property->CurrentOptionID());
225 		Notify();
226 	}
227 	// join mode
228 	property = dynamic_cast<OptionProperty*>(
229 		object->FindProperty(PROPERTY_JOIN_MODE));
230 	if (property && line_join() != property->CurrentOptionID()) {
231 		line_join((agg::line_join_e)property->CurrentOptionID());
232 		Notify();
233 	}
234 
235 	// miter limit
236 	float l = object->Value(PROPERTY_MITER_LIMIT, (float)miter_limit());
237 	if (l != miter_limit()) {
238 		miter_limit(l);
239 		Notify();
240 	}
241 
242 	// shorten
243 	float s = object->Value(PROPERTY_STROKE_SHORTEN, (float)shorten());
244 	if (s != shorten()) {
245 		shorten(s);
246 		Notify();
247 	}
248 
249 	return HasPendingNotifications();
250 }
251 
252 #endif // ICON_O_MATIC
253 
254 
255