Return to FunctionReference page
Function
float SWFText_get!StringWidth(SWFText text, const char* string);
Purpose
- Returns the display width of a given string, calculated using the settings of a given text object.
Parameters
SWFText text - The text object to base the calculation on
const char* string - A pointer to the text string to work out the width of. (i.e. "This is a string")
Returns
- A float, with the swf display width of the text string.
Language
- C
Ming version
- (unknown). At least 0.4.0 and onwards, possibly earlier versions as well.
Notes
None as yet
Example
1 #define _ISOC99_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <ming.h>
6
7 int main(void)
8 {
9 // Create local variables
10 int i;
11 SWFDisplayItem image_display_item;
12 SWFFont font_object;
13 float string_width;
14 SWFMovie test_movie;
15 SWFText text_object;
16
17
18 // Initialise the movie structure in memory
19 Ming_init();
20 test_movie = newSWFMovieWithVersion(7);
21
22 // Set the desired compression level for the output (9 = maximum compression)
23 Ming_setSWFCompression(9);
24
25 // Set the background color for the movie
26 SWFMovie_setBackground(test_movie, 0x00, 0x00, 0x00);
27
28 // Adjust the dimensions of the movie
29 SWFMovie_setDimension(test_movie, 800, 600);
30
31 // Set the frame rate for the movie to 24 frames per second
32 SWFMovie_setRate(test_movie, 24.0);
33
34 // Load a font from disk
35 font_object = newSWFFont_fromFile("font.fdb");
36 if (NULL == font_object)
37 {
38 // Something went wrong, so exit
39 printf("Unable to load font from file.\n");
40 return EXIT_FAILURE;
41 }
42
43 // Create a new, empty text object
44 text_object = newSWFText();
45
46 // Tell the text object to use the font previously loaded
47 SWFText_setFont(text_object, font_object);
48
49 // Set the height of the text
50 SWFText_setHeight(text_object, 18.0);
51
52 // Set the color of the text
53 SWFText_setColor(text_object, 0x00, 0x00, 0xff, 0xff);
54
55 // Determine the width of a text string
56 string_width = SWFText_getStringWidth(text_object, "This is some example text");
57
58 // Add a string to the text object
59 SWFText_addString(text_object, "This is some example text", NULL);
60
61 // Add the text object to the movie (at 0,0)
62 image_display_item = SWFMovie_add(test_movie, (SWFBlock) text_object);
63
64 // Move to 100, 100
65 SWFDisplayItem_moveTo(image_display_item, 100.00, 100.0);
66
67 // Progressively move the text down and to the right
68 for (i = 0; i <= 250; i++)
69 {
70 SWFMovie_nextFrame(test_movie);
71 SWFDisplayItem_move(image_display_item, roundf(string_width), 2);
72 }
73
74 // Save the swf movie file to disk
75 SWFMovie_save(test_movie, "ming-test.swf");
76
77 // Free the swf movie in memory
78 destroySWFMovie(test_movie);
79
80 // Free the swf text object
81 destroySWFText(text_object);
82
83 return EXIT_SUCCESS;
84 }