gtkfont.c 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564
  1. /*
  2. * Unified font management for GTK.
  3. *
  4. * PuTTY is willing to use both old-style X server-side bitmap
  5. * fonts _and_ GTK2/Pango client-side fonts. This requires us to
  6. * do a bit of work to wrap the two wildly different APIs into
  7. * forms the rest of the code can switch between seamlessly, and
  8. * also requires a custom font selector capable of handling both
  9. * types of font.
  10. */
  11. #include <assert.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <gtk/gtk.h>
  15. #if !GTK_CHECK_VERSION(3,0,0)
  16. #include <gdk/gdkkeysyms.h>
  17. #endif
  18. #define MAY_REFER_TO_GTK_IN_HEADERS
  19. #include "putty.h"
  20. #include "gtkfont.h"
  21. #include "gtkcompat.h"
  22. #include "gtkmisc.h"
  23. #include "tree234.h"
  24. #ifndef NOT_X_WINDOWS
  25. #include <gdk/gdkx.h>
  26. #include <X11/Xlib.h>
  27. #include <X11/Xutil.h>
  28. #include <X11/Xatom.h>
  29. #endif
  30. /*
  31. * Future work:
  32. *
  33. * - it would be nice to have a display of the current font name,
  34. * and in particular whether it's client- or server-side,
  35. * during the progress of the font selector.
  36. */
  37. #if !GLIB_CHECK_VERSION(1,3,7)
  38. #define g_ascii_strcasecmp g_strcasecmp
  39. #define g_ascii_strncasecmp g_strncasecmp
  40. #endif
  41. /*
  42. * Ad-hoc vtable mechanism to allow font structures to be
  43. * polymorphic.
  44. *
  45. * Any instance of `unifont' used in the vtable functions will
  46. * actually be the first element of a larger structure containing
  47. * data specific to the subtype. This is permitted by the ISO C
  48. * provision that one may safely cast between a pointer to a
  49. * structure and a pointer to its first element.
  50. */
  51. #define FONTFLAG_CLIENTSIDE 0x0001
  52. #define FONTFLAG_SERVERSIDE 0x0002
  53. #define FONTFLAG_SERVERALIAS 0x0004
  54. #define FONTFLAG_NONMONOSPACED 0x0008
  55. #define FONTFLAG_SORT_MASK 0x0007 /* used to disambiguate font families */
  56. typedef void (*fontsel_add_entry)(void *ctx, const char *realfontname,
  57. const char *family, const char *charset,
  58. const char *style, const char *stylekey,
  59. int size, int flags,
  60. const struct unifont_vtable *fontclass);
  61. struct unifont_vtable {
  62. /*
  63. * `Methods' of the `class'.
  64. */
  65. unifont *(*create)(GtkWidget *widget, const char *name, int wide, int bold,
  66. int shadowoffset, int shadowalways);
  67. unifont *(*create_fallback)(GtkWidget *widget, int height, int wide,
  68. int bold, int shadowoffset, int shadowalways);
  69. void (*destroy)(unifont *font);
  70. int (*has_glyph)(unifont *font, wchar_t glyph);
  71. void (*draw_text)(unifont_drawctx *ctx, unifont *font,
  72. int x, int y, const wchar_t *string, int len,
  73. int wide, int bold, int cellwidth);
  74. void (*draw_combining)(unifont_drawctx *ctx, unifont *font,
  75. int x, int y, const wchar_t *string, int len,
  76. int wide, int bold, int cellwidth);
  77. void (*enum_fonts)(GtkWidget *widget,
  78. fontsel_add_entry callback, void *callback_ctx);
  79. char *(*canonify_fontname)(GtkWidget *widget, const char *name, int *size,
  80. int *flags, int resolve_aliases);
  81. char *(*scale_fontname)(GtkWidget *widget, const char *name, int size);
  82. /*
  83. * `Static data members' of the `class'.
  84. */
  85. const char *prefix;
  86. };
  87. #ifndef NOT_X_WINDOWS
  88. /* ----------------------------------------------------------------------
  89. * X11 font implementation, directly using Xlib calls. Conditioned out
  90. * if X11 fonts aren't available at all (e.g. building with GTK3 for a
  91. * back end other than X).
  92. */
  93. static int x11font_has_glyph(unifont *font, wchar_t glyph);
  94. static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
  95. int x, int y, const wchar_t *string, int len,
  96. int wide, int bold, int cellwidth);
  97. static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
  98. int x, int y, const wchar_t *string,
  99. int len, int wide, int bold, int cellwidth);
  100. static unifont *x11font_create(GtkWidget *widget, const char *name,
  101. int wide, int bold,
  102. int shadowoffset, int shadowalways);
  103. static void x11font_destroy(unifont *font);
  104. static void x11font_enum_fonts(GtkWidget *widget,
  105. fontsel_add_entry callback, void *callback_ctx);
  106. static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
  107. int *size, int *flags,
  108. int resolve_aliases);
  109. static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
  110. int size);
  111. #ifdef DRAW_TEXT_CAIRO
  112. struct cairo_cached_glyph {
  113. cairo_surface_t *surface;
  114. unsigned char *bitmap;
  115. };
  116. #endif
  117. /*
  118. * Structure storing a single physical XFontStruct, plus associated
  119. * data.
  120. */
  121. typedef struct x11font_individual {
  122. /* The XFontStruct itself. */
  123. XFontStruct *xfs;
  124. /*
  125. * The `allocated' flag indicates whether we've tried to fetch
  126. * this subfont already (thus distinguishing xfs==NULL because we
  127. * haven't tried yet from xfs==NULL because we tried and failed,
  128. * so that we don't keep trying and failing subsequently).
  129. */
  130. int allocated;
  131. #ifdef DRAW_TEXT_CAIRO
  132. /*
  133. * A cache of glyph bitmaps downloaded from the X server when
  134. * we're in Cairo rendering mode. If glyphcache itself is
  135. * non-NULL, then entries in [0,nglyphs) are expected to be
  136. * initialised to either NULL or a bitmap pointer.
  137. */
  138. struct cairo_cached_glyph *glyphcache;
  139. int nglyphs;
  140. /*
  141. * X server paraphernalia for actually downloading the glyphs.
  142. */
  143. Pixmap pixmap;
  144. GC gc;
  145. int pixwidth, pixheight, pixoriginx, pixoriginy;
  146. /*
  147. * Paraphernalia for loading the resulting bitmaps into Cairo.
  148. */
  149. int rowsize, allsize, indexflip;
  150. #endif
  151. } x11font_individual;
  152. struct x11font {
  153. struct unifont u;
  154. /*
  155. * Individual physical X fonts. We store a number of these, for
  156. * automatically guessed bold and wide variants.
  157. */
  158. x11font_individual fonts[4];
  159. /*
  160. * `sixteen_bit' is true iff the font object is indexed by
  161. * values larger than a byte. That is, this flag tells us
  162. * whether we use XDrawString or XDrawString16, etc.
  163. */
  164. int sixteen_bit;
  165. /*
  166. * `variable' is true iff the font is non-fixed-pitch. This
  167. * enables some code which takes greater care over character
  168. * positioning during text drawing.
  169. */
  170. int variable;
  171. /*
  172. * real_charset is the charset used when translating text into the
  173. * font's internal encoding inside draw_text(). This need not be
  174. * the same as the public_charset provided to the client; for
  175. * example, public_charset might be CS_ISO8859_1 while
  176. * real_charset is CS_ISO8859_1_X11.
  177. */
  178. int real_charset;
  179. /*
  180. * Data passed in to unifont_create().
  181. */
  182. int wide, bold, shadowoffset, shadowalways;
  183. };
  184. static const struct unifont_vtable x11font_vtable = {
  185. x11font_create,
  186. NULL, /* no fallback fonts in X11 */
  187. x11font_destroy,
  188. x11font_has_glyph,
  189. x11font_draw_text,
  190. x11font_draw_combining,
  191. x11font_enum_fonts,
  192. x11font_canonify_fontname,
  193. x11font_scale_fontname,
  194. "server",
  195. };
  196. static char *x11_guess_derived_font_name(XFontStruct *xfs, int bold, int wide)
  197. {
  198. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  199. Atom fontprop = XInternAtom(disp, "FONT", False);
  200. unsigned long ret;
  201. if (XGetFontProperty(xfs, fontprop, &ret)) {
  202. char *name = XGetAtomName(disp, (Atom)ret);
  203. if (name && name[0] == '-') {
  204. const char *strings[13];
  205. char *dupname, *extrafree = NULL, *ret;
  206. char *p, *q;
  207. int nstr;
  208. p = q = dupname = dupstr(name); /* skip initial minus */
  209. nstr = 0;
  210. while (*p && nstr < lenof(strings)) {
  211. if (*p == '-') {
  212. *p = '\0';
  213. strings[nstr++] = p+1;
  214. }
  215. p++;
  216. }
  217. if (nstr < lenof(strings)) {
  218. sfree(dupname);
  219. return NULL; /* XLFD was malformed */
  220. }
  221. if (bold)
  222. strings[2] = "bold";
  223. if (wide) {
  224. /* 4 is `wideness', which obviously may have changed. */
  225. /* 5 is additional style, which may be e.g. `ja' or `ko'. */
  226. strings[4] = strings[5] = "*";
  227. strings[11] = extrafree = dupprintf("%d", 2*atoi(strings[11]));
  228. }
  229. ret = dupcat("-", strings[ 0], "-", strings[ 1], "-", strings[ 2],
  230. "-", strings[ 3], "-", strings[ 4], "-", strings[ 5],
  231. "-", strings[ 6], "-", strings[ 7], "-", strings[ 8],
  232. "-", strings[ 9], "-", strings[10], "-", strings[11],
  233. "-", strings[12], NULL);
  234. sfree(extrafree);
  235. sfree(dupname);
  236. return ret;
  237. }
  238. }
  239. return NULL;
  240. }
  241. static int x11_font_width(XFontStruct *xfs, int sixteen_bit)
  242. {
  243. if (sixteen_bit) {
  244. XChar2b space;
  245. space.byte1 = 0;
  246. space.byte2 = '0';
  247. return XTextWidth16(xfs, &space, 1);
  248. } else {
  249. return XTextWidth(xfs, "0", 1);
  250. }
  251. }
  252. static const XCharStruct *x11_char_struct(XFontStruct *xfs,
  253. int byte1, int byte2)
  254. {
  255. int index;
  256. /*
  257. * The man page for XQueryFont is rather confusing about how the
  258. * per_char array in the XFontStruct is laid out, because it gives
  259. * formulae for determining the two-byte X character code _from_
  260. * an index into the per_char array. Going the other way, it's
  261. * rather simpler:
  262. *
  263. * The valid character codes have byte1 between min_byte1 and
  264. * max_byte1 inclusive, and byte2 between min_char_or_byte2 and
  265. * max_char_or_byte2 inclusive. This gives a rectangle of size
  266. * (max_byte2-min_byte1+1) by
  267. * (max_char_or_byte2-min_char_or_byte2+1), which is precisely the
  268. * rectangle encoded in the per_char array. Hence, given a
  269. * character code which is valid in the sense that it falls
  270. * somewhere in that rectangle, its index in per_char is given by
  271. * setting
  272. *
  273. * x = byte2 - min_char_or_byte2
  274. * y = byte1 - min_byte1
  275. * index = y * (max_char_or_byte2-min_char_or_byte2+1) + x
  276. *
  277. * If min_byte1 and min_byte2 are both zero, that's a special case
  278. * which can be treated as if min_byte2 was 1 instead, i.e. the
  279. * per_char array just runs from min_char_or_byte2 to
  280. * max_char_or_byte2 inclusive, and byte1 should always be zero.
  281. */
  282. if (byte2 < xfs->min_char_or_byte2 || byte2 > xfs->max_char_or_byte2)
  283. return FALSE;
  284. if (xfs->min_byte1 == 0 && xfs->max_byte1 == 0) {
  285. index = byte2 - xfs->min_char_or_byte2;
  286. } else {
  287. if (byte1 < xfs->min_byte1 || byte1 > xfs->max_byte1)
  288. return FALSE;
  289. index = ((byte2 - xfs->min_char_or_byte2) +
  290. ((byte1 - xfs->min_byte1) *
  291. (xfs->max_char_or_byte2 - xfs->min_char_or_byte2 + 1)));
  292. }
  293. if (!xfs->per_char) /* per_char NULL => everything in range exists */
  294. return &xfs->max_bounds;
  295. return &xfs->per_char[index];
  296. }
  297. static int x11_font_has_glyph(XFontStruct *xfs, int byte1, int byte2)
  298. {
  299. /*
  300. * Not to be confused with x11font_has_glyph, which is a method of
  301. * the x11font 'class' and hence takes a unifont as argument. This
  302. * is the low-level function which grubs about in an actual
  303. * XFontStruct to see if a given glyph exists.
  304. *
  305. * We must do this ourselves rather than letting Xlib's
  306. * XTextExtents16 do the job, because XTextExtents will helpfully
  307. * substitute the font's default_char for any missing glyph and
  308. * not tell us it did so, which precisely won't help us find out
  309. * which glyphs _are_ missing.
  310. */
  311. const XCharStruct *xcs = x11_char_struct(xfs, byte1, byte2);
  312. return xcs && (xcs->ascent + xcs->descent > 0 || xcs->width > 0);
  313. }
  314. static unifont *x11font_create(GtkWidget *widget, const char *name,
  315. int wide, int bold,
  316. int shadowoffset, int shadowalways)
  317. {
  318. struct x11font *xfont;
  319. XFontStruct *xfs;
  320. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  321. Atom charset_registry, charset_encoding, spacing;
  322. unsigned long registry_ret, encoding_ret, spacing_ret;
  323. int pubcs, realcs, sixteen_bit, variable;
  324. int i;
  325. xfs = XLoadQueryFont(disp, name);
  326. if (!xfs)
  327. return NULL;
  328. charset_registry = XInternAtom(disp, "CHARSET_REGISTRY", False);
  329. charset_encoding = XInternAtom(disp, "CHARSET_ENCODING", False);
  330. pubcs = realcs = CS_NONE;
  331. sixteen_bit = FALSE;
  332. variable = TRUE;
  333. if (XGetFontProperty(xfs, charset_registry, &registry_ret) &&
  334. XGetFontProperty(xfs, charset_encoding, &encoding_ret)) {
  335. char *reg, *enc;
  336. reg = XGetAtomName(disp, (Atom)registry_ret);
  337. enc = XGetAtomName(disp, (Atom)encoding_ret);
  338. if (reg && enc) {
  339. char *encoding = dupcat(reg, "-", enc, NULL);
  340. pubcs = realcs = charset_from_xenc(encoding);
  341. /*
  342. * iso10646-1 is the only wide font encoding we
  343. * support. In this case, we expect clients to give us
  344. * UTF-8, which this module must internally convert
  345. * into 16-bit Unicode.
  346. */
  347. if (!strcasecmp(encoding, "iso10646-1")) {
  348. sixteen_bit = TRUE;
  349. pubcs = realcs = CS_UTF8;
  350. }
  351. /*
  352. * Hack for X line-drawing characters: if the primary font
  353. * is encoded as ISO-8859-1, and has valid glyphs in the
  354. * low character positions, it is assumed that those
  355. * glyphs are the VT100 line-drawing character set.
  356. */
  357. if (pubcs == CS_ISO8859_1) {
  358. int ch;
  359. for (ch = 1; ch < 32; ch++)
  360. if (!x11_font_has_glyph(xfs, 0, ch))
  361. break;
  362. if (ch == 32)
  363. realcs = CS_ISO8859_1_X11;
  364. }
  365. sfree(encoding);
  366. }
  367. }
  368. spacing = XInternAtom(disp, "SPACING", False);
  369. if (XGetFontProperty(xfs, spacing, &spacing_ret)) {
  370. char *spc;
  371. spc = XGetAtomName(disp, (Atom)spacing_ret);
  372. if (spc && strchr("CcMm", spc[0]))
  373. variable = FALSE;
  374. }
  375. xfont = snew(struct x11font);
  376. xfont->u.vt = &x11font_vtable;
  377. xfont->u.width = x11_font_width(xfs, sixteen_bit);
  378. xfont->u.ascent = xfs->ascent;
  379. xfont->u.descent = xfs->descent;
  380. xfont->u.height = xfont->u.ascent + xfont->u.descent;
  381. xfont->u.public_charset = pubcs;
  382. xfont->u.want_fallback = TRUE;
  383. #ifdef DRAW_TEXT_GDK
  384. xfont->u.preferred_drawtype = DRAWTYPE_GDK;
  385. #elif defined DRAW_TEXT_CAIRO
  386. xfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
  387. #else
  388. #error No drawtype available at all
  389. #endif
  390. xfont->real_charset = realcs;
  391. xfont->sixteen_bit = sixteen_bit;
  392. xfont->variable = variable;
  393. xfont->wide = wide;
  394. xfont->bold = bold;
  395. xfont->shadowoffset = shadowoffset;
  396. xfont->shadowalways = shadowalways;
  397. for (i = 0; i < lenof(xfont->fonts); i++) {
  398. xfont->fonts[i].xfs = NULL;
  399. xfont->fonts[i].allocated = FALSE;
  400. #ifdef DRAW_TEXT_CAIRO
  401. xfont->fonts[i].glyphcache = NULL;
  402. xfont->fonts[i].nglyphs = 0;
  403. xfont->fonts[i].pixmap = None;
  404. xfont->fonts[i].gc = None;
  405. #endif
  406. }
  407. xfont->fonts[0].xfs = xfs;
  408. xfont->fonts[0].allocated = TRUE;
  409. return (unifont *)xfont;
  410. }
  411. static void x11font_destroy(unifont *font)
  412. {
  413. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  414. struct x11font *xfont = (struct x11font *)font;
  415. int i;
  416. for (i = 0; i < lenof(xfont->fonts); i++) {
  417. if (xfont->fonts[i].xfs)
  418. XFreeFont(disp, xfont->fonts[i].xfs);
  419. #ifdef DRAW_TEXT_CAIRO
  420. if (xfont->fonts[i].gc != None)
  421. XFreeGC(disp, xfont->fonts[i].gc);
  422. if (xfont->fonts[i].pixmap != None)
  423. XFreePixmap(disp, xfont->fonts[i].pixmap);
  424. if (xfont->fonts[i].glyphcache) {
  425. int j;
  426. for (j = 0; j < xfont->fonts[i].nglyphs; j++) {
  427. cairo_surface_destroy(xfont->fonts[i].glyphcache[j].surface);
  428. sfree(xfont->fonts[i].glyphcache[j].bitmap);
  429. }
  430. sfree(xfont->fonts[i].glyphcache);
  431. }
  432. #endif
  433. }
  434. sfree(font);
  435. }
  436. static void x11_alloc_subfont(struct x11font *xfont, int sfid)
  437. {
  438. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  439. char *derived_name = x11_guess_derived_font_name
  440. (xfont->fonts[0].xfs, sfid & 1, !!(sfid & 2));
  441. xfont->fonts[sfid].xfs = XLoadQueryFont(disp, derived_name);
  442. xfont->fonts[sfid].allocated = TRUE;
  443. sfree(derived_name);
  444. /* Note that xfont->fonts[sfid].xfs may still be NULL, if XLQF failed. */
  445. }
  446. static int x11font_has_glyph(unifont *font, wchar_t glyph)
  447. {
  448. struct x11font *xfont = (struct x11font *)font;
  449. if (xfont->sixteen_bit) {
  450. /*
  451. * This X font has 16-bit character indices, which means
  452. * we can directly use our Unicode input value.
  453. */
  454. return x11_font_has_glyph(xfont->fonts[0].xfs,
  455. glyph >> 8, glyph & 0xFF);
  456. } else {
  457. /*
  458. * This X font has 8-bit indices, so we must convert to the
  459. * appropriate character set.
  460. */
  461. char sbstring[2];
  462. int sblen = wc_to_mb(xfont->real_charset, 0, &glyph, 1,
  463. sbstring, 2, "", NULL, NULL);
  464. if (sblen == 0 || !sbstring[0])
  465. return FALSE; /* not even in the charset */
  466. return x11_font_has_glyph(xfont->fonts[0].xfs, 0,
  467. (unsigned char)sbstring[0]);
  468. }
  469. }
  470. #if !GTK_CHECK_VERSION(2,0,0)
  471. #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XWINDOW(d) /* GTK1's name for this */
  472. #elif GTK_CHECK_VERSION(3,0,0)
  473. #define GDK_DRAWABLE_XID(d) GDK_WINDOW_XID(d) /* GTK3's name for this */
  474. #endif
  475. static int x11font_width_16(unifont_drawctx *ctx, x11font_individual *xfi,
  476. const void *vstring, int start, int length)
  477. {
  478. const XChar2b *string = (const XChar2b *)vstring;
  479. return XTextWidth16(xfi->xfs, string+start, length);
  480. }
  481. static int x11font_width_8(unifont_drawctx *ctx, x11font_individual *xfi,
  482. const void *vstring, int start, int length)
  483. {
  484. const char *string = (const char *)vstring;
  485. return XTextWidth(xfi->xfs, string+start, length);
  486. }
  487. #ifdef DRAW_TEXT_GDK
  488. static void x11font_gdk_setup(unifont_drawctx *ctx, x11font_individual *xfi)
  489. {
  490. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  491. XSetFont(disp, GDK_GC_XGC(ctx->u.gdk.gc), xfi->xfs->fid);
  492. }
  493. static void x11font_gdk_draw_16(unifont_drawctx *ctx,
  494. x11font_individual *xfi, int x, int y,
  495. const void *vstring, int start, int length)
  496. {
  497. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  498. const XChar2b *string = (const XChar2b *)vstring;
  499. XDrawString16(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
  500. GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
  501. }
  502. static void x11font_gdk_draw_8(unifont_drawctx *ctx,
  503. x11font_individual *xfi, int x, int y,
  504. const void *vstring, int start, int length)
  505. {
  506. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  507. const char *string = (const char *)vstring;
  508. XDrawString(disp, GDK_DRAWABLE_XID(ctx->u.gdk.target),
  509. GDK_GC_XGC(ctx->u.gdk.gc), x, y, string+start, length);
  510. }
  511. #endif
  512. #ifdef DRAW_TEXT_CAIRO
  513. static void x11font_cairo_setup(unifont_drawctx *ctx, x11font_individual *xfi)
  514. {
  515. if (xfi->pixmap == None) {
  516. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  517. XGCValues gcvals;
  518. GdkWindow *widgetwin = gtk_widget_get_window(ctx->u.cairo.widget);
  519. int widgetscr = GDK_SCREEN_XNUMBER(gdk_window_get_screen(widgetwin));
  520. xfi->pixwidth =
  521. xfi->xfs->max_bounds.rbearing - xfi->xfs->min_bounds.lbearing;
  522. xfi->pixheight =
  523. xfi->xfs->max_bounds.ascent + xfi->xfs->max_bounds.descent;
  524. xfi->pixoriginx = -xfi->xfs->min_bounds.lbearing;
  525. xfi->pixoriginy = xfi->xfs->max_bounds.ascent;
  526. xfi->rowsize = cairo_format_stride_for_width(CAIRO_FORMAT_A1,
  527. xfi->pixwidth);
  528. xfi->allsize = xfi->rowsize * xfi->pixheight;
  529. {
  530. /*
  531. * Test host endianness and use it to set xfi->indexflip,
  532. * which is XORed into our left-shift counts in order to
  533. * implement the CAIRO_FORMAT_A1 specification, in which
  534. * each bitmap byte is oriented LSB-first on little-endian
  535. * platforms and MSB-first on big-endian ones.
  536. *
  537. * This is the same technique Cairo itself uses to test
  538. * endianness, so hopefully it'll work in any situation
  539. * where Cairo is usable at all.
  540. */
  541. static const int endianness_test = 1;
  542. xfi->indexflip = (*((char *) &endianness_test) == 1) ? 0 : 7;
  543. }
  544. xfi->pixmap = XCreatePixmap
  545. (disp,
  546. GDK_DRAWABLE_XID(gtk_widget_get_window(ctx->u.cairo.widget)),
  547. xfi->pixwidth, xfi->pixheight, 1);
  548. gcvals.foreground = WhitePixel(disp, widgetscr);
  549. gcvals.background = BlackPixel(disp, widgetscr);
  550. gcvals.font = xfi->xfs->fid;
  551. xfi->gc = XCreateGC(disp, xfi->pixmap,
  552. GCForeground | GCBackground | GCFont, &gcvals);
  553. }
  554. }
  555. static void x11font_cairo_cache_glyph(x11font_individual *xfi, int glyphindex)
  556. {
  557. XImage *image;
  558. int x, y;
  559. unsigned char *bitmap;
  560. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  561. const XCharStruct *xcs = x11_char_struct(xfi->xfs, glyphindex >> 8,
  562. glyphindex & 0xFF);
  563. bitmap = snewn(xfi->allsize, unsigned char);
  564. memset(bitmap, 0, xfi->allsize);
  565. image = XGetImage(disp, xfi->pixmap, 0, 0,
  566. xfi->pixwidth, xfi->pixheight, AllPlanes, XYPixmap);
  567. for (y = xfi->pixoriginy - xcs->ascent;
  568. y < xfi->pixoriginy + xcs->descent; y++) {
  569. for (x = xfi->pixoriginx + xcs->lbearing;
  570. x < xfi->pixoriginx + xcs->rbearing; x++) {
  571. unsigned long pixel = XGetPixel(image, x, y);
  572. if (pixel) {
  573. int byteindex = y * xfi->rowsize + x/8;
  574. int bitindex = (x & 7) ^ xfi->indexflip;
  575. bitmap[byteindex] |= 1U << bitindex;
  576. }
  577. }
  578. }
  579. XDestroyImage(image);
  580. if (xfi->nglyphs <= glyphindex) {
  581. /* Round up to the next multiple of 256 on the general
  582. * principle that Unicode characters come in contiguous blocks
  583. * often used together */
  584. int old_nglyphs = xfi->nglyphs;
  585. xfi->nglyphs = (glyphindex + 0x100) & ~0xFF;
  586. xfi->glyphcache = sresize(xfi->glyphcache, xfi->nglyphs,
  587. struct cairo_cached_glyph);
  588. while (old_nglyphs < xfi->nglyphs) {
  589. xfi->glyphcache[old_nglyphs].surface = NULL;
  590. xfi->glyphcache[old_nglyphs].bitmap = NULL;
  591. old_nglyphs++;
  592. }
  593. }
  594. xfi->glyphcache[glyphindex].bitmap = bitmap;
  595. xfi->glyphcache[glyphindex].surface = cairo_image_surface_create_for_data
  596. (bitmap, CAIRO_FORMAT_A1, xfi->pixwidth, xfi->pixheight, xfi->rowsize);
  597. }
  598. static void x11font_cairo_draw_glyph(unifont_drawctx *ctx,
  599. x11font_individual *xfi, int x, int y,
  600. int glyphindex)
  601. {
  602. if (xfi->glyphcache[glyphindex].surface) {
  603. cairo_mask_surface(ctx->u.cairo.cr,
  604. xfi->glyphcache[glyphindex].surface,
  605. x - xfi->pixoriginx, y - xfi->pixoriginy);
  606. }
  607. }
  608. static void x11font_cairo_draw_16(unifont_drawctx *ctx,
  609. x11font_individual *xfi, int x, int y,
  610. const void *vstring, int start, int length)
  611. {
  612. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  613. const XChar2b *string = (const XChar2b *)vstring + start;
  614. int i;
  615. for (i = 0; i < length; i++) {
  616. if (x11_font_has_glyph(xfi->xfs, string[i].byte1, string[i].byte2)) {
  617. int glyphindex = (256 * (unsigned char)string[i].byte1 +
  618. (unsigned char)string[i].byte2);
  619. if (glyphindex >= xfi->nglyphs ||
  620. !xfi->glyphcache[glyphindex].surface) {
  621. XDrawImageString16(disp, xfi->pixmap, xfi->gc,
  622. xfi->pixoriginx, xfi->pixoriginy,
  623. string+i, 1);
  624. x11font_cairo_cache_glyph(xfi, glyphindex);
  625. }
  626. x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
  627. x += XTextWidth16(xfi->xfs, string+i, 1);
  628. }
  629. }
  630. }
  631. static void x11font_cairo_draw_8(unifont_drawctx *ctx,
  632. x11font_individual *xfi, int x, int y,
  633. const void *vstring, int start, int length)
  634. {
  635. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  636. const char *string = (const char *)vstring + start;
  637. int i;
  638. for (i = 0; i < length; i++) {
  639. if (x11_font_has_glyph(xfi->xfs, 0, string[i])) {
  640. int glyphindex = (unsigned char)string[i];
  641. if (glyphindex >= xfi->nglyphs ||
  642. !xfi->glyphcache[glyphindex].surface) {
  643. XDrawImageString(disp, xfi->pixmap, xfi->gc,
  644. xfi->pixoriginx, xfi->pixoriginy,
  645. string+i, 1);
  646. x11font_cairo_cache_glyph(xfi, glyphindex);
  647. }
  648. x11font_cairo_draw_glyph(ctx, xfi, x, y, glyphindex);
  649. x += XTextWidth(xfi->xfs, string+i, 1);
  650. }
  651. }
  652. }
  653. #endif /* DRAW_TEXT_CAIRO */
  654. struct x11font_drawfuncs {
  655. int (*width)(unifont_drawctx *ctx, x11font_individual *xfi,
  656. const void *vstring, int start, int length);
  657. void (*setup)(unifont_drawctx *ctx, x11font_individual *xfi);
  658. void (*draw)(unifont_drawctx *ctx, x11font_individual *xfi, int x, int y,
  659. const void *vstring, int start, int length);
  660. };
  661. /*
  662. * This array has two entries per compiled-in drawtype; of each pair,
  663. * the first is for an 8-bit font and the second for 16-bit.
  664. */
  665. static const struct x11font_drawfuncs x11font_drawfuncs[2*DRAWTYPE_NTYPES] = {
  666. #ifdef DRAW_TEXT_GDK
  667. /* gdk, 8-bit */
  668. {
  669. x11font_width_8,
  670. x11font_gdk_setup,
  671. x11font_gdk_draw_8,
  672. },
  673. /* gdk, 16-bit */
  674. {
  675. x11font_width_16,
  676. x11font_gdk_setup,
  677. x11font_gdk_draw_16,
  678. },
  679. #endif
  680. #ifdef DRAW_TEXT_CAIRO
  681. /* cairo, 8-bit */
  682. {
  683. x11font_width_8,
  684. x11font_cairo_setup,
  685. x11font_cairo_draw_8,
  686. },
  687. /* [3] cairo, 16-bit */
  688. {
  689. x11font_width_16,
  690. x11font_cairo_setup,
  691. x11font_cairo_draw_16,
  692. },
  693. #endif
  694. };
  695. static void x11font_really_draw_text(const struct x11font_drawfuncs *dfns,
  696. unifont_drawctx *ctx,
  697. x11font_individual *xfi, int x, int y,
  698. const void *string, int nchars,
  699. int shadowoffset,
  700. int fontvariable, int cellwidth)
  701. {
  702. int start = 0, step, nsteps, centre;
  703. if (fontvariable) {
  704. /*
  705. * In a variable-pitch font, we draw one character at a
  706. * time, and centre it in the character cell.
  707. */
  708. step = 1;
  709. nsteps = nchars;
  710. centre = TRUE;
  711. } else {
  712. /*
  713. * In a fixed-pitch font, we can draw the whole lot in one go.
  714. */
  715. step = nchars;
  716. nsteps = 1;
  717. centre = FALSE;
  718. }
  719. dfns->setup(ctx, xfi);
  720. while (nsteps-- > 0) {
  721. int X = x;
  722. if (centre)
  723. X += (cellwidth - dfns->width(ctx, xfi, string, start, step)) / 2;
  724. dfns->draw(ctx, xfi, X, y, string, start, step);
  725. if (shadowoffset)
  726. dfns->draw(ctx, xfi, X + shadowoffset, y, string, start, step);
  727. x += cellwidth;
  728. start += step;
  729. }
  730. }
  731. static void x11font_draw_text(unifont_drawctx *ctx, unifont *font,
  732. int x, int y, const wchar_t *string, int len,
  733. int wide, int bold, int cellwidth)
  734. {
  735. struct x11font *xfont = (struct x11font *)font;
  736. int sfid;
  737. int shadowoffset = 0;
  738. int mult = (wide ? 2 : 1);
  739. int index = 2 * (int)ctx->type;
  740. wide -= xfont->wide;
  741. bold -= xfont->bold;
  742. /*
  743. * Decide which subfont we're using, and whether we have to
  744. * use shadow bold.
  745. */
  746. if (xfont->shadowalways && bold) {
  747. shadowoffset = xfont->shadowoffset;
  748. bold = 0;
  749. }
  750. sfid = 2 * wide + bold;
  751. if (!xfont->fonts[sfid].allocated)
  752. x11_alloc_subfont(xfont, sfid);
  753. if (bold && !xfont->fonts[sfid].xfs) {
  754. bold = 0;
  755. shadowoffset = xfont->shadowoffset;
  756. sfid = 2 * wide + bold;
  757. if (!xfont->fonts[sfid].allocated)
  758. x11_alloc_subfont(xfont, sfid);
  759. }
  760. if (!xfont->fonts[sfid].xfs)
  761. return; /* we've tried our best, but no luck */
  762. if (xfont->sixteen_bit) {
  763. /*
  764. * This X font has 16-bit character indices, which means
  765. * we can directly use our Unicode input string.
  766. */
  767. XChar2b *xcs;
  768. int i;
  769. xcs = snewn(len, XChar2b);
  770. for (i = 0; i < len; i++) {
  771. xcs[i].byte1 = string[i] >> 8;
  772. xcs[i].byte2 = string[i];
  773. }
  774. x11font_really_draw_text(x11font_drawfuncs + index + 1, ctx,
  775. &xfont->fonts[sfid], x, y,
  776. xcs, len, shadowoffset,
  777. xfont->variable, cellwidth * mult);
  778. sfree(xcs);
  779. } else {
  780. /*
  781. * This X font has 8-bit indices, so we must convert to the
  782. * appropriate character set.
  783. */
  784. char *sbstring = snewn(len+1, char);
  785. int sblen = wc_to_mb(xfont->real_charset, 0, string, len,
  786. sbstring, len+1, ".", NULL, NULL);
  787. x11font_really_draw_text(x11font_drawfuncs + index + 0, ctx,
  788. &xfont->fonts[sfid], x, y,
  789. sbstring, sblen, shadowoffset,
  790. xfont->variable, cellwidth * mult);
  791. sfree(sbstring);
  792. }
  793. }
  794. static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
  795. int x, int y, const wchar_t *string,
  796. int len, int wide, int bold, int cellwidth)
  797. {
  798. /*
  799. * For server-side fonts, there's no sophisticated system for
  800. * combining characters intelligently, so the best we can do is to
  801. * overprint them on each other in the obvious way.
  802. */
  803. int i;
  804. for (i = 0; i < len; i++)
  805. x11font_draw_text(ctx, font, x, y, string+i, 1, wide, bold, cellwidth);
  806. }
  807. static void x11font_enum_fonts(GtkWidget *widget,
  808. fontsel_add_entry callback, void *callback_ctx)
  809. {
  810. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  811. char **fontnames;
  812. char *tmp = NULL;
  813. int nnames, i, max, tmpsize;
  814. max = 32768;
  815. while (1) {
  816. fontnames = XListFonts(disp, "*", max, &nnames);
  817. if (nnames >= max) {
  818. XFreeFontNames(fontnames);
  819. max *= 2;
  820. } else
  821. break;
  822. }
  823. tmpsize = 0;
  824. for (i = 0; i < nnames; i++) {
  825. if (fontnames[i][0] == '-') {
  826. /*
  827. * Dismember an XLFD and convert it into the format
  828. * we'll be using in the font selector.
  829. */
  830. char *components[14];
  831. char *p, *font, *style, *stylekey, *charset;
  832. int j, weightkey, slantkey, setwidthkey;
  833. int thistmpsize, fontsize, flags;
  834. thistmpsize = 4 * strlen(fontnames[i]) + 256;
  835. if (tmpsize < thistmpsize) {
  836. tmpsize = thistmpsize;
  837. tmp = sresize(tmp, tmpsize, char);
  838. }
  839. strcpy(tmp, fontnames[i]);
  840. p = tmp;
  841. for (j = 0; j < 14; j++) {
  842. if (*p)
  843. *p++ = '\0';
  844. components[j] = p;
  845. while (*p && *p != '-')
  846. p++;
  847. }
  848. *p++ = '\0';
  849. /*
  850. * Font name is made up of fields 0 and 1, in reverse
  851. * order with parentheses. (This is what the GTK 1.2 X
  852. * font selector does, and it seems to come out
  853. * looking reasonably sensible.)
  854. */
  855. font = p;
  856. p += 1 + sprintf(p, "%s (%s)", components[1], components[0]);
  857. /*
  858. * Charset is made up of fields 12 and 13.
  859. */
  860. charset = p;
  861. p += 1 + sprintf(p, "%s-%s", components[12], components[13]);
  862. /*
  863. * Style is a mixture of quite a lot of the fields,
  864. * with some strange formatting.
  865. */
  866. style = p;
  867. p += sprintf(p, "%s", components[2][0] ? components[2] :
  868. "regular");
  869. if (!g_ascii_strcasecmp(components[3], "i"))
  870. p += sprintf(p, " italic");
  871. else if (!g_ascii_strcasecmp(components[3], "o"))
  872. p += sprintf(p, " oblique");
  873. else if (!g_ascii_strcasecmp(components[3], "ri"))
  874. p += sprintf(p, " reverse italic");
  875. else if (!g_ascii_strcasecmp(components[3], "ro"))
  876. p += sprintf(p, " reverse oblique");
  877. else if (!g_ascii_strcasecmp(components[3], "ot"))
  878. p += sprintf(p, " other-slant");
  879. if (components[4][0] && g_ascii_strcasecmp(components[4], "normal"))
  880. p += sprintf(p, " %s", components[4]);
  881. if (!g_ascii_strcasecmp(components[10], "m"))
  882. p += sprintf(p, " [M]");
  883. if (!g_ascii_strcasecmp(components[10], "c"))
  884. p += sprintf(p, " [C]");
  885. if (components[5][0])
  886. p += sprintf(p, " %s", components[5]);
  887. /*
  888. * Style key is the same stuff as above, but with a
  889. * couple of transformations done on it to make it
  890. * sort more sensibly.
  891. */
  892. p++;
  893. stylekey = p;
  894. if (!g_ascii_strcasecmp(components[2], "medium") ||
  895. !g_ascii_strcasecmp(components[2], "regular") ||
  896. !g_ascii_strcasecmp(components[2], "normal") ||
  897. !g_ascii_strcasecmp(components[2], "book"))
  898. weightkey = 0;
  899. else if (!g_ascii_strncasecmp(components[2], "demi", 4) ||
  900. !g_ascii_strncasecmp(components[2], "semi", 4))
  901. weightkey = 1;
  902. else
  903. weightkey = 2;
  904. if (!g_ascii_strcasecmp(components[3], "r"))
  905. slantkey = 0;
  906. else if (!g_ascii_strncasecmp(components[3], "r", 1))
  907. slantkey = 2;
  908. else
  909. slantkey = 1;
  910. if (!g_ascii_strcasecmp(components[4], "normal"))
  911. setwidthkey = 0;
  912. else
  913. setwidthkey = 1;
  914. p += sprintf(p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
  915. weightkey,
  916. (int)strlen(components[2]), components[2],
  917. slantkey,
  918. (int)strlen(components[3]), components[3],
  919. setwidthkey,
  920. (int)strlen(components[4]), components[4],
  921. (int)strlen(components[10]), components[10],
  922. (int)strlen(components[5]), components[5]);
  923. assert(p - tmp < thistmpsize);
  924. /*
  925. * Size is in pixels, for our application, so we
  926. * derive it directly from the pixel size field,
  927. * number 6.
  928. */
  929. fontsize = atoi(components[6]);
  930. /*
  931. * Flags: we need to know whether this is a monospaced
  932. * font, which we do by examining the spacing field
  933. * again.
  934. */
  935. flags = FONTFLAG_SERVERSIDE;
  936. if (!strchr("CcMm", components[10][0]))
  937. flags |= FONTFLAG_NONMONOSPACED;
  938. /*
  939. * Not sure why, but sometimes the X server will
  940. * deliver dummy font types in which fontsize comes
  941. * out as zero. Filter those out.
  942. */
  943. if (fontsize)
  944. callback(callback_ctx, fontnames[i], font, charset,
  945. style, stylekey, fontsize, flags, &x11font_vtable);
  946. } else {
  947. /*
  948. * This isn't an XLFD, so it must be an alias.
  949. * Transmit it with mostly null data.
  950. *
  951. * It would be nice to work out if it's monospaced
  952. * here, but at the moment I can't see that being
  953. * anything but computationally hideous. Ah well.
  954. */
  955. callback(callback_ctx, fontnames[i], fontnames[i], NULL,
  956. NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);
  957. }
  958. }
  959. XFreeFontNames(fontnames);
  960. }
  961. static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
  962. int *size, int *flags,
  963. int resolve_aliases)
  964. {
  965. /*
  966. * When given an X11 font name to try to make sense of for a
  967. * font selector, we must attempt to load it (to see if it
  968. * exists), and then canonify it by extracting its FONT
  969. * property, which should give its full XLFD even if what we
  970. * originally had was a wildcard.
  971. *
  972. * However, we must carefully avoid canonifying font
  973. * _aliases_, unless specifically asked to, because the font
  974. * selector treats them as worthwhile in their own right.
  975. */
  976. XFontStruct *xfs;
  977. Display *disp = GDK_DISPLAY_XDISPLAY(gdk_display_get_default());
  978. Atom fontprop, fontprop2;
  979. unsigned long ret;
  980. xfs = XLoadQueryFont(disp, name);
  981. if (!xfs)
  982. return NULL; /* didn't make sense to us, sorry */
  983. fontprop = XInternAtom(disp, "FONT", False);
  984. if (XGetFontProperty(xfs, fontprop, &ret)) {
  985. char *newname = XGetAtomName(disp, (Atom)ret);
  986. if (newname) {
  987. unsigned long fsize = 12;
  988. fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);
  989. if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {
  990. *size = fsize;
  991. XFreeFont(disp, xfs);
  992. if (flags) {
  993. if (name[0] == '-' || resolve_aliases)
  994. *flags = FONTFLAG_SERVERSIDE;
  995. else
  996. *flags = FONTFLAG_SERVERALIAS;
  997. }
  998. return dupstr(name[0] == '-' || resolve_aliases ?
  999. newname : name);
  1000. }
  1001. }
  1002. }
  1003. XFreeFont(disp, xfs);
  1004. return NULL; /* something went wrong */
  1005. }
  1006. static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
  1007. int size)
  1008. {
  1009. return NULL; /* shan't */
  1010. }
  1011. #endif /* NOT_X_WINDOWS */
  1012. #if GTK_CHECK_VERSION(2,0,0)
  1013. /* ----------------------------------------------------------------------
  1014. * Pango font implementation (for GTK 2 only).
  1015. */
  1016. #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
  1017. #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
  1018. #endif
  1019. static int pangofont_has_glyph(unifont *font, wchar_t glyph);
  1020. static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
  1021. int x, int y, const wchar_t *string, int len,
  1022. int wide, int bold, int cellwidth);
  1023. static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1024. int x, int y, const wchar_t *string,
  1025. int len, int wide, int bold,
  1026. int cellwidth);
  1027. static unifont *pangofont_create(GtkWidget *widget, const char *name,
  1028. int wide, int bold,
  1029. int shadowoffset, int shadowalways);
  1030. static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
  1031. int wide, int bold,
  1032. int shadowoffset, int shadowalways);
  1033. static void pangofont_destroy(unifont *font);
  1034. static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
  1035. void *callback_ctx);
  1036. static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
  1037. int *size, int *flags,
  1038. int resolve_aliases);
  1039. static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
  1040. int size);
  1041. struct pangofont {
  1042. struct unifont u;
  1043. /*
  1044. * Pango objects.
  1045. */
  1046. PangoFontDescription *desc;
  1047. PangoFontset *fset;
  1048. /*
  1049. * The containing widget.
  1050. */
  1051. GtkWidget *widget;
  1052. /*
  1053. * Data passed in to unifont_create().
  1054. */
  1055. int bold, shadowoffset, shadowalways;
  1056. /*
  1057. * Cache of character widths, indexed by Unicode code point. In
  1058. * pixels; -1 means we haven't asked Pango about this character
  1059. * before.
  1060. */
  1061. int *widthcache;
  1062. unsigned nwidthcache;
  1063. };
  1064. static const struct unifont_vtable pangofont_vtable = {
  1065. pangofont_create,
  1066. pangofont_create_fallback,
  1067. pangofont_destroy,
  1068. pangofont_has_glyph,
  1069. pangofont_draw_text,
  1070. pangofont_draw_combining,
  1071. pangofont_enum_fonts,
  1072. pangofont_canonify_fontname,
  1073. pangofont_scale_fontname,
  1074. "client",
  1075. };
  1076. /*
  1077. * This function is used to rigorously validate a
  1078. * PangoFontDescription. Later versions of Pango have a nasty
  1079. * habit of accepting _any_ old string as input to
  1080. * pango_font_description_from_string and returning a font
  1081. * description which can actually be used to display text, even if
  1082. * they have to do it by falling back to their most default font.
  1083. * This is doubtless helpful in some situations, but not here,
  1084. * because we need to know if a Pango font string actually _makes
  1085. * sense_ in order to fall back to treating it as an X font name
  1086. * if it doesn't. So we check that the font family is actually one
  1087. * supported by Pango.
  1088. */
  1089. static int pangofont_check_desc_makes_sense(PangoContext *ctx,
  1090. PangoFontDescription *desc)
  1091. {
  1092. #ifndef PANGO_PRE_1POINT6
  1093. PangoFontMap *map;
  1094. #endif
  1095. PangoFontFamily **families;
  1096. int i, nfamilies, matched;
  1097. /*
  1098. * Ask Pango for a list of font families, and iterate through
  1099. * them to see if one of them matches the family in the
  1100. * PangoFontDescription.
  1101. */
  1102. #ifndef PANGO_PRE_1POINT6
  1103. map = pango_context_get_font_map(ctx);
  1104. if (!map)
  1105. return FALSE;
  1106. pango_font_map_list_families(map, &families, &nfamilies);
  1107. #else
  1108. pango_context_list_families(ctx, &families, &nfamilies);
  1109. #endif
  1110. matched = FALSE;
  1111. for (i = 0; i < nfamilies; i++) {
  1112. if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]),
  1113. pango_font_description_get_family(desc))) {
  1114. matched = TRUE;
  1115. break;
  1116. }
  1117. }
  1118. g_free(families);
  1119. return matched;
  1120. }
  1121. static unifont *pangofont_create_internal(GtkWidget *widget,
  1122. PangoContext *ctx,
  1123. PangoFontDescription *desc,
  1124. int wide, int bold,
  1125. int shadowoffset, int shadowalways)
  1126. {
  1127. struct pangofont *pfont;
  1128. #ifndef PANGO_PRE_1POINT6
  1129. PangoFontMap *map;
  1130. #endif
  1131. PangoFontset *fset;
  1132. PangoFontMetrics *metrics;
  1133. #ifndef PANGO_PRE_1POINT6
  1134. map = pango_context_get_font_map(ctx);
  1135. if (!map) {
  1136. pango_font_description_free(desc);
  1137. return NULL;
  1138. }
  1139. fset = pango_font_map_load_fontset(map, ctx, desc,
  1140. pango_context_get_language(ctx));
  1141. #else
  1142. fset = pango_context_load_fontset(ctx, desc,
  1143. pango_context_get_language(ctx));
  1144. #endif
  1145. if (!fset) {
  1146. pango_font_description_free(desc);
  1147. return NULL;
  1148. }
  1149. metrics = pango_fontset_get_metrics(fset);
  1150. if (!metrics ||
  1151. pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
  1152. pango_font_description_free(desc);
  1153. g_object_unref(fset);
  1154. return NULL;
  1155. }
  1156. pfont = snew(struct pangofont);
  1157. pfont->u.vt = &pangofont_vtable;
  1158. pfont->u.width =
  1159. PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));
  1160. pfont->u.ascent = PANGO_PIXELS(pango_font_metrics_get_ascent(metrics));
  1161. pfont->u.descent = PANGO_PIXELS(pango_font_metrics_get_descent(metrics));
  1162. pfont->u.height = pfont->u.ascent + pfont->u.descent;
  1163. pfont->u.want_fallback = FALSE;
  1164. #ifdef DRAW_TEXT_CAIRO
  1165. pfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
  1166. #elif defined DRAW_TEXT_GDK
  1167. pfont->u.preferred_drawtype = DRAWTYPE_GDK;
  1168. #else
  1169. #error No drawtype available at all
  1170. #endif
  1171. /* The Pango API is hardwired to UTF-8 */
  1172. pfont->u.public_charset = CS_UTF8;
  1173. pfont->desc = desc;
  1174. pfont->fset = fset;
  1175. pfont->widget = widget;
  1176. pfont->bold = bold;
  1177. pfont->shadowoffset = shadowoffset;
  1178. pfont->shadowalways = shadowalways;
  1179. pfont->widthcache = NULL;
  1180. pfont->nwidthcache = 0;
  1181. pango_font_metrics_unref(metrics);
  1182. return (unifont *)pfont;
  1183. }
  1184. static unifont *pangofont_create(GtkWidget *widget, const char *name,
  1185. int wide, int bold,
  1186. int shadowoffset, int shadowalways)
  1187. {
  1188. PangoContext *ctx;
  1189. PangoFontDescription *desc;
  1190. desc = pango_font_description_from_string(name);
  1191. if (!desc)
  1192. return NULL;
  1193. ctx = gtk_widget_get_pango_context(widget);
  1194. if (!ctx) {
  1195. pango_font_description_free(desc);
  1196. return NULL;
  1197. }
  1198. if (!pangofont_check_desc_makes_sense(ctx, desc)) {
  1199. pango_font_description_free(desc);
  1200. return NULL;
  1201. }
  1202. return pangofont_create_internal(widget, ctx, desc, wide, bold,
  1203. shadowoffset, shadowalways);
  1204. }
  1205. static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
  1206. int wide, int bold,
  1207. int shadowoffset, int shadowalways)
  1208. {
  1209. PangoContext *ctx;
  1210. PangoFontDescription *desc;
  1211. desc = pango_font_description_from_string("Monospace");
  1212. if (!desc)
  1213. return NULL;
  1214. ctx = gtk_widget_get_pango_context(widget);
  1215. if (!ctx) {
  1216. pango_font_description_free(desc);
  1217. return NULL;
  1218. }
  1219. pango_font_description_set_absolute_size(desc, height * PANGO_SCALE);
  1220. return pangofont_create_internal(widget, ctx, desc, wide, bold,
  1221. shadowoffset, shadowalways);
  1222. }
  1223. static void pangofont_destroy(unifont *font)
  1224. {
  1225. struct pangofont *pfont = (struct pangofont *)font;
  1226. pango_font_description_free(pfont->desc);
  1227. sfree(pfont->widthcache);
  1228. g_object_unref(pfont->fset);
  1229. sfree(font);
  1230. }
  1231. static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont,
  1232. wchar_t uchr, const char *utfchr, int utflen)
  1233. {
  1234. /*
  1235. * Here we check whether a character has the same width as the
  1236. * character cell it'll be drawn in. Because profiling showed that
  1237. * asking Pango for text sizes was a huge bottleneck when we were
  1238. * calling it every time we needed to know this, we instead call
  1239. * it only on characters we don't already know about, and cache
  1240. * the results.
  1241. */
  1242. if ((unsigned)uchr >= pfont->nwidthcache) {
  1243. unsigned newsize = ((int)uchr + 0x100) & ~0xFF;
  1244. pfont->widthcache = sresize(pfont->widthcache, newsize, int);
  1245. while (pfont->nwidthcache < newsize)
  1246. pfont->widthcache[pfont->nwidthcache++] = -1;
  1247. }
  1248. if (pfont->widthcache[uchr] < 0) {
  1249. PangoRectangle rect;
  1250. pango_layout_set_text(layout, utfchr, utflen);
  1251. pango_layout_get_extents(layout, NULL, &rect);
  1252. pfont->widthcache[uchr] = rect.width;
  1253. }
  1254. return pfont->widthcache[uchr];
  1255. }
  1256. static int pangofont_has_glyph(unifont *font, wchar_t glyph)
  1257. {
  1258. /* Pango implements font fallback, so assume it has everything */
  1259. return TRUE;
  1260. }
  1261. #ifdef DRAW_TEXT_GDK
  1262. static void pango_gdk_draw_layout(unifont_drawctx *ctx,
  1263. gint x, gint y, PangoLayout *layout)
  1264. {
  1265. gdk_draw_layout(ctx->u.gdk.target, ctx->u.gdk.gc, x, y, layout);
  1266. }
  1267. #endif
  1268. #ifdef DRAW_TEXT_CAIRO
  1269. static void pango_cairo_draw_layout(unifont_drawctx *ctx,
  1270. gint x, gint y, PangoLayout *layout)
  1271. {
  1272. cairo_move_to(ctx->u.cairo.cr, x, y);
  1273. pango_cairo_show_layout(ctx->u.cairo.cr, layout);
  1274. }
  1275. #endif
  1276. static void pangofont_draw_internal(unifont_drawctx *ctx, unifont *font,
  1277. int x, int y, const wchar_t *string,
  1278. int len, int wide, int bold, int cellwidth,
  1279. int combining)
  1280. {
  1281. struct pangofont *pfont = (struct pangofont *)font;
  1282. PangoLayout *layout;
  1283. PangoRectangle rect;
  1284. char *utfstring, *utfptr;
  1285. int utflen;
  1286. int shadowbold = FALSE;
  1287. void (*draw_layout)(unifont_drawctx *ctx,
  1288. gint x, gint y, PangoLayout *layout) = NULL;
  1289. #ifdef DRAW_TEXT_GDK
  1290. if (ctx->type == DRAWTYPE_GDK) {
  1291. draw_layout = pango_gdk_draw_layout;
  1292. }
  1293. #endif
  1294. #ifdef DRAW_TEXT_CAIRO
  1295. if (ctx->type == DRAWTYPE_CAIRO) {
  1296. draw_layout = pango_cairo_draw_layout;
  1297. }
  1298. #endif
  1299. if (wide)
  1300. cellwidth *= 2;
  1301. y -= pfont->u.ascent;
  1302. layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));
  1303. pango_layout_set_font_description(layout, pfont->desc);
  1304. if (bold > pfont->bold) {
  1305. if (pfont->shadowalways)
  1306. shadowbold = TRUE;
  1307. else {
  1308. PangoFontDescription *desc2 =
  1309. pango_font_description_copy_static(pfont->desc);
  1310. pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);
  1311. pango_layout_set_font_description(layout, desc2);
  1312. }
  1313. }
  1314. /*
  1315. * Pango always expects UTF-8, so convert the input wide character
  1316. * string to UTF-8.
  1317. */
  1318. utfstring = snewn(len*6+1, char); /* UTF-8 has max 6 bytes/char */
  1319. utflen = wc_to_mb(CS_UTF8, 0, string, len,
  1320. utfstring, len*6+1, ".", NULL, NULL);
  1321. utfptr = utfstring;
  1322. while (utflen > 0) {
  1323. int clen, n;
  1324. int desired = cellwidth * PANGO_SCALE;
  1325. /*
  1326. * We want to display every character from this string in
  1327. * the centre of its own character cell. In the worst case,
  1328. * this requires a separate text-drawing call for each
  1329. * character; but in the common case where the font is
  1330. * properly fixed-width, we can draw many characters in one
  1331. * go which is much faster.
  1332. *
  1333. * This still isn't really ideal. If you look at what
  1334. * happens in the X protocol as a result of all of this, you
  1335. * find - naturally enough - that each call to
  1336. * gdk_draw_layout() generates a separate set of X RENDER
  1337. * operations involving creating a picture, setting a clip
  1338. * rectangle, doing some drawing and undoing the whole lot.
  1339. * In an ideal world, we should _always_ be able to turn the
  1340. * contents of this loop into a single RenderCompositeGlyphs
  1341. * operation which internally specifies inter-character
  1342. * deltas to get the spacing right, which would give us full
  1343. * speed _even_ in the worst case of a non-fixed-width font.
  1344. * However, Pango's architecture and documentation are so
  1345. * unhelpful that I have no idea how if at all to persuade
  1346. * them to do that.
  1347. */
  1348. if (combining) {
  1349. /*
  1350. * For a character with combining stuff, we just dump the
  1351. * whole lot in one go, and expect it to take up just one
  1352. * character cell.
  1353. */
  1354. clen = utflen;
  1355. n = 1;
  1356. } else {
  1357. /*
  1358. * Start by extracting a single UTF-8 character from the
  1359. * string.
  1360. */
  1361. clen = 1;
  1362. while (clen < utflen &&
  1363. (unsigned char)utfptr[clen] >= 0x80 &&
  1364. (unsigned char)utfptr[clen] < 0xC0)
  1365. clen++;
  1366. n = 1;
  1367. if (is_rtl(string[0]) ||
  1368. pangofont_char_width(layout, pfont, string[n-1],
  1369. utfptr, clen) != desired) {
  1370. /*
  1371. * If this character is a right-to-left one, or has an
  1372. * unusual width, then we must display it on its own.
  1373. */
  1374. } else {
  1375. /*
  1376. * Try to amalgamate a contiguous string of characters
  1377. * with the expected sensible width, for the common case
  1378. * in which we're using a monospaced font and everything
  1379. * works as expected.
  1380. */
  1381. while (clen < utflen) {
  1382. int oldclen = clen;
  1383. clen++; /* skip UTF-8 introducer byte */
  1384. while (clen < utflen &&
  1385. (unsigned char)utfptr[clen] >= 0x80 &&
  1386. (unsigned char)utfptr[clen] < 0xC0)
  1387. clen++;
  1388. n++;
  1389. if (is_rtl(string[n-1]) ||
  1390. pangofont_char_width(layout, pfont,
  1391. string[n-1], utfptr + oldclen,
  1392. clen - oldclen) != desired) {
  1393. clen = oldclen;
  1394. n--;
  1395. break;
  1396. }
  1397. }
  1398. }
  1399. }
  1400. pango_layout_set_text(layout, utfptr, clen);
  1401. pango_layout_get_pixel_extents(layout, NULL, &rect);
  1402. draw_layout(ctx,
  1403. x + (n*cellwidth - rect.width)/2,
  1404. y + (pfont->u.height - rect.height)/2, layout);
  1405. if (shadowbold)
  1406. draw_layout(ctx,
  1407. x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,
  1408. y + (pfont->u.height - rect.height)/2, layout);
  1409. utflen -= clen;
  1410. utfptr += clen;
  1411. string += n;
  1412. x += n * cellwidth;
  1413. }
  1414. sfree(utfstring);
  1415. g_object_unref(layout);
  1416. }
  1417. static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
  1418. int x, int y, const wchar_t *string, int len,
  1419. int wide, int bold, int cellwidth)
  1420. {
  1421. pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
  1422. cellwidth, FALSE);
  1423. }
  1424. static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1425. int x, int y, const wchar_t *string,
  1426. int len, int wide, int bold,
  1427. int cellwidth)
  1428. {
  1429. wchar_t *tmpstring = NULL;
  1430. if (mk_wcwidth(string[0]) == 0) {
  1431. /*
  1432. * If we've been told to draw a sequence of _only_ combining
  1433. * characters, prefix a space so that they have something to
  1434. * combine with.
  1435. */
  1436. tmpstring = snewn(len+1, wchar_t);
  1437. memcpy(tmpstring+1, string, len * sizeof(wchar_t));
  1438. tmpstring[0] = L' ';
  1439. string = tmpstring;
  1440. len++;
  1441. }
  1442. pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
  1443. cellwidth, TRUE);
  1444. sfree(tmpstring);
  1445. }
  1446. /*
  1447. * Dummy size value to be used when converting a
  1448. * PangoFontDescription of a scalable font to a string for
  1449. * internal use.
  1450. */
  1451. #define PANGO_DUMMY_SIZE 12
  1452. static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
  1453. void *callback_ctx)
  1454. {
  1455. PangoContext *ctx;
  1456. #ifndef PANGO_PRE_1POINT6
  1457. PangoFontMap *map;
  1458. #endif
  1459. PangoFontFamily **families;
  1460. int i, nfamilies;
  1461. ctx = gtk_widget_get_pango_context(widget);
  1462. if (!ctx)
  1463. return;
  1464. /*
  1465. * Ask Pango for a list of font families, and iterate through
  1466. * them.
  1467. */
  1468. #ifndef PANGO_PRE_1POINT6
  1469. map = pango_context_get_font_map(ctx);
  1470. if (!map)
  1471. return;
  1472. pango_font_map_list_families(map, &families, &nfamilies);
  1473. #else
  1474. pango_context_list_families(ctx, &families, &nfamilies);
  1475. #endif
  1476. for (i = 0; i < nfamilies; i++) {
  1477. PangoFontFamily *family = families[i];
  1478. const char *familyname;
  1479. int flags;
  1480. PangoFontFace **faces;
  1481. int j, nfaces;
  1482. /*
  1483. * Set up our flags for this font family, and get the name
  1484. * string.
  1485. */
  1486. flags = FONTFLAG_CLIENTSIDE;
  1487. #ifndef PANGO_PRE_1POINT4
  1488. /*
  1489. * In very early versions of Pango, we can't tell
  1490. * monospaced fonts from non-monospaced.
  1491. */
  1492. if (!pango_font_family_is_monospace(family))
  1493. flags |= FONTFLAG_NONMONOSPACED;
  1494. #endif
  1495. familyname = pango_font_family_get_name(family);
  1496. /*
  1497. * Go through the available font faces in this family.
  1498. */
  1499. pango_font_family_list_faces(family, &faces, &nfaces);
  1500. for (j = 0; j < nfaces; j++) {
  1501. PangoFontFace *face = faces[j];
  1502. PangoFontDescription *desc;
  1503. const char *facename;
  1504. int *sizes;
  1505. int k, nsizes, dummysize;
  1506. /*
  1507. * Get the face name string.
  1508. */
  1509. facename = pango_font_face_get_face_name(face);
  1510. /*
  1511. * Set up a font description with what we've got so
  1512. * far. We'll fill in the size field manually and then
  1513. * call pango_font_description_to_string() to give the
  1514. * full real name of the specific font.
  1515. */
  1516. desc = pango_font_face_describe(face);
  1517. /*
  1518. * See if this font has a list of specific sizes.
  1519. */
  1520. #ifndef PANGO_PRE_1POINT4
  1521. pango_font_face_list_sizes(face, &sizes, &nsizes);
  1522. #else
  1523. /*
  1524. * In early versions of Pango, that call wasn't
  1525. * supported; we just have to assume everything is
  1526. * scalable.
  1527. */
  1528. sizes = NULL;
  1529. #endif
  1530. if (!sizes) {
  1531. /*
  1532. * Write a single entry with a dummy size.
  1533. */
  1534. dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;
  1535. sizes = &dummysize;
  1536. nsizes = 1;
  1537. }
  1538. /*
  1539. * If so, go through them one by one.
  1540. */
  1541. for (k = 0; k < nsizes; k++) {
  1542. char *fullname;
  1543. char stylekey[128];
  1544. pango_font_description_set_size(desc, sizes[k]);
  1545. fullname = pango_font_description_to_string(desc);
  1546. /*
  1547. * Construct the sorting key for font styles.
  1548. */
  1549. {
  1550. char *p = stylekey;
  1551. int n;
  1552. n = pango_font_description_get_weight(desc);
  1553. /* Weight: normal, then lighter, then bolder */
  1554. if (n <= PANGO_WEIGHT_NORMAL)
  1555. n = PANGO_WEIGHT_NORMAL - n;
  1556. p += sprintf(p, "%4d", n);
  1557. n = pango_font_description_get_style(desc);
  1558. p += sprintf(p, " %2d", n);
  1559. n = pango_font_description_get_stretch(desc);
  1560. /* Stretch: closer to normal sorts earlier */
  1561. n = 2 * abs(PANGO_STRETCH_NORMAL - n) +
  1562. (n < PANGO_STRETCH_NORMAL);
  1563. p += sprintf(p, " %2d", n);
  1564. n = pango_font_description_get_variant(desc);
  1565. p += sprintf(p, " %2d", n);
  1566. }
  1567. /*
  1568. * Got everything. Hand off to the callback.
  1569. * (The charset string is NULL, because only
  1570. * server-side X fonts use it.)
  1571. */
  1572. callback(callback_ctx, fullname, familyname, NULL, facename,
  1573. stylekey,
  1574. (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
  1575. flags, &pangofont_vtable);
  1576. g_free(fullname);
  1577. }
  1578. if (sizes != &dummysize)
  1579. g_free(sizes);
  1580. pango_font_description_free(desc);
  1581. }
  1582. g_free(faces);
  1583. }
  1584. g_free(families);
  1585. }
  1586. static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
  1587. int *size, int *flags,
  1588. int resolve_aliases)
  1589. {
  1590. /*
  1591. * When given a Pango font name to try to make sense of for a
  1592. * font selector, we must normalise it to PANGO_DUMMY_SIZE and
  1593. * extract its original size (in pixels) into the `size' field.
  1594. */
  1595. PangoContext *ctx;
  1596. #ifndef PANGO_PRE_1POINT6
  1597. PangoFontMap *map;
  1598. #endif
  1599. PangoFontDescription *desc;
  1600. PangoFontset *fset;
  1601. PangoFontMetrics *metrics;
  1602. char *newname, *retname;
  1603. desc = pango_font_description_from_string(name);
  1604. if (!desc)
  1605. return NULL;
  1606. ctx = gtk_widget_get_pango_context(widget);
  1607. if (!ctx) {
  1608. pango_font_description_free(desc);
  1609. return NULL;
  1610. }
  1611. if (!pangofont_check_desc_makes_sense(ctx, desc)) {
  1612. pango_font_description_free(desc);
  1613. return NULL;
  1614. }
  1615. #ifndef PANGO_PRE_1POINT6
  1616. map = pango_context_get_font_map(ctx);
  1617. if (!map) {
  1618. pango_font_description_free(desc);
  1619. return NULL;
  1620. }
  1621. fset = pango_font_map_load_fontset(map, ctx, desc,
  1622. pango_context_get_language(ctx));
  1623. #else
  1624. fset = pango_context_load_fontset(ctx, desc,
  1625. pango_context_get_language(ctx));
  1626. #endif
  1627. if (!fset) {
  1628. pango_font_description_free(desc);
  1629. return NULL;
  1630. }
  1631. metrics = pango_fontset_get_metrics(fset);
  1632. if (!metrics ||
  1633. pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
  1634. pango_font_description_free(desc);
  1635. g_object_unref(fset);
  1636. return NULL;
  1637. }
  1638. *size = PANGO_PIXELS(pango_font_description_get_size(desc));
  1639. *flags = FONTFLAG_CLIENTSIDE;
  1640. pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);
  1641. newname = pango_font_description_to_string(desc);
  1642. retname = dupstr(newname);
  1643. g_free(newname);
  1644. pango_font_metrics_unref(metrics);
  1645. pango_font_description_free(desc);
  1646. g_object_unref(fset);
  1647. return retname;
  1648. }
  1649. static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
  1650. int size)
  1651. {
  1652. PangoFontDescription *desc;
  1653. char *newname, *retname;
  1654. desc = pango_font_description_from_string(name);
  1655. if (!desc)
  1656. return NULL;
  1657. pango_font_description_set_size(desc, size * PANGO_SCALE);
  1658. newname = pango_font_description_to_string(desc);
  1659. retname = dupstr(newname);
  1660. g_free(newname);
  1661. pango_font_description_free(desc);
  1662. return retname;
  1663. }
  1664. #endif /* GTK_CHECK_VERSION(2,0,0) */
  1665. /* ----------------------------------------------------------------------
  1666. * Outermost functions which do the vtable dispatch.
  1667. */
  1668. /*
  1669. * Complete list of font-type subclasses. Listed in preference
  1670. * order for unifont_create(). (That is, in the extremely unlikely
  1671. * event that the same font name is valid as both a Pango and an
  1672. * X11 font, it will be interpreted as the former in the absence
  1673. * of an explicit type-disambiguating prefix.)
  1674. *
  1675. * The 'multifont' subclass is omitted here, as discussed above.
  1676. */
  1677. static const struct unifont_vtable *unifont_types[] = {
  1678. #if GTK_CHECK_VERSION(2,0,0)
  1679. &pangofont_vtable,
  1680. #endif
  1681. #ifndef NOT_X_WINDOWS
  1682. &x11font_vtable,
  1683. #endif
  1684. };
  1685. /*
  1686. * Function which takes a font name and processes the optional
  1687. * scheme prefix. Returns the tail of the font name suitable for
  1688. * passing to individual font scheme functions, and also provides
  1689. * a subrange of the unifont_types[] array above.
  1690. *
  1691. * The return values `start' and `end' denote a half-open interval
  1692. * in unifont_types[]; that is, the correct way to iterate over
  1693. * them is
  1694. *
  1695. * for (i = start; i < end; i++) {...}
  1696. */
  1697. static const char *unifont_do_prefix(const char *name, int *start, int *end)
  1698. {
  1699. int colonpos = strcspn(name, ":");
  1700. int i;
  1701. if (name[colonpos]) {
  1702. /*
  1703. * There's a colon prefix on the font name. Use it to work
  1704. * out which subclass to use.
  1705. */
  1706. for (i = 0; i < lenof(unifont_types); i++) {
  1707. if (strlen(unifont_types[i]->prefix) == colonpos &&
  1708. !strncmp(unifont_types[i]->prefix, name, colonpos)) {
  1709. *start = i;
  1710. *end = i+1;
  1711. return name + colonpos + 1;
  1712. }
  1713. }
  1714. /*
  1715. * None matched, so return an empty scheme list to prevent
  1716. * any scheme from being called at all.
  1717. */
  1718. *start = *end = 0;
  1719. return name + colonpos + 1;
  1720. } else {
  1721. /*
  1722. * No colon prefix, so just use all the subclasses.
  1723. */
  1724. *start = 0;
  1725. *end = lenof(unifont_types);
  1726. return name;
  1727. }
  1728. }
  1729. unifont *unifont_create(GtkWidget *widget, const char *name, int wide,
  1730. int bold, int shadowoffset, int shadowalways)
  1731. {
  1732. int i, start, end;
  1733. name = unifont_do_prefix(name, &start, &end);
  1734. for (i = start; i < end; i++) {
  1735. unifont *ret = unifont_types[i]->create(widget, name, wide, bold,
  1736. shadowoffset, shadowalways);
  1737. if (ret)
  1738. return ret;
  1739. }
  1740. return NULL; /* font not found in any scheme */
  1741. }
  1742. void unifont_destroy(unifont *font)
  1743. {
  1744. font->vt->destroy(font);
  1745. }
  1746. void unifont_draw_text(unifont_drawctx *ctx, unifont *font,
  1747. int x, int y, const wchar_t *string, int len,
  1748. int wide, int bold, int cellwidth)
  1749. {
  1750. font->vt->draw_text(ctx, font, x, y, string, len, wide, bold, cellwidth);
  1751. }
  1752. void unifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1753. int x, int y, const wchar_t *string, int len,
  1754. int wide, int bold, int cellwidth)
  1755. {
  1756. font->vt->draw_combining(ctx, font, x, y, string, len, wide, bold,
  1757. cellwidth);
  1758. }
  1759. /* ----------------------------------------------------------------------
  1760. * Multiple-font wrapper. This is a type of unifont which encapsulates
  1761. * up to two other unifonts, permitting missing glyphs in the main
  1762. * font to be filled in by a fallback font.
  1763. *
  1764. * This is a type of unifont just like the previous two, but it has a
  1765. * separate constructor which is manually called by the client, so it
  1766. * doesn't appear in the list of available font types enumerated by
  1767. * unifont_create. This means it's not used by unifontsel either, so
  1768. * it doesn't need to support any methods except draw_text and
  1769. * destroy.
  1770. */
  1771. static void multifont_draw_text(unifont_drawctx *ctx, unifont *font,
  1772. int x, int y, const wchar_t *string, int len,
  1773. int wide, int bold, int cellwidth);
  1774. static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1775. int x, int y, const wchar_t *string,
  1776. int len, int wide, int bold,
  1777. int cellwidth);
  1778. static void multifont_destroy(unifont *font);
  1779. struct multifont {
  1780. struct unifont u;
  1781. unifont *main;
  1782. unifont *fallback;
  1783. };
  1784. static const struct unifont_vtable multifont_vtable = {
  1785. NULL, /* creation is done specially */
  1786. NULL,
  1787. multifont_destroy,
  1788. NULL,
  1789. multifont_draw_text,
  1790. multifont_draw_combining,
  1791. NULL,
  1792. NULL,
  1793. NULL,
  1794. "client",
  1795. };
  1796. unifont *multifont_create(GtkWidget *widget, const char *name,
  1797. int wide, int bold,
  1798. int shadowoffset, int shadowalways)
  1799. {
  1800. int i;
  1801. unifont *font, *fallback;
  1802. struct multifont *mfont;
  1803. font = unifont_create(widget, name, wide, bold,
  1804. shadowoffset, shadowalways);
  1805. if (!font)
  1806. return NULL;
  1807. fallback = NULL;
  1808. if (font->want_fallback) {
  1809. for (i = 0; i < lenof(unifont_types); i++) {
  1810. if (unifont_types[i]->create_fallback) {
  1811. fallback = unifont_types[i]->create_fallback
  1812. (widget, font->height, wide, bold,
  1813. shadowoffset, shadowalways);
  1814. if (fallback)
  1815. break;
  1816. }
  1817. }
  1818. }
  1819. /*
  1820. * Construct our multifont. Public members are all copied from the
  1821. * primary font we're wrapping.
  1822. */
  1823. mfont = snew(struct multifont);
  1824. mfont->u.vt = &multifont_vtable;
  1825. mfont->u.width = font->width;
  1826. mfont->u.ascent = font->ascent;
  1827. mfont->u.descent = font->descent;
  1828. mfont->u.height = font->height;
  1829. mfont->u.public_charset = font->public_charset;
  1830. mfont->u.want_fallback = FALSE; /* shouldn't be needed, but just in case */
  1831. mfont->u.preferred_drawtype = font->preferred_drawtype;
  1832. mfont->main = font;
  1833. mfont->fallback = fallback;
  1834. return (unifont *)mfont;
  1835. }
  1836. static void multifont_destroy(unifont *font)
  1837. {
  1838. struct multifont *mfont = (struct multifont *)font;
  1839. unifont_destroy(mfont->main);
  1840. if (mfont->fallback)
  1841. unifont_destroy(mfont->fallback);
  1842. sfree(font);
  1843. }
  1844. typedef void (*unifont_draw_func_t)(unifont_drawctx *ctx, unifont *font,
  1845. int x, int y, const wchar_t *string,
  1846. int len, int wide, int bold,
  1847. int cellwidth);
  1848. static void multifont_draw_main(unifont_drawctx *ctx, unifont *font, int x,
  1849. int y, const wchar_t *string, int len,
  1850. int wide, int bold, int cellwidth,
  1851. int cellinc, unifont_draw_func_t draw)
  1852. {
  1853. struct multifont *mfont = (struct multifont *)font;
  1854. unifont *f;
  1855. int ok, i;
  1856. while (len > 0) {
  1857. /*
  1858. * Find a maximal sequence of characters which are, or are
  1859. * not, supported by our main font.
  1860. */
  1861. ok = mfont->main->vt->has_glyph(mfont->main, string[0]);
  1862. for (i = 1;
  1863. i < len &&
  1864. !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok;
  1865. i++);
  1866. /*
  1867. * Now display it.
  1868. */
  1869. f = ok ? mfont->main : mfont->fallback;
  1870. if (f)
  1871. draw(ctx, f, x, y, string, i, wide, bold, cellwidth);
  1872. string += i;
  1873. len -= i;
  1874. x += i * cellinc;
  1875. }
  1876. }
  1877. static void multifont_draw_text(unifont_drawctx *ctx, unifont *font, int x,
  1878. int y, const wchar_t *string, int len,
  1879. int wide, int bold, int cellwidth)
  1880. {
  1881. multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
  1882. cellwidth, cellwidth, unifont_draw_text);
  1883. }
  1884. static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1885. int x, int y, const wchar_t *string,
  1886. int len, int wide, int bold,
  1887. int cellwidth)
  1888. {
  1889. multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
  1890. cellwidth, 0, unifont_draw_combining);
  1891. }
  1892. #if GTK_CHECK_VERSION(2,0,0)
  1893. /* ----------------------------------------------------------------------
  1894. * Implementation of a unified font selector. Used on GTK 2 only;
  1895. * for GTK 1 we still use the standard font selector.
  1896. */
  1897. typedef struct fontinfo fontinfo;
  1898. typedef struct unifontsel_internal {
  1899. /* This must be the structure's first element, for cross-casting */
  1900. unifontsel u;
  1901. GtkListStore *family_model, *style_model, *size_model;
  1902. GtkWidget *family_list, *style_list, *size_entry, *size_list;
  1903. GtkWidget *filter_buttons[4];
  1904. int n_filter_buttons;
  1905. GtkWidget *preview_area;
  1906. #ifndef NO_BACKING_PIXMAPS
  1907. GdkPixmap *preview_pixmap;
  1908. #endif
  1909. int preview_width, preview_height;
  1910. GdkColor preview_fg, preview_bg;
  1911. int filter_flags;
  1912. tree234 *fonts_by_realname, *fonts_by_selorder;
  1913. fontinfo *selected;
  1914. int selsize, intendedsize;
  1915. int inhibit_response; /* inhibit callbacks when we change GUI controls */
  1916. } unifontsel_internal;
  1917. /*
  1918. * The structure held in the tree234s. All the string members are
  1919. * part of the same allocated area, so don't need freeing
  1920. * separately.
  1921. */
  1922. struct fontinfo {
  1923. char *realname;
  1924. char *family, *charset, *style, *stylekey;
  1925. int size, flags;
  1926. /*
  1927. * Fallback sorting key, to permit multiple identical entries
  1928. * to exist in the selorder tree.
  1929. */
  1930. int index;
  1931. /*
  1932. * Indices mapping fontinfo structures to indices in the list
  1933. * boxes. sizeindex is irrelevant if the font is scalable
  1934. * (size==0).
  1935. */
  1936. int familyindex, styleindex, sizeindex;
  1937. /*
  1938. * The class of font.
  1939. */
  1940. const struct unifont_vtable *fontclass;
  1941. };
  1942. struct fontinfo_realname_find {
  1943. const char *realname;
  1944. int flags;
  1945. };
  1946. static int strnullcasecmp(const char *a, const char *b)
  1947. {
  1948. int i;
  1949. /*
  1950. * If exactly one of the inputs is NULL, it compares before
  1951. * the other one.
  1952. */
  1953. if ((i = (!b) - (!a)) != 0)
  1954. return i;
  1955. /*
  1956. * NULL compares equal.
  1957. */
  1958. if (!a)
  1959. return 0;
  1960. /*
  1961. * Otherwise, ordinary strcasecmp.
  1962. */
  1963. return g_ascii_strcasecmp(a, b);
  1964. }
  1965. static int fontinfo_realname_compare(void *av, void *bv)
  1966. {
  1967. fontinfo *a = (fontinfo *)av;
  1968. fontinfo *b = (fontinfo *)bv;
  1969. int i;
  1970. if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
  1971. return i;
  1972. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  1973. return ((a->flags & FONTFLAG_SORT_MASK) <
  1974. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  1975. return 0;
  1976. }
  1977. static int fontinfo_realname_find(void *av, void *bv)
  1978. {
  1979. struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;
  1980. fontinfo *b = (fontinfo *)bv;
  1981. int i;
  1982. if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
  1983. return i;
  1984. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  1985. return ((a->flags & FONTFLAG_SORT_MASK) <
  1986. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  1987. return 0;
  1988. }
  1989. static int fontinfo_selorder_compare(void *av, void *bv)
  1990. {
  1991. fontinfo *a = (fontinfo *)av;
  1992. fontinfo *b = (fontinfo *)bv;
  1993. int i;
  1994. if ((i = strnullcasecmp(a->family, b->family)) != 0)
  1995. return i;
  1996. /*
  1997. * Font class comes immediately after family, so that fonts
  1998. * from different classes with the same family
  1999. */
  2000. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  2001. return ((a->flags & FONTFLAG_SORT_MASK) <
  2002. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  2003. if ((i = strnullcasecmp(a->charset, b->charset)) != 0)
  2004. return i;
  2005. if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)
  2006. return i;
  2007. if ((i = strnullcasecmp(a->style, b->style)) != 0)
  2008. return i;
  2009. if (a->size != b->size)
  2010. return (a->size < b->size ? -1 : +1);
  2011. if (a->index != b->index)
  2012. return (a->index < b->index ? -1 : +1);
  2013. return 0;
  2014. }
  2015. static void unifontsel_draw_preview_text(unifontsel_internal *fs);
  2016. static void unifontsel_deselect(unifontsel_internal *fs)
  2017. {
  2018. fs->selected = NULL;
  2019. gtk_list_store_clear(fs->style_model);
  2020. gtk_list_store_clear(fs->size_model);
  2021. gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
  2022. gtk_widget_set_sensitive(fs->size_entry, FALSE);
  2023. unifontsel_draw_preview_text(fs);
  2024. }
  2025. static void unifontsel_setup_familylist(unifontsel_internal *fs)
  2026. {
  2027. GtkTreeIter iter;
  2028. int i, listindex, minpos = -1, maxpos = -1;
  2029. char *currfamily = NULL;
  2030. int currflags = -1;
  2031. fontinfo *info;
  2032. fs->inhibit_response = TRUE;
  2033. gtk_list_store_clear(fs->family_model);
  2034. listindex = 0;
  2035. /*
  2036. * Search through the font tree for anything matching our
  2037. * current filter criteria. When we find one, add its font
  2038. * name to the list box.
  2039. */
  2040. for (i = 0 ;; i++) {
  2041. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2042. /*
  2043. * info may be NULL if we've just run off the end of the
  2044. * tree. We must still do a processing pass in that
  2045. * situation, in case we had an unfinished font record in
  2046. * progress.
  2047. */
  2048. if (info && (info->flags &~ fs->filter_flags)) {
  2049. info->familyindex = -1;
  2050. continue; /* we're filtering out this font */
  2051. }
  2052. if (!info || strnullcasecmp(currfamily, info->family) ||
  2053. currflags != (info->flags & FONTFLAG_SORT_MASK)) {
  2054. /*
  2055. * We've either finished a family, or started a new
  2056. * one, or both.
  2057. */
  2058. if (currfamily) {
  2059. gtk_list_store_append(fs->family_model, &iter);
  2060. gtk_list_store_set(fs->family_model, &iter,
  2061. 0, currfamily, 1, minpos, 2, maxpos+1, -1);
  2062. listindex++;
  2063. }
  2064. if (info) {
  2065. minpos = i;
  2066. currfamily = info->family;
  2067. currflags = info->flags & FONTFLAG_SORT_MASK;
  2068. }
  2069. }
  2070. if (!info)
  2071. break; /* now we're done */
  2072. info->familyindex = listindex;
  2073. maxpos = i;
  2074. }
  2075. /*
  2076. * If we've just filtered out the previously selected font,
  2077. * deselect it thoroughly.
  2078. */
  2079. if (fs->selected && fs->selected->familyindex < 0)
  2080. unifontsel_deselect(fs);
  2081. fs->inhibit_response = FALSE;
  2082. }
  2083. static void unifontsel_setup_stylelist(unifontsel_internal *fs,
  2084. int start, int end)
  2085. {
  2086. GtkTreeIter iter;
  2087. int i, listindex, minpos = -1, maxpos = -1, started = FALSE;
  2088. char *currcs = NULL, *currstyle = NULL;
  2089. fontinfo *info;
  2090. gtk_list_store_clear(fs->style_model);
  2091. listindex = 0;
  2092. started = FALSE;
  2093. /*
  2094. * Search through the font tree for anything matching our
  2095. * current filter criteria. When we find one, add its charset
  2096. * and/or style name to the list box.
  2097. */
  2098. for (i = start; i <= end; i++) {
  2099. if (i == end)
  2100. info = NULL;
  2101. else
  2102. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2103. /*
  2104. * info may be NULL if we've just run off the end of the
  2105. * relevant data. We must still do a processing pass in
  2106. * that situation, in case we had an unfinished font
  2107. * record in progress.
  2108. */
  2109. if (info && (info->flags &~ fs->filter_flags)) {
  2110. info->styleindex = -1;
  2111. continue; /* we're filtering out this font */
  2112. }
  2113. if (!info || !started || strnullcasecmp(currcs, info->charset) ||
  2114. strnullcasecmp(currstyle, info->style)) {
  2115. /*
  2116. * We've either finished a style/charset, or started a
  2117. * new one, or both.
  2118. */
  2119. started = TRUE;
  2120. if (currstyle) {
  2121. gtk_list_store_append(fs->style_model, &iter);
  2122. gtk_list_store_set(fs->style_model, &iter,
  2123. 0, currstyle, 1, minpos, 2, maxpos+1,
  2124. 3, TRUE, 4, PANGO_WEIGHT_NORMAL, -1);
  2125. listindex++;
  2126. }
  2127. if (info) {
  2128. minpos = i;
  2129. if (info->charset && strnullcasecmp(currcs, info->charset)) {
  2130. gtk_list_store_append(fs->style_model, &iter);
  2131. gtk_list_store_set(fs->style_model, &iter,
  2132. 0, info->charset, 1, -1, 2, -1,
  2133. 3, FALSE, 4, PANGO_WEIGHT_BOLD, -1);
  2134. listindex++;
  2135. }
  2136. currcs = info->charset;
  2137. currstyle = info->style;
  2138. }
  2139. }
  2140. if (!info)
  2141. break; /* now we're done */
  2142. info->styleindex = listindex;
  2143. maxpos = i;
  2144. }
  2145. }
  2146. static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };
  2147. static void unifontsel_setup_sizelist(unifontsel_internal *fs,
  2148. int start, int end)
  2149. {
  2150. GtkTreeIter iter;
  2151. int i, listindex;
  2152. char sizetext[40];
  2153. fontinfo *info;
  2154. gtk_list_store_clear(fs->size_model);
  2155. listindex = 0;
  2156. /*
  2157. * Search through the font tree for anything matching our
  2158. * current filter criteria. When we find one, add its font
  2159. * name to the list box.
  2160. */
  2161. for (i = start; i < end; i++) {
  2162. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2163. if (info->flags &~ fs->filter_flags) {
  2164. info->sizeindex = -1;
  2165. continue; /* we're filtering out this font */
  2166. }
  2167. if (info->size) {
  2168. sprintf(sizetext, "%d", info->size);
  2169. info->sizeindex = listindex;
  2170. gtk_list_store_append(fs->size_model, &iter);
  2171. gtk_list_store_set(fs->size_model, &iter,
  2172. 0, sizetext, 1, i, 2, info->size, -1);
  2173. listindex++;
  2174. } else {
  2175. int j;
  2176. assert(i == start);
  2177. assert(i+1 == end);
  2178. for (j = 0; j < lenof(unifontsel_default_sizes); j++) {
  2179. sprintf(sizetext, "%d", unifontsel_default_sizes[j]);
  2180. gtk_list_store_append(fs->size_model, &iter);
  2181. gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,
  2182. 2, unifontsel_default_sizes[j], -1);
  2183. listindex++;
  2184. }
  2185. }
  2186. }
  2187. }
  2188. static void unifontsel_set_filter_buttons(unifontsel_internal *fs)
  2189. {
  2190. int i;
  2191. for (i = 0; i < fs->n_filter_buttons; i++) {
  2192. int flagbit = GPOINTER_TO_INT(g_object_get_data
  2193. (G_OBJECT(fs->filter_buttons[i]),
  2194. "user-data"));
  2195. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),
  2196. !!(fs->filter_flags & flagbit));
  2197. }
  2198. }
  2199. static void unifontsel_draw_preview_text_inner(unifont_drawctx *dctx,
  2200. unifontsel_internal *fs)
  2201. {
  2202. unifont *font;
  2203. char *sizename = NULL;
  2204. fontinfo *info = fs->selected;
  2205. if (info) {
  2206. sizename = info->fontclass->scale_fontname
  2207. (GTK_WIDGET(fs->u.window), info->realname, fs->selsize);
  2208. font = info->fontclass->create(GTK_WIDGET(fs->u.window),
  2209. sizename ? sizename : info->realname,
  2210. FALSE, FALSE, 0, 0);
  2211. } else
  2212. font = NULL;
  2213. #ifdef DRAW_TEXT_GDK
  2214. if (dctx->type == DRAWTYPE_GDK) {
  2215. gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_bg);
  2216. gdk_draw_rectangle(dctx->u.gdk.target, dctx->u.gdk.gc, 1, 0, 0,
  2217. fs->preview_width, fs->preview_height);
  2218. gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_fg);
  2219. }
  2220. #endif
  2221. #ifdef DRAW_TEXT_CAIRO
  2222. if (dctx->type == DRAWTYPE_CAIRO) {
  2223. cairo_set_source_rgb(dctx->u.cairo.cr,
  2224. fs->preview_bg.red / 65535.0,
  2225. fs->preview_bg.green / 65535.0,
  2226. fs->preview_bg.blue / 65535.0);
  2227. cairo_paint(dctx->u.cairo.cr);
  2228. cairo_set_source_rgb(dctx->u.cairo.cr,
  2229. fs->preview_fg.red / 65535.0,
  2230. fs->preview_fg.green / 65535.0,
  2231. fs->preview_fg.blue / 65535.0);
  2232. }
  2233. #endif
  2234. if (font) {
  2235. /*
  2236. * The pangram used here is rather carefully
  2237. * constructed: it contains a sequence of very narrow
  2238. * letters (`jil') and a pair of adjacent very wide
  2239. * letters (`wm').
  2240. *
  2241. * If the user selects a proportional font, it will be
  2242. * coerced into fixed-width character cells when used
  2243. * in the actual terminal window. We therefore display
  2244. * it the same way in the preview pane, so as to show
  2245. * it the way it will actually be displayed - and we
  2246. * deliberately pick a pangram which will show the
  2247. * resulting miskerning at its worst.
  2248. *
  2249. * We aren't trying to sell people these fonts; we're
  2250. * trying to let them make an informed choice. Better
  2251. * that they find out the problems with using
  2252. * proportional fonts in terminal windows here than
  2253. * that they go to the effort of selecting their font
  2254. * and _then_ realise it was a mistake.
  2255. */
  2256. info->fontclass->draw_text(dctx, font,
  2257. 0, font->ascent,
  2258. L"bankrupt jilted showmen quiz convex fogey",
  2259. 41, FALSE, FALSE, font->width);
  2260. info->fontclass->draw_text(dctx, font,
  2261. 0, font->ascent + font->height,
  2262. L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
  2263. 41, FALSE, FALSE, font->width);
  2264. /*
  2265. * The ordering of punctuation here is also selected
  2266. * with some specific aims in mind. I put ` and '
  2267. * together because some software (and people) still
  2268. * use them as matched quotes no matter what Unicode
  2269. * might say on the matter, so people can quickly
  2270. * check whether they look silly in a candidate font.
  2271. * The sequence #_@ is there to let people judge the
  2272. * suitability of the underscore as an effectively
  2273. * alphabetic character (since that's how it's often
  2274. * used in practice, at least by programmers).
  2275. */
  2276. info->fontclass->draw_text(dctx, font,
  2277. 0, font->ascent + font->height * 2,
  2278. L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
  2279. 42, FALSE, FALSE, font->width);
  2280. info->fontclass->destroy(font);
  2281. }
  2282. sfree(sizename);
  2283. }
  2284. static void unifontsel_draw_preview_text(unifontsel_internal *fs)
  2285. {
  2286. unifont_drawctx dctx;
  2287. GdkWindow *target;
  2288. #ifndef NO_BACKING_PIXMAPS
  2289. target = fs->preview_pixmap;
  2290. #else
  2291. target = gtk_widget_get_window(fs->preview_area);
  2292. #endif
  2293. if (!target) /* we may be called when we haven't created everything yet */
  2294. return;
  2295. dctx.type = DRAWTYPE_DEFAULT;
  2296. #ifdef DRAW_TEXT_GDK
  2297. if (dctx.type == DRAWTYPE_GDK) {
  2298. dctx.u.gdk.target = target;
  2299. dctx.u.gdk.gc = gdk_gc_new(target);
  2300. }
  2301. #endif
  2302. #ifdef DRAW_TEXT_CAIRO
  2303. if (dctx.type == DRAWTYPE_CAIRO) {
  2304. dctx.u.cairo.widget = GTK_WIDGET(fs->preview_area);
  2305. dctx.u.cairo.cr = gdk_cairo_create(target);
  2306. }
  2307. #endif
  2308. unifontsel_draw_preview_text_inner(&dctx, fs);
  2309. #ifdef DRAW_TEXT_GDK
  2310. if (dctx.type == DRAWTYPE_GDK) {
  2311. gdk_gc_unref(dctx.u.gdk.gc);
  2312. }
  2313. #endif
  2314. #ifdef DRAW_TEXT_CAIRO
  2315. if (dctx.type == DRAWTYPE_CAIRO) {
  2316. cairo_destroy(dctx.u.cairo.cr);
  2317. }
  2318. #endif
  2319. gdk_window_invalidate_rect(gtk_widget_get_window(fs->preview_area),
  2320. NULL, FALSE);
  2321. }
  2322. static void unifontsel_select_font(unifontsel_internal *fs,
  2323. fontinfo *info, int size, int leftlist,
  2324. int size_is_explicit)
  2325. {
  2326. int index;
  2327. int minval, maxval;
  2328. gboolean success;
  2329. GtkTreePath *treepath;
  2330. GtkTreeIter iter;
  2331. fs->inhibit_response = TRUE;
  2332. fs->selected = info;
  2333. fs->selsize = size;
  2334. if (size_is_explicit)
  2335. fs->intendedsize = size;
  2336. gtk_widget_set_sensitive(fs->u.ok_button, TRUE);
  2337. /*
  2338. * Find the index of this fontinfo in the selorder list.
  2339. */
  2340. index = -1;
  2341. findpos234(fs->fonts_by_selorder, info, NULL, &index);
  2342. assert(index >= 0);
  2343. /*
  2344. * Adjust the font selector flags and redo the font family
  2345. * list box, if necessary.
  2346. */
  2347. if (leftlist <= 0 &&
  2348. (fs->filter_flags | info->flags) != fs->filter_flags) {
  2349. fs->filter_flags |= info->flags;
  2350. unifontsel_set_filter_buttons(fs);
  2351. unifontsel_setup_familylist(fs);
  2352. }
  2353. /*
  2354. * Find the appropriate family name and select it in the list.
  2355. */
  2356. assert(info->familyindex >= 0);
  2357. treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);
  2358. gtk_tree_selection_select_path
  2359. (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),
  2360. treepath);
  2361. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),
  2362. treepath, NULL, FALSE, 0.0, 0.0);
  2363. success = gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model),
  2364. &iter, treepath);
  2365. assert(success);
  2366. gtk_tree_path_free(treepath);
  2367. /*
  2368. * Now set up the font style list.
  2369. */
  2370. gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,
  2371. 1, &minval, 2, &maxval, -1);
  2372. if (leftlist <= 1)
  2373. unifontsel_setup_stylelist(fs, minval, maxval);
  2374. /*
  2375. * Find the appropriate style name and select it in the list.
  2376. */
  2377. if (info->style) {
  2378. assert(info->styleindex >= 0);
  2379. treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);
  2380. gtk_tree_selection_select_path
  2381. (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),
  2382. treepath);
  2383. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),
  2384. treepath, NULL, FALSE, 0.0, 0.0);
  2385. gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),
  2386. &iter, treepath);
  2387. gtk_tree_path_free(treepath);
  2388. /*
  2389. * And set up the size list.
  2390. */
  2391. gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,
  2392. 1, &minval, 2, &maxval, -1);
  2393. if (leftlist <= 2)
  2394. unifontsel_setup_sizelist(fs, minval, maxval);
  2395. /*
  2396. * Find the appropriate size, and select it in the list.
  2397. */
  2398. if (info->size) {
  2399. assert(info->sizeindex >= 0);
  2400. treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);
  2401. gtk_tree_selection_select_path
  2402. (gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),
  2403. treepath);
  2404. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
  2405. treepath, NULL, FALSE, 0.0, 0.0);
  2406. gtk_tree_path_free(treepath);
  2407. size = info->size;
  2408. } else {
  2409. int j;
  2410. for (j = 0; j < lenof(unifontsel_default_sizes); j++)
  2411. if (unifontsel_default_sizes[j] == size) {
  2412. treepath = gtk_tree_path_new_from_indices(j, -1);
  2413. gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),
  2414. treepath, NULL, FALSE);
  2415. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
  2416. treepath, NULL, FALSE, 0.0,
  2417. 0.0);
  2418. gtk_tree_path_free(treepath);
  2419. }
  2420. }
  2421. /*
  2422. * And set up the font size text entry box.
  2423. */
  2424. {
  2425. char sizetext[40];
  2426. sprintf(sizetext, "%d", size);
  2427. gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);
  2428. }
  2429. } else {
  2430. if (leftlist <= 2)
  2431. unifontsel_setup_sizelist(fs, 0, 0);
  2432. gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");
  2433. }
  2434. /*
  2435. * Grey out the font size edit box if we're not using a
  2436. * scalable font.
  2437. */
  2438. gtk_editable_set_editable(GTK_EDITABLE(fs->size_entry),
  2439. fs->selected->size == 0);
  2440. gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);
  2441. unifontsel_draw_preview_text(fs);
  2442. fs->inhibit_response = FALSE;
  2443. }
  2444. static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)
  2445. {
  2446. unifontsel_internal *fs = (unifontsel_internal *)data;
  2447. int newstate = gtk_toggle_button_get_active(tb);
  2448. int newflags;
  2449. int flagbit = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tb),
  2450. "user-data"));
  2451. if (newstate)
  2452. newflags = fs->filter_flags | flagbit;
  2453. else
  2454. newflags = fs->filter_flags & ~flagbit;
  2455. if (fs->filter_flags != newflags) {
  2456. fs->filter_flags = newflags;
  2457. unifontsel_setup_familylist(fs);
  2458. }
  2459. }
  2460. static void unifontsel_add_entry(void *ctx, const char *realfontname,
  2461. const char *family, const char *charset,
  2462. const char *style, const char *stylekey,
  2463. int size, int flags,
  2464. const struct unifont_vtable *fontclass)
  2465. {
  2466. unifontsel_internal *fs = (unifontsel_internal *)ctx;
  2467. fontinfo *info;
  2468. int totalsize;
  2469. char *p;
  2470. totalsize = sizeof(fontinfo) + strlen(realfontname) +
  2471. (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +
  2472. (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;
  2473. info = (fontinfo *)smalloc(totalsize);
  2474. info->fontclass = fontclass;
  2475. p = (char *)info + sizeof(fontinfo);
  2476. info->realname = p;
  2477. strcpy(p, realfontname);
  2478. p += 1+strlen(p);
  2479. if (family) {
  2480. info->family = p;
  2481. strcpy(p, family);
  2482. p += 1+strlen(p);
  2483. } else
  2484. info->family = NULL;
  2485. if (charset) {
  2486. info->charset = p;
  2487. strcpy(p, charset);
  2488. p += 1+strlen(p);
  2489. } else
  2490. info->charset = NULL;
  2491. if (style) {
  2492. info->style = p;
  2493. strcpy(p, style);
  2494. p += 1+strlen(p);
  2495. } else
  2496. info->style = NULL;
  2497. if (stylekey) {
  2498. info->stylekey = p;
  2499. strcpy(p, stylekey);
  2500. p += 1+strlen(p);
  2501. } else
  2502. info->stylekey = NULL;
  2503. assert(p - (char *)info <= totalsize);
  2504. info->size = size;
  2505. info->flags = flags;
  2506. info->index = count234(fs->fonts_by_selorder);
  2507. /*
  2508. * It's just conceivable that a misbehaving font enumerator
  2509. * might tell us about the same font real name more than once,
  2510. * in which case we should silently drop the new one.
  2511. */
  2512. if (add234(fs->fonts_by_realname, info) != info) {
  2513. sfree(info);
  2514. return;
  2515. }
  2516. /*
  2517. * However, we should never get a duplicate key in the
  2518. * selorder tree, because the index field carefully
  2519. * disambiguates otherwise identical records.
  2520. */
  2521. add234(fs->fonts_by_selorder, info);
  2522. }
  2523. static fontinfo *update_for_intended_size(unifontsel_internal *fs,
  2524. fontinfo *info)
  2525. {
  2526. fontinfo info2, *below, *above;
  2527. int pos;
  2528. /*
  2529. * Copy the info structure. This doesn't copy its dynamic
  2530. * string fields, but that's unimportant because all we're
  2531. * going to do is to adjust the size field and use it in one
  2532. * tree search.
  2533. */
  2534. info2 = *info;
  2535. info2.size = fs->intendedsize;
  2536. /*
  2537. * Search in the tree to find the fontinfo structure which
  2538. * best approximates the size the user last requested.
  2539. */
  2540. below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
  2541. REL234_LE, &pos);
  2542. if (!below)
  2543. pos = -1;
  2544. above = index234(fs->fonts_by_selorder, pos+1);
  2545. /*
  2546. * See if we've found it exactly, which is an easy special
  2547. * case. If we have, it'll be in `below' and not `above',
  2548. * because we did a REL234_LE rather than REL234_LT search.
  2549. */
  2550. if (below && !fontinfo_selorder_compare(&info2, below))
  2551. return below;
  2552. /*
  2553. * Now we've either found two suitable fonts, one smaller and
  2554. * one larger, or we're at one or other extreme end of the
  2555. * scale. Find out which, by NULLing out either of below and
  2556. * above if it differs from this one in any respect but size
  2557. * (and the disambiguating index field). Bear in mind, also,
  2558. * that either one might _already_ be NULL if we're at the
  2559. * extreme ends of the font list.
  2560. */
  2561. if (below) {
  2562. info2.size = below->size;
  2563. info2.index = below->index;
  2564. if (fontinfo_selorder_compare(&info2, below))
  2565. below = NULL;
  2566. }
  2567. if (above) {
  2568. info2.size = above->size;
  2569. info2.index = above->index;
  2570. if (fontinfo_selorder_compare(&info2, above))
  2571. above = NULL;
  2572. }
  2573. /*
  2574. * Now return whichever of above and below is non-NULL, if
  2575. * that's unambiguous.
  2576. */
  2577. if (!above)
  2578. return below;
  2579. if (!below)
  2580. return above;
  2581. /*
  2582. * And now we really do have to make a choice about whether to
  2583. * round up or down. We'll do it by rounding to nearest,
  2584. * breaking ties by rounding up.
  2585. */
  2586. if (above->size - fs->intendedsize <= fs->intendedsize - below->size)
  2587. return above;
  2588. else
  2589. return below;
  2590. }
  2591. static void family_changed(GtkTreeSelection *treeselection, gpointer data)
  2592. {
  2593. unifontsel_internal *fs = (unifontsel_internal *)data;
  2594. GtkTreeModel *treemodel;
  2595. GtkTreeIter treeiter;
  2596. int minval;
  2597. fontinfo *info;
  2598. if (fs->inhibit_response) /* we made this change ourselves */
  2599. return;
  2600. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2601. return;
  2602. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
  2603. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2604. info = update_for_intended_size(fs, info);
  2605. if (!info)
  2606. return; /* _shouldn't_ happen unless font list is completely funted */
  2607. if (!info->size)
  2608. fs->selsize = fs->intendedsize; /* font is scalable */
  2609. unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
  2610. 1, FALSE);
  2611. }
  2612. static void style_changed(GtkTreeSelection *treeselection, gpointer data)
  2613. {
  2614. unifontsel_internal *fs = (unifontsel_internal *)data;
  2615. GtkTreeModel *treemodel;
  2616. GtkTreeIter treeiter;
  2617. int minval;
  2618. fontinfo *info;
  2619. if (fs->inhibit_response) /* we made this change ourselves */
  2620. return;
  2621. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2622. return;
  2623. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
  2624. if (minval < 0)
  2625. return; /* somehow a charset heading got clicked */
  2626. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2627. info = update_for_intended_size(fs, info);
  2628. if (!info)
  2629. return; /* _shouldn't_ happen unless font list is completely funted */
  2630. if (!info->size)
  2631. fs->selsize = fs->intendedsize; /* font is scalable */
  2632. unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
  2633. 2, FALSE);
  2634. }
  2635. static void size_changed(GtkTreeSelection *treeselection, gpointer data)
  2636. {
  2637. unifontsel_internal *fs = (unifontsel_internal *)data;
  2638. GtkTreeModel *treemodel;
  2639. GtkTreeIter treeiter;
  2640. int minval, size;
  2641. fontinfo *info;
  2642. if (fs->inhibit_response) /* we made this change ourselves */
  2643. return;
  2644. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2645. return;
  2646. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);
  2647. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2648. unifontsel_select_font(fs, info, info->size ? info->size : size, 3, TRUE);
  2649. }
  2650. static void size_entry_changed(GtkEditable *ed, gpointer data)
  2651. {
  2652. unifontsel_internal *fs = (unifontsel_internal *)data;
  2653. const char *text;
  2654. int size;
  2655. if (fs->inhibit_response) /* we made this change ourselves */
  2656. return;
  2657. text = gtk_entry_get_text(GTK_ENTRY(ed));
  2658. size = atoi(text);
  2659. if (size > 0) {
  2660. assert(fs->selected->size == 0);
  2661. unifontsel_select_font(fs, fs->selected, size, 3, TRUE);
  2662. }
  2663. }
  2664. static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,
  2665. GtkTreeViewColumn *column, gpointer data)
  2666. {
  2667. unifontsel_internal *fs = (unifontsel_internal *)data;
  2668. GtkTreeIter iter;
  2669. int minval, newsize;
  2670. fontinfo *info, *newinfo;
  2671. char *newname;
  2672. if (fs->inhibit_response) /* we made this change ourselves */
  2673. return;
  2674. gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);
  2675. gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);
  2676. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2677. if (info) {
  2678. int flags;
  2679. struct fontinfo_realname_find f;
  2680. newname = info->fontclass->canonify_fontname
  2681. (GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, TRUE);
  2682. f.realname = newname;
  2683. f.flags = flags;
  2684. newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  2685. sfree(newname);
  2686. if (!newinfo)
  2687. return; /* font name not in our index */
  2688. if (newinfo == info)
  2689. return; /* didn't change under canonification => not an alias */
  2690. unifontsel_select_font(fs, newinfo,
  2691. newinfo->size ? newinfo->size : newsize,
  2692. 1, TRUE);
  2693. }
  2694. }
  2695. #if GTK_CHECK_VERSION(3,0,0)
  2696. static gint unifontsel_draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
  2697. {
  2698. unifontsel_internal *fs = (unifontsel_internal *)data;
  2699. unifont_drawctx dctx;
  2700. dctx.type = DRAWTYPE_CAIRO;
  2701. dctx.u.cairo.widget = widget;
  2702. dctx.u.cairo.cr = cr;
  2703. unifontsel_draw_preview_text_inner(&dctx, fs);
  2704. return TRUE;
  2705. }
  2706. #else
  2707. static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,
  2708. gpointer data)
  2709. {
  2710. unifontsel_internal *fs = (unifontsel_internal *)data;
  2711. #ifndef NO_BACKING_PIXMAPS
  2712. if (fs->preview_pixmap) {
  2713. gdk_draw_pixmap(gtk_widget_get_window(widget),
  2714. (gtk_widget_get_style(widget)->fg_gc
  2715. [gtk_widget_get_state(widget)]),
  2716. fs->preview_pixmap,
  2717. event->area.x, event->area.y,
  2718. event->area.x, event->area.y,
  2719. event->area.width, event->area.height);
  2720. }
  2721. #else
  2722. unifontsel_draw_preview_text(fs);
  2723. #endif
  2724. return TRUE;
  2725. }
  2726. #endif
  2727. static gint unifontsel_configure_area(GtkWidget *widget,
  2728. GdkEventConfigure *event, gpointer data)
  2729. {
  2730. #ifndef NO_BACKING_PIXMAPS
  2731. unifontsel_internal *fs = (unifontsel_internal *)data;
  2732. int ox, oy, nx, ny, x, y;
  2733. /*
  2734. * Enlarge the pixmap, but never shrink it.
  2735. */
  2736. ox = fs->preview_width;
  2737. oy = fs->preview_height;
  2738. x = event->width;
  2739. y = event->height;
  2740. if (x > ox || y > oy) {
  2741. if (fs->preview_pixmap)
  2742. gdk_pixmap_unref(fs->preview_pixmap);
  2743. nx = (x > ox ? x : ox);
  2744. ny = (y > oy ? y : oy);
  2745. fs->preview_pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
  2746. nx, ny, -1);
  2747. fs->preview_width = nx;
  2748. fs->preview_height = ny;
  2749. unifontsel_draw_preview_text(fs);
  2750. }
  2751. #endif
  2752. gdk_window_invalidate_rect(gtk_widget_get_window(widget), NULL, FALSE);
  2753. return TRUE;
  2754. }
  2755. unifontsel *unifontsel_new(const char *wintitle)
  2756. {
  2757. unifontsel_internal *fs = snew(unifontsel_internal);
  2758. GtkWidget *table, *label, *w, *ww, *scroll;
  2759. GtkListStore *model;
  2760. GtkTreeViewColumn *column;
  2761. int lists_height, preview_height, font_width, style_width, size_width;
  2762. int i;
  2763. fs->inhibit_response = FALSE;
  2764. fs->selected = NULL;
  2765. {
  2766. int width, height;
  2767. /*
  2768. * Invent some magic size constants.
  2769. */
  2770. get_label_text_dimensions("Quite Long Font Name (Foundry)",
  2771. &width, &height);
  2772. font_width = width;
  2773. lists_height = 14 * height;
  2774. preview_height = 5 * height;
  2775. get_label_text_dimensions("Italic Extra Condensed", &width, &height);
  2776. style_width = width;
  2777. get_label_text_dimensions("48000", &width, &height);
  2778. size_width = width;
  2779. }
  2780. /*
  2781. * Create the dialog box and initialise the user-visible
  2782. * fields in the returned structure.
  2783. */
  2784. fs->u.user_data = NULL;
  2785. fs->u.window = GTK_WINDOW(gtk_dialog_new());
  2786. gtk_window_set_title(fs->u.window, wintitle);
  2787. fs->u.cancel_button = gtk_dialog_add_button
  2788. (GTK_DIALOG(fs->u.window), STANDARD_CANCEL_LABEL, GTK_RESPONSE_CANCEL);
  2789. fs->u.ok_button = gtk_dialog_add_button
  2790. (GTK_DIALOG(fs->u.window), STANDARD_OK_LABEL, GTK_RESPONSE_OK);
  2791. gtk_widget_grab_default(fs->u.ok_button);
  2792. /*
  2793. * Now set up the internal fields, including in particular all
  2794. * the controls that actually allow the user to select fonts.
  2795. */
  2796. #if GTK_CHECK_VERSION(3,0,0)
  2797. table = gtk_grid_new();
  2798. gtk_grid_set_column_spacing(GTK_GRID(table), 8);
  2799. #else
  2800. table = gtk_table_new(8, 3, FALSE);
  2801. gtk_table_set_col_spacings(GTK_TABLE(table), 8);
  2802. #endif
  2803. gtk_widget_show(table);
  2804. #if GTK_CHECK_VERSION(3,0,0)
  2805. /* GtkAlignment has become deprecated and we use the "margin"
  2806. * property */
  2807. w = table;
  2808. g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
  2809. #elif GTK_CHECK_VERSION(2,4,0)
  2810. /* GtkAlignment seems to be the simplest way to put padding round things */
  2811. w = gtk_alignment_new(0, 0, 1, 1);
  2812. gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
  2813. gtk_container_add(GTK_CONTAINER(w), table);
  2814. gtk_widget_show(w);
  2815. #else
  2816. /* In GTK < 2.4, even that isn't available */
  2817. w = table;
  2818. #endif
  2819. gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area
  2820. (GTK_DIALOG(fs->u.window))),
  2821. w, TRUE, TRUE, 0);
  2822. label = gtk_label_new_with_mnemonic("_Font:");
  2823. gtk_widget_show(label);
  2824. align_label_left(GTK_LABEL(label));
  2825. #if GTK_CHECK_VERSION(3,0,0)
  2826. gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
  2827. g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
  2828. #else
  2829. gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
  2830. #endif
  2831. /*
  2832. * The Font list box displays only a string, but additionally
  2833. * stores two integers which give the limits within the
  2834. * tree234 of the font entries covered by this list entry.
  2835. */
  2836. model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
  2837. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  2838. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
  2839. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  2840. gtk_widget_show(w);
  2841. column = gtk_tree_view_column_new_with_attributes
  2842. ("Font", gtk_cell_renderer_text_new(),
  2843. "text", 0, (char *)NULL);
  2844. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  2845. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  2846. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  2847. "changed", G_CALLBACK(family_changed), fs);
  2848. g_signal_connect(G_OBJECT(w), "row-activated",
  2849. G_CALLBACK(alias_resolve), fs);
  2850. scroll = gtk_scrolled_window_new(NULL, NULL);
  2851. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  2852. GTK_SHADOW_IN);
  2853. gtk_container_add(GTK_CONTAINER(scroll), w);
  2854. gtk_widget_show(scroll);
  2855. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  2856. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  2857. gtk_widget_set_size_request(scroll, font_width, lists_height);
  2858. #if GTK_CHECK_VERSION(3,0,0)
  2859. gtk_grid_attach(GTK_GRID(table), scroll, 0, 1, 1, 2);
  2860. g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
  2861. #else
  2862. gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,
  2863. GTK_EXPAND | GTK_FILL, 0, 0);
  2864. #endif
  2865. fs->family_model = model;
  2866. fs->family_list = w;
  2867. label = gtk_label_new_with_mnemonic("_Style:");
  2868. gtk_widget_show(label);
  2869. align_label_left(GTK_LABEL(label));
  2870. #if GTK_CHECK_VERSION(3,0,0)
  2871. gtk_grid_attach(GTK_GRID(table), label, 1, 0, 1, 1);
  2872. g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
  2873. #else
  2874. gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
  2875. #endif
  2876. /*
  2877. * The Style list box can contain insensitive elements (character
  2878. * set headings for server-side fonts), so we add an extra column
  2879. * to the list store to hold that information. Also, since GTK3 at
  2880. * least doesn't seem to display insensitive elements differently
  2881. * by default, we add a further column to change their style.
  2882. */
  2883. model = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
  2884. G_TYPE_BOOLEAN, G_TYPE_INT);
  2885. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  2886. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
  2887. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  2888. gtk_widget_show(w);
  2889. column = gtk_tree_view_column_new_with_attributes
  2890. ("Style", gtk_cell_renderer_text_new(),
  2891. "text", 0, "sensitive", 3, "weight", 4, (char *)NULL);
  2892. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  2893. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  2894. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  2895. "changed", G_CALLBACK(style_changed), fs);
  2896. scroll = gtk_scrolled_window_new(NULL, NULL);
  2897. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  2898. GTK_SHADOW_IN);
  2899. gtk_container_add(GTK_CONTAINER(scroll), w);
  2900. gtk_widget_show(scroll);
  2901. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  2902. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  2903. gtk_widget_set_size_request(scroll, style_width, lists_height);
  2904. #if GTK_CHECK_VERSION(3,0,0)
  2905. gtk_grid_attach(GTK_GRID(table), scroll, 1, 1, 1, 2);
  2906. g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
  2907. #else
  2908. gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,
  2909. GTK_EXPAND | GTK_FILL, 0, 0);
  2910. #endif
  2911. fs->style_model = model;
  2912. fs->style_list = w;
  2913. label = gtk_label_new_with_mnemonic("Si_ze:");
  2914. gtk_widget_show(label);
  2915. align_label_left(GTK_LABEL(label));
  2916. #if GTK_CHECK_VERSION(3,0,0)
  2917. gtk_grid_attach(GTK_GRID(table), label, 2, 0, 1, 1);
  2918. g_object_set(G_OBJECT(label), "hexpand", TRUE, (const char *)NULL);
  2919. #else
  2920. gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
  2921. #endif
  2922. /*
  2923. * The Size label attaches primarily to a text input box so
  2924. * that the user can select a size of their choice. The list
  2925. * of available sizes is secondary.
  2926. */
  2927. fs->size_entry = w = gtk_entry_new();
  2928. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  2929. gtk_widget_set_size_request(w, size_width, -1);
  2930. gtk_widget_show(w);
  2931. #if GTK_CHECK_VERSION(3,0,0)
  2932. gtk_grid_attach(GTK_GRID(table), w, 2, 1, 1, 1);
  2933. g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
  2934. #else
  2935. gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);
  2936. #endif
  2937. g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),
  2938. fs);
  2939. model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
  2940. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  2941. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), FALSE);
  2942. gtk_widget_show(w);
  2943. column = gtk_tree_view_column_new_with_attributes
  2944. ("Size", gtk_cell_renderer_text_new(),
  2945. "text", 0, (char *)NULL);
  2946. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  2947. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  2948. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  2949. "changed", G_CALLBACK(size_changed), fs);
  2950. scroll = gtk_scrolled_window_new(NULL, NULL);
  2951. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  2952. GTK_SHADOW_IN);
  2953. gtk_container_add(GTK_CONTAINER(scroll), w);
  2954. gtk_widget_show(scroll);
  2955. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  2956. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  2957. #if GTK_CHECK_VERSION(3,0,0)
  2958. gtk_grid_attach(GTK_GRID(table), scroll, 2, 2, 1, 1);
  2959. g_object_set(G_OBJECT(scroll), "expand", TRUE, (const char *)NULL);
  2960. #else
  2961. gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,
  2962. GTK_EXPAND | GTK_FILL, 0, 0);
  2963. #endif
  2964. fs->size_model = model;
  2965. fs->size_list = w;
  2966. /*
  2967. * Preview widget.
  2968. */
  2969. fs->preview_area = gtk_drawing_area_new();
  2970. #ifndef NO_BACKING_PIXMAPS
  2971. fs->preview_pixmap = NULL;
  2972. #endif
  2973. fs->preview_width = 0;
  2974. fs->preview_height = 0;
  2975. fs->preview_fg.pixel = fs->preview_bg.pixel = 0;
  2976. fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;
  2977. fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;
  2978. #if !GTK_CHECK_VERSION(3,0,0)
  2979. gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,
  2980. FALSE, FALSE);
  2981. gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,
  2982. FALSE, FALSE);
  2983. #endif
  2984. #if GTK_CHECK_VERSION(3,0,0)
  2985. g_signal_connect(G_OBJECT(fs->preview_area), "draw",
  2986. G_CALLBACK(unifontsel_draw_area), fs);
  2987. #else
  2988. g_signal_connect(G_OBJECT(fs->preview_area), "expose_event",
  2989. G_CALLBACK(unifontsel_expose_area), fs);
  2990. #endif
  2991. g_signal_connect(G_OBJECT(fs->preview_area), "configure_event",
  2992. G_CALLBACK(unifontsel_configure_area), fs);
  2993. gtk_widget_set_size_request(fs->preview_area, 1, preview_height);
  2994. gtk_widget_show(fs->preview_area);
  2995. ww = fs->preview_area;
  2996. w = gtk_frame_new(NULL);
  2997. gtk_container_add(GTK_CONTAINER(w), ww);
  2998. gtk_widget_show(w);
  2999. #if GTK_CHECK_VERSION(3,0,0)
  3000. /* GtkAlignment has become deprecated and we use the "margin"
  3001. * property */
  3002. g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
  3003. #elif GTK_CHECK_VERSION(2,4,0)
  3004. ww = w;
  3005. /* GtkAlignment seems to be the simplest way to put padding round things */
  3006. w = gtk_alignment_new(0, 0, 1, 1);
  3007. gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
  3008. gtk_container_add(GTK_CONTAINER(w), ww);
  3009. gtk_widget_show(w);
  3010. #endif
  3011. ww = w;
  3012. w = gtk_frame_new("Preview of font");
  3013. gtk_container_add(GTK_CONTAINER(w), ww);
  3014. gtk_widget_show(w);
  3015. #if GTK_CHECK_VERSION(3,0,0)
  3016. gtk_grid_attach(GTK_GRID(table), w, 0, 3, 3, 1);
  3017. g_object_set(G_OBJECT(w), "expand", TRUE, (const char *)NULL);
  3018. #else
  3019. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,
  3020. GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);
  3021. #endif
  3022. /*
  3023. * We only provide the checkboxes for client- and server-side
  3024. * fonts if we have the X11 back end available, because that's the
  3025. * only situation in which more than one class of font is
  3026. * available anyway.
  3027. */
  3028. fs->n_filter_buttons = 0;
  3029. #ifndef NOT_X_WINDOWS
  3030. w = gtk_check_button_new_with_label("Show client-side fonts");
  3031. g_object_set_data(G_OBJECT(w), "user-data",
  3032. GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));
  3033. g_signal_connect(G_OBJECT(w), "toggled",
  3034. G_CALLBACK(unifontsel_button_toggled), fs);
  3035. gtk_widget_show(w);
  3036. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3037. #if GTK_CHECK_VERSION(3,0,0)
  3038. gtk_grid_attach(GTK_GRID(table), w, 0, 4, 3, 1);
  3039. g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
  3040. #else
  3041. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);
  3042. #endif
  3043. w = gtk_check_button_new_with_label("Show server-side fonts");
  3044. g_object_set_data(G_OBJECT(w), "user-data",
  3045. GINT_TO_POINTER(FONTFLAG_SERVERSIDE));
  3046. g_signal_connect(G_OBJECT(w), "toggled",
  3047. G_CALLBACK(unifontsel_button_toggled), fs);
  3048. gtk_widget_show(w);
  3049. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3050. #if GTK_CHECK_VERSION(3,0,0)
  3051. gtk_grid_attach(GTK_GRID(table), w, 0, 5, 3, 1);
  3052. g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
  3053. #else
  3054. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);
  3055. #endif
  3056. w = gtk_check_button_new_with_label("Show server-side font aliases");
  3057. g_object_set_data(G_OBJECT(w), "user-data",
  3058. GINT_TO_POINTER(FONTFLAG_SERVERALIAS));
  3059. g_signal_connect(G_OBJECT(w), "toggled",
  3060. G_CALLBACK(unifontsel_button_toggled), fs);
  3061. gtk_widget_show(w);
  3062. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3063. #if GTK_CHECK_VERSION(3,0,0)
  3064. gtk_grid_attach(GTK_GRID(table), w, 0, 6, 3, 1);
  3065. g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
  3066. #else
  3067. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);
  3068. #endif
  3069. #endif /* NOT_X_WINDOWS */
  3070. w = gtk_check_button_new_with_label("Show non-monospaced fonts");
  3071. g_object_set_data(G_OBJECT(w), "user-data",
  3072. GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));
  3073. g_signal_connect(G_OBJECT(w), "toggled",
  3074. G_CALLBACK(unifontsel_button_toggled), fs);
  3075. gtk_widget_show(w);
  3076. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3077. #if GTK_CHECK_VERSION(3,0,0)
  3078. gtk_grid_attach(GTK_GRID(table), w, 0, 7, 3, 1);
  3079. g_object_set(G_OBJECT(w), "hexpand", TRUE, (const char *)NULL);
  3080. #else
  3081. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);
  3082. #endif
  3083. assert(fs->n_filter_buttons <= lenof(fs->filter_buttons));
  3084. fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |
  3085. FONTFLAG_SERVERALIAS;
  3086. unifontsel_set_filter_buttons(fs);
  3087. /*
  3088. * Go and find all the font names, and set up our master font
  3089. * list.
  3090. */
  3091. fs->fonts_by_realname = newtree234(fontinfo_realname_compare);
  3092. fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);
  3093. for (i = 0; i < lenof(unifont_types); i++)
  3094. unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),
  3095. unifontsel_add_entry, fs);
  3096. /*
  3097. * And set up the initial font names list.
  3098. */
  3099. unifontsel_setup_familylist(fs);
  3100. fs->selsize = fs->intendedsize = 13; /* random default */
  3101. gtk_widget_set_sensitive(fs->u.ok_button, FALSE);
  3102. return (unifontsel *)fs;
  3103. }
  3104. void unifontsel_destroy(unifontsel *fontsel)
  3105. {
  3106. unifontsel_internal *fs = (unifontsel_internal *)fontsel;
  3107. fontinfo *info;
  3108. #ifndef NO_BACKING_PIXMAPS
  3109. if (fs->preview_pixmap)
  3110. gdk_pixmap_unref(fs->preview_pixmap);
  3111. #endif
  3112. freetree234(fs->fonts_by_selorder);
  3113. while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)
  3114. sfree(info);
  3115. freetree234(fs->fonts_by_realname);
  3116. gtk_widget_destroy(GTK_WIDGET(fs->u.window));
  3117. sfree(fs);
  3118. }
  3119. void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
  3120. {
  3121. unifontsel_internal *fs = (unifontsel_internal *)fontsel;
  3122. int i, start, end, size, flags;
  3123. const char *fontname2 = NULL;
  3124. fontinfo *info;
  3125. /*
  3126. * Provide a default if given an empty or null font name.
  3127. */
  3128. if (!fontname || !*fontname)
  3129. fontname = DEFAULT_GTK_FONT;
  3130. /*
  3131. * Call the canonify_fontname function.
  3132. */
  3133. fontname = unifont_do_prefix(fontname, &start, &end);
  3134. for (i = start; i < end; i++) {
  3135. fontname2 = unifont_types[i]->canonify_fontname
  3136. (GTK_WIDGET(fs->u.window), fontname, &size, &flags, FALSE);
  3137. if (fontname2)
  3138. break;
  3139. }
  3140. if (i == end)
  3141. return; /* font name not recognised */
  3142. /*
  3143. * Now look up the canonified font name in our index.
  3144. */
  3145. {
  3146. struct fontinfo_realname_find f;
  3147. f.realname = fontname2;
  3148. f.flags = flags;
  3149. info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  3150. }
  3151. /*
  3152. * If we've found the font, and its size field is either
  3153. * correct or zero (the latter indicating a scalable font),
  3154. * then we're done. Otherwise, try looking up the original
  3155. * font name instead.
  3156. */
  3157. if (!info || (info->size != size && info->size != 0)) {
  3158. struct fontinfo_realname_find f;
  3159. f.realname = fontname;
  3160. f.flags = flags;
  3161. info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  3162. if (!info || info->size != size)
  3163. return; /* font name not in our index */
  3164. }
  3165. /*
  3166. * Now we've got a fontinfo structure and a font size, so we
  3167. * know everything we need to fill in all the fields in the
  3168. * dialog.
  3169. */
  3170. unifontsel_select_font(fs, info, size, 0, TRUE);
  3171. }
  3172. char *unifontsel_get_name(unifontsel *fontsel)
  3173. {
  3174. unifontsel_internal *fs = (unifontsel_internal *)fontsel;
  3175. char *name;
  3176. if (!fs->selected)
  3177. return NULL;
  3178. if (fs->selected->size == 0) {
  3179. name = fs->selected->fontclass->scale_fontname
  3180. (GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
  3181. if (name) {
  3182. char *ret = dupcat(fs->selected->fontclass->prefix, ":",
  3183. name, NULL);
  3184. sfree(name);
  3185. return ret;
  3186. }
  3187. }
  3188. return dupcat(fs->selected->fontclass->prefix, ":",
  3189. fs->selected->realname, NULL);
  3190. }
  3191. #endif /* GTK_CHECK_VERSION(2,0,0) */