Team Fortress 2 Source Code as on 22/4/2020
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.

195 lines
10 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef CSSHELPERS_H
  6. #define CSSHELPERS_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "float.h"
  11. #include "tier0/dbg.h"
  12. #include "tier1/utlvector.h"
  13. #include "mathlib/vmatrix.h"
  14. #include "mathlib/beziercurve.h"
  15. #include "uilength.h"
  16. #include "fillbrush.h"
  17. #include "fmtstr.h"
  18. #include "../text/iuitextlayout.h"
  19. #include "panorama/layout/stylesymbol.h"
  20. class CUtlString;
  21. class Color;
  22. class CUtlSymbol;
  23. namespace panorama
  24. {
  25. class CTransform3D;
  26. class CBackgroundPosition;
  27. class CBackgroundRepeat;
  28. const char k_rgchCSSDefaultTerm[] = { ' ', ';', '{', '}', ':' };
  29. const char k_rgchCSSAtRuleNameTerm[] = { ' ', ';', '{', '}' };
  30. const char k_rgchCSSSelectorTerm[] = { ';', '{', '}' };
  31. const char k_rgchCSSValueTerm[] = { ';', '{', '}' };
  32. const char k_rgchCSSValueTermOrEndOfString[] = { ';', '{', '}', '\0' };
  33. //-----------------------------------------------------------------------------
  34. // Purpose: defines
  35. //-----------------------------------------------------------------------------
  36. const int k_nCSSPropertyNameMax = 128;
  37. //-----------------------------------------------------------------------------
  38. // Purpose: string to type routines
  39. //-----------------------------------------------------------------------------
  40. const char* PchNameFromEFontWeight( int nValue );
  41. const char* PchNameFromEFontStyle( int nValue );
  42. const char* PchNameFromETextAlign( int nValue );
  43. const char* PchNameFromETextDecoration( int nValue );
  44. const char *PchNameFromETextTransform( int nValue );
  45. const char* PchNameFromEAnimationTimingFunction( int nValue );
  46. const char *PchNameFromEHorizontalAlignment( EHorizontalAlignment eHorizontalAlignment );
  47. const char *PchNameFromEVerticalAlignment( EVerticalAlignment eVerticalAlignment );
  48. const char *PchNameFromEContextUIPosition( EContextUIPosition ePosition );
  49. const char *PchNameFromEBackgroundRepeat( int nValue );
  50. EFontWeight EFontWeightFromName( const char *pchName );
  51. EFontStyle EFontStyleFromName( const char *pchName );
  52. ETextAlign ETextAlignFromName( const char *pchName );
  53. ETextDecoration ETextDecorationFromName( const char *pchName );
  54. ETextTransform ETextTransformFromName( const char *pchName );
  55. EAnimationTimingFunction EAnimationTimingFunctionFromName( const char *pchName );
  56. //-----------------------------------------------------------------------------
  57. // Purpose: Common functions for dealing with CSS
  58. //-----------------------------------------------------------------------------
  59. namespace CSSHelpers
  60. {
  61. bool BParseFillBrushCollection( CFillBrushCollection *pCollection, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  62. bool BParseFillBrush( CFillBrush *pBrush, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  63. bool BParseGradientColorStop( CGradientColorStop *pColorStop, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  64. bool BParseGaussianBlur( float &flPasses, float &flStdDevHorizontal, float &flStdDevVertical, const char *pchString, const char **pchAfterParse = NULL );
  65. bool BParseColor( Color *pColor, const char *pchString, const char **pchAfterParse = NULL );
  66. bool BParseRect( CUILength *pTop, CUILength *pRight, CUILength *pBottom, CUILength *pLeft, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  67. bool BParseNamedColor( Color *pColor, const char *pchString, const char **pchAfterParse = NULL );
  68. bool BParseLength( float *pLength, const char *pchString, const char **pchAfterParse = NULL );
  69. bool BParseNumber( float *pNumber, const char *pchString, const char **pchAfterParse = NULL );
  70. bool BParseTime( double *pSeconds, const char *pchString, const char **pchAfterParse = NULL );
  71. bool BParseIdent( char *rgchIdent, int cubIdent, const char *pchString, const char **pchAfterParse = NULL );
  72. bool BParseIdentToSymbol( CPanoramaSymbol *pSymbol, const char *pchString, const char **pchAfterParse = NULL );
  73. bool BParseIdentToStyleSymbol( panorama::CStyleSymbol *pSymbol, const char *pchString, const char **pchAfterParse = NULL );
  74. bool BParseQuotedString( CUtlString &sOutput, const char *pchString, const char **pchAfterParse = NULL );
  75. bool BParseURL( CUtlString &sPath, const char *pchString, const char **pchAfterParse = NULL );
  76. bool BParsePercent( float *pPercent, const char *pchString, const char **pchAfterParse = NULL );
  77. bool BParseIntoUILengthForSizing( CUILength *pLength, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  78. bool BParseIntoUILength( CUILength *pLength, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  79. bool BParseIntoTwoUILengths( CUILength *pLength1, CUILength *pLength2, const char *pchString, const char **pchAfterParse, float flScalingFactor = 1.0f );
  80. bool BParseTimingFunction( EAnimationTimingFunction *peTimingFunction, CCubicBezierCurve< Vector2D > *pCubicBezier, const char *pchString, const char **pchAfterParse );
  81. bool BParseAnimationDirectionFunction( EAnimationDirection *peAnimationDirection, const char *pchString, const char **pchAfterParse );
  82. bool BParseAngle( float *pDegrees, const char *pchString, const char **pchAfterParse = NULL );
  83. bool BParseTransformFunction( CTransform3D **pTransform, const char *pchString, const char **pchAfterParse = NULL, float flScalingFactor = 1.0f );
  84. bool BParseBorderStyle( EBorderStyle *pStyle, const char *pchString, const char **pchAfterParse = NULL );
  85. bool BParseHorizontalAlignment( EHorizontalAlignment *eHorizontalAlignment, const char *pchString, const char **pchAfterParse = NULL );
  86. bool BParseVerticalAlignment( EVerticalAlignment *eVerticalAlignment, const char *pchString, const char **pchAfterParse = NULL );
  87. bool BParseBackgroundPosition( CBackgroundPosition *pPosition, const char *pchString, const char **pchAfterParse = NULL );
  88. bool BParseBackgroundRepeat( CBackgroundRepeat *pBackgroundRepeat, const char *pchString, const char **pchAfterParse );
  89. bool BParseFunctionName( CPanoramaSymbol &symFunctionNameOut, const char *pchString, const char **pchAfterParse = NULL );
  90. template <typename T> bool BParseCommaSepList( CUtlVector< T, CUtlMemory<T> > *pvec, bool (*func)( T*, const char *, const char ** ), const char *pchString );
  91. bool BParseCommaSepList( CUtlVector< EAnimationTimingFunction > *pvec, CUtlVector< CCubicBezierCurve< Vector2D > > *pvec2, bool (*func)( EAnimationTimingFunction *pvec, CCubicBezierCurve< Vector2D > *pvec2, const char *, const char ** ), const char *pchString );
  92. template <typename T> bool BParseCommaSepListWithScaling( CUtlVector< T, CUtlMemory<T> > *pvec, bool (*func)( T*, const char *, const char **, float ), const char *pchString, float flScalingFactor );
  93. const char *SkipSpaces( const char *pchString );
  94. bool BSkipComma( const char *pchString, const char **pchAfterParse = NULL );
  95. bool BSkipLeftParen( const char *pchString, const char **pchAfterParse = NULL );
  96. bool BSkipRightParen( const char *pchString, const char **pchAfterParse = NULL );
  97. bool BSkipQuote( const char *pchString, const char **pchAfterParse = NULL );
  98. bool BSkipSlash( const char *pchString, const char **pchAfterParse = NULL );
  99. void AppendUILength( CFmtStr1024 *pfmtBuffer, const CUILength &length );
  100. void AppendFloat( CFmtStr1024 *pfmtBuffer, float flValue );
  101. void AppendColor( CFmtStr1024 *pfmtBuffer, Color c );
  102. void AppendTransform( CFmtStr1024 *pfmtBuffer, CTransform3D *pTransform );
  103. void AppendTime( CFmtStr1024 *pfmtBuffer, float flValue );
  104. void AppendFillBrushCollection( CFmtStr1024 *pfmtBuffer, const CFillBrushCollection &collection );
  105. void AppendFillBrush( CFmtStr1024 *pfmtBuffer, const CFillBrush &brush );
  106. void AppendGradientColorStop( CFmtStr1024 *pfmtBuffer, const CGradientColorStop &stops );
  107. void AppendURL( CFmtStr1024 *pfmtBuffer, const char *pchURL );
  108. void AppendLength( CFmtStr1024 *pfmtBuffer, float flValue );
  109. bool BParseTrueFalse( const char *pchString, bool *pbValue );
  110. bool EatCSSComment( CUtlBuffer &buffer );
  111. void EatCSSIgnorables( CUtlBuffer &buffer );
  112. bool BPeekCSSToken( CUtlBuffer &buffer, char *pchNextChar );
  113. bool BReadCSSComment( CUtlBuffer &buffer, char *pchBuffer, uint cubBuffer );
  114. bool BReadCSSToken( CUtlBuffer &buffer, char *pchToken, uint cubToken );
  115. bool BReadCSSToken( CUtlBuffer &buffer, char *pchToken, uint cubToken, const char *pchStopAt, uint cchStopAt );
  116. };
  117. //-----------------------------------------------------------------------------
  118. // Purpose: Helper to parse comma separated lists with same value type
  119. //-----------------------------------------------------------------------------
  120. template <typename T> bool CSSHelpers::BParseCommaSepList( CUtlVector< T, CUtlMemory<T> > *pvec, bool (*func)( T*, const char *, const char ** ), const char *pchString )
  121. {
  122. while ( *pchString != '\0' )
  123. {
  124. T val;
  125. if ( !func( &val, pchString, &pchString ) )
  126. return false;
  127. pvec->AddToTail( val );
  128. // done?
  129. if ( !CSSHelpers::BSkipComma( pchString, &pchString ) )
  130. {
  131. // no comma, should be empty string
  132. pchString = CSSHelpers::SkipSpaces( pchString );
  133. if ( pchString[0] != '\0' )
  134. return false;
  135. }
  136. }
  137. return true;
  138. }
  139. //-----------------------------------------------------------------------------
  140. // Purpose: Helper to parse comma separated lists with same value type
  141. //-----------------------------------------------------------------------------
  142. template <typename T> bool CSSHelpers::BParseCommaSepListWithScaling( CUtlVector< T, CUtlMemory<T> > *pvec, bool (*func)( T*, const char *, const char **, float ), const char *pchString, float flScalingFactor )
  143. {
  144. while ( *pchString != '\0' )
  145. {
  146. T val;
  147. if ( !func( &val, pchString, &pchString, flScalingFactor ) )
  148. return false;
  149. pvec->AddToTail( val );
  150. // done?
  151. if ( !CSSHelpers::BSkipComma( pchString, &pchString ) )
  152. {
  153. // no comma, should be empty string
  154. pchString = CSSHelpers::SkipSpaces( pchString );
  155. if ( pchString[0] != '\0' )
  156. return false;
  157. }
  158. }
  159. return true;
  160. }
  161. } // namespace panorama
  162. #endif //CSSHELPERS_H