Font.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /******************************************************************************
  2. Use 'FontMake' to create a base font image.
  3. Use 'Font' to:
  4. -create final fonts out of the base font image
  5. -draw the text
  6. /******************************************************************************/
  7. #define FONT_WIDTH_TEST 8
  8. /******************************************************************************/
  9. enum CHARSET_TYPE : Byte
  10. {
  11. CHARSET_ANSI =0 ,
  12. CHARSET_DEFAULT =1 ,
  13. CHARSET_EAST_EUROPE=238,
  14. CHARSET_RUSSIAN =204,
  15. CHARSET_SHIFT_JIS =128,
  16. CHARSET_HANGEUL =129,
  17. CHARSET_HANGUL =129,
  18. CHARSET_GB_2312 =134,
  19. CHARSET_CHINESE =136,
  20. CHARSET_GREEK =161,
  21. CHARSET_ARABIC =178,
  22. CHARSET_TURKISH =162,
  23. CHARSET_VIETNAMESE =163,
  24. CHARSET_THAI =222,
  25. CHARSET_BALTIC =186,
  26. CHARSET_JOHAB =130,
  27. CHARSET_HEBREW =177,
  28. CHARSET_MAC =77 ,
  29. CHARSET_SYMBOL =2 ,
  30. CHARSET_OEM =255,
  31. };
  32. /******************************************************************************/
  33. enum SPACING_MODE : Byte // Text Spacing Mode
  34. {
  35. SPACING_CONST, // constant spacing, characters are aligned by a constant factor taken from TextStyle::space
  36. SPACING_FAST , // fast spacing, characters are aligned by their average width (1 width test per character)
  37. SPACING_NICE , // nice spacing, characters are aligned by their width (multiple width tests per character performed on different parts of the character)
  38. };
  39. /******************************************************************************/
  40. struct Font
  41. {
  42. enum MODE : Byte
  43. {
  44. DEFAULT , // anti-aliased
  45. SMOOTHED , // anti-aliased with extra smoothing but no sub-pixel precision
  46. SUB_PIXEL, // anti-aliased with extra smoothing and sub-pixel precision, Warning: enabling this option will increase font quality only if it will be drawn with correct scale (in other case font quality will be worse), using this option also disables font shadows
  47. };
  48. struct Params // font creation parameters
  49. {
  50. Str system_font , // name of the font
  51. characters ; // characters to include in the font
  52. Bool software , // create in software mode for CPU processing only, true/false
  53. shadow_diagonal; // if set shadows in diagonal mode , true/false
  54. Byte mip_maps ; // amount of desired mip maps for the textures , 0..8 (0=autodetect and create full mip-map chain)
  55. Int size , // font size (in pixels) , 1..Inf
  56. max_image_size ; // maximum allowed image size to be generated , 1..Inf
  57. Flt scale , // scale applied to source font characters , 0.5..2.0
  58. shadow_blur , // amount of shadow blurring , 0..1
  59. shadow_opacity , // shadow opacity , 0..1
  60. shadow_spread , // shadow spread , 0..1
  61. weight , // font weight , 0..1
  62. minimum_filter ; // distance in pixels for the minimum filter , 0..1
  63. MODE mode ;
  64. IMAGE_TYPE image_type ; // image format
  65. CHARSET_TYPE charset ; // charset type
  66. Params()
  67. {
  68. software=false; shadow_diagonal=true; mip_maps=3; size=48; max_image_size=2048;
  69. scale=1.0f; shadow_blur=0.04f; shadow_opacity=1.0f; shadow_spread=0.0f; weight=0; minimum_filter=0;
  70. mode=DEFAULT; image_type=IMAGE_BC7; charset=CHARSET_DEFAULT;
  71. }
  72. };
  73. // manage
  74. Bool create(C Params &params); // create font using specified 'params', false on fail
  75. // get
  76. Bool is ()C {return _height>0;} // if is a valid font
  77. Int height()C {return _height ;} // get font height in pixels
  78. Bool hasChar(Char8 c)C; // if font supports 'c' character
  79. Bool hasChar(Char c)C; // if font supports 'c' character
  80. UInt memUsage()C; // get approximate memory size usage of the font images
  81. Int charWidth(Char c)C; // get width of character (in pixels)
  82. Int charWidth(Char8 c)C; // get width of character (in pixels)
  83. Int textWidth(Int &base_chars, SPACING_MODE spacing, CChar *text, Int max_length=-1)C; // get width of text (in pixels)
  84. Int textWidth(Int &base_chars, SPACING_MODE spacing, CChar8 *text, Int max_length=-1)C; // get width of text (in pixels)
  85. // operations
  86. Font& freeOpenGLESData( ); // this method is used only under OpenGL ES (on other platforms it is ignored), the method frees the software copy of the GPU data which increases available memory, however after calling this method the data can no longer be accessed on the CPU (can no longer be locked or saved to file)
  87. Bool imageType (IMAGE_TYPE type ); // this method will change the image type of the font, returns true if any changes were applied to the font, and false if no changes were applied
  88. void toSoft ( ); // convert to software mode, for faster software processing
  89. Font& replace (Char src, Char dest, Bool permanent=false); // replace original 'src' character to be drawn as the 'dest' character, 'permanent'=if apply the change permanently (will be included in 'save') or just temporarily
  90. Font& removeAccent ( Bool permanent=false); // this method will replace drawing of all accented characters with their non-accented version, for example 'ą' will be replaced with 'a', 'permanent'=if apply the change permanently (will be included in 'save') or just temporarily
  91. // io
  92. void operator=(C Str &name) ; // load, Exit on fail
  93. void operator=(C UID &id ) ; // load, Exit on fail
  94. Bool save (C Str &name)C; // save, false on fail
  95. Bool load (C Str &name) ; // load, false on fail
  96. Bool save(File &f)C; // save, false on fail
  97. Bool load(File &f) ; // load, false on fail
  98. #if EE_PRIVATE
  99. Int charWidth(Char c0, Char c1, SPACING_MODE spacing)C;
  100. Int charWidth(Char8 c0, Char8 c1, SPACING_MODE spacing)C;
  101. void setRemap ();
  102. void setGLFont();
  103. void zero ();
  104. #endif
  105. Font& del(); // delete manually
  106. Font();
  107. // direct access to font character data
  108. struct Chr // single character data
  109. {
  110. Char chr ; // character which this data is assigned to
  111. Byte image , // image index from the 'images' container that includes this character
  112. offset , // vertical offset - number of pixels that need to be added from the top when drawing this character
  113. width , // full width of this character in pixels (this does not include the shadow padding)
  114. height , // full height of this character in pixels (this does not include the shadow padding and 'offset')
  115. width_padd , // full width of this character in pixels (this does include the shadow padding)
  116. height_padd, // full height of this character in pixels (this does include the shadow padding but not 'offset')
  117. width2[2][FONT_WIDTH_TEST];
  118. Rect tex ; // texture coordinates covering the font pixels in the image (this already includes the shadow padding)
  119. };
  120. C Mems<Chr >& chrs ()C {return _chrs ;} // get list of characters supported in this Font
  121. C Mems<Image>& images()C {return _images;} // get list of images used for storing character data, these images have data packed in channels: Green=Character Intensity, Alpha=Shadow Intensity, Red/Blue=Unused, please keep in mind that these images are IMAGE_2D hardware textures with possibly compressed texture formats, which are not suitable for software processing, when wanting to do software drawing it's recommended to first copy the images to separate software copies
  122. Int charIndex(Char8 c)C; // get 'c' character index in the 'chrs' container, -1 if it's not included
  123. Int charIndex(Char c)C; // get 'c' character index in the 'chrs' container, -1 if it's not included
  124. // get character image shadow padding - amount of pixels on each side that is used only for shadows and does not contain the actual font char pixels
  125. Int paddingL()C {return _padd.x;} // get left padding
  126. Int paddingT()C {return _padd.y;} // get top padding
  127. Int paddingR()C {return _padd.z;} // get right padding
  128. Int paddingB()C {return _padd.w;} // get bottom padding
  129. #if !EE_PRIVATE
  130. private:
  131. #endif
  132. Bool _sub_pixel;
  133. Byte _height;
  134. VecB4 _padd;
  135. UShort _char_to_font[ 0x100],
  136. _wide_to_font[0x10000];
  137. Mems<Chr > _chrs;
  138. Mems<Image> _images;
  139. };
  140. /******************************************************************************/
  141. DECLARE_CACHE(Font, Fonts, FontPtr); // 'Fonts' cache storing 'Font' objects which can be accessed by 'FontPtr' pointer
  142. /******************************************************************************/
  143. #if EE_PRIVATE
  144. void ShutFont();
  145. #endif
  146. /******************************************************************************/