OpenSDE Packages Database (without history before r20070)
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.

2811 lines
76 KiB

  1. # --- SDE-COPYRIGHT-NOTE-BEGIN ---
  2. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  3. #
  4. # Filename: package/.../sdlpango/SDL_Pango_0.1.2-API-adds.patch
  5. # Copyright (C) 2006 The OpenSDE Project
  6. #
  7. # More information can be found in the files COPYING and README.
  8. #
  9. # This patch file is dual-licensed. It is available under the license the
  10. # patched project is licensed under, as long as it is an OpenSource license
  11. # as defined at http://www.opensource.org/ (e.g. BSD, X11) or under the terms
  12. # of the GNU General Public License as published by the Free Software
  13. # Foundation; either version 2 of the License, or (at your option) any later
  14. # version.
  15. # --- SDE-COPYRIGHT-NOTE-END ---
  16. --- SDL_Pango-0.1.2/src/SDL_Pango.h.original 2006-12-13 15:36:50.000000000 +0100
  17. +++ SDL_Pango-0.1.2/src/SDL_Pango.h 2006-12-13 16:16:07.000000000 +0100
  18. @@ -1,205 +1,219 @@
  19. -/* SDL_Pango.h -- A companion library to SDL for working with Pango.
  20. - Copyright (C) 2004 NAKAMURA Ken'ichi
  21. -
  22. - This library is free software; you can redistribute it and/or
  23. - modify it under the terms of the GNU Lesser General Public
  24. - License as published by the Free Software Foundation; either
  25. - version 2.1 of the License, or (at your option) any later version.
  26. -
  27. - This library is distributed in the hope that it will be useful,
  28. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. - Lesser General Public License for more details.
  31. -
  32. - You should have received a copy of the GNU Lesser General Public
  33. - License along with this library; if not, write to the Free Software
  34. - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  35. -*/
  36. -
  37. -/*! @file
  38. - @brief Header file of SDL_Pango
  39. -
  40. - @author NAKAMURA Ken'ichi
  41. - @date 2004/08/26
  42. - $Revision: 1.3 $
  43. -*/
  44. -
  45. -#ifndef SDL_PANGO_H
  46. -#define SDL_PANGO_H
  47. -
  48. -#include "SDL.h"
  49. -
  50. -#include "begin_code.h"
  51. -
  52. -#ifdef __cplusplus
  53. -extern "C" {
  54. -#endif
  55. -
  56. -
  57. -
  58. -typedef struct _contextImpl SDLPango_Context;
  59. -
  60. -/*!
  61. - General 4 X 4 matrix struct.
  62. -*/
  63. -typedef struct _SDLPango_Matrix {
  64. - Uint8 m[4][4]; /*! Matrix variables */
  65. -} SDLPango_Matrix;
  66. -
  67. -const SDLPango_Matrix _MATRIX_WHITE_BACK
  68. - = {255, 0, 0, 0,
  69. - 255, 0, 0, 0,
  70. - 255, 0, 0, 0,
  71. - 255, 255, 0, 0,};
  72. -
  73. -/*!
  74. - Specifies white back and black letter.
  75. -*/
  76. -const SDLPango_Matrix *MATRIX_WHITE_BACK = &_MATRIX_WHITE_BACK;
  77. -
  78. -const SDLPango_Matrix _MATRIX_BLACK_BACK
  79. - = {0, 255, 0, 0,
  80. - 0, 255, 0, 0,
  81. - 0, 255, 0, 0,
  82. - 255, 255, 0, 0,};
  83. -/*!
  84. - Specifies black back and white letter.
  85. -*/
  86. -const SDLPango_Matrix *MATRIX_BLACK_BACK = &_MATRIX_BLACK_BACK;
  87. -
  88. -const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_BLACK_LETTER
  89. - = {0, 0, 0, 0,
  90. - 0, 0, 0, 0,
  91. - 0, 0, 0, 0,
  92. - 0, 255, 0, 0,};
  93. -/*!
  94. - Specifies transparent back and black letter.
  95. -*/
  96. -const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_BLACK_LETTER = &_MATRIX_TRANSPARENT_BACK_BLACK_LETTER;
  97. -
  98. -const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_WHITE_LETTER
  99. - = {255, 255, 0, 0,
  100. - 255, 255, 0, 0,
  101. - 255, 255, 0, 0,
  102. - 0, 255, 0, 0,};
  103. -/*!
  104. - Specifies transparent back and white letter.
  105. -*/
  106. -const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_WHITE_LETTER = &_MATRIX_TRANSPARENT_BACK_WHITE_LETTER;
  107. -
  108. -const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER
  109. - = {255, 255, 0, 0,
  110. - 255, 255, 0, 0,
  111. - 255, 255, 0, 0,
  112. - 0, 0, 0, 0,};
  113. -/*!
  114. - Specifies transparent back and transparent letter.
  115. - This is useful for KARAOKE like rendering.
  116. -*/
  117. -const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER = &_MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER;
  118. -
  119. -/*!
  120. - Specifies direction of text. See Pango reference for detail
  121. -*/
  122. -typedef enum {
  123. - SDLPANGO_DIRECTION_LTR, /*! Left to right */
  124. - SDLPANGO_DIRECTION_RTL, /*! Right to left */
  125. - SDLPANGO_DIRECTION_WEAK_LTR, /*! Left to right (weak) */
  126. - SDLPANGO_DIRECTION_WEAK_RTL, /*! Right to left (weak) */
  127. - SDLPANGO_DIRECTION_NEUTRAL /*! Neutral */
  128. -} SDLPango_Direction;
  129. -
  130. -
  131. -
  132. -extern DECLSPEC int SDLCALL SDLPango_Init();
  133. -
  134. -extern DECLSPEC int SDLCALL SDLPango_WasInit();
  135. -
  136. -extern DECLSPEC SDLPango_Context* SDLCALL SDLPango_CreateContext();
  137. -
  138. -extern DECLSPEC void SDLCALL SDLPango_FreeContext(
  139. - SDLPango_Context *context);
  140. -
  141. -extern DECLSPEC void SDLCALL SDLPango_SetSurfaceCreateArgs(
  142. - SDLPango_Context *context,
  143. - Uint32 flags,
  144. - int depth,
  145. - Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
  146. -
  147. -extern DECLSPEC SDL_Surface * SDLCALL SDLPango_CreateSurfaceDraw(
  148. - SDLPango_Context *context);
  149. -
  150. -extern DECLSPEC void SDLCALL SDLPango_Draw(
  151. - SDLPango_Context *context,
  152. - SDL_Surface *surface,
  153. - int x, int y);
  154. -
  155. -extern DECLSPEC void SDLCALL SDLPango_SetDpi(
  156. - SDLPango_Context *context,
  157. - double dpi_x, double dpi_y);
  158. -
  159. -extern DECLSPEC void SDLCALL SDLPango_SetMinimumSize(
  160. - SDLPango_Context *context,
  161. - int width, int height);
  162. -
  163. -extern DECLSPEC void SDLCALL SDLPango_SetDefaultColor(
  164. - SDLPango_Context *context,
  165. - const SDLPango_Matrix *color_matrix);
  166. -
  167. -extern DECLSPEC int SDLCALL SDLPango_GetLayoutWidth(
  168. - SDLPango_Context *context);
  169. -
  170. -extern DECLSPEC int SDLCALL SDLPango_GetLayoutHeight(
  171. - SDLPango_Context *context);
  172. -
  173. -extern DECLSPEC void SDLCALL SDLPango_SetMarkup(
  174. - SDLPango_Context *context,
  175. - const char *markup,
  176. - int length);
  177. -
  178. -extern DECLSPEC void SDLCALL SDLPango_SetText(
  179. - SDLPango_Context *context,
  180. - const char *markup,
  181. - int length);
  182. -
  183. -extern DECLSPEC void SDLCALL SDLPango_SetLanguage(
  184. - SDLPango_Context *context,
  185. - const char *language_tag);
  186. -
  187. -extern DECLSPEC void SDLCALL SDLPango_SetBaseDirection(
  188. - SDLPango_Context *context,
  189. - SDLPango_Direction direction);
  190. -
  191. -
  192. -#ifdef __FT2_BUILD_UNIX_H__
  193. -
  194. -extern DECLSPEC void SDLCALL SDLPango_CopyFTBitmapToSurface(
  195. - const FT_Bitmap *bitmap,
  196. - SDL_Surface *surface,
  197. - const SDLPango_Matrix *matrix,
  198. - SDL_Rect *rect);
  199. -
  200. -#endif /* __FT2_BUILD_UNIX_H__ */
  201. -
  202. -
  203. -#ifdef __PANGO_H__
  204. -
  205. -extern DECLSPEC PangoFontMap* SDLCALL SDLPango_GetPangoFontMap(
  206. - SDLPango_Context *context);
  207. -
  208. -extern DECLSPEC PangoFontDescription* SDLCALL SDLPango_GetPangoFontDescription(
  209. - SDLPango_Context *context);
  210. -
  211. -extern DECLSPEC PangoLayout* SDLCALL SDLPango_GetPangoLayout(
  212. - SDLPango_Context *context);
  213. -
  214. -#endif /* __PANGO_H__ */
  215. -
  216. -
  217. -#ifdef __cplusplus
  218. -}
  219. -#endif
  220. -
  221. -#include "close_code.h"
  222. -
  223. -#endif /* SDL_PANGO_H */
  224. +/* SDL_Pango.h -- A companion library to SDL for working with Pango.
  225. + Copyright (C) 2004 NAKAMURA Ken'ichi
  226. +
  227. + This library is free software; you can redistribute it and/or
  228. + modify it under the terms of the GNU Lesser General Public
  229. + License as published by the Free Software Foundation; either
  230. + version 2.1 of the License, or (at your option) any later version.
  231. +
  232. + This library is distributed in the hope that it will be useful,
  233. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  234. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  235. + Lesser General Public License for more details.
  236. +
  237. + You should have received a copy of the GNU Lesser General Public
  238. + License along with this library; if not, write to the Free Software
  239. + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  240. +*/
  241. +
  242. +/*! @file
  243. + @brief Header file of SDL_Pango
  244. +
  245. + @author NAKAMURA Ken'ichi
  246. + @date 2004/08/26
  247. + $Revision: 1.3 $
  248. +*/
  249. +
  250. +#ifndef SDL_PANGO_H
  251. +#define SDL_PANGO_H
  252. +
  253. +#include "SDL.h"
  254. +
  255. +#include "begin_code.h"
  256. +
  257. +#ifdef __cplusplus
  258. +extern "C" {
  259. +#endif
  260. +
  261. +
  262. +
  263. +typedef struct _contextImpl SDLPango_Context;
  264. +
  265. +/*!
  266. + General 4 X 4 matrix struct.
  267. +*/
  268. +typedef struct _SDLPango_Matrix {
  269. + Uint8 m[4][4]; /*! Matrix variables */
  270. +} SDLPango_Matrix;
  271. +
  272. +const SDLPango_Matrix _MATRIX_WHITE_BACK
  273. + = {255, 0, 0, 0,
  274. + 255, 0, 0, 0,
  275. + 255, 0, 0, 0,
  276. + 255, 255, 0, 0,};
  277. +
  278. +/*!
  279. + Specifies white back and black letter.
  280. +*/
  281. +const SDLPango_Matrix *MATRIX_WHITE_BACK = &_MATRIX_WHITE_BACK;
  282. +
  283. +const SDLPango_Matrix _MATRIX_BLACK_BACK
  284. + = {0, 255, 0, 0,
  285. + 0, 255, 0, 0,
  286. + 0, 255, 0, 0,
  287. + 255, 255, 0, 0,};
  288. +/*!
  289. + Specifies black back and white letter.
  290. +*/
  291. +const SDLPango_Matrix *MATRIX_BLACK_BACK = &_MATRIX_BLACK_BACK;
  292. +
  293. +const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_BLACK_LETTER
  294. + = {0, 0, 0, 0,
  295. + 0, 0, 0, 0,
  296. + 0, 0, 0, 0,
  297. + 0, 255, 0, 0,};
  298. +/*!
  299. + Specifies transparent back and black letter.
  300. +*/
  301. +const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_BLACK_LETTER = &_MATRIX_TRANSPARENT_BACK_BLACK_LETTER;
  302. +
  303. +const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_WHITE_LETTER
  304. + = {255, 255, 0, 0,
  305. + 255, 255, 0, 0,
  306. + 255, 255, 0, 0,
  307. + 0, 255, 0, 0,};
  308. +/*!
  309. + Specifies transparent back and white letter.
  310. +*/
  311. +const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_WHITE_LETTER = &_MATRIX_TRANSPARENT_BACK_WHITE_LETTER;
  312. +
  313. +const SDLPango_Matrix _MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER
  314. + = {255, 255, 0, 0,
  315. + 255, 255, 0, 0,
  316. + 255, 255, 0, 0,
  317. + 0, 0, 0, 0,};
  318. +/*!
  319. + Specifies transparent back and transparent letter.
  320. + This is useful for KARAOKE like rendering.
  321. +*/
  322. +const SDLPango_Matrix *MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER = &_MATRIX_TRANSPARENT_BACK_TRANSPARENT_LETTER;
  323. +
  324. +/*!
  325. + Specifies direction of text. See Pango reference for detail
  326. +*/
  327. +typedef enum {
  328. + SDLPANGO_DIRECTION_LTR, /*! Left to right */
  329. + SDLPANGO_DIRECTION_RTL, /*! Right to left */
  330. + SDLPANGO_DIRECTION_WEAK_LTR, /*! Left to right (weak) */
  331. + SDLPANGO_DIRECTION_WEAK_RTL, /*! Right to left (weak) */
  332. + SDLPANGO_DIRECTION_NEUTRAL /*! Neutral */
  333. +} SDLPango_Direction;
  334. +
  335. +/*!
  336. + Specifies alignment of text. See Pango reference for detail
  337. +*/
  338. +typedef enum {
  339. + SDLPANGO_ALIGN_LEFT,
  340. + SDLPANGO_ALIGN_CENTER,
  341. + SDLPANGO_ALIGN_RIGHT
  342. +} SDLPango_Alignment;
  343. +
  344. +extern DECLSPEC int SDLCALL SDLPango_Init();
  345. +
  346. +extern DECLSPEC int SDLCALL SDLPango_WasInit();
  347. +
  348. +extern DECLSPEC SDLPango_Context* SDLCALL SDLPango_CreateContext_GivenFontDesc(const char* font_desc);
  349. +extern DECLSPEC SDLPango_Context* SDLCALL SDLPango_CreateContext();
  350. +
  351. +extern DECLSPEC void SDLCALL SDLPango_FreeContext(
  352. + SDLPango_Context *context);
  353. +
  354. +extern DECLSPEC void SDLCALL SDLPango_SetSurfaceCreateArgs(
  355. + SDLPango_Context *context,
  356. + Uint32 flags,
  357. + int depth,
  358. + Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask);
  359. +
  360. +extern DECLSPEC SDL_Surface * SDLCALL SDLPango_CreateSurfaceDraw(
  361. + SDLPango_Context *context);
  362. +
  363. +extern DECLSPEC void SDLCALL SDLPango_Draw(
  364. + SDLPango_Context *context,
  365. + SDL_Surface *surface,
  366. + int x, int y);
  367. +
  368. +extern DECLSPEC void SDLCALL SDLPango_SetDpi(
  369. + SDLPango_Context *context,
  370. + double dpi_x, double dpi_y);
  371. +
  372. +extern DECLSPEC void SDLCALL SDLPango_SetMinimumSize(
  373. + SDLPango_Context *context,
  374. + int width, int height);
  375. +
  376. +extern DECLSPEC void SDLCALL SDLPango_SetDefaultColor(
  377. + SDLPango_Context *context,
  378. + const SDLPango_Matrix *color_matrix);
  379. +
  380. +extern DECLSPEC int SDLCALL SDLPango_GetLayoutWidth(
  381. + SDLPango_Context *context);
  382. +
  383. +extern DECLSPEC int SDLCALL SDLPango_GetLayoutHeight(
  384. + SDLPango_Context *context);
  385. +
  386. +extern DECLSPEC void SDLCALL SDLPango_SetMarkup(
  387. + SDLPango_Context *context,
  388. + const char *markup,
  389. + int length);
  390. +
  391. +extern DECLSPEC void SDLCALL SDLPango_SetText_GivenAlignment(
  392. + SDLPango_Context *context,
  393. + const char *text,
  394. + int length,
  395. + SDLPango_Alignment alignment);
  396. +
  397. +extern DECLSPEC void SDLCALL SDLPango_SetText(
  398. + SDLPango_Context *context,
  399. + const char *markup,
  400. + int length);
  401. +
  402. +extern DECLSPEC void SDLCALL SDLPango_SetLanguage(
  403. + SDLPango_Context *context,
  404. + const char *language_tag);
  405. +
  406. +extern DECLSPEC void SDLCALL SDLPango_SetBaseDirection(
  407. + SDLPango_Context *context,
  408. + SDLPango_Direction direction);
  409. +
  410. +
  411. +#ifdef __FT2_BUILD_UNIX_H__
  412. +
  413. +extern DECLSPEC void SDLCALL SDLPango_CopyFTBitmapToSurface(
  414. + const FT_Bitmap *bitmap,
  415. + SDL_Surface *surface,
  416. + const SDLPango_Matrix *matrix,
  417. + SDL_Rect *rect);
  418. +
  419. +#endif /* __FT2_BUILD_UNIX_H__ */
  420. +
  421. +
  422. +#ifdef __PANGO_H__
  423. +
  424. +extern DECLSPEC PangoFontMap* SDLCALL SDLPango_GetPangoFontMap(
  425. + SDLPango_Context *context);
  426. +
  427. +extern DECLSPEC PangoFontDescription* SDLCALL SDLPango_GetPangoFontDescription(
  428. + SDLPango_Context *context);
  429. +
  430. +extern DECLSPEC PangoLayout* SDLCALL SDLPango_GetPangoLayout(
  431. + SDLPango_Context *context);
  432. +
  433. +#endif /* __PANGO_H__ */
  434. +
  435. +
  436. +#ifdef __cplusplus
  437. +}
  438. +#endif
  439. +
  440. +#include "close_code.h"
  441. +
  442. +#endif /* SDL_PANGO_H */
  443. --- SDL_Pango-0.1.2/src/SDL_Pango.c.original 2006-12-13 15:36:40.000000000 +0100
  444. +++ SDL_Pango-0.1.2/src/SDL_Pango.c 2006-12-13 16:14:46.000000000 +0100
  445. @@ -1,1175 +1,1190 @@
  446. -/* SDL_Pango.c -- A companion library to SDL for working with Pango.
  447. - Copyright (C) 2004 NAKAMURA Ken'ichi
  448. -
  449. - This library is free software; you can redistribute it and/or
  450. - modify it under the terms of the GNU Lesser General Public
  451. - License as published by the Free Software Foundation; either
  452. - version 2.1 of the License, or (at your option) any later version.
  453. -
  454. - This library is distributed in the hope that it will be useful,
  455. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  456. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  457. - Lesser General Public License for more details.
  458. -
  459. - You should have received a copy of the GNU Lesser General Public
  460. - License along with this library; if not, write to the Free Software
  461. - Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  462. -*/
  463. -
  464. -/*!
  465. - \mainpage
  466. -
  467. - \section intro Introduction
  468. -
  469. - Pango is the text rendering engine of GNOME 2.x. SDL_Pango connects the
  470. - engine to SDL. In Windows, pre-built binary package (MSI and merge module)
  471. - is provided.
  472. -
  473. - \subsection dist Distribution
  474. -
  475. - If you are a game software developer, you should know the difficulties of
  476. - distribution. So I will start to introduce SDL_Pango from the viewpoint
  477. - of distribution.
  478. -
  479. - In Un*x, SDL_Pango is hard to use as system-independent module, because
  480. - it depends on fontconfig and Pango which are designed as system-singleton
  481. - modules. If you use SDL_Pango, your software will require those modules
  482. - installed to target system. If your software is shipped as shrink-wrap
  483. - package, it may cause much problem on your support desk. You should
  484. - carefully design your installation process.
  485. -
  486. - In Windows, SDL_Pango is distributed as "merge module" which contains
  487. - fontconfig and Pango. Those binaries are modified as side-by-side components.
  488. - You should use Windows Installer and merge the module
  489. - on your MSI package. The merge module not only contains files, but also includes
  490. - custom action which must be run at installation.
  491. -
  492. - \subsection api High-level API
  493. -
  494. - From the viewpoint of text rendering, the heart of SDL_Pango is high-level API.
  495. - Other text rendering APIs, like DrawText() of Windows, font and text must be
  496. - specified separately. In SDL_Pango, font specification is embedded in text like
  497. - HTML:
  498. -
  499. - \code
  500. - <span font_family="Courier New"><i>This is Courier New and italic.</i></span>
  501. - \endcode
  502. -
  503. - Color, size, subscript/superscript, obliquing, weight, and other many features
  504. - are also available in same way.
  505. -
  506. - \subsection i18n Internationalized Text
  507. -
  508. - Internationalized text is another key feature. Text is specified by UTF-8. RTL
  509. - script (Arabic and Hebrew) and complicated rendering (Arabic, Indic and Thai) are
  510. - supported. You can see it with GNOME 2.x.
  511. -
  512. - \section get Getting Started
  513. -
  514. - \subsection getlatest Get latest files
  515. -
  516. - Get latest files from http://sourceforge.net/projects/sdlpango/ .
  517. -
  518. - \subsection install Install Header and Library
  519. -
  520. - In Windows and VS2003, I strongly recommend you to install MSI package. It contains Pango
  521. - and fontconfig binaries which are modified as side-by-side components. It is
  522. - nearly impossible to build them. (I spent much time to build them...)
  523. -
  524. - In MinGW, I recommend you to use VS2003. Otherwise you may run into the maze of
  525. - distribution. If you insist MinGW, you should use MinGW binary archive.
  526. -
  527. - In Un*x, installation consists of:
  528. -
  529. - \code
  530. - ./configure
  531. - make
  532. - make install
  533. - \endcode
  534. -
  535. - \subsection inc Includes
  536. -
  537. - To use SDL_Pango functions in a C/C++ source code file, you must use the SDL_Pango.h
  538. - include file:
  539. -
  540. - \code
  541. - #include "SDL_Pango.h"
  542. - \endcode
  543. -
  544. - In Windows, SDL_Pango.h is installed on \c \%ProgramFiles\%\\SDL_Pango \c Development\\include
  545. - (usually \c C:\\Program \c Files\\SDL_Pango \c Development\\include). You should add this
  546. - directory to include path.
  547. -
  548. - \subsection comp Compiling
  549. -
  550. - In Un*x, to link with SDL_Pango you should use sdl-config to get the required SDL
  551. - compilation options. After that, compiling with SDL_Pango is quite easy.
  552. -
  553. - Note: Some systems may not have the SDL_Pango library and include file in the same
  554. - place as the SDL library and includes are located, in that case you will need to
  555. - add more -I and -L paths to these command lines.
  556. -
  557. - Simple Example for compiling an object file:
  558. -
  559. - \code
  560. - cc -c `sdl-config --cflags` mysource.c
  561. - \endcode
  562. -
  563. - Simple Example for linking an object file:
  564. -
  565. - \code
  566. - cc -o myprogram mysource.o `sdl-config --libs` -lSDL_Pango
  567. - \endcode
  568. -
  569. - Now myprogram is ready to run.
  570. -
  571. - You can see a sample of autoconfiscation in 'test' directory.
  572. -
  573. - In Windows, MSI package installs many dlls to \c \%ProgramFiles\%\\SDL_Pango \c Development\\import_lib.
  574. - To link with SDL_Pango you should use SDL_Pango.lib.
  575. -
  576. - SDL_Pango.dll depends on many dlls and other many files. Those dlls are installed on
  577. - \c \%ProgramFiles\%\\SDL_Pango \c Development\\bin. MSI package adds the directory to PATH environment
  578. - variable.
  579. -
  580. - \section devel Development
  581. -
  582. - \subsection font Font Handling
  583. -
  584. - In Un*x, font handling depends on fontconfig of your system.
  585. -
  586. - In Windows, local.conf of fontconfig is placed on \c \%ProgramFiles\%\\SDL_Pango \c Development\\etc\\fonts.
  587. - You should know about fontconfig's font cache mechanism.
  588. -
  589. - \subsection example Step-by-step Example
  590. -
  591. - The operation of SDL_Pango is done via context.
  592. -
  593. - \code
  594. - SDLPango_Context *context = SDLPango_CreateContext();
  595. - \endcode
  596. -
  597. - Specify default colors and minimum surface size.
  598. -
  599. - \code
  600. - SDLPango_SetDefaultColor(context, MATRIX_TRANSPARENT_BACK_WHITE_LETTER);
  601. - SDLPango_SetMinimumSize(context, 640, 0);
  602. - \endcode
  603. -
  604. - Set markup text.
  605. -
  606. - \code
  607. - SDLPango_SetMarkup(context, "This is <i>markup</i> text.", -1);
  608. - \endcode
  609. -
  610. - Now you can get the size of surface.
  611. -
  612. - \code
  613. - int w = SDLPango_GetLayoutWidth(context);
  614. - int h = SDLPango_GetLayoutHeight(context);
  615. - \endcode
  616. -
  617. - Create surface to draw.
  618. -
  619. - \code
  620. - int margin_x = 10;
  621. - int margin_y = 10;
  622. - SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  623. - w + margin_x * 2, h + margin_y * 2,
  624. - 32, (Uint32)(255 << (8 * 3)), (Uint32)(255 << (8 * 2)),
  625. - (Uint32)(255 << (8 * 1)), 255);
  626. - \endcode
  627. -
  628. - And draw on it.
  629. -
  630. - \code
  631. - SDLPango_Draw(context, surface, margin_x, margin_y);
  632. - \endcode
  633. -
  634. - You must free the surface by yourself.
  635. -
  636. - \code
  637. - SDL_FreeSurface(surface);
  638. - \endcode
  639. -
  640. - Free context.
  641. -
  642. - \code
  643. - SDLPango_FreeContext(context);
  644. - \endcode
  645. -
  646. - You can see actual code in \c test/testbench.cpp.
  647. -
  648. - \subsection pack Packaging
  649. -
  650. - In Un*x, do it yourself.
  651. -
  652. - In Windows, font files must be installed on apprication folder (usually
  653. - \c C:\\Program \c Files\\[Manufacturer]\\[ProductName]). The property of
  654. - apprication folder must be \c TARGETDIR (this is default setting of VS2003).
  655. - SDL.dll also must be installed on apprication folder. Add SDL_Pango.msm to
  656. - your MSI package.
  657. -
  658. - \section ack Acknowledgment
  659. -
  660. - SDL_Pango is developed with financial assistance of Information-technology Promotion Agency, Japan.
  661. -
  662. -- NAKAMURA Ken'ichi <nakamura@sbp.fp.a.u-tokyo.ac.jp>
  663. -
  664. -*/
  665. -
  666. -/*! @file
  667. - @brief Implementation of SDL_Pango
  668. -
  669. - @author NAKAMURA Ken'ichi
  670. - @date 2004/12/07
  671. - $Revision: 1.6 $
  672. -*/
  673. -
  674. -#include <pango/pango.h>
  675. -#include <pango/pangoft2.h>
  676. -
  677. -#include "SDL_Pango.h"
  678. -
  679. -//! non-zero if initialized
  680. -static int IS_INITIALIZED = 0;
  681. -
  682. -#define DEFAULT_FONT_FAMILY "Sans"
  683. -#define DEFAULT_FONT_SIZE 12
  684. -#define DEFAULT_DPI 96
  685. -#define _MAKE_FONT_NAME(family, size) family " " #size
  686. -#define MAKE_FONT_NAME(family, size) _MAKE_FONT_NAME(family, size)
  687. -#define DEFAULT_DEPTH 32
  688. -#define DEFAULT_RMASK (Uint32)(255 << (8 * 3))
  689. -#define DEFAULT_GMASK (Uint32)(255 << (8 * 2))
  690. -#define DEFAULT_BMASK (Uint32)(255 << (8 * 1))
  691. -#define DEFAULT_AMASK (Uint32)255
  692. -
  693. -static FT_Bitmap *createFTBitmap(int width, int height);
  694. -
  695. -static void freeFTBitmap(FT_Bitmap *bitmap);
  696. -
  697. -static void getItemProperties (
  698. - PangoItem *item,
  699. - PangoUnderline *uline,
  700. - gboolean *strikethrough,
  701. - gint *rise,
  702. - PangoColor *fg_color,
  703. - gboolean *fg_set,
  704. - PangoColor *bg_color,
  705. - gboolean *bg_set,
  706. - gboolean *shape_set,
  707. - PangoRectangle *ink_rect,
  708. - PangoRectangle *logical_rect);
  709. -
  710. -static void clearFTBitmap(FT_Bitmap *bitmap);
  711. -
  712. -typedef struct _surfaceArgs {
  713. - Uint32 flags;
  714. - int depth;
  715. - Uint32 Rmask;
  716. - Uint32 Gmask;
  717. - Uint32 Bmask;
  718. - Uint32 Amask;
  719. -} surfaceArgs;
  720. -
  721. -typedef struct _contextImpl {
  722. - PangoContext *context;
  723. - PangoFontMap *font_map;
  724. - PangoFontDescription *font_desc;
  725. - PangoLayout *layout;
  726. - surfaceArgs surface_args;
  727. - FT_Bitmap *tmp_ftbitmap;
  728. - SDLPango_Matrix color_matrix;
  729. - int min_width;
  730. - int min_height;
  731. -} contextImpl;
  732. -
  733. -
  734. -/*!
  735. - Initialize the Glib and Pango API.
  736. - This must be called before using other functions in this library,
  737. - excepting SDLPango_WasInit.
  738. - SDL does not have to be initialized before this call.
  739. -
  740. -
  741. - @return always 0.
  742. -*/
  743. -int
  744. -SDLPango_Init()
  745. -{
  746. - g_type_init();
  747. -
  748. - IS_INITIALIZED = -1;
  749. -
  750. - return 0;
  751. -}
  752. -
  753. -/*!
  754. - Query the initilization status of the Glib and Pango API.
  755. - You may, of course, use this before SDLPango_Init to avoid
  756. - initilizing twice in a row.
  757. -
  758. - @return zero when already initialized.
  759. - non-zero when not initialized.
  760. -*/
  761. -int
  762. -SDLPango_WasInit()
  763. -{
  764. - return IS_INITIALIZED;
  765. -}
  766. -
  767. -/*!
  768. - Draw glyphs on rect.
  769. -
  770. - @param *context [in] Context
  771. - @param *surface [out] Surface to draw on it
  772. - @param *color_matrix [in] Foreground and background color
  773. - @param *font [in] Innter variable of Pango
  774. - @param *glyphs [in] Innter variable of Pango
  775. - @param *rect [in] Draw on this area
  776. - @param baseline [in] Horizontal location of glyphs
  777. -*/
  778. -static void
  779. -drawGlyphString(
  780. - SDLPango_Context *context,
  781. - SDL_Surface *surface,
  782. - SDLPango_Matrix *color_matrix,
  783. - PangoFont *font,
  784. - PangoGlyphString *glyphs,
  785. - SDL_Rect *rect,
  786. - int baseline)
  787. -{
  788. - pango_ft2_render(context->tmp_ftbitmap, font, glyphs, rect->x, rect->y + baseline);
  789. -
  790. - SDLPango_CopyFTBitmapToSurface(
  791. - context->tmp_ftbitmap,
  792. - surface,
  793. - color_matrix,
  794. - rect);
  795. -
  796. - clearFTBitmap(context->tmp_ftbitmap);
  797. -}
  798. -
  799. -/*!
  800. - Draw horizontal line of a pixel.
  801. -
  802. - @param *surface [out] Surface to draw on it
  803. - @param *color_matrix [in] Foreground and background color
  804. - @param y [in] Y location of line
  805. - @param start [in] Left of line
  806. - @param end [in] Right of line
  807. -*/
  808. -static void drawHLine(
  809. - SDL_Surface *surface,
  810. - SDLPango_Matrix *color_matrix,
  811. - int y,
  812. - int start,
  813. - int end)
  814. -{
  815. - Uint8 *p;
  816. - Uint16 *p16;
  817. - Uint32 *p32;
  818. - Uint32 color;
  819. - int ix;
  820. - int pixel_bytes = surface->format->BytesPerPixel;
  821. -
  822. - if (y < 0 || y >= surface->h)
  823. - return;
  824. -
  825. - if (end <= 0 || start >= surface->w)
  826. - return;
  827. -
  828. - if (start < 0)
  829. - start = 0;
  830. -
  831. - if (end >= surface->w)
  832. - end = surface->w;
  833. -
  834. - p = (Uint8 *)(surface->pixels) + y * surface->pitch + start * pixel_bytes;
  835. - color = SDL_MapRGBA(surface->format,
  836. - color_matrix->m[0][1],
  837. - color_matrix->m[1][1],
  838. - color_matrix->m[2][1],
  839. - color_matrix->m[3][1]);
  840. -
  841. - switch(pixel_bytes) {
  842. - case 2:
  843. - p16 = (Uint16 *)p;
  844. - for (ix = 0; ix < end - start; ix++)
  845. - *p16++ = (Uint16)color;
  846. - break;
  847. - case 4:
  848. - p32 = (Uint32 *)p;
  849. - for (ix = 0; ix < end - start; ix++)
  850. - *p32++ = color;
  851. - break;
  852. - default:
  853. - SDL_SetError("surface->format->BytesPerPixel is invalid value");
  854. - break;
  855. - }
  856. -}
  857. -
  858. -/*!
  859. - Draw a line.
  860. -
  861. - @param *context [in] Context
  862. - @param *surface [out] Surface to draw on it
  863. - @param *line [in] Innter variable of Pango
  864. - @param x [in] X location of line
  865. - @param y [in] Y location of line
  866. - @param height [in] Height of line
  867. - @param baseline [in] Rise / sink of line (for super/subscript)
  868. -*/
  869. -static void
  870. -drawLine(
  871. - SDLPango_Context *context,
  872. - SDL_Surface *surface,
  873. - PangoLayoutLine *line,
  874. - gint x,
  875. - gint y,
  876. - gint height,
  877. - gint baseline)
  878. -{
  879. - GSList *tmp_list = line->runs;
  880. - PangoColor fg_color, bg_color;
  881. - PangoRectangle logical_rect;
  882. - PangoRectangle ink_rect;
  883. - int x_off = 0;
  884. -
  885. - while (tmp_list) {
  886. - SDLPango_Matrix color_matrix = context->color_matrix;
  887. - PangoUnderline uline = PANGO_UNDERLINE_NONE;
  888. - gboolean strike, fg_set, bg_set, shape_set;
  889. - gint rise, risen_y;
  890. - PangoLayoutRun *run = tmp_list->data;
  891. - SDL_Rect d_rect;
  892. -
  893. - tmp_list = tmp_list->next;
  894. -
  895. - getItemProperties(run->item,
  896. - &uline, &strike, &rise,
  897. - &fg_color, &fg_set, &bg_color, &bg_set,
  898. - &shape_set, &ink_rect, &logical_rect);
  899. -
  900. - risen_y = y + baseline - PANGO_PIXELS (rise);
  901. -
  902. - if(fg_set) {
  903. - color_matrix.m[0][1] = (Uint8)(fg_color.red >> 8);
  904. - color_matrix.m[1][1] = (Uint8)(fg_color.green >> 8);
  905. - color_matrix.m[2][1] = (Uint8)(fg_color.blue >> 8);
  906. - color_matrix.m[3][1] = 255;
  907. - if(color_matrix.m[3][0] == 0) {
  908. - color_matrix.m[0][0] = (Uint8)(fg_color.red >> 8);
  909. - color_matrix.m[1][0] = (Uint8)(fg_color.green >> 8);
  910. - color_matrix.m[2][0] = (Uint8)(fg_color.blue >> 8);
  911. - }
  912. - }
  913. -
  914. - if (bg_set) {
  915. - color_matrix.m[0][0] = (Uint8)(bg_color.red >> 8);
  916. - color_matrix.m[1][0] = (Uint8)(bg_color.green >> 8);
  917. - color_matrix.m[2][0] = (Uint8)(bg_color.blue >> 8);
  918. - color_matrix.m[3][0] = 255;
  919. - }
  920. -
  921. - if(! shape_set) {
  922. - if (uline == PANGO_UNDERLINE_NONE)
  923. - pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
  924. - NULL, &logical_rect);
  925. - else
  926. - pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
  927. - &ink_rect, &logical_rect);
  928. -
  929. - d_rect.w = (Uint16)PANGO_PIXELS(logical_rect.width);
  930. - d_rect.h = (Uint16)height;
  931. - d_rect.x = (Uint16)(x + PANGO_PIXELS (x_off));
  932. - d_rect.y = (Uint16)(risen_y - baseline);
  933. -
  934. - if((! context->tmp_ftbitmap) || d_rect.w + d_rect.x > context->tmp_ftbitmap->width
  935. - || d_rect.h + d_rect.y > context->tmp_ftbitmap->rows)
  936. - {
  937. - freeFTBitmap(context->tmp_ftbitmap);
  938. - context->tmp_ftbitmap = createFTBitmap(d_rect.w + d_rect.x, d_rect.h + d_rect.y);
  939. - }
  940. -
  941. - drawGlyphString(context, surface,
  942. - &color_matrix,
  943. - run->item->analysis.font, run->glyphs, &d_rect, baseline);
  944. - }
  945. - switch (uline) {
  946. - case PANGO_UNDERLINE_NONE:
  947. - break;
  948. - case PANGO_UNDERLINE_DOUBLE:
  949. - drawHLine(surface, &color_matrix,
  950. - risen_y + 4,
  951. - x + PANGO_PIXELS (x_off + ink_rect.x),
  952. - x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  953. - /* Fall through */
  954. - case PANGO_UNDERLINE_SINGLE:
  955. - drawHLine(surface, &color_matrix,
  956. - risen_y + 2,
  957. - x + PANGO_PIXELS (x_off + ink_rect.x),
  958. - x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  959. - break;
  960. - case PANGO_UNDERLINE_ERROR:
  961. - {
  962. - int point_x;
  963. - int counter = 0;
  964. - int end_x = x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width);
  965. -
  966. - for (point_x = x + PANGO_PIXELS (x_off + ink_rect.x) - 1;
  967. - point_x <= end_x;
  968. - point_x += 2)
  969. - {
  970. - if (counter)
  971. - drawHLine(surface, &color_matrix,
  972. - risen_y + 2,
  973. - point_x, MIN (point_x + 1, end_x));
  974. - else
  975. - drawHLine(surface, &color_matrix,
  976. - risen_y + 3,
  977. - point_x, MIN (point_x + 1, end_x));
  978. -
  979. - counter = (counter + 1) % 2;
  980. - }
  981. - }
  982. - break;
  983. - case PANGO_UNDERLINE_LOW:
  984. - drawHLine(surface, &color_matrix,
  985. - risen_y + PANGO_PIXELS (ink_rect.y + ink_rect.height),
  986. - x + PANGO_PIXELS (x_off + ink_rect.x),
  987. - x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  988. - break;
  989. - }
  990. -
  991. - if (strike)
  992. - drawHLine(surface, &color_matrix,
  993. - risen_y + PANGO_PIXELS (logical_rect.y + logical_rect.height / 2),
  994. - x + PANGO_PIXELS (x_off + logical_rect.x),
  995. - x + PANGO_PIXELS (x_off + logical_rect.x + logical_rect.width));
  996. -
  997. - x_off += logical_rect.width;
  998. - }
  999. -}
  1000. -
  1001. -/*!
  1002. - Innter function of Pango. Stolen from GDK.
  1003. -
  1004. - @param *item [in] The item to get property
  1005. - @param *uline [out] Kind of underline
  1006. - @param *strikethrough [out] Strike-through line
  1007. - @param *rise [out] Rise/sink of line (for super/subscript)
  1008. - @param *fg_color [out] Color of foreground
  1009. - @param *fg_set [out] True if fg_color set
  1010. - @param *bg_color [out] Color of background
  1011. - @param *bg_set [out] True if bg_color valid
  1012. - @param *shape_set [out] True if ink_rect and logical_rect valid
  1013. - @param *ink_rect [out] Ink rect
  1014. - @param *logical_rect [out] Logical rect
  1015. -*/
  1016. -static void
  1017. -getItemProperties (
  1018. - PangoItem *item,
  1019. - PangoUnderline *uline,
  1020. - gboolean *strikethrough,
  1021. - gint *rise,
  1022. - PangoColor *fg_color,
  1023. - gboolean *fg_set,
  1024. - PangoColor *bg_color,
  1025. - gboolean *bg_set,
  1026. - gboolean *shape_set,
  1027. - PangoRectangle *ink_rect,
  1028. - PangoRectangle *logical_rect)
  1029. -{
  1030. - GSList *tmp_list = item->analysis.extra_attrs;
  1031. -
  1032. - if (strikethrough)
  1033. - *strikethrough = FALSE;
  1034. -
  1035. - if (fg_set)
  1036. - *fg_set = FALSE;
  1037. -
  1038. - if (bg_set)
  1039. - *bg_set = FALSE;
  1040. -
  1041. - if (shape_set)
  1042. - *shape_set = FALSE;
  1043. -
  1044. - if (rise)
  1045. - *rise = 0;
  1046. -
  1047. - while (tmp_list) {
  1048. - PangoAttribute *attr = tmp_list->data;
  1049. -
  1050. - switch (attr->klass->type) {
  1051. - case PANGO_ATTR_UNDERLINE:
  1052. - if (uline)
  1053. - *uline = ((PangoAttrInt *)attr)->value;
  1054. - break;
  1055. -
  1056. - case PANGO_ATTR_STRIKETHROUGH:
  1057. - if (strikethrough)
  1058. - *strikethrough = ((PangoAttrInt *)attr)->value;
  1059. - break;
  1060. -
  1061. - case PANGO_ATTR_FOREGROUND:
  1062. - if (fg_color)
  1063. - *fg_color = ((PangoAttrColor *)attr)->color;
  1064. - if (fg_set)
  1065. - *fg_set = TRUE;
  1066. - break;
  1067. -
  1068. - case PANGO_ATTR_BACKGROUND:
  1069. - if (bg_color)
  1070. - *bg_color = ((PangoAttrColor *)attr)->color;
  1071. - if (bg_set)
  1072. - *bg_set = TRUE;
  1073. - break;
  1074. -
  1075. - case PANGO_ATTR_SHAPE:
  1076. - if (shape_set)
  1077. - *shape_set = TRUE;
  1078. - if (logical_rect)
  1079. - *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
  1080. - if (ink_rect)
  1081. - *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
  1082. - break;
  1083. -
  1084. - case PANGO_ATTR_RISE:
  1085. - if (rise)
  1086. - *rise = ((PangoAttrInt *)attr)->value;
  1087. - break;
  1088. -
  1089. - default:
  1090. - break;
  1091. - }
  1092. - tmp_list = tmp_list->next;
  1093. - }
  1094. -}
  1095. -
  1096. -/*!
  1097. - Copy bitmap to surface.
  1098. - From (x, y)-(w, h) to (x, y)-(w, h) of rect.
  1099. -
  1100. - @param *bitmap [in] Grayscale bitmap
  1101. - @param *surface [out] Surface
  1102. - @param *matrix [in] Foreground and background color
  1103. - @param *rect [in] Rect to copy
  1104. -*/
  1105. -void
  1106. -SDLPango_CopyFTBitmapToSurface(
  1107. - const FT_Bitmap *bitmap,
  1108. - SDL_Surface *surface,
  1109. - const SDLPango_Matrix *matrix,
  1110. - SDL_Rect *rect)
  1111. -{
  1112. - int i;
  1113. - Uint8 *p_ft;
  1114. - Uint8 *p_sdl;
  1115. - int width = rect->w;
  1116. - int height = rect->h;
  1117. - int x = rect->x;
  1118. - int y = rect->y;
  1119. -
  1120. - if(x + width > surface->w) {
  1121. - width = surface->w - x;
  1122. - if(width <= 0)
  1123. - return;
  1124. - }
  1125. - if(y + height > surface->h) {
  1126. - height = surface->h - y;
  1127. - if(height <= 0)
  1128. - return;
  1129. - }
  1130. -
  1131. - if(SDL_LockSurface(surface)) {
  1132. - SDL_SetError("surface lock failed");
  1133. - SDL_FreeSurface(surface);
  1134. - return;
  1135. - }
  1136. -
  1137. - p_ft = (Uint8 *)bitmap->buffer + (bitmap->pitch * y);
  1138. - p_sdl = (Uint8 *)surface->pixels + (surface->pitch * y);
  1139. - for(i = 0; i < height; i ++) {
  1140. - int k;
  1141. - for(k = 0; k < width; k ++) {
  1142. - /* TODO: rewrite by matrix calculation library */
  1143. - Uint8 pixel[4]; /* 4: RGBA */
  1144. - int n;
  1145. -
  1146. - for(n = 0; n < 4; n ++) {
  1147. - Uint16 w;
  1148. - w = ((Uint16)matrix->m[n][0] * (256 - p_ft[k + x])) + ((Uint16)matrix->m[n][1] * p_ft[k + x]);
  1149. - pixel[n] = (Uint8)(w >> 8);
  1150. - }
  1151. -
  1152. - switch(surface->format->BytesPerPixel) {
  1153. - case 2:
  1154. - ((Uint16 *)p_sdl)[k + x] = (Uint16)SDL_MapRGBA(surface->format, pixel[0], pixel[1], pixel[2], pixel[3]);
  1155. - break;
  1156. - case 4:
  1157. - ((Uint32 *)p_sdl)[k + x] = SDL_MapRGBA(surface->format, pixel[0], pixel[1], pixel[2], pixel[3]);
  1158. - break;
  1159. - default:
  1160. - SDL_SetError("surface->format->BytesPerPixel is invalid value");
  1161. - return;
  1162. - }
  1163. - }
  1164. - p_ft += bitmap->pitch;
  1165. - p_sdl += surface->pitch;
  1166. - }
  1167. -
  1168. - SDL_UnlockSurface(surface);
  1169. -}
  1170. -
  1171. -/*!
  1172. - Create a context which contains Pango objects.
  1173. -
  1174. - @return A pointer to the context as a SDLPango_Context*.
  1175. -*/
  1176. -SDLPango_Context*
  1177. -SDLPango_CreateContext()
  1178. -{
  1179. - SDLPango_Context *context = g_malloc(sizeof(SDLPango_Context));
  1180. - G_CONST_RETURN char *charset;
  1181. -
  1182. - context->font_map = pango_ft2_font_map_new ();
  1183. - pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (context->font_map), DEFAULT_DPI, DEFAULT_DPI);
  1184. -
  1185. - context->context = pango_ft2_font_map_create_context (PANGO_FT2_FONT_MAP (context->font_map));
  1186. -
  1187. - g_get_charset(&charset);
  1188. - pango_context_set_language (context->context, pango_language_from_string (charset));
  1189. - pango_context_set_base_dir (context->context, PANGO_DIRECTION_LTR);
  1190. -
  1191. - context->font_desc = pango_font_description_from_string(
  1192. - MAKE_FONT_NAME (DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE));
  1193. -
  1194. - context->layout = pango_layout_new (context->context);
  1195. -
  1196. - SDLPango_SetSurfaceCreateArgs(context, SDL_SWSURFACE | SDL_SRCALPHA, DEFAULT_DEPTH,
  1197. - DEFAULT_RMASK, DEFAULT_GMASK, DEFAULT_BMASK, DEFAULT_AMASK);
  1198. -
  1199. - context->tmp_ftbitmap = NULL;
  1200. -
  1201. - context->color_matrix = *MATRIX_TRANSPARENT_BACK_BLACK_LETTER;
  1202. -
  1203. - context->min_height = 0;
  1204. - context->min_width = 0;
  1205. -
  1206. - return context;
  1207. -}
  1208. -
  1209. -/*!
  1210. - Free a context.
  1211. -
  1212. - @param *context [i/o] Context to be free
  1213. -*/
  1214. -void
  1215. -SDLPango_FreeContext(SDLPango_Context *context)
  1216. -{
  1217. - freeFTBitmap(context->tmp_ftbitmap);
  1218. -
  1219. - g_object_unref (context->layout);
  1220. -
  1221. - pango_font_description_free(context->font_desc);
  1222. -
  1223. - g_object_unref(context->context);
  1224. -
  1225. - g_object_unref(context->font_map);
  1226. -
  1227. - g_free(context);
  1228. -}
  1229. -
  1230. -/*!
  1231. - Specify Arguments when create a surface.
  1232. - When SDL_Pango creates a surface, the arguments are used.
  1233. -
  1234. - @param *context [i/o] Context
  1235. - @param flags [in] Same as SDL_CreateRGBSurface()
  1236. - @param depth [in] Same as SDL_CreateRGBSurface()
  1237. - @param Rmask [in] Same as SDL_CreateRGBSurface()
  1238. - @param Gmask [in] Same as SDL_CreateRGBSurface()
  1239. - @param Bmask [in] Same as SDL_CreateRGBSurface()
  1240. - @param Amask [in] Same as SDL_CreateRGBSurface()
  1241. -*/
  1242. -void
  1243. -SDLPango_SetSurfaceCreateArgs(
  1244. - SDLPango_Context *context,
  1245. - Uint32 flags,
  1246. - int depth,
  1247. - Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
  1248. -{
  1249. - context->surface_args.flags = flags;
  1250. - context->surface_args.depth = depth;
  1251. - context->surface_args.Rmask = Rmask;
  1252. - context->surface_args.Gmask = Gmask;
  1253. - context->surface_args.Bmask = Bmask;
  1254. - context->surface_args.Amask = Amask;
  1255. -}
  1256. -
  1257. -/*!
  1258. - Create a surface and draw text on it.
  1259. - The size of surface is same as lauout size.
  1260. -
  1261. - @param *context [in] Context
  1262. - @return A newly created surface
  1263. -*/
  1264. -SDL_Surface * SDLPango_CreateSurfaceDraw(
  1265. - SDLPango_Context *context)
  1266. -{
  1267. - PangoRectangle logical_rect;
  1268. - SDL_Surface *surface;
  1269. - int width, height;
  1270. -
  1271. - pango_layout_get_extents (context->layout, NULL, &logical_rect);
  1272. - width = PANGO_PIXELS (logical_rect.width);
  1273. - height = PANGO_PIXELS (logical_rect.height);
  1274. - if(width < context->min_width)
  1275. - width = context->min_width;
  1276. - if(height < context->min_height)
  1277. - height = context->min_height;
  1278. -
  1279. - surface = SDL_CreateRGBSurface(
  1280. - context->surface_args.flags,
  1281. - width, height, context->surface_args.depth,
  1282. - context->surface_args.Rmask,
  1283. - context->surface_args.Gmask,
  1284. - context->surface_args.Bmask,
  1285. - context->surface_args.Amask);
  1286. -
  1287. - SDLPango_Draw(context, surface, 0, 0);
  1288. -
  1289. - return surface;
  1290. -}
  1291. -
  1292. -/*!
  1293. - Draw text on a existing surface.
  1294. -
  1295. - @param *context [in] Context
  1296. - @param *surface [i/o] Surface to draw on it
  1297. - @param x [in] X of left-top of drawing area
  1298. - @param y [in] Y of left-top of drawing area
  1299. -*/
  1300. -void
  1301. -SDLPango_Draw(
  1302. - SDLPango_Context *context,
  1303. - SDL_Surface *surface,
  1304. - int x, int y)
  1305. -{
  1306. - PangoLayoutIter *iter;
  1307. - PangoRectangle logical_rect;
  1308. - int width, height;
  1309. -
  1310. - if(! surface) {
  1311. - SDL_SetError("surface is NULL");
  1312. - return;
  1313. - }
  1314. -
  1315. - iter = pango_layout_get_iter (context->layout);
  1316. -
  1317. - pango_layout_get_extents (context->layout, NULL, &logical_rect);
  1318. - width = PANGO_PIXELS (logical_rect.width);
  1319. - height = PANGO_PIXELS (logical_rect.height);
  1320. -
  1321. - SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
  1322. -
  1323. - if((! context->tmp_ftbitmap) || context->tmp_ftbitmap->width < width
  1324. - || context->tmp_ftbitmap->rows < height)
  1325. - {
  1326. - freeFTBitmap(context->tmp_ftbitmap);
  1327. - context->tmp_ftbitmap = createFTBitmap(width, height);
  1328. - }
  1329. -
  1330. - do {
  1331. - PangoLayoutLine *line;
  1332. - int baseline;
  1333. -
  1334. - line = pango_layout_iter_get_line (iter);
  1335. -
  1336. - pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
  1337. - baseline = pango_layout_iter_get_baseline (iter);
  1338. -
  1339. - drawLine(
  1340. - context,
  1341. - surface,
  1342. - line,
  1343. - x + PANGO_PIXELS (logical_rect.x),
  1344. - y + PANGO_PIXELS (logical_rect.y),
  1345. - PANGO_PIXELS (logical_rect.height),
  1346. - PANGO_PIXELS (baseline - logical_rect.y));
  1347. - } while (pango_layout_iter_next_line (iter));
  1348. -
  1349. - pango_layout_iter_free (iter);
  1350. -}
  1351. -
  1352. -/*!
  1353. - Allocate buffer and create a FTBitmap object.
  1354. -
  1355. - @param width [in] Width
  1356. - @param height [in] Height
  1357. - @return FTBitmap object
  1358. -*/
  1359. -static FT_Bitmap *
  1360. -createFTBitmap(
  1361. - int width, int height)
  1362. -{
  1363. - FT_Bitmap *bitmap;
  1364. - guchar *buf;
  1365. -
  1366. - bitmap = g_malloc(sizeof(FT_Bitmap));
  1367. - bitmap->width = width;
  1368. - bitmap->rows = height;
  1369. - bitmap->pitch = (width + 3) & ~3;
  1370. - bitmap->num_grays = 256;
  1371. - bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
  1372. - buf = g_malloc (bitmap->pitch * bitmap->rows);
  1373. - memset (buf, 0x00, bitmap->pitch * bitmap->rows);
  1374. - bitmap->buffer = buf;
  1375. -
  1376. - return bitmap;
  1377. -}
  1378. -
  1379. -/*!
  1380. - Free a FTBitmap object.
  1381. -
  1382. - @param *bitmap [i/o] FTbitmap to be free
  1383. -*/
  1384. -static void
  1385. -freeFTBitmap(
  1386. - FT_Bitmap *bitmap)
  1387. -{
  1388. - if(bitmap) {
  1389. - g_free(bitmap->buffer);
  1390. - g_free(bitmap);
  1391. - }
  1392. -}
  1393. -
  1394. -/*!
  1395. - Clear a FTBitmap object.
  1396. -
  1397. - @param *bitmap [i/o] FTbitmap to be clear
  1398. -*/
  1399. -static void
  1400. -clearFTBitmap(
  1401. - FT_Bitmap *bitmap)
  1402. -{
  1403. - Uint8 *p = (Uint8 *)bitmap->buffer;
  1404. - int length = bitmap->pitch * bitmap->rows;
  1405. -
  1406. - memset(p, 0, length);
  1407. -}
  1408. -
  1409. -/*!
  1410. - Specify minimum size of drawing rect.
  1411. -
  1412. - @param *context [i/o] Context
  1413. - @param width [in] Width. -1 means no wrapping mode.
  1414. - @param height [in] Height. zero/minus value means non-specified.
  1415. -*/
  1416. -void
  1417. -SDLPango_SetMinimumSize(
  1418. - SDLPango_Context *context,
  1419. - int width, int height)
  1420. -{
  1421. - int pango_width;
  1422. - if(width > 0)
  1423. - pango_width = width * PANGO_SCALE;
  1424. - else
  1425. - pango_width = -1;
  1426. - pango_layout_set_width(context->layout, pango_width);
  1427. -
  1428. - context->min_width = width;
  1429. - context->min_height = height;
  1430. -}
  1431. -
  1432. -/*!
  1433. - Specify default color.
  1434. -
  1435. - @param *context [i/o] Context
  1436. - @param *color_matrix [in] Foreground and background color
  1437. -*/
  1438. -void
  1439. -SDLPango_SetDefaultColor(
  1440. - SDLPango_Context *context,
  1441. - const SDLPango_Matrix *color_matrix)
  1442. -{
  1443. - context->color_matrix = *color_matrix;
  1444. -}
  1445. -
  1446. -/*!
  1447. - Get layout width.
  1448. -
  1449. - @param *context [in] Context
  1450. - @return Width
  1451. -*/
  1452. -int
  1453. -SDLPango_GetLayoutWidth(
  1454. - SDLPango_Context *context)
  1455. -{
  1456. - PangoRectangle logical_rect;
  1457. -
  1458. - pango_layout_get_extents (context->layout, NULL, &logical_rect);
  1459. -
  1460. - return PANGO_PIXELS (logical_rect.width);
  1461. -}
  1462. -
  1463. -/*!
  1464. - Get layout height.
  1465. -
  1466. - @param *context [in] Context
  1467. - @return Height
  1468. -*/
  1469. -int
  1470. -SDLPango_GetLayoutHeight(
  1471. - SDLPango_Context *context)
  1472. -{
  1473. - PangoRectangle logical_rect;
  1474. -
  1475. - pango_layout_get_extents (context->layout, NULL, &logical_rect);
  1476. -
  1477. - return PANGO_PIXELS (logical_rect.height);
  1478. -}
  1479. -
  1480. -/*!
  1481. - Set markup text to context.
  1482. - Text must be utf-8.
  1483. - Markup format is same as pango.
  1484. -
  1485. - @param *context [i/o] Context
  1486. - @param *markup [in] Markup text
  1487. - @param length [in] Text length. -1 means NULL-terminated text.
  1488. -*/
  1489. -void
  1490. -SDLPango_SetMarkup(
  1491. - SDLPango_Context *context,
  1492. - const char *markup,
  1493. - int length)
  1494. -{
  1495. - pango_layout_set_markup (context->layout, markup, length);
  1496. - pango_layout_set_auto_dir (context->layout, TRUE);
  1497. - pango_layout_set_alignment (context->layout, PANGO_ALIGN_LEFT);
  1498. - pango_layout_set_font_description (context->layout, context->font_desc);
  1499. -}
  1500. -
  1501. -/*!
  1502. - Set plain text to context.
  1503. - Text must be utf-8.
  1504. -
  1505. - @param *context [i/o] Context
  1506. - @param *text [in] Plain text
  1507. - @param length [in] Text length. -1 means NULL-terminated text.
  1508. -*/
  1509. -void
  1510. -SDLPango_SetText(
  1511. - SDLPango_Context *context,
  1512. - const char *text,
  1513. - int length)
  1514. -{
  1515. - pango_layout_set_attributes(context->layout, NULL);
  1516. - pango_layout_set_text (context->layout, text, length);
  1517. - pango_layout_set_auto_dir (context->layout, TRUE);
  1518. - pango_layout_set_alignment (context->layout, PANGO_ALIGN_LEFT);
  1519. - pango_layout_set_font_description (context->layout, context->font_desc);
  1520. -}
  1521. -
  1522. -/*!
  1523. - Set DPI to context.
  1524. -
  1525. - @param *context [i/o] Context
  1526. - @param dpi_x [in] X dpi
  1527. - @param dpi_y [in] Y dpi
  1528. -*/
  1529. -void
  1530. -SDLPango_SetDpi(
  1531. - SDLPango_Context *context,
  1532. - double dpi_x, double dpi_y)
  1533. -{
  1534. - pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (context->font_map), dpi_x, dpi_y);
  1535. -}
  1536. -
  1537. -/*!
  1538. - Set language to context.
  1539. -
  1540. - @param *context [i/o] Context
  1541. - @param *language_tag [in] A RFC-3066 format language tag
  1542. -*/
  1543. -void SDLCALL SDLPango_SetLanguage(
  1544. - SDLPango_Context *context,
  1545. - const char *language_tag)
  1546. -{
  1547. - pango_context_set_language (context->context, pango_language_from_string (language_tag));
  1548. -}
  1549. -
  1550. -/*!
  1551. - Set base direction to context.
  1552. -
  1553. - @param *context [i/o] Context
  1554. - @param direction [in] Direction
  1555. -*/
  1556. -void SDLCALL SDLPango_SetBaseDirection(
  1557. - SDLPango_Context *context,
  1558. - SDLPango_Direction direction)
  1559. -{
  1560. - PangoDirection pango_dir;
  1561. -
  1562. - switch(direction) {
  1563. - case SDLPANGO_DIRECTION_LTR:
  1564. - pango_dir = PANGO_DIRECTION_LTR;
  1565. - break;
  1566. - case SDLPANGO_DIRECTION_RTL:
  1567. - pango_dir = PANGO_DIRECTION_RTL;
  1568. - break;
  1569. - case SDLPANGO_DIRECTION_WEAK_LTR:
  1570. - pango_dir = PANGO_DIRECTION_WEAK_LTR;
  1571. - break;
  1572. - case SDLPANGO_DIRECTION_WEAK_RTL:
  1573. - pango_dir = PANGO_DIRECTION_WEAK_RTL;
  1574. - break;
  1575. - case SDLPANGO_DIRECTION_NEUTRAL:
  1576. - pango_dir = PANGO_DIRECTION_NEUTRAL;
  1577. - break;
  1578. - default:
  1579. - SDL_SetError("unknown direction value");
  1580. - return;
  1581. - }
  1582. -
  1583. - pango_context_set_base_dir (context->context, pango_dir);
  1584. -}
  1585. -
  1586. -/*!
  1587. - Get font map from context.
  1588. -
  1589. - @param *context [in] Context
  1590. - @return Font map
  1591. -*/
  1592. -PangoFontMap* SDLCALL SDLPango_GetPangoFontMap(
  1593. - SDLPango_Context *context)
  1594. -{
  1595. - return context->font_map;
  1596. -}
  1597. -
  1598. -/*!
  1599. - Get font description from context.
  1600. -
  1601. - @param *context [in] Context
  1602. - @return Font description
  1603. -*/
  1604. -PangoFontDescription* SDLCALL SDLPango_GetPangoFontDescription(
  1605. - SDLPango_Context *context)
  1606. -{
  1607. - return context->font_desc;
  1608. -}
  1609. -
  1610. -/*!
  1611. - Get layout from context.
  1612. -
  1613. - @param *context [in] Context
  1614. - @return Layout
  1615. -*/
  1616. -PangoLayout* SDLCALL SDLPango_GetPangoLayout(
  1617. - SDLPango_Context *context)
  1618. -{
  1619. - return context->layout;
  1620. -}
  1621. +/* SDL_Pango.c -- A companion library to SDL for working with Pango.
  1622. + Copyright (C) 2004 NAKAMURA Ken'ichi
  1623. +
  1624. + This library is free software; you can redistribute it and/or
  1625. + modify it under the terms of the GNU Lesser General Public
  1626. + License as published by the Free Software Foundation; either
  1627. + version 2.1 of the License, or (at your option) any later version.
  1628. +
  1629. + This library is distributed in the hope that it will be useful,
  1630. + but WITHOUT ANY WARRANTY; without even the implied warranty of
  1631. + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  1632. + Lesser General Public License for more details.
  1633. +
  1634. + You should have received a copy of the GNU Lesser General Public
  1635. + License along with this library; if not, write to the Free Software
  1636. + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  1637. +*/
  1638. +
  1639. +/*!
  1640. + \mainpage
  1641. +
  1642. + \section intro Introduction
  1643. +
  1644. + Pango is the text rendering engine of GNOME 2.x. SDL_Pango connects the
  1645. + engine to SDL. In Windows, pre-built binary package (MSI and merge module)
  1646. + is provided.
  1647. +
  1648. + \subsection dist Distribution
  1649. +
  1650. + If you are a game software developer, you should know the difficulties of
  1651. + distribution. So I will start to introduce SDL_Pango from the viewpoint
  1652. + of distribution.
  1653. +
  1654. + In Un*x, SDL_Pango is hard to use as system-independent module, because
  1655. + it depends on fontconfig and Pango which are designed as system-singleton
  1656. + modules. If you use SDL_Pango, your software will require those modules
  1657. + installed to target system. If your software is shipped as shrink-wrap
  1658. + package, it may cause much problem on your support desk. You should
  1659. + carefully design your installation process.
  1660. +
  1661. + In Windows, SDL_Pango is distributed as "merge module" which contains
  1662. + fontconfig and Pango. Those binaries are modified as side-by-side components.
  1663. + You should use Windows Installer and merge the module
  1664. + on your MSI package. The merge module not only contains files, but also includes
  1665. + custom action which must be run at installation.
  1666. +
  1667. + \subsection api High-level API
  1668. +
  1669. + From the viewpoint of text rendering, the heart of SDL_Pango is high-level API.
  1670. + Other text rendering APIs, like DrawText() of Windows, font and text must be
  1671. + specified separately. In SDL_Pango, font specification is embedded in text like
  1672. + HTML:
  1673. +
  1674. + \code
  1675. + <span font_family="Courier New"><i>This is Courier New and italic.</i></span>
  1676. + \endcode
  1677. +
  1678. + Color, size, subscript/superscript, obliquing, weight, and other many features
  1679. + are also available in same way.
  1680. +
  1681. + \subsection i18n Internationalized Text
  1682. +
  1683. + Internationalized text is another key feature. Text is specified by UTF-8. RTL
  1684. + script (Arabic and Hebrew) and complicated rendering (Arabic, Indic and Thai) are
  1685. + supported. You can see it with GNOME 2.x.
  1686. +
  1687. + \section get Getting Started
  1688. +
  1689. + \subsection getlatest Get latest files
  1690. +
  1691. + Get latest files from http://sourceforge.net/projects/sdlpango/ .
  1692. +
  1693. + \subsection install Install Header and Library
  1694. +
  1695. + In Windows and VS2003, I strongly recommend you to install MSI package. It contains Pango
  1696. + and fontconfig binaries which are modified as side-by-side components. It is
  1697. + nearly impossible to build them. (I spent much time to build them...)
  1698. +
  1699. + In MinGW, I recommend you to use VS2003. Otherwise you may run into the maze of
  1700. + distribution. If you insist MinGW, you should use MinGW binary archive.
  1701. +
  1702. + In Un*x, installation consists of:
  1703. +
  1704. + \code
  1705. + ./configure
  1706. + make
  1707. + make install
  1708. + \endcode
  1709. +
  1710. + \subsection inc Includes
  1711. +
  1712. + To use SDL_Pango functions in a C/C++ source code file, you must use the SDL_Pango.h
  1713. + include file:
  1714. +
  1715. + \code
  1716. + #include "SDL_Pango.h"
  1717. + \endcode
  1718. +
  1719. + In Windows, SDL_Pango.h is installed on \c \%ProgramFiles\%\\SDL_Pango \c Development\\include
  1720. + (usually \c C:\\Program \c Files\\SDL_Pango \c Development\\include). You should add this
  1721. + directory to include path.
  1722. +
  1723. + \subsection comp Compiling
  1724. +
  1725. + In Un*x, to link with SDL_Pango you should use sdl-config to get the required SDL
  1726. + compilation options. After that, compiling with SDL_Pango is quite easy.
  1727. +
  1728. + Note: Some systems may not have the SDL_Pango library and include file in the same
  1729. + place as the SDL library and includes are located, in that case you will need to
  1730. + add more -I and -L paths to these command lines.
  1731. +
  1732. + Simple Example for compiling an object file:
  1733. +
  1734. + \code
  1735. + cc -c `sdl-config --cflags` mysource.c
  1736. + \endcode
  1737. +
  1738. + Simple Example for linking an object file:
  1739. +
  1740. + \code
  1741. + cc -o myprogram mysource.o `sdl-config --libs` -lSDL_Pango
  1742. + \endcode
  1743. +
  1744. + Now myprogram is ready to run.
  1745. +
  1746. + You can see a sample of autoconfiscation in 'test' directory.
  1747. +
  1748. + In Windows, MSI package installs many dlls to \c \%ProgramFiles\%\\SDL_Pango \c Development\\import_lib.
  1749. + To link with SDL_Pango you should use SDL_Pango.lib.
  1750. +
  1751. + SDL_Pango.dll depends on many dlls and other many files. Those dlls are installed on
  1752. + \c \%ProgramFiles\%\\SDL_Pango \c Development\\bin. MSI package adds the directory to PATH environment
  1753. + variable.
  1754. +
  1755. + \section devel Development
  1756. +
  1757. + \subsection font Font Handling
  1758. +
  1759. + In Un*x, font handling depends on fontconfig of your system.
  1760. +
  1761. + In Windows, local.conf of fontconfig is placed on \c \%ProgramFiles\%\\SDL_Pango \c Development\\etc\\fonts.
  1762. + You should know about fontconfig's font cache mechanism.
  1763. +
  1764. + \subsection example Step-by-step Example
  1765. +
  1766. + The operation of SDL_Pango is done via context.
  1767. +
  1768. + \code
  1769. + SDLPango_Context *context = SDLPango_CreateContext();
  1770. + \endcode
  1771. +
  1772. + Specify default colors and minimum surface size.
  1773. +
  1774. + \code
  1775. + SDLPango_SetDefaultColor(context, MATRIX_TRANSPARENT_BACK_WHITE_LETTER);
  1776. + SDLPango_SetMinimumSize(context, 640, 0);
  1777. + \endcode
  1778. +
  1779. + Set markup text.
  1780. +
  1781. + \code
  1782. + SDLPango_SetMarkup(context, "This is <i>markup</i> text.", -1);
  1783. + \endcode
  1784. +
  1785. + Now you can get the size of surface.
  1786. +
  1787. + \code
  1788. + int w = SDLPango_GetLayoutWidth(context);
  1789. + int h = SDLPango_GetLayoutHeight(context);
  1790. + \endcode
  1791. +
  1792. + Create surface to draw.
  1793. +
  1794. + \code
  1795. + int margin_x = 10;
  1796. + int margin_y = 10;
  1797. + SDL_Surface *surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
  1798. + w + margin_x * 2, h + margin_y * 2,
  1799. + 32, (Uint32)(255 << (8 * 3)), (Uint32)(255 << (8 * 2)),
  1800. + (Uint32)(255 << (8 * 1)), 255);
  1801. + \endcode
  1802. +
  1803. + And draw on it.
  1804. +
  1805. + \code
  1806. + SDLPango_Draw(context, surface, margin_x, margin_y);
  1807. + \endcode
  1808. +
  1809. + You must free the surface by yourself.
  1810. +
  1811. + \code
  1812. + SDL_FreeSurface(surface);
  1813. + \endcode
  1814. +
  1815. + Free context.
  1816. +
  1817. + \code
  1818. + SDLPango_FreeContext(context);
  1819. + \endcode
  1820. +
  1821. + You can see actual code in \c test/testbench.cpp.
  1822. +
  1823. + \subsection pack Packaging
  1824. +
  1825. + In Un*x, do it yourself.
  1826. +
  1827. + In Windows, font files must be installed on apprication folder (usually
  1828. + \c C:\\Program \c Files\\[Manufacturer]\\[ProductName]). The property of
  1829. + apprication folder must be \c TARGETDIR (this is default setting of VS2003).
  1830. + SDL.dll also must be installed on apprication folder. Add SDL_Pango.msm to
  1831. + your MSI package.
  1832. +
  1833. + \section ack Acknowledgment
  1834. +
  1835. + SDL_Pango is developed with financial assistance of Information-technology Promotion Agency, Japan.
  1836. +
  1837. +- NAKAMURA Ken'ichi <nakamura@sbp.fp.a.u-tokyo.ac.jp>
  1838. +
  1839. +*/
  1840. +
  1841. +/*! @file
  1842. + @brief Implementation of SDL_Pango
  1843. +
  1844. + @author NAKAMURA Ken'ichi
  1845. + @date 2004/12/07
  1846. + $Revision: 1.6 $
  1847. +*/
  1848. +
  1849. +#include <pango/pango.h>
  1850. +#include <pango/pangoft2.h>
  1851. +
  1852. +#include "SDL_Pango.h"
  1853. +
  1854. +//! non-zero if initialized
  1855. +static int IS_INITIALIZED = 0;
  1856. +
  1857. +#define DEFAULT_FONT_FAMILY "Sans"
  1858. +#define DEFAULT_FONT_SIZE 12
  1859. +#define DEFAULT_DPI 96
  1860. +#define _MAKE_FONT_NAME(family, size) family " " #size
  1861. +#define MAKE_FONT_NAME(family, size) _MAKE_FONT_NAME(family, size)
  1862. +#define DEFAULT_DEPTH 32
  1863. +#define DEFAULT_RMASK (Uint32)(255 << (8 * 3))
  1864. +#define DEFAULT_GMASK (Uint32)(255 << (8 * 2))
  1865. +#define DEFAULT_BMASK (Uint32)(255 << (8 * 1))
  1866. +#define DEFAULT_AMASK (Uint32)255
  1867. +
  1868. +static FT_Bitmap *createFTBitmap(int width, int height);
  1869. +
  1870. +static void freeFTBitmap(FT_Bitmap *bitmap);
  1871. +
  1872. +static void getItemProperties (
  1873. + PangoItem *item,
  1874. + PangoUnderline *uline,
  1875. + gboolean *strikethrough,
  1876. + gint *rise,
  1877. + PangoColor *fg_color,
  1878. + gboolean *fg_set,
  1879. + PangoColor *bg_color,
  1880. + gboolean *bg_set,
  1881. + gboolean *shape_set,
  1882. + PangoRectangle *ink_rect,
  1883. + PangoRectangle *logical_rect);
  1884. +
  1885. +static void clearFTBitmap(FT_Bitmap *bitmap);
  1886. +
  1887. +typedef struct _surfaceArgs {
  1888. + Uint32 flags;
  1889. + int depth;
  1890. + Uint32 Rmask;
  1891. + Uint32 Gmask;
  1892. + Uint32 Bmask;
  1893. + Uint32 Amask;
  1894. +} surfaceArgs;
  1895. +
  1896. +typedef struct _contextImpl {
  1897. + PangoContext *context;
  1898. + PangoFontMap *font_map;
  1899. + PangoFontDescription *font_desc;
  1900. + PangoLayout *layout;
  1901. + surfaceArgs surface_args;
  1902. + FT_Bitmap *tmp_ftbitmap;
  1903. + SDLPango_Matrix color_matrix;
  1904. + int min_width;
  1905. + int min_height;
  1906. +} contextImpl;
  1907. +
  1908. +
  1909. +/*!
  1910. + Initialize the Glib and Pango API.
  1911. + This must be called before using other functions in this library,
  1912. + excepting SDLPango_WasInit.
  1913. + SDL does not have to be initialized before this call.
  1914. +
  1915. +
  1916. + @return always 0.
  1917. +*/
  1918. +int
  1919. +SDLPango_Init()
  1920. +{
  1921. + g_type_init();
  1922. +
  1923. + IS_INITIALIZED = -1;
  1924. +
  1925. + return 0;
  1926. +}
  1927. +
  1928. +/*!
  1929. + Query the initilization status of the Glib and Pango API.
  1930. + You may, of course, use this before SDLPango_Init to avoid
  1931. + initilizing twice in a row.
  1932. +
  1933. + @return zero when already initialized.
  1934. + non-zero when not initialized.
  1935. +*/
  1936. +int
  1937. +SDLPango_WasInit()
  1938. +{
  1939. + return IS_INITIALIZED;
  1940. +}
  1941. +
  1942. +/*!
  1943. + Draw glyphs on rect.
  1944. +
  1945. + @param *context [in] Context
  1946. + @param *surface [out] Surface to draw on it
  1947. + @param *color_matrix [in] Foreground and background color
  1948. + @param *font [in] Innter variable of Pango
  1949. + @param *glyphs [in] Innter variable of Pango
  1950. + @param *rect [in] Draw on this area
  1951. + @param baseline [in] Horizontal location of glyphs
  1952. +*/
  1953. +static void
  1954. +drawGlyphString(
  1955. + SDLPango_Context *context,
  1956. + SDL_Surface *surface,
  1957. + SDLPango_Matrix *color_matrix,
  1958. + PangoFont *font,
  1959. + PangoGlyphString *glyphs,
  1960. + SDL_Rect *rect,
  1961. + int baseline)
  1962. +{
  1963. + pango_ft2_render(context->tmp_ftbitmap, font, glyphs, rect->x, rect->y + baseline);
  1964. +
  1965. + SDLPango_CopyFTBitmapToSurface(
  1966. + context->tmp_ftbitmap,
  1967. + surface,
  1968. + color_matrix,
  1969. + rect);
  1970. +
  1971. + clearFTBitmap(context->tmp_ftbitmap);
  1972. +}
  1973. +
  1974. +/*!
  1975. + Draw horizontal line of a pixel.
  1976. +
  1977. + @param *surface [out] Surface to draw on it
  1978. + @param *color_matrix [in] Foreground and background color
  1979. + @param y [in] Y location of line
  1980. + @param start [in] Left of line
  1981. + @param end [in] Right of line
  1982. +*/
  1983. +static void drawHLine(
  1984. + SDL_Surface *surface,
  1985. + SDLPango_Matrix *color_matrix,
  1986. + int y,
  1987. + int start,
  1988. + int end)
  1989. +{
  1990. + Uint8 *p;
  1991. + Uint16 *p16;
  1992. + Uint32 *p32;
  1993. + Uint32 color;
  1994. + int ix;
  1995. + int pixel_bytes = surface->format->BytesPerPixel;
  1996. +
  1997. + if (y < 0 || y >= surface->h)
  1998. + return;
  1999. +
  2000. + if (end <= 0 || start >= surface->w)
  2001. + return;
  2002. +
  2003. + if (start < 0)
  2004. + start = 0;
  2005. +
  2006. + if (end >= surface->w)
  2007. + end = surface->w;
  2008. +
  2009. + p = (Uint8 *)(surface->pixels) + y * surface->pitch + start * pixel_bytes;
  2010. + color = SDL_MapRGBA(surface->format,
  2011. + color_matrix->m[0][1],
  2012. + color_matrix->m[1][1],
  2013. + color_matrix->m[2][1],
  2014. + color_matrix->m[3][1]);
  2015. +
  2016. + switch(pixel_bytes) {
  2017. + case 2:
  2018. + p16 = (Uint16 *)p;
  2019. + for (ix = 0; ix < end - start; ix++)
  2020. + *p16++ = (Uint16)color;
  2021. + break;
  2022. + case 4:
  2023. + p32 = (Uint32 *)p;
  2024. + for (ix = 0; ix < end - start; ix++)
  2025. + *p32++ = color;
  2026. + break;
  2027. + default:
  2028. + SDL_SetError("surface->format->BytesPerPixel is invalid value");
  2029. + break;
  2030. + }
  2031. +}
  2032. +
  2033. +/*!
  2034. + Draw a line.
  2035. +
  2036. + @param *context [in] Context
  2037. + @param *surface [out] Surface to draw on it
  2038. + @param *line [in] Innter variable of Pango
  2039. + @param x [in] X location of line
  2040. + @param y [in] Y location of line
  2041. + @param height [in] Height of line
  2042. + @param baseline [in] Rise / sink of line (for super/subscript)
  2043. +*/
  2044. +static void
  2045. +drawLine(
  2046. + SDLPango_Context *context,
  2047. + SDL_Surface *surface,
  2048. + PangoLayoutLine *line,
  2049. + gint x,
  2050. + gint y,
  2051. + gint height,
  2052. + gint baseline)
  2053. +{
  2054. + GSList *tmp_list = line->runs;
  2055. + PangoColor fg_color, bg_color;
  2056. + PangoRectangle logical_rect;
  2057. + PangoRectangle ink_rect;
  2058. + int x_off = 0;
  2059. +
  2060. + while (tmp_list) {
  2061. + SDLPango_Matrix color_matrix = context->color_matrix;
  2062. + PangoUnderline uline = PANGO_UNDERLINE_NONE;
  2063. + gboolean strike, fg_set, bg_set, shape_set;
  2064. + gint rise, risen_y;
  2065. + PangoLayoutRun *run = tmp_list->data;
  2066. + SDL_Rect d_rect;
  2067. +
  2068. + tmp_list = tmp_list->next;
  2069. +
  2070. + getItemProperties(run->item,
  2071. + &uline, &strike, &rise,
  2072. + &fg_color, &fg_set, &bg_color, &bg_set,
  2073. + &shape_set, &ink_rect, &logical_rect);
  2074. +
  2075. + risen_y = y + baseline - PANGO_PIXELS (rise);
  2076. +
  2077. + if(fg_set) {
  2078. + color_matrix.m[0][1] = (Uint8)(fg_color.red >> 8);
  2079. + color_matrix.m[1][1] = (Uint8)(fg_color.green >> 8);
  2080. + color_matrix.m[2][1] = (Uint8)(fg_color.blue >> 8);
  2081. + color_matrix.m[3][1] = 255;
  2082. + if(color_matrix.m[3][0] == 0) {
  2083. + color_matrix.m[0][0] = (Uint8)(fg_color.red >> 8);
  2084. + color_matrix.m[1][0] = (Uint8)(fg_color.green >> 8);
  2085. + color_matrix.m[2][0] = (Uint8)(fg_color.blue >> 8);
  2086. + }
  2087. + }
  2088. +
  2089. + if (bg_set) {
  2090. + color_matrix.m[0][0] = (Uint8)(bg_color.red >> 8);
  2091. + color_matrix.m[1][0] = (Uint8)(bg_color.green >> 8);
  2092. + color_matrix.m[2][0] = (Uint8)(bg_color.blue >> 8);
  2093. + color_matrix.m[3][0] = 255;
  2094. + }
  2095. +
  2096. + if(! shape_set) {
  2097. + if (uline == PANGO_UNDERLINE_NONE)
  2098. + pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
  2099. + NULL, &logical_rect);
  2100. + else
  2101. + pango_glyph_string_extents (run->glyphs, run->item->analysis.font,
  2102. + &ink_rect, &logical_rect);
  2103. +
  2104. + d_rect.w = (Uint16)PANGO_PIXELS(logical_rect.width);
  2105. + d_rect.h = (Uint16)height;
  2106. + d_rect.x = (Uint16)(x + PANGO_PIXELS (x_off));
  2107. + d_rect.y = (Uint16)(risen_y - baseline);
  2108. +
  2109. + if((! context->tmp_ftbitmap) || d_rect.w + d_rect.x > context->tmp_ftbitmap->width
  2110. + || d_rect.h + d_rect.y > context->tmp_ftbitmap->rows)
  2111. + {
  2112. + freeFTBitmap(context->tmp_ftbitmap);
  2113. + context->tmp_ftbitmap = createFTBitmap(d_rect.w + d_rect.x, d_rect.h + d_rect.y);
  2114. + }
  2115. +
  2116. + drawGlyphString(context, surface,
  2117. + &color_matrix,
  2118. + run->item->analysis.font, run->glyphs, &d_rect, baseline);
  2119. + }
  2120. + switch (uline) {
  2121. + case PANGO_UNDERLINE_NONE:
  2122. + break;
  2123. + case PANGO_UNDERLINE_DOUBLE:
  2124. + drawHLine(surface, &color_matrix,
  2125. + risen_y + 4,
  2126. + x + PANGO_PIXELS (x_off + ink_rect.x),
  2127. + x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  2128. + /* Fall through */
  2129. + case PANGO_UNDERLINE_SINGLE:
  2130. + drawHLine(surface, &color_matrix,
  2131. + risen_y + 2,
  2132. + x + PANGO_PIXELS (x_off + ink_rect.x),
  2133. + x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  2134. + break;
  2135. + case PANGO_UNDERLINE_ERROR:
  2136. + {
  2137. + int point_x;
  2138. + int counter = 0;
  2139. + int end_x = x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width);
  2140. +
  2141. + for (point_x = x + PANGO_PIXELS (x_off + ink_rect.x) - 1;
  2142. + point_x <= end_x;
  2143. + point_x += 2)
  2144. + {
  2145. + if (counter)
  2146. + drawHLine(surface, &color_matrix,
  2147. + risen_y + 2,
  2148. + point_x, MIN (point_x + 1, end_x));
  2149. + else
  2150. + drawHLine(surface, &color_matrix,
  2151. + risen_y + 3,
  2152. + point_x, MIN (point_x + 1, end_x));
  2153. +
  2154. + counter = (counter + 1) % 2;
  2155. + }
  2156. + }
  2157. + break;
  2158. + case PANGO_UNDERLINE_LOW:
  2159. + drawHLine(surface, &color_matrix,
  2160. + risen_y + PANGO_PIXELS (ink_rect.y + ink_rect.height),
  2161. + x + PANGO_PIXELS (x_off + ink_rect.x),
  2162. + x + PANGO_PIXELS (x_off + ink_rect.x + ink_rect.width));
  2163. + break;
  2164. + }
  2165. +
  2166. + if (strike)
  2167. + drawHLine(surface, &color_matrix,
  2168. + risen_y + PANGO_PIXELS (logical_rect.y + logical_rect.height / 2),
  2169. + x + PANGO_PIXELS (x_off + logical_rect.x),
  2170. + x + PANGO_PIXELS (x_off + logical_rect.x + logical_rect.width));
  2171. +
  2172. + x_off += logical_rect.width;
  2173. + }
  2174. +}
  2175. +
  2176. +/*!
  2177. + Innter function of Pango. Stolen from GDK.
  2178. +
  2179. + @param *item [in] The item to get property
  2180. + @param *uline [out] Kind of underline
  2181. + @param *strikethrough [out] Strike-through line
  2182. + @param *rise [out] Rise/sink of line (for super/subscript)
  2183. + @param *fg_color [out] Color of foreground
  2184. + @param *fg_set [out] True if fg_color set
  2185. + @param *bg_color [out] Color of background
  2186. + @param *bg_set [out] True if bg_color valid
  2187. + @param *shape_set [out] True if ink_rect and logical_rect valid
  2188. + @param *ink_rect [out] Ink rect
  2189. + @param *logical_rect [out] Logical rect
  2190. +*/
  2191. +static void
  2192. +getItemProperties (
  2193. + PangoItem *item,
  2194. + PangoUnderline *uline,
  2195. + gboolean *strikethrough,
  2196. + gint *rise,
  2197. + PangoColor *fg_color,
  2198. + gboolean *fg_set,
  2199. + PangoColor *bg_color,
  2200. + gboolean *bg_set,
  2201. + gboolean *shape_set,
  2202. + PangoRectangle *ink_rect,
  2203. + PangoRectangle *logical_rect)
  2204. +{
  2205. + GSList *tmp_list = item->analysis.extra_attrs;
  2206. +
  2207. + if (strikethrough)
  2208. + *strikethrough = FALSE;
  2209. +
  2210. + if (fg_set)
  2211. + *fg_set = FALSE;
  2212. +
  2213. + if (bg_set)
  2214. + *bg_set = FALSE;
  2215. +
  2216. + if (shape_set)
  2217. + *shape_set = FALSE;
  2218. +
  2219. + if (rise)
  2220. + *rise = 0;
  2221. +
  2222. + while (tmp_list) {
  2223. + PangoAttribute *attr = tmp_list->data;
  2224. +
  2225. + switch (attr->klass->type) {
  2226. + case PANGO_ATTR_UNDERLINE:
  2227. + if (uline)
  2228. + *uline = ((PangoAttrInt *)attr)->value;
  2229. + break;
  2230. +
  2231. + case PANGO_ATTR_STRIKETHROUGH:
  2232. + if (strikethrough)
  2233. + *strikethrough = ((PangoAttrInt *)attr)->value;
  2234. + break;
  2235. +
  2236. + case PANGO_ATTR_FOREGROUND:
  2237. + if (fg_color)
  2238. + *fg_color = ((PangoAttrColor *)attr)->color;
  2239. + if (fg_set)
  2240. + *fg_set = TRUE;
  2241. + break;
  2242. +
  2243. + case PANGO_ATTR_BACKGROUND:
  2244. + if (bg_color)
  2245. + *bg_color = ((PangoAttrColor *)attr)->color;
  2246. + if (bg_set)
  2247. + *bg_set = TRUE;
  2248. + break;
  2249. +
  2250. + case PANGO_ATTR_SHAPE:
  2251. + if (shape_set)
  2252. + *shape_set = TRUE;
  2253. + if (logical_rect)
  2254. + *logical_rect = ((PangoAttrShape *)attr)->logical_rect;
  2255. + if (ink_rect)
  2256. + *ink_rect = ((PangoAttrShape *)attr)->ink_rect;
  2257. + break;
  2258. +
  2259. + case PANGO_ATTR_RISE:
  2260. + if (rise)
  2261. + *rise = ((PangoAttrInt *)attr)->value;
  2262. + break;
  2263. +
  2264. + default:
  2265. + break;
  2266. + }
  2267. + tmp_list = tmp_list->next;
  2268. + }
  2269. +}
  2270. +
  2271. +/*!
  2272. + Copy bitmap to surface.
  2273. + From (x, y)-(w, h) to (x, y)-(w, h) of rect.
  2274. +
  2275. + @param *bitmap [in] Grayscale bitmap
  2276. + @param *surface [out] Surface
  2277. + @param *matrix [in] Foreground and background color
  2278. + @param *rect [in] Rect to copy
  2279. +*/
  2280. +void
  2281. +SDLPango_CopyFTBitmapToSurface(
  2282. + const FT_Bitmap *bitmap,
  2283. + SDL_Surface *surface,
  2284. + const SDLPango_Matrix *matrix,
  2285. + SDL_Rect *rect)
  2286. +{
  2287. + int i;
  2288. + Uint8 *p_ft;
  2289. + Uint8 *p_sdl;
  2290. + int width = rect->w;
  2291. + int height = rect->h;
  2292. + int x = rect->x;
  2293. + int y = rect->y;
  2294. +
  2295. + if(x + width > surface->w) {
  2296. + width = surface->w - x;
  2297. + if(width <= 0)
  2298. + return;
  2299. + }
  2300. + if(y + height > surface->h) {
  2301. + height = surface->h - y;
  2302. + if(height <= 0)
  2303. + return;
  2304. + }
  2305. +
  2306. + if(SDL_LockSurface(surface)) {
  2307. + SDL_SetError("surface lock failed");
  2308. + SDL_FreeSurface(surface);
  2309. + return;
  2310. + }
  2311. +
  2312. + p_ft = (Uint8 *)bitmap->buffer + (bitmap->pitch * y);
  2313. + p_sdl = (Uint8 *)surface->pixels + (surface->pitch * y);
  2314. + for(i = 0; i < height; i ++) {
  2315. + int k;
  2316. + for(k = 0; k < width; k ++) {
  2317. + /* TODO: rewrite by matrix calculation library */
  2318. + Uint8 pixel[4]; /* 4: RGBA */
  2319. + int n;
  2320. +
  2321. + for(n = 0; n < 4; n ++) {
  2322. + Uint16 w;
  2323. + w = ((Uint16)matrix->m[n][0] * (256 - p_ft[k + x])) + ((Uint16)matrix->m[n][1] * p_ft[k + x]);
  2324. + pixel[n] = (Uint8)(w >> 8);
  2325. + }
  2326. +
  2327. + switch(surface->format->BytesPerPixel) {
  2328. + case 2:
  2329. + ((Uint16 *)p_sdl)[k + x] = (Uint16)SDL_MapRGBA(surface->format, pixel[0], pixel[1], pixel[2], pixel[3]);
  2330. + break;
  2331. + case 4:
  2332. + ((Uint32 *)p_sdl)[k + x] = SDL_MapRGBA(surface->format, pixel[0], pixel[1], pixel[2], pixel[3]);
  2333. + break;
  2334. + default:
  2335. + SDL_SetError("surface->format->BytesPerPixel is invalid value");
  2336. + return;
  2337. + }
  2338. + }
  2339. + p_ft += bitmap->pitch;
  2340. + p_sdl += surface->pitch;
  2341. + }
  2342. +
  2343. + SDL_UnlockSurface(surface);
  2344. +}
  2345. +
  2346. +SDLPango_Context*
  2347. +SDLPango_CreateContext_GivenFontDesc(const char* font_desc)
  2348. +{
  2349. + SDLPango_Context *context = g_malloc(sizeof(SDLPango_Context));
  2350. + G_CONST_RETURN char *charset;
  2351. +
  2352. + context->font_map = pango_ft2_font_map_new ();
  2353. + pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (context->font_map), DEFAULT_DPI, DEFAULT_DPI);
  2354. +
  2355. + context->context = pango_ft2_font_map_create_context (PANGO_FT2_FONT_MAP (context->font_map));
  2356. +
  2357. + g_get_charset(&charset);
  2358. + pango_context_set_language (context->context, pango_language_from_string (charset));
  2359. + pango_context_set_base_dir (context->context, PANGO_DIRECTION_LTR);
  2360. +
  2361. + context->font_desc = pango_font_description_from_string(font_desc);
  2362. +
  2363. + context->layout = pango_layout_new (context->context);
  2364. +
  2365. + SDLPango_SetSurfaceCreateArgs(context, SDL_SWSURFACE | SDL_SRCALPHA, DEFAULT_DEPTH,
  2366. + DEFAULT_RMASK, DEFAULT_GMASK, DEFAULT_BMASK, DEFAULT_AMASK);
  2367. +
  2368. + context->tmp_ftbitmap = NULL;
  2369. +
  2370. + context->color_matrix = *MATRIX_TRANSPARENT_BACK_BLACK_LETTER;
  2371. +
  2372. + context->min_height = 0;
  2373. + context->min_width = 0;
  2374. +
  2375. + return context;
  2376. +}
  2377. +
  2378. +/*!
  2379. + Create a context which contains Pango objects.
  2380. +
  2381. + @return A pointer to the context as a SDLPango_Context*.
  2382. +*/
  2383. +SDLPango_Context*
  2384. +SDLPango_CreateContext()
  2385. +{
  2386. + SDLPango_CreateContext_GivenFontDesc(MAKE_FONT_NAME(DEFAULT_FONT_FAMILY, DEFAULT_FONT_SIZE));
  2387. +}
  2388. +
  2389. +/*!
  2390. + Free a context.
  2391. +
  2392. + @param *context [i/o] Context to be free
  2393. +*/
  2394. +void
  2395. +SDLPango_FreeContext(SDLPango_Context *context)
  2396. +{
  2397. + freeFTBitmap(context->tmp_ftbitmap);
  2398. +
  2399. + g_object_unref (context->layout);
  2400. +
  2401. + pango_font_description_free(context->font_desc);
  2402. +
  2403. + g_object_unref(context->context);
  2404. +
  2405. + g_object_unref(context->font_map);
  2406. +
  2407. + g_free(context);
  2408. +}
  2409. +
  2410. +/*!
  2411. + Specify Arguments when create a surface.
  2412. + When SDL_Pango creates a surface, the arguments are used.
  2413. +
  2414. + @param *context [i/o] Context
  2415. + @param flags [in] Same as SDL_CreateRGBSurface()
  2416. + @param depth [in] Same as SDL_CreateRGBSurface()
  2417. + @param Rmask [in] Same as SDL_CreateRGBSurface()
  2418. + @param Gmask [in] Same as SDL_CreateRGBSurface()
  2419. + @param Bmask [in] Same as SDL_CreateRGBSurface()
  2420. + @param Amask [in] Same as SDL_CreateRGBSurface()
  2421. +*/
  2422. +void
  2423. +SDLPango_SetSurfaceCreateArgs(
  2424. + SDLPango_Context *context,
  2425. + Uint32 flags,
  2426. + int depth,
  2427. + Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask)
  2428. +{
  2429. + context->surface_args.flags = flags;
  2430. + context->surface_args.depth = depth;
  2431. + context->surface_args.Rmask = Rmask;
  2432. + context->surface_args.Gmask = Gmask;
  2433. + context->surface_args.Bmask = Bmask;
  2434. + context->surface_args.Amask = Amask;
  2435. +}
  2436. +
  2437. +/*!
  2438. + Create a surface and draw text on it.
  2439. + The size of surface is same as lauout size.
  2440. +
  2441. + @param *context [in] Context
  2442. + @return A newly created surface
  2443. +*/
  2444. +SDL_Surface * SDLPango_CreateSurfaceDraw(
  2445. + SDLPango_Context *context)
  2446. +{
  2447. + PangoRectangle logical_rect;
  2448. + SDL_Surface *surface;
  2449. + int width, height;
  2450. +
  2451. + pango_layout_get_extents (context->layout, NULL, &logical_rect);
  2452. + width = PANGO_PIXELS (logical_rect.width);
  2453. + height = PANGO_PIXELS (logical_rect.height);
  2454. + if(width < context->min_width)
  2455. + width = context->min_width;
  2456. + if(height < context->min_height)
  2457. + height = context->min_height;
  2458. +
  2459. + surface = SDL_CreateRGBSurface(
  2460. + context->surface_args.flags,
  2461. + width, height, context->surface_args.depth,
  2462. + context->surface_args.Rmask,
  2463. + context->surface_args.Gmask,
  2464. + context->surface_args.Bmask,
  2465. + context->surface_args.Amask);
  2466. +
  2467. + SDLPango_Draw(context, surface, 0, 0);
  2468. +
  2469. + return surface;
  2470. +}
  2471. +
  2472. +/*!
  2473. + Draw text on a existing surface.
  2474. +
  2475. + @param *context [in] Context
  2476. + @param *surface [i/o] Surface to draw on it
  2477. + @param x [in] X of left-top of drawing area
  2478. + @param y [in] Y of left-top of drawing area
  2479. +*/
  2480. +void
  2481. +SDLPango_Draw(
  2482. + SDLPango_Context *context,
  2483. + SDL_Surface *surface,
  2484. + int x, int y)
  2485. +{
  2486. + PangoLayoutIter *iter;
  2487. + PangoRectangle logical_rect;
  2488. + int width, height;
  2489. +
  2490. + if(! surface) {
  2491. + SDL_SetError("surface is NULL");
  2492. + return;
  2493. + }
  2494. +
  2495. + iter = pango_layout_get_iter (context->layout);
  2496. +
  2497. + pango_layout_get_extents (context->layout, NULL, &logical_rect);
  2498. + width = PANGO_PIXELS (logical_rect.width);
  2499. + height = PANGO_PIXELS (logical_rect.height);
  2500. +
  2501. + SDL_FillRect(surface, NULL, SDL_MapRGBA(surface->format, 0, 0, 0, 0));
  2502. +
  2503. + if((! context->tmp_ftbitmap) || context->tmp_ftbitmap->width < width
  2504. + || context->tmp_ftbitmap->rows < height)
  2505. + {
  2506. + freeFTBitmap(context->tmp_ftbitmap);
  2507. + context->tmp_ftbitmap = createFTBitmap(width, height);
  2508. + }
  2509. +
  2510. + do {
  2511. + PangoLayoutLine *line;
  2512. + int baseline;
  2513. +
  2514. + line = pango_layout_iter_get_line (iter);
  2515. +
  2516. + pango_layout_iter_get_line_extents (iter, NULL, &logical_rect);
  2517. + baseline = pango_layout_iter_get_baseline (iter);
  2518. +
  2519. + drawLine(
  2520. + context,
  2521. + surface,
  2522. + line,
  2523. + x + PANGO_PIXELS (logical_rect.x),
  2524. + y + PANGO_PIXELS (logical_rect.y),
  2525. + PANGO_PIXELS (logical_rect.height),
  2526. + PANGO_PIXELS (baseline - logical_rect.y));
  2527. + } while (pango_layout_iter_next_line (iter));
  2528. +
  2529. + pango_layout_iter_free (iter);
  2530. +}
  2531. +
  2532. +/*!
  2533. + Allocate buffer and create a FTBitmap object.
  2534. +
  2535. + @param width [in] Width
  2536. + @param height [in] Height
  2537. + @return FTBitmap object
  2538. +*/
  2539. +static FT_Bitmap *
  2540. +createFTBitmap(
  2541. + int width, int height)
  2542. +{
  2543. + FT_Bitmap *bitmap;
  2544. + guchar *buf;
  2545. +
  2546. + bitmap = g_malloc(sizeof(FT_Bitmap));
  2547. + bitmap->width = width;
  2548. + bitmap->rows = height;
  2549. + bitmap->pitch = (width + 3) & ~3;
  2550. + bitmap->num_grays = 256;
  2551. + bitmap->pixel_mode = FT_PIXEL_MODE_GRAY;
  2552. + buf = g_malloc (bitmap->pitch * bitmap->rows);
  2553. + memset (buf, 0x00, bitmap->pitch * bitmap->rows);
  2554. + bitmap->buffer = buf;
  2555. +
  2556. + return bitmap;
  2557. +}
  2558. +
  2559. +/*!
  2560. + Free a FTBitmap object.
  2561. +
  2562. + @param *bitmap [i/o] FTbitmap to be free
  2563. +*/
  2564. +static void
  2565. +freeFTBitmap(
  2566. + FT_Bitmap *bitmap)
  2567. +{
  2568. + if(bitmap) {
  2569. + g_free(bitmap->buffer);
  2570. + g_free(bitmap);
  2571. + }
  2572. +}
  2573. +
  2574. +/*!
  2575. + Clear a FTBitmap object.
  2576. +
  2577. + @param *bitmap [i/o] FTbitmap to be clear
  2578. +*/
  2579. +static void
  2580. +clearFTBitmap(
  2581. + FT_Bitmap *bitmap)
  2582. +{
  2583. + Uint8 *p = (Uint8 *)bitmap->buffer;
  2584. + int length = bitmap->pitch * bitmap->rows;
  2585. +
  2586. + memset(p, 0, length);
  2587. +}
  2588. +
  2589. +/*!
  2590. + Specify minimum size of drawing rect.
  2591. +
  2592. + @param *context [i/o] Context
  2593. + @param width [in] Width. -1 means no wrapping mode.
  2594. + @param height [in] Height. zero/minus value means non-specified.
  2595. +*/
  2596. +void
  2597. +SDLPango_SetMinimumSize(
  2598. + SDLPango_Context *context,
  2599. + int width, int height)
  2600. +{
  2601. + int pango_width;
  2602. + if(width > 0)
  2603. + pango_width = width * PANGO_SCALE;
  2604. + else
  2605. + pango_width = -1;
  2606. + pango_layout_set_width(context->layout, pango_width);
  2607. +
  2608. + context->min_width = width;
  2609. + context->min_height = height;
  2610. +}
  2611. +
  2612. +/*!
  2613. + Specify default color.
  2614. +
  2615. + @param *context [i/o] Context
  2616. + @param *color_matrix [in] Foreground and background color
  2617. +*/
  2618. +void
  2619. +SDLPango_SetDefaultColor(
  2620. + SDLPango_Context *context,
  2621. + const SDLPango_Matrix *color_matrix)
  2622. +{
  2623. + context->color_matrix = *color_matrix;
  2624. +}
  2625. +
  2626. +/*!
  2627. + Get layout width.
  2628. +
  2629. + @param *context [in] Context
  2630. + @return Width
  2631. +*/
  2632. +int
  2633. +SDLPango_GetLayoutWidth(
  2634. + SDLPango_Context *context)
  2635. +{
  2636. + PangoRectangle logical_rect;
  2637. +
  2638. + pango_layout_get_extents (context->layout, NULL, &logical_rect);
  2639. +
  2640. + return PANGO_PIXELS (logical_rect.width);
  2641. +}
  2642. +
  2643. +/*!
  2644. + Get layout height.
  2645. +
  2646. + @param *context [in] Context
  2647. + @return Height
  2648. +*/
  2649. +int
  2650. +SDLPango_GetLayoutHeight(
  2651. + SDLPango_Context *context)
  2652. +{
  2653. + PangoRectangle logical_rect;
  2654. +
  2655. + pango_layout_get_extents (context->layout, NULL, &logical_rect);
  2656. +
  2657. + return PANGO_PIXELS (logical_rect.height);
  2658. +}
  2659. +
  2660. +/*!
  2661. + Set markup text to context.
  2662. + Text must be utf-8.
  2663. + Markup format is same as pango.
  2664. +
  2665. + @param *context [i/o] Context
  2666. + @param *markup [in] Markup text
  2667. + @param length [in] Text length. -1 means NULL-terminated text.
  2668. +*/
  2669. +void
  2670. +SDLPango_SetMarkup(
  2671. + SDLPango_Context *context,
  2672. + const char *markup,
  2673. + int length)
  2674. +{
  2675. + pango_layout_set_markup (context->layout, markup, length);
  2676. + pango_layout_set_auto_dir (context->layout, TRUE);
  2677. + pango_layout_set_alignment (context->layout, PANGO_ALIGN_LEFT);
  2678. + pango_layout_set_font_description (context->layout, context->font_desc);
  2679. +}
  2680. +
  2681. +void
  2682. +SDLPango_SetText_GivenAlignment(
  2683. + SDLPango_Context *context,
  2684. + const char *text,
  2685. + int length,
  2686. + SDLPango_Alignment alignment)
  2687. +{
  2688. + pango_layout_set_attributes(context->layout, NULL);
  2689. + pango_layout_set_text (context->layout, text, length);
  2690. + pango_layout_set_auto_dir (context->layout, TRUE);
  2691. + pango_layout_set_alignment (context->layout, alignment);
  2692. + pango_layout_set_font_description (context->layout, context->font_desc);
  2693. +}
  2694. +
  2695. +/*!
  2696. + Set plain text to context.
  2697. + Text must be utf-8.
  2698. +
  2699. + @param *context [i/o] Context
  2700. + @param *text [in] Plain text
  2701. + @param length [in] Text length. -1 means NULL-terminated text.
  2702. +*/
  2703. +void
  2704. +SDLPango_SetText(
  2705. + SDLPango_Context *context,
  2706. + const char *text,
  2707. + int length)
  2708. +{
  2709. + SDLPango_SetText_GivenAlignment(context, text, length, SDLPANGO_ALIGN_LEFT);
  2710. +}
  2711. +
  2712. +/*!
  2713. + Set DPI to context.
  2714. +
  2715. + @param *context [i/o] Context
  2716. + @param dpi_x [in] X dpi
  2717. + @param dpi_y [in] Y dpi
  2718. +*/
  2719. +void
  2720. +SDLPango_SetDpi(
  2721. + SDLPango_Context *context,
  2722. + double dpi_x, double dpi_y)
  2723. +{
  2724. + pango_ft2_font_map_set_resolution (PANGO_FT2_FONT_MAP (context->font_map), dpi_x, dpi_y);
  2725. +}
  2726. +
  2727. +/*!
  2728. + Set language to context.
  2729. +
  2730. + @param *context [i/o] Context
  2731. + @param *language_tag [in] A RFC-3066 format language tag
  2732. +*/
  2733. +void SDLCALL SDLPango_SetLanguage(
  2734. + SDLPango_Context *context,
  2735. + const char *language_tag)
  2736. +{
  2737. + pango_context_set_language (context->context, pango_language_from_string (language_tag));
  2738. +}
  2739. +
  2740. +/*!
  2741. + Set base direction to context.
  2742. +
  2743. + @param *context [i/o] Context
  2744. + @param direction [in] Direction
  2745. +*/
  2746. +void SDLCALL SDLPango_SetBaseDirection(
  2747. + SDLPango_Context *context,
  2748. + SDLPango_Direction direction)
  2749. +{
  2750. + PangoDirection pango_dir;
  2751. +
  2752. + switch(direction) {
  2753. + case SDLPANGO_DIRECTION_LTR:
  2754. + pango_dir = PANGO_DIRECTION_LTR;
  2755. + break;
  2756. + case SDLPANGO_DIRECTION_RTL:
  2757. + pango_dir = PANGO_DIRECTION_RTL;
  2758. + break;
  2759. + case SDLPANGO_DIRECTION_WEAK_LTR:
  2760. + pango_dir = PANGO_DIRECTION_WEAK_LTR;
  2761. + break;
  2762. + case SDLPANGO_DIRECTION_WEAK_RTL:
  2763. + pango_dir = PANGO_DIRECTION_WEAK_RTL;
  2764. + break;
  2765. + case SDLPANGO_DIRECTION_NEUTRAL:
  2766. + pango_dir = PANGO_DIRECTION_NEUTRAL;
  2767. + break;
  2768. + default:
  2769. + SDL_SetError("unknown direction value");
  2770. + return;
  2771. + }
  2772. +
  2773. + pango_context_set_base_dir (context->context, pango_dir);
  2774. +}
  2775. +
  2776. +/*!
  2777. + Get font map from context.
  2778. +
  2779. + @param *context [in] Context
  2780. + @return Font map
  2781. +*/
  2782. +PangoFontMap* SDLCALL SDLPango_GetPangoFontMap(
  2783. + SDLPango_Context *context)
  2784. +{
  2785. + return context->font_map;
  2786. +}
  2787. +
  2788. +/*!
  2789. + Get font description from context.
  2790. +
  2791. + @param *context [in] Context
  2792. + @return Font description
  2793. +*/
  2794. +PangoFontDescription* SDLCALL SDLPango_GetPangoFontDescription(
  2795. + SDLPango_Context *context)
  2796. +{
  2797. + return context->font_desc;
  2798. +}
  2799. +
  2800. +/*!
  2801. + Get layout from context.
  2802. +
  2803. + @param *context [in] Context
  2804. + @return Layout
  2805. +*/
  2806. +PangoLayout* SDLCALL SDLPango_GetPangoLayout(
  2807. + SDLPango_Context *context)
  2808. +{
  2809. + return context->layout;
  2810. +}