HardCopyFP.C 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. // $TOG: HardCopyFP.C /main/4 1998/04/17 11:46:58 mgreess $
  24. #include "HardCopyFP.h"
  25. char* loutFeatureProcessor::empty_string()
  26. {
  27. char* x = new char[1]; x[0] = 0;
  28. return x;
  29. }
  30. char*
  31. loutFeatureProcessor::prepend(const char* header, const char* body)
  32. {
  33. int hlen = strlen(header);
  34. int blen = strlen(body);
  35. char* x = new char[hlen + blen + 1];
  36. *((char *) memcpy (x, header, hlen) + hlen) = '\0';
  37. *((char *) memcpy (x + hlen, body, blen) + blen) = '\0';
  38. return x;
  39. }
  40. void
  41. loutFeatureProcessor::handleData(const char *data, unsigned int size, ostream& out)
  42. {
  43. for (unsigned int i=0; i<size; i++ )
  44. out << data[i];
  45. }
  46. char* literalBuffer = new char[1024];
  47. int literalBufferSize = 1024;
  48. const char* loutFeatureProcessor::convertToLiteral(const char* str)
  49. {
  50. int size = strlen(str);
  51. for (int i=0; i<size; i++ )
  52. if ( str[i] == '\\' || str[i] == '"' ) {
  53. if ( literalBufferSize < 2*size + 3 ) {
  54. literalBufferSize = 2*size + 3;
  55. literalBufferSize *= 2;
  56. delete [] literalBuffer;
  57. literalBuffer = new char[literalBufferSize];
  58. }
  59. int j = 0;
  60. literalBuffer[j++] = '"';
  61. for (int i=0; i<size; i++ ) {
  62. if ( str[i] == '\\' || str[i] == '"' ) {
  63. literalBuffer[j++] = '\\';
  64. }
  65. literalBuffer[j++] = str[i];
  66. }
  67. literalBuffer[j++] = '"';
  68. literalBuffer[j] = 0;
  69. return literalBuffer;
  70. }
  71. return str;
  72. }
  73. const char* loutFeatureProcessor::stringToCharPtr(const FeatureValue* f)
  74. {
  75. if ( f ) {
  76. if ( f -> type() == FeatureValue::string ) {
  77. const char* x = *f;
  78. return x;
  79. } else
  80. throw(CASTHCREXCEPT hardCopyRendererException());
  81. }
  82. return 0;
  83. }
  84. unsigned int
  85. loutFeatureProcessor::dimensionToFloat(
  86. float& y,
  87. FeatureValueDimension::Unit& unitOfY,
  88. const FeatureValue* f,
  89. FeatureValueDimension::Unit u
  90. )
  91. {
  92. if ( f ) {
  93. if ( f -> type() == FeatureValue::dimension ) {
  94. FeatureValueDimension* x = (FeatureValueDimension*)f;
  95. unitOfY = x -> unit();
  96. if ( u == FeatureValueDimension::NONE ) {
  97. if ( x -> unit() == FeatureValueDimension::PICA ) {
  98. y = x -> getValue(FeatureValueDimension::POINT);
  99. unitOfY = FeatureValueDimension::POINT;
  100. } else
  101. y = *x;
  102. } else {
  103. y = x -> getValue(u);
  104. }
  105. return 1;
  106. } else
  107. throw(CASTHCREXCEPT hardCopyRendererException());
  108. } else {
  109. y = 0;
  110. return 0;
  111. }
  112. }
  113. const char*
  114. loutFeatureProcessor::dimensionToCharPtr(const FeatureValue* f,
  115. FeatureValueDimension::Unit u)
  116. {
  117. static char dBuf[50];
  118. float y;
  119. FeatureValueDimension::Unit unitOfY;
  120. if ( dimensionToFloat(y, unitOfY, f, u) ) {
  121. if ( u != FeatureValueDimension::NONE ) {
  122. unitOfY = u;
  123. }
  124. switch ( unitOfY ) {
  125. case FeatureValueDimension::INCH:
  126. snprintf(dBuf, sizeof(dBuf), "%.2fi", y);
  127. break;
  128. case FeatureValueDimension::PICA:
  129. throw(CASTHCREXCEPT hardCopyRendererException());
  130. break;
  131. case FeatureValueDimension::POINT:
  132. snprintf(dBuf, sizeof(dBuf), "%.2fp", y);
  133. break;
  134. case FeatureValueDimension::CM:
  135. snprintf(dBuf, sizeof(dBuf), "%.2fc", y);
  136. break;
  137. case FeatureValueDimension::PIXEL:
  138. throw(CASTHCREXCEPT hardCopyRendererException());
  139. break;
  140. default:
  141. throw(CASTHCREXCEPT hardCopyRendererException());
  142. }
  143. return (const char*)dBuf;
  144. } else
  145. return 0;
  146. }
  147. const char*
  148. featureProcessor::getFeatureValue(const Feature* f)
  149. {
  150. if ( f )
  151. return stringToCharPtr(f -> value());
  152. else
  153. return 0;
  154. }
  155. FeatureValueArray*
  156. featureProcessor::getFeatureValueArray(const Feature* f)
  157. {
  158. if ( f ) {
  159. if ( f -> value() -> type() == FeatureValue::array )
  160. return (FeatureValueArray*)(f -> value());
  161. else {
  162. MESSAGE(cerr, form("%s is not of an array type.", f -> name().name()));
  163. throw(CASTHCREXCEPT hardCopyRendererException());
  164. }
  165. } else
  166. return 0;
  167. }
  168. unsigned int
  169. featureProcessor::getDimensionValue(float& x, const Feature* f,
  170. FeatureValueDimension::Unit u)
  171. {
  172. FeatureValueDimension::Unit unit;
  173. if ( f )
  174. return dimensionToFloat(x, unit, f -> value(), u);
  175. else
  176. return 0;
  177. }
  178. const char*
  179. featureProcessor::getDimensionValue(const Feature* f,
  180. FeatureValueDimension::Unit u)
  181. {
  182. if ( f )
  183. return dimensionToCharPtr(f -> value(), u);
  184. else
  185. return 0;
  186. }
  187. unsigned int
  188. featureProcessor::getFeatureValue(int& x, const Feature* f)
  189. {
  190. if ( f ) {
  191. x = *(f -> value());
  192. return true;
  193. } else
  194. return false;
  195. }
  196. unsigned int
  197. featureProcessor::getFeatureValue(float& x, const Feature* f)
  198. {
  199. if ( f ) {
  200. x = *(f -> value());
  201. return true;
  202. } else
  203. return false;
  204. }
  205. const FeatureSet *
  206. featureProcessor::getFeatureSetValue(const Feature* f)
  207. {
  208. if ( f && f -> value() -> type() == FeatureValue::featureset )
  209. return ((const FeatureValueFeatureSet *)f->value())->value();
  210. else
  211. return 0;
  212. }
  213. FeatureValue*
  214. loutFeatureProcessor::evaluate(const char*)
  215. {
  216. MESSAGE(cerr, "featureProcessor::evaluate(): FP specific function should be called.");
  217. throw(CASTHCREXCEPT hardCopyRendererException());
  218. return 0;
  219. }
  220. unsigned int
  221. loutFeatureProcessor::accept(const char*, const Expression*)
  222. {
  223. MESSAGE(cerr, "featureProcessor::accept(): FP specific function should be called");
  224. throw(CASTHCREXCEPT hardCopyRendererException());
  225. return false;
  226. }
  227. void loutFeatureProcessor::clear()
  228. {
  229. MESSAGE(cerr, "featureProcessor::clear(): FP specific function should be called");
  230. throw(CASTHCREXCEPT hardCopyRendererException());
  231. }
  232. void
  233. loutFeatureProcessor::preEvaluate(const Element& e)
  234. {
  235. MESSAGE(cerr, "featureProcessor::preEvaluate(): FP specific function should be called");
  236. throw(CASTHCREXCEPT hardCopyRendererException());
  237. }
  238. void
  239. loutFeatureProcessor::postEvaluate(const Element& e)
  240. {
  241. MESSAGE(cerr, "featureProcessor::postEvaluate(): FP specific function should be called");
  242. throw(CASTHCREXCEPT hardCopyRendererException());
  243. }