Font.hx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package h2d;
  2. /**
  3. A `FontChar` kerning information as well as linked list of kernings. See `FontChar.kerning`.
  4. **/
  5. class Kerning {
  6. /**
  7. A character that should precede current character in order to apply this kerning.
  8. **/
  9. public var prevChar : Int;
  10. /**
  11. A kerning offset between the character pair in pixels.
  12. **/
  13. public var offset : Float;
  14. /**
  15. The next kerning reference.
  16. **/
  17. public var next : Null<Kerning>;
  18. /**
  19. Create a new kerning instance.
  20. @param c The preceding character.
  21. @param o The kerning offset.
  22. **/
  23. public function new(c, o) {
  24. this.prevChar = c;
  25. this.offset = o;
  26. }
  27. }
  28. /**
  29. A single `Font` character descriptor.
  30. **/
  31. class FontChar {
  32. /**
  33. A Tile representing position of a character on the texture.
  34. **/
  35. public var t : h2d.Tile;
  36. /**
  37. Horizontal advance value of the character.
  38. On top of advance, letter spacing is affected by `FontChar.kerning` matches and `Text.letterSpacing`.
  39. **/
  40. public var width : Float;
  41. /**
  42. Linked list of kerning values.
  43. In order to add new kerning values use `FontChar.addKerning` and `FontChar.getKerningOffset` to retrieve kerning offsets.
  44. **/
  45. @:dox(show)
  46. var kerning : Null<Kerning>;
  47. /**
  48. Create a new font character.
  49. @param t The character Tile.
  50. @param width The horizontal advance of the character.
  51. **/
  52. public function new(t,w) {
  53. this.t = t;
  54. this.width = w;
  55. }
  56. /**
  57. Adds a new kerning to the character with specified `prevChar` and `offset`.
  58. **/
  59. public function addKerning( prevChar : Int, offset : Int ) {
  60. var k = new Kerning(prevChar, offset);
  61. k.next = kerning;
  62. kerning = k;
  63. }
  64. /**
  65. Returns kerning offset for a pair `[prevChar, currentChar]` or `0` if there was no paired kerning value.
  66. **/
  67. public function getKerningOffset( prevChar : Int ) {
  68. var k = kerning;
  69. while( k != null ) {
  70. if( k.prevChar == prevChar )
  71. return k.offset;
  72. k = k.next;
  73. }
  74. return 0;
  75. }
  76. /**
  77. Clones the character instance.
  78. **/
  79. public function clone() {
  80. var c = new FontChar(t.clone(), width);
  81. // Clone entire kerning tree in case Font got resized.
  82. var k = kerning;
  83. if ( k != null ) {
  84. var kc = new Kerning(k.prevChar, k.offset);
  85. c.kerning = kc;
  86. k = k.next;
  87. while ( k != null ) {
  88. var kn = new Kerning(k.prevChar, k.offset);
  89. kc = kc.next = kn;
  90. k = k.next;
  91. }
  92. }
  93. return c;
  94. }
  95. }
  96. /**
  97. Channel reading method for `FontType.SignedDistanceField`.
  98. **/
  99. enum abstract SDFChannel(Int) from Int to Int {
  100. /** Use red channel of a texture to determine distance. **/
  101. var Red = 0;
  102. /** Use green channel of a texture to determine distance. **/
  103. var Green = 1;
  104. /** Use blue channel of a texture to determine distance. **/
  105. var Blue = 2;
  106. /** Use alpha channel of a texture to determine distance. **/
  107. var Alpha = 3;
  108. /** Use RGB channels of a texture to determine distance. See here for details: https://github.com/Chlumsky/msdfgen **/
  109. var MultiChannel = 4;
  110. }
  111. /**
  112. The rendering type of the of the `Font` instance.
  113. **/
  114. enum FontType {
  115. /**
  116. A simple raster bitmap font.
  117. **/
  118. BitmapFont;
  119. /**
  120. A Signed Distance Field font data. Each glyph pixel contains the distance to the closest glyph edge instead of actual color.
  121. To render an SDF font, `Text` utilizes `h3d.shader.SignedDistanceField` shader to produce smoothed and scalable text.
  122. Because shader expects texture to use bilinear filtering, Text automatically enables `Drawable.smooth` on itself.
  123. See [Text](https://github.com/HeapsIO/heaps/wiki/Text) manual and [libgdx wiki](https://github.com/libgdx/libgdx/wiki/Distance-field-fonts) for more details.
  124. @param channel The channel that serves as distance data source.
  125. @param alphaCutoff The distance value that is considered to be the edge. Usually should be 0.5.
  126. @param smoothing The smoothing of edge. Lower value lead to sharper edges. Value of -1 sets it to automatic.
  127. **/
  128. SignedDistanceField(channel : SDFChannel, alphaCutoff : Float, smoothing : Float);
  129. }
  130. /**
  131. An instance of a text font.
  132. Heaps comes with a default Font that covers basic ASCII characters, and can be retrieved via `hxd.res.DefaultFont.get()`.
  133. **/
  134. class Font {
  135. /**
  136. The font name. Assigned on font creation and can be used to identify font instances.
  137. **/
  138. public var name(default, null) : String;
  139. /**
  140. Current font size. Font can be resized with `resizeTo`.
  141. **/
  142. public var size(default, null) : Int;
  143. /**
  144. The baseline value of the font represents the base on which characters will sit.
  145. Used primarily with `HtmlText` to sit multiple fonts and images at the same line.
  146. **/
  147. public var baseLine(default, null) : Float;
  148. /**
  149. Font line height provides vertical offset for each new line of the text.
  150. **/
  151. public var lineHeight(default, null) : Float;
  152. /**
  153. Reference to the source Tile containing all glyphs of the Font.
  154. **/
  155. public var tile(default,null) : h2d.Tile;
  156. /**
  157. The resource path of the source Tile. Either relative to .fnt or to resources root.
  158. **/
  159. public var tilePath(default,null) : String;
  160. /**
  161. The font type. BitmapFonts rendered as-is, but SDF fonts will use an extra shader to produce scalable smooth fonts.
  162. See `FontType.SignedDistanceField` for more details.
  163. **/
  164. public var type : FontType;
  165. /**
  166. Font charset allows to resolve specific char codes that are not directly present in glyph map as well as detect spaces.
  167. Defaults to `hxd.Charset.getDefault()`.
  168. **/
  169. public var charset : hxd.Charset;
  170. var glyphs : Map<Int,FontChar>;
  171. var nullChar : FontChar;
  172. var defaultChar : FontChar;
  173. var initSize:Int;
  174. var offsetX:Float = 0;
  175. var offsetY:Float = 0;
  176. /**
  177. Creates an empty font instance with specified parameters.
  178. @param name The name of the font.
  179. @param size Initial size of the font.
  180. @param type The font type.
  181. **/
  182. function new(name : String, size : Int, ?type : FontType) {
  183. this.name = name;
  184. this.size = size;
  185. this.initSize = size;
  186. glyphs = new Map();
  187. defaultChar = nullChar = new FontChar(new Tile(null, 0, 0, 0, 0),0);
  188. charset = hxd.Charset.getDefault();
  189. if (name != null)
  190. this.tilePath = haxe.io.Path.withExtension(name, "png");
  191. if (type == null)
  192. this.type = BitmapFont;
  193. else
  194. this.type = type;
  195. }
  196. /**
  197. Returns a `FontChar` instance corresponding to the `code`.
  198. If font char is not present in glyph list, `charset.resolveChar` is called.
  199. Returns `null` if glyph under specified charcode does not exist.
  200. @param code The charcode to search for.
  201. **/
  202. public inline function getChar( code : Int ) {
  203. var c = glyphs.get(code);
  204. if( c == null ) {
  205. c = charset.resolveChar(code, glyphs);
  206. if( c == null )
  207. c = code == "\r".code || code == "\n".code ? nullChar : defaultChar;
  208. }
  209. return c;
  210. }
  211. /**
  212. Offsets all glyphs by specified amount.
  213. Affects each glyph `Tile.dx` and `Tile.dy`.
  214. @param x The X offset of the glyphs.
  215. @param y The Y offset of the glyphs.
  216. **/
  217. public function setOffset( x : Float, y :Float ) {
  218. var dx = x - offsetX;
  219. var dy = y - offsetY;
  220. if( dx == 0 && dy == 0 ) return;
  221. for( g in glyphs ) {
  222. g.t.dx += dx;
  223. g.t.dy += dy;
  224. }
  225. this.offsetX += dx;
  226. this.offsetY += dy;
  227. }
  228. /**
  229. Creates a copy of the font instance.
  230. **/
  231. public function clone() {
  232. var f = new Font(name, size);
  233. f.baseLine = baseLine;
  234. f.lineHeight = lineHeight;
  235. f.tile = tile.clone();
  236. f.charset = charset;
  237. f.defaultChar = defaultChar.clone();
  238. f.type = type;
  239. f.offsetX = offsetX;
  240. f.offsetY = offsetY;
  241. for( g in glyphs.keys() ) {
  242. var c = glyphs.get(g);
  243. var c2 = c.clone();
  244. if( c == defaultChar )
  245. f.defaultChar = c2;
  246. f.glyphs.set(g, c2);
  247. }
  248. return f;
  249. }
  250. /**
  251. Resizes the Font instance to specified size.
  252. For BitmapFonts it can be used to create smoother fonts by rasterizing them with double size while still keeping the original glyph size by downscaling the font.
  253. And SDF fonts can be resized to arbitrary sizes to produce scalable fonts of any size.
  254. @param size The new font size.
  255. **/
  256. public function resizeTo( size : Int ) {
  257. var ratio = size / initSize;
  258. for( c in glyphs ) {
  259. c.width *= ratio;
  260. c.t.scaleToSize(c.t.width * ratio, c.t.height * ratio);
  261. c.t.dx *= ratio;
  262. c.t.dy *= ratio;
  263. var k = @:privateAccess c.kerning;
  264. while ( k != null ) {
  265. k.offset *= ratio;
  266. k = k.next;
  267. }
  268. }
  269. lineHeight = Math.ceil(lineHeight * ratio);
  270. baseLine = Math.ceil(baseLine * ratio);
  271. this.size = size;
  272. }
  273. /**
  274. Checks if character is present in glyph list.
  275. Compared to `getChar` does not check if it exists through `Font.charset`.
  276. @param code The charcode to look up.
  277. **/
  278. public function hasChar( code : Int ) : Bool {
  279. return glyphs.get(code) != null;
  280. }
  281. /**
  282. Disposes of the Font instance. Equivalent to `Tile.dispose`.
  283. **/
  284. public function dispose() {
  285. tile.dispose();
  286. }
  287. /**
  288. Calculate a baseLine default value based on available glyphs.
  289. */
  290. public function calcBaseLine() {
  291. var padding : Float = 0;
  292. var space = glyphs.get(" ".code);
  293. if( space != null )
  294. padding = (space.t.height * .5);
  295. var a = glyphs.get("A".code);
  296. if( a == null )
  297. a = glyphs.get("a".code);
  298. if( a == null )
  299. a = glyphs.get("0".code); // numerical only
  300. if( a == null )
  301. return lineHeight - 2 - padding;
  302. return a.t.dy + a.t.height - padding;
  303. }
  304. }