font.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*************************************************************************/
  2. /* font.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "font.h"
  30. #include "core/os/file_access.h"
  31. #include "core/io/resource_loader.h"
  32. void Font::_set_chars(const DVector<int>& p_chars) {
  33. int len = p_chars.size();
  34. //char 1 charsize 1 texture, 4 rect, 2 align, advance 1
  35. ERR_FAIL_COND(len%9);
  36. if (!len)
  37. return; //none to do
  38. int chars = len/9;
  39. DVector<int>::Read r=p_chars.read();
  40. for(int i=0;i<chars;i++) {
  41. const int* data = &r[i*9];
  42. add_char(data[0],data[1],Rect2(data[2],data[3],data[4],data[5]), Size2(data[6],data[7]),data[8]);
  43. }
  44. }
  45. DVector<int> Font::_get_chars() const {
  46. DVector<int> chars;
  47. const CharType* key=NULL;
  48. while((key=char_map.next(key))) {
  49. const Character *c=char_map.getptr(*key);
  50. chars.push_back(*key);
  51. chars.push_back(c->texture_idx);
  52. chars.push_back(c->rect.pos.x);
  53. chars.push_back(c->rect.pos.y);
  54. chars.push_back(c->rect.size.x);
  55. chars.push_back(c->rect.size.y);
  56. chars.push_back(c->h_align);
  57. chars.push_back(c->v_align);
  58. chars.push_back(c->advance);
  59. }
  60. return chars;
  61. }
  62. void Font::_set_kernings(const DVector<int>& p_kernings) {
  63. int len=p_kernings.size();
  64. ERR_FAIL_COND(len%3);
  65. if (!len)
  66. return;
  67. DVector<int>::Read r=p_kernings.read();
  68. for(int i=0;i<len/3;i++) {
  69. const int* data = &r[i*3];
  70. add_kerning_pair(data[0],data[1],data[2]);
  71. }
  72. }
  73. DVector<int> Font::_get_kernings() const {
  74. DVector<int> kernings;
  75. for(Map<KerningPairKey,int>::Element *E=kerning_map.front();E;E=E->next()) {
  76. kernings.push_back(E->key().A);
  77. kernings.push_back(E->key().B);
  78. kernings.push_back(E->get());
  79. }
  80. return kernings;
  81. }
  82. void Font::_set_textures(const Vector<Variant> & p_textures) {
  83. for(int i=0;i<p_textures.size();i++) {
  84. Ref<Texture> tex = p_textures[i];
  85. ERR_CONTINUE(!tex.is_valid());
  86. add_texture(tex);
  87. }
  88. }
  89. Vector<Variant> Font::_get_textures() const {
  90. Vector<Variant> rtextures;
  91. for(int i=0;i<textures.size();i++)
  92. rtextures.push_back(textures[i].get_ref_ptr());
  93. return rtextures;
  94. }
  95. Error Font::create_from_fnt(const String& p_string) {
  96. //fnt format used by angelcode bmfont
  97. //http://www.angelcode.com/products/bmfont/
  98. FileAccess *f = FileAccess::open(p_string,FileAccess::READ);
  99. if (!f) {
  100. ERR_EXPLAIN("Can't open font: "+p_string);
  101. ERR_FAIL_V(ERR_FILE_NOT_FOUND);
  102. }
  103. clear();
  104. while(true) {
  105. String line=f->get_line();
  106. int delimiter=line.find(" ");
  107. String type=line.substr(0,delimiter);
  108. int pos = delimiter+1;
  109. Map<String,String> keys;
  110. while (pos < line.size() && line[pos]==' ')
  111. pos++;
  112. while(pos<line.size()) {
  113. int eq = line.find("=",pos);
  114. if (eq==-1)
  115. break;
  116. String key=line.substr(pos,eq-pos);
  117. int end=-1;
  118. String value;
  119. if (line[eq+1]=='"') {
  120. end=line.find("\"",eq+2);
  121. if (end==-1)
  122. break;
  123. value=line.substr(eq+2,end-1-eq-1);
  124. pos=end+1;
  125. } else {
  126. end=line.find(" ",eq+1);
  127. if (end==-1)
  128. end=line.size();
  129. value=line.substr(eq+1,end-eq);
  130. pos=end;
  131. }
  132. while (pos<line.size() && line[pos]==' ')
  133. pos++;
  134. keys[key]=value;
  135. }
  136. if (type=="info") {
  137. if (keys.has("face"))
  138. set_name(keys["face"]);
  139. //if (keys.has("size"))
  140. // font->set_height(keys["size"].to_int());
  141. } else if (type=="common") {
  142. if (keys.has("lineHeight"))
  143. set_height(keys["lineHeight"].to_int());
  144. if (keys.has("base"))
  145. set_ascent(keys["base"].to_int());
  146. } else if (type=="page") {
  147. if (keys.has("file")) {
  148. String file = keys["file"];
  149. file=p_string.get_base_dir()+"/"+file;
  150. Ref<Texture> tex = ResourceLoader::load(file);
  151. if (tex.is_null()) {
  152. ERR_PRINT("Can't load font texture!");
  153. } else {
  154. add_texture(tex);
  155. }
  156. }
  157. } else if (type=="char") {
  158. CharType idx=0;
  159. if (keys.has("id"))
  160. idx=keys["id"].to_int();
  161. Rect2 rect;
  162. if (keys.has("x"))
  163. rect.pos.x=keys["x"].to_int();
  164. if (keys.has("y"))
  165. rect.pos.y=keys["y"].to_int();
  166. if (keys.has("width"))
  167. rect.size.width=keys["width"].to_int();
  168. if (keys.has("height"))
  169. rect.size.height=keys["height"].to_int();
  170. Point2 ofs;
  171. if (keys.has("xoffset"))
  172. ofs.x=keys["xoffset"].to_int();
  173. if (keys.has("yoffset"))
  174. ofs.y=keys["yoffset"].to_int();
  175. int texture=0;
  176. if (keys.has("page"))
  177. texture=keys["page"].to_int();
  178. int advance=-1;
  179. if (keys.has("xadvance"))
  180. advance=keys["xadvance"].to_int();
  181. add_char(idx,texture,rect,ofs,advance);
  182. } else if (type=="kerning") {
  183. CharType first=0,second=0;
  184. int k=0;
  185. if (keys.has("first"))
  186. first=keys["first"].to_int();
  187. if (keys.has("second"))
  188. second=keys["second"].to_int();
  189. if (keys.has("amount"))
  190. k=keys["amount"].to_int();
  191. add_kerning_pair(first,second,-k);
  192. }
  193. if (f->eof_reached())
  194. break;
  195. }
  196. memdelete(f);
  197. return OK;
  198. }
  199. void Font::set_height(float p_height) {
  200. height=p_height;
  201. }
  202. float Font::get_height() const{
  203. return height;
  204. }
  205. void Font::set_ascent(float p_ascent){
  206. ascent=p_ascent;
  207. }
  208. float Font::get_ascent() const {
  209. return ascent;
  210. }
  211. float Font::get_descent() const {
  212. return height-ascent;
  213. }
  214. void Font::add_texture(const Ref<Texture>& p_texture) {
  215. ERR_FAIL_COND( p_texture.is_null());
  216. textures.push_back( p_texture );
  217. }
  218. int Font::get_texture_count() const {
  219. return textures.size();
  220. };
  221. Ref<Texture> Font::get_texture(int p_idx) const {
  222. ERR_FAIL_INDEX_V(p_idx, textures.size(), Ref<Texture>());
  223. return textures[p_idx];
  224. };
  225. int Font::get_character_count() const {
  226. return char_map.size();
  227. };
  228. Vector<CharType> Font::get_char_keys() const {
  229. Vector<CharType> chars;
  230. chars.resize(char_map.size());
  231. const CharType* ct = NULL;
  232. int count = 0;
  233. while ( (ct = char_map.next(ct)) ) {
  234. chars[count++] = *ct;
  235. };
  236. return chars;
  237. };
  238. Font::Character Font::get_character(CharType p_char) const {
  239. if (!char_map.has(p_char)) {
  240. ERR_FAIL_V(Character());
  241. };
  242. return char_map[p_char];
  243. };
  244. void Font::add_char(CharType p_char, int p_texture_idx, const Rect2& p_rect, const Size2& p_align, float p_advance) {
  245. if (p_advance<0)
  246. p_advance=p_rect.size.width;
  247. Character c;
  248. c.rect=p_rect;
  249. c.texture_idx=p_texture_idx;
  250. c.v_align=p_align.y;
  251. c.advance=p_advance;
  252. c.h_align=p_align.x;
  253. char_map[p_char]=c;
  254. }
  255. void Font::add_kerning_pair(CharType p_A,CharType p_B,int p_kerning) {
  256. KerningPairKey kpk;
  257. kpk.A=p_A;
  258. kpk.B=p_B;
  259. if (p_kerning==0 && kerning_map.has(kpk)) {
  260. kerning_map.erase(kpk);
  261. } else {
  262. kerning_map[kpk]=p_kerning;
  263. }
  264. }
  265. Vector<Font::KerningPairKey> Font::get_kerning_pair_keys() const {
  266. Vector<Font::KerningPairKey> ret;
  267. ret.resize(kerning_map.size());
  268. int i=0;
  269. for (Map<KerningPairKey,int>::Element *E=kerning_map.front();E;E=E->next()) {
  270. ret[i++]=E->key();
  271. }
  272. return ret;
  273. }
  274. int Font::get_kerning_pair(CharType p_A,CharType p_B) const {
  275. KerningPairKey kpk;
  276. kpk.A=p_A;
  277. kpk.B=p_B;
  278. const Map<KerningPairKey,int>::Element *E=kerning_map.find(kpk);
  279. if (E)
  280. return E->get();
  281. return 0;
  282. }
  283. void Font::clear() {
  284. height=1;
  285. ascent=0;
  286. char_map.clear();
  287. textures.clear();
  288. kerning_map.clear();
  289. }
  290. Size2 Font::get_string_size(const String& p_string) const {
  291. float w=0;
  292. int l = p_string.length();
  293. if (l==0)
  294. return Size2(0,height);
  295. const CharType *sptr = &p_string[0];
  296. for (int i=0;i<l;i++) {
  297. w+=get_char_size(sptr[i],sptr[i+1]).width;
  298. }
  299. return Size2(w,height);
  300. }
  301. void Font::draw_halign(RID p_canvas_item, const Point2& p_pos, HAlign p_align,float p_width,const String& p_text,const Color& p_modulate) const {
  302. float length=get_string_size(p_text).width;
  303. if (length>=p_width) {
  304. draw(p_canvas_item,p_pos,p_text,p_modulate,p_width);
  305. return;
  306. }
  307. float ofs;
  308. switch(p_align) {
  309. case HALIGN_LEFT: {
  310. ofs=0;
  311. } break;
  312. case HALIGN_CENTER: {
  313. ofs = Math::floor( (p_width-length) / 2.0 );
  314. } break;
  315. case HALIGN_RIGHT: {
  316. ofs=p_width-length;
  317. } break;
  318. }
  319. draw(p_canvas_item,p_pos+Point2(ofs,0),p_text,p_modulate,p_width);
  320. }
  321. void Font::draw(RID p_canvas_item, const Point2& p_pos, const String& p_text, const Color& p_modulate,int p_clip_w) const {
  322. Point2 pos=p_pos;
  323. float ofs=0;
  324. VisualServer *vs = VisualServer::get_singleton();
  325. for (int i=0;i<p_text.length();i++) {
  326. const Character * c = char_map.getptr(p_text[i]);
  327. if (!c)
  328. continue;
  329. // if (p_clip_w>=0 && (ofs+c->rect.size.width)>(p_clip_w))
  330. // break; //width exceeded
  331. if (p_clip_w>=0 && (ofs+c->rect.size.width)>p_clip_w)
  332. break; //clip
  333. Point2 cpos=pos;
  334. cpos.x+=ofs+c->h_align;
  335. cpos.y-=ascent;
  336. cpos.y+=c->v_align;
  337. ERR_CONTINUE( c->texture_idx<-1 || c->texture_idx>=textures.size());
  338. if (c->texture_idx!=-1)
  339. textures[c->texture_idx]->draw_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), c->rect, p_modulate );
  340. ofs+=get_char_size(p_text[i],p_text[i+1]).width;
  341. }
  342. }
  343. float Font::draw_char(RID p_canvas_item, const Point2& p_pos, const CharType& p_char,const CharType& p_next,const Color& p_modulate) const {
  344. const Character * c = char_map.getptr(p_char);
  345. if (!c)
  346. return 0;
  347. Point2 cpos=p_pos;
  348. cpos.x+=c->h_align;
  349. cpos.y-=ascent;
  350. cpos.y+=c->v_align;
  351. ERR_FAIL_COND_V( c->texture_idx<-1 || c->texture_idx>=textures.size(),0);
  352. if (c->texture_idx!=-1)
  353. VisualServer::get_singleton()->canvas_item_add_texture_rect_region( p_canvas_item, Rect2( cpos, c->rect.size ), textures[c->texture_idx]->get_rid(),c->rect, p_modulate );
  354. return get_char_size(p_char,p_next).width;
  355. }
  356. void Font::_bind_methods() {
  357. ObjectTypeDB::bind_method(_MD("set_height","px"),&Font::set_height);
  358. ObjectTypeDB::bind_method(_MD("get_height"),&Font::get_height);
  359. ObjectTypeDB::bind_method(_MD("set_ascent","px"),&Font::set_ascent);
  360. ObjectTypeDB::bind_method(_MD("get_ascent"),&Font::get_ascent);
  361. ObjectTypeDB::bind_method(_MD("get_descent"),&Font::get_descent);
  362. ObjectTypeDB::bind_method(_MD("add_kerning_pair","char_a","char_b","kerning"),&Font::add_kerning_pair);
  363. ObjectTypeDB::bind_method(_MD("get_kerning_pair"),&Font::get_kerning_pair);
  364. ObjectTypeDB::bind_method(_MD("add_texture","texture:Texture"),&Font::add_texture);
  365. ObjectTypeDB::bind_method(_MD("add_char","character","texture","rect","align","advance"),&Font::add_char,DEFVAL(Point2()),DEFVAL(-1));
  366. ObjectTypeDB::bind_method(_MD("get_char_size","char","next"),&Font::get_char_size,DEFVAL(0));
  367. ObjectTypeDB::bind_method(_MD("get_string_size","string"),&Font::get_string_size);
  368. ObjectTypeDB::bind_method(_MD("clear"),&Font::clear);
  369. ObjectTypeDB::bind_method(_MD("draw","canvas_item","pos","string","modulate","clip_w"),&Font::draw,DEFVAL(Color(1,1,1)),DEFVAL(-1));
  370. ObjectTypeDB::bind_method(_MD("draw_char","canvas_item","pos","char","next","modulate"),&Font::draw_char,DEFVAL(-1),DEFVAL(Color(1,1,1)));
  371. ObjectTypeDB::bind_method(_MD("_set_chars"),&Font::_set_chars);
  372. ObjectTypeDB::bind_method(_MD("_get_chars"),&Font::_get_chars);
  373. ObjectTypeDB::bind_method(_MD("_set_kernings"),&Font::_set_kernings);
  374. ObjectTypeDB::bind_method(_MD("_get_kernings"),&Font::_get_kernings);
  375. ObjectTypeDB::bind_method(_MD("_set_textures"),&Font::_set_textures);
  376. ObjectTypeDB::bind_method(_MD("_get_textures"),&Font::_get_textures);
  377. ADD_PROPERTY( PropertyInfo( Variant::ARRAY, "textures", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_textures"), _SCS("_get_textures") );
  378. ADD_PROPERTY( PropertyInfo( Variant::INT_ARRAY, "chars", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_chars"), _SCS("_get_chars") );
  379. ADD_PROPERTY( PropertyInfo( Variant::INT_ARRAY, "kernings", PROPERTY_HINT_NONE,"", PROPERTY_USAGE_NOEDITOR ), _SCS("_set_kernings"), _SCS("_get_kernings") );
  380. ADD_PROPERTY( PropertyInfo( Variant::REAL, "height", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_height"), _SCS("get_height") );
  381. ADD_PROPERTY( PropertyInfo( Variant::REAL, "ascent", PROPERTY_HINT_RANGE,"-1024,1024,1" ), _SCS("set_ascent"), _SCS("get_ascent") );
  382. }
  383. Font::Font() {
  384. clear();
  385. }
  386. Font::~Font() {
  387. clear();
  388. }