format.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. +----------------------------------------------------------------------+
  3. | XlsWriter Extension |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2017-2018 The Viest |
  6. +----------------------------------------------------------------------+
  7. | http://www.viest.me |
  8. +----------------------------------------------------------------------+
  9. | Author: viest <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #include "include.h"
  13. zend_class_entry *vtiful_format_ce;
  14. /* {{{ format_objects_new
  15. */
  16. static zend_object_handlers format_handlers;
  17. static zend_always_inline void *vtiful_format_object_alloc(size_t obj_size, zend_class_entry *ce) {
  18. void *obj = emalloc(obj_size);
  19. memset(obj, 0, obj_size);
  20. return obj;
  21. }
  22. PHP_VTIFUL_API zend_object *format_objects_new(zend_class_entry *ce)
  23. {
  24. format_object *format = vtiful_format_object_alloc(sizeof(format_object), ce);
  25. zend_object_std_init(&format->zo, ce);
  26. object_properties_init(&format->zo, ce);
  27. format->ptr.format = NULL;
  28. format->zo.handlers = &format_handlers;
  29. return &format->zo;
  30. }
  31. /* }}} */
  32. /* {{{ format_objects_free
  33. */
  34. static void format_objects_free(zend_object *object)
  35. {
  36. format_object *intern = php_vtiful_format_fetch_object(object);
  37. if (intern->ptr.format != NULL) {
  38. // free by workbook
  39. intern->ptr.format = NULL;
  40. }
  41. zend_object_std_dtor(&intern->zo);
  42. }
  43. /* }}} */
  44. /* {{{ ARG_INFO
  45. */
  46. ZEND_BEGIN_ARG_INFO_EX(format_construct_arginfo, 0, 0, 1)
  47. ZEND_ARG_INFO(0, handle)
  48. ZEND_END_ARG_INFO()
  49. ZEND_BEGIN_ARG_INFO_EX(format_underline_arginfo, 0, 0, 1)
  50. ZEND_ARG_INFO(0, style)
  51. ZEND_END_ARG_INFO()
  52. ZEND_BEGIN_ARG_INFO_EX(format_align_arginfo, 0, 0, 1)
  53. ZEND_ARG_INFO(0, style)
  54. ZEND_END_ARG_INFO()
  55. /* }}} */
  56. /** {{{ \Vtiful\Kernel\Format::__construct()
  57. */
  58. PHP_METHOD(vtiful_format, __construct)
  59. {
  60. zval *handle;
  61. format_object *obj;
  62. xls_resource_t *xls_res;
  63. ZEND_PARSE_PARAMETERS_START(1, 1)
  64. Z_PARAM_RESOURCE(handle)
  65. ZEND_PARSE_PARAMETERS_END();
  66. ZVAL_COPY(return_value, getThis());
  67. xls_res = zval_get_resource(handle);
  68. obj = Z_FORMAT_P(getThis());
  69. if (obj->ptr.format == NULL) {
  70. obj->ptr.format = workbook_add_format(xls_res->workbook);
  71. }
  72. }
  73. /* }}} */
  74. /** {{{ \Vtiful\Kernel\Format::bold()
  75. */
  76. PHP_METHOD(vtiful_format, bold)
  77. {
  78. ZVAL_COPY(return_value, getThis());
  79. format_object *obj = Z_FORMAT_P(getThis());
  80. format_set_bold(obj->ptr.format);
  81. }
  82. /* }}} */
  83. /** {{{ \Vtiful\Kernel\Format::italic()
  84. */
  85. PHP_METHOD(vtiful_format, italic)
  86. {
  87. ZVAL_COPY(return_value, getThis());
  88. format_object *obj = Z_FORMAT_P(getThis());
  89. format_set_italic(obj->ptr.format);
  90. }
  91. /* }}} */
  92. /** {{{ \Vtiful\Kernel\Format::underline()
  93. */
  94. PHP_METHOD(vtiful_format, underline)
  95. {
  96. zend_long style;
  97. ZEND_PARSE_PARAMETERS_START(1, 1)
  98. Z_PARAM_LONG(style)
  99. ZEND_PARSE_PARAMETERS_END();
  100. ZVAL_COPY(return_value, getThis());
  101. format_object *obj = Z_FORMAT_P(getThis());
  102. format_set_underline(obj->ptr.format, style);
  103. }
  104. /* }}} */
  105. /** {{{ \Vtiful\Kernel\Format::align()
  106. */
  107. PHP_METHOD(vtiful_format, align)
  108. {
  109. zval *args = NULL;
  110. int argc, i;
  111. ZEND_PARSE_PARAMETERS_START(1, -1)
  112. Z_PARAM_VARIADIC('+', args, argc)
  113. ZEND_PARSE_PARAMETERS_END();
  114. ZVAL_COPY(return_value, getThis());
  115. format_object *obj = Z_FORMAT_P(getThis());
  116. for (i = 0; i < argc; ++i) {
  117. zval *arg = args + i;
  118. if (Z_TYPE_P(arg) != IS_LONG) {
  119. zend_throw_exception(vtiful_exception_ce, "Format exception, please view the manual", 150);
  120. }
  121. format_set_align(obj->ptr.format, Z_LVAL_P(arg));
  122. }
  123. }
  124. /* }}} */
  125. /** {{{ \Vtiful\Kernel\Format::toResource()
  126. */
  127. PHP_METHOD(vtiful_format, toResource)
  128. {
  129. format_object *obj = Z_FORMAT_P(getThis());
  130. RETURN_RES(zend_register_resource(obj->ptr.format, le_xls_writer));
  131. }
  132. /* }}} */
  133. /** {{{ xls_methods
  134. */
  135. zend_function_entry format_methods[] = {
  136. PHP_ME(vtiful_format, __construct, format_construct_arginfo, ZEND_ACC_PUBLIC)
  137. PHP_ME(vtiful_format, bold, NULL, ZEND_ACC_PUBLIC)
  138. PHP_ME(vtiful_format, italic, NULL, ZEND_ACC_PUBLIC)
  139. PHP_ME(vtiful_format, underline, format_underline_arginfo, ZEND_ACC_PUBLIC)
  140. PHP_ME(vtiful_format, align, format_align_arginfo, ZEND_ACC_PUBLIC)
  141. PHP_ME(vtiful_format, toResource, NULL, ZEND_ACC_PUBLIC)
  142. PHP_FE_END
  143. };
  144. /* }}} */
  145. /** {{{ VTIFUL_STARTUP_FUNCTION
  146. */
  147. VTIFUL_STARTUP_FUNCTION(format) {
  148. zend_class_entry ce;
  149. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Format", format_methods);
  150. ce.create_object = format_objects_new;
  151. vtiful_format_ce = zend_register_internal_class(&ce);
  152. memcpy(&format_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  153. format_handlers.offset = XtOffsetOf(format_object, zo);
  154. format_handlers.free_obj = format_objects_free;
  155. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_SINGLE", LXW_UNDERLINE_SINGLE)
  156. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_DOUBLE ", LXW_UNDERLINE_DOUBLE)
  157. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_SINGLE_ACCOUNTING", LXW_UNDERLINE_SINGLE_ACCOUNTING)
  158. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "UNDERLINE_DOUBLE_ACCOUNTING", LXW_UNDERLINE_DOUBLE_ACCOUNTING)
  159. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_LEFT", LXW_ALIGN_LEFT)
  160. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_CENTER", LXW_ALIGN_CENTER)
  161. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_RIGHT", LXW_ALIGN_RIGHT)
  162. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_FILL", LXW_ALIGN_FILL)
  163. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_JUSTIFY", LXW_ALIGN_JUSTIFY)
  164. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_CENTER_ACROSS", LXW_ALIGN_CENTER_ACROSS)
  165. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_DISTRIBUTED", LXW_ALIGN_DISTRIBUTED)
  166. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_VERTICAL_TOP", LXW_ALIGN_VERTICAL_TOP)
  167. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_VERTICAL_BOTTOM", LXW_ALIGN_VERTICAL_BOTTOM)
  168. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_VERTICAL_CENTER", LXW_ALIGN_VERTICAL_CENTER)
  169. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_VERTICAL_JUSTIFY", LXW_ALIGN_VERTICAL_JUSTIFY)
  170. REGISTER_CLASS_CONST_LONG(vtiful_format_ce, "FORMAT_ALIGN_VERTICAL_DISTRIBUTED", LXW_ALIGN_VERTICAL_DISTRIBUTED)
  171. return SUCCESS;
  172. }
  173. /* }}} */