/* libsswf_tag_csmtextsettings.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2002-2008 */

/*

Copyright (c) 2002-2008 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

/** \file
 *
 * \brief The implementation of the sswf::TagCSMTextSettings class
 *
 * This file declares the body of the functions which are not
 * inline. It is part of the SSWF library.
 */

#include	"sswf/libsswf.h"

using namespace sswf;




/////////////////////////////////////////// TagCSMTextSettings



/** \class sswf::TagCSMTextSettings
 *
 * \brief Defines extraneous settings for text tags.
 *
 * This object defines extraneous settings to be used with text
 * tags such as DefineText, DefineText2 and DefineEditText.
 *
 * \sa sswf::TagEditText
 * \sa sswf::TagFont
 * \sa sswf::TagText
 * \sa <a href="../SWFalexref.html#tag_csmtextsettings">SWF Alexis' Reference&mdash;CSM Text Settings</a>
 * \sa <a href="../SWFalexref.html#swf_tag">SWF Alexis' Reference&mdash;swf_tag</a>
 */


/** \enum sswf::TagCSMTextSettings::csm_text_renderer_t
 *
 * \brief The list of renderer available in the Flash player
 *
 * This enumeration is used to list all the text renderers available
 * in a Flash Player.
 */

/** \var sswf::TagCSMTextSettings::CSM_TEXT_RENDERER_NORMAL
 *
 * \brief Use the normal renderer.
 *
 * This value is used as the default. It will use the older Flash renderer
 * instead of the FontType specialized renderer.
 */

/** \var sswf::TagCSMTextSettings::CSM_TEXT_RENDERER_FLASH
 *
 * \brief Use the special FontType renderer
 *
 * This value is used to ask the player to use the new Font Type renderer.
 * This option appeared in SFW version 8.
 *
 * It is supposed to do a much better job at drawing small fonts.
 */

/** \enum sswf::TagCSMTextSettings::csm_text_gridfit_t
 *
 * \brief Defines how to draw points in regard to screen pixels.
 *
 * This enumeration is used to define how to render fonts in regard
 * to pixels.
 */

/** \var sswf::TagCSMTextSettings::CSM_TEXT_GRIDFIT_NO_GRID
 *
 * \brief The default value meaning draw text anywhere.
 *
 * This value is the default and lets the renderer draw the font at
 * any position. Very useful if you are scrolling your text horizontally
 * or rotating it.
 */

/** \var sswf::TagCSMTextSettings::CSM_TEXT_GRIDFIT_PIXEL
 *
 * \brief The simple pixel grid snap feature.
 *
 * This value indicates that fonts should be drawn on a per pixel
 * basis.
 *
 * There are two problems with this method:
 *
 * \li Some glyphs will be rendered a little further than they
 *     should otherwise be. This can make some words look broken.
 *
 * \li Moving such fonts in any direction will make it jump if the
 *     move is too slow.
 */

/** \var sswf::TagCSMTextSettings::CSM_TEXT_GRIDFIT_SUBPIXEL
 *
 * \brief Use 1/3rd of a pixel to snap the font.
 *
 * This feature allows for 1/3rd of a pixel snapping system that
 * mostly solves the two problems of the PIXEL Grid Fit.
 */


/** \brief Initializes the CSM Text Settings tag.
 *
 * This function initializes the CSM Text Settings tag to its
 * defaults. This means it will not be used.
 */
TagCSMTextSettings::TagCSMTextSettings(void)
{
	f_csm_text_renderer = CSM_TEXT_RENDERER_NORMAL;
	f_csm_text_gridfit = CSM_TEXT_GRIDFIT_NO_GRID;
	f_thickness = 0.0f;
	f_sharpness = 0.0f;
}

/** \brief Clean up the TagCSMTextSettings
 *
 * This function cleans up the CSM Text Settings tag.
 *
 * Also, the existance of a virtual function makes the
 * TagCSMTextSettings class polymorphic.
 */
TagCSMTextSettings::~TagCSMTextSettings()
{
}





/** \brief Check whether an option is not the default.
 *
 * This function checks all the parameters of the TagCSMTextSettings
 * and returns true if any one of them was modified and thus the
 * tag needs to be saved.
 *
 * \return True if the tag needs to be saved.
 */
bool TagCSMTextSettings::IsCSMTextSettingsDefined(void) const
{
	return f_csm_text_renderer != CSM_TEXT_RENDERER_NORMAL
		|| f_csm_text_gridfit != CSM_TEXT_GRIDFIT_NO_GRID
		|| f_thickness != 0.0f
		|| f_sharpness != 0.0f;
}


/** \brief Defines the renderer to use.
 *
 * This function is used to define the renderer to use
 * with this TagCSMTextSetting.
 *
 * \param[in] csm_text_renderer The new renderer.
 */
void TagCSMTextSettings::SetRenderer(csm_text_renderer_t csm_text_renderer)
{
	f_csm_text_renderer = csm_text_renderer;
}


/** \brief Defines the grid to use.
 *
 * This function is used to define the grid to use
 * with this TagCSMTextSetting.
 *
 * \param[in] csm_text_gridfit The new grid mode.
 */
void TagCSMTextSettings::SetGridFit(csm_text_gridfit_t csm_text_gridfit)
{
	f_csm_text_gridfit = csm_text_gridfit;
}


/** \brief Defines the thickness to use.
 *
 * This function is used to define the thickness to use
 * with this TagCSMTextSetting.
 *
 * \param[in] thickness The new thickness.
 */
void TagCSMTextSettings::SetThickness(float thickness)
{
	f_thickness = thickness;
}


/** \brief Defines the sharpness to use.
 *
 * This function is used to define the sharpness to use
 * with this TagCSMTextSetting.
 *
 * \param[in] sharpness The new sharpness.
 */
void TagCSMTextSettings::SetSharpness(float sharpness)
{
	f_sharpness = sharpness;
}



/** \brief Ensure that the minimum version is 8.
 *
 * This function ensures that the minimum version is 8 if the
 * tag needs to be saved. Otherwise it does nothing.
 *
 * \return An error code or ErrorManager::ERROR_CODE_NONE.
 */
ErrorManager::error_code_t TagCSMTextSettings::PreSaveCSMTextSettings(void)
{
	TagBaseID *base_id = dynamic_cast<TagBaseID *>(this);

	if(IsCSMTextSettingsDefined()) {
		base_id->MinimumVersion(8);
	}

	return ErrorManager::ERROR_CODE_NONE;
}


/** \brief Save the TagCSMTextSettings in the specified Data buffer.
 *
 * This function checks whether the tag needs to be saved.
 * If not, nothing happens and the function returns immediately.
 * Otherwise, it saves the TagCSMTextSettings.
 *
 * \param[in] data The Data buffer where the tag is saved
 *
 * \return An error code or ErrorManager::ERROR_CODE_NONE.
 */
ErrorManager::error_code_t TagCSMTextSettings::SaveCSMTextSettings(Data& data)
{
	if(!IsCSMTextSettingsDefined()) {
		return ErrorManager::ERROR_CODE_NONE;
	}

	TagBaseID *base_id = dynamic_cast<TagBaseID *>(this);
	base_id->SaveTag(data, TagBase::SWF_TAG_CSM_TEXT_SETTINGS, 12);
	base_id->SaveID(data);
	data.WriteBits(f_csm_text_renderer, 2);
	data.WriteBits(f_csm_text_gridfit, 3);
	data.WriteBits(0, 3);
	data.PutLongFloat(f_thickness);
	data.PutLongFloat(f_sharpness);
	data.PutByte(0);

	return ErrorManager::ERROR_CODE_NONE;
}




/* The following options fold the documentation; use 'zi' to turn on and off
 *
 * vim: foldexpr=getline(v\:lnum)!~'^/\\*\\*'&&getline(v\:lnum)!~'^\ \\*'?0\:1 foldcolumn=2 foldmethod=expr
 */
