SDL_ttf.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501
  1. /*
  2. SDL_ttf: A companion library to SDL for working with TrueType (tm) fonts
  3. Copyright (C) 2001-2018 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <ft2build.h>
  23. #include FT_FREETYPE_H
  24. #include FT_OUTLINE_H
  25. #include FT_STROKER_H
  26. #include FT_GLYPH_H
  27. #include FT_TRUETYPE_IDS_H
  28. #include "SDL.h"
  29. #include "SDL_endian.h"
  30. #include "SDL_ttf.h"
  31. #ifdef HAVE_RAQM
  32. #include "raqm.h"
  33. #endif
  34. /* FIXME: Right now we assume the gray-scale renderer Freetype is using
  35. supports 256 shades of gray, but we should instead key off of num_grays
  36. in the result FT_Bitmap after the FT_Render_Glyph() call. */
  37. #define NUM_GRAYS 256
  38. /* Handy routines for converting from fixed point */
  39. #define FT_FLOOR(X) ((X & -64) / 64)
  40. #define FT_CEIL(X) (((X + 63) & -64) / 64)
  41. #define CACHED_METRICS 0x10
  42. #define CACHED_BITMAP 0x01
  43. #define CACHED_PIXMAP 0x02
  44. /* Cached glyph information */
  45. typedef struct cached_glyph {
  46. int stored;
  47. FT_UInt index;
  48. FT_Bitmap bitmap;
  49. FT_Bitmap pixmap;
  50. int minx;
  51. int maxx;
  52. int miny;
  53. int maxy;
  54. int yoffset;
  55. int advance;
  56. Uint32 cached;
  57. } c_glyph;
  58. /* The structure used to hold internal font information */
  59. struct _TTF_Font {
  60. /* Freetype2 maintains all sorts of useful info itself */
  61. FT_Face face;
  62. /* We'll cache these ourselves */
  63. int height;
  64. int ascent;
  65. int descent;
  66. int lineskip;
  67. /* The font style */
  68. int face_style;
  69. int style;
  70. int outline;
  71. /* Whether kerning is desired */
  72. int kerning;
  73. /* Extra width in glyph bounds for text styles */
  74. int glyph_overhang;
  75. float glyph_italics;
  76. /* Information in the font for underlining */
  77. int underline_offset;
  78. int underline_height;
  79. /* Cache for style-transformed glyphs */
  80. c_glyph *current;
  81. c_glyph cache[257]; /* 257 is a prime */
  82. /* We are responsible for closing the font stream */
  83. SDL_RWops *src;
  84. int freesrc;
  85. FT_Open_Args args;
  86. /* For non-scalable formats, we must remember which font index size */
  87. int font_size_family;
  88. /* really just flags passed into FT_Load_Glyph */
  89. int hinting;
  90. };
  91. /* Handle a style only if the font does not already handle it */
  92. #define TTF_HANDLE_STYLE_BOLD(font) (((font)->style & TTF_STYLE_BOLD) && \
  93. !((font)->face_style & TTF_STYLE_BOLD))
  94. #define TTF_HANDLE_STYLE_ITALIC(font) (((font)->style & TTF_STYLE_ITALIC) && \
  95. !((font)->face_style & TTF_STYLE_ITALIC))
  96. #define TTF_HANDLE_STYLE_UNDERLINE(font) ((font)->style & TTF_STYLE_UNDERLINE)
  97. #define TTF_HANDLE_STYLE_STRIKETHROUGH(font) ((font)->style & TTF_STYLE_STRIKETHROUGH)
  98. /* Font styles that does not impact glyph drawing */
  99. #define TTF_STYLE_NO_GLYPH_CHANGE (TTF_STYLE_UNDERLINE | TTF_STYLE_STRIKETHROUGH)
  100. /* The FreeType font engine/library */
  101. static FT_Library library;
  102. static int TTF_initialized = 0;
  103. static int TTF_byteswapped = 0;
  104. #define TTF_CHECKPOINTER(p, errval) \
  105. if (!TTF_initialized) { \
  106. TTF_SetError("Library not initialized"); \
  107. return errval; \
  108. } \
  109. if (!p) { \
  110. TTF_SetError("Passed a NULL pointer"); \
  111. return errval; \
  112. }
  113. /* Gets the top row of the underline. The outline
  114. is taken into account.
  115. */
  116. static int TTF_underline_top_row(TTF_Font *font)
  117. {
  118. /* With outline, the underline_offset is underline_offset+outline. */
  119. /* So, we don't have to remove the top part of the outline height. */
  120. return font->ascent - font->underline_offset - 1;
  121. }
  122. /* Gets the top row of the underline. for a given glyph. The outline
  123. is taken into account.
  124. Need to update row according to height difference between font and glyph:
  125. font_value - font->ascent + glyph->maxy
  126. */
  127. static int TTF_Glyph_underline_top_row(TTF_Font *font, c_glyph *glyph)
  128. {
  129. return glyph->maxy - font->underline_offset - 1;
  130. }
  131. /* Gets the bottom row of the underline. The outline
  132. is taken into account.
  133. */
  134. static int TTF_underline_bottom_row(TTF_Font *font)
  135. {
  136. int row = TTF_underline_top_row(font) + font->underline_height;
  137. if (font->outline > 0) {
  138. /* Add underline_offset outline offset and */
  139. /* the bottom part of the outline. */
  140. row += font->outline * 2;
  141. }
  142. return row;
  143. }
  144. /* Gets the bottom row of the underline. for a given glyph. The outline
  145. is taken into account.
  146. Need to update row according to height difference between font and glyph:
  147. font_value - font->ascent + glyph->maxy
  148. */
  149. static int TTF_Glyph_underline_bottom_row(TTF_Font *font, c_glyph *glyph)
  150. {
  151. return TTF_underline_bottom_row(font) - font->ascent + glyph->maxy;
  152. }
  153. /* Gets the top row of the strikethrough. The outline
  154. is taken into account.
  155. */
  156. static int TTF_strikethrough_top_row(TTF_Font *font)
  157. {
  158. /* With outline, the first text row is 'outline'. */
  159. /* So, we don't have to remove the top part of the outline height. */
  160. return font->height / 2;
  161. }
  162. /* Gets the top row of the strikethrough for a given glyph. The outline
  163. is taken into account.
  164. Need to update row according to height difference between font and glyph:
  165. font_value - font->ascent + glyph->maxy
  166. */
  167. static int TTF_Glyph_strikethrough_top_row(TTF_Font *font, c_glyph *glyph)
  168. {
  169. return TTF_strikethrough_top_row(font) - font->ascent + glyph->maxy;
  170. }
  171. static void TTF_initLineMectrics(const TTF_Font *font, const SDL_Surface *textbuf, const int row, Uint8 **pdst, int *pheight)
  172. {
  173. Uint8 *dst;
  174. int height;
  175. dst = (Uint8 *)textbuf->pixels;
  176. if (row > 0) {
  177. dst += row * textbuf->pitch;
  178. }
  179. height = font->underline_height;
  180. /* Take outline into account */
  181. if (font->outline > 0) {
  182. height += font->outline * 2;
  183. }
  184. *pdst = dst;
  185. *pheight = height;
  186. }
  187. /* Draw a solid line of underline_height (+ optional outline)
  188. at the given row. The row value must take the
  189. outline into account.
  190. */
  191. static void TTF_drawLine_Solid(const TTF_Font *font, const SDL_Surface *textbuf, const int row)
  192. {
  193. int line;
  194. Uint8 *dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
  195. Uint8 *dst;
  196. int height;
  197. TTF_initLineMectrics(font, textbuf, row, &dst, &height);
  198. /* Draw line */
  199. for (line=height; line>0 && dst < dst_check; --line) {
  200. /* 1 because 0 is the bg color */
  201. SDL_memset(dst, 1, textbuf->w);
  202. dst += textbuf->pitch;
  203. }
  204. }
  205. /* Draw a shaded line of underline_height (+ optional outline)
  206. at the given row. The row value must take the
  207. outline into account.
  208. */
  209. static void TTF_drawLine_Shaded(const TTF_Font *font, const SDL_Surface *textbuf, const int row)
  210. {
  211. int line;
  212. Uint8 *dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
  213. Uint8 *dst;
  214. int height;
  215. TTF_initLineMectrics(font, textbuf, row, &dst, &height);
  216. /* Draw line */
  217. for (line=height; line>0 && dst < dst_check; --line) {
  218. SDL_memset(dst, NUM_GRAYS - 1, textbuf->w);
  219. dst += textbuf->pitch;
  220. }
  221. }
  222. /* Draw a blended line of underline_height (+ optional outline)
  223. at the given row. The row value must take the
  224. outline into account.
  225. */
  226. static void TTF_drawLine_Blended(const TTF_Font *font, const SDL_Surface *textbuf, const int row, const Uint32 color)
  227. {
  228. int line;
  229. Uint32 *dst_check = (Uint32*)textbuf->pixels + textbuf->pitch/4 * textbuf->h;
  230. Uint8 *dst8; /* destination, byte version */
  231. Uint32 *dst;
  232. int height;
  233. int col;
  234. Uint32 pixel = color | 0xFF000000; /* Amask */
  235. TTF_initLineMectrics(font, textbuf, row, &dst8, &height);
  236. dst = (Uint32 *) dst8;
  237. /* Draw line */
  238. for (line=height; line>0 && dst < dst_check; --line) {
  239. for (col=0; col < textbuf->w; ++col) {
  240. dst[col] = pixel;
  241. }
  242. dst += textbuf->pitch/4;
  243. }
  244. }
  245. /* rcg06192001 get linked library's version. */
  246. const SDL_version *TTF_Linked_Version(void)
  247. {
  248. static SDL_version linked_version;
  249. SDL_TTF_VERSION(&linked_version);
  250. return(&linked_version);
  251. }
  252. /* This function tells the library whether UNICODE text is generally
  253. byteswapped. A UNICODE BOM character at the beginning of a string
  254. will override this setting for that string.
  255. */
  256. void TTF_ByteSwappedUNICODE(int swapped)
  257. {
  258. TTF_byteswapped = swapped;
  259. }
  260. static void TTF_SetFTError(const char *msg, FT_Error error)
  261. {
  262. #ifdef USE_FREETYPE_ERRORS
  263. #undef FTERRORS_H
  264. #define FT_ERRORDEF(e, v, s) { e, s },
  265. static const struct
  266. {
  267. int err_code;
  268. const char* err_msg;
  269. } ft_errors[] = {
  270. #include <freetype/fterrors.h>
  271. };
  272. int i;
  273. const char *err_msg;
  274. char buffer[1024];
  275. err_msg = NULL;
  276. for (i=0; i<((sizeof ft_errors)/(sizeof ft_errors[0])); ++i) {
  277. if (error == ft_errors[i].err_code) {
  278. err_msg = ft_errors[i].err_msg;
  279. break;
  280. }
  281. }
  282. if (!err_msg) {
  283. err_msg = "unknown FreeType error";
  284. }
  285. TTF_SetError("%s: %s", msg, err_msg);
  286. #else
  287. TTF_SetError("%s", msg);
  288. #endif /* USE_FREETYPE_ERRORS */
  289. }
  290. static FT_Error Find_Glyph( TTF_Font* font, Uint32 ch, int want );
  291. static Uint32 UTF8_getch(const char **src, size_t *srclen);
  292. #ifndef HAVE_RAQM
  293. typedef struct {
  294. int index;
  295. int x_offset;
  296. int x_advance;
  297. int y_offset;
  298. } raqm_glyph_t;
  299. typedef struct {
  300. raqm_glyph_t* g_info;
  301. } raqm_t;
  302. void raqm_destroy(raqm_t *rq)
  303. {
  304. free(rq->g_info);
  305. free(rq);
  306. }
  307. #endif
  308. static FT_Error Find_GlyphByIndex( TTF_Font* font, Uint16 idx, int want );
  309. int text_layout(const char *text, size_t textlen, TTF_Font *font,
  310. raqm_t **rq, raqm_glyph_t **g_info, size_t *glyph_count)
  311. {
  312. #ifdef HAVE_RAQM
  313. *rq = raqm_create();
  314. if ( *rq == NULL )
  315. {
  316. return -1;
  317. }
  318. if ( !raqm_set_text_utf8(*rq, text, textlen) )
  319. {
  320. raqm_destroy(*rq);
  321. return -1;
  322. }
  323. if ( !raqm_set_freetype_face(*rq, font->face) )
  324. {
  325. raqm_destroy(*rq);
  326. return -1;
  327. }
  328. if ( !raqm_set_par_direction(*rq, RAQM_DIRECTION_DEFAULT) )
  329. {
  330. raqm_destroy(*rq);
  331. return -1;
  332. }
  333. if ( !raqm_layout(*rq) )
  334. {
  335. raqm_destroy(*rq);
  336. return -1;
  337. }
  338. *g_info = raqm_get_glyphs(*rq, glyph_count);
  339. if ( *g_info == NULL )
  340. {
  341. raqm_destroy(*rq);
  342. return -1;
  343. }
  344. return 0;
  345. #else
  346. int xstart;
  347. c_glyph *glyph;
  348. FT_Error error;
  349. FT_Long use_kerning;
  350. FT_UInt prev_index = 0;
  351. size_t count = 0;
  352. /* check kerning */
  353. use_kerning = FT_HAS_KERNING( font->face ) && font->kerning;
  354. *rq = (raqm_t*) malloc(sizeof(raqm_t));
  355. *g_info = (raqm_glyph_t*) malloc(sizeof(raqm_glyph_t) * (textlen));
  356. (*rq)->g_info = *g_info;
  357. xstart = 0;
  358. for ( count = 0; textlen > 0; count++ ) {
  359. Uint16 c = UTF8_getch(&text, &textlen);
  360. if ( c == UNICODE_BOM_NATIVE || c == UNICODE_BOM_SWAPPED ) {
  361. continue;
  362. }
  363. FT_UInt index = FT_Get_Char_Index( font->face, c );
  364. error = Find_GlyphByIndex(font, index, CACHED_METRICS|CACHED_BITMAP);
  365. if ( error ) {
  366. TTF_SetFTError("Couldn't find glyph", error);
  367. raqm_destroy(*rq);
  368. *glyph_count = 0;
  369. return -1;
  370. }
  371. glyph = font->current;
  372. (*g_info)[count].index = glyph->index;
  373. (*g_info)[count].y_offset = 0;
  374. (*g_info)[count].x_offset = 0;
  375. (*g_info)[count].x_advance = glyph->advance;
  376. /* do kerning, if possible AC-Patch */
  377. if ( use_kerning && prev_index && glyph->index ) {
  378. FT_Vector delta;
  379. FT_Get_Kerning( font->face, prev_index, (*g_info)->index, ft_kerning_default, &delta );
  380. xstart += delta.x >> 6;
  381. (*g_info)[count - 1].x_advance += delta.x;
  382. }
  383. }
  384. *glyph_count = count;
  385. return 0;
  386. #endif
  387. }
  388. int TTF_Init(void)
  389. {
  390. int status = 0;
  391. if (!TTF_initialized) {
  392. FT_Error error = FT_Init_FreeType(&library);
  393. if (error) {
  394. TTF_SetFTError("Couldn't init FreeType engine", error);
  395. status = -1;
  396. }
  397. }
  398. if (status == 0) {
  399. ++TTF_initialized;
  400. }
  401. return status;
  402. }
  403. static unsigned long RWread(
  404. FT_Stream stream,
  405. unsigned long offset,
  406. unsigned char* buffer,
  407. unsigned long count
  408. )
  409. {
  410. SDL_RWops *src;
  411. src = (SDL_RWops *)stream->descriptor.pointer;
  412. SDL_RWseek(src, (int)offset, RW_SEEK_SET);
  413. if (count == 0) {
  414. return 0;
  415. }
  416. return (unsigned long)SDL_RWread(src, buffer, 1, (int)count);
  417. }
  418. TTF_Font* TTF_OpenFontIndexRW(SDL_RWops *src, int freesrc, int ptsize, long index)
  419. {
  420. TTF_Font* font;
  421. FT_Error error;
  422. FT_Face face;
  423. FT_Fixed scale;
  424. FT_Stream stream;
  425. FT_CharMap found;
  426. Sint64 position;
  427. int i;
  428. if (!TTF_initialized) {
  429. TTF_SetError("Library not initialized");
  430. if (src && freesrc) {
  431. SDL_RWclose(src);
  432. }
  433. return NULL;
  434. }
  435. if (!src) {
  436. TTF_SetError("Passed a NULL font source");
  437. return NULL;
  438. }
  439. /* Check to make sure we can seek in this stream */
  440. position = SDL_RWtell(src);
  441. if (position < 0) {
  442. TTF_SetError("Can't seek in stream");
  443. if (freesrc) {
  444. SDL_RWclose(src);
  445. }
  446. return NULL;
  447. }
  448. font = (TTF_Font*)SDL_malloc(sizeof *font);
  449. if (font == NULL) {
  450. TTF_SetError("Out of memory");
  451. if (freesrc) {
  452. SDL_RWclose(src);
  453. }
  454. return NULL;
  455. }
  456. SDL_memset(font, 0, sizeof(*font));
  457. font->src = src;
  458. font->freesrc = freesrc;
  459. stream = (FT_Stream)SDL_malloc(sizeof(*stream));
  460. if (stream == NULL) {
  461. TTF_SetError("Out of memory");
  462. TTF_CloseFont(font);
  463. return NULL;
  464. }
  465. SDL_memset(stream, 0, sizeof(*stream));
  466. stream->read = RWread;
  467. stream->descriptor.pointer = src;
  468. stream->pos = (unsigned long)position;
  469. stream->size = (unsigned long)(SDL_RWsize(src) - position);
  470. font->args.flags = FT_OPEN_STREAM;
  471. font->args.stream = stream;
  472. error = FT_Open_Face(library, &font->args, index, &font->face);
  473. if (error) {
  474. TTF_SetFTError("Couldn't load font file", error);
  475. TTF_CloseFont(font);
  476. return NULL;
  477. }
  478. face = font->face;
  479. /* Set charmap for loaded font */
  480. found = 0;
  481. #if 0 /* Font debug code */
  482. for (i = 0; i < face->num_charmaps; i++) {
  483. FT_CharMap charmap = face->charmaps[i];
  484. printf("Found charmap: platform id %d, encoding id %d\n", charmap->platform_id, charmap->encoding_id);
  485. }
  486. #endif
  487. if (!found) {
  488. for (i = 0; i < face->num_charmaps; i++) {
  489. FT_CharMap charmap = face->charmaps[i];
  490. if (charmap->platform_id == 3 && charmap->encoding_id == 10) { /* UCS-4 Unicode */
  491. found = charmap;
  492. break;
  493. }
  494. }
  495. }
  496. if (!found) {
  497. for (i = 0; i < face->num_charmaps; i++) {
  498. FT_CharMap charmap = face->charmaps[i];
  499. if ((charmap->platform_id == 3 && charmap->encoding_id == 1) /* Windows Unicode */
  500. || (charmap->platform_id == 3 && charmap->encoding_id == 0) /* Windows Symbol */
  501. || (charmap->platform_id == 2 && charmap->encoding_id == 1) /* ISO Unicode */
  502. || (charmap->platform_id == 0)) { /* Apple Unicode */
  503. found = charmap;
  504. break;
  505. }
  506. }
  507. }
  508. if (found) {
  509. /* If this fails, continue using the default charmap */
  510. FT_Set_Charmap(face, found);
  511. }
  512. /* Make sure that our font face is scalable (global metrics) */
  513. if (FT_IS_SCALABLE(face)) {
  514. /* Set the character size and use default DPI (72) */
  515. error = FT_Set_Char_Size(font->face, 0, ptsize * 64, 0, 0);
  516. if (error) {
  517. TTF_SetFTError("Couldn't set font size", error);
  518. TTF_CloseFont(font);
  519. return NULL;
  520. }
  521. /* Get the scalable font metrics for this font */
  522. scale = face->size->metrics.y_scale;
  523. font->ascent = FT_CEIL(FT_MulFix(face->ascender, scale));
  524. font->descent = FT_CEIL(FT_MulFix(face->descender, scale));
  525. font->height = font->ascent - font->descent + /* baseline */ 1;
  526. font->lineskip = FT_CEIL(FT_MulFix(face->height, scale));
  527. font->underline_offset = FT_FLOOR(FT_MulFix(face->underline_position, scale));
  528. font->underline_height = FT_FLOOR(FT_MulFix(face->underline_thickness, scale));
  529. } else {
  530. /* Non-scalable font case. ptsize determines which family
  531. * or series of fonts to grab from the non-scalable format.
  532. * It is not the point size of the font.
  533. * */
  534. if (ptsize >= font->face->num_fixed_sizes)
  535. ptsize = font->face->num_fixed_sizes - 1;
  536. font->font_size_family = ptsize;
  537. error = FT_Set_Pixel_Sizes(face,
  538. face->available_sizes[ptsize].width,
  539. face->available_sizes[ptsize].height);
  540. /* With non-scalale fonts, Freetype2 likes to fill many of the
  541. * font metrics with the value of 0. The size of the
  542. * non-scalable fonts must be determined differently
  543. * or sometimes cannot be determined.
  544. * */
  545. font->ascent = face->available_sizes[ptsize].height;
  546. font->descent = 0;
  547. font->height = face->available_sizes[ptsize].height;
  548. font->lineskip = FT_CEIL(font->ascent);
  549. font->underline_offset = FT_FLOOR(face->underline_position);
  550. font->underline_height = FT_FLOOR(face->underline_thickness);
  551. }
  552. if (font->underline_height < 1) {
  553. font->underline_height = 1;
  554. }
  555. #ifdef DEBUG_FONTS
  556. printf("Font metrics:\n");
  557. printf("\tascent = %d, descent = %d\n",
  558. font->ascent, font->descent);
  559. printf("\theight = %d, lineskip = %d\n",
  560. font->height, font->lineskip);
  561. printf("\tunderline_offset = %d, underline_height = %d\n",
  562. font->underline_offset, font->underline_height);
  563. printf("\tunderline_top_row = %d, strikethrough_top_row = %d\n",
  564. TTF_underline_top_row(font), TTF_strikethrough_top_row(font));
  565. #endif
  566. /* Initialize the font face style */
  567. font->face_style = TTF_STYLE_NORMAL;
  568. if (font->face->style_flags & FT_STYLE_FLAG_BOLD) {
  569. font->face_style |= TTF_STYLE_BOLD;
  570. }
  571. if (font->face->style_flags & FT_STYLE_FLAG_ITALIC) {
  572. font->face_style |= TTF_STYLE_ITALIC;
  573. }
  574. /* Set the default font style */
  575. font->style = font->face_style;
  576. font->outline = 0;
  577. font->kerning = 1;
  578. font->glyph_overhang = face->size->metrics.y_ppem / 10;
  579. /* x offset = cos(((90.0-12)/360)*2*M_PI), or 12 degree angle */
  580. font->glyph_italics = 0.207f;
  581. font->glyph_italics *= font->height;
  582. return font;
  583. }
  584. TTF_Font* TTF_OpenFontRW(SDL_RWops *src, int freesrc, int ptsize)
  585. {
  586. return TTF_OpenFontIndexRW(src, freesrc, ptsize, 0);
  587. }
  588. TTF_Font* TTF_OpenFontIndex(const char *file, int ptsize, long index)
  589. {
  590. SDL_RWops *rw = SDL_RWFromFile(file, "rb");
  591. if (rw == NULL) {
  592. return NULL;
  593. }
  594. return TTF_OpenFontIndexRW(rw, 1, ptsize, index);
  595. }
  596. TTF_Font* TTF_OpenFont(const char *file, int ptsize)
  597. {
  598. return TTF_OpenFontIndex(file, ptsize, 0);
  599. }
  600. static void Flush_Glyph(c_glyph* glyph)
  601. {
  602. glyph->stored = 0;
  603. glyph->index = 0;
  604. if (glyph->bitmap.buffer) {
  605. SDL_free(glyph->bitmap.buffer);
  606. glyph->bitmap.buffer = 0;
  607. }
  608. if (glyph->pixmap.buffer) {
  609. SDL_free(glyph->pixmap.buffer);
  610. glyph->pixmap.buffer = 0;
  611. }
  612. glyph->cached = 0;
  613. }
  614. static void Flush_Cache(TTF_Font* font)
  615. {
  616. int i;
  617. int size = sizeof(font->cache) / sizeof(font->cache[0]);
  618. for (i = 0; i < size; ++i) {
  619. if (font->cache[i].cached) {
  620. Flush_Glyph(&font->cache[i]);
  621. }
  622. }
  623. }
  624. static FT_Error Load_Glyph(TTF_Font* font, Uint32 ch, c_glyph* cached, int want)
  625. {
  626. FT_Face face;
  627. FT_Error error;
  628. FT_GlyphSlot glyph;
  629. FT_Glyph_Metrics* metrics;
  630. FT_Outline* outline;
  631. if (!font || !font->face) {
  632. return FT_Err_Invalid_Handle;
  633. }
  634. face = font->face;
  635. /* Load the glyph */
  636. if (!cached->index) {
  637. cached->index = FT_Get_Char_Index(face, ch);
  638. }
  639. error = FT_Load_Glyph(face, cached->index, FT_LOAD_DEFAULT | font->hinting);
  640. if (error) {
  641. return error;
  642. }
  643. /* Get our glyph shortcuts */
  644. glyph = face->glyph;
  645. metrics = &glyph->metrics;
  646. outline = &glyph->outline;
  647. /* Get the glyph metrics if desired */
  648. if ((want & CACHED_METRICS) && !(cached->stored & CACHED_METRICS)) {
  649. if (FT_IS_SCALABLE(face)) {
  650. /* Get the bounding box */
  651. cached->minx = FT_FLOOR(metrics->horiBearingX);
  652. cached->maxx = FT_CEIL(metrics->horiBearingX + metrics->width);
  653. cached->maxy = FT_FLOOR(metrics->horiBearingY);
  654. cached->miny = cached->maxy - FT_CEIL(metrics->height);
  655. cached->yoffset = font->ascent - cached->maxy;
  656. cached->advance = metrics->horiAdvance;
  657. } else {
  658. /* Get the bounding box for non-scalable format.
  659. * Again, freetype2 fills in many of the font metrics
  660. * with the value of 0, so some of the values we
  661. * need must be calculated differently with certain
  662. * assumptions about non-scalable formats.
  663. * */
  664. cached->minx = FT_FLOOR(metrics->horiBearingX);
  665. cached->maxx = FT_CEIL(metrics->horiBearingX + metrics->width);
  666. cached->maxy = FT_FLOOR(metrics->horiBearingY);
  667. cached->miny = cached->maxy - FT_CEIL(face->available_sizes[font->font_size_family].height);
  668. cached->yoffset = 0;
  669. cached->advance = metrics->horiAdvance;
  670. }
  671. /* Adjust for bold and italic text */
  672. if (TTF_HANDLE_STYLE_BOLD(font)) {
  673. cached->maxx += font->glyph_overhang;
  674. }
  675. if (TTF_HANDLE_STYLE_ITALIC(font)) {
  676. cached->maxx += (int)SDL_ceil(font->glyph_italics);
  677. }
  678. cached->stored |= CACHED_METRICS;
  679. }
  680. if (((want & CACHED_BITMAP) && !(cached->stored & CACHED_BITMAP)) ||
  681. ((want & CACHED_PIXMAP) && !(cached->stored & CACHED_PIXMAP))) {
  682. int mono = (want & CACHED_BITMAP);
  683. int i;
  684. FT_Bitmap* src;
  685. FT_Bitmap* dst;
  686. FT_Glyph bitmap_glyph = NULL;
  687. /* Handle the italic style */
  688. if (TTF_HANDLE_STYLE_ITALIC(font)) {
  689. FT_Matrix shear;
  690. shear.xx = 1 << 16;
  691. shear.xy = (int) (font->glyph_italics * (1 << 16)) / font->height;
  692. shear.yx = 0;
  693. shear.yy = 1 << 16;
  694. FT_Outline_Transform(outline, &shear);
  695. }
  696. /* Render as outline */
  697. if ((font->outline > 0) && glyph->format != FT_GLYPH_FORMAT_BITMAP) {
  698. FT_Stroker stroker;
  699. FT_Get_Glyph(glyph, &bitmap_glyph);
  700. error = FT_Stroker_New(library, &stroker);
  701. if (error) {
  702. return error;
  703. }
  704. FT_Stroker_Set(stroker, font->outline * 64, FT_STROKER_LINECAP_ROUND, FT_STROKER_LINEJOIN_ROUND, 0);
  705. FT_Glyph_Stroke(&bitmap_glyph, stroker, 1 /* delete the original glyph */);
  706. FT_Stroker_Done(stroker);
  707. /* Render the glyph */
  708. error = FT_Glyph_To_Bitmap(&bitmap_glyph, mono ? ft_render_mode_mono : ft_render_mode_normal, 0, 1);
  709. if (error) {
  710. FT_Done_Glyph(bitmap_glyph);
  711. return error;
  712. }
  713. src = &((FT_BitmapGlyph)bitmap_glyph)->bitmap;
  714. } else {
  715. /* Render the glyph */
  716. error = FT_Render_Glyph(glyph, mono ? ft_render_mode_mono : ft_render_mode_normal);
  717. if (error) {
  718. return error;
  719. }
  720. src = &glyph->bitmap;
  721. }
  722. /* Copy over information to cache */
  723. if (mono) {
  724. dst = &cached->bitmap;
  725. } else {
  726. dst = &cached->pixmap;
  727. }
  728. SDL_memcpy(dst, src, sizeof(*dst));
  729. /* FT_Render_Glyph() and .fon fonts always generate a
  730. * two-color (black and white) glyphslot surface, even
  731. * when rendered in ft_render_mode_normal. */
  732. /* FT_IS_SCALABLE() means that the font is in outline format,
  733. * but does not imply that outline is rendered as 8-bit
  734. * grayscale, because embedded bitmap/graymap is preferred
  735. * (see FT_LOAD_DEFAULT section of FreeType2 API Reference).
  736. * FT_Render_Glyph() canreturn two-color bitmap or 4/16/256-
  737. * color graymap according to the format of embedded bitmap/
  738. * graymap. */
  739. if (src->pixel_mode == FT_PIXEL_MODE_MONO) {
  740. dst->pitch *= 8;
  741. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY2) {
  742. dst->pitch *= 4;
  743. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY4) {
  744. dst->pitch *= 2;
  745. }
  746. /* Adjust for bold and italic text */
  747. if (TTF_HANDLE_STYLE_BOLD(font)) {
  748. int bump = font->glyph_overhang;
  749. dst->pitch += bump;
  750. dst->width += bump;
  751. }
  752. if (TTF_HANDLE_STYLE_ITALIC(font)) {
  753. int bump = (int)SDL_ceil(font->glyph_italics);
  754. dst->pitch += bump;
  755. dst->width += bump;
  756. }
  757. if (dst->rows != 0) {
  758. dst->buffer = (unsigned char *)SDL_malloc(dst->pitch * dst->rows);
  759. if (!dst->buffer) {
  760. return FT_Err_Out_Of_Memory;
  761. }
  762. SDL_memset(dst->buffer, 0, dst->pitch * dst->rows);
  763. for (i = 0; i < src->rows; i++) {
  764. int soffset = i * src->pitch;
  765. int doffset = i * dst->pitch;
  766. if (mono) {
  767. unsigned char *srcp = src->buffer + soffset;
  768. unsigned char *dstp = dst->buffer + doffset;
  769. int j;
  770. if (src->pixel_mode == FT_PIXEL_MODE_MONO) {
  771. for (j = 0; j < src->width; j += 8) {
  772. unsigned char c = *srcp++;
  773. *dstp++ = (c&0x80) >> 7;
  774. c <<= 1;
  775. *dstp++ = (c&0x80) >> 7;
  776. c <<= 1;
  777. *dstp++ = (c&0x80) >> 7;
  778. c <<= 1;
  779. *dstp++ = (c&0x80) >> 7;
  780. c <<= 1;
  781. *dstp++ = (c&0x80) >> 7;
  782. c <<= 1;
  783. *dstp++ = (c&0x80) >> 7;
  784. c <<= 1;
  785. *dstp++ = (c&0x80) >> 7;
  786. c <<= 1;
  787. *dstp++ = (c&0x80) >> 7;
  788. }
  789. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY2) {
  790. for (j = 0; j < src->width; j += 4) {
  791. unsigned char c = *srcp++;
  792. *dstp++ = (((c&0xA0) >> 6) >= 0x2) ? 1 : 0;
  793. c <<= 2;
  794. *dstp++ = (((c&0xA0) >> 6) >= 0x2) ? 1 : 0;
  795. c <<= 2;
  796. *dstp++ = (((c&0xA0) >> 6) >= 0x2) ? 1 : 0;
  797. c <<= 2;
  798. *dstp++ = (((c&0xA0) >> 6) >= 0x2) ? 1 : 0;
  799. }
  800. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY4) {
  801. for (j = 0; j < src->width; j += 2) {
  802. unsigned char c = *srcp++;
  803. *dstp++ = (((c&0xF0) >> 4) >= 0x8) ? 1 : 0;
  804. c <<= 4;
  805. *dstp++ = (((c&0xF0) >> 4) >= 0x8) ? 1 : 0;
  806. }
  807. } else {
  808. for (j = 0; j < src->width; j++) {
  809. unsigned char c = *srcp++;
  810. *dstp++ = (c >= 0x80) ? 1 : 0;
  811. }
  812. }
  813. } else if (src->pixel_mode == FT_PIXEL_MODE_MONO) {
  814. /* This special case wouldn't
  815. * be here if the FT_Render_Glyph()
  816. * function wasn't buggy when it tried
  817. * to render a .fon font with 256
  818. * shades of gray. Instead, it
  819. * returns a black and white surface
  820. * and we have to translate it back
  821. * to a 256 gray shaded surface.
  822. * */
  823. unsigned char *srcp = src->buffer + soffset;
  824. unsigned char *dstp = dst->buffer + doffset;
  825. unsigned char c;
  826. int j, k;
  827. for (j = 0; j < src->width; j += 8) {
  828. c = *srcp++;
  829. for (k = 0; k < 8; ++k) {
  830. if ((c&0x80) >> 7) {
  831. *dstp++ = NUM_GRAYS - 1;
  832. } else {
  833. *dstp++ = 0x00;
  834. }
  835. c <<= 1;
  836. }
  837. }
  838. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY2) {
  839. unsigned char *srcp = src->buffer + soffset;
  840. unsigned char *dstp = dst->buffer + doffset;
  841. unsigned char c;
  842. int j, k;
  843. for (j = 0; j < src->width; j += 4) {
  844. c = *srcp++;
  845. for (k = 0; k < 4; ++k) {
  846. if ((c&0xA0) >> 6) {
  847. *dstp++ = NUM_GRAYS * ((c&0xA0) >> 6) / 3 - 1;
  848. } else {
  849. *dstp++ = 0x00;
  850. }
  851. c <<= 2;
  852. }
  853. }
  854. } else if (src->pixel_mode == FT_PIXEL_MODE_GRAY4) {
  855. unsigned char *srcp = src->buffer + soffset;
  856. unsigned char *dstp = dst->buffer + doffset;
  857. unsigned char c;
  858. int j, k;
  859. for (j = 0; j < src->width; j += 2) {
  860. c = *srcp++;
  861. for (k = 0; k < 2; ++k) {
  862. if ((c&0xF0) >> 4) {
  863. *dstp++ = NUM_GRAYS * ((c&0xF0) >> 4) / 15 - 1;
  864. } else {
  865. *dstp++ = 0x00;
  866. }
  867. c <<= 4;
  868. }
  869. }
  870. } else {
  871. SDL_memcpy(dst->buffer+doffset,
  872. src->buffer+soffset, src->pitch);
  873. }
  874. }
  875. }
  876. /* Handle the bold style */
  877. if (TTF_HANDLE_STYLE_BOLD(font)) {
  878. int row;
  879. int col;
  880. int offset;
  881. int pixel;
  882. Uint8* pixmap;
  883. /* The pixmap is a little hard, we have to add and clamp */
  884. for (row = dst->rows - 1; row >= 0; --row) {
  885. pixmap = (Uint8*) dst->buffer + row * dst->pitch;
  886. for (offset=1; offset <= font->glyph_overhang; ++offset) {
  887. for (col = dst->width - 1; col > 0; --col) {
  888. if (mono) {
  889. pixmap[col] |= pixmap[col-1];
  890. } else {
  891. pixel = (pixmap[col] + pixmap[col-1]);
  892. if (pixel > NUM_GRAYS - 1) {
  893. pixel = NUM_GRAYS - 1;
  894. }
  895. pixmap[col] = (Uint8) pixel;
  896. }
  897. }
  898. }
  899. }
  900. }
  901. /* Mark that we rendered this format */
  902. if (mono) {
  903. cached->stored |= CACHED_BITMAP;
  904. } else {
  905. cached->stored |= CACHED_PIXMAP;
  906. }
  907. /* Free outlined glyph */
  908. if (bitmap_glyph) {
  909. FT_Done_Glyph(bitmap_glyph);
  910. }
  911. }
  912. /* We're done, mark this glyph cached */
  913. cached->cached = ch;
  914. return 0;
  915. }
  916. static FT_Error Find_Glyph(TTF_Font* font, Uint32 ch, int want)
  917. {
  918. int retval = 0;
  919. int hsize = sizeof(font->cache) / sizeof(font->cache[0]);
  920. int h = ch % hsize;
  921. font->current = &font->cache[h];
  922. if (font->current->cached != ch)
  923. Flush_Glyph(font->current);
  924. if ((font->current->stored & want) != want) {
  925. retval = Load_Glyph(font, ch, font->current, want);
  926. }
  927. return retval;
  928. }
  929. static FT_Error Find_GlyphByIndex( TTF_Font* font, Uint16 idx, int want )
  930. {
  931. int retval = 0;
  932. int hsize = sizeof( font->cache ) / sizeof( font->cache[0] );
  933. int h = idx % hsize;
  934. font->current = &font->cache[h];
  935. if (font->current->cached != idx)
  936. Flush_Glyph( font->current );
  937. if ( (font->current->stored & want) != want ) {
  938. font->current->index = idx;
  939. retval = Load_Glyph( font, idx, font->current, want );
  940. }
  941. return retval;
  942. }
  943. void TTF_CloseFont(TTF_Font* font)
  944. {
  945. if (font) {
  946. Flush_Cache(font);
  947. if (font->face) {
  948. FT_Done_Face(font->face);
  949. }
  950. if (font->args.stream) {
  951. SDL_free(font->args.stream);
  952. }
  953. if (font->freesrc) {
  954. SDL_RWclose(font->src);
  955. }
  956. SDL_free(font);
  957. }
  958. }
  959. /* Gets the number of bytes needed to convert a Latin-1 string to UTF-8 */
  960. static size_t LATIN1_to_UTF8_len(const char *text)
  961. {
  962. size_t bytes = 1;
  963. while (*text) {
  964. Uint8 ch = *(Uint8*)text++;
  965. if (ch <= 0x7F) {
  966. bytes += 1;
  967. } else {
  968. bytes += 2;
  969. }
  970. }
  971. return bytes;
  972. }
  973. /* Gets the number of bytes needed to convert a UCS2 string to UTF-8 */
  974. static size_t UCS2_to_UTF8_len(const Uint16 *text)
  975. {
  976. size_t bytes = 1;
  977. while (*text) {
  978. Uint16 ch = *text++;
  979. if (ch <= 0x7F) {
  980. bytes += 1;
  981. } else if (ch <= 0x7FF) {
  982. bytes += 2;
  983. } else {
  984. bytes += 3;
  985. }
  986. }
  987. return bytes;
  988. }
  989. /* Convert a Latin-1 string to a UTF-8 string */
  990. static void LATIN1_to_UTF8(const char *src, Uint8 *dst)
  991. {
  992. while (*src) {
  993. Uint8 ch = *(Uint8*)src++;
  994. if (ch <= 0x7F) {
  995. *dst++ = ch;
  996. } else {
  997. *dst++ = 0xC0 | ((ch >> 6) & 0x1F);
  998. *dst++ = 0x80 | (ch & 0x3F);
  999. }
  1000. }
  1001. *dst = '\0';
  1002. }
  1003. /* Convert a UCS-2 string to a UTF-8 string */
  1004. static void UCS2_to_UTF8(const Uint16 *src, Uint8 *dst)
  1005. {
  1006. int swapped = TTF_byteswapped;
  1007. while (*src) {
  1008. Uint16 ch = *src++;
  1009. if (ch == UNICODE_BOM_NATIVE) {
  1010. swapped = 0;
  1011. continue;
  1012. }
  1013. if (ch == UNICODE_BOM_SWAPPED) {
  1014. swapped = 1;
  1015. continue;
  1016. }
  1017. if (swapped) {
  1018. ch = SDL_Swap16(ch);
  1019. }
  1020. if (ch <= 0x7F) {
  1021. *dst++ = (Uint8) ch;
  1022. } else if (ch <= 0x7FF) {
  1023. *dst++ = 0xC0 | (Uint8) ((ch >> 6) & 0x1F);
  1024. *dst++ = 0x80 | (Uint8) (ch & 0x3F);
  1025. } else {
  1026. *dst++ = 0xE0 | (Uint8) ((ch >> 12) & 0x0F);
  1027. *dst++ = 0x80 | (Uint8) ((ch >> 6) & 0x3F);
  1028. *dst++ = 0x80 | (Uint8) (ch & 0x3F);
  1029. }
  1030. }
  1031. *dst = '\0';
  1032. }
  1033. /* Gets a unicode value from a UTF-8 encoded string and advance the string */
  1034. #define UNKNOWN_UNICODE 0xFFFD
  1035. static Uint32 UTF8_getch(const char **src, size_t *srclen)
  1036. {
  1037. const Uint8 *p = *(const Uint8**)src;
  1038. size_t left = 0;
  1039. SDL_bool overlong = SDL_FALSE;
  1040. SDL_bool underflow = SDL_FALSE;
  1041. Uint32 ch = UNKNOWN_UNICODE;
  1042. if (*srclen == 0) {
  1043. return UNKNOWN_UNICODE;
  1044. }
  1045. if (p[0] >= 0xFC) {
  1046. if ((p[0] & 0xFE) == 0xFC) {
  1047. if (p[0] == 0xFC && (p[1] & 0xFC) == 0x80) {
  1048. overlong = SDL_TRUE;
  1049. }
  1050. ch = (Uint32) (p[0] & 0x01);
  1051. left = 5;
  1052. }
  1053. } else if (p[0] >= 0xF8) {
  1054. if ((p[0] & 0xFC) == 0xF8) {
  1055. if (p[0] == 0xF8 && (p[1] & 0xF8) == 0x80) {
  1056. overlong = SDL_TRUE;
  1057. }
  1058. ch = (Uint32) (p[0] & 0x03);
  1059. left = 4;
  1060. }
  1061. } else if (p[0] >= 0xF0) {
  1062. if ((p[0] & 0xF8) == 0xF0) {
  1063. if (p[0] == 0xF0 && (p[1] & 0xF0) == 0x80) {
  1064. overlong = SDL_TRUE;
  1065. }
  1066. ch = (Uint32) (p[0] & 0x07);
  1067. left = 3;
  1068. }
  1069. } else if (p[0] >= 0xE0) {
  1070. if ((p[0] & 0xF0) == 0xE0) {
  1071. if (p[0] == 0xE0 && (p[1] & 0xE0) == 0x80) {
  1072. overlong = SDL_TRUE;
  1073. }
  1074. ch = (Uint32) (p[0] & 0x0F);
  1075. left = 2;
  1076. }
  1077. } else if (p[0] >= 0xC0) {
  1078. if ((p[0] & 0xE0) == 0xC0) {
  1079. if ((p[0] & 0xDE) == 0xC0) {
  1080. overlong = SDL_TRUE;
  1081. }
  1082. ch = (Uint32) (p[0] & 0x1F);
  1083. left = 1;
  1084. }
  1085. } else {
  1086. if ((p[0] & 0x80) == 0x00) {
  1087. ch = (Uint32) p[0];
  1088. }
  1089. }
  1090. ++*src;
  1091. --*srclen;
  1092. while (left > 0 && *srclen > 0) {
  1093. ++p;
  1094. if ((p[0] & 0xC0) != 0x80) {
  1095. ch = UNKNOWN_UNICODE;
  1096. break;
  1097. }
  1098. ch <<= 6;
  1099. ch |= (p[0] & 0x3F);
  1100. ++*src;
  1101. --*srclen;
  1102. --left;
  1103. }
  1104. if (left > 0) {
  1105. underflow = SDL_TRUE;
  1106. }
  1107. /* Technically overlong sequences are invalid and should not be interpreted.
  1108. However, it doesn't cause a security risk here and I don't see any harm in
  1109. displaying them. The application is responsible for any other side effects
  1110. of allowing overlong sequences (e.g. string compares failing, etc.)
  1111. See bug 1931 for sample input that triggers this.
  1112. */
  1113. /*if (overlong) return UNKNOWN_UNICODE;*/
  1114. if (underflow ||
  1115. (ch >= 0xD800 && ch <= 0xDFFF) ||
  1116. (ch == 0xFFFE || ch == 0xFFFF) || ch > 0x10FFFF) {
  1117. ch = UNKNOWN_UNICODE;
  1118. }
  1119. return ch;
  1120. }
  1121. int TTF_FontHeight(const TTF_Font *font)
  1122. {
  1123. return(font->height);
  1124. }
  1125. int TTF_FontAscent(const TTF_Font *font)
  1126. {
  1127. return(font->ascent);
  1128. }
  1129. int TTF_FontDescent(const TTF_Font *font)
  1130. {
  1131. return(font->descent);
  1132. }
  1133. int TTF_FontLineSkip(const TTF_Font *font)
  1134. {
  1135. return(font->lineskip);
  1136. }
  1137. int TTF_GetFontKerning(const TTF_Font *font)
  1138. {
  1139. return(font->kerning);
  1140. }
  1141. void TTF_SetFontKerning(TTF_Font *font, int allowed)
  1142. {
  1143. font->kerning = allowed;
  1144. }
  1145. long TTF_FontFaces(const TTF_Font *font)
  1146. {
  1147. return(font->face->num_faces);
  1148. }
  1149. int TTF_FontFaceIsFixedWidth(const TTF_Font *font)
  1150. {
  1151. return(FT_IS_FIXED_WIDTH(font->face));
  1152. }
  1153. char *TTF_FontFaceFamilyName(const TTF_Font *font)
  1154. {
  1155. return(font->face->family_name);
  1156. }
  1157. char *TTF_FontFaceStyleName(const TTF_Font *font)
  1158. {
  1159. return(font->face->style_name);
  1160. }
  1161. int TTF_GlyphIsProvided(const TTF_Font *font, Uint16 ch)
  1162. {
  1163. return(FT_Get_Char_Index(font->face, ch));
  1164. }
  1165. int TTF_GlyphMetrics(TTF_Font *font, Uint16 ch,
  1166. int* minx, int* maxx, int* miny, int* maxy, int* advance)
  1167. {
  1168. FT_Error error;
  1169. error = Find_Glyph(font, ch, CACHED_METRICS);
  1170. if (error) {
  1171. TTF_SetFTError("Couldn't find glyph", error);
  1172. return -1;
  1173. }
  1174. if (minx) {
  1175. *minx = font->current->minx;
  1176. }
  1177. if (maxx) {
  1178. *maxx = font->current->maxx;
  1179. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1180. *maxx += font->glyph_overhang;
  1181. }
  1182. }
  1183. if (miny) {
  1184. *miny = font->current->miny;
  1185. }
  1186. if (maxy) {
  1187. *maxy = font->current->maxy;
  1188. }
  1189. if (advance) {
  1190. *advance = FT_CEIL(font->current->advance);
  1191. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1192. *advance += font->glyph_overhang;
  1193. }
  1194. }
  1195. return 0;
  1196. }
  1197. int TTF_SizeText(TTF_Font *font, const char *text, int *w, int *h)
  1198. {
  1199. int status = -1;
  1200. Uint8 *utf8;
  1201. TTF_CHECKPOINTER(text, -1);
  1202. utf8 = SDL_stack_alloc(Uint8, LATIN1_to_UTF8_len(text));
  1203. if (utf8) {
  1204. LATIN1_to_UTF8(text, utf8);
  1205. status = TTF_SizeUTF8(font, (char *)utf8, w, h);
  1206. SDL_stack_free(utf8);
  1207. } else {
  1208. SDL_OutOfMemory();
  1209. }
  1210. return status;
  1211. }
  1212. static int CalculateSize(TTF_Font *font, raqm_glyph_t *g_info, size_t glyph_count, int *w, int*h)
  1213. {
  1214. int status;
  1215. int x, z;
  1216. int minx, maxx;
  1217. int miny, maxy;
  1218. c_glyph *glyph;
  1219. FT_Error error;
  1220. FT_UInt prev_index = 0;
  1221. int outline_delta = 0;
  1222. int i;
  1223. /* Initialize everything to 0 */
  1224. status = 0;
  1225. minx = maxx = 0;
  1226. miny = maxy = 0;
  1227. /* Init outline handling */
  1228. if (font->outline > 0) {
  1229. outline_delta = font->outline * 2;
  1230. }
  1231. /* Load each character and sum it's bounding box */
  1232. x= 0;
  1233. for (i = 0; i < glyph_count; i++) {
  1234. error = Find_GlyphByIndex(font, g_info[i].index, CACHED_METRICS);
  1235. if (error) {
  1236. TTF_SetFTError("Couldn't find glyph", error);
  1237. return -1;
  1238. }
  1239. glyph = font->current;
  1240. z = x + FT_FLOOR(g_info[i].x_offset) + glyph->minx;
  1241. if (minx > z) {
  1242. minx = z;
  1243. }
  1244. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1245. x += font->glyph_overhang;
  1246. }
  1247. if (glyph->advance > glyph->maxx) {
  1248. z = x + FT_FLOOR(g_info[i].x_advance);
  1249. } else {
  1250. z = x + FT_FLOOR(g_info[i].x_offset) + glyph->maxx;
  1251. }
  1252. if (maxx < z) {
  1253. maxx = z;
  1254. }
  1255. x += FT_FLOOR(g_info[i].x_advance);
  1256. if (glyph->miny < miny) {
  1257. miny = glyph->miny;
  1258. }
  1259. if (glyph->maxy > maxy) {
  1260. maxy = glyph->maxy;
  1261. }
  1262. prev_index = g_info[i].index;
  1263. }
  1264. /* Fill the bounds rectangle */
  1265. if (w) {
  1266. /* Add outline extra width */
  1267. *w = (maxx - minx) + outline_delta;
  1268. }
  1269. if (h) {
  1270. /* Some fonts descend below font height (FletcherGothicFLF) */
  1271. /* Add outline extra height */
  1272. *h = (font->ascent - miny) + outline_delta;
  1273. if (*h < font->height) {
  1274. *h = font->height;
  1275. }
  1276. /* Update height according to the needs of the underline style */
  1277. if (TTF_HANDLE_STYLE_UNDERLINE(font)) {
  1278. int bottom_row = TTF_underline_bottom_row(font);
  1279. if (*h < bottom_row) {
  1280. *h = bottom_row;
  1281. }
  1282. }
  1283. }
  1284. return status;
  1285. }
  1286. int TTF_SizeUTF8(TTF_Font *font, const char *text, int *w, int *h)
  1287. {
  1288. size_t textlen;
  1289. raqm_t *rq = NULL;
  1290. raqm_glyph_t *g_info = NULL;
  1291. size_t glyph_count = 0;
  1292. TTF_CHECKPOINTER(text, -1);
  1293. textlen = SDL_strlen(text);
  1294. /* Shape text */
  1295. if (text_layout(text, textlen, font, &rq, &g_info, &glyph_count) < 0)
  1296. {
  1297. return 0;
  1298. }
  1299. else
  1300. {
  1301. /* Calculate the size */
  1302. int size = CalculateSize(font, g_info, glyph_count, w, h);
  1303. raqm_destroy(rq);
  1304. return size;
  1305. }
  1306. }
  1307. int TTF_SizeUNICODE(TTF_Font *font, const Uint16 *text, int *w, int *h)
  1308. {
  1309. int status = -1;
  1310. Uint8 *utf8;
  1311. TTF_CHECKPOINTER(text, -1);
  1312. utf8 = SDL_stack_alloc(Uint8, UCS2_to_UTF8_len(text));
  1313. if (utf8) {
  1314. UCS2_to_UTF8(text, utf8);
  1315. status = TTF_SizeUTF8(font, (char *)utf8, w, h);
  1316. SDL_stack_free(utf8);
  1317. } else {
  1318. SDL_OutOfMemory();
  1319. }
  1320. return status;
  1321. }
  1322. SDL_Surface *TTF_RenderText_Solid(TTF_Font *font,
  1323. const char *text, SDL_Color fg)
  1324. {
  1325. SDL_Surface *surface = NULL;
  1326. Uint8 *utf8;
  1327. TTF_CHECKPOINTER(text, NULL);
  1328. utf8 = SDL_stack_alloc(Uint8, LATIN1_to_UTF8_len(text));
  1329. if (utf8) {
  1330. LATIN1_to_UTF8(text, utf8);
  1331. surface = TTF_RenderUTF8_Solid(font, (char *)utf8, fg);
  1332. SDL_stack_free(utf8);
  1333. } else {
  1334. SDL_OutOfMemory();
  1335. }
  1336. return surface;
  1337. }
  1338. SDL_Surface *TTF_RenderUTF8_Solid(TTF_Font *font,
  1339. const char *text, SDL_Color fg)
  1340. {
  1341. SDL_bool first;
  1342. int xstart;
  1343. int width;
  1344. int height;
  1345. SDL_Surface* textbuf;
  1346. SDL_Palette* palette;
  1347. Uint8* src;
  1348. Uint8* dst;
  1349. Uint8 *dst_check;
  1350. int row, col;
  1351. int i;
  1352. c_glyph *glyph;
  1353. raqm_t *rq = NULL;
  1354. raqm_glyph_t *g_info = NULL;
  1355. size_t glyph_count = 0;
  1356. FT_Bitmap *current;
  1357. FT_Error error;
  1358. FT_UInt prev_index = 0;
  1359. size_t textlen;
  1360. TTF_CHECKPOINTER(text, NULL);
  1361. textlen = SDL_strlen(text);
  1362. /* Shape text */
  1363. if (text_layout(text, textlen, font, &rq, &g_info, &glyph_count) < 0)
  1364. {
  1365. TTF_SetError( "Text layout failed" );
  1366. return NULL;
  1367. }
  1368. /* Get the dimensions of the text surface */
  1369. if ( ( CalculateSize(font, g_info, glyph_count, &width, &height) < 0 ) || !width ) {
  1370. TTF_SetError("Text has zero width");
  1371. raqm_destroy(rq);
  1372. return NULL;
  1373. }
  1374. /* Create the target surface */
  1375. textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  1376. if (textbuf == NULL) {
  1377. raqm_destroy(rq);
  1378. return NULL;
  1379. }
  1380. /* Adding bound checking to avoid all kinds of memory corruption errors
  1381. that may occur. */
  1382. dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
  1383. /* Fill the palette with the foreground color */
  1384. palette = textbuf->format->palette;
  1385. palette->colors[0].r = 255 - fg.r;
  1386. palette->colors[0].g = 255 - fg.g;
  1387. palette->colors[0].b = 255 - fg.b;
  1388. palette->colors[1].r = fg.r;
  1389. palette->colors[1].g = fg.g;
  1390. palette->colors[1].b = fg.b;
  1391. palette->colors[1].a = fg.a ? fg.a : SDL_ALPHA_OPAQUE;
  1392. SDL_SetColorKey(textbuf, SDL_TRUE, 0);
  1393. /* Load and render each glyph */
  1394. first = SDL_TRUE;
  1395. xstart = 0;
  1396. for (i = 0; i < glyph_count; i++) {
  1397. int y_offset;
  1398. error = Find_GlyphByIndex(font, g_info[i].index, CACHED_METRICS|CACHED_BITMAP);
  1399. if (error) {
  1400. TTF_SetFTError("Couldn't find glyph", error);
  1401. SDL_FreeSurface(textbuf);
  1402. raqm_destroy(rq);
  1403. return NULL;
  1404. }
  1405. glyph = font->current;
  1406. y_offset = glyph->yoffset - FT_FLOOR(g_info[i].y_offset);
  1407. current = &glyph->bitmap;
  1408. /* Ensure the width of the pixmap is correct. On some cases,
  1409. * freetype may report a larger pixmap than possible.*/
  1410. width = current->width;
  1411. if (font->outline <= 0 && width > glyph->maxx - glyph->minx) {
  1412. width = glyph->maxx - glyph->minx;
  1413. }
  1414. /* Compensate for wrap around bug with negative minx's */
  1415. if (first && (glyph->minx < 0)) {
  1416. xstart -= glyph->minx;
  1417. }
  1418. first = SDL_FALSE;
  1419. for (row = 0; row < current->rows; ++row) {
  1420. /* Make sure we don't go either over, or under the
  1421. * limit */
  1422. if ( row+y_offset < 0 ) {
  1423. continue;
  1424. }
  1425. if ( row+y_offset >= textbuf->h ) {
  1426. continue;
  1427. }
  1428. dst = (Uint8*) textbuf->pixels +
  1429. (row+y_offset) * textbuf->pitch +
  1430. xstart + FT_FLOOR(g_info[i].x_offset) + glyph->minx;
  1431. src = current->buffer + row * current->pitch;
  1432. for (col = width; col > 0 && dst < dst_check; --col) {
  1433. *dst++ |= *src++;
  1434. }
  1435. }
  1436. xstart += FT_FLOOR(g_info[i].x_advance);
  1437. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1438. xstart += font->glyph_overhang;
  1439. }
  1440. prev_index = g_info[i].index;
  1441. }
  1442. /* Handle the underline style */
  1443. if (TTF_HANDLE_STYLE_UNDERLINE(font)) {
  1444. row = TTF_underline_top_row(font);
  1445. TTF_drawLine_Solid(font, textbuf, row);
  1446. }
  1447. /* Handle the strikethrough style */
  1448. if (TTF_HANDLE_STYLE_STRIKETHROUGH(font)) {
  1449. row = TTF_strikethrough_top_row(font);
  1450. TTF_drawLine_Solid(font, textbuf, row);
  1451. }
  1452. raqm_destroy(rq);
  1453. return textbuf;
  1454. }
  1455. SDL_Surface *TTF_RenderUNICODE_Solid(TTF_Font *font,
  1456. const Uint16 *text, SDL_Color fg)
  1457. {
  1458. SDL_Surface *surface = NULL;
  1459. Uint8 *utf8;
  1460. TTF_CHECKPOINTER(text, NULL);
  1461. utf8 = SDL_stack_alloc(Uint8, UCS2_to_UTF8_len(text));
  1462. if (utf8) {
  1463. UCS2_to_UTF8(text, utf8);
  1464. surface = TTF_RenderUTF8_Solid(font, (char *)utf8, fg);
  1465. SDL_stack_free(utf8);
  1466. } else {
  1467. SDL_OutOfMemory();
  1468. }
  1469. return surface;
  1470. }
  1471. SDL_Surface *TTF_RenderGlyph_Solid(TTF_Font *font, Uint16 ch, SDL_Color fg)
  1472. {
  1473. Uint16 ucs2[2];
  1474. Uint8 utf8[4];
  1475. ucs2[0] = ch;
  1476. ucs2[1] = 0;
  1477. UCS2_to_UTF8(ucs2, utf8);
  1478. return TTF_RenderUTF8_Solid(font, (char *)utf8, fg);
  1479. }
  1480. SDL_Surface *TTF_RenderText_Shaded(TTF_Font *font,
  1481. const char *text, SDL_Color fg, SDL_Color bg)
  1482. {
  1483. SDL_Surface *surface = NULL;
  1484. Uint8 *utf8;
  1485. TTF_CHECKPOINTER(text, NULL);
  1486. utf8 = SDL_stack_alloc(Uint8, LATIN1_to_UTF8_len(text));
  1487. if (utf8) {
  1488. LATIN1_to_UTF8(text, utf8);
  1489. surface = TTF_RenderUTF8_Shaded(font, (char *)utf8, fg, bg);
  1490. SDL_stack_free(utf8);
  1491. } else {
  1492. SDL_OutOfMemory();
  1493. }
  1494. return surface;
  1495. }
  1496. /* Convert the UTF-8 text to UNICODE and render it
  1497. */
  1498. SDL_Surface *TTF_RenderUTF8_Shaded(TTF_Font *font,
  1499. const char *text, SDL_Color fg, SDL_Color bg)
  1500. {
  1501. SDL_bool first;
  1502. int xstart;
  1503. int width;
  1504. int height;
  1505. int i;
  1506. SDL_Surface* textbuf;
  1507. SDL_Palette* palette;
  1508. int index;
  1509. int rdiff;
  1510. int gdiff;
  1511. int bdiff;
  1512. int adiff;
  1513. Uint8* src;
  1514. Uint8* dst;
  1515. Uint8* dst_check;
  1516. int row, col;
  1517. FT_Bitmap* current;
  1518. c_glyph *glyph;
  1519. raqm_t *rq = NULL;
  1520. raqm_glyph_t *g_info = NULL;
  1521. size_t glyph_count = 0;
  1522. size_t textlen;
  1523. FT_Error error;
  1524. FT_UInt prev_index = 0;
  1525. TTF_CHECKPOINTER(text, NULL);
  1526. textlen = SDL_strlen(text);
  1527. /* Shape text */
  1528. if (text_layout(text, textlen, font, &rq, &g_info, &glyph_count) < 0)
  1529. {
  1530. TTF_SetError("Text layout failed");
  1531. return NULL;
  1532. }
  1533. /* Get the dimensions of the text surface */
  1534. if ( ( CalculateSize(font, g_info, glyph_count, &width, &height) < 0 ) || !width ) {
  1535. raqm_destroy(rq);
  1536. TTF_SetError("Text has zero width");
  1537. return NULL;
  1538. }
  1539. /* Create the target surface */
  1540. textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8, 0, 0, 0, 0);
  1541. if (textbuf == NULL) {
  1542. raqm_destroy(rq);
  1543. return NULL;
  1544. }
  1545. /* Adding bound checking to avoid all kinds of memory corruption errors
  1546. that may occur. */
  1547. dst_check = (Uint8*)textbuf->pixels + textbuf->pitch * textbuf->h;
  1548. /* Support alpha blending */
  1549. if (!fg.a) {
  1550. fg.a = SDL_ALPHA_OPAQUE;
  1551. }
  1552. if (!bg.a) {
  1553. bg.a = SDL_ALPHA_OPAQUE;
  1554. }
  1555. if (fg.a != SDL_ALPHA_OPAQUE || bg.a != SDL_ALPHA_OPAQUE) {
  1556. SDL_SetSurfaceBlendMode(textbuf, SDL_BLENDMODE_BLEND);
  1557. }
  1558. /* Fill the palette with NUM_GRAYS levels of shading from bg to fg */
  1559. palette = textbuf->format->palette;
  1560. rdiff = fg.r - bg.r;
  1561. gdiff = fg.g - bg.g;
  1562. bdiff = fg.b - bg.b;
  1563. adiff = fg.a - bg.a;
  1564. for (index = 0; index < NUM_GRAYS; ++index) {
  1565. palette->colors[index].r = bg.r + (index*rdiff) / (NUM_GRAYS-1);
  1566. palette->colors[index].g = bg.g + (index*gdiff) / (NUM_GRAYS-1);
  1567. palette->colors[index].b = bg.b + (index*bdiff) / (NUM_GRAYS-1);
  1568. palette->colors[index].a = bg.a + (index*adiff) / (NUM_GRAYS-1);
  1569. }
  1570. /* Load and render each glyph */
  1571. first = SDL_TRUE;
  1572. xstart = 0;
  1573. for (i = 0; i < glyph_count; i++) {
  1574. int y_offset;
  1575. error = Find_GlyphByIndex(font, g_info[i].index, CACHED_METRICS|CACHED_PIXMAP);
  1576. if (error) {
  1577. TTF_SetFTError("Couldn't find glyph", error);
  1578. SDL_FreeSurface(textbuf);
  1579. raqm_destroy(rq);
  1580. return NULL;
  1581. }
  1582. glyph = font->current;
  1583. y_offset = glyph->yoffset - FT_FLOOR(g_info[i].y_offset);
  1584. /* Ensure the width of the pixmap is correct. On some cases,
  1585. * freetype may report a larger pixmap than possible.*/
  1586. width = glyph->pixmap.width;
  1587. if (font->outline <= 0 && width > glyph->maxx - glyph->minx) {
  1588. width = glyph->maxx - glyph->minx;
  1589. }
  1590. /* Compensate for the wrap around with negative minx's */
  1591. if (first && (glyph->minx < 0)) {
  1592. xstart -= glyph->minx;
  1593. }
  1594. first = SDL_FALSE;
  1595. current = &glyph->pixmap;
  1596. for (row = 0; row < current->rows; ++row) {
  1597. /* Make sure we don't go either over, or under the
  1598. * limit */
  1599. if ( row+ y_offset < 0 ) {
  1600. continue;
  1601. }
  1602. if ( row+ y_offset >= textbuf->h ) {
  1603. continue;
  1604. }
  1605. dst = (Uint8*) textbuf->pixels +
  1606. (row+y_offset) * textbuf->pitch +
  1607. xstart + FT_FLOOR(g_info[i].x_offset) + glyph->minx;
  1608. src = current->buffer + row * current->pitch;
  1609. for (col = width; col > 0 && dst < dst_check; --col) {
  1610. *dst++ |= *src++;
  1611. }
  1612. }
  1613. xstart += FT_FLOOR(g_info[i].x_advance);
  1614. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1615. xstart += font->glyph_overhang;
  1616. }
  1617. prev_index = g_info[i].index;
  1618. }
  1619. /* Handle the underline style */
  1620. if (TTF_HANDLE_STYLE_UNDERLINE(font)) {
  1621. row = TTF_underline_top_row(font);
  1622. TTF_drawLine_Shaded(font, textbuf, row);
  1623. }
  1624. /* Handle the strikethrough style */
  1625. if (TTF_HANDLE_STYLE_STRIKETHROUGH(font)) {
  1626. row = TTF_strikethrough_top_row(font);
  1627. TTF_drawLine_Shaded(font, textbuf, row);
  1628. }
  1629. raqm_destroy(rq);
  1630. return textbuf;
  1631. }
  1632. SDL_Surface* TTF_RenderUNICODE_Shaded(TTF_Font* font,
  1633. const Uint16* text,
  1634. SDL_Color fg,
  1635. SDL_Color bg)
  1636. {
  1637. SDL_Surface *surface = NULL;
  1638. Uint8 *utf8;
  1639. TTF_CHECKPOINTER(text, NULL);
  1640. utf8 = SDL_stack_alloc(Uint8, UCS2_to_UTF8_len(text));
  1641. if (utf8) {
  1642. UCS2_to_UTF8(text, utf8);
  1643. surface = TTF_RenderUTF8_Shaded(font, (char *)utf8, fg, bg);
  1644. SDL_stack_free(utf8);
  1645. } else {
  1646. SDL_OutOfMemory();
  1647. }
  1648. return surface;
  1649. }
  1650. SDL_Surface* TTF_RenderGlyph_Shaded(TTF_Font* font,
  1651. Uint16 ch,
  1652. SDL_Color fg,
  1653. SDL_Color bg)
  1654. {
  1655. Uint16 ucs2[2];
  1656. Uint8 utf8[4];
  1657. ucs2[0] = ch;
  1658. ucs2[1] = 0;
  1659. UCS2_to_UTF8(ucs2, utf8);
  1660. return TTF_RenderUTF8_Shaded(font, (char *)utf8, fg, bg);
  1661. }
  1662. SDL_Surface *TTF_RenderText_Blended(TTF_Font *font,
  1663. const char *text, SDL_Color fg)
  1664. {
  1665. SDL_Surface *surface = NULL;
  1666. Uint8 *utf8;
  1667. TTF_CHECKPOINTER(text, NULL);
  1668. utf8 = SDL_stack_alloc(Uint8, LATIN1_to_UTF8_len(text));
  1669. if (utf8) {
  1670. LATIN1_to_UTF8(text, utf8);
  1671. surface = TTF_RenderUTF8_Blended(font, (char *)utf8, fg);
  1672. SDL_stack_free(utf8);
  1673. } else {
  1674. SDL_OutOfMemory();
  1675. }
  1676. return surface;
  1677. }
  1678. SDL_Surface *TTF_RenderUTF8_Blended(TTF_Font *font,
  1679. const char *text, SDL_Color fg)
  1680. {
  1681. SDL_bool first;
  1682. int i;
  1683. int xstart;
  1684. int width, height;
  1685. SDL_Surface *textbuf;
  1686. Uint8 alpha;
  1687. Uint8 alpha_table[256];
  1688. Uint32 pixel;
  1689. Uint8 *src;
  1690. Uint32 *dst;
  1691. Uint32 *dst_check;
  1692. int row, col;
  1693. c_glyph *glyph;
  1694. raqm_t *rq = NULL;
  1695. raqm_glyph_t *g_info = NULL;
  1696. size_t glyph_count = 0;
  1697. FT_Error error;
  1698. FT_UInt prev_index = 0;
  1699. size_t textlen;
  1700. TTF_CHECKPOINTER(text, NULL);
  1701. textlen = SDL_strlen(text);
  1702. /* Shape text */
  1703. if (text_layout(text, textlen, font, &rq, &g_info, &glyph_count) < 0)
  1704. {
  1705. TTF_SetError("Text layout failed");
  1706. return NULL;
  1707. }
  1708. /* Get the dimensions of the text surface */
  1709. if ( ( CalculateSize(font, g_info, glyph_count, &width, &height) < 0 ) || !width ) {
  1710. raqm_destroy(rq);
  1711. TTF_SetError("Text has zero width");
  1712. return(NULL);
  1713. }
  1714. /* Create the target surface */
  1715. textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,
  1716. 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
  1717. if (textbuf == NULL) {
  1718. raqm_destroy(rq);
  1719. TTF_SetError("SDL_Surface creation failed");
  1720. return NULL;
  1721. }
  1722. /* Adding bound checking to avoid all kinds of memory corruption errors
  1723. that may occur. */
  1724. dst_check = (Uint32*)textbuf->pixels + textbuf->pitch/4 * textbuf->h;
  1725. /* Support alpha blending */
  1726. if (!fg.a) {
  1727. fg.a = SDL_ALPHA_OPAQUE;
  1728. }
  1729. if (fg.a == SDL_ALPHA_OPAQUE) {
  1730. for (i = 0; i < SDL_arraysize(alpha_table); ++i) {
  1731. alpha_table[i] = (Uint8)i;
  1732. }
  1733. } else {
  1734. for (i = 0; i < SDL_arraysize(alpha_table); ++i) {
  1735. alpha_table[i] = (Uint8)(i * fg.a / 255);
  1736. }
  1737. SDL_SetSurfaceBlendMode(textbuf, SDL_BLENDMODE_BLEND);
  1738. }
  1739. /* Load and render each glyph */
  1740. first = SDL_TRUE;
  1741. xstart = 0;
  1742. pixel = (fg.r<<16)|(fg.g<<8)|fg.b;
  1743. SDL_FillRect(textbuf, NULL, pixel); /* Initialize with fg and 0 alpha */
  1744. for (i = 0; i < glyph_count; i++) {
  1745. int y_offset;
  1746. error = Find_GlyphByIndex(font, g_info[i].index, CACHED_METRICS|CACHED_PIXMAP);
  1747. if (error) {
  1748. TTF_SetFTError("Couldn't find glyph", error);
  1749. SDL_FreeSurface(textbuf);
  1750. raqm_destroy(rq);
  1751. return NULL;
  1752. }
  1753. glyph = font->current;
  1754. y_offset = glyph->yoffset - FT_FLOOR(g_info[i].y_offset);
  1755. /* Ensure the width of the pixmap is correct. On some cases,
  1756. * freetype may report a larger pixmap than possible.*/
  1757. width = glyph->pixmap.width;
  1758. if (font->outline <= 0 && width > glyph->maxx - glyph->minx) {
  1759. width = glyph->maxx - glyph->minx;
  1760. }
  1761. for (row = 0; row < glyph->pixmap.rows; ++row) {
  1762. /* Make sure we don't go either over, or under the
  1763. * limit */
  1764. if ( row+y_offset < 0 ) {
  1765. continue;
  1766. }
  1767. if ( row+y_offset >= textbuf->h ) {
  1768. continue;
  1769. }
  1770. dst = (Uint32*) textbuf->pixels +
  1771. (row+y_offset) * textbuf->pitch/4 +
  1772. xstart + FT_FLOOR(g_info[i].x_offset) + glyph->minx;
  1773. /* Added code to adjust src pointer for pixmaps to
  1774. * account for pitch.
  1775. * */
  1776. dst_check = (Uint32*) textbuf->pixels + (row + y_offset + 1) * textbuf->pitch/4;
  1777. src = (Uint8*) (glyph->pixmap.buffer + glyph->pixmap.pitch * row);
  1778. for (col = width; col>0 && dst < dst_check; --col) {
  1779. alpha = *src++;
  1780. *dst++ |= pixel | ((Uint32)alpha_table[alpha] << 24);
  1781. }
  1782. }
  1783. xstart += FT_FLOOR(g_info[i].x_advance);
  1784. if (TTF_HANDLE_STYLE_BOLD(font)) {
  1785. xstart += font->glyph_overhang;
  1786. }
  1787. prev_index = g_info[i].index;
  1788. }
  1789. /* Handle the underline style */
  1790. if (TTF_HANDLE_STYLE_UNDERLINE(font)) {
  1791. row = TTF_underline_top_row(font);
  1792. TTF_drawLine_Blended(font, textbuf, row, pixel | (Uint32)fg.a << 24);
  1793. }
  1794. /* Handle the strikethrough style */
  1795. if (TTF_HANDLE_STYLE_STRIKETHROUGH(font)) {
  1796. row = TTF_strikethrough_top_row(font);
  1797. TTF_drawLine_Blended(font, textbuf, row, pixel | (Uint32)fg.a << 24);
  1798. }
  1799. raqm_destroy(rq);
  1800. return(textbuf);
  1801. }
  1802. SDL_Surface *TTF_RenderUNICODE_Blended(TTF_Font *font,
  1803. const Uint16 *text, SDL_Color fg)
  1804. {
  1805. SDL_Surface *surface = NULL;
  1806. Uint8 *utf8;
  1807. TTF_CHECKPOINTER(text, NULL);
  1808. utf8 = SDL_stack_alloc(Uint8, UCS2_to_UTF8_len(text));
  1809. if (utf8) {
  1810. UCS2_to_UTF8(text, utf8);
  1811. surface = TTF_RenderUTF8_Blended(font, (char *)utf8, fg);
  1812. SDL_stack_free(utf8);
  1813. } else {
  1814. SDL_OutOfMemory();
  1815. }
  1816. return surface;
  1817. }
  1818. SDL_Surface *TTF_RenderText_Blended_Wrapped(TTF_Font *font, const char *text, SDL_Color fg, Uint32 wrapLength)
  1819. {
  1820. SDL_Surface *surface = NULL;
  1821. Uint8 *utf8;
  1822. TTF_CHECKPOINTER(text, NULL);
  1823. utf8 = SDL_stack_alloc(Uint8, LATIN1_to_UTF8_len(text));
  1824. if (utf8) {
  1825. LATIN1_to_UTF8(text, utf8);
  1826. surface = TTF_RenderUTF8_Blended_Wrapped(font, (char *)utf8, fg, wrapLength);
  1827. SDL_stack_free(utf8);
  1828. } else {
  1829. SDL_OutOfMemory();
  1830. }
  1831. return surface;
  1832. }
  1833. static SDL_bool CharacterIsDelimiter(char c, const char *delimiters)
  1834. {
  1835. while (*delimiters) {
  1836. if (c == *delimiters) {
  1837. return SDL_TRUE;
  1838. }
  1839. ++delimiters;
  1840. }
  1841. return SDL_FALSE;
  1842. }
  1843. /* Don't define this until we have a release where we can change font rendering
  1844. #define TTF_USE_LINESKIP
  1845. */
  1846. SDL_Surface *TTF_RenderUTF8_Blended_Wrapped(TTF_Font *font,
  1847. const char *text, SDL_Color fg, Uint32 wrapLength)
  1848. {
  1849. SDL_bool first;
  1850. int i;
  1851. int xstart;
  1852. int width, height;
  1853. SDL_Surface *textbuf;
  1854. Uint8 alpha;
  1855. Uint8 alpha_table[256];
  1856. Uint32 pixel;
  1857. Uint8 *src;
  1858. Uint32 *dst;
  1859. Uint32 *dst_check;
  1860. int row, col;
  1861. c_glyph *glyph;
  1862. raqm_t *rq = NULL;
  1863. raqm_glyph_t *g_info = NULL;
  1864. size_t glyph_count = 0;
  1865. FT_Error error;
  1866. FT_UInt prev_index = 0;
  1867. #ifndef TTF_USE_LINESKIP
  1868. const int lineSpace = 2;
  1869. #endif
  1870. int line, numLines, rowSize;
  1871. char *str, **strLines, **newLines;
  1872. size_t textlen;
  1873. TTF_CHECKPOINTER(text, NULL);
  1874. textlen = SDL_strlen(text);
  1875. /* Shape text */
  1876. if (text_layout(text,textlen, font, &rq, &g_info, &glyph_count) < 0)
  1877. {
  1878. TTF_SetError("Text layout failed");
  1879. return NULL;
  1880. }
  1881. /* Get the dimensions of the text surface */
  1882. if ( ( CalculateSize(font, g_info, glyph_count, &width, &height) < 0 ) || !width ) {
  1883. raqm_destroy(rq);
  1884. TTF_SetError("Text has zero width");
  1885. return(NULL);
  1886. }
  1887. numLines = 1;
  1888. str = NULL;
  1889. strLines = NULL;
  1890. if (wrapLength > 0 && *text) {
  1891. const char *wrapDelims = " \t\r\n";
  1892. int w, h;
  1893. char *spot, *tok, *next_tok, *end;
  1894. char delim;
  1895. size_t str_len = SDL_strlen(text);
  1896. numLines = 0;
  1897. str = SDL_stack_alloc(char, str_len+1);
  1898. if (str == NULL) {
  1899. raqm_destroy(rq);
  1900. TTF_SetError("Out of memory");
  1901. return(NULL);
  1902. }
  1903. SDL_strlcpy(str, text, str_len+1);
  1904. tok = str;
  1905. end = str + str_len;
  1906. do {
  1907. newLines = (char **)SDL_realloc(strLines, (numLines+1)*sizeof(*strLines));
  1908. if (!newLines) {
  1909. raqm_destroy(rq);
  1910. TTF_SetError("Out of memory");
  1911. SDL_free(strLines);
  1912. SDL_stack_free(str);
  1913. return(NULL);
  1914. }
  1915. strLines = newLines;
  1916. strLines[numLines++] = tok;
  1917. /* Look for the end of the line */
  1918. if ((spot = SDL_strchr(tok, '\r')) != NULL ||
  1919. (spot = SDL_strchr(tok, '\n')) != NULL) {
  1920. if (*spot == '\r') {
  1921. ++spot;
  1922. }
  1923. if (*spot == '\n') {
  1924. ++spot;
  1925. }
  1926. } else {
  1927. spot = end;
  1928. }
  1929. next_tok = spot;
  1930. /* Get the longest string that will fit in the desired space */
  1931. for (; ;) {
  1932. /* Strip trailing whitespace */
  1933. while (spot > tok &&
  1934. CharacterIsDelimiter(spot[-1], wrapDelims)) {
  1935. --spot;
  1936. }
  1937. if (spot == tok) {
  1938. if (CharacterIsDelimiter(*spot, wrapDelims)) {
  1939. *spot = '\0';
  1940. }
  1941. break;
  1942. }
  1943. delim = *spot;
  1944. *spot = '\0';
  1945. TTF_SizeUTF8(font, tok, &w, &h);
  1946. if ((Uint32)w <= wrapLength) {
  1947. break;
  1948. } else {
  1949. /* Back up and try again... */
  1950. *spot = delim;
  1951. }
  1952. while (spot > tok &&
  1953. !CharacterIsDelimiter(spot[-1], wrapDelims)) {
  1954. --spot;
  1955. }
  1956. if (spot > tok) {
  1957. next_tok = spot;
  1958. }
  1959. }
  1960. tok = next_tok;
  1961. } while (tok < end);
  1962. }
  1963. /* Create the target surface */
  1964. textbuf = SDL_CreateRGBSurface(SDL_SWSURFACE,
  1965. (numLines > 1) ? wrapLength : width,
  1966. #ifdef TTF_USE_LINESKIP
  1967. numLines * TTF_FontLineSkip(font),
  1968. #else
  1969. height * numLines + (lineSpace * (numLines - 1)),
  1970. #endif
  1971. 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
  1972. if (textbuf == NULL) {
  1973. if (strLines) {
  1974. SDL_free(strLines);
  1975. SDL_stack_free(str);
  1976. }
  1977. raqm_destroy(rq);
  1978. return(NULL);
  1979. }
  1980. #ifdef TTF_USE_LINESKIP
  1981. rowSize = textbuf->pitch/4 * TTF_FontLineSkip(font);
  1982. #else
  1983. rowSize = textbuf->pitch/4 * height;
  1984. #endif
  1985. /* Adding bound checking to avoid all kinds of memory corruption errors
  1986. that may occur. */
  1987. dst_check = (Uint32*)textbuf->pixels + textbuf->pitch/4 * textbuf->h;
  1988. /* Support alpha blending */
  1989. if (!fg.a) {
  1990. fg.a = SDL_ALPHA_OPAQUE;
  1991. }
  1992. if (fg.a == SDL_ALPHA_OPAQUE) {
  1993. for (i = 0; i < SDL_arraysize(alpha_table); ++i) {
  1994. alpha_table[i] = (Uint8)i;
  1995. }
  1996. } else {
  1997. for (i = 0; i < SDL_arraysize(alpha_table); ++i) {
  1998. alpha_table[i] = (Uint8)(i * fg.a / 255);
  1999. }
  2000. SDL_SetSurfaceBlendMode(textbuf, SDL_BLENDMODE_BLEND);
  2001. }
  2002. /* Load and render each glyph */
  2003. pixel = (fg.r<<16)|(fg.g<<8)|fg.b;
  2004. SDL_FillRect(textbuf, NULL, pixel); /* Initialize with fg and 0 alpha */
  2005. for (line = 0; line < numLines; line++) {
  2006. if (strLines) {
  2007. text = strLines[line];
  2008. }
  2009. first = SDL_TRUE;
  2010. xstart = 0;
  2011. for (i = 0; i < glyph_count; i++) {
  2012. int y_offset;
  2013. error = Find_GlyphByIndex(font, g_info[i].index, CACHED_METRICS|CACHED_PIXMAP);
  2014. if (error) {
  2015. TTF_SetFTError("Couldn't find glyph", error);
  2016. SDL_FreeSurface(textbuf);
  2017. if (strLines) {
  2018. SDL_free(strLines);
  2019. SDL_stack_free(str);
  2020. }
  2021. return NULL;
  2022. }
  2023. glyph = font->current;
  2024. y_offset = glyph->yoffset - FT_FLOOR(g_info[i].y_offset);
  2025. /* Ensure the width of the pixmap is correct. On some cases,
  2026. * freetype may report a larger pixmap than possible.*/
  2027. width = glyph->pixmap.width;
  2028. if (font->outline <= 0 && width > glyph->maxx - glyph->minx) {
  2029. width = glyph->maxx - glyph->minx;
  2030. }
  2031. /* Compensate for the wrap around bug with negative minx's */
  2032. if (first && (glyph->minx < 0)) {
  2033. xstart -= glyph->minx;
  2034. }
  2035. first = SDL_FALSE;
  2036. for (row = 0; row < glyph->pixmap.rows; ++row) {
  2037. /* Make sure we don't go either over, or under the
  2038. * limit */
  2039. if ( row+y_offset < 0 ) {
  2040. continue;
  2041. }
  2042. if ( row+y_offset >= textbuf->h ) {
  2043. continue;
  2044. }
  2045. dst = ((Uint32*)textbuf->pixels + rowSize * line) +
  2046. (row+y_offset) * textbuf->pitch/4 +
  2047. xstart + FT_FLOOR(g_info[i].x_offset) + glyph->minx;
  2048. /* Added code to adjust src pointer for pixmaps to
  2049. * account for pitch.
  2050. * */
  2051. src = (Uint8*) (glyph->pixmap.buffer + glyph->pixmap.pitch * row);
  2052. for (col = width; col>0 && dst < dst_check; --col) {
  2053. alpha = *src++;
  2054. *dst++ |= pixel | ((Uint32)alpha_table[alpha] << 24);
  2055. }
  2056. }
  2057. xstart += FT_FLOOR(g_info[i].x_advance);
  2058. if (TTF_HANDLE_STYLE_BOLD(font)) {
  2059. xstart += font->glyph_overhang;
  2060. }
  2061. prev_index = g_info[i].index;
  2062. }
  2063. /* Handle the underline style *
  2064. if (TTF_HANDLE_STYLE_UNDERLINE(font)) {
  2065. row = TTF_underline_top_row(font);
  2066. TTF_drawLine_Blended(font, textbuf, row, pixel | (Uint32)fg.a << 24);
  2067. }
  2068. */
  2069. /* Handle the strikethrough style *
  2070. if (TTF_HANDLE_STYLE_STRIKETHROUGH(font)) {
  2071. row = TTF_strikethrough_top_row(font);
  2072. TTF_drawLine_Blended(font, textbuf, row, pixel | (Uint32)fg.a << 24);
  2073. }
  2074. */
  2075. }
  2076. if (strLines) {
  2077. SDL_free(strLines);
  2078. SDL_stack_free(str);
  2079. }
  2080. raqm_destroy(rq);
  2081. return(textbuf);
  2082. }
  2083. SDL_Surface *TTF_RenderUNICODE_Blended_Wrapped(TTF_Font *font, const Uint16* text,
  2084. SDL_Color fg, Uint32 wrapLength)
  2085. {
  2086. SDL_Surface *surface = NULL;
  2087. Uint8 *utf8;
  2088. TTF_CHECKPOINTER(text, NULL);
  2089. utf8 = SDL_stack_alloc(Uint8, UCS2_to_UTF8_len(text));
  2090. if (utf8) {
  2091. UCS2_to_UTF8(text, utf8);
  2092. surface = TTF_RenderUTF8_Blended_Wrapped(font, (char *)utf8, fg, wrapLength);
  2093. SDL_stack_free(utf8);
  2094. } else {
  2095. SDL_OutOfMemory();
  2096. }
  2097. return surface;
  2098. }
  2099. SDL_Surface *TTF_RenderGlyph_Blended(TTF_Font *font, Uint16 ch, SDL_Color fg)
  2100. {
  2101. Uint16 ucs2[2];
  2102. Uint8 utf8[4];
  2103. ucs2[0] = ch;
  2104. ucs2[1] = 0;
  2105. UCS2_to_UTF8(ucs2, utf8);
  2106. return TTF_RenderUTF8_Blended(font, (char *)utf8, fg);
  2107. }
  2108. void TTF_SetFontStyle(TTF_Font* font, int style)
  2109. {
  2110. int prev_style = font->style;
  2111. font->style = style | font->face_style;
  2112. /* Flush the cache if the style has changed.
  2113. * Ignore UNDERLINE which does not impact glyph drawning.
  2114. * */
  2115. if ((font->style | TTF_STYLE_NO_GLYPH_CHANGE) != (prev_style | TTF_STYLE_NO_GLYPH_CHANGE)) {
  2116. Flush_Cache(font);
  2117. }
  2118. }
  2119. int TTF_GetFontStyle(const TTF_Font* font)
  2120. {
  2121. return font->style;
  2122. }
  2123. void TTF_SetFontOutline(TTF_Font* font, int outline)
  2124. {
  2125. font->outline = outline;
  2126. Flush_Cache(font);
  2127. }
  2128. int TTF_GetFontOutline(const TTF_Font* font)
  2129. {
  2130. return font->outline;
  2131. }
  2132. void TTF_SetFontHinting(TTF_Font* font, int hinting)
  2133. {
  2134. if (hinting == TTF_HINTING_LIGHT)
  2135. font->hinting = FT_LOAD_TARGET_LIGHT;
  2136. else if (hinting == TTF_HINTING_MONO)
  2137. font->hinting = FT_LOAD_TARGET_MONO;
  2138. else if (hinting == TTF_HINTING_NONE)
  2139. font->hinting = FT_LOAD_NO_HINTING;
  2140. else
  2141. font->hinting = 0;
  2142. Flush_Cache(font);
  2143. }
  2144. int TTF_GetFontHinting(const TTF_Font* font)
  2145. {
  2146. if (font->hinting == FT_LOAD_TARGET_LIGHT)
  2147. return TTF_HINTING_LIGHT;
  2148. else if (font->hinting == FT_LOAD_TARGET_MONO)
  2149. return TTF_HINTING_MONO;
  2150. else if (font->hinting == FT_LOAD_NO_HINTING)
  2151. return TTF_HINTING_NONE;
  2152. return 0;
  2153. }
  2154. void TTF_Quit(void)
  2155. {
  2156. if (TTF_initialized) {
  2157. if (--TTF_initialized == 0) {
  2158. FT_Done_FreeType(library);
  2159. }
  2160. }
  2161. }
  2162. int TTF_WasInit(void)
  2163. {
  2164. return TTF_initialized;
  2165. }
  2166. /* don't use this function. It's just here for binary compatibility. */
  2167. int TTF_GetFontKerningSize(TTF_Font* font, int prev_index, int index)
  2168. {
  2169. FT_Vector delta;
  2170. FT_Get_Kerning(font->face, prev_index, index, ft_kerning_default, &delta);
  2171. return (delta.x >> 6);
  2172. }
  2173. int TTF_GetFontKerningSizeGlyphs(TTF_Font *font, Uint16 previous_ch, Uint16 ch)
  2174. {
  2175. int error;
  2176. int glyph_index, prev_index;
  2177. FT_Vector delta;
  2178. if (ch == UNICODE_BOM_NATIVE || ch == UNICODE_BOM_SWAPPED) {
  2179. return 0;
  2180. }
  2181. if (previous_ch == UNICODE_BOM_NATIVE || previous_ch == UNICODE_BOM_SWAPPED) {
  2182. return 0;
  2183. }
  2184. error = Find_Glyph(font, ch, CACHED_METRICS);
  2185. if (error) {
  2186. TTF_SetFTError("Couldn't find glyph", error);
  2187. return -1;
  2188. }
  2189. glyph_index = font->current->index;
  2190. error = Find_Glyph(font, previous_ch, CACHED_METRICS);
  2191. if (error) {
  2192. TTF_SetFTError("Couldn't find glyph", error);
  2193. return -1;
  2194. }
  2195. prev_index = font->current->index;
  2196. error = FT_Get_Kerning(font->face, prev_index, glyph_index, ft_kerning_default, &delta);
  2197. if (error) {
  2198. TTF_SetFTError("Couldn't get glyph kerning", error);
  2199. return -1;
  2200. }
  2201. return (delta.x >> 6);
  2202. }
  2203. /* vi: set ts=4 sw=4 expandtab: */