unifont.c 124 KB

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