ttutil.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* -*- mode: c++; c-basic-offset: 4 -*- */
  2. /*
  3. * Modified for use within matplotlib
  4. * 5 July 2007
  5. * Michael Droettboom
  6. */
  7. /* Very simple interface to the ppr TT routines */
  8. /* (c) Frank Siegert 1996 */
  9. #include <cstdio>
  10. #include <cstdarg>
  11. #include <cstdlib>
  12. #include "pprdrv.h"
  13. #if DEBUG_TRUETYPE
  14. void debug(const char *format, ... )
  15. {
  16. va_list arg_list;
  17. va_start(arg_list, format);
  18. printf(format, arg_list);
  19. va_end(arg_list);
  20. }
  21. #endif
  22. #define PRINTF_BUFFER_SIZE 512
  23. void TTStreamWriter::printf(const char* format, ...)
  24. {
  25. va_list arg_list;
  26. va_start(arg_list, format);
  27. char buffer[PRINTF_BUFFER_SIZE];
  28. #if defined(WIN32) || defined(_MSC_VER)
  29. int size = _vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, arg_list);
  30. #else
  31. int size = vsnprintf(buffer, PRINTF_BUFFER_SIZE, format, arg_list);
  32. #endif
  33. if (size >= PRINTF_BUFFER_SIZE) {
  34. char* buffer2 = (char*)malloc(size);
  35. #if defined(WIN32) || defined(_MSC_VER)
  36. _vsnprintf(buffer2, size, format, arg_list);
  37. #else
  38. vsnprintf(buffer2, size, format, arg_list);
  39. #endif
  40. free(buffer2);
  41. } else {
  42. this->write(buffer);
  43. }
  44. va_end(arg_list);
  45. }
  46. void TTStreamWriter::put_char(int val)
  47. {
  48. char c[2];
  49. c[0] = (char)val;
  50. c[1] = 0;
  51. this->write(c);
  52. }
  53. void TTStreamWriter::puts(const char *a)
  54. {
  55. this->write(a);
  56. }
  57. void TTStreamWriter::putline(const char *a)
  58. {
  59. this->write(a);
  60. this->write("\n");
  61. }
  62. void replace_newlines_with_spaces(char *a) {
  63. char* i = a;
  64. while (*i != 0) {
  65. if (*i == '\r' || *i == '\n')
  66. *i = ' ';
  67. i++;
  68. }
  69. }