xref: /haiku/src/kits/interface/PictureDataWriter.cpp (revision 90ab1a44ad13fcd373397744fae3434fead4aa6b)
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>
17*90ab1a44SX512 #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.
304bfa8f22SStefano Ceccherini PictureDataWriter::PictureDataWriter()
314bfa8f22SStefano Ceccherini 	:
324bfa8f22SStefano Ceccherini 	fData(NULL)
334bfa8f22SStefano Ceccherini {
344bfa8f22SStefano Ceccherini }
354bfa8f22SStefano Ceccherini 
364bfa8f22SStefano Ceccherini 
37420fe80eSStefano Ceccherini PictureDataWriter::PictureDataWriter(BPositionIO* data)
38420fe80eSStefano Ceccherini 	:
39420fe80eSStefano Ceccherini 	fData(data)
40420fe80eSStefano Ceccherini {
41420fe80eSStefano Ceccherini }
42420fe80eSStefano Ceccherini 
43420fe80eSStefano Ceccherini 
448752f52dSStefano Ceccherini PictureDataWriter::~PictureDataWriter()
458752f52dSStefano Ceccherini {
468752f52dSStefano Ceccherini }
478752f52dSStefano Ceccherini 
488752f52dSStefano Ceccherini 
49420fe80eSStefano Ceccherini status_t
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
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
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
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
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
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
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
160420fe80eSStefano Ceccherini PictureDataWriter::WriteSetScale(const float& scale)
161420fe80eSStefano Ceccherini {
162140334f8SStefano Ceccherini 	try {
163420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_SCALE);
164420fe80eSStefano Ceccherini 		Write<float>(scale);
165420fe80eSStefano Ceccherini 		EndOp();
166140334f8SStefano Ceccherini 	} catch (status_t& status) {
167140334f8SStefano Ceccherini 		return status;
168140334f8SStefano Ceccherini 	}
1692c3505deSJohn Scipione 
170420fe80eSStefano Ceccherini 	return B_OK;
171420fe80eSStefano Ceccherini }
172420fe80eSStefano Ceccherini 
173420fe80eSStefano Ceccherini 
174420fe80eSStefano Ceccherini status_t
175a6db6bd4SStephan Aßmus PictureDataWriter::WriteSetTransform(BAffineTransform transform)
176a6db6bd4SStephan Aßmus {
177a6db6bd4SStephan Aßmus 	try {
178a6db6bd4SStephan Aßmus 		BeginOp(B_PIC_SET_TRANSFORM);
179a6db6bd4SStephan Aßmus 		Write<BAffineTransform>(transform);
180a6db6bd4SStephan Aßmus 		EndOp();
181a6db6bd4SStephan Aßmus 	} catch (status_t& status) {
182a6db6bd4SStephan Aßmus 		return status;
183a6db6bd4SStephan Aßmus 	}
1842c3505deSJohn Scipione 
185a6db6bd4SStephan Aßmus 	return B_OK;
186a6db6bd4SStephan Aßmus }
187a6db6bd4SStephan Aßmus 
188a6db6bd4SStephan Aßmus 
189a6db6bd4SStephan Aßmus status_t
19025dbf74bSJulian Harnath PictureDataWriter::WriteTranslateBy(double x, double y)
19125dbf74bSJulian Harnath {
19225dbf74bSJulian Harnath 	try {
19325dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_TRANSLATE);
19425dbf74bSJulian Harnath 		Write<double>(x);
19525dbf74bSJulian Harnath 		Write<double>(y);
19625dbf74bSJulian Harnath 		EndOp();
19725dbf74bSJulian Harnath 	} catch (status_t& status) {
19825dbf74bSJulian Harnath 		return status;
19925dbf74bSJulian Harnath 	}
20025dbf74bSJulian Harnath 
20125dbf74bSJulian Harnath 	return B_OK;
20225dbf74bSJulian Harnath }
20325dbf74bSJulian Harnath 
20425dbf74bSJulian Harnath 
20525dbf74bSJulian Harnath status_t
20625dbf74bSJulian Harnath PictureDataWriter::WriteScaleBy(double x, double y)
20725dbf74bSJulian Harnath {
20825dbf74bSJulian Harnath 	try {
20925dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_SCALE);
21025dbf74bSJulian Harnath 		Write<double>(x);
21125dbf74bSJulian Harnath 		Write<double>(y);
21225dbf74bSJulian Harnath 		EndOp();
21325dbf74bSJulian Harnath 	} catch (status_t& status) {
21425dbf74bSJulian Harnath 		return status;
21525dbf74bSJulian Harnath 	}
21625dbf74bSJulian Harnath 
21725dbf74bSJulian Harnath 	return B_OK;
21825dbf74bSJulian Harnath }
21925dbf74bSJulian Harnath 
22025dbf74bSJulian Harnath 
22125dbf74bSJulian Harnath status_t
22225dbf74bSJulian Harnath PictureDataWriter::WriteRotateBy(double angleRadians)
22325dbf74bSJulian Harnath {
22425dbf74bSJulian Harnath 	try {
22525dbf74bSJulian Harnath 		BeginOp(B_PIC_AFFINE_ROTATE);
22625dbf74bSJulian Harnath 		Write<double>(angleRadians);
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
2372c3505deSJohn Scipione PictureDataWriter::WriteSetPattern(const ::pattern& pattern)
23837033892SStefano Ceccherini {
23937033892SStefano Ceccherini 	try {
24037033892SStefano Ceccherini 		BeginOp(B_PIC_SET_STIPLE_PATTERN);
2412c3505deSJohn Scipione 		Write< ::pattern>(pattern);
24237033892SStefano Ceccherini 		EndOp();
24337033892SStefano Ceccherini 	} catch (status_t& status) {
24437033892SStefano Ceccherini 		return status;
24537033892SStefano Ceccherini 	}
2462c3505deSJohn Scipione 
24737033892SStefano Ceccherini 	return B_OK;
24837033892SStefano Ceccherini }
24937033892SStefano Ceccherini 
25037033892SStefano Ceccherini 
25137033892SStefano Ceccherini status_t
252ce27f961SStefano Ceccherini PictureDataWriter::WriteClipToPicture(int32 pictureToken,
253ce27f961SStefano Ceccherini 						const BPoint& origin, bool inverse)
254ce27f961SStefano Ceccherini {
255ce27f961SStefano Ceccherini 	// TODO: I don't know if it's compatible with R5's BPicture version
256ce27f961SStefano Ceccherini 	try {
257ce27f961SStefano Ceccherini 		BeginOp(B_PIC_CLIP_TO_PICTURE);
258ce27f961SStefano Ceccherini 		Write<int32>(pictureToken);
259ce27f961SStefano Ceccherini 		Write<BPoint>(origin);
260ce27f961SStefano Ceccherini 		Write<bool>(inverse);
261ce27f961SStefano Ceccherini 		EndOp();
262ce27f961SStefano Ceccherini 	} catch (status_t& status) {
263ce27f961SStefano Ceccherini 		return status;
264ce27f961SStefano Ceccherini 	}
265ce27f961SStefano Ceccherini 
266ce27f961SStefano Ceccherini 	return B_OK;
267ce27f961SStefano Ceccherini }
268ce27f961SStefano Ceccherini 
269ce27f961SStefano Ceccherini 
270ce27f961SStefano Ceccherini status_t
2718752f52dSStefano Ceccherini PictureDataWriter::WriteSetClipping(const BRegion& region)
272253d42a6SStefano Ceccherini {
273253d42a6SStefano Ceccherini 	// TODO: I don't know if it's compatible with R5's BPicture version
274253d42a6SStefano Ceccherini 	try {
275253d42a6SStefano Ceccherini 		const int32 numRects = region.CountRects();
276253d42a6SStefano Ceccherini 		if (numRects > 0 && region.Frame().IsValid()) {
277253d42a6SStefano Ceccherini 			BeginOp(B_PIC_SET_CLIPPING_RECTS);
27846b4e489SStefano Ceccherini 			Write<uint32>(numRects);
2792c3505deSJohn Scipione 			for (int32 i = 0; i < numRects; i++)
280253d42a6SStefano Ceccherini 				Write<BRect>(region.RectAt(i));
2812c3505deSJohn Scipione 
282253d42a6SStefano Ceccherini 			EndOp();
283253d42a6SStefano Ceccherini 		} else
284253d42a6SStefano Ceccherini 			WriteClearClipping();
285253d42a6SStefano Ceccherini 	} catch (status_t& status) {
286253d42a6SStefano Ceccherini 		return status;
287253d42a6SStefano Ceccherini 	}
28846b4e489SStefano Ceccherini 
289253d42a6SStefano Ceccherini 	return B_OK;
290253d42a6SStefano Ceccherini }
291253d42a6SStefano Ceccherini 
292253d42a6SStefano Ceccherini 
293253d42a6SStefano Ceccherini status_t
294253d42a6SStefano Ceccherini PictureDataWriter::WriteClearClipping()
295253d42a6SStefano Ceccherini {
296253d42a6SStefano Ceccherini 	try {
297253d42a6SStefano Ceccherini 		BeginOp(B_PIC_CLEAR_CLIPPING_RECTS);
298253d42a6SStefano Ceccherini 		EndOp();
299253d42a6SStefano Ceccherini 	} catch (status_t& status) {
300253d42a6SStefano Ceccherini 		return status;
301253d42a6SStefano Ceccherini 	}
3022c3505deSJohn Scipione 
303253d42a6SStefano Ceccherini 	return B_OK;
304253d42a6SStefano Ceccherini }
305253d42a6SStefano Ceccherini 
306253d42a6SStefano Ceccherini 
307253d42a6SStefano Ceccherini status_t
308420fe80eSStefano Ceccherini PictureDataWriter::WriteSetHighColor(const rgb_color& color)
309420fe80eSStefano Ceccherini {
310140334f8SStefano Ceccherini 	try {
311420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_FORE_COLOR);
312420fe80eSStefano Ceccherini 		Write<rgb_color>(color);
313420fe80eSStefano Ceccherini 		EndOp();
314140334f8SStefano Ceccherini 	} catch (status_t& status) {
315140334f8SStefano Ceccherini 		return status;
316140334f8SStefano Ceccherini 	}
3172c3505deSJohn Scipione 
318420fe80eSStefano Ceccherini 	return B_OK;
319420fe80eSStefano Ceccherini }
320420fe80eSStefano Ceccherini 
321420fe80eSStefano Ceccherini 
322420fe80eSStefano Ceccherini status_t
323420fe80eSStefano Ceccherini PictureDataWriter::WriteSetLowColor(const rgb_color& color)
324420fe80eSStefano Ceccherini {
325140334f8SStefano Ceccherini 	try {
326420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_BACK_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
3382c3505deSJohn Scipione PictureDataWriter::WriteDrawRect(const BRect& rect, const bool& fill)
339420fe80eSStefano Ceccherini {
340140334f8SStefano Ceccherini 	try {
341420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_RECT : B_PIC_STROKE_RECT);
342420fe80eSStefano Ceccherini 		Write<BRect>(rect);
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
3532c3505deSJohn Scipione PictureDataWriter::WriteDrawRoundRect(const BRect& rect, const BPoint& radius,
3542c3505deSJohn Scipione 	const bool& fill)
355420fe80eSStefano Ceccherini {
356140334f8SStefano Ceccherini 	try {
357420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ROUND_RECT : B_PIC_STROKE_ROUND_RECT);
358420fe80eSStefano Ceccherini 		Write<BRect>(rect);
359420fe80eSStefano Ceccherini 		Write<BPoint>(radius);
360420fe80eSStefano Ceccherini 		EndOp();
361140334f8SStefano Ceccherini 	} catch (status_t& status) {
362140334f8SStefano Ceccherini 		return status;
363140334f8SStefano Ceccherini 	}
3642c3505deSJohn Scipione 
365420fe80eSStefano Ceccherini 	return B_OK;
366420fe80eSStefano Ceccherini }
367420fe80eSStefano Ceccherini 
368420fe80eSStefano Ceccherini 
369420fe80eSStefano Ceccherini status_t
370420fe80eSStefano Ceccherini PictureDataWriter::WriteDrawEllipse(const BRect& rect, const bool& fill)
371420fe80eSStefano Ceccherini {
372140334f8SStefano Ceccherini 	try {
373420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ELLIPSE : B_PIC_STROKE_ELLIPSE);
374420fe80eSStefano Ceccherini 		Write<BRect>(rect);
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
3852c3505deSJohn Scipione PictureDataWriter::WriteDrawArc(const BPoint& center, const BPoint& radius,
3862c3505deSJohn Scipione 	const float& startTheta, const float& arcTheta, const bool& fill)
387420fe80eSStefano Ceccherini {
388140334f8SStefano Ceccherini 	try {
389420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_ARC : B_PIC_STROKE_ARC);
390420fe80eSStefano Ceccherini 		Write<BPoint>(center);
391420fe80eSStefano Ceccherini 		Write<BPoint>(radius);
392420fe80eSStefano Ceccherini 		Write<float>(startTheta);
393420fe80eSStefano Ceccherini 		Write<float>(arcTheta);
394420fe80eSStefano Ceccherini 		EndOp();
395140334f8SStefano Ceccherini 	} catch (status_t& status) {
396140334f8SStefano Ceccherini 		return status;
397140334f8SStefano Ceccherini 	}
3982c3505deSJohn Scipione 
399420fe80eSStefano Ceccherini 	return B_OK;
400420fe80eSStefano Ceccherini }
401420fe80eSStefano Ceccherini 
402420fe80eSStefano Ceccherini 
403420fe80eSStefano Ceccherini status_t
4042c3505deSJohn Scipione PictureDataWriter::WriteDrawPolygon(const int32& numPoints, BPoint* points,
4052c3505deSJohn Scipione 	const bool& isClosed, const bool& fill)
406ed5de868SStefano Ceccherini {
407ed5de868SStefano Ceccherini 	try {
408ed5de868SStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_POLYGON : B_PIC_STROKE_POLYGON);
409ed5de868SStefano Ceccherini 		Write<int32>(numPoints);
410ed5de868SStefano Ceccherini 		for (int32 i = 0; i < numPoints; i++)
411ed5de868SStefano Ceccherini 			Write<BPoint>(points[i]);
4122c3505deSJohn Scipione 
413ed5de868SStefano Ceccherini 		if (!fill)
414ed5de868SStefano Ceccherini 			Write<uint8>((uint8)isClosed);
4152c3505deSJohn Scipione 
416ed5de868SStefano Ceccherini 		EndOp();
417ed5de868SStefano Ceccherini 	} catch (status_t& status) {
418ed5de868SStefano Ceccherini 		return status;
419ed5de868SStefano Ceccherini 	}
4202c3505deSJohn Scipione 
421ed5de868SStefano Ceccherini 	return B_OK;
422ed5de868SStefano Ceccherini }
423ed5de868SStefano Ceccherini 
424ed5de868SStefano Ceccherini 
425ed5de868SStefano Ceccherini status_t
4262c3505deSJohn Scipione PictureDataWriter::WriteDrawBezier(const BPoint points[4], const bool& fill)
427ed5de868SStefano Ceccherini {
428ed5de868SStefano Ceccherini 	try {
429ed5de868SStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_BEZIER : B_PIC_STROKE_BEZIER);
430ed5de868SStefano Ceccherini 		for (int32 i = 0; i < 4; i++)
431ed5de868SStefano Ceccherini 			Write<BPoint>(points[i]);
4322c3505deSJohn Scipione 
433ed5de868SStefano Ceccherini 		EndOp();
434ed5de868SStefano Ceccherini 	} catch (status_t& status) {
435ed5de868SStefano Ceccherini 		return status;
436ed5de868SStefano Ceccherini 	}
4372c3505deSJohn Scipione 
438ed5de868SStefano Ceccherini 	return B_OK;
439ed5de868SStefano Ceccherini }
440ed5de868SStefano Ceccherini 
441ed5de868SStefano Ceccherini 
442ed5de868SStefano Ceccherini status_t
4432c3505deSJohn Scipione PictureDataWriter::WriteStrokeLine(const BPoint& start, const BPoint& end)
444420fe80eSStefano Ceccherini {
445140334f8SStefano Ceccherini 	try {
446420fe80eSStefano Ceccherini 		BeginOp(B_PIC_STROKE_LINE);
447420fe80eSStefano Ceccherini 		Write<BPoint>(start);
448420fe80eSStefano Ceccherini 		Write<BPoint>(end);
449420fe80eSStefano Ceccherini 		EndOp();
450140334f8SStefano Ceccherini 	} catch (status_t& status) {
451140334f8SStefano Ceccherini 		return status;
452140334f8SStefano Ceccherini 	}
4532c3505deSJohn Scipione 
454420fe80eSStefano Ceccherini 	return B_OK;
455420fe80eSStefano Ceccherini }
456420fe80eSStefano Ceccherini 
457420fe80eSStefano Ceccherini 
458420fe80eSStefano Ceccherini status_t
4592c3505deSJohn Scipione PictureDataWriter::WriteDrawString(const BPoint& where, const char* string,
4602c3505deSJohn Scipione 	const int32& length, const escapement_delta& escapement)
461420fe80eSStefano Ceccherini {
462140334f8SStefano Ceccherini 	try {
463420fe80eSStefano Ceccherini 		BeginOp(B_PIC_SET_PEN_LOCATION);
464420fe80eSStefano Ceccherini 		Write<BPoint>(where);
465420fe80eSStefano Ceccherini 		EndOp();
466420fe80eSStefano Ceccherini 
467420fe80eSStefano Ceccherini 		BeginOp(B_PIC_DRAW_STRING);
468420fe80eSStefano Ceccherini 		Write<float>(escapement.space);
469420fe80eSStefano Ceccherini 		Write<float>(escapement.nonspace);
4708752f52dSStefano Ceccherini 		//WriteData(string, length + 1);
4718752f52dSStefano Ceccherini 			// TODO: is string 0 terminated? why is length given?
4720b0ecfabSMarcus Overhagen 		WriteData(string, length);
4730b0ecfabSMarcus Overhagen 		Write<uint8>(0);
474420fe80eSStefano Ceccherini 		EndOp();
475140334f8SStefano Ceccherini 	} catch (status_t& status) {
476140334f8SStefano Ceccherini 		return status;
477140334f8SStefano Ceccherini 	}
478420fe80eSStefano Ceccherini 
479420fe80eSStefano Ceccherini 	return B_OK;
480420fe80eSStefano Ceccherini }
481420fe80eSStefano Ceccherini 
482420fe80eSStefano Ceccherini 
483420fe80eSStefano Ceccherini status_t
484954a0a0cSStephan Aßmus PictureDataWriter::WriteDrawString(const char* string,
485954a0a0cSStephan Aßmus 	int32 length, const BPoint* locations, int32 locationCount)
486954a0a0cSStephan Aßmus {
487954a0a0cSStephan Aßmus 	try {
488954a0a0cSStephan Aßmus 		BeginOp(B_PIC_DRAW_STRING_LOCATIONS);
489954a0a0cSStephan Aßmus 		Write<int32>(locationCount);
490954a0a0cSStephan Aßmus 		for (int32 i = 0; i < locationCount; i++) {
491954a0a0cSStephan Aßmus 			Write<BPoint>(locations[i]);
492954a0a0cSStephan Aßmus 		}
493954a0a0cSStephan Aßmus 		WriteData(string, length);
494954a0a0cSStephan Aßmus 		Write<uint8>(0);
495954a0a0cSStephan Aßmus 		EndOp();
496954a0a0cSStephan Aßmus 	} catch (status_t& status) {
497954a0a0cSStephan Aßmus 		return status;
498954a0a0cSStephan Aßmus 	}
499954a0a0cSStephan Aßmus 
500954a0a0cSStephan Aßmus 	return B_OK;
501954a0a0cSStephan Aßmus }
502954a0a0cSStephan Aßmus 
503954a0a0cSStephan Aßmus 
504954a0a0cSStephan Aßmus status_t
5052c3505deSJohn Scipione PictureDataWriter::WriteDrawShape(const int32& opCount, const void* opList,
5062c3505deSJohn Scipione 	const int32& ptCount, const void* ptList, const bool& fill)
507420fe80eSStefano Ceccherini {
508140334f8SStefano Ceccherini 	try {
509420fe80eSStefano Ceccherini 		BeginOp(fill ? B_PIC_FILL_SHAPE : B_PIC_STROKE_SHAPE);
510420fe80eSStefano Ceccherini 		Write<int32>(opCount);
511420fe80eSStefano Ceccherini 		Write<int32>(ptCount);
5120b0ecfabSMarcus Overhagen 		WriteData(opList, opCount * sizeof(uint32));
513420fe80eSStefano Ceccherini 		WriteData(ptList, ptCount * sizeof(BPoint));
514420fe80eSStefano Ceccherini 		EndOp();
515140334f8SStefano Ceccherini 	} catch (status_t& status) {
516140334f8SStefano Ceccherini 		return status;
517140334f8SStefano Ceccherini 	}
518420fe80eSStefano Ceccherini 
519420fe80eSStefano Ceccherini 	return B_OK;
520420fe80eSStefano Ceccherini }
521420fe80eSStefano Ceccherini 
522420fe80eSStefano Ceccherini 
523420fe80eSStefano Ceccherini status_t
524*90ab1a44SX512 PictureDataWriter::WriteDrawRectGradient(const BRect& rect, const BGradient& gradient, const bool& fill)
525*90ab1a44SX512 {
526*90ab1a44SX512 	try {
527*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_RECT_GRADIENT : B_PIC_STROKE_RECT_GRADIENT);
528*90ab1a44SX512 		Write<BRect>(rect);
529*90ab1a44SX512 		gradient.Flatten(fData);
530*90ab1a44SX512 		EndOp();
531*90ab1a44SX512 	} catch (status_t& status) {
532*90ab1a44SX512 		return status;
533*90ab1a44SX512 	}
534*90ab1a44SX512 
535*90ab1a44SX512 	return B_OK;
536*90ab1a44SX512 }
537*90ab1a44SX512 
538*90ab1a44SX512 
539*90ab1a44SX512 status_t
540*90ab1a44SX512 PictureDataWriter::WriteDrawRoundRectGradient(const BRect& rect, const BPoint& radius, const BGradient& gradient,
541*90ab1a44SX512 	const bool& fill)
542*90ab1a44SX512 {
543*90ab1a44SX512 	try {
544*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ROUND_RECT_GRADIENT : B_PIC_STROKE_ROUND_RECT_GRADIENT);
545*90ab1a44SX512 		Write<BRect>(rect);
546*90ab1a44SX512 		Write<BPoint>(radius);
547*90ab1a44SX512 		gradient.Flatten(fData);
548*90ab1a44SX512 		EndOp();
549*90ab1a44SX512 	} catch (status_t& status) {
550*90ab1a44SX512 		return status;
551*90ab1a44SX512 	}
552*90ab1a44SX512 
553*90ab1a44SX512 	return B_OK;
554*90ab1a44SX512 }
555*90ab1a44SX512 
556*90ab1a44SX512 
557*90ab1a44SX512 status_t
558*90ab1a44SX512 PictureDataWriter::WriteDrawBezierGradient(const BPoint points[4], const BGradient& gradient, const bool& fill)
559*90ab1a44SX512 {
560*90ab1a44SX512 	try {
561*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_BEZIER_GRADIENT : B_PIC_STROKE_BEZIER_GRADIENT);
562*90ab1a44SX512 		for (int32 i = 0; i < 4; i++)
563*90ab1a44SX512 			Write<BPoint>(points[i]);
564*90ab1a44SX512 
565*90ab1a44SX512 		gradient.Flatten(fData);
566*90ab1a44SX512 		EndOp();
567*90ab1a44SX512 	} catch (status_t& status) {
568*90ab1a44SX512 		return status;
569*90ab1a44SX512 	}
570*90ab1a44SX512 
571*90ab1a44SX512 	return B_OK;
572*90ab1a44SX512 }
573*90ab1a44SX512 
574*90ab1a44SX512 
575*90ab1a44SX512 status_t
576*90ab1a44SX512 PictureDataWriter::WriteDrawArcGradient(const BPoint& center, const BPoint& radius,
577*90ab1a44SX512 	const float& startTheta, const float& arcTheta, const BGradient& gradient, const bool& fill)
578*90ab1a44SX512 {
579*90ab1a44SX512 	try {
580*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ARC_GRADIENT : B_PIC_STROKE_ARC_GRADIENT);
581*90ab1a44SX512 		Write<BPoint>(center);
582*90ab1a44SX512 		Write<BPoint>(radius);
583*90ab1a44SX512 		Write<float>(startTheta);
584*90ab1a44SX512 		Write<float>(arcTheta);
585*90ab1a44SX512 		gradient.Flatten(fData);
586*90ab1a44SX512 		EndOp();
587*90ab1a44SX512 	} catch (status_t& status) {
588*90ab1a44SX512 		return status;
589*90ab1a44SX512 	}
590*90ab1a44SX512 
591*90ab1a44SX512 	return B_OK;
592*90ab1a44SX512 }
593*90ab1a44SX512 
594*90ab1a44SX512 
595*90ab1a44SX512 status_t
596*90ab1a44SX512 PictureDataWriter::WriteDrawEllipseGradient(const BRect& rect, const BGradient& gradient, const bool& fill)
597*90ab1a44SX512 {
598*90ab1a44SX512 	try {
599*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_ELLIPSE_GRADIENT : B_PIC_STROKE_ELLIPSE_GRADIENT);
600*90ab1a44SX512 		Write<BRect>(rect);
601*90ab1a44SX512 		gradient.Flatten(fData);
602*90ab1a44SX512 		EndOp();
603*90ab1a44SX512 	} catch (status_t& status) {
604*90ab1a44SX512 		return status;
605*90ab1a44SX512 	}
606*90ab1a44SX512 
607*90ab1a44SX512 	return B_OK;
608*90ab1a44SX512 }
609*90ab1a44SX512 
610*90ab1a44SX512 
611*90ab1a44SX512 status_t
612*90ab1a44SX512 PictureDataWriter::WriteDrawPolygonGradient(const int32& numPoints, BPoint* points,
613*90ab1a44SX512 	const bool& isClosed, const BGradient& gradient, const bool& fill)
614*90ab1a44SX512 {
615*90ab1a44SX512 	try {
616*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_POLYGON_GRADIENT : B_PIC_STROKE_POLYGON_GRADIENT);
617*90ab1a44SX512 		Write<int32>(numPoints);
618*90ab1a44SX512 		for (int32 i = 0; i < numPoints; i++)
619*90ab1a44SX512 			Write<BPoint>(points[i]);
620*90ab1a44SX512 
621*90ab1a44SX512 		if (!fill)
622*90ab1a44SX512 			Write<uint8>((uint8)isClosed);
623*90ab1a44SX512 
624*90ab1a44SX512 		gradient.Flatten(fData);
625*90ab1a44SX512 		EndOp();
626*90ab1a44SX512 	} catch (status_t& status) {
627*90ab1a44SX512 		return status;
628*90ab1a44SX512 	}
629*90ab1a44SX512 
630*90ab1a44SX512 	return B_OK;
631*90ab1a44SX512 }
632*90ab1a44SX512 
633*90ab1a44SX512 
634*90ab1a44SX512 status_t
635*90ab1a44SX512 PictureDataWriter::WriteDrawShapeGradient(const int32& opCount, const void* opList,
636*90ab1a44SX512 	const int32& ptCount, const void* ptList, const BGradient& gradient, const bool& fill)
637*90ab1a44SX512 {
638*90ab1a44SX512 	try {
639*90ab1a44SX512 		BeginOp(fill ? B_PIC_FILL_SHAPE_GRADIENT : B_PIC_STROKE_SHAPE_GRADIENT);
640*90ab1a44SX512 		Write<int32>(opCount);
641*90ab1a44SX512 		Write<int32>(ptCount);
642*90ab1a44SX512 		WriteData(opList, opCount * sizeof(uint32));
643*90ab1a44SX512 		WriteData(ptList, ptCount * sizeof(BPoint));
644*90ab1a44SX512 		gradient.Flatten(fData);
645*90ab1a44SX512 		EndOp();
646*90ab1a44SX512 	} catch (status_t& status) {
647*90ab1a44SX512 		return status;
648*90ab1a44SX512 	}
649*90ab1a44SX512 
650*90ab1a44SX512 	return B_OK;
651*90ab1a44SX512 }
652*90ab1a44SX512 
653*90ab1a44SX512 
654*90ab1a44SX512 status_t
6552c3505deSJohn Scipione PictureDataWriter::WriteDrawBitmap(const BRect& srcRect, const BRect& dstRect,
6562c3505deSJohn Scipione 	const int32& width, const int32& height, const int32& bytesPerRow,
6572c3505deSJohn Scipione 	const int32& colorSpace, const int32& flags, const void* data,
6582c3505deSJohn Scipione 	const int32& length)
659420fe80eSStefano Ceccherini {
6600b0ecfabSMarcus Overhagen 	if (length != height * bytesPerRow)
6610b0ecfabSMarcus Overhagen 		debugger("PictureDataWriter::WriteDrawBitmap: invalid length");
662140334f8SStefano Ceccherini 	try {
663420fe80eSStefano Ceccherini 		BeginOp(B_PIC_DRAW_PIXELS);
664420fe80eSStefano Ceccherini 		Write<BRect>(srcRect);
665420fe80eSStefano Ceccherini 		Write<BRect>(dstRect);
666420fe80eSStefano Ceccherini 		Write<int32>(width);
667420fe80eSStefano Ceccherini 		Write<int32>(height);
668420fe80eSStefano Ceccherini 		Write<int32>(bytesPerRow);
669420fe80eSStefano Ceccherini 		Write<int32>(colorSpace);
670420fe80eSStefano Ceccherini 		Write<int32>(flags);
671420fe80eSStefano Ceccherini 		WriteData(data, length);
672420fe80eSStefano Ceccherini 		EndOp();
673140334f8SStefano Ceccherini 	} catch (status_t& status) {
674140334f8SStefano Ceccherini 		return status;
675140334f8SStefano Ceccherini 	}
6762c3505deSJohn Scipione 
677420fe80eSStefano Ceccherini 	return B_OK;
678420fe80eSStefano Ceccherini }
679420fe80eSStefano Ceccherini 
680420fe80eSStefano Ceccherini 
681420fe80eSStefano Ceccherini status_t
6822c3505deSJohn Scipione PictureDataWriter::WriteDrawPicture(const BPoint& where, const int32& token)
683ed5de868SStefano Ceccherini {
6848752f52dSStefano Ceccherini 	// TODO: I'm not sure about this function. I think we need
6858752f52dSStefano Ceccherini 	// to attach the picture data too.
6868752f52dSStefano Ceccherini 	// The token won't be sufficient in many cases (for example, when
6878752f52dSStefano Ceccherini 	// we archive/flatten the picture.
688ed5de868SStefano Ceccherini 	try {
689ed5de868SStefano Ceccherini 		BeginOp(B_PIC_DRAW_PICTURE);
690ed5de868SStefano Ceccherini 		Write<BPoint>(where);
691ed5de868SStefano Ceccherini 		Write<int32>(token);
692ed5de868SStefano Ceccherini 		EndOp();
693ed5de868SStefano Ceccherini 	} catch (status_t& status) {
694ed5de868SStefano Ceccherini 		return status;
695ed5de868SStefano Ceccherini 	}
6962c3505deSJohn Scipione 
697ed5de868SStefano Ceccherini 	return B_OK;
698ed5de868SStefano Ceccherini }
699ed5de868SStefano Ceccherini 
700ed5de868SStefano Ceccherini 
701ed5de868SStefano Ceccherini status_t
702feb557a6SStefano Ceccherini PictureDataWriter::WriteSetFontFamily(const font_family family)
703a3d758e3SStefano Ceccherini {
704feb557a6SStefano Ceccherini 	try {
705feb557a6SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FAMILY);
706feb557a6SStefano Ceccherini 		WriteData(family, strlen(family));
707796e4e45SStefano Ceccherini 		Write<uint8>(0);
708feb557a6SStefano Ceccherini 		EndOp();
709feb557a6SStefano Ceccherini 	} catch (status_t& status) {
710feb557a6SStefano Ceccherini 		return status;
711feb557a6SStefano Ceccherini 	}
7122c3505deSJohn Scipione 
713a3d758e3SStefano Ceccherini 	return B_OK;
714a3d758e3SStefano Ceccherini }
715a3d758e3SStefano Ceccherini 
716a3d758e3SStefano Ceccherini 
717a3d758e3SStefano Ceccherini status_t
718feb557a6SStefano Ceccherini PictureDataWriter::WriteSetFontStyle(const font_style style)
719a3d758e3SStefano Ceccherini {
720feb557a6SStefano Ceccherini 	try {
721feb557a6SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_STYLE);
722feb557a6SStefano Ceccherini 		WriteData(style, strlen(style));
723796e4e45SStefano Ceccherini 		Write<uint8>(0);
724feb557a6SStefano Ceccherini 		EndOp();
725feb557a6SStefano Ceccherini 	} catch (status_t& status) {
726feb557a6SStefano Ceccherini 		return status;
727feb557a6SStefano Ceccherini 	}
7282c3505deSJohn Scipione 
729a3d758e3SStefano Ceccherini 	return B_OK;
730a3d758e3SStefano Ceccherini }
731a3d758e3SStefano Ceccherini 
732a3d758e3SStefano Ceccherini 
733a3d758e3SStefano Ceccherini status_t
734a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontSpacing(const int32& spacing)
735a3d758e3SStefano Ceccherini {
736140334f8SStefano Ceccherini 	try {
737a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SPACING);
738a3d758e3SStefano Ceccherini 		Write<int32>(spacing);
739a3d758e3SStefano Ceccherini 		EndOp();
740140334f8SStefano Ceccherini 	} catch (status_t& status) {
741140334f8SStefano Ceccherini 		return status;
742140334f8SStefano Ceccherini 	}
7432c3505deSJohn Scipione 
744a3d758e3SStefano Ceccherini 	return B_OK;
745a3d758e3SStefano Ceccherini }
746a3d758e3SStefano Ceccherini 
747a3d758e3SStefano Ceccherini 
748a3d758e3SStefano Ceccherini status_t
749a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontSize(const float& size)
750a3d758e3SStefano Ceccherini {
751140334f8SStefano Ceccherini 	try {
752a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SIZE);
753a3d758e3SStefano Ceccherini 		Write<float>(size);
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
764a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontRotation(const float& rotation)
765a3d758e3SStefano Ceccherini {
766140334f8SStefano Ceccherini 	try {
767a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_ROTATE);
768a3d758e3SStefano Ceccherini 		Write<float>(rotation);
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
779a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontEncoding(const int32& encoding)
780a3d758e3SStefano Ceccherini {
781140334f8SStefano Ceccherini 	try {
782a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_ENCODING);
783a3d758e3SStefano Ceccherini 		Write<int32>(encoding);
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
794a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontFlags(const int32& flags)
795a3d758e3SStefano Ceccherini {
796140334f8SStefano Ceccherini 	try {
797a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FLAGS);
798a3d758e3SStefano Ceccherini 		Write<int32>(flags);
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
809b1af0a7aSStefano Ceccherini PictureDataWriter::WriteSetFontShear(const float& shear)
810a3d758e3SStefano Ceccherini {
811140334f8SStefano Ceccherini 	try {
812a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_SHEAR);
813b1af0a7aSStefano Ceccherini 		Write<float>(shear);
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
824a3d758e3SStefano Ceccherini PictureDataWriter::WriteSetFontFace(const int32& face)
825a3d758e3SStefano Ceccherini {
826140334f8SStefano Ceccherini 	try {
827a3d758e3SStefano Ceccherini 		BeginOp(B_PIC_SET_FONT_FACE);
828a3d758e3SStefano Ceccherini 		Write<int32>(face);
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
839420fe80eSStefano Ceccherini PictureDataWriter::WritePushState()
840420fe80eSStefano Ceccherini {
841140334f8SStefano Ceccherini 	try {
842420fe80eSStefano Ceccherini 		BeginOp(B_PIC_PUSH_STATE);
843420fe80eSStefano Ceccherini 		EndOp();
844140334f8SStefano Ceccherini 	} catch (status_t& status) {
845140334f8SStefano Ceccherini 		return status;
846140334f8SStefano Ceccherini 	}
8472c3505deSJohn Scipione 
848420fe80eSStefano Ceccherini 	return B_OK;
849420fe80eSStefano Ceccherini }
850420fe80eSStefano Ceccherini 
851420fe80eSStefano Ceccherini 
852420fe80eSStefano Ceccherini status_t
853420fe80eSStefano Ceccherini PictureDataWriter::WritePopState()
854420fe80eSStefano Ceccherini {
855140334f8SStefano Ceccherini 	try {
856420fe80eSStefano Ceccherini 		BeginOp(B_PIC_POP_STATE);
857420fe80eSStefano Ceccherini 		EndOp();
858140334f8SStefano Ceccherini 	} catch (status_t& status) {
859140334f8SStefano Ceccherini 		return status;
860140334f8SStefano Ceccherini 	}
8612c3505deSJohn Scipione 
862420fe80eSStefano Ceccherini 	return B_OK;
863420fe80eSStefano Ceccherini }
864420fe80eSStefano Ceccherini 
865420fe80eSStefano Ceccherini 
866551438b9SJulian Harnath status_t
867551438b9SJulian Harnath PictureDataWriter::WriteBlendLayer(Layer* layer)
868551438b9SJulian Harnath {
869551438b9SJulian Harnath 	try {
870551438b9SJulian Harnath 		BeginOp(B_PIC_BLEND_LAYER);
871551438b9SJulian Harnath 		Write<Layer*>(layer);
872551438b9SJulian Harnath 		EndOp();
873551438b9SJulian Harnath 	} catch (status_t& status) {
874551438b9SJulian Harnath 		return status;
875551438b9SJulian Harnath 	}
876551438b9SJulian Harnath 
877551438b9SJulian Harnath 	return B_OK;
878551438b9SJulian Harnath }
879551438b9SJulian Harnath 
880551438b9SJulian Harnath 
8814bd6f322SJulian Harnath status_t
8824bd6f322SJulian Harnath PictureDataWriter::WriteClipToRect(const BRect& rect, bool inverse)
8834bd6f322SJulian Harnath {
8844bd6f322SJulian Harnath 	try {
8854bd6f322SJulian Harnath 		BeginOp(B_PIC_CLIP_TO_RECT);
8864bd6f322SJulian Harnath 		Write<bool>(inverse);
8874bd6f322SJulian Harnath 		Write<BRect>(rect);
8884bd6f322SJulian Harnath 		EndOp();
8894bd6f322SJulian Harnath 	} catch (status_t& status) {
8904bd6f322SJulian Harnath 		return status;
8914bd6f322SJulian Harnath 	}
8924bd6f322SJulian Harnath 
8934bd6f322SJulian Harnath 	return B_OK;
8944bd6f322SJulian Harnath }
8954bd6f322SJulian Harnath 
8964bd6f322SJulian Harnath 
8974bd6f322SJulian Harnath status_t
8984bd6f322SJulian Harnath PictureDataWriter::WriteClipToShape(int32 opCount, const void* opList,
8994bd6f322SJulian Harnath 	int32 ptCount, const void* ptList, bool inverse)
9004bd6f322SJulian Harnath {
9014bd6f322SJulian Harnath 	try {
9024bd6f322SJulian Harnath 		BeginOp(B_PIC_CLIP_TO_SHAPE);
9034bd6f322SJulian Harnath 		Write<bool>(inverse);
9044bd6f322SJulian Harnath 		Write<int32>(opCount);
9054bd6f322SJulian Harnath 		Write<int32>(ptCount);
9064bd6f322SJulian Harnath 		WriteData(opList, opCount * sizeof(uint32));
9074bd6f322SJulian Harnath 		WriteData(ptList, ptCount * sizeof(BPoint));
9084bd6f322SJulian Harnath 		EndOp();
9094bd6f322SJulian Harnath 	} catch (status_t& status) {
9104bd6f322SJulian Harnath 		return status;
9114bd6f322SJulian Harnath 	}
9124bd6f322SJulian Harnath 
9134bd6f322SJulian Harnath 	return B_OK;
9144bd6f322SJulian Harnath }
9154bd6f322SJulian Harnath 
9164bd6f322SJulian Harnath 
917420fe80eSStefano Ceccherini // private
918140334f8SStefano Ceccherini void
919420fe80eSStefano Ceccherini PictureDataWriter::BeginOp(const int16& op)
920420fe80eSStefano Ceccherini {
921140334f8SStefano Ceccherini 	if (fData == NULL)
92284c2abeeSStefano Ceccherini 		THROW_ERROR(B_NO_INIT);
923140334f8SStefano Ceccherini 
924420fe80eSStefano Ceccherini 	fStack.push(fData->Position());
925420fe80eSStefano Ceccherini 	fData->Write(&op, sizeof(op));
926420fe80eSStefano Ceccherini 
927420fe80eSStefano Ceccherini 	// Init the size of the opcode block to 0
928efffcb29SAlex Smith 	int32 size = 0;
929420fe80eSStefano Ceccherini 	fData->Write(&size, sizeof(size));
930420fe80eSStefano Ceccherini }
931420fe80eSStefano Ceccherini 
932420fe80eSStefano Ceccherini 
933140334f8SStefano Ceccherini void
934420fe80eSStefano Ceccherini PictureDataWriter::EndOp()
935420fe80eSStefano Ceccherini {
936140334f8SStefano Ceccherini 	if (fData == NULL)
93784c2abeeSStefano Ceccherini 		THROW_ERROR(B_NO_INIT);
938140334f8SStefano Ceccherini 
939420fe80eSStefano Ceccherini 	off_t curPos = fData->Position();
940420fe80eSStefano Ceccherini 	off_t stackPos = fStack.top();
941420fe80eSStefano Ceccherini 	fStack.pop();
942420fe80eSStefano Ceccherini 
943420fe80eSStefano Ceccherini 	// The size of the op is calculated like this:
944420fe80eSStefano Ceccherini 	// current position on the stream minus the position on the stack,
945420fe80eSStefano Ceccherini 	// minus the space occupied by the op code itself (int16)
946efffcb29SAlex Smith 	// and the space occupied by the size field (int32)
947efffcb29SAlex Smith 	int32 size = curPos - stackPos - sizeof(int32) - sizeof(int16);
948420fe80eSStefano Ceccherini 
9498752f52dSStefano Ceccherini 	// Size was set to 0 in BeginOp()
9508752f52dSStefano Ceccherini 	// Now we overwrite it with the correct value
951420fe80eSStefano Ceccherini 	fData->Seek(stackPos + sizeof(int16), SEEK_SET);
952420fe80eSStefano Ceccherini 	fData->Write(&size, sizeof(size));
953420fe80eSStefano Ceccherini 	fData->Seek(curPos, SEEK_SET);
954420fe80eSStefano Ceccherini }
955420fe80eSStefano Ceccherini 
956420fe80eSStefano Ceccherini 
957140334f8SStefano Ceccherini void
958420fe80eSStefano Ceccherini PictureDataWriter::WriteData(const void* data, size_t size)
959420fe80eSStefano Ceccherini {
960140334f8SStefano Ceccherini 	ssize_t result = fData->Write(data, size);
961140334f8SStefano Ceccherini 	if (result < 0)
96284c2abeeSStefano Ceccherini 		THROW_ERROR(result);
9632c3505deSJohn Scipione 
964ed5de868SStefano Ceccherini 	if ((size_t)result != size)
96584c2abeeSStefano Ceccherini 		THROW_ERROR(B_IO_ERROR);
966420fe80eSStefano Ceccherini }
967