You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

160 lines
4.5 KiB

  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. #if !defined(__KERNEL__)
  18. # include <stdio.h>
  19. # include <stdlib.h>
  20. # include <string.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /** \brief Get the number of frames in a canvas.
  25. *
  26. * This function returns the current canvas frame count.
  27. *
  28. * \param cv A libcucul canvas
  29. * \return The frame count
  30. */
  31. unsigned int cucul_get_canvas_frame_count(cucul_canvas_t *cv)
  32. {
  33. return cv->framecount;
  34. }
  35. /** \brief Activate a given canvas frame.
  36. *
  37. * This function sets the active canvas frame. All subsequent drawing
  38. * operations will be performed on that frame. The current painting
  39. * context set by cucul_set_color() or cucul_set_truecolor() is inherited.
  40. *
  41. * If the frame index is outside the canvas' frame range, nothing happens.
  42. *
  43. * \param cv A libcucul canvas
  44. * \param frame The canvas frame to activate
  45. */
  46. void cucul_set_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  47. {
  48. if(frame >= cv->framecount)
  49. return;
  50. cv->frame = frame;
  51. cv->chars = cv->allchars[cv->frame];
  52. cv->attr = cv->allattr[cv->frame];
  53. }
  54. /** \brief Add a frame to a canvas.
  55. *
  56. * This function creates a new frame within the given canvas. Its contents
  57. * are copied from the currently active frame.
  58. *
  59. * The frame index indicates where the frame should be inserted. Valid
  60. * values range from 0 to the current canvas frame count. If the frame
  61. * index is greater the or equals the current canvas frame count, the new
  62. * frame is appended at the end of the canvas.
  63. *
  64. * The active frame does not change, but its index may be renumbered due
  65. * to the insertion.
  66. *
  67. * \param cv A libcucul canvas
  68. * \param frame The index where to insert the new frame
  69. */
  70. void cucul_create_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  71. {
  72. unsigned int size = cv->width * cv->height * sizeof(uint32_t);
  73. unsigned int f;
  74. if(frame > cv->framecount)
  75. frame = cv->framecount;
  76. cv->framecount++;
  77. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  78. cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
  79. for(f = cv->framecount - 1; f > frame; f--)
  80. {
  81. cv->allchars[f] = cv->allchars[f - 1];
  82. cv->allattr[f] = cv->allattr[f - 1];
  83. }
  84. cv->allchars[frame] = malloc(size);
  85. memcpy(cv->allchars[frame], cv->chars, size);
  86. cv->allattr[frame] = malloc(size);
  87. memcpy(cv->allattr[frame], cv->attr, size);
  88. if(cv->frame >= frame)
  89. cv->frame++;
  90. cv->chars = cv->allchars[cv->frame];
  91. cv->attr = cv->allattr[cv->frame];
  92. }
  93. /** \brief Remove a frame from a canvas.
  94. *
  95. * This function deletes a frame from a given canvas.
  96. *
  97. * It is not legal to remove the last frame from a canvas. Such a request
  98. * will be ignored by cucul_free_canvas_frame().
  99. *
  100. * The frame index indicates the frame to delete. Valid values range from
  101. * 0 to the current canvas frame count minus 1. If the frame index is
  102. * greater the or equals the current canvas frame count, the last frame
  103. * is deleted.
  104. *
  105. * If the active frame is deleted, frame 0 becomes the new active frame.
  106. * Otherwise, the active frame does not change, but its index may be
  107. * renumbered due to the deletion.
  108. *
  109. * \param cv A libcucul canvas
  110. * \param frame The index of the frame to delete
  111. */
  112. void cucul_free_canvas_frame(cucul_canvas_t *cv, unsigned int frame)
  113. {
  114. unsigned int f;
  115. if(frame >= cv->framecount)
  116. return;
  117. if(cv->framecount == 1)
  118. return;
  119. free(cv->allchars[frame]);
  120. free(cv->allattr[frame]);
  121. for(f = frame + 1; f < cv->framecount; f++)
  122. {
  123. cv->allchars[f - 1] = cv->allchars[f];
  124. cv->allattr[f - 1] = cv->allattr[f];
  125. }
  126. cv->framecount--;
  127. cv->allchars = realloc(cv->allchars, sizeof(uint32_t *) * cv->framecount);
  128. cv->allattr = realloc(cv->allattr, sizeof(uint32_t *) * cv->framecount);
  129. if(cv->frame > frame)
  130. cv->frame--;
  131. else if(cv->frame == frame)
  132. cv->frame = 0;
  133. cv->chars = cv->allchars[cv->frame];
  134. cv->attr = cv->allattr[cv->frame];
  135. }