1 /*
2    Source File : Type2CharStringWriter.cpp
3 
4 
5    Copyright 2011 Gal Kahana PDFWriter
6 
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10 
11        http://www.apache.org/licenses/LICENSE-2.0
12 
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 
19 
20 */
21 #include "Type2CharStringWriter.h"
22 #include "IByteWriter.h"
23 
24 using namespace PDFHummus;
25 
Type2CharStringWriter(IByteWriter * inTargetStream)26 Type2CharStringWriter::Type2CharStringWriter(IByteWriter* inTargetStream)
27 {
28 	mTargetStream = inTargetStream;
29 }
30 
~Type2CharStringWriter(void)31 Type2CharStringWriter::~Type2CharStringWriter(void)
32 {
33 }
34 
Assign(IByteWriter * inTargetStream)35 void Type2CharStringWriter::Assign(IByteWriter* inTargetStream)
36 {
37 	mTargetStream = inTargetStream;
38 }
39 
WriteHintMask(unsigned long inMask,unsigned long inMaskSize)40 EStatusCode Type2CharStringWriter::WriteHintMask(unsigned long inMask,unsigned long inMaskSize)
41 {
42 	unsigned long maskByteSize = inMaskSize/8 + (inMaskSize % 8 != 0 ? 1:0);
43 
44 	EStatusCode status = WriteOperator(19);
45 	if(status != PDFHummus::eSuccess)
46 		return status;
47 
48 	return WriteMaskBytes(inMask,maskByteSize);
49 }
50 
WriteMaskBytes(unsigned long inMask,unsigned long inMaskByteSize)51 EStatusCode Type2CharStringWriter::WriteMaskBytes(unsigned long inMask,unsigned long inMaskByteSize)
52 {
53 	EStatusCode status;
54 
55 	if(inMaskByteSize > 1)
56 	{
57 		status = WriteMaskBytes(inMask >> 1,inMaskByteSize - 1);
58 		if(status != PDFHummus::eSuccess)
59 			return status;
60 	}
61 	return WriteByte((Byte)(inMask & 0xff));
62 }
63 
WriteByte(Byte inValue)64 EStatusCode Type2CharStringWriter::WriteByte(Byte inValue)
65 {
66 	return (mTargetStream->Write(&inValue,1) == 1 ? PDFHummus::eSuccess:PDFHummus::eFailure);
67 }
68 
WriteIntegerOperand(long inOperand)69 EStatusCode Type2CharStringWriter::WriteIntegerOperand(long inOperand)
70 {
71 	long value = inOperand;
72 
73 	if(-107 <= value && value <= 107)
74 	{
75 		return WriteByte((Byte)(value + 139));
76 	}
77 	else if(108 <= value && value <= 1131)
78 	{
79 		Byte byte0,byte1;
80 
81 		value-=108;
82 		byte0 = ((value >> 8) & 0xff) + 247;
83 		byte1 = value & 0xff;
84 
85 		if(WriteByte(byte0) != PDFHummus::eSuccess)
86 			return PDFHummus::eFailure;
87 
88 		if(WriteByte(byte1) != PDFHummus::eSuccess)
89 			return PDFHummus::eFailure;
90 		return PDFHummus::eSuccess;
91 	}
92 	else if(-1131 <= value && value <= -108)
93 	{
94 		Byte byte0,byte1;
95 
96 		value = -(value + 108);
97 
98 		byte0 = ((value >> 8) & 0xff) + 251;
99 		byte1 = value & 0xff;
100 
101 		if(WriteByte(byte0) != PDFHummus::eSuccess)
102 			return PDFHummus::eFailure;
103 
104 		if(WriteByte(byte1) != PDFHummus::eSuccess)
105 			return PDFHummus::eFailure;
106 		return PDFHummus::eSuccess;
107 	}
108 	else if(-32768 <= value && value<= 32767)
109 	{
110 		Byte byte1,byte2;
111 
112 		byte1 = (value >> 8) & 0xff;
113 		byte2 = value & 0xff;
114 
115 		if(WriteByte(28) != PDFHummus::eSuccess)
116 			return PDFHummus::eFailure;
117 
118 		if(WriteByte(byte1) != PDFHummus::eSuccess)
119 			return PDFHummus::eFailure;
120 
121 		if(WriteByte(byte2) != PDFHummus::eSuccess)
122 			return PDFHummus::eFailure;
123 		return PDFHummus::eSuccess;
124 	}
125 	else
126         return PDFHummus::eFailure;
127 
128 }
129 
WriteOperator(unsigned short inOperatorCode)130 EStatusCode Type2CharStringWriter::WriteOperator(unsigned short inOperatorCode)
131 {
132 	if((inOperatorCode & 0xff00) == 0x0c00)
133 	{
134 		if(WriteByte(0x0c) != PDFHummus::eSuccess)
135 			return PDFHummus::eFailure;
136 	}
137 	return WriteByte(Byte(inOperatorCode & 0x00ff));
138 }
139 
140