您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains a small framework for canvas frame management.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. # if defined(HAVE_ERRNO_H)
  23. # include <errno.h>
  24. # endif
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. /** \brief Get the number of frames in a canvas.
  29. *
  30. * Return the current canvas' frame count.
  31. *
  32. * This function never fails.
  33. *
  34. * \param cv A libcucul canvas
  35. * \return The frame count
  36. */
  37. unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
  38. {
  39. return cv->framecount;
  40. }
  41. /** \brief Activate a given canvas frame.
  42. *
  43. * Set the active canvas frame. All subsequent drawing operations will
  44. * be performed on that frame. The current painting context set by
  45. * cucul_set_attr() is inherited.
  46. *
  47. * If the frame index is outside the canvas' frame range, nothing happens.
  48. *
  49. * If an error occurs, -1 is returned and \b errno is set accordingly:
  50. * - \c EINVAL Requested frame is out of range.
  51. *
  52. * \param cv A libcucul canvas
  53. * \param frame The canvas frame to activate
  54. * \return 0 in case of success, -1 if an error occurred.
  55. */
  56. int cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  57. {
  58. if(frame >= cv->framecount)
  59. {
  60. #if defined(HAVE_ERRNO_H)
  61. errno = EINVAL;
  62. #endif
  63. return -1;
  64. }
  65. cv->frame = frame;
  66. cv->chars = cv->allchars[cv->frame];
  67. cv->attrs = cv->allattrs[cv->frame];
  68. return 0;
  69. }
  70. /** \brief Add a frame to a canvas.
  71. *
  72. * Create a new frame within the given canvas. Its contents are copied
  73. * from the currently active frame.
  74. *
  75. * The frame index indicates where the frame should be inserted. Valid
  76. * values range from 0 to the current canvas frame count. If the frame
  77. * index is greater the or equals the current canvas frame count, the new
  78. * frame is appended at the end of the canvas.
  79. *
  80. * The active frame does not change, but its index may be renumbered due
  81. * to the insertion.
  82. *
  83. * If an error occurs, -1 is returned and \b errno is set accordingly:
  84. * - \c ENOMEM Not enough memory to allocate new frame.
  85. *
  86. * \param cv A libcucul canvas
  87. * \param frame The index where to insert the new frame
  88. * \return 0 in case of success, -1 if an error occurred.
  89. */
  90. int cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  91. {
  92. unsigned int size = cv->width * cv->height * sizeof(uint32_t);
  93. unsigned int f;
  94. if(frame > cv->framecount)
  95. frame = cv->framecount;
  96. cv->framecount++;
  97. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  98. cv->allattrs = realloc(cv->allattrs, sizeof(uint32_t *) * cv->framecount);
  99. for(f = cv->framecount - 1; f > frame; f--)
  100. {
  101. cv->allchars[f] = cv->allchars[f - 1];
  102. cv->allattrs[f] = cv->allattrs[f - 1];
  103. }
  104. cv->allchars[frame] = malloc(size);
  105. memcpy(cv->allchars[frame], cv->chars, size);
  106. cv->allattrs[frame] = malloc(size);
  107. memcpy(cv->allattrs[frame], cv->attrs, size);
  108. if(cv->frame >= frame)
  109. cv->frame++;
  110. cv->chars = cv->allchars[cv->frame];
  111. cv->attrs = cv->allattrs[cv->frame];
  112. return 0;
  113. }
  114. /** \brief Remove a frame from a canvas.
  115. *
  116. * Delete a frame from a given canvas.
  117. *
  118. * The frame index indicates the frame to delete. Valid values range from
  119. * 0 to the current canvas frame count minus 1. If the frame index is
  120. * greater the or equals the current canvas frame count, the last frame
  121. * is deleted.
  122. *
  123. * If the active frame is deleted, frame 0 becomes the new active frame.
  124. * Otherwise, the active frame does not change, but its index may be
  125. * renumbered due to the deletion.
  126. *
  127. * If an error occurs, -1 is returned and \b errno is set accordingly:
  128. * - \c EINVAL Requested frame is out of range, or attempt to delete the
  129. * last frame of the canvas.
  130. *
  131. * \param cv A libcucul canvas
  132. * \param frame The index of the frame to delete
  133. * \return 0 in case of success, -1 if an error occurred.
  134. */
  135. int cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  136. {
  137. unsigned int f;
  138. if(frame >= cv->framecount)
  139. {
  140. #if defined(HAVE_ERRNO_H)
  141. errno = EINVAL;
  142. #endif
  143. return -1;
  144. }
  145. if(cv->framecount == 1)
  146. {
  147. #if defined(HAVE_ERRNO_H)
  148. errno = EINVAL;
  149. #endif
  150. return -1;
  151. }
  152. free(cv->allchars[frame]);
  153. free(cv->allattrs[frame]);
  154. for(f = frame + 1; f < cv->framecount; f++)
  155. {
  156. cv->allchars[f - 1] = cv->allchars[f];
  157. cv->allattrs[f - 1] = cv->allattrs[f];
  158. }
  159. cv->framecount--;
  160. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  161. cv->allattrs = realloc(cv->allattrs, sizeof(uint32_t *) * cv->framecount);
  162. if(cv->frame > frame)
  163. cv->frame--;
  164. else if(cv->frame == frame)
  165. cv->frame = 0;
  166. cv->chars = cv->allchars[cv->frame];
  167. cv->attrs = cv->allattrs[cv->frame];
  168. return 0;
  169. }