xref: /haiku/src/kits/interface/PictureDataWriter.cpp (revision 4399ec510de9714e2440cdae346d00c286d69906)
17022e790SStefano Ceccherini /*
2954a0a0cSStephan Aßmus  * Copyright 2006-2018 Haiku, Inc. All rights reserved.
37022e790SStefano Ceccherini  * Distributed under the terms of the MIT License.
47022e790SStefano Ceccherini  *
57022e790SStefano Ceccherini  * Authors:
62c3505deSJohn Scipione  *		Stefano Ceccherini, stefano.ceccherini@gmail.com
7551438b9SJulian Harnath  *		Julian Harnath, <julian.harnath@rwth-achen.de>
8954a0a0cSStephan Aßmus  *		Stephan Aßmus <superstippi@gmx.de>
97022e790SStefano Ceccherini  */
107022e790SStefano Ceccherini 
116f867cc1SStephan Aßmus #include <PictureDataWriter.h>
126f867cc1SStephan Aßmus 
136f867cc1SStephan Aßmus #include <stdio.h>
1418cd67c7SMichael Lotz #include <string.h>
156f867cc1SStephan Aßmus 
16420fe80eSStefano Ceccherini #include <DataIO.h>
1790ab1a44SX512 #include <Gradient.h>
18420fe80eSStefano Ceccherini #include <Point.h>
19420fe80eSStefano Ceccherini #include <Rect.h>
20253d42a6SStefano Ceccherini #include <Region.h>
21420fe80eSStefano Ceccherini 
22420fe80eSStefano Ceccherini #include <PictureProtocol.h>
23420fe80eSStefano Ceccherini 
2484c2abeeSStefano Ceccherini #define THROW_ERROR(error) throw (status_t)(error)
2584c2abeeSStefano Ceccherini 
262c3505deSJohn Scipione 
27feb557a6SStefano Ceccherini // TODO: Review writing of strings. AFAIK in the picture data format
288752f52dSStefano Ceccherini // They are not supposed to be NULL terminated
298752f52dSStefano Ceccherini // (at least, it's not mandatory) so we should write their size too.
PictureDataWriter()304bfa8f22SStefano Ceccherini PictureDataWriter::PictureDataWriter()
314bfa8f22SStefano Ceccherini 	:
324bfa8f22SStefano Ceccherini 	fData(NULL)
334bfa8f22SStefano Ceccherini {
344bfa8f22SStefano Ceccherini }
354bfa8f22SStefano Ceccherini 
364bfa8f22SStefano Ceccherini 
PictureDataWriter(BPositionIO * data)37420fe80eSStefano Ceccherini PictureDataWriter::PictureDataWriter(BPositionIO* data)
38420fe80eSStefano Ceccherini 	:
39420fe80eSStefano Ceccherini 	fData(data)
40420fe80eSStefano Ceccherini {
41420fe80eSStefano Ceccherini }
42420fe80eSStefano Ceccherini 
43420fe80eSStefano Ceccherini 
~PictureDataWriter()448752f52dSStefano Ceccherini PictureDataWriter::~PictureDataWriter()
458752f52dSStefano Ceccherini {
468752f52dSStefano Ceccherini }
478752f52dSStefano Ceccherini 
488752f52dSStefano Ceccherini 
49420fe80eSStefano Ceccherini status_t
SetTo(BPositionIO * data)504bfa8f22SStefano Ceccherini PictureDataWriter::SetTo(BPositionIO* data)
514bfa8f22SStefano Ceccherini {
52140334f8SStefano Ceccherini 	if (data == NULL)
53140334f8SStefano Ceccherini 		return B_BAD_VALUE;
542c3505deSJohn Scipione 
554bfa8f22SStefano Ceccherini 	fData = data;
562c3505deSJohn Scipione 
574bfa8f22SStefano Ceccherini 	return B_OK;
584bfa8f22SStefano Ceccherini }
594bfa8f22SStefano Ceccherini 
604bfa8f22SStefano Ceccherini 
614bfa8f22SStefano Ceccherini status_t
WriteSetOrigin(const BPoint & point)62420fe80eSStefano Ceccherini PictureDataWriter::WriteSetOrigin(const BPoint& point)
63420fe80eSStefano Ceccherini {
64140334f8SStefano Ceccherini 	try {
65420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_ORIGIN);
66420fe80eSStefano Ceccherini 		Write<BPoint>(point);
67420fe80eSStefano Ceccherini 		EndOp();
68140334f8SStefano Ceccherini 	} catch (status_t& status) {
69140334f8SStefano Ceccherini 		return status;
70140334f8SStefano Ceccherini 	}
712c3505deSJohn Scipione 
72420fe80eSStefano Ceccherini 	return B_OK;
73420fe80eSStefano Ceccherini }
74420fe80eSStefano Ceccherini 
75420fe80eSStefano Ceccherini 
76420fe80eSStefano Ceccherini status_t
WriteInvertRect(const BRect & rect)77420fe80eSStefano Ceccherini PictureDataWriter::WriteInvertRect(const BRect& rect)
78420fe80eSStefano Ceccherini {
79140334f8SStefano Ceccherini 	try {
8057067377SJackBurton79 		WritePushState();
81420fe80eSStefano Ceccherini 		WriteSetDrawingMode(B_OP_INVERT);
82420fe80eSStefano Ceccherini 
83420fe80eSStefano Ceccherini 		BeginOp(B_PIC_FILL_RECT);
84420fe80eSStefano Ceccherini 		Write<BRect>(rect);
85420fe80eSStefano Ceccherini 		EndOp();
86420fe80eSStefano Ceccherini 
8757067377SJackBurton79 		WritePopState();
88140334f8SStefano Ceccherini 	} catch (status_t& status) {
89140334f8SStefano Ceccherini 		return status;
90140334f8SStefano Ceccherini 	}
912c3505deSJohn Scipione 
92420fe80eSStefano Ceccherini 	return B_OK;
93420fe80eSStefano Ceccherini }
94420fe80eSStefano Ceccherini 
95420fe80eSStefano Ceccherini 
96420fe80eSStefano Ceccherini status_t
WriteSetDrawingMode(const drawing_mode & mode)97420fe80eSStefano Ceccherini PictureDataWriter::WriteSetDrawingMode(const drawing_mode& mode)
98420fe80eSStefano Ceccherini {
99140334f8SStefano Ceccherini 	try {
100420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_DRAWING_MODE);
101420fe80eSStefano Ceccherini 		Write<int16>((int16)mode);
102420fe80eSStefano Ceccherini 		EndOp();
103140334f8SStefano Ceccherini 	} catch (status_t& status) {
104140334f8SStefano Ceccherini 		return status;
105140334f8SStefano Ceccherini 	}
1062c3505deSJohn Scipione 
107420fe80eSStefano Ceccherini 	return B_OK;
108420fe80eSStefano Ceccherini }
109420fe80eSStefano Ceccherini 
110420fe80eSStefano Ceccherini 
111420fe80eSStefano Ceccherini status_t
WriteSetPenLocation(const BPoint & point)112690c7474SStefano Ceccherini PictureDataWriter::WriteSetPenLocation(const BPoint& point)
113690c7474SStefano Ceccherini {
114690c7474SStefano Ceccherini 	try {
115690c7474SStefano Ceccherini 		BeginOp(B_PIC_SET_PEN_LOCATION);
116690c7474SStefano Ceccherini 		Write<BPoint>(point);
117690c7474SStefano Ceccherini 		EndOp();
118690c7474SStefano Ceccherini 	} catch (status_t& status) {
119690c7474SStefano Ceccherini 		return status;
120690c7474SStefano Ceccherini 	}
1212c3505deSJohn Scipione 
122690c7474SStefano Ceccherini 	return B_OK;
123690c7474SStefano Ceccherini }
124690c7474SStefano Ceccherini 
125690c7474SStefano Ceccherini 
126690c7474SStefano Ceccherini status_t
WriteSetPenSize(const float & penSize)127420fe80eSStefano Ceccherini PictureDataWriter::WriteSetPenSize(const float& penSize)
128420fe80eSStefano Ceccherini {
129140334f8SStefano Ceccherini 	try {
130420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_PEN_SIZE);
131420fe80eSStefano Ceccherini 		Write<float>(penSize);
132420fe80eSStefano Ceccherini 		EndOp();
133140334f8SStefano Ceccherini 	} catch (status_t& status) {
134140334f8SStefano Ceccherini 		return status;
135140334f8SStefano Ceccherini 	}
1362c3505deSJohn Scipione 
137420fe80eSStefano Ceccherini 	return B_OK;
138420fe80eSStefano Ceccherini }
139420fe80eSStefano Ceccherini 
140420fe80eSStefano Ceccherini 
141420fe80eSStefano Ceccherini status_t
WriteSetLineMode(const cap_mode & cap,const join_mode & join,const float & miterLimit)1422c3505deSJohn Scipione PictureDataWriter::WriteSetLineMode(const cap_mode& cap, const join_mode& join,
1432c3505deSJohn Scipione 	const float& miterLimit)
144420fe80eSStefano Ceccherini {
145140334f8SStefano Ceccherini 	try {
146420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_LINE_MODE);
147420fe80eSStefano Ceccherini 		Write<int16>((int16)cap);
148420fe80eSStefano Ceccherini 		Write<int16>((int16)join);
149420fe80eSStefano Ceccherini 		Write<float>(miterLimit);
150420fe80eSStefano Ceccherini 		EndOp();
151140334f8SStefano Ceccherini 	} catch (status_t& status) {
152140334f8SStefano Ceccherini 		return status;
153140334f8SStefano Ceccherini 	}
1542c3505deSJohn Scipione 
155420fe80eSStefano Ceccherini 	return B_OK;
156420fe80eSStefano Ceccherini }
157420fe80eSStefano Ceccherini 
158420fe80eSStefano Ceccherini 
159420fe80eSStefano Ceccherini status_t
WriteSetFillRule(int32 fillRule)160*4399ec51SX512 PictureDataWriter::WriteSetFillRule(int32 fillRule)
161*4399ec51SX512 {
162*4399ec51SX512 	try {
163*4399ec51SX512 		BeginOp(B_PIC_SET_FILL_RULE);
164*4399ec51SX512 		Write<int32>(fillRule);
165*4399ec51SX512 		EndOp();
166*4399ec51SX512 	} catch (status_t& status) {
167*4399ec51SX512 		return status;
168*4399ec51SX512 	}
169*4399ec51SX512 
170*4399ec51SX512 	return B_OK;
171*4399ec51SX512 }
172*4399ec51SX512 
173*4399ec51SX512 
174*4399ec51SX512 status_t
WriteSetScale(const float & scale)175420fe80eSStefano Ceccherini PictureDataWriter::WriteSetScale(const float& scale)
176420fe80eSStefano Ceccherini {
177140334f8SStefano Ceccherini 	try {
178420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_SCALE);
179420fe80eSStefano Ceccherini 		Write<float>(scale);
180420fe80eSStefano Ceccherini 		EndOp();
181140334f8SStefano Ceccherini 	} catch (status_t& status) {
182140334f8SStefano Ceccherini 		return status;
183140334f8SStefano Ceccherini 	}
1842c3505deSJohn Scipione 
185420fe80eSStefano Ceccherini 	return B_OK;
186420fe80eSStefano Ceccherini }
187420fe80eSStefano Ceccherini 
188420fe80eSStefano Ceccherini 
189420fe80eSStefano Ceccherini status_t
WriteSetTransform(BAffineTransform transform)190a6db6bd4SStephan Aßmus PictureDataWriter::WriteSetTransform(BAffineTransform transform)
191a6db6bd4SStephan Aßmus {
192a6db6bd4SStephan Aßmus 	try {
193a6db6bd4SStephan Aßmus 		BeginOp(B_PIC_SET_TRANSFORM);
194a6db6bd4SStephan Aßmus 		Write<BAffineTransform>(transform);
195a6db6bd4SStephan Aßmus 		EndOp();
196a6db6bd4SStephan Aßmus 	} catch (status_t& status) {
197a6db6bd4SStephan Aßmus 		return status;
198a6db6bd4SStephan Aßmus 	}
1992c3505deSJohn Scipione 
200a6db6bd4SStephan Aßmus 	return B_OK;
201a6db6bd4SStephan Aßmus }
202a6db6bd4SStephan Aßmus 
203a6db6bd4SStephan Aßmus 
204a6db6bd4SStephan Aßmus status_t
WriteTranslateBy(double x,double y)20525dbf74bSJulian Harnath PictureDataWriter::WriteTranslateBy(double x, double y)
20625dbf74bSJulian Harnath {
20725dbf74bSJulian Harnath 	try {
20825dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_TRANSLATE);
20925dbf74bSJulian Harnath 		Write<double>(x);
21025dbf74bSJulian Harnath 		Write<double>(y);
21125dbf74bSJulian Harnath 		EndOp();
21225dbf74bSJulian Harnath 	} catch (status_t& status) {
21325dbf74bSJulian Harnath 		return status;
21425dbf74bSJulian Harnath 	}
21525dbf74bSJulian Harnath 
21625dbf74bSJulian Harnath 	return B_OK;
21725dbf74bSJulian Harnath }
21825dbf74bSJulian Harnath 
21925dbf74bSJulian Harnath 
22025dbf74bSJulian Harnath status_t
WriteScaleBy(double x,double y)22125dbf74bSJulian Harnath PictureDataWriter::WriteScaleBy(double x, double y)
22225dbf74bSJulian Harnath {
22325dbf74bSJulian Harnath 	try {
22425dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_SCALE);
22525dbf74bSJulian Harnath 		Write<double>(x);
22625dbf74bSJulian Harnath 		Write<double>(y);
22725dbf74bSJulian Harnath 		EndOp();
22825dbf74bSJulian Harnath 	} catch (status_t& status) {
22925dbf74bSJulian Harnath 		return status;
23025dbf74bSJulian Harnath 	}
23125dbf74bSJulian Harnath 
23225dbf74bSJulian Harnath 	return B_OK;
23325dbf74bSJulian Harnath }
23425dbf74bSJulian Harnath 
23525dbf74bSJulian Harnath 
23625dbf74bSJulian Harnath status_t
WriteRotateBy(double angleRadians)23725dbf74bSJulian Harnath PictureDataWriter::WriteRotateBy(double angleRadians)
23825dbf74bSJulian Harnath {
23925dbf74bSJulian Harnath 	try {
24025dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_ROTATE);
24125dbf74bSJulian Harnath 		Write<double>(angleRadians);
24225dbf74bSJulian Harnath 		EndOp();
24325dbf74bSJulian Harnath 	} catch (status_t& status) {
24425dbf74bSJulian Harnath 		return status;
24525dbf74bSJulian Harnath 	}
24625dbf74bSJulian Harnath 
24725dbf74bSJulian Harnath 	return B_OK;
24825dbf74bSJulian Harnath }
24925dbf74bSJulian Harnath 
25025dbf74bSJulian Harnath 
25125dbf74bSJulian Harnath status_t
WriteSetPattern(const::pattern & pattern)2522c3505deSJohn Scipione PictureDataWriter::WriteSetPattern(const ::pattern& pattern)
25337033892SStefano Ceccherini {
25437033892SStefano Ceccherini 	try {
25537033892SStefano Ceccherini 		BeginOp(B_PIC_SET_STIPLE_PATTERN);
2562c3505deSJohn Scipione 		Write< ::pattern>(pattern);
25737033892SStefano Ceccherini 		EndOp();
25837033892SStefano Ceccherini 	} catch (status_t& status) {
25937033892SStefano Ceccherini 		return status;
26037033892SStefano Ceccherini 	}
2612c3505deSJohn Scipione 
26237033892SStefano Ceccherini 	return B_OK;
26337033892SStefano Ceccherini }
26437033892SStefano Ceccherini 
26537033892SStefano Ceccherini 
26637033892SStefano Ceccherini status_t
WriteClipToPicture(int32 pictureToken,const BPoint & origin,bool inverse)267ce27f961SStefano Ceccherini PictureDataWriter::WriteClipToPicture(int32 pictureToken,
268ce27f961SStefano Ceccherini 						const BPoint& origin, bool inverse)
269ce27f961SStefano Ceccherini {
270ce27f961SStefano Ceccherini 	// TODO: I don't know if it's compatible with R5's BPicture version
271ce27f961SStefano Ceccherini 	try {
272ce27f961SStefano Ceccherini 		BeginOp(B_PIC_CLIP_TO_PICTURE);
273ce27f961SStefano Ceccherini 		Write<int32>(pictureToken);
274ce27f961SStefano Ceccherini 		Write<BPoint>(origin);
275ce27f961SStefano Ceccherini 		Write<bool>(inverse);
276ce27f961SStefano Ceccherini 		EndOp();
277ce27f961SStefano Ceccherini 	} catch (status_t& status) {
278ce27f961SStefano Ceccherini 		return status;
279ce27f961SStefano Ceccherini 	}
280ce27f961SStefano Ceccherini 
281ce27f961SStefano Ceccherini 	return B_OK;
282ce27f961SStefano Ceccherini }
283ce27f961SStefano Ceccherini 
284ce27f961SStefano Ceccherini 
285ce27f961SStefano Ceccherini status_t
WriteSetClipping(const BRegion & region)2868752f52dSStefano Ceccherini PictureDataWriter::WriteSetClipping(const BRegion& region)
287253d42a6SStefano Ceccherini {
288253d42a6SStefano Ceccherini 	// TODO: I don't know if it's compatible with R5's BPicture version
289253d42a6SStefano Ceccherini 	try {
290253d42a6SStefano Ceccherini 		const int32 numRects = region.CountRects();
291253d42a6SStefano Ceccherini 		if (numRects > 0 && region.Frame().IsValid()) {
292253d42a6SStefano Ceccherini 			BeginOp(B_PIC_SET_CLIPPING_RECTS);
29346b4e489SStefano Ceccherini 			Write<uint32>(numRects);
2942c3505deSJohn Scipione 			for (int32 i = 0; i < numRects; i++)
295253d42a6SStefano Ceccherini 				Write<BRect>(region.RectAt(i));
2962c3505deSJohn Scipione 
297253d42a6SStefano Ceccherini 			EndOp();
298253d42a6SStefano Ceccherini 		} else
299253d42a6SStefano Ceccherini 			WriteClearClipping();
300253d42a6SStefano Ceccherini 	} catch (status_t& status) {
301253d42a6SStefano Ceccherini 		return status;
302253d42a6SStefano Ceccherini 	}
30346b4e489SStefano Ceccherini 
304253d42a6SStefano Ceccherini 	return B_OK;
305253d42a6SStefano Ceccherini }
306253d42a6SStefano Ceccherini 
307253d42a6SStefano Ceccherini 
308253d42a6SStefano Ceccherini status_t
WriteClearClipping()309253d42a6SStefano Ceccherini PictureDataWriter::WriteClearClipping()
310253d42a6SStefano Ceccherini {
311253d42a6SStefano Ceccherini 	try {
312253d42a6SStefano Ceccherini 		BeginOp(B_PIC_CLEAR_CLIPPING_RECTS);
313253d42a6SStefano Ceccherini 		EndOp();
314253d42a6SStefano Ceccherini 	} catch (status_t& status) {
315253d42a6SStefano Ceccherini 		return status;
316253d42a6SStefano Ceccherini 	}
3172c3505deSJohn Scipione 
318253d42a6SStefano Ceccherini 	return B_OK;
319253d42a6SStefano Ceccherini }
320253d42a6SStefano Ceccherini 
321253d42a6SStefano Ceccherini 
322253d42a6SStefano Ceccherini status_t
WriteSetHighColor(const rgb_color & color)323420fe80eSStefano Ceccherini PictureDataWriter::WriteSetHighColor(const rgb_color& color)
324420fe80eSStefano Ceccherini {
325140334f8SStefano Ceccherini 	try {
326420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_FORE_COLOR);
327420fe80eSStefano Ceccherini 		Write<rgb_color>(color);
328420fe80eSStefano Ceccherini 		EndOp();
329140334f8SStefano Ceccherini 	} catch (status_t& status) {
330140334f8SStefano Ceccherini 		return status;
331140334f8SStefano Ceccherini 	}
3322c3505deSJohn Scipione 
333420fe80eSStefano Ceccherini 	return B_OK;
334420fe80eSStefano Ceccherini }
335420fe80eSStefano Ceccherini 
336420fe80eSStefano Ceccherini 
337420fe80eSStefano Ceccherini status_t
WriteSetLowColor(const rgb_color & color)338420fe80eSStefano Ceccherini PictureDataWriter::WriteSetLowColor(const rgb_color& color)
339420fe80eSStefano Ceccherini {
340140334f8SStefano Ceccherini 	try {
341420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_BACK_COLOR);
342420fe80eSStefano Ceccherini 		Write<rgb_color>(color);
343420fe80eSStefano Ceccherini 		EndOp();
344140334f8SStefano Ceccherini 	} catch (status_t& status) {
345140334f8SStefano Ceccherini 		return status;
346140334f8SStefano Ceccherini 	}
3472c3505deSJohn Scipione 
348420fe80eSStefano Ceccherini 	return B_OK;
349420fe80eSStefano Ceccherini }
350420fe80eSStefano Ceccherini 
351420fe80eSStefano Ceccherini 
352420fe80eSStefano Ceccherini status_t
WriteDrawRect(const BRect & rect,const bool & fill)3532c3505deSJohn Scipione PictureDataWriter::WriteDrawRect(const BRect& rect, const bool& fill)
354420fe80eSStefano Ceccherini {
355140334f8SStefano Ceccherini 	try {
356420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT);
357420fe80eSStefano Ceccherini 		Write<BRect>(rect);
358420fe80eSStefano Ceccherini 		EndOp();
359140334f8SStefano Ceccherini 	} catch (status_t& status) {
360140334f8SStefano Ceccherini 		return status;
361140334f8SStefano Ceccherini 	}
3622c3505deSJohn Scipione 
363420fe80eSStefano Ceccherini 	return B_OK;
364420fe80eSStefano Ceccherini }
365420fe80eSStefano Ceccherini 
366420fe80eSStefano Ceccherini 
367420fe80eSStefano Ceccherini status_t
WriteDrawRoundRect(const BRect & rect,const BPoint & radius,const bool & fill)3682c3505deSJohn Scipione PictureDataWriter::WriteDrawRoundRect(const BRect& rect, const BPoint& radius,
3692c3505deSJohn Scipione 	const bool& fill)
370420fe80eSStefano Ceccherini {
371140334f8SStefano Ceccherini 	try {
372420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT);
373420fe80eSStefano Ceccherini 		Write<BRect>(rect);
374420fe80eSStefano Ceccherini 		Write<BPoint>(radius);
375420fe80eSStefano Ceccherini 		EndOp();
376140334f8SStefano Ceccherini 	} catch (status_t& status) {
377140334f8SStefano Ceccherini 		return status;
378140334f8SStefano Ceccherini 	}
3792c3505deSJohn Scipione 
380420fe80eSStefano Ceccherini 	return B_OK;
381420fe80eSStefano Ceccherini }
382420fe80eSStefano Ceccherini 
383420fe80eSStefano Ceccherini 
384420fe80eSStefano Ceccherini status_t
WriteDrawEllipse(const BRect & rect,const bool & fill)385420fe80eSStefano Ceccherini PictureDataWriter::WriteDrawEllipse(const BRect& rect, const bool& fill)
386420fe80eSStefano Ceccherini {
387140334f8SStefano Ceccherini 	try {
388420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE);
389420fe80eSStefano Ceccherini 		Write<BRect>(rect);
390420fe80eSStefano Ceccherini 		EndOp();
391140334f8SStefano Ceccherini 	} catch (status_t& status) {
392140334f8SStefano Ceccherini 		return status;
393140334f8SStefano Ceccherini 	}
3942c3505deSJohn Scipione 
395420fe80eSStefano Ceccherini 	return B_OK;
396420fe80eSStefano Ceccherini }
397420fe80eSStefano Ceccherini 
398420fe80eSStefano Ceccherini 
399420fe80eSStefano Ceccherini status_t
WriteDrawArc(const BPoint & center,const BPoint & radius,const float & startTheta,const float & arcTheta,const bool & fill)4002c3505deSJohn Scipione PictureDataWriter::WriteDrawArc(const BPoint& center, const BPoint& radius,
4012c3505deSJohn Scipione 	const float& startTheta, const float& arcTheta, const bool& fill)
402420fe80eSStefano Ceccherini {
403140334f8SStefano Ceccherini 	try {
404420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC);
405420fe80eSStefano Ceccherini 		Write<BPoint>(center);
406420fe80eSStefano Ceccherini 		Write<BPoint>(radius);
407420fe80eSStefano Ceccherini 		Write<float>(startTheta);
408420fe80eSStefano Ceccherini 		Write<float>(arcTheta);
409420fe80eSStefano Ceccherini 		EndOp();
410140334f8SStefano Ceccherini 	} catch (status_t& status) {
411140334f8SStefano Ceccherini 		return status;
412140334f8SStefano Ceccherini 	}
4132c3505deSJohn Scipione 
414420fe80eSStefano Ceccherini 	return B_OK;
415420fe80eSStefano Ceccherini }
416420fe80eSStefano Ceccherini 
417420fe80eSStefano Ceccherini 
418420fe80eSStefano Ceccherini status_t
WriteDrawPolygon(const int32 & numPoints,BPoint * points,const bool & isClosed,const bool & fill)4192c3505deSJohn Scipione PictureDataWriter::WriteDrawPolygon(const int32& numPoints, BPoint* points,
4202c3505deSJohn Scipione 	const bool& isClosed, const bool& fill)
421ed5de868SStefano Ceccherini {
422ed5de868SStefano Ceccherini 	try {
423ed5de868SStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_POLYGON : B_PIC_STROKE_POLYGON);
424ed5de868SStefano Ceccherini 		Write<int32>(numPoints);
425ed5de868SStefano Ceccherini 		for (int32 i = 0; i < numPoints; i++)
426ed5de868SStefano Ceccherini 			Write<BPoint>(points[i]);
4272c3505deSJohn Scipione 
428ed5de868SStefano Ceccherini 		if (!fill)
429ed5de868SStefano Ceccherini 			Write<uint8>((uint8)isClosed);
4302c3505deSJohn Scipione 
431ed5de868SStefano Ceccherini 		EndOp();
432ed5de868SStefano Ceccherini 	} catch (status_t& status) {
433ed5de868SStefano Ceccherini 		return status;
434ed5de868SStefano Ceccherini 	}
4352c3505deSJohn Scipione 
436ed5de868SStefano Ceccherini 	return B_OK;
437ed5de868SStefano Ceccherini }
438ed5de868SStefano Ceccherini 
439ed5de868SStefano Ceccherini 
440ed5de868SStefano Ceccherini status_t
WriteDrawBezier(const BPoint points[4],const bool & fill)4412c3505deSJohn Scipione PictureDataWriter::WriteDrawBezier(const BPoint points[4], const bool& fill)
442ed5de868SStefano Ceccherini {
443ed5de868SStefano Ceccherini 	try {
444ed5de868SStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_BEZIER : B_PIC_STROKE_BEZIER);
445ed5de868SStefano Ceccherini 		for (int32 i = 0; i < 4; i++)
446ed5de868SStefano Ceccherini 			Write<BPoint>(points[i]);
4472c3505deSJohn Scipione 
448ed5de868SStefano Ceccherini 		EndOp();
449ed5de868SStefano Ceccherini 	} catch (status_t& status) {
450ed5de868SStefano Ceccherini 		return status;
451ed5de868SStefano Ceccherini 	}
4522c3505deSJohn Scipione 
453ed5de868SStefano Ceccherini 	return B_OK;
454ed5de868SStefano Ceccherini }
455ed5de868SStefano Ceccherini 
456ed5de868SStefano Ceccherini 
457ed5de868SStefano Ceccherini status_t
WriteStrokeLine(const BPoint & start,const BPoint & end)4582c3505deSJohn Scipione PictureDataWriter::WriteStrokeLine(const BPoint& start, const BPoint& end)
459420fe80eSStefano Ceccherini {
460140334f8SStefano Ceccherini 	try {
461420fe80eSStefano Ceccherini 		BeginOp(B_PIC_STROKE_LINE);
462420fe80eSStefano Ceccherini 		Write<BPoint>(start);
463420fe80eSStefano Ceccherini 		Write<BPoint>(end);
464420fe80eSStefano Ceccherini 		EndOp();
465140334f8SStefano Ceccherini 	} catch (status_t& status) {
466140334f8SStefano Ceccherini 		return status;
467140334f8SStefano Ceccherini 	}
4682c3505deSJohn Scipione 
469420fe80eSStefano Ceccherini 	return B_OK;
470420fe80eSStefano Ceccherini }
471420fe80eSStefano Ceccherini 
472420fe80eSStefano Ceccherini 
473420fe80eSStefano Ceccherini status_t
WriteDrawString(const BPoint & where,const char * string,const int32 & length,const escapement_delta & escapement)4742c3505deSJohn Scipione PictureDataWriter::WriteDrawString(const BPoint& where, const char* string,
4752c3505deSJohn Scipione 	const int32& length, const escapement_delta& escapement)
476420fe80eSStefano Ceccherini {
477140334f8SStefano Ceccherini 	try {
478420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_PEN_LOCATION);
479420fe80eSStefano Ceccherini 		Write<BPoint>(where);
480420fe80eSStefano Ceccherini 		EndOp();
481420fe80eSStefano Ceccherini 
482420fe80eSStefano Ceccherini 		BeginOp(B_PIC_DRAW_STRING);
483420fe80eSStefano Ceccherini 		Write<float>(escapement.space);
484420fe80eSStefano Ceccherini 		Write<float>(escapement.nonspace);
4858752f52dSStefano Ceccherini 		//WriteData(string, length + 1);
4868752f52dSStefano Ceccherini 			// TODO: is string 0 terminated? why is length given?
4870b0ecfabSMarcus Overhagen 		WriteData(string, length);
4880b0ecfabSMarcus Overhagen 		Write<uint8>(0);
489420fe80eSStefano Ceccherini 		EndOp();
490140334f8SStefano Ceccherini 	} catch (status_t& status) {
491140334f8SStefano Ceccherini 		return status;
492140334f8SStefano Ceccherini 	}
493420fe80eSStefano Ceccherini 
494420fe80eSStefano Ceccherini 	return B_OK;
495420fe80eSStefano Ceccherini }
496420fe80eSStefano Ceccherini 
497420fe80eSStefano Ceccherini 
498420fe80eSStefano Ceccherini status_t
WriteDrawString(const char * string,int32 length,const BPoint * locations,int32 locationCount)499954a0a0cSStephan Aßmus PictureDataWriter::WriteDrawString(const char* string,
500954a0a0cSStephan Aßmus 	int32 length, const BPoint* locations, int32 locationCount)
501954a0a0cSStephan Aßmus {
502954a0a0cSStephan Aßmus 	try {
503954a0a0cSStephan Aßmus 		BeginOp(B_PIC_DRAW_STRING_LOCATIONS);
504954a0a0cSStephan Aßmus 		Write<int32>(locationCount);
505954a0a0cSStephan Aßmus 		for (int32 i = 0; i < locationCount; i++) {
506954a0a0cSStephan Aßmus 			Write<BPoint>(locations[i]);
507954a0a0cSStephan Aßmus 		}
508954a0a0cSStephan Aßmus 		WriteData(string, length);
509954a0a0cSStephan Aßmus 		Write<uint8>(0);
510954a0a0cSStephan Aßmus 		EndOp();
511954a0a0cSStephan Aßmus 	} catch (status_t& status) {
512954a0a0cSStephan Aßmus 		return status;
513954a0a0cSStephan Aßmus 	}
514954a0a0cSStephan Aßmus 
515954a0a0cSStephan Aßmus 	return B_OK;
516954a0a0cSStephan Aßmus }
517954a0a0cSStephan Aßmus 
518954a0a0cSStephan Aßmus 
519954a0a0cSStephan Aßmus status_t
WriteDrawShape(const int32 & opCount,const void * opList,const int32 & ptCount,const void * ptList,const bool & fill)5202c3505deSJohn Scipione PictureDataWriter::WriteDrawShape(const int32& opCount, const void* opList,
5212c3505deSJohn Scipione 	const int32& ptCount, const void* ptList, const bool& fill)
522420fe80eSStefano Ceccherini {
523140334f8SStefano Ceccherini 	try {
524420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE);
525420fe80eSStefano Ceccherini 		Write<int32>(opCount);
526420fe80eSStefano Ceccherini 		Write<int32>(ptCount);
5270b0ecfabSMarcus Overhagen 		WriteData(opList, opCount * sizeof(uint32));
528420fe80eSStefano Ceccherini 		WriteData(ptList, ptCount * sizeof(BPoint));
529420fe80eSStefano Ceccherini 		EndOp();
530140334f8SStefano Ceccherini 	} catch (status_t& status) {
531140334f8SStefano Ceccherini 		return status;
532140334f8SStefano Ceccherini 	}
533420fe80eSStefano Ceccherini 
534420fe80eSStefano Ceccherini 	return B_OK;
535420fe80eSStefano Ceccherini }
536420fe80eSStefano Ceccherini 
537420fe80eSStefano Ceccherini 
538420fe80eSStefano Ceccherini status_t
WriteDrawRectGradient(const BRect & rect,const BGradient & gradient,const bool & fill)53990ab1a44SX512 PictureDataWriter::WriteDrawRectGradient(const BRect& rect, const BGradient& gradient, const bool& fill)
54090ab1a44SX512 {
54190ab1a44SX512 	try {
54290ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_RECT_GRADIENT : B_PIC_STROKE_RECT_GRADIENT);
54390ab1a44SX512 		Write<BRect>(rect);
54490ab1a44SX512 		gradient.Flatten(fData);
54590ab1a44SX512 		EndOp();
54690ab1a44SX512 	} catch (status_t& status) {
54790ab1a44SX512 		return status;
54890ab1a44SX512 	}
54990ab1a44SX512 
55090ab1a44SX512 	return B_OK;
55190ab1a44SX512 }
55290ab1a44SX512 
55390ab1a44SX512 
55490ab1a44SX512 status_t
WriteDrawRoundRectGradient(const BRect & rect,const BPoint & radius,const BGradient & gradient,const bool & fill)55590ab1a44SX512 PictureDataWriter::WriteDrawRoundRectGradient(const BRect& rect, const BPoint& radius, const BGradient& gradient,
55690ab1a44SX512 	const bool& fill)
55790ab1a44SX512 {
55890ab1a44SX512 	try {
55990ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ROUND_RECT_GRADIENT : B_PIC_STROKE_ROUND_RECT_GRADIENT);
56090ab1a44SX512 		Write<BRect>(rect);
56190ab1a44SX512 		Write<BPoint>(radius);
56290ab1a44SX512 		gradient.Flatten(fData);
56390ab1a44SX512 		EndOp();
56490ab1a44SX512 	} catch (status_t& status) {
56590ab1a44SX512 		return status;
56690ab1a44SX512 	}
56790ab1a44SX512 
56890ab1a44SX512 	return B_OK;
56990ab1a44SX512 }
57090ab1a44SX512 
57190ab1a44SX512 
57290ab1a44SX512 status_t
WriteDrawBezierGradient(const BPoint points[4],const BGradient & gradient,const bool & fill)57390ab1a44SX512 PictureDataWriter::WriteDrawBezierGradient(const BPoint points[4], const BGradient& gradient, const bool& fill)
57490ab1a44SX512 {
57590ab1a44SX512 	try {
57690ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_BEZIER_GRADIENT : B_PIC_STROKE_BEZIER_GRADIENT);
57790ab1a44SX512 		for (int32 i = 0; i < 4; i++)
57890ab1a44SX512 			Write<BPoint>(points[i]);
57990ab1a44SX512 
58090ab1a44SX512 		gradient.Flatten(fData);
58190ab1a44SX512 		EndOp();
58290ab1a44SX512 	} catch (status_t& status) {
58390ab1a44SX512 		return status;
58490ab1a44SX512 	}
58590ab1a44SX512 
58690ab1a44SX512 	return B_OK;
58790ab1a44SX512 }
58890ab1a44SX512 
58990ab1a44SX512 
59090ab1a44SX512 status_t
WriteDrawArcGradient(const BPoint & center,const BPoint & radius,const float & startTheta,const float & arcTheta,const BGradient & gradient,const bool & fill)59190ab1a44SX512 PictureDataWriter::WriteDrawArcGradient(const BPoint& center, const BPoint& radius,
59290ab1a44SX512 	const float& startTheta, const float& arcTheta, const BGradient& gradient, const bool& fill)
59390ab1a44SX512 {
59490ab1a44SX512 	try {
59590ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ARC_GRADIENT : B_PIC_STROKE_ARC_GRADIENT);
59690ab1a44SX512 		Write<BPoint>(center);
59790ab1a44SX512 		Write<BPoint>(radius);
59890ab1a44SX512 		Write<float>(startTheta);
59990ab1a44SX512 		Write<float>(arcTheta);
60090ab1a44SX512 		gradient.Flatten(fData);
60190ab1a44SX512 		EndOp();
60290ab1a44SX512 	} catch (status_t& status) {
60390ab1a44SX512 		return status;
60490ab1a44SX512 	}
60590ab1a44SX512 
60690ab1a44SX512 	return B_OK;
60790ab1a44SX512 }
60890ab1a44SX512 
60990ab1a44SX512 
61090ab1a44SX512 status_t
WriteDrawEllipseGradient(const BRect & rect,const BGradient & gradient,const bool & fill)61190ab1a44SX512 PictureDataWriter::WriteDrawEllipseGradient(const BRect& rect, const BGradient& gradient, const bool& fill)
61290ab1a44SX512 {
61390ab1a44SX512 	try {
61490ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ELLIPSE_GRADIENT : B_PIC_STROKE_ELLIPSE_GRADIENT);
61590ab1a44SX512 		Write<BRect>(rect);
61690ab1a44SX512 		gradient.Flatten(fData);
61790ab1a44SX512 		EndOp();
61890ab1a44SX512 	} catch (status_t& status) {
61990ab1a44SX512 		return status;
62090ab1a44SX512 	}
62190ab1a44SX512 
62290ab1a44SX512 	return B_OK;
62390ab1a44SX512 }
62490ab1a44SX512 
62590ab1a44SX512 
62690ab1a44SX512 status_t
WriteDrawPolygonGradient(const int32 & numPoints,BPoint * points,const bool & isClosed,const BGradient & gradient,const bool & fill)62790ab1a44SX512 PictureDataWriter::WriteDrawPolygonGradient(const int32& numPoints, BPoint* points,
62890ab1a44SX512 	const bool& isClosed, const BGradient& gradient, const bool& fill)
62990ab1a44SX512 {
63090ab1a44SX512 	try {
63190ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_POLYGON_GRADIENT : B_PIC_STROKE_POLYGON_GRADIENT);
63290ab1a44SX512 		Write<int32>(numPoints);
63390ab1a44SX512 		for (int32 i = 0; i < numPoints; i++)
63490ab1a44SX512 			Write<BPoint>(points[i]);
63590ab1a44SX512 
63690ab1a44SX512 		if (!fill)
63790ab1a44SX512 			Write<uint8>((uint8)isClosed);
63890ab1a44SX512 
63990ab1a44SX512 		gradient.Flatten(fData);
64090ab1a44SX512 		EndOp();
64190ab1a44SX512 	} catch (status_t& status) {
64290ab1a44SX512 		return status;
64390ab1a44SX512 	}
64490ab1a44SX512 
64590ab1a44SX512 	return B_OK;
64690ab1a44SX512 }
64790ab1a44SX512 
64890ab1a44SX512 
64990ab1a44SX512 status_t
WriteDrawShapeGradient(const int32 & opCount,const void * opList,const int32 & ptCount,const void * ptList,const BGradient & gradient,const bool & fill)65090ab1a44SX512 PictureDataWriter::WriteDrawShapeGradient(const int32& opCount, const void* opList,
65190ab1a44SX512 	const int32& ptCount, const void* ptList, const BGradient& gradient, const bool& fill)
65290ab1a44SX512 {
65390ab1a44SX512 	try {
65490ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_SHAPE_GRADIENT : B_PIC_STROKE_SHAPE_GRADIENT);
65590ab1a44SX512 		Write<int32>(opCount);
65690ab1a44SX512 		Write<int32>(ptCount);
65790ab1a44SX512 		WriteData(opList, opCount * sizeof(uint32));
65890ab1a44SX512 		WriteData(ptList, ptCount * sizeof(BPoint));
65990ab1a44SX512 		gradient.Flatten(fData);
66090ab1a44SX512 		EndOp();
66190ab1a44SX512 	} catch (status_t& status) {
66290ab1a44SX512 		return status;
66390ab1a44SX512 	}
66490ab1a44SX512 
66590ab1a44SX512 	return B_OK;
66690ab1a44SX512 }
66790ab1a44SX512 
66890ab1a44SX512 
66990ab1a44SX512 status_t
WriteDrawBitmap(const BRect & srcRect,const BRect & dstRect,const int32 & width,const int32 & height,const int32 & bytesPerRow,const int32 & colorSpace,const int32 & flags,const void * data,const int32 & length)6702c3505deSJohn Scipione PictureDataWriter::WriteDrawBitmap(const BRect& srcRect, const BRect& dstRect,
6712c3505deSJohn Scipione 	const int32& width, const int32& height, const int32& bytesPerRow,
6722c3505deSJohn Scipione 	const int32& colorSpace, const int32& flags, const void* data,
6732c3505deSJohn Scipione 	const int32& length)
674420fe80eSStefano Ceccherini {
6750b0ecfabSMarcus Overhagen 	if (length != height * bytesPerRow)
6760b0ecfabSMarcus Overhagen 		debugger("PictureDataWriter::WriteDrawBitmap: invalid length");
677140334f8SStefano Ceccherini 	try {
678420fe80eSStefano Ceccherini 		BeginOp(B_PIC_DRAW_PIXELS);
679420fe80eSStefano Ceccherini 		Write<BRect>(srcRect);
680420fe80eSStefano Ceccherini 		Write<BRect>(dstRect);
681420fe80eSStefano Ceccherini 		Write<int32>(width);
682420fe80eSStefano Ceccherini 		Write<int32>(height);
683420fe80eSStefano Ceccherini 		Write<int32>(bytesPerRow);
684420fe80eSStefano Ceccherini 		Write<int32>(colorSpace);
685420fe80eSStefano Ceccherini 		Write<int32>(flags);
686420fe80eSStefano Ceccherini 		WriteData(data, length);
687420fe80eSStefano Ceccherini 		EndOp();
688140334f8SStefano Ceccherini 	} catch (status_t& status) {
689140334f8SStefano Ceccherini 		return status;
690140334f8SStefano Ceccherini 	}
6912c3505deSJohn Scipione 
692420fe80eSStefano Ceccherini 	return B_OK;
693420fe80eSStefano Ceccherini }
694420fe80eSStefano Ceccherini 
695420fe80eSStefano Ceccherini 
696420fe80eSStefano Ceccherini status_t
WriteDrawPicture(const BPoint & where,const int32 & token)6972c3505deSJohn Scipione PictureDataWriter::WriteDrawPicture(const BPoint& where, const int32& token)
698ed5de868SStefano Ceccherini {
6998752f52dSStefano Ceccherini 	// TODO: I'm not sure about this function. I think we need
7008752f52dSStefano Ceccherini 	// to attach the picture data too.
7018752f52dSStefano Ceccherini 	// The token won't be sufficient in many cases (for example, when
7028752f52dSStefano Ceccherini 	// we archive/flatten the picture.
703ed5de868SStefano Ceccherini 	try {
704ed5de868SStefano Ceccherini 		BeginOp(B_PIC_DRAW_PICTURE);
705ed5de868SStefano Ceccherini 		Write<BPoint>(where);
706ed5de868SStefano Ceccherini 		Write<int32>(token);
707ed5de868SStefano Ceccherini 		EndOp();
708ed5de868SStefano Ceccherini 	} catch (status_t& status) {
709ed5de868SStefano Ceccherini 		return status;
710ed5de868SStefano Ceccherini 	}
7112c3505deSJohn Scipione 
712ed5de868SStefano Ceccherini 	return B_OK;
713ed5de868SStefano Ceccherini }
714ed5de868SStefano Ceccherini 
715ed5de868SStefano Ceccherini 
716ed5de868SStefano Ceccherini status_t
WriteSetFontFamily(const font_family family)717feb557a6SStefano Ceccherini PictureDataWriter::WriteSetFontFamily(const font_family family)
718a3d758e3SStefano Ceccherini {
719feb557a6SStefano Ceccherini 	try {
720feb557a6SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FAMILY);
721feb557a6SStefano Ceccherini 		WriteData(family, strlen(family));
722796e4e45SStefano Ceccherini 		Write<uint8>(0);
723feb557a6SStefano Ceccherini 		EndOp();
724feb557a6SStefano Ceccherini 	} catch (status_t& status) {
725feb557a6SStefano Ceccherini 		return status;
726feb557a6SStefano Ceccherini 	}
7272c3505deSJohn Scipione 
728a3d758e3SStefano Ceccherini 	return B_OK;
729a3d758e3SStefano Ceccherini }
730a3d758e3SStefano Ceccherini 
731a3d758e3SStefano Ceccherini 
732a3d758e3SStefano Ceccherini status_t
WriteSetFontStyle(const font_style style)733feb557a6SStefano Ceccherini PictureDataWriter::WriteSetFontStyle(const font_style style)
734a3d758e3SStefano Ceccherini {
735feb557a6SStefano Ceccherini 	try {
736feb557a6SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_STYLE);
737feb557a6SStefano Ceccherini 		WriteData(style, strlen(style));
738796e4e45SStefano Ceccherini 		Write<uint8>(0);
739feb557a6SStefano Ceccherini 		EndOp();
740feb557a6SStefano Ceccherini 	} catch (status_t& status) {
741feb557a6SStefano Ceccherini 		return status;
742feb557a6SStefano Ceccherini 	}
7432c3505deSJohn Scipione 
744a3d758e3SStefano Ceccherini 	return B_OK;
745a3d758e3SStefano Ceccherini }
746a3d758e3SStefano Ceccherini 
747a3d758e3SStefano Ceccherini 
748a3d758e3SStefano Ceccherini status_t
WriteSetFontSpacing(const int32 & spacing)749a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontSpacing(const int32& spacing)
750a3d758e3SStefano Ceccherini {
751140334f8SStefano Ceccherini 	try {
752a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SPACING);
753a3d758e3SStefano Ceccherini 		Write<int32>(spacing);
754a3d758e3SStefano Ceccherini 		EndOp();
755140334f8SStefano Ceccherini 	} catch (status_t& status) {
756140334f8SStefano Ceccherini 		return status;
757140334f8SStefano Ceccherini 	}
7582c3505deSJohn Scipione 
759a3d758e3SStefano Ceccherini 	return B_OK;
760a3d758e3SStefano Ceccherini }
761a3d758e3SStefano Ceccherini 
762a3d758e3SStefano Ceccherini 
763a3d758e3SStefano Ceccherini status_t
WriteSetFontSize(const float & size)764a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontSize(const float& size)
765a3d758e3SStefano Ceccherini {
766140334f8SStefano Ceccherini 	try {
767a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SIZE);
768a3d758e3SStefano Ceccherini 		Write<float>(size);
769a3d758e3SStefano Ceccherini 		EndOp();
770140334f8SStefano Ceccherini 	} catch (status_t& status) {
771140334f8SStefano Ceccherini 		return status;
772140334f8SStefano Ceccherini 	}
7732c3505deSJohn Scipione 
774a3d758e3SStefano Ceccherini 	return B_OK;
775a3d758e3SStefano Ceccherini }
776a3d758e3SStefano Ceccherini 
777a3d758e3SStefano Ceccherini 
778a3d758e3SStefano Ceccherini status_t
WriteSetFontRotation(const float & rotation)779a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontRotation(const float& rotation)
780a3d758e3SStefano Ceccherini {
781140334f8SStefano Ceccherini 	try {
782a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_ROTATE);
783a3d758e3SStefano Ceccherini 		Write<float>(rotation);
784a3d758e3SStefano Ceccherini 		EndOp();
785140334f8SStefano Ceccherini 	} catch (status_t& status) {
786140334f8SStefano Ceccherini 		return status;
787140334f8SStefano Ceccherini 	}
7882c3505deSJohn Scipione 
789a3d758e3SStefano Ceccherini 	return B_OK;
790a3d758e3SStefano Ceccherini }
791a3d758e3SStefano Ceccherini 
792a3d758e3SStefano Ceccherini 
793a3d758e3SStefano Ceccherini status_t
WriteSetFontEncoding(const int32 & encoding)794a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontEncoding(const int32& encoding)
795a3d758e3SStefano Ceccherini {
796140334f8SStefano Ceccherini 	try {
797a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_ENCODING);
798a3d758e3SStefano Ceccherini 		Write<int32>(encoding);
799a3d758e3SStefano Ceccherini 		EndOp();
800140334f8SStefano Ceccherini 	} catch (status_t& status) {
801140334f8SStefano Ceccherini 		return status;
802140334f8SStefano Ceccherini 	}
8032c3505deSJohn Scipione 
804a3d758e3SStefano Ceccherini 	return B_OK;
805a3d758e3SStefano Ceccherini }
806a3d758e3SStefano Ceccherini 
807a3d758e3SStefano Ceccherini 
808a3d758e3SStefano Ceccherini status_t
WriteSetFontFlags(const int32 & flags)809a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontFlags(const int32& flags)
810a3d758e3SStefano Ceccherini {
811140334f8SStefano Ceccherini 	try {
812a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FLAGS);
813a3d758e3SStefano Ceccherini 		Write<int32>(flags);
814a3d758e3SStefano Ceccherini 		EndOp();
815140334f8SStefano Ceccherini 	} catch (status_t& status) {
816140334f8SStefano Ceccherini 		return status;
817140334f8SStefano Ceccherini 	}
8182c3505deSJohn Scipione 
819a3d758e3SStefano Ceccherini 	return B_OK;
820a3d758e3SStefano Ceccherini }
821a3d758e3SStefano Ceccherini 
822a3d758e3SStefano Ceccherini 
823a3d758e3SStefano Ceccherini status_t
WriteSetFontShear(const float & shear)824b1af0a7aSStefano Ceccherini PictureDataWriter::WriteSetFontShear(const float& shear)
825a3d758e3SStefano Ceccherini {
826140334f8SStefano Ceccherini 	try {
827a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SHEAR);
828b1af0a7aSStefano Ceccherini 		Write<float>(shear);
829a3d758e3SStefano Ceccherini 		EndOp();
830140334f8SStefano Ceccherini 	} catch (status_t& status) {
831140334f8SStefano Ceccherini 		return status;
832140334f8SStefano Ceccherini 	}
8332c3505deSJohn Scipione 
834a3d758e3SStefano Ceccherini 	return B_OK;
835a3d758e3SStefano Ceccherini }
836a3d758e3SStefano Ceccherini 
837a3d758e3SStefano Ceccherini 
838a3d758e3SStefano Ceccherini status_t
WriteSetFontFace(const int32 & face)839a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontFace(const int32& face)
840a3d758e3SStefano Ceccherini {
841140334f8SStefano Ceccherini 	try {
842a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FACE);
843a3d758e3SStefano Ceccherini 		Write<int32>(face);
844a3d758e3SStefano Ceccherini 		EndOp();
845140334f8SStefano Ceccherini 	} catch (status_t& status) {
846140334f8SStefano Ceccherini 		return status;
847140334f8SStefano Ceccherini 	}
8482c3505deSJohn Scipione 
849a3d758e3SStefano Ceccherini 	return B_OK;
850a3d758e3SStefano Ceccherini }
851a3d758e3SStefano Ceccherini 
852a3d758e3SStefano Ceccherini 
853a3d758e3SStefano Ceccherini status_t
WritePushState()854420fe80eSStefano Ceccherini PictureDataWriter::WritePushState()
855420fe80eSStefano Ceccherini {
856140334f8SStefano Ceccherini 	try {
857420fe80eSStefano Ceccherini 		BeginOp(B_PIC_PUSH_STATE);
858420fe80eSStefano Ceccherini 		EndOp();
859140334f8SStefano Ceccherini 	} catch (status_t& status) {
860140334f8SStefano Ceccherini 		return status;
861140334f8SStefano Ceccherini 	}
8622c3505deSJohn Scipione 
863420fe80eSStefano Ceccherini 	return B_OK;
864420fe80eSStefano Ceccherini }
865420fe80eSStefano Ceccherini 
866420fe80eSStefano Ceccherini 
867420fe80eSStefano Ceccherini status_t
WritePopState()868420fe80eSStefano Ceccherini PictureDataWriter::WritePopState()
869420fe80eSStefano Ceccherini {
870140334f8SStefano Ceccherini 	try {
871420fe80eSStefano Ceccherini 		BeginOp(B_PIC_POP_STATE);
872420fe80eSStefano Ceccherini 		EndOp();
873140334f8SStefano Ceccherini 	} catch (status_t& status) {
874140334f8SStefano Ceccherini 		return status;
875140334f8SStefano Ceccherini 	}
8762c3505deSJohn Scipione 
877420fe80eSStefano Ceccherini 	return B_OK;
878420fe80eSStefano Ceccherini }
879420fe80eSStefano Ceccherini 
880420fe80eSStefano Ceccherini 
881551438b9SJulian Harnath status_t
WriteBlendLayer(Layer * layer)882551438b9SJulian Harnath PictureDataWriter::WriteBlendLayer(Layer* layer)
883551438b9SJulian Harnath {
884551438b9SJulian Harnath 	try {
885551438b9SJulian Harnath 		BeginOp(B_PIC_BLEND_LAYER);
886551438b9SJulian Harnath 		Write<Layer*>(layer);
887551438b9SJulian Harnath 		EndOp();
888551438b9SJulian Harnath 	} catch (status_t& status) {
889551438b9SJulian Harnath 		return status;
890551438b9SJulian Harnath 	}
891551438b9SJulian Harnath 
892551438b9SJulian Harnath 	return B_OK;
893551438b9SJulian Harnath }
894551438b9SJulian Harnath 
895551438b9SJulian Harnath 
8964bd6f322SJulian Harnath status_t
WriteClipToRect(const BRect & rect,bool inverse)8974bd6f322SJulian Harnath PictureDataWriter::WriteClipToRect(const BRect& rect, bool inverse)
8984bd6f322SJulian Harnath {
8994bd6f322SJulian Harnath 	try {
9004bd6f322SJulian Harnath 		BeginOp(B_PIC_CLIP_TO_RECT);
9014bd6f322SJulian Harnath 		Write<bool>(inverse);
9024bd6f322SJulian Harnath 		Write<BRect>(rect);
9034bd6f322SJulian Harnath 		EndOp();
9044bd6f322SJulian Harnath 	} catch (status_t& status) {
9054bd6f322SJulian Harnath 		return status;
9064bd6f322SJulian Harnath 	}
9074bd6f322SJulian Harnath 
9084bd6f322SJulian Harnath 	return B_OK;
9094bd6f322SJulian Harnath }
9104bd6f322SJulian Harnath 
9114bd6f322SJulian Harnath 
9124bd6f322SJulian Harnath status_t
WriteClipToShape(int32 opCount,const void * opList,int32 ptCount,const void * ptList,bool inverse)9134bd6f322SJulian Harnath PictureDataWriter::WriteClipToShape(int32 opCount, const void* opList,
9144bd6f322SJulian Harnath 	int32 ptCount, const void* ptList, bool inverse)
9154bd6f322SJulian Harnath {
9164bd6f322SJulian Harnath 	try {
9174bd6f322SJulian Harnath 		BeginOp(B_PIC_CLIP_TO_SHAPE);
9184bd6f322SJulian Harnath 		Write<bool>(inverse);
9194bd6f322SJulian Harnath 		Write<int32>(opCount);
9204bd6f322SJulian Harnath 		Write<int32>(ptCount);
9214bd6f322SJulian Harnath 		WriteData(opList, opCount * sizeof(uint32));
9224bd6f322SJulian Harnath 		WriteData(ptList, ptCount * sizeof(BPoint));
9234bd6f322SJulian Harnath 		EndOp();
9244bd6f322SJulian Harnath 	} catch (status_t& status) {
9254bd6f322SJulian Harnath 		return status;
9264bd6f322SJulian Harnath 	}
9274bd6f322SJulian Harnath 
9284bd6f322SJulian Harnath 	return B_OK;
9294bd6f322SJulian Harnath }
9304bd6f322SJulian Harnath 
9314bd6f322SJulian Harnath 
932420fe80eSStefano Ceccherini // private
933140334f8SStefano Ceccherini void
BeginOp(const int16 & op)934420fe80eSStefano Ceccherini PictureDataWriter::BeginOp(const int16& op)
935420fe80eSStefano Ceccherini {
936140334f8SStefano Ceccherini 	if (fData == NULL)
93784c2abeeSStefano Ceccherini 		THROW_ERROR(B_NO_INIT);
938140334f8SStefano Ceccherini 
939420fe80eSStefano Ceccherini 	fStack.push(fData->Position());
940420fe80eSStefano Ceccherini 	fData->Write(&op, sizeof(op));
941420fe80eSStefano Ceccherini 
942420fe80eSStefano Ceccherini 	// Init the size of the opcode block to 0
943efffcb29SAlex Smith 	int32 size = 0;
944420fe80eSStefano Ceccherini 	fData->Write(&size, sizeof(size));
945420fe80eSStefano Ceccherini }
946420fe80eSStefano Ceccherini 
947420fe80eSStefano Ceccherini 
948140334f8SStefano Ceccherini void
EndOp()949420fe80eSStefano Ceccherini PictureDataWriter::EndOp()
950420fe80eSStefano Ceccherini {
951140334f8SStefano Ceccherini 	if (fData == NULL)
95284c2abeeSStefano Ceccherini 		THROW_ERROR(B_NO_INIT);
953140334f8SStefano Ceccherini 
954420fe80eSStefano Ceccherini 	off_t curPos = fData->Position();
955420fe80eSStefano Ceccherini 	off_t stackPos = fStack.top();
956420fe80eSStefano Ceccherini 	fStack.pop();
957420fe80eSStefano Ceccherini 
958420fe80eSStefano Ceccherini 	// The size of the op is calculated like this:
959420fe80eSStefano Ceccherini 	// current position on the stream minus the position on the stack,
960420fe80eSStefano Ceccherini 	// minus the space occupied by the op code itself (int16)
961efffcb29SAlex Smith 	// and the space occupied by the size field (int32)
962efffcb29SAlex Smith 	int32 size = curPos - stackPos - sizeof(int32) - sizeof(int16);
963420fe80eSStefano Ceccherini 
9648752f52dSStefano Ceccherini 	// Size was set to 0 in BeginOp()
9658752f52dSStefano Ceccherini 	// Now we overwrite it with the correct value
966420fe80eSStefano Ceccherini 	fData->Seek(stackPos + sizeof(int16), SEEK_SET);
967420fe80eSStefano Ceccherini 	fData->Write(&size, sizeof(size));
968420fe80eSStefano Ceccherini 	fData->Seek(curPos, SEEK_SET);
969420fe80eSStefano Ceccherini }
970420fe80eSStefano Ceccherini 
971420fe80eSStefano Ceccherini 
972140334f8SStefano Ceccherini void
WriteData(const void * data,size_t size)973420fe80eSStefano Ceccherini PictureDataWriter::WriteData(const void* data, size_t size)
974420fe80eSStefano Ceccherini {
975140334f8SStefano Ceccherini 	ssize_t result = fData->Write(data, size);
976140334f8SStefano Ceccherini 	if (result < 0)
97784c2abeeSStefano Ceccherini 		THROW_ERROR(result);
9782c3505deSJohn Scipione 
979ed5de868SStefano Ceccherini 	if ((size_t)result != size)
98084c2abeeSStefano Ceccherini 		THROW_ERROR(B_IO_ERROR);
981420fe80eSStefano Ceccherini }
982