unifont.c 128 KB

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