GFX 3.3 Drawtext API
GFX 3.3 Drawtext API
This document describes the GFxDrawText API available in GFx 3.3. This API can be used for C++
driven text rendering and formatting outside of the GFxMovieView and ActionScript sandbox.
Author:
Version:
Last Edited:
Artem Bolgar
1. 03
May 26, 2009
Copyright Notice
Autodesk Scaleform 3
2011 Autodesk, Inc. All rights reserved. Except as otherwise permitted by Autodesk, Inc., this
publication, or parts thereof, may not be reproduced in any form, by any method, for any purpose.
Certain materials included in this publication are reprinted with the permission of the copyright holder.
The following are registered trademarks or trademarks of Autodesk, Inc., and/or its subsidiaries and/or
affiliates in the USA and/or other countries: 3DEC (design/logo), 3December, 3December.com, 3ds
Max, Algor, Alias, Alias (swirl design/logo), AliasStudio, Alias|Wavefront (design/logo), ATC, AUGI,
AutoCAD, AutoCAD Learning Assistance, AutoCAD LT, AutoCAD Simulator, AutoCAD SQL Extension,
AutoCAD SQL Interface, Autodesk, Autodesk Intent, Autodesk Inventor, Autodesk MapGuide,
Autodesk Streamline, AutoLISP, AutoSnap, AutoSketch, AutoTrack, Backburner, Backdraft, Beast,
Built with ObjectARX (logo), Burn, Buzzsaw, CAiCE, Civil 3D, Cleaner, Cleaner Central, ClearScale,
Colour Warper, Combustion, Communication Specification, Constructware, Content Explorer, Dancing
Baby (image), DesignCenter, Design Doctor, Designer's Toolkit, DesignKids, DesignProf, DesignServer,
DesignStudio, Design Web Format, Discreet, DWF, DWG, DWG (logo), DWG Extreme, DWG
TrueConvert, DWG TrueView, DXF, Ecotect, Exposure, Extending the Design Team, Face Robot, FBX,
Fempro, Fire, Flame, Flare, Flint, FMDesktop, Freewheel, GDX Driver, Green Building Studio, Headsup Design, Heidi, HumanIK, IDEA Server, i-drop, Illuminate Labs AB (design/logo), ImageModeler,
iMOUT, Incinerator, Inferno, Inventor, Inventor LT, Kynapse, Kynogon, LandXplorer, LiquidLight,
LiquidLight (design/logo), Lustre, MatchMover, Maya, Mechanical Desktop, Moldflow, Moldflow
Plastics Advisers, MPI, Moldflow Plastics Insight, Moldflow Plastics Xpert, Moondust, MotionBuilder,
Movimento, MPA, MPA (design/logo), MPX, MPX (design/logo), Mudbox, Multi-Master Editing,
Navisworks, ObjectARX, ObjectDBX, Opticore, Pipeplus, PolarSnap, PortfolioWall, Powered with
Autodesk Technology, Productstream, ProMaterials, RasterDWG, RealDWG, Real-time Roto,
Recognize, Render Queue, Retimer, Reveal, Revit, RiverCAD, Robot, Scaleform, Scaleform AMP,
Scaleform CLIK, Scaleform GFx, Scaleform IME, Scaleform Video, Showcase, Show Me, ShowMotion,
SketchBook, Smoke, Softimage, Softimage|XSI (design/logo), Sparks, SteeringWheels, Stitcher, Stone,
StormNET, StudioTools, ToolClip, Topobase, Toxik, TrustedDWG, U-Vis, ViewCube, Visual, Visual
LISP, Volo, Vtour, WaterNetworks, Wire, Wiretap, WiretapCentral, XSI.
All other brand names, product names or trademarks belong to their respective holders.
Disclaimer
THIS PUBLICATION AND THE INFORMATION CONTAINED HEREIN IS MADE AVAILABLE BY
AUTODESK, INC. AS IS. AUTODESK, INC. DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR
DrawText API
Address
Website
Scaleform Corporation
6305 Ivy Lane, Suite 310
Greenbelt, MD 20770, USA
www.scaleform.com
info@scaleform.com
Direct
(301) 446-3200
Fax
(301) 446-3199
Table of Contents
1. Introduction ................................................................................................................................. 1
1.1. GFxDrawTextManager ........................................................................................................... 2
1.1.1. Creating GFxDrawText. ................................................................................................... 3
1.1.2.Text Rendering ................................................................................................................. 5
1.1.3. Measuring Text Extents ................................................................................................... 5
1.2. GFxDrawText ......................................................................................................................... 8
1.2.1. Setting and Getting Plain Text Methods .......................................................................... 8
1.2.2. Setting and Getting HTML Text Methods ........................................................................ 8
1.2.3. Setting Text Format Methods .......................................................................................... 9
1.2.4. Aligning Text Methods ................................................................................................... 10
1.2.5. Changing Text Position and Orientation ........................................................................ 10
1.2.6. Rendering Text Method ................................................................................................. 11
1.2.7. Using Custom Directives for Rendering Text ................................................................. 11
2. Overview of DrawText Sample Code ....................................................................................... 12
1. Introduction
The Scaleform GFx SDK implementation contains a powerful font rendering and text formatting engine,
primarily used for rendering text displayed as a part of the Flash based UI. Text rendering in GFx
supports a number of advanced features such as on-the-fly rasterization and caching of screen-pixel
aligned glyphs, scrollable text fields with paragraph alignment, and rich HTML-tag formatted text with
fonts obtained from either system font providers or the SWF/GFX embedded file data.
In most cases, GFx text rendering system is used automatically when playing Flash files, with text
fields arranged visually by the artist in the Flash Studio. For additional control, text formatting is
exposed through the TextField and TextFormat ActionScript (AS) classes, which provide a preferred
way of interfacing with text. With these APIs in place, it is rarely necessary to use the C++ APIs for this
purpose.
There are, however, occasional situations when going through ActionScript for text rendering can be
inconvenient, or the associated overhead undesirable. Rendering of name billboards over moving
avatars on screen or text labels next to items on a radar can often be done more efficiently through
C++ if your game can not afford having full-fledge Flash UI for those items. Furthermore, if your game
combines the GFx based menu system with a 3D engine rendered HUD, it is still desirable to use the
same font system in both cases.
Scaleform GFx 2.2 and higher versions includes a C++ driven DrawText API, exposing the GFx font
rendering and text formatting engines outside of the movie view sandbox. The new text API still uses
the same GRenderer interface as the rest of GFx player; however, it does not require creation of the
GFxMovieView object, allowing developers to position and manipulate text fields directly within the
viewport without the associated ActionScript overhead.
There are two possible sources of fonts for the DrawText API: system fonts and fonts loaded from
SWF/GFX files. In the first case, the system font provider should be used. In the second case, the
SWF file with fonts should be loaded as a GFxMovieDef (using the GFxLoader::CreateMovie method)
and then the created GFxMovieDef could be passed as a parameter to GFxDrawTextManager
constructor.
The two C++ that classes that provide text rendering functionality are GFxDrawTextManager and
GFxDrawText; their use is outlined below and then explained in detail through the rest of this
document.
1.1. GFxDrawTextManager
The GFxDrawTextManager class instance manages instances of GFxDrawText class; it creates,
renders and measures text extents.
There are several ways of creating the GFxDrawTextManager class instance:
1. GFxDrawTextManager();
A default constructor. Will create internal instances of font cache manager, resource library and log. A
system font provider should be used as font source (method SetFontProvider).
Example:
GPtr<GFxDrawTextManager> pdm = *new GFxDrawTextManager();
2. GFxDrawTextManager(GFxMovieDef* pmovieDef);
3. GFxDrawTextManager(GFxLoader* ploader);
This constructor takes a pointer to GFxLoader as a parameter and inherits all the states set on
the loader including the font library.
Example:
GFxLoader loader;
..
GFxDrawText* CreateText(const
const
GFxDrawText* CreateText(const
const
The methods above create GFxDrawText instances using plain text (null-terminated UTF-8 string, nullterminated Unicode/UCS-2 and GString respectively).
putf8str : specifies a null-terminated UTF-8 string.
pwstr : specifies a nullterminated Unicode/UCS-2 string.
str: specifies a GString.
viewRect : specifies coordinates of text view area (in pixels), relatively to top-left corner of viewport
(see BeginDisplay).
ptxtParams : Optional parameter. It specifies parameters of newly created text. TextParams has the
following structure:
struct TextParams
{
GColor
GFxDrawText::Alignment
GFxDrawText::VAlignment
GFxDrawText::FontStyle
Float
GString
bool
bool
bool
};
TextColor;
HAlignment;
VAlignment;
FontStyle;
FontSize;
FontName;
Underline;
Multiline;
WordWrap;
TextColor : specifies the color of the text, including the alpha channel.
HAlignment: specifies horizontal alignment. Possible values are: GFxDrawText::Align_Left (default),
If ptxtParams optional parameter is not specified then GFxDrawTextManager uses default text
parameters. It is possible to set and get these default text parameters by using the following methods:
void SetDefaultTextParams(const TextParams& params);
const TextParams& GetDefaultTextParams() const;
Examples:
// Creation of GFxDrawText object using plain text with parameters.
GString str("String No 2");
GFxDrawTextManager::TextParams params;
params.FontName
= "Symbol";
params.FontSize
params.FontStyle
params.Multiline
params.HAlignment
params.VAlignment
=
=
=
=
=
30;
GFxDrawText::Italic;
false;
GFxDrawText::Align_Right;
GFxDrawText::VAlign_Bottom;
GPtr<GFxDrawText> ptxt;
ptxt = *pdm->CreateText(str, GRectF(20, 300, 400, 700), ¶ms);
// Creation of GFxDrawText object from HTML.
GPtr<GFxDrawText> ptxt;
ptxt = *pdm->CreateHtmlText("<p><FONT size='20'>AB
<b>singleline</b><i> CD</i>O",
GRectF(20, 300, 400, 700));
1.1.2.Text Rendering
GFxDrawText::Display method is used to render the text. It is necessary to call
GFxDrawTextManager::BeginDisplay before the first call to GFxDrawText::Display and to call
GFxDrawTextManager::EndDisplay after the last call to GFxDrawText::Display:
GViewport vp(GetWidth(), GetHeight(), 0, 0, GetWidth(), GetHeight(), 0);
pdm->BeginDisplay(vp);
ptxt->Display();
ptxt2->Display();
ptxt3->Display();
pdm->EndDisplay();
GetTextExtent method calculates dimensions of text rectangle using plain text and, optionally, using
TextParams and desired width of text.
GetHtmlTextExtent method calculates dimensions of text rectangle using HTML text and, optionally,
using TextParams and desired width of text.
The optional width parameter specifies desired width of text rectangle in the case when multiline and
word wrapping are used. In this case, only the height of text rectangle will be calculated.
The optional ptxtParams specifies text parameters, which will be used during text dimensions
measurement. If it is not specified then the default text parameters will be used (see
SetDefaultTextParams / GetDefaultTextParams). For HTML version, the ptxtParams parameter works
a set of default text parameters, so, all styles from parsed HTML virtually will be applied above the
styles from the ptxtParams.
Examples:
// Plain text extents
GString str("String No 2");
GFxDrawTextManager::TextParams params;
params.FontName
= "Symbol";
params.FontSize
= 30;
params.FontStyle = GFxDrawText::Italic;
params.Multiline = false;
params.HAlignment = GFxDrawText::Align_Right;
params.VAlignment = GFxDrawText::VAlign_Bottom;
GSizeF sz = pdm->GetTextExtent(str, 0, params);
params.WordWrap = true;
params.Multiline = true;
sz = pdm->GetTextExtent(str, 120, params);
1.2. GFxDrawText
The GFxDrawText class provides functionality of setting text, parsing HTML, formatting and rendering
text.
GetText method returns currently set text in UTF-8 format. It returns plain text value; even if HTML is
used, it returns the string with all HTML tags stripped out.
GString GetText() const;
GetHtmlText method returns currently set text in HTML format. If plain text is used with setting
formatting by calling methods, such as SetColor, SetFont, etc, then this text will be converted to
appropriate HTML format by this method.
GString GetHtmlText() const;
Sets color (R, G, B, A) to whole text or to the part of text in interval [startPos..endPos]. Both
startPos and endPos parameters are optional.
void SetFont (const char* pfontName, UPInt startPos = 0, UPInt endPos = UPInt(-1))
Sets font to whole text or to the part of text in interval [startPos..endPos]. Both startPos and
endPos parameters are optional.
void SetFontSize(Float fontSize, UPInt startPos = 0, UPInt endPos = UPInt(-1))
Sets font size to whole text or to the part of text in interval [startPos..endPos]. Both startPos and
endPos parameters are optional.
void SetFontStyle(FontStyle, UPInt startPos = 0, UPInt endPos = UPInt(-1));
enum FontStyle
{
Normal,
Bold,
Italic,
BoldItalic,
ItalicBold = BoldItalic
};
Sets font style to whole text or to the part of text in interval [startPos..endPos]. Both startPos
and endPos parameters are optional.
void SetUnderline(bool underline, UPInt startPos = 0, UPInt endPos = UPInt(-1))
Sets or clears underline to whole text or to the part of text in interval [startPos..endPos]. Both
startPos and endPos parameters are optional.
void SetMultiline(bool multiline);
Sets multiline (parameter multiline is set to true) or singleline (false) type of the text.
bool IsMultiline() const;
The different methods for aligning the text methods are given below.
void SetAlignment(Alignment);
Sets horizontal text alignment (right, left, center).
Alignment
GetAlignment() const;
Returns horizontal text alignment (right, left, center).
void SetVAlignment(VAlignment);
Sets vertical text alignment (top, bottom, center).
VAlignment GetVAlignment() const;
Returns vertical text alignment (top, bottom, center).
GetRect() const;
10
GetRendererFloat and GetRendererString return the float and string values respectively which are
used in custom renderer implementations.
float GetRendererFloat();
GString GetRendererString();
11
The DrawText sample describes how to render text using two different instances of
GFxDrawTextManager class interface.
Method 1
In this example, GFxDrawTextManager instance is created using a pointer to GFxLoader to inherit all
states from the loader.
As the first step in drawing a text object using this method, create GFxLoader object and pass the
tracing information to SetLog which handles the log stream for debugging.
12
GFxLoader loader;
loader.SetLog(GPtr<GFxLog>(*new GFxPlayerLog()));
Here, we use the system fonts for the text to be displayed and thus GFxFontProvider is used for the
purpose.
GPtr<GFxFontProviderWin32> fontProvider =
*new GFxFontProviderWin32(::GetDC(0));
pdm1->SetFontProvider(fontProvider);
As mentioned in the API, there are several ways of creating DrawText instance by using either plain
text or HTML text. In this example, DrawText instances are created using plain text and GString.
// Create text using plain text and default text parameters.
GFxDrawTextManager::TextParams defParams =
pdm1->GetDefaultTextParams();
defParams.TextColor = GColor(0xF0, 0, 0, 0xFF); // red, alpha = 255
defParams.FontName = "Arial";
defParams.FontSize = 16;
pdm1->SetDefaultTextParams(defParams);
The text is created by calling CreateText method and uses the default text parameters set by the
previous call to SetDefaultTextParams.
GPtr<GFxDrawText> ptxt11 = *pdm1->CreateText("Plain text, red, Arial,
16pts",GRectF(20, 20, 500, 400));
13
GFxDrawTextManager::TextParams params;
params.FontName
= "Arial";
params.FontSize
= 14;
params.FontStyle = GFxDrawText::Italic;
params.Multiline = true;
params.WordWrap
= true;
params.HAlignment = GFxDrawText::Align_Justify;
sz = pdm1->GetTextExtent(str, 200, ¶ms);
GPtr<GFxDrawText> ptxt12 = *pdm1->CreateText(str, GRectF(200, 300, sz),
¶ms);
ptxt12->SetColor(GColor(0, 0, 255, 130), 0, 1);
Once the text is created using the system fonts, call the BeginDisplay method of
GFxDrawTextManager which has a pointer to the viewport and then call Display method of
GFxDrawText to render the text on the viewport.After the text is rendered, use the EndDisplay
method of GFxDrawTextManager to end the display.
pdm1->BeginDisplay(vp);
ptxt11->Display();
ptxt12->Display();
pdm1->EndDisplay();
Method 2
This example creates an instance of GFxDrawTextManger which uses a GFxMovieDef pointer to share
the fonts contained in GFxMovieDef. The font for the text is obtained from the SWF file loaded in
GFxMovieDef. Please refer to GFx Integration Tutorial for an understanding of GFx and loading movie
objects.
GPtr<GFxMovieDef> pmovieDef = *loader.CreateMovie("drawtext_fonts.swf");
GPtr<GFxDrawTextManager> pdm2 = *new GFxDrawTextManager(pmovieDef);
In differing from the previous example, we display the text using HTML text instead of plain text (but,
of course, the HTML might be used in the previous example as well). The dimensions of the text
rectangle is calculated by using the GFxDrawTextManager::GetHtmlExtent method.
// Create HTML text, using fonts from GFxMovieDef
const wchar_t* html = L"<P>o123 <FONT FACE=\"Times New Roman\" SIZE
=\"140\">"L"<b><i><FONT
COLOR='#3484AA'></FONT></i></b>!</FONT></P>"
14
We also need to create text to demonstrate text animation (rotation and changing color matrix):
GSizeF sz;
sz = pdm2->GetTextExtent("Animated");
GPtr<GFxDrawText> ptxt21 = *pdm2->CreateText("Animated",
GRectF(600, 400, sz));
ptxt21->SetColor(GColor(255, 255, 255, 255));
Float angle = 0;
UInt32 color = 0xFF00;
Make use of the different functions available in GFxDrawText to change color or position the text
object.
As an example, rotate and change color of text created by using SetMatrix and SetCxform methods
of GFxDrawText. We do it at the end of the main loop inside the FxPlayer::Run:
while(1)
{
. . . .
// Rotate and animate color for ptxt21
GFxDrawText::Matrix txt21matrix;
angle += 1;
GRectF r = ptxt21->GetRect();
txt21matrix.AppendTranslation(-r.Left, -r.Top);
txt21matrix.AppendRotation(angle*3.14159f / 180.f);
15
txt21matrix.AppendScaling(2);
txt21matrix.AppendTranslation(r.Left, r.Top);
ptxt21->SetMatrix(txt21matrix);
GFxDrawText::Cxform txt21cx;
txt21cx.M_[0][0] = Float(color & 0xFF) /255.f;
txt21cx.M_[1][0] = Float((color >> 8) & 0xFF) /255.f;
txt21cx.M_[2][0] = Float((color >> 16) & 0xFF) /255.f;
color += 32;
ptxt21->SetCxform(txt21cx);
}
NOTE: Copy the drawtext_fonts.swf from the Bin/Samples directory into the same directory where the
executable is located (if you run the executable manually) or into the project directory (e.g.:
Projects/Win32/Msvc90/Samples/DrawText, if run from the Visual Studio 2008). Otherwise, instead of
most of the glyphs you will just see rectangles.
Screenshot:
16