PageRenderTime 196ms CodeModel.GetById 94ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/v5_9_7_debian/src/plfreetype.c

#
C | 1287 lines | 677 code | 177 blank | 433 comment | 150 complexity | 295c233353904037eece46637912f4e5 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, Apache-2.0, GPL-2.0
  1. /* $Id: plfreetype.c 11176 2010-09-13 02:01:25Z airwin $
  2. *
  3. * Copyright (C) 2002, 2004, 2005 Andrew Roach
  4. * Copyright (C) 2002 Maurice LeBrun
  5. * Copyright (C) 2002, 2004, 2005 Alan W. Irwin
  6. * Copyright (C) 2003, 2004 Joao Cardoso
  7. * Copyright (C) 2003, 2004, 2005 Rafael Laboissiere
  8. * Copyright (C) 2004 Andrew Ross
  9. *
  10. * This file is part of PLplot.
  11. *
  12. * PLplot is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Library Public License as published
  14. * by the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * PLplot is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Library General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Library General Public License
  23. * along with PLplot; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. *
  26. *
  27. * Support routines for freetype font engine
  28. *
  29. * This file contains a series of support routines for drivers interested
  30. * in using freetype rendered fonts instead of plplot plotter fonts.
  31. * Freetype supports a gerth of font formats including TrueType, OpenType,
  32. * Adobe Type1, Type42 etc... the list seems almost endless. Any bitmap
  33. * driver should be able to use any of these freetype fonts from plplot if
  34. * these routines are properly initialised.
  35. *
  36. * Freetype support is not intended to be a "feature" of the common API,
  37. * but is implemented as a driver-specific optional extra invoked via the
  38. * -drvopt command line toggle. It is intended to be used in the context of
  39. * "PLESC_HAS_TEXT" for any bitmap drivers without native font support.
  40. * Implementing freetype in this manner minimise changes to the overall
  41. * API. Because of this approach, there is not a "wealth" of font options
  42. * available to the programmer. You can not do anything you can't do for a
  43. * normal freetype plotter font like boldface. You can do most of the
  44. * things that you can do with a plotter font however, like greek
  45. * characters superscripting, and selecting one of the four "pre-defined"
  46. * plplot font types. At present underlining and overlining are not
  47. * supported.
  48. *
  49. * To give the user some level of control over the fonts that are used,
  50. * environmental variables can be set to over-ride the definitions used by
  51. * the five default plplot fonts.
  52. *
  53. * The exact syntax for evoking freetype fonts is dependant on each
  54. * driver, but for the GD and GNUSVGA drivers I have followed the syntax of
  55. * the PS driver and use the command-line switch of "-drvopt text" to
  56. * activate the feature, and suggest other programmers do the same for
  57. * commonality.
  58. *
  59. * Both anti-aliased and monochrome font rendering is supported by these
  60. * routines. How these are evoked depends on the programmer, but with the
  61. * GD and GNUSVGA driver families I have used the command-line switch
  62. * "-drvopt smooth" to activate the feature; but, considering you also need
  63. * to turn freetype on, it would probably really be more like "-drvopt
  64. * text,smooth".
  65. *
  66. */
  67. #if !defined ( WIN32 ) || defined ( __GNUC__ )
  68. #include <unistd.h>
  69. #else
  70. #define F_OK 1
  71. #include <stdio.h>
  72. int access( char *filename, int flag )
  73. {
  74. FILE *infile;
  75. infile = fopen( filename, "r" );
  76. if ( infile != NULL )
  77. {
  78. fclose( infile );
  79. return 0;
  80. }
  81. else
  82. {
  83. return 1;
  84. }
  85. }
  86. #endif
  87. #define makeunixslash( b ) do { char *I; for ( I = b; *I != 0; *I++ ) if ( *I == '\\' ) *I = '/';} while ( 0 )
  88. #include "plDevs.h"
  89. #include "plplotP.h"
  90. #include "drivers.h"
  91. #ifdef HAVE_FREETYPE
  92. #include "plfreetype.h"
  93. #include "plfci-truetype.h"
  94. #define FT_Data _FT_Data_
  95. /* Font lookup table that is constructed in plD_FreeType_init*/
  96. PLDLLIMPEXP_DATA( FCI_to_FontName_Table ) FontLookup[N_TrueTypeLookup];
  97. /* TOP LEVEL DEFINES */
  98. /* Freetype lets you set the text size absolutely. It also takes into
  99. * account the DPI when doing so. So does plplot. Why, then, is it that the
  100. * size of the text drawn by plplot is bigger than the text drawn by
  101. * freetype when given IDENTICAL parameters ? Perhaps I am missing
  102. * something somewhere, but to fix this up we use TEXT_SCALING_FACTOR to
  103. * set a scaling factor to try and square things up a bit.
  104. */
  105. #define TEXT_SCALING_FACTOR .7
  106. /* default size of temporary text buffer */
  107. /* If we wanted to be fancy we could add sizing, but this should be big enough */
  108. #define NTEXT_ALLOC 1024
  109. /*--------------------------------------------------------------------------*\
  110. * Some debugging macros
  111. \*--------------------------------------------------------------------------*/
  112. #define Debug6( a, b, c, d, e, f ) do { if ( pls->debug ) { fprintf( stderr, a, b, c, d, e, f ); } } while ( 0 )
  113. /* FUNCTION PROTOTYPES */
  114. /* Public prototypes, generally available to the API */
  115. void plD_FreeType_init( PLStream *pls );
  116. void plD_render_freetype_text( PLStream *pls, EscText *args );
  117. void plD_FreeType_Destroy( PLStream *pls );
  118. void pl_set_extended_cmap0( PLStream *pls, int ncol0_width, int ncol0_org );
  119. void pl_RemakeFreeType_text_from_buffer( PLStream *pls );
  120. void plD_render_freetype_sym( PLStream *pls, EscText *args );
  121. /* Private prototypes for use in this file only */
  122. static void FT_PlotChar( PLStream *pls, FT_Data *FT, FT_GlyphSlot slot, int x, int y, short colour );
  123. static void FT_SetFace( PLStream *pls, PLUNICODE fci );
  124. static PLFLT CalculateIncrement( int bg, int fg, int levels );
  125. /* These are never defined, maybe they will be used in the future?
  126. *
  127. * static void pl_save_FreeType_text_to_buffer (PLStream *pls, EscText *args);
  128. * static FT_ULong hershey_to_unicode (char in);
  129. *
  130. */
  131. static void FT_WriteStrW( PLStream *pls, const PLUNICODE *text, short len, int x, int y );
  132. static void FT_StrX_YW( PLStream *pls, const PLUNICODE *text, short len, int *xx, int *yy );
  133. /*----------------------------------------------------------------------*\
  134. * FT_StrX_YW()
  135. *
  136. * Returns the dimensions of the text box. It does this by fully parsing
  137. * the supplied text through the rendering engine. It does everything
  138. * but draw the text. This seems, to me, the easiest and most accurate
  139. * way of determining the text's dimensions. If/when caching is added,
  140. * the CPU hit for this "double processing" will be minimal.
  141. \*----------------------------------------------------------------------*/
  142. void
  143. FT_StrX_YW( PLStream *pls, const PLUNICODE *text, short len, int *xx, int *yy )
  144. {
  145. FT_Data *FT = (FT_Data *) pls->FT;
  146. short i = 0;
  147. FT_Vector akerning;
  148. int x = 0, y = 0;
  149. char esc;
  150. plgesc( &esc );
  151. /*
  152. * Things seems to work better with this line than without it;
  153. * I guess because there is no vertical kerning or advancement for most
  154. * non-transformed fonts, so we need to define *something* for the y height,
  155. * and this is the best thing I could think of.
  156. */
  157. y -= FT->face->size->metrics.height;
  158. /* walk through the text character by character */
  159. for ( i = 0; i < len; i++ )
  160. {
  161. if ( ( text[i] == esc ) && ( text[i - 1] != esc ) )
  162. {
  163. if ( text[i + 1] == esc )
  164. continue;
  165. switch ( text[i + 1] )
  166. {
  167. case 'u': /* super script */
  168. case 'd': /* subscript */
  169. case 'U':
  170. case 'D':
  171. i++;
  172. break;
  173. }
  174. }
  175. else if ( text[i] & PL_FCI_MARK )
  176. {
  177. /* FCI in text stream; change font accordingly. */
  178. FT_SetFace( pls, text[i] );
  179. }
  180. else
  181. {
  182. /* see if we have kerning for the particular character pair */
  183. if ( ( i > 0 ) && FT_HAS_KERNING( FT->face ) )
  184. {
  185. FT_Get_Kerning( FT->face,
  186. text[i - 1],
  187. text[i],
  188. ft_kerning_default,
  189. &akerning );
  190. x += ( akerning.x >> 6 ); /* add (or subtract) the kerning */
  191. }
  192. /*
  193. * Next we load the char. This also draws the char, transforms it, and
  194. * converts it to a bitmap. At present this is a bit wasteful, but
  195. * if/when I add cache support, then this data won't go to waste.
  196. * Since there is no sense in going to the trouble of doing anti-aliasing
  197. * calculations since we aren't REALLY plotting anything, we will render
  198. * this as monochrome since it is probably marginally quicker. If/when
  199. * cache support is added, naturally this will have to change.
  200. */
  201. FT_Load_Char( FT->face, text[i], FT_LOAD_MONOCHROME + FT_LOAD_RENDER );
  202. /*
  203. * Add in the "advancement" needed to position the cursor for the next
  204. * character. Unless the text is transformed, "y" will always be zero.
  205. * Y is negative because freetype does things upside down
  206. */
  207. x += ( FT->face->glyph->advance.x );
  208. y -= ( FT->face->glyph->advance.y );
  209. }
  210. }
  211. /*
  212. * Convert from unit of 1/64 of a pixel to pixels, and do it real fast with
  213. * a bitwise shift (mind you, any decent compiler SHOULD optimise /64 this way
  214. * anyway...)
  215. */
  216. /* (RL, on 2005-01-23) Removed the shift bellow to avoid truncation errors
  217. * later.
  218. *yy=y>> 6;
  219. *xx=x>> 6;
  220. */
  221. *yy = y;
  222. *xx = x;
  223. }
  224. /*----------------------------------------------------------------------*\
  225. * FT_WriteStrW()
  226. *
  227. * Writes a string of FT text at the current cursor location.
  228. * most of the code here is identical to "FT_StrX_Y" and I will probably
  229. * collapse the two into some more efficient code eventually.
  230. \*----------------------------------------------------------------------*/
  231. void
  232. FT_WriteStrW( PLStream *pls, const PLUNICODE *text, short len, int x, int y )
  233. {
  234. FT_Data *FT = (FT_Data *) pls->FT;
  235. short i = 0, last_char = -1;
  236. FT_Vector akerning, adjust;
  237. char esc;
  238. plgesc( &esc );
  239. /*
  240. * Adjust for the descender - make sure the font is nice and centred
  241. * vertically. Freetype assumes we have a base-line, but plplot thinks of
  242. * centre-lines, so that's why we have to do this. Since this is one of our
  243. * own adjustments, rather than a freetype one, we have to run it through
  244. * the transform matrix manually.
  245. *
  246. * For some odd reason, this works best if we triple the
  247. * descender's height and then adjust the height later on...
  248. * Don't ask me why, 'cause I don't know. But it does seem to work.
  249. *
  250. * I really wish I knew *why* it worked better though...
  251. *
  252. * y-=FT->face->descender >> 6;
  253. */
  254. #ifdef DODGIE_DECENDER_HACK
  255. adjust.y = ( FT->face->descender >> 6 ) * 3;
  256. #else
  257. adjust.y = ( FT->face->descender >> 6 );
  258. #endif
  259. /* (RL) adjust.y is zeroed below,, making the code above (around
  260. * DODGIE_DECENDER_HACK) completely useless. This is necessary for
  261. * getting the vertical alignment of text right, which is coped with
  262. * in function plD_render_freetype_text now.
  263. */
  264. adjust.x = 0;
  265. adjust.y = 0;
  266. FT_Vector_Transform( &adjust, &FT->matrix );
  267. x += adjust.x;
  268. y -= adjust.y;
  269. /* (RL, on 2005-01-25) The computation of cumulated glyph width within
  270. * the text is done now with full precision, using 26.6 Freetype
  271. * arithmetics. We should then shift the x and y variables by 6 bits,
  272. * as below. Inside the character for loop, all operations regarding
  273. * x and y will be done in 26.6 mode and these variables will be
  274. * converted to integers when passed to FT_PlotChar. Notrice that we
  275. * are using ROUND and float division instead of ">> 6" now. This
  276. * minimizes truncation errors.
  277. */
  278. x <<= 6;
  279. y <<= 6;
  280. /* walk through the text character by character */
  281. for ( i = 0; i < len; i++ )
  282. {
  283. if ( ( text[i] == esc ) && ( text[i - 1] != esc ) )
  284. {
  285. if ( text[i + 1] == esc )
  286. continue;
  287. switch ( text[i + 1] )
  288. {
  289. /*
  290. * We run the OFFSET for the super-script and sub-script through the
  291. * transformation matrix so we can calculate nice and easy the required
  292. * offset no matter what's happened rotation wise. Everything else, like
  293. * kerning and advancing from character to character is transformed
  294. * automatically by freetype, but since the superscript/subscript is a
  295. * feature of plplot, and not freetype, we have to make allowances.
  296. */
  297. case 'u': /* super script */
  298. case 'U': /* super script */
  299. adjust.y = FT->face->size->metrics.height / 2;
  300. adjust.x = 0;
  301. FT_Vector_Transform( &adjust, &FT->matrix );
  302. x += adjust.x;
  303. y -= adjust.y;
  304. i++;
  305. break;
  306. case 'd': /* subscript */
  307. case 'D': /* subscript */
  308. adjust.y = -FT->face->size->metrics.height / 2;
  309. adjust.x = 0;
  310. FT_Vector_Transform( &adjust, &FT->matrix );
  311. x += adjust.x;
  312. y -= adjust.y;
  313. i++;
  314. break;
  315. }
  316. }
  317. else if ( text[i] & PL_FCI_MARK )
  318. {
  319. /* FCI in text stream; change font accordingly. */
  320. FT_SetFace( pls, text[i] );
  321. FT = (FT_Data *) pls->FT;
  322. FT_Set_Transform( FT->face, &FT->matrix, &FT->pos );
  323. }
  324. else
  325. {
  326. /* see if we have kerning for the particular character pair */
  327. if ( ( last_char != -1 ) && ( i > 0 ) && FT_HAS_KERNING( FT->face ) )
  328. {
  329. FT_Get_Kerning( FT->face,
  330. text[last_char],
  331. text[i],
  332. ft_kerning_default, &akerning );
  333. x += akerning.x; /* add (or subtract) the kerning */
  334. y -= akerning.y; /* Do I need this in case of rotation ? */
  335. }
  336. FT_Load_Char( FT->face, text[i], ( FT->smooth_text == 0 ) ? FT_LOAD_MONOCHROME + FT_LOAD_RENDER : FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT );
  337. FT_PlotChar( pls, FT, FT->face->glyph,
  338. ROUND( x / 64.0 ), ROUND( y / 64.0 ), 2 ); /* render the text */
  339. x += FT->face->glyph->advance.x;
  340. y -= FT->face->glyph->advance.y;
  341. last_char = i;
  342. }
  343. } /* end for */
  344. }
  345. /*----------------------------------------------------------------------*\
  346. * FT_PlotChar()
  347. *
  348. * Plots an individual character. I know some of this stuff, like colour
  349. * could be parsed from plstream, but it was just quicker this way.
  350. \*----------------------------------------------------------------------*/
  351. void
  352. FT_PlotChar( PLStream *pls, FT_Data *FT, FT_GlyphSlot slot,
  353. int x, int y, short colour )
  354. {
  355. unsigned char bittest;
  356. short i, k, j;
  357. int n = slot->bitmap.pitch;
  358. int current_pixel_colour;
  359. int R, G, B;
  360. PLFLT alpha_a, alpha_b;
  361. int xx;
  362. short imin, imax, kmin, kmax;
  363. /* Corners of the clipping rectangle */
  364. PLINT clipxmin, clipymin, clipxmax, clipymax, tmp;
  365. PLINT clpxmi, clpxma, clpymi, clpyma;
  366. /* Convert clipping box into normal coordinates */
  367. clipxmin = pls->clpxmi;
  368. clipxmax = pls->clpxma;
  369. clipymin = pls->clpymi;
  370. clipymax = pls->clpyma;
  371. if ( plsc->difilt )
  372. {
  373. difilt( &clipxmin, &clipymin, 1, &clpxmi, &clpxma, &clpymi, &clpyma );
  374. difilt( &clipxmax, &clipymax, 1, &clpxmi, &clpxma, &clpymi, &clpyma );
  375. }
  376. if ( FT->scale != 0.0 ) /* scale was set */
  377. {
  378. clipxmin = clipxmin / FT->scale;
  379. clipxmax = clipxmax / FT->scale;
  380. if ( FT->invert_y == 1 )
  381. {
  382. clipymin = FT->ymax - ( clipymin / FT->scale );
  383. clipymax = FT->ymax - ( clipymax / FT->scale );
  384. }
  385. else
  386. {
  387. clipymin = clipymin / FT->scale;
  388. clipymax = clipymax / FT->scale;
  389. }
  390. }
  391. else
  392. {
  393. clipxmin = clipxmin / FT->scalex;
  394. clipxmax = clipxmax / FT->scalex;
  395. if ( FT->invert_y == 1 )
  396. {
  397. clipymin = FT->ymax - ( clipymin / FT->scaley );
  398. clipymax = FT->ymax - ( clipymax / FT->scaley );
  399. }
  400. else
  401. {
  402. clipymin = clipymin / FT->scaley;
  403. clipymax = clipymax / FT->scaley;
  404. }
  405. }
  406. if ( clipxmin > clipxmax )
  407. {
  408. tmp = clipxmax;
  409. clipxmax = clipxmin;
  410. clipxmin = tmp;
  411. }
  412. if ( clipymin > clipymax )
  413. {
  414. tmp = clipymax;
  415. clipymax = clipymin;
  416. clipymin = tmp;
  417. }
  418. /* Comment this out as it fails for cases where we want to plot text
  419. * in the background font, i.e. example 24.
  420. */
  421. /*if ((slot->bitmap.pixel_mode==ft_pixel_mode_mono)||(pls->icol0==0)) {*/
  422. if ( slot->bitmap.pixel_mode == ft_pixel_mode_mono )
  423. {
  424. x += slot->bitmap_left;
  425. y -= slot->bitmap_top;
  426. imin = MAX( 0, clipymin - y );
  427. imax = MIN( slot->bitmap.rows, clipymax - y );
  428. for ( i = imin; i < imax; i++ )
  429. {
  430. for ( k = 0; k < n; k++ )
  431. {
  432. bittest = 128;
  433. for ( j = 0; j < 8; j++ )
  434. {
  435. if ( ( bittest & (unsigned char) slot->bitmap.buffer[( i * n ) + k] ) == bittest )
  436. {
  437. xx = x + ( k * 8 ) + j;
  438. if ( ( xx >= clipxmin ) && ( xx <= clipxmax ) )
  439. FT->pixel( pls, xx, y + i );
  440. }
  441. bittest >>= 1;
  442. }
  443. }
  444. }
  445. }
  446. /* this is the anti-aliased stuff */
  447. else
  448. {
  449. x += slot->bitmap_left;
  450. y -= slot->bitmap_top;
  451. imin = MAX( 0, clipymin - y );
  452. imax = MIN( slot->bitmap.rows, clipymax - y );
  453. kmin = MAX( 0, clipxmin - x );
  454. kmax = MIN( slot->bitmap.width, clipxmax - x );
  455. for ( i = imin; i < imax; i++ )
  456. {
  457. for ( k = kmin; k < kmax; k++ )
  458. {
  459. FT->shade = ( slot->bitmap.buffer[( i * slot->bitmap.width ) + k] );
  460. if ( FT->shade > 0 )
  461. {
  462. if ( ( FT->BLENDED_ANTIALIASING == 1 ) && ( FT->read_pixel != NULL ) )
  463. /* The New anti-aliasing technique */
  464. {
  465. if ( FT->shade == 255 )
  466. {
  467. FT->pixel( pls, x + k, y + i );
  468. }
  469. else
  470. {
  471. current_pixel_colour = FT->read_pixel( pls, x + k, y + i );
  472. G = GetGValue( current_pixel_colour );
  473. R = GetRValue( current_pixel_colour );
  474. B = GetBValue( current_pixel_colour );
  475. alpha_a = (float) FT->shade / 255.0;
  476. /* alpha_b=1.0-alpha_a;
  477. * R=(plsc->cmap0[pls->icol0].r*alpha_a)+(R*alpha_b);
  478. * G=(plsc->cmap0[pls->icol0].g*alpha_a)+(G*alpha_b);
  479. * B=(plsc->cmap0[pls->icol0].b*alpha_a)+(B*alpha_b);
  480. */
  481. /* This next bit of code is, I *think*, computationally
  482. * more efficient than the bit above. It results in
  483. * an indistinguishable plot, but file sizes are different
  484. * suggesting subtle variations doubtless caused by rounding
  485. * and/or floating point conversions. Questions are - which is
  486. * better ? Which is more "correct" ? Does it make a difference ?
  487. * Is one faster than the other so that you'd ever notice ?
  488. */
  489. R = ( ( ( plsc->cmap0[pls->icol0].r - R ) * alpha_a ) + R );
  490. G = ( ( ( plsc->cmap0[pls->icol0].g - G ) * alpha_a ) + G );
  491. B = ( ( ( plsc->cmap0[pls->icol0].b - B ) * alpha_a ) + B );
  492. FT->set_pixel( pls, x + k, y + i, RGB( R > 255 ? 255 : R, G > 255 ? 255 : G, B > 255 ? 255 : B ) );
  493. }
  494. }
  495. else /* The old anti-aliasing technique */
  496. {
  497. FT->col_idx = FT->ncol0_width - ( ( FT->ncol0_width * FT->shade ) / 255 );
  498. FT->last_icol0 = pls->icol0;
  499. plcol0( pls->icol0 + ( FT->col_idx * ( FT->ncol0_org - 1 ) ) );
  500. FT->pixel( pls, x + k, y + i );
  501. plcol0( FT->last_icol0 );
  502. }
  503. }
  504. }
  505. }
  506. }
  507. }
  508. /*----------------------------------------------------------------------*\
  509. * plD_FreeType_init()
  510. *
  511. * Allocates memory to Freetype structure
  512. * Initialises the freetype library.
  513. * Initialises freetype structure
  514. \*----------------------------------------------------------------------*/
  515. void plD_FreeType_init( PLStream *pls )
  516. {
  517. FT_Data *FT;
  518. char *a;
  519. /* font paths and file names can be long so leave generous (1024) room */
  520. char font_dir[PLPLOT_MAX_PATH];
  521. /* N.B. must be in exactly same order as TrueTypeLookup */
  522. const char *env_font_names[N_TrueTypeLookup] = {
  523. "PLPLOT_FREETYPE_SANS_FONT",
  524. "PLPLOT_FREETYPE_SERIF_FONT",
  525. "PLPLOT_FREETYPE_MONO_FONT",
  526. "PLPLOT_FREETYPE_SCRIPT_FONT",
  527. "PLPLOT_FREETYPE_SYMBOL_FONT",
  528. "PLPLOT_FREETYPE_SANS_ITALIC_FONT",
  529. "PLPLOT_FREETYPE_SERIF_ITALIC_FONT",
  530. "PLPLOT_FREETYPE_MONO_ITALIC_FONT",
  531. "PLPLOT_FREETYPE_SCRIPT_ITALIC_FONT",
  532. "PLPLOT_FREETYPE_SYMBOL_ITALIC_FONT",
  533. "PLPLOT_FREETYPE_SANS_OBLIQUE_FONT",
  534. "PLPLOT_FREETYPE_SERIF_OBLIQUE_FONT",
  535. "PLPLOT_FREETYPE_MONO_OBLIQUE_FONT",
  536. "PLPLOT_FREETYPE_SCRIPT_OBLIQUE_FONT",
  537. "PLPLOT_FREETYPE_SYMBOL_OBLIQUE_FONT",
  538. "PLPLOT_FREETYPE_SANS_BOLD_FONT",
  539. "PLPLOT_FREETYPE_SERIF_BOLD_FONT",
  540. "PLPLOT_FREETYPE_MONO_BOLD_FONT",
  541. "PLPLOT_FREETYPE_SCRIPT_BOLD_FONT",
  542. "PLPLOT_FREETYPE_SYMBOL_BOLD_FONT",
  543. "PLPLOT_FREETYPE_SANS_BOLD_ITALIC_FONT",
  544. "PLPLOT_FREETYPE_SERIF_BOLD_ITALIC_FONT",
  545. "PLPLOT_FREETYPE_MONO_BOLD_ITALIC_FONT",
  546. "PLPLOT_FREETYPE_SCRIPT_BOLD_ITALIC_FONT",
  547. "PLPLOT_FREETYPE_SYMBOL_BOLD_ITALIC_FONT",
  548. "PLPLOT_FREETYPE_SANS_BOLD_OBLIQUE_FONT",
  549. "PLPLOT_FREETYPE_SERIF_BOLD_OBLIQUE_FONT",
  550. "PLPLOT_FREETYPE_MONO_BOLD_OBLIQUE_FONT",
  551. "PLPLOT_FREETYPE_SCRIPT_BOLD_OBLIQUE_FONT",
  552. "PLPLOT_FREETYPE_SYMBOL_BOLD_OBLIQUE_FONT"
  553. };
  554. short i;
  555. #if defined ( MSDOS ) || defined ( WIN32 )
  556. static char *default_font_names[] = { "arial.ttf", "times.ttf", "timesi.ttf", "arial.ttf",
  557. "symbol.ttf" };
  558. char WINDIR_PATH[PLPLOT_MAX_PATH];
  559. char *b;
  560. b = getenv( "WINDIR" );
  561. strncpy( WINDIR_PATH, b, PLPLOT_MAX_PATH - 1 );
  562. WINDIR_PATH[PLPLOT_MAX_PATH - 1] = '\0';
  563. #else
  564. const char *default_unix_font_dir = PL_FREETYPE_FONT_DIR;
  565. #endif
  566. if ( pls->FT )
  567. {
  568. plwarn( "Freetype seems already to have been initialised!" );
  569. return;
  570. }
  571. if ( ( pls->FT = calloc( 1, (size_t) sizeof ( FT_Data ) ) ) == NULL )
  572. plexit( "Could not allocate memory for Freetype" );
  573. FT = (FT_Data *) pls->FT;
  574. if ( ( FT->textbuf = calloc( NTEXT_ALLOC, 1 ) ) == NULL )
  575. plexit( "Could not allocate memory for Freetype text buffer" );
  576. if ( FT_Init_FreeType( &FT->library ) )
  577. plexit( "Could not initialise Freetype library" );
  578. /* set to an impossible value for an FCI */
  579. FT->fci = PL_FCI_IMPOSSIBLE;
  580. #if defined ( MSDOS ) || defined ( WIN32 )
  581. /*
  582. * Work out if we have Win95+ or Win3.?... sort of.
  583. * Actually, this just tries to find the place where the fonts live by looking
  584. * for arial, which should be on all windows machines.
  585. * At present, it only looks in two places, on one drive. I might change this
  586. * soon.
  587. */
  588. if ( WINDIR_PATH == NULL )
  589. {
  590. if ( access( "c:\\windows\\fonts\\arial.ttf", F_OK ) == 0 )
  591. {
  592. strcpy( font_dir, "c:/windows/fonts/" );
  593. }
  594. else if ( access( "c:\\windows\\system\\arial.ttf", F_OK ) == 0 )
  595. {
  596. strcpy( font_dir, "c:/windows/system/" );
  597. }
  598. else
  599. plwarn( "Could not find font path; I sure hope you have defined fonts manually !" );
  600. }
  601. else
  602. {
  603. strncat( WINDIR_PATH, "\\fonts\\arial.ttf", PLPLOT_MAX_PATH - 1 - strlen( WINDIR_PATH ) );
  604. if ( access( WINDIR_PATH, F_OK ) == 0 )
  605. {
  606. b = strrchr( WINDIR_PATH, '\\' );
  607. b++;
  608. *b = 0;
  609. makeunixslash( WINDIR_PATH );
  610. strcpy( font_dir, WINDIR_PATH );
  611. }
  612. else
  613. plwarn( "Could not find font path; I sure hope you have defined fonts manually !" );
  614. }
  615. if ( pls->debug )
  616. fprintf( stderr, "%s\n", font_dir );
  617. #else
  618. /*
  619. * For Unix systems, we will set the font path up a little differently in
  620. * that the configured PL_FREETYPE_FONT_DIR has been set as the default path,
  621. * but the user can override this by setting the environmental variable
  622. * "PLPLOT_FREETYPE_FONT_DIR" to something else.
  623. * NOTE WELL - the trailing slash must be added for now !
  624. */
  625. if ( ( a = getenv( "PLPLOT_FREETYPE_FONT_DIR" ) ) != NULL )
  626. strncpy( font_dir, a, PLPLOT_MAX_PATH - 1 );
  627. else
  628. strncpy( font_dir, default_unix_font_dir, PLPLOT_MAX_PATH - 1 );
  629. font_dir[PLPLOT_MAX_PATH - 1] = '\0';
  630. #endif
  631. /*
  632. * The driver looks for N_TrueTypeLookup environmental variables
  633. * where the path and name of these fonts can be OPTIONALLY set,
  634. * overriding the configured default values.
  635. */
  636. for ( i = 0; i < N_TrueTypeLookup; i++ )
  637. {
  638. if ( ( a = getenv( env_font_names[i] ) ) != NULL )
  639. {
  640. /*
  641. * Work out if we have been given an absolute path to a font name, or just
  642. * a font name sans-path. To do this we will look for a directory separator
  643. * character, which means some system specific junk. DJGPP is all wise, and
  644. * understands both Unix and DOS conventions. DOS only knows DOS, and
  645. * I assume everything else knows Unix-speak. (Why Bill, didn't you just
  646. * pay the extra 15c and get a REAL separator???)
  647. */
  648. #ifdef MSDOS
  649. if ( a[1] == ':' ) /* check for MS-DOS absolute path */
  650. #else
  651. if ( ( a[0] == '/' ) || ( a[0] == '~' ) ) /* check for unix abs path */
  652. #endif
  653. strncpy( FT->font_name[i], a, PLPLOT_MAX_PATH - 1 );
  654. else
  655. {
  656. strncpy( FT->font_name[i], font_dir, PLPLOT_MAX_PATH - 1 );
  657. strncat( FT->font_name[i], a, PLPLOT_MAX_PATH - 1 - strlen( FT->font_name[i] ) );
  658. }
  659. }
  660. else
  661. {
  662. strncpy( FT->font_name[i], font_dir, PLPLOT_MAX_PATH - 1 );
  663. strncat( FT->font_name[i], (char *) TrueTypeLookup[i].pfont, PLPLOT_MAX_PATH - 1 - strlen( FT->font_name[i] ) );
  664. }
  665. FT->font_name[i][PLPLOT_MAX_PATH - 1] = '\0';
  666. {
  667. FILE *infile;
  668. if ( ( infile = fopen( FT->font_name[i], "r" ) ) == NULL )
  669. {
  670. char msgbuf[1024];
  671. snprintf( msgbuf, 1024,
  672. "plD_FreeType_init: Could not find the freetype compatible font:\n %s",
  673. FT->font_name[i] );
  674. plwarn( msgbuf );
  675. }
  676. else
  677. {
  678. fclose( infile );
  679. }
  680. }
  681. FontLookup[i].fci = TrueTypeLookup[i].fci;
  682. FontLookup[i].pfont = (unsigned char *) FT->font_name[i];
  683. }
  684. /*
  685. * Next, we check to see if -drvopt has been used on the command line to
  686. * over-ride any settings
  687. */
  688. }
  689. /*----------------------------------------------------------------------*\
  690. * FT_SetFace( PLStream *pls, PLUNICODE fci )
  691. *
  692. * Sets up the font face and size
  693. \*----------------------------------------------------------------------*/
  694. void FT_SetFace( PLStream *pls, PLUNICODE fci )
  695. {
  696. FT_Data *FT = (FT_Data *) pls->FT;
  697. double font_size = pls->chrht * 72 / 25.4; /* font_size in points, chrht is in mm */
  698. /* save a copy of character height and resolution */
  699. FT->chrht = pls->chrht;
  700. FT->xdpi = pls->xdpi;
  701. FT->ydpi = pls->ydpi;
  702. if ( fci != FT->fci )
  703. {
  704. char *font_name = plP_FCI2FontName( fci, FontLookup, N_TrueTypeLookup );
  705. if ( font_name == NULL )
  706. {
  707. if ( FT->fci == PL_FCI_IMPOSSIBLE )
  708. plexit( "FT_SetFace: Bad FCI and no previous valid font to fall back on" );
  709. else
  710. plwarn( "FT_SetFace: Bad FCI. Falling back to previous font." );
  711. }
  712. else
  713. {
  714. FT->fci = fci;
  715. if ( FT->face != NULL )
  716. {
  717. FT_Done_Face( FT->face );
  718. FT->face = NULL;
  719. }
  720. if ( FT->face == NULL )
  721. {
  722. if ( FT_New_Face( FT->library, font_name, 0, &FT->face ) )
  723. plexit( "FT_SetFace: Error loading a font in freetype" );
  724. }
  725. }
  726. }
  727. FT_Set_Char_Size( FT->face, 0,
  728. font_size * 64 / TEXT_SCALING_FACTOR, pls->xdpi,
  729. pls->ydpi );
  730. }
  731. /*----------------------------------------------------------------------*\
  732. * plD_render_freetype_text()
  733. *
  734. * Transforms the font
  735. * calculates real-world bitmap coordinates from plplot ones
  736. * renders text using freetype
  737. \*----------------------------------------------------------------------*/
  738. void plD_render_freetype_text( PLStream *pls, EscText *args )
  739. {
  740. FT_Data *FT = (FT_Data *) pls->FT;
  741. int x, y;
  742. int w = 0, h = 0;
  743. PLFLT *t = args->xform;
  744. FT_Matrix matrix;
  745. PLFLT angle = PI * pls->diorot / 2;
  746. /*
  747. * Used later in a commented out section (See Rotate The Page), if that
  748. * section will never be used again, remove these as well.
  749. * PLINT clxmin, clxmax, clymin, clymax;
  750. */
  751. PLFLT Sin_A, Cos_A;
  752. FT_Vector adjust;
  753. PLUNICODE fci;
  754. FT_Fixed height;
  755. PLFLT height_factor;
  756. if ( ( args->string != NULL ) || ( args->unicode_array_len > 0 ) )
  757. {
  758. /*
  759. * Work out if either the font size, the font face or the
  760. * resolution has changed.
  761. * If either has, then we will reload the font face.
  762. */
  763. plgfci( &fci );
  764. if ( ( FT->fci != fci ) || ( FT->chrht != pls->chrht ) || ( FT->xdpi != pls->xdpi ) || ( FT->ydpi != pls->ydpi ) )
  765. FT_SetFace( pls, fci );
  766. /* this will help work out underlining and overlining*/
  767. Debug6( "%s %d %d %d %d\n", "plD_render_freetype_text:",
  768. FT->face->underline_position >> 6,
  769. FT->face->descender >> 6,
  770. FT->face->ascender >> 6,
  771. ( ( FT->face->underline_position * -1 ) + FT->face->ascender ) >> 6 );
  772. /*
  773. * Now we work out how long the text is (for justification etc...) and how
  774. * high the text is. This is done on UN-TRANSFORMED text, since we will
  775. * apply our own transformations on it later, so it's necessary for us
  776. * to to turn all transformations off first, before calling the function
  777. * that calculates the text size.
  778. */
  779. FT->matrix.xx = 0x10000;
  780. FT->matrix.xy = 0x00000;
  781. FT->matrix.yx = 0x00000;
  782. FT->matrix.yy = 0x10000;
  783. FT_Vector_Transform( &FT->pos, &FT->matrix );
  784. FT_Set_Transform( FT->face, &FT->matrix, &FT->pos );
  785. FT_StrX_YW( pls, args->unicode_array, args->unicode_array_len, &w, &h );
  786. /*
  787. * Set up the transformation Matrix
  788. *
  789. * Fortunately this is almost identical to plplot's own transformation matrix;
  790. * you have NO idea how much effort that saves ! Some params are in a
  791. * different order, and Freetype wants integers whereas plplot likes floats,
  792. * but such differences are quite trivial.
  793. *
  794. * For some odd reason, this needs to be set a different way for DJGPP. Why ?
  795. * I wish I knew.
  796. */
  797. /* (RL, on 2005-01-21) The height_factor variable is introduced below.
  798. * It is used here and farther below when computing the vertical
  799. * adjustment. The rationale for its introduction is as follow: up to
  800. * now, the text produced with Hershey fonts was systematically taller
  801. * than the same text produced with TT fonts, and tha by a factor of
  802. * around 1.125 (I discovered this empirically). This corresponds
  803. * roughly to the ratio between total height and the ascender of some
  804. * TT faces. Hence the computation below. Remember that descender is
  805. * always a negative quantity.
  806. */
  807. height_factor = (PLFLT) ( FT->face->ascender - FT->face->descender )
  808. / FT->face->ascender;
  809. height = (FT_Fixed) ( 0x10000 * height_factor );
  810. #ifdef DJGPP
  811. FT->matrix.xx = height * t[0];
  812. FT->matrix.xy = height * t[2];
  813. FT->matrix.yx = height * t[1];
  814. FT->matrix.yy = height * t[3];
  815. #else
  816. FT->matrix.xx = height * t[0];
  817. FT->matrix.xy = height * t[1];
  818. FT->matrix.yx = height * t[2];
  819. FT->matrix.yy = height * t[3];
  820. #endif
  821. /* Rotate the Font
  822. *
  823. * If the page has been rotated using -ori, this is where we rotate the
  824. * font to point in the right direction. To make things nice and easy, we
  825. * will use freetypes matrix math stuff to do this for us.
  826. */
  827. Cos_A = cos( angle );
  828. Sin_A = sin( angle );
  829. matrix.xx = (FT_Fixed) 0x10000 * Cos_A;
  830. #ifdef DJGPP
  831. matrix.xy = (FT_Fixed) 0x10000 * Sin_A * -1;
  832. matrix.yx = (FT_Fixed) 0x10000 * Sin_A;
  833. #else
  834. matrix.xy = (FT_Fixed) 0x10000 * Sin_A;
  835. matrix.yx = (FT_Fixed) 0x10000 * Sin_A * -1;
  836. #endif
  837. matrix.yy = (FT_Fixed) 0x10000 * Cos_A;
  838. FT_Matrix_Multiply( &matrix, &FT->matrix );
  839. /* Calculate a Vector from the matrix
  840. *
  841. * This is closely related to the "transform matrix".
  842. * The matrix is used for rendering the glyph, while the vector is used for
  843. * calculating offsets of the text box, so we need both. Why ? I dunno, but
  844. * we have to live with it, and it works...
  845. */
  846. FT_Vector_Transform( &FT->pos, &FT->matrix );
  847. /* Transform the font face
  848. *
  849. * This is where our matrix transformation is calculated for the font face.
  850. * This is only done once for each unique transformation since it is "sticky"
  851. * within the font. Font rendering is done later, using the supplied matrix,
  852. * but invisibly to us from here on. I don't believe the vector is used, but
  853. * it is asked for.
  854. */
  855. FT_Set_Transform( FT->face, &FT->matrix, &FT->pos );
  856. /* Rotate the Page
  857. *
  858. * If the page has been rotated using -ori, this is we recalculate the
  859. * reference point for the text using plplot functions.
  860. */
  861. /* difilt(&args->x, &args->y, 1, &clxmin, &clxmax, &clymin, &clymax); */
  862. /*
  863. * Convert into normal coordinates from virtual coordinates
  864. */
  865. if ( FT->scale != 0.0 ) /* scale was set */
  866. {
  867. x = args->x / FT->scale;
  868. if ( FT->invert_y == 1 )
  869. y = FT->ymax - ( args->y / FT->scale );
  870. else
  871. y = args->y / FT->scale;
  872. }
  873. else
  874. {
  875. x = args->x / FT->scalex;
  876. if ( FT->invert_y == 1 )
  877. y = FT->ymax - ( args->y / FT->scaley );
  878. else
  879. y = args->y / FT->scaley;
  880. }
  881. /* Adjust for the justification and character height
  882. *
  883. * Eeeksss... this wasn't a nice bit of code to work out, let me tell you.
  884. * I could not work out an entirely satisfactory solution that made
  885. * logical sense, so came up with an "illogical" one as well.
  886. * The logical one works fine for text in the normal "portrait"
  887. * orientation, and does so for reasons you might expect it to work; But
  888. * for all other orientations, the text's base line is either a little
  889. * high, or a little low. This is because of the way the base-line pos
  890. * is calculated from the decender height. The "dodgie" way of calculating
  891. * the position is to use the character height here, then adjust for the
  892. * decender height by a three-fold factor later on. That approach seems to
  893. * work a little better for rotated pages, but why it should be so, I
  894. * don't understand. You can compile in or out which way you want it by
  895. * defining "DODGIE_DECENDER_HACK".
  896. *
  897. * note: the logic of the page rotation coming up next is that we pump in
  898. * the justification factor and then use freetype to rotate and transform
  899. * the values, which we then use to change the plotting location.
  900. */
  901. #ifdef DODGIE_DECENDER_HACK
  902. adjust.y = h;
  903. #else
  904. adjust.y = 0;
  905. #endif
  906. /* (RL, on 2005-01-24) The code below uses floating point and division
  907. * operations instead of integer shift used before. This is slower but
  908. * gives accurate placement of text in plots.
  909. */
  910. /* (RL, on 2005-01-21) The hack below is intended to align single
  911. * glyphs being generated via plpoin. The way to detect this
  912. * situation is completely hackish, I must admit, by checking whether the
  913. * length of the Unicode array is equal 2 and whether the first
  914. * character is actually a font-changing command to font number 4 (for
  915. * symbols). This is ugly because it depends on definitions set
  916. * elsewhere, but it works.
  917. *
  918. * The computation of the vertical and horizontal adjustments are
  919. * based on the bouding box of the glyph being loaded (since there is
  920. * only one glyph in the string in this case, we are okay here).
  921. */
  922. if ( ( args->unicode_array_len == 2 )
  923. && ( args->unicode_array[0] == ( PL_FCI_MARK | 0x004 ) ) )
  924. {
  925. adjust.x = args->just * ROUND( FT->face->glyph->metrics.width / 64.0 );
  926. adjust.y = (FT_Pos) ROUND( FT->face->glyph->metrics.height / 128.0 );
  927. }
  928. else
  929. {
  930. /* (RL, on 2005-01-21) The vertical adjustment is set below, making
  931. * the DODGIE conditional moot. I use the value of h as return by FT_StrX_YW,
  932. * which should correspond to the total height of the text being
  933. * drawn. Freetype aligns text around the baseline, while PLplot
  934. * aligns to the center of the ascender portion. We must then adjust
  935. * by half of the ascender and this is why there is a division by
  936. * height_factor below.
  937. */
  938. adjust.y = (FT_Pos)
  939. ROUND( FT->face->size->metrics.height / height_factor / 128.0 );
  940. adjust.x = (FT_Pos) ( args->just * ROUND( w / 64.0 ) );
  941. }
  942. FT_Vector_Transform( &adjust, &FT->matrix ); /* was /&matrix); - was I using the wrong matrix all this time ?*/
  943. x -= adjust.x;
  944. y += adjust.y;
  945. FT_WriteStrW( pls, args->unicode_array, args->unicode_array_len, x, y ); /* write it out */
  946. }
  947. else
  948. {
  949. plD_render_freetype_sym( pls, args );
  950. }
  951. }
  952. /*----------------------------------------------------------------------*\
  953. * plD_FreeType_Destroy()
  954. *
  955. * Restores cmap0 if it had been modifed for anti-aliasing
  956. * closes the freetype library.
  957. * Deallocates memory to the Freetype structure
  958. \*----------------------------------------------------------------------*/
  959. void plD_FreeType_Destroy( PLStream *pls )
  960. {
  961. FT_Data *FT = (FT_Data *) pls->FT;
  962. extern int FT_Done_Library( FT_Library library );
  963. if ( FT )
  964. {
  965. if ( ( FT->smooth_text == 1 ) && ( FT->BLENDED_ANTIALIASING == 0 ) )
  966. plscmap0n( FT->ncol0_org );
  967. if ( FT->textbuf )
  968. free( FT->textbuf );
  969. FT_Done_Library( FT->library );
  970. free( pls->FT );
  971. pls->FT = NULL;
  972. }
  973. }
  974. /*----------------------------------------------------------------------*\
  975. * PLFLT CalculateIncrement( int bg, int fg, int levels)
  976. *
  977. * Takes the value of the foreground, and the background, and when
  978. * given the number of desired steps, calculates how much to incriment
  979. * a value to transition from fg to bg.
  980. * This function only does it for one colour channel at a time.
  981. \*----------------------------------------------------------------------*/
  982. static PLFLT CalculateIncrement( int bg, int fg, int levels )
  983. {
  984. PLFLT ret = 0;
  985. if ( levels > 1 )
  986. {
  987. if ( fg > bg )
  988. ret = ( ( fg + 1 ) - bg ) / levels;
  989. else if ( fg < bg )
  990. ret = ( ( ( fg - 1 ) - bg ) / levels );
  991. }
  992. return ( ret );
  993. }
  994. /*----------------------------------------------------------------------*\
  995. * void pl_set_extended_cmap0(PLStream *pls, int ncol0_width, int ncol0_org)
  996. *
  997. * ncol0_width - how many greyscale levels to accolate to each CMAP0 entry
  998. * ncol0_org - the originl number of CMAP0 entries.
  999. *
  1000. * This function calcualtes and sets an extended CMAP0 entry for the
  1001. * driver. It is assumed that the caller has checked to make sure there is
  1002. * room for extending CMAP0 already.
  1003. *
  1004. * NOTES
  1005. * We don't bother calculating an entry for CMAP[0], the background.
  1006. * It is assumed the caller has already expanded the size of CMAP[0]
  1007. \*----------------------------------------------------------------------*/
  1008. void pl_set_extended_cmap0( PLStream *pls, int ncol0_width, int ncol0_org )
  1009. {
  1010. int i, j, k;
  1011. int r, g, b;
  1012. PLFLT r_inc, g_inc, b_inc;
  1013. for ( i = 1; i < ncol0_org; i++ )
  1014. {
  1015. r = pls->cmap0[i].r;
  1016. g = pls->cmap0[i].g;
  1017. b = pls->cmap0[i].b;
  1018. r_inc = CalculateIncrement( pls->cmap0[0].r, r, ncol0_width );
  1019. g_inc = CalculateIncrement( pls->cmap0[0].g, g, ncol0_width );
  1020. b_inc = CalculateIncrement( pls->cmap0[0].b, b, ncol0_width );
  1021. for ( j = 0, k = ncol0_org + i - 1; j < ncol0_width; j++, k += ( ncol0_org - 1 ) )
  1022. {
  1023. r -= r_inc;
  1024. g -= g_inc;
  1025. b -= b_inc;
  1026. if ( ( r < 0 ) || ( g < 0 ) || ( b < 0 ) )
  1027. plscol0( k, 0, 0, 0 );
  1028. else
  1029. plscol0( k, ( r > 0xff ? 0xff : r ), ( g > 0xff ? 0xff : g ), ( b > 0xff ? 0xff : b ) );
  1030. }
  1031. }
  1032. }
  1033. /*----------------------------------------------------------------------*\
  1034. * plD_render_freetype_sym( PLStream *pls, EscText *args )
  1035. * PLStream *pls - pointer to plot stream
  1036. * EscText *args - pointer to standard "string" object.
  1037. *
  1038. * This function is a simple rendering function which draws a single
  1039. * character at a time. The function is an alternative to the text
  1040. * functions which are considerably, and needlessly, more complicated
  1041. * than what we need here.
  1042. \*----------------------------------------------------------------------*/
  1043. void plD_render_freetype_sym( PLStream *pls, EscText *args )
  1044. {
  1045. FT_Data *FT = (FT_Data *) pls->FT;
  1046. int x, y;
  1047. FT_Vector adjust;
  1048. PLUNICODE fci;
  1049. if ( FT->scale != 0.0 ) /* scale was set */
  1050. {
  1051. x = args->x / FT->scale;
  1052. if ( FT->invert_y == 1 )
  1053. y = FT->ymax - ( args->y / FT->scale );
  1054. else
  1055. y = args->y / FT->scale;
  1056. }
  1057. else
  1058. {
  1059. x = args->x / FT->scalex;
  1060. if ( FT->invert_y == 1 )
  1061. y = FT->ymax - ( args->y / FT->scaley );
  1062. else
  1063. y = args->y / FT->scaley;
  1064. }
  1065. /*
  1066. * Adjust for the descender - make sure the font is nice and centred
  1067. * vertically. Freetype assumes we have a base-line, but plplot thinks of
  1068. * centre-lines, so that's why we have to do this. Since this is one of our
  1069. * own adjustments, rather than a freetype one, we have to run it through
  1070. * the transform matrix manually.
  1071. *
  1072. * For some odd reason, this works best if we triple the
  1073. * descender's height and then adjust the height later on...
  1074. * Don't ask me why, 'cause I don't know. But it does seem to work.
  1075. *
  1076. * I really wish I knew *why* it worked better though...
  1077. *
  1078. * y-=FT->face->descender >> 6;
  1079. */
  1080. #ifdef DODGIE_DECENDER_HACK
  1081. adjust.y = ( FT->face->descender >> 6 ) * 3;
  1082. #else
  1083. adjust.y = ( FT->face->descender >> 6 );
  1084. #endif
  1085. adjust.x = 0;
  1086. FT_Vector_Transform( &adjust, &FT->matrix );
  1087. x += adjust.x;
  1088. y -= adjust.y;
  1089. plgfci( &fci );
  1090. FT_SetFace( pls, fci );
  1091. FT = (FT_Data *) pls->FT;
  1092. FT_Set_Transform( FT->face, &FT->matrix, &FT->pos );
  1093. FT_Load_Char( FT->face, args->unicode_char, ( FT->smooth_text == 0 ) ? FT_LOAD_MONOCHROME + FT_LOAD_RENDER : FT_LOAD_RENDER | FT_LOAD_FORCE_AUTOHINT );
  1094. /*
  1095. * Now we have to try and componsate for the fact that the freetype glyphs are left
  1096. * justified, and plplot's glyphs are centred. To do this, we will just work out the
  1097. * advancment, halve it, and take it away from the x position. This wont be 100%
  1098. * accurate because "spacing" is factored into the right hand side of the glyph,
  1099. * but it is as good a way as I can think of.
  1100. */
  1101. x -= ( FT->face->glyph->advance.x >> 6 ) / 2;
  1102. FT_PlotChar( pls, FT, FT->face->glyph, x, y, pls->icol0 ); /* render the text */
  1103. }
  1104. #else
  1105. int
  1106. plfreetype()
  1107. {
  1108. return 0;
  1109. }
  1110. #endif