cairo_font.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. #include "cairo_font.h"
  2. cairo_font::cairo_font(IMLangFontLink2* fl, HFONT hFont, int size )
  3. {
  4. init();
  5. m_font_link = fl;
  6. if(m_font_link)
  7. {
  8. m_font_link->AddRef();
  9. }
  10. m_size = size;
  11. set_font(hFont);
  12. }
  13. cairo_font::cairo_font(IMLangFontLink2* fl, LPCWSTR facename, int size, int weight, BOOL italic, BOOL strikeout, BOOL underline )
  14. {
  15. init();
  16. m_size = size;
  17. m_font_link = fl;
  18. if(m_font_link)
  19. {
  20. m_font_link->AddRef();
  21. }
  22. LOGFONT lf;
  23. ZeroMemory(&lf, sizeof(lf));
  24. if(!lstrcmpi(facename, L"monospace"))
  25. {
  26. wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Courier New");
  27. } else if(!lstrcmpi(facename, L"serif"))
  28. {
  29. wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Times New Roman");
  30. } else if(!lstrcmpi(facename, L"sans-serif"))
  31. {
  32. wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Arial");
  33. } else if(!lstrcmpi(facename, L"fantasy"))
  34. {
  35. wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Impact");
  36. } else if(!lstrcmpi(facename, L"cursive"))
  37. {
  38. wcscpy_s(lf.lfFaceName, LF_FACESIZE, L"Comic Sans MS");
  39. } else
  40. {
  41. wcscpy_s(lf.lfFaceName, LF_FACESIZE, facename);
  42. }
  43. lf.lfHeight = -size;
  44. lf.lfWeight = weight;
  45. lf.lfItalic = italic;
  46. lf.lfCharSet = DEFAULT_CHARSET;
  47. lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  48. lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  49. lf.lfQuality = DEFAULT_QUALITY;
  50. lf.lfStrikeOut = strikeout;
  51. lf.lfUnderline = underline;
  52. HFONT fnt = CreateFontIndirect(&lf);
  53. set_font(fnt);
  54. }
  55. cairo_font::~cairo_font()
  56. {
  57. if(m_font_face)
  58. {
  59. cairo_font_face_destroy(m_font_face);
  60. }
  61. for(size_t i = 0; i < m_linked_fonts.size(); i++)
  62. {
  63. if(m_linked_fonts[i]->hFont)
  64. {
  65. m_font_link->ReleaseFont(m_linked_fonts[i]->hFont);
  66. }
  67. if(m_linked_fonts[i]->font_face)
  68. {
  69. cairo_font_face_destroy(m_linked_fonts[i]->font_face);
  70. }
  71. }
  72. m_linked_fonts.clear();
  73. if(m_font_link)
  74. {
  75. m_font_link->AddRef();
  76. }
  77. if(m_hFont)
  78. {
  79. DeleteObject(m_hFont);
  80. }
  81. }
  82. void cairo_font::show_text( cairo_t* cr, int x, int y, const litehtml::tchar_t* str )
  83. {
  84. lock();
  85. text_chunk::vector chunks;
  86. split_text(str, chunks);
  87. cairo_set_font_size(cr, m_size);
  88. cairo_move_to(cr, x, y);
  89. for(size_t i = 0; i < chunks.size(); i++)
  90. {
  91. if(chunks[i]->font)
  92. {
  93. cairo_set_font_face(cr, chunks[i]->font->font_face);
  94. } else
  95. {
  96. cairo_set_font_face(cr, m_font_face);
  97. }
  98. cairo_show_text(cr, chunks[i]->text);
  99. }
  100. unlock();
  101. if(m_bUnderline)
  102. {
  103. int tw = text_width(cr, chunks);
  104. lock();
  105. cairo_set_line_width(cr, 1);
  106. cairo_move_to(cr, x, y + 1.5);
  107. cairo_line_to(cr, x + tw, y + 1.5);
  108. cairo_stroke(cr);
  109. unlock();
  110. }
  111. if(m_bStrikeOut)
  112. {
  113. int tw = text_width(cr, chunks);
  114. cairo_font_metrics fm;
  115. get_metrics(cr, &fm);
  116. int ln_y = y - fm.x_height / 2;
  117. lock();
  118. cairo_set_line_width(cr, 1);
  119. cairo_move_to(cr, x, (double) ln_y - 0.5);
  120. cairo_line_to(cr, x + tw, (double) ln_y - 0.5);
  121. cairo_stroke(cr);
  122. unlock();
  123. }
  124. free_text_chunks(chunks);
  125. }
  126. void cairo_font::split_text( const litehtml::tchar_t* src, text_chunk::vector& chunks )
  127. {
  128. wchar_t* str;
  129. #ifdef LITEHTML_UTF8
  130. str = cairo_font::utf8_to_wchar(src);
  131. wchar_t* str_start = str;
  132. #else
  133. str = (wchar_t*) src;
  134. #endif
  135. int cch = lstrlen(str);
  136. HDC hdc = GetDC(NULL);
  137. SelectObject(hdc, m_hFont);
  138. HRESULT hr = S_OK;
  139. while(cch > 0)
  140. {
  141. DWORD dwActualCodePages;
  142. long cchActual;
  143. if(m_font_link)
  144. {
  145. hr = m_font_link->GetStrCodePages(str, cch, m_font_code_pages, &dwActualCodePages, &cchActual);
  146. } else
  147. {
  148. hr = S_FALSE;
  149. }
  150. if(hr != S_OK)
  151. {
  152. break;
  153. }
  154. text_chunk* chk = new text_chunk;
  155. int sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, 0, NULL, NULL) + 1;
  156. chk->text = new CHAR[sz];
  157. sz = WideCharToMultiByte(CP_UTF8, 0, str, cchActual, chk->text, sz, NULL, NULL);
  158. chk->text[sz] = 0;
  159. chk->font = NULL;
  160. if(!(dwActualCodePages & m_font_code_pages))
  161. {
  162. for(linked_font::vector::iterator i = m_linked_fonts.begin(); i != m_linked_fonts.end(); i++)
  163. {
  164. if((*i)->code_pages == dwActualCodePages)
  165. {
  166. chk->font = (*i);
  167. break;
  168. }
  169. }
  170. if(!chk->font)
  171. {
  172. linked_font* lkf = new linked_font;
  173. lkf->code_pages = dwActualCodePages;
  174. lkf->hFont = NULL;
  175. m_font_link->MapFont(hdc, dwActualCodePages, 0, &lkf->hFont);
  176. if (lkf->hFont)
  177. {
  178. lkf->font_face = create_font_face(lkf->hFont);
  179. m_linked_fonts.push_back(lkf);
  180. }
  181. else
  182. {
  183. delete lkf;
  184. lkf = NULL;
  185. }
  186. chk->font = lkf;
  187. }
  188. }
  189. chunks.push_back(chk);
  190. cch -= cchActual;
  191. str += cchActual;
  192. }
  193. if(hr != S_OK)
  194. {
  195. text_chunk* chk = new text_chunk;
  196. int sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, 0, NULL, NULL) + 1;
  197. chk->text = new CHAR[sz];
  198. sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, chk->text, sz, NULL, NULL);
  199. chk->text[sz] = 0;
  200. chk->font = NULL;
  201. chunks.push_back(chk);
  202. }
  203. ReleaseDC(NULL, hdc);
  204. #ifdef LITEHTML_UTF8
  205. delete str_start;
  206. #endif
  207. }
  208. void cairo_font::free_text_chunks( text_chunk::vector& chunks )
  209. {
  210. for(size_t i = 0; i < chunks.size(); i++)
  211. {
  212. delete chunks[i];
  213. }
  214. chunks.clear();
  215. }
  216. cairo_font_face_t* cairo_font::create_font_face( HFONT fnt )
  217. {
  218. LOGFONT lf;
  219. GetObject(fnt, sizeof(LOGFONT), &lf);
  220. return cairo_win32_font_face_create_for_logfontw(&lf);
  221. }
  222. int cairo_font::text_width( cairo_t* cr, const litehtml::tchar_t* str )
  223. {
  224. text_chunk::vector chunks;
  225. split_text(str, chunks);
  226. int ret = text_width(cr, chunks);
  227. free_text_chunks(chunks);
  228. return (int) ret;
  229. }
  230. int cairo_font::text_width( cairo_t* cr, text_chunk::vector& chunks )
  231. {
  232. lock();
  233. cairo_set_font_size(cr, m_size);
  234. double ret = 0;
  235. for(size_t i = 0; i < chunks.size(); i++)
  236. {
  237. if(chunks[i]->font)
  238. {
  239. cairo_set_font_face(cr, chunks[i]->font->font_face);
  240. } else
  241. {
  242. cairo_set_font_face(cr, m_font_face);
  243. }
  244. cairo_text_extents_t ext;
  245. cairo_text_extents(cr, chunks[i]->text, &ext);
  246. ret += ext.x_advance;
  247. }
  248. unlock();
  249. return (int) ret;
  250. }
  251. void cairo_font::get_metrics(cairo_t* cr, cairo_font_metrics* fm )
  252. {
  253. lock();
  254. cairo_set_font_face(cr, m_font_face);
  255. cairo_set_font_size(cr, m_size);
  256. cairo_font_extents_t ext;
  257. cairo_font_extents(cr, &ext);
  258. cairo_text_extents_t tex;
  259. cairo_text_extents(cr, "x", &tex);
  260. fm->ascent = (int) ext.ascent;
  261. fm->descent = (int) ext.descent;
  262. fm->height = (int) (ext.ascent + ext.descent);
  263. fm->x_height = (int) tex.height;
  264. unlock();
  265. }
  266. void cairo_font::set_font( HFONT hFont )
  267. {
  268. clear();
  269. m_hFont = hFont;
  270. m_font_face = create_font_face(m_hFont);
  271. m_font_code_pages = 0;
  272. if(m_font_link)
  273. {
  274. HDC hdc = GetDC(NULL);
  275. SelectObject(hdc, m_hFont);
  276. m_font_link->GetFontCodePages(hdc, m_hFont, &m_font_code_pages);
  277. ReleaseDC(NULL, hdc);
  278. }
  279. LOGFONT lf;
  280. GetObject(m_hFont, sizeof(LOGFONT), &lf);
  281. m_bUnderline = lf.lfUnderline;
  282. m_bStrikeOut = lf.lfStrikeOut;
  283. }
  284. void cairo_font::clear()
  285. {
  286. if(m_font_face)
  287. {
  288. cairo_font_face_destroy(m_font_face);
  289. m_font_face = NULL;
  290. }
  291. for(size_t i = 0; i < m_linked_fonts.size(); i++)
  292. {
  293. if(m_linked_fonts[i]->hFont && m_font_link)
  294. {
  295. m_font_link->ReleaseFont(m_linked_fonts[i]->hFont);
  296. }
  297. if(m_linked_fonts[i]->font_face)
  298. {
  299. cairo_font_face_destroy(m_linked_fonts[i]->font_face);
  300. }
  301. }
  302. m_linked_fonts.clear();
  303. if(m_hFont)
  304. {
  305. DeleteObject(m_hFont);
  306. m_hFont = NULL;
  307. }
  308. }
  309. void cairo_font::init()
  310. {
  311. m_hFont = NULL;
  312. m_font_face = NULL;
  313. m_font_link = NULL;
  314. m_font_code_pages = 0;
  315. m_size = 0;
  316. m_bUnderline = FALSE;
  317. m_bStrikeOut = FALSE;
  318. }
  319. wchar_t* cairo_font::utf8_to_wchar( const char* src )
  320. {
  321. if(!src) return NULL;
  322. int len = (int) strlen(src);
  323. wchar_t* ret = new wchar_t[len + 1];
  324. MultiByteToWideChar(CP_UTF8, 0, src, -1, ret, len + 1);
  325. return ret;
  326. }
  327. char* cairo_font::wchar_to_utf8( const wchar_t* src )
  328. {
  329. if(!src) return NULL;
  330. int len = WideCharToMultiByte(CP_UTF8, 0, src, -1, NULL, 0, NULL, NULL);
  331. char* ret = new char[len];
  332. WideCharToMultiByte(CP_UTF8, 0, src, -1, ret, len, NULL, NULL);
  333. return ret;
  334. }