unifont.c 127 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805
  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 c = '\0';
  534. buffer_sink bs[1];
  535. buffer_sink_init(bs, &c, 1);
  536. put_wc_to_mb(bs, xfont->real_charset, &glyph, 1, "");
  537. if (!c)
  538. return false; /* not even in the charset */
  539. return x11_font_has_glyph(xfont->fonts[0].xfs, 0, (unsigned char)c);
  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. strbuf *sb = strbuf_new();
  854. put_wc_to_mb(sb, xfont->real_charset, string, len, ".");
  855. x11font_really_draw_text(x11font_drawfuncs + index + 0, ctx,
  856. &xfont->fonts[sfid], xfont->disp, x, y,
  857. sb->s, sb->len, shadowoffset,
  858. xfont->variable, cellwidth * mult);
  859. strbuf_free(sb);
  860. }
  861. }
  862. static void x11font_draw_combining(unifont_drawctx *ctx, unifont *font,
  863. int x, int y, const wchar_t *string,
  864. int len, bool wide, bool bold,
  865. int cellwidth)
  866. {
  867. /*
  868. * For server-side fonts, there's no sophisticated system for
  869. * combining characters intelligently, so the best we can do is to
  870. * overprint them on each other in the obvious way.
  871. */
  872. int i;
  873. for (i = 0; i < len; i++)
  874. x11font_draw_text(ctx, font, x, y, string+i, 1, wide, bold, cellwidth);
  875. }
  876. static void x11font_enum_fonts(GtkWidget *widget,
  877. fontsel_add_entry callback, void *callback_ctx)
  878. {
  879. Display *disp;
  880. char **fontnames;
  881. char *tmp = NULL;
  882. int nnames, i, max, tmpsize;
  883. if ((disp = get_x11_display()) == NULL)
  884. return;
  885. max = 32768;
  886. while (1) {
  887. fontnames = XListFonts(disp, "*", max, &nnames);
  888. if (nnames >= max) {
  889. XFreeFontNames(fontnames);
  890. max *= 2;
  891. } else
  892. break;
  893. }
  894. tmpsize = 0;
  895. for (i = 0; i < nnames; i++) {
  896. struct xlfd_decomposed *xlfd = xlfd_decompose(fontnames[i]);
  897. if (xlfd) {
  898. char *p, *font, *style, *stylekey, *charset;
  899. int weightkey, slantkey, setwidthkey;
  900. int thistmpsize;
  901. /*
  902. * Convert a dismembered XLFD into the format we'll be
  903. * using in the font selector.
  904. */
  905. thistmpsize = 4 * strlen(fontnames[i]) + 256;
  906. if (tmpsize < thistmpsize) {
  907. tmpsize = thistmpsize;
  908. tmp = sresize(tmp, tmpsize, char);
  909. }
  910. p = tmp;
  911. /*
  912. * Font name is in the form "family (foundry)". (This is
  913. * what the GTK 1.2 X font selector does, and it seems to
  914. * come out looking reasonably sensible.)
  915. */
  916. font = p;
  917. p += 1 + sprintf(p, "%s (%s)", xlfd->family_name, xlfd->foundry);
  918. /*
  919. * Character set.
  920. */
  921. charset = p;
  922. p += 1 + sprintf(p, "%s-%s", xlfd->charset_registry,
  923. xlfd->charset_encoding);
  924. /*
  925. * Style is a mixture of quite a lot of the fields,
  926. * with some strange formatting.
  927. */
  928. style = p;
  929. p += sprintf(p, "%s", xlfd->weight_name[0] ? xlfd->weight_name :
  930. "regular");
  931. if (!g_ascii_strcasecmp(xlfd->slant, "i"))
  932. p += sprintf(p, " italic");
  933. else if (!g_ascii_strcasecmp(xlfd->slant, "o"))
  934. p += sprintf(p, " oblique");
  935. else if (!g_ascii_strcasecmp(xlfd->slant, "ri"))
  936. p += sprintf(p, " reverse italic");
  937. else if (!g_ascii_strcasecmp(xlfd->slant, "ro"))
  938. p += sprintf(p, " reverse oblique");
  939. else if (!g_ascii_strcasecmp(xlfd->slant, "ot"))
  940. p += sprintf(p, " other-slant");
  941. if (xlfd->setwidth_name[0] &&
  942. g_ascii_strcasecmp(xlfd->setwidth_name, "normal"))
  943. p += sprintf(p, " %s", xlfd->setwidth_name);
  944. if (!g_ascii_strcasecmp(xlfd->spacing, "m"))
  945. p += sprintf(p, " [M]");
  946. if (!g_ascii_strcasecmp(xlfd->spacing, "c"))
  947. p += sprintf(p, " [C]");
  948. if (xlfd->add_style_name[0])
  949. p += sprintf(p, " %s", xlfd->add_style_name);
  950. /*
  951. * Style key is the same stuff as above, but with a
  952. * couple of transformations done on it to make it
  953. * sort more sensibly.
  954. */
  955. p++;
  956. stylekey = p;
  957. if (!g_ascii_strcasecmp(xlfd->weight_name, "medium") ||
  958. !g_ascii_strcasecmp(xlfd->weight_name, "regular") ||
  959. !g_ascii_strcasecmp(xlfd->weight_name, "normal") ||
  960. !g_ascii_strcasecmp(xlfd->weight_name, "book"))
  961. weightkey = 0;
  962. else if (!g_ascii_strncasecmp(xlfd->weight_name, "demi", 4) ||
  963. !g_ascii_strncasecmp(xlfd->weight_name, "semi", 4))
  964. weightkey = 1;
  965. else
  966. weightkey = 2;
  967. if (!g_ascii_strcasecmp(xlfd->slant, "r"))
  968. slantkey = 0;
  969. else if (!g_ascii_strncasecmp(xlfd->slant, "r", 1))
  970. slantkey = 2;
  971. else
  972. slantkey = 1;
  973. if (!g_ascii_strcasecmp(xlfd->setwidth_name, "normal"))
  974. setwidthkey = 0;
  975. else
  976. setwidthkey = 1;
  977. p += sprintf(
  978. p, "%04d%04d%s%04d%04d%s%04d%04d%s%04d%s%04d%s",
  979. weightkey, (int)strlen(xlfd->weight_name), xlfd->weight_name,
  980. slantkey, (int)strlen(xlfd->slant), xlfd->slant,
  981. setwidthkey,
  982. (int)strlen(xlfd->setwidth_name), xlfd->setwidth_name,
  983. (int)strlen(xlfd->spacing), xlfd->spacing,
  984. (int)strlen(xlfd->add_style_name), xlfd->add_style_name);
  985. assert(p - tmp < thistmpsize);
  986. /*
  987. * Flags: we need to know whether this is a monospaced
  988. * font, which we do by examining the spacing field
  989. * again.
  990. */
  991. int flags = FONTFLAG_SERVERSIDE;
  992. if (!strchr("CcMm", xlfd->spacing[0]))
  993. flags |= FONTFLAG_NONMONOSPACED;
  994. /*
  995. * Some fonts have a pixel size of zero, meaning they're
  996. * treated as scalable. For these purposes, we only want
  997. * fonts whose pixel size we actually know, so filter
  998. * those out.
  999. */
  1000. if (xlfd->pixel_size)
  1001. callback(callback_ctx, fontnames[i], font, charset,
  1002. style, stylekey, xlfd->pixel_size, flags,
  1003. &x11font_vtable);
  1004. sfree(xlfd);
  1005. } else {
  1006. /*
  1007. * This isn't an XLFD, so it must be an alias.
  1008. * Transmit it with mostly null data.
  1009. *
  1010. * It would be nice to work out if it's monospaced
  1011. * here, but at the moment I can't see that being
  1012. * anything but computationally hideous. Ah well.
  1013. */
  1014. callback(callback_ctx, fontnames[i], fontnames[i], NULL,
  1015. NULL, NULL, 0, FONTFLAG_SERVERALIAS, &x11font_vtable);
  1016. sfree(xlfd);
  1017. }
  1018. }
  1019. XFreeFontNames(fontnames);
  1020. sfree(tmp);
  1021. }
  1022. static char *x11font_canonify_fontname(GtkWidget *widget, const char *name,
  1023. int *size, int *flags,
  1024. bool resolve_aliases)
  1025. {
  1026. /*
  1027. * When given an X11 font name to try to make sense of for a
  1028. * font selector, we must attempt to load it (to see if it
  1029. * exists), and then canonify it by extracting its FONT
  1030. * property, which should give its full XLFD even if what we
  1031. * originally had was a wildcard.
  1032. *
  1033. * However, we must carefully avoid canonifying font
  1034. * _aliases_, unless specifically asked to, because the font
  1035. * selector treats them as worthwhile in their own right.
  1036. */
  1037. XFontStruct *xfs;
  1038. Display *disp;
  1039. Atom fontprop, fontprop2;
  1040. unsigned long ret;
  1041. if ((disp = get_x11_display()) == NULL)
  1042. return NULL;
  1043. xfs = XLoadQueryFont(disp, name);
  1044. if (!xfs)
  1045. return NULL; /* didn't make sense to us, sorry */
  1046. fontprop = XInternAtom(disp, "FONT", False);
  1047. if (XGetFontProperty(xfs, fontprop, &ret)) {
  1048. char *newname = XGetAtomName(disp, (Atom)ret);
  1049. if (newname) {
  1050. unsigned long fsize = 12;
  1051. fontprop2 = XInternAtom(disp, "PIXEL_SIZE", False);
  1052. if (XGetFontProperty(xfs, fontprop2, &fsize) && fsize > 0) {
  1053. *size = fsize;
  1054. XFreeFont(disp, xfs);
  1055. if (flags) {
  1056. if (name[0] == '-' || resolve_aliases)
  1057. *flags = FONTFLAG_SERVERSIDE;
  1058. else
  1059. *flags = FONTFLAG_SERVERALIAS;
  1060. }
  1061. return dupstr(name[0] == '-' || resolve_aliases ?
  1062. newname : name);
  1063. }
  1064. }
  1065. }
  1066. XFreeFont(disp, xfs);
  1067. return NULL; /* something went wrong */
  1068. }
  1069. static char *x11font_scale_fontname(GtkWidget *widget, const char *name,
  1070. int size)
  1071. {
  1072. return NULL; /* shan't */
  1073. }
  1074. static char *x11font_size_increment(unifont *font, int increment)
  1075. {
  1076. struct x11font *xfont = container_of(font, struct x11font, u);
  1077. Display *disp = xfont->disp;
  1078. Atom fontprop = XInternAtom(disp, "FONT", False);
  1079. char *returned_name = NULL;
  1080. unsigned long ret;
  1081. if (XGetFontProperty(xfont->fonts[0].xfs, fontprop, &ret)) {
  1082. struct xlfd_decomposed *xlfd;
  1083. struct xlfd_decomposed *xlfd_best;
  1084. char *wc;
  1085. char **fontnames;
  1086. int nnames, i, max;
  1087. xlfd = xlfd_decompose(XGetAtomName(disp, (Atom)ret));
  1088. if (!xlfd)
  1089. return NULL;
  1090. /*
  1091. * Form a wildcard consisting of everything in the
  1092. * original XLFD except for the size-related fields.
  1093. */
  1094. {
  1095. struct xlfd_decomposed xlfd_wc = *xlfd; /* structure copy */
  1096. xlfd_wc.pixel_size = XLFD_INT_WILDCARD;
  1097. xlfd_wc.point_size = XLFD_INT_WILDCARD;
  1098. xlfd_wc.average_width = XLFD_INT_WILDCARD;
  1099. wc = xlfd_recompose(&xlfd_wc);
  1100. }
  1101. /*
  1102. * Fetch all the font names matching that wildcard.
  1103. */
  1104. max = 32768;
  1105. while (1) {
  1106. fontnames = XListFonts(disp, wc, max, &nnames);
  1107. if (nnames >= max) {
  1108. XFreeFontNames(fontnames);
  1109. max *= 2;
  1110. } else
  1111. break;
  1112. }
  1113. sfree(wc);
  1114. /*
  1115. * Iterate over those to find the one closest in size to the
  1116. * original font, in the correct direction.
  1117. */
  1118. #define FLIPPED_SIZE(xlfd) \
  1119. (((xlfd)->pixel_size + (xlfd)->point_size) * \
  1120. (increment < 0 ? -1 : +1))
  1121. xlfd_best = NULL;
  1122. for (i = 0; i < nnames; i++) {
  1123. struct xlfd_decomposed *xlfd2 = xlfd_decompose(fontnames[i]);
  1124. if (!xlfd2)
  1125. continue;
  1126. if (xlfd2->pixel_size != 0 &&
  1127. FLIPPED_SIZE(xlfd2) > FLIPPED_SIZE(xlfd) &&
  1128. (!xlfd_best || FLIPPED_SIZE(xlfd2)<FLIPPED_SIZE(xlfd_best))) {
  1129. sfree(xlfd_best);
  1130. xlfd_best = xlfd2;
  1131. xlfd2 = NULL;
  1132. }
  1133. sfree(xlfd2);
  1134. }
  1135. #undef FLIPPED_SIZE
  1136. if (xlfd_best) {
  1137. char *bare_returned_name = xlfd_recompose(xlfd_best);
  1138. returned_name = dupcat(
  1139. xfont->u.vt->prefix, ":", bare_returned_name);
  1140. sfree(bare_returned_name);
  1141. }
  1142. XFreeFontNames(fontnames);
  1143. sfree(xlfd);
  1144. sfree(xlfd_best);
  1145. }
  1146. return returned_name;
  1147. }
  1148. #endif /* NOT_X_WINDOWS */
  1149. #if GTK_CHECK_VERSION(2,0,0)
  1150. /* ----------------------------------------------------------------------
  1151. * Pango font implementation (for GTK 2 only).
  1152. */
  1153. #if defined PANGO_PRE_1POINT4 && !defined PANGO_PRE_1POINT6
  1154. #define PANGO_PRE_1POINT6 /* make life easier for pre-1.4 folk */
  1155. #endif
  1156. static bool pangofont_has_glyph(unifont *font, wchar_t glyph);
  1157. static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
  1158. int x, int y, const wchar_t *string, int len,
  1159. bool wide, bool bold, int cellwidth);
  1160. static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1161. int x, int y, const wchar_t *string,
  1162. int len, bool wide, bool bold,
  1163. int cellwidth);
  1164. static unifont *pangofont_create(GtkWidget *widget, const char *name,
  1165. bool wide, bool bold,
  1166. int shadowoffset, bool shadowalways);
  1167. static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
  1168. bool wide, bool bold,
  1169. int shadowoffset, bool shadowalways);
  1170. static void pangofont_destroy(unifont *font);
  1171. static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
  1172. void *callback_ctx);
  1173. static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
  1174. int *size, int *flags,
  1175. bool resolve_aliases);
  1176. static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
  1177. int size);
  1178. static char *pangofont_size_increment(unifont *font, int increment);
  1179. struct pangofont {
  1180. /*
  1181. * Pango objects.
  1182. */
  1183. PangoFontDescription *desc;
  1184. PangoFontset *fset;
  1185. /*
  1186. * The containing widget.
  1187. */
  1188. GtkWidget *widget;
  1189. /*
  1190. * Data passed in to unifont_create().
  1191. */
  1192. int shadowoffset;
  1193. bool bold, shadowalways;
  1194. /*
  1195. * Cache of character widths, indexed by Unicode code point. In
  1196. * pixels; -1 means we haven't asked Pango about this character
  1197. * before.
  1198. */
  1199. int *widthcache;
  1200. unsigned nwidthcache;
  1201. struct unifont u;
  1202. };
  1203. static const UnifontVtable pangofont_vtable = {
  1204. .create = pangofont_create,
  1205. .create_fallback = pangofont_create_fallback,
  1206. .destroy = pangofont_destroy,
  1207. .has_glyph = pangofont_has_glyph,
  1208. .draw_text = pangofont_draw_text,
  1209. .draw_combining = pangofont_draw_combining,
  1210. .enum_fonts = pangofont_enum_fonts,
  1211. .canonify_fontname = pangofont_canonify_fontname,
  1212. .scale_fontname = pangofont_scale_fontname,
  1213. .size_increment = pangofont_size_increment,
  1214. .prefix = "client",
  1215. };
  1216. /*
  1217. * This function is used to rigorously validate a
  1218. * PangoFontDescription. Later versions of Pango have a nasty
  1219. * habit of accepting _any_ old string as input to
  1220. * pango_font_description_from_string and returning a font
  1221. * description which can actually be used to display text, even if
  1222. * they have to do it by falling back to their most default font.
  1223. * This is doubtless helpful in some situations, but not here,
  1224. * because we need to know if a Pango font string actually _makes
  1225. * sense_ in order to fall back to treating it as an X font name
  1226. * if it doesn't. So we check that the font family is actually one
  1227. * supported by Pango.
  1228. */
  1229. static bool pangofont_check_desc_makes_sense(PangoContext *ctx,
  1230. PangoFontDescription *desc)
  1231. {
  1232. #ifndef PANGO_PRE_1POINT6
  1233. PangoFontMap *map;
  1234. #endif
  1235. PangoFontFamily **families;
  1236. int i, nfamilies;
  1237. bool matched;
  1238. /*
  1239. * Ask Pango for a list of font families, and iterate through
  1240. * them to see if one of them matches the family in the
  1241. * PangoFontDescription.
  1242. */
  1243. #ifndef PANGO_PRE_1POINT6
  1244. map = pango_context_get_font_map(ctx);
  1245. if (!map)
  1246. return false;
  1247. pango_font_map_list_families(map, &families, &nfamilies);
  1248. #else
  1249. pango_context_list_families(ctx, &families, &nfamilies);
  1250. #endif
  1251. matched = false;
  1252. for (i = 0; i < nfamilies; i++) {
  1253. if (!g_ascii_strcasecmp(pango_font_family_get_name(families[i]),
  1254. pango_font_description_get_family(desc))) {
  1255. matched = true;
  1256. break;
  1257. }
  1258. }
  1259. g_free(families);
  1260. return matched;
  1261. }
  1262. static unifont *pangofont_create_internal(GtkWidget *widget,
  1263. PangoContext *ctx,
  1264. PangoFontDescription *desc,
  1265. bool wide, bool bold,
  1266. int shadowoffset, bool shadowalways)
  1267. {
  1268. struct pangofont *pfont;
  1269. #ifndef PANGO_PRE_1POINT6
  1270. PangoFontMap *map;
  1271. #endif
  1272. PangoFontset *fset;
  1273. PangoFontMetrics *metrics;
  1274. #ifndef PANGO_PRE_1POINT6
  1275. map = pango_context_get_font_map(ctx);
  1276. if (!map) {
  1277. pango_font_description_free(desc);
  1278. return NULL;
  1279. }
  1280. fset = pango_font_map_load_fontset(map, ctx, desc,
  1281. pango_context_get_language(ctx));
  1282. #else
  1283. fset = pango_context_load_fontset(ctx, desc,
  1284. pango_context_get_language(ctx));
  1285. #endif
  1286. if (!fset) {
  1287. pango_font_description_free(desc);
  1288. return NULL;
  1289. }
  1290. metrics = pango_fontset_get_metrics(fset);
  1291. if (!metrics ||
  1292. pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
  1293. pango_font_description_free(desc);
  1294. g_object_unref(fset);
  1295. return NULL;
  1296. }
  1297. pfont = snew(struct pangofont);
  1298. pfont->u.vt = &pangofont_vtable;
  1299. pfont->u.width =
  1300. PANGO_PIXELS(pango_font_metrics_get_approximate_digit_width(metrics));
  1301. pfont->u.ascent =
  1302. PANGO_PIXELS_CEIL(pango_font_metrics_get_ascent(metrics));
  1303. pfont->u.descent =
  1304. PANGO_PIXELS_CEIL(pango_font_metrics_get_descent(metrics));
  1305. pfont->u.height = pfont->u.ascent + pfont->u.descent;
  1306. pfont->u.strikethrough_y =
  1307. PANGO_PIXELS(pango_font_metrics_get_ascent(metrics) -
  1308. pango_font_metrics_get_strikethrough_position(metrics));
  1309. pfont->u.want_fallback = false;
  1310. #ifdef DRAW_TEXT_CAIRO
  1311. pfont->u.preferred_drawtype = DRAWTYPE_CAIRO;
  1312. #elif defined DRAW_TEXT_GDK
  1313. pfont->u.preferred_drawtype = DRAWTYPE_GDK;
  1314. #else
  1315. #error No drawtype available at all
  1316. #endif
  1317. /* The Pango API is hardwired to UTF-8 */
  1318. pfont->u.public_charset = CS_UTF8;
  1319. pfont->desc = desc;
  1320. pfont->fset = fset;
  1321. pfont->widget = widget;
  1322. pfont->bold = bold;
  1323. pfont->shadowoffset = shadowoffset;
  1324. pfont->shadowalways = shadowalways;
  1325. pfont->widthcache = NULL;
  1326. pfont->nwidthcache = 0;
  1327. pango_font_metrics_unref(metrics);
  1328. return &pfont->u;
  1329. }
  1330. static unifont *pangofont_create(GtkWidget *widget, const char *name,
  1331. bool wide, bool bold,
  1332. int shadowoffset, bool shadowalways)
  1333. {
  1334. PangoContext *ctx;
  1335. PangoFontDescription *desc;
  1336. desc = pango_font_description_from_string(name);
  1337. if (!desc)
  1338. return NULL;
  1339. ctx = gtk_widget_get_pango_context(widget);
  1340. if (!ctx) {
  1341. pango_font_description_free(desc);
  1342. return NULL;
  1343. }
  1344. if (!pangofont_check_desc_makes_sense(ctx, desc)) {
  1345. pango_font_description_free(desc);
  1346. return NULL;
  1347. }
  1348. return pangofont_create_internal(widget, ctx, desc, wide, bold,
  1349. shadowoffset, shadowalways);
  1350. }
  1351. static unifont *pangofont_create_fallback(GtkWidget *widget, int height,
  1352. bool wide, bool bold,
  1353. int shadowoffset, bool shadowalways)
  1354. {
  1355. PangoContext *ctx;
  1356. PangoFontDescription *desc;
  1357. desc = pango_font_description_from_string("Monospace");
  1358. if (!desc)
  1359. return NULL;
  1360. ctx = gtk_widget_get_pango_context(widget);
  1361. if (!ctx) {
  1362. pango_font_description_free(desc);
  1363. return NULL;
  1364. }
  1365. pango_font_description_set_absolute_size(desc, height * PANGO_SCALE);
  1366. return pangofont_create_internal(widget, ctx, desc, wide, bold,
  1367. shadowoffset, shadowalways);
  1368. }
  1369. static void pangofont_destroy(unifont *font)
  1370. {
  1371. struct pangofont *pfont = container_of(font, struct pangofont, u);
  1372. pango_font_description_free(pfont->desc);
  1373. sfree(pfont->widthcache);
  1374. g_object_unref(pfont->fset);
  1375. sfree(pfont);
  1376. }
  1377. static int pangofont_char_width(PangoLayout *layout, struct pangofont *pfont,
  1378. wchar_t uchr, const char *utfchr, int utflen)
  1379. {
  1380. /*
  1381. * Here we check whether a character has the same width as the
  1382. * character cell it'll be drawn in. Because profiling showed that
  1383. * asking Pango for text sizes was a huge bottleneck when we were
  1384. * calling it every time we needed to know this, we instead call
  1385. * it only on characters we don't already know about, and cache
  1386. * the results.
  1387. */
  1388. if ((unsigned)uchr >= pfont->nwidthcache) {
  1389. unsigned newsize = ((int)uchr + 0x100) & ~0xFF;
  1390. pfont->widthcache = sresize(pfont->widthcache, newsize, int);
  1391. while (pfont->nwidthcache < newsize)
  1392. pfont->widthcache[pfont->nwidthcache++] = -1;
  1393. }
  1394. if (pfont->widthcache[uchr] < 0) {
  1395. PangoRectangle rect;
  1396. pango_layout_set_text(layout, utfchr, utflen);
  1397. pango_layout_get_extents(layout, NULL, &rect);
  1398. pfont->widthcache[uchr] = rect.width;
  1399. }
  1400. return pfont->widthcache[uchr];
  1401. }
  1402. static bool pangofont_has_glyph(unifont *font, wchar_t glyph)
  1403. {
  1404. /* Pango implements font fallback, so assume it has everything */
  1405. return true;
  1406. }
  1407. #ifdef DRAW_TEXT_GDK
  1408. static void pango_gdk_draw_layout(unifont_drawctx *ctx,
  1409. gint x, gint y, PangoLayout *layout)
  1410. {
  1411. gdk_draw_layout(ctx->u.gdk.target, ctx->u.gdk.gc, x, y, layout);
  1412. }
  1413. #endif
  1414. #ifdef DRAW_TEXT_CAIRO
  1415. static void pango_cairo_draw_layout(unifont_drawctx *ctx,
  1416. gint x, gint y, PangoLayout *layout)
  1417. {
  1418. cairo_move_to(ctx->u.cairo.cr, x, y);
  1419. pango_cairo_show_layout(ctx->u.cairo.cr, layout);
  1420. }
  1421. #endif
  1422. static void pangofont_draw_internal(unifont_drawctx *ctx, unifont *font,
  1423. int x, int y, const wchar_t *string,
  1424. int len, bool wide, bool bold,
  1425. int cellwidth, bool combining)
  1426. {
  1427. struct pangofont *pfont = container_of(font, struct pangofont, u);
  1428. PangoLayout *layout;
  1429. PangoRectangle rect;
  1430. char *utfstring, *utfptr;
  1431. size_t utflen;
  1432. bool shadowbold = false;
  1433. void (*draw_layout)(unifont_drawctx *ctx,
  1434. gint x, gint y, PangoLayout *layout) = NULL;
  1435. #ifdef DRAW_TEXT_GDK
  1436. if (ctx->type == DRAWTYPE_GDK) {
  1437. draw_layout = pango_gdk_draw_layout;
  1438. }
  1439. #endif
  1440. #ifdef DRAW_TEXT_CAIRO
  1441. if (ctx->type == DRAWTYPE_CAIRO) {
  1442. draw_layout = pango_cairo_draw_layout;
  1443. }
  1444. #endif
  1445. assert(draw_layout);
  1446. if (wide)
  1447. cellwidth *= 2;
  1448. y -= pfont->u.ascent;
  1449. layout = pango_layout_new(gtk_widget_get_pango_context(pfont->widget));
  1450. pango_layout_set_font_description(layout, pfont->desc);
  1451. if (bold && !pfont->bold) {
  1452. if (pfont->shadowalways)
  1453. shadowbold = true;
  1454. else {
  1455. PangoFontDescription *desc2 =
  1456. pango_font_description_copy_static(pfont->desc);
  1457. pango_font_description_set_weight(desc2, PANGO_WEIGHT_BOLD);
  1458. pango_layout_set_font_description(layout, desc2);
  1459. }
  1460. }
  1461. /*
  1462. * Pango always expects UTF-8, so convert the input wide character
  1463. * string to UTF-8.
  1464. */
  1465. utfstring = dup_wc_to_mb_c(CS_UTF8, string, len, "", &utflen);
  1466. utfptr = utfstring;
  1467. while (utflen > 0) {
  1468. size_t clen, n;
  1469. int desired = cellwidth * PANGO_SCALE;
  1470. /*
  1471. * We want to display every character from this string in
  1472. * the centre of its own character cell. In the worst case,
  1473. * this requires a separate text-drawing call for each
  1474. * character; but in the common case where the font is
  1475. * properly fixed-width, we can draw many characters in one
  1476. * go which is much faster.
  1477. *
  1478. * This still isn't really ideal. If you look at what
  1479. * happens in the X protocol as a result of all of this, you
  1480. * find - naturally enough - that each call to
  1481. * gdk_draw_layout() generates a separate set of X RENDER
  1482. * operations involving creating a picture, setting a clip
  1483. * rectangle, doing some drawing and undoing the whole lot.
  1484. * In an ideal world, we should _always_ be able to turn the
  1485. * contents of this loop into a single RenderCompositeGlyphs
  1486. * operation which internally specifies inter-character
  1487. * deltas to get the spacing right, which would give us full
  1488. * speed _even_ in the worst case of a non-fixed-width font.
  1489. * However, Pango's architecture and documentation are so
  1490. * unhelpful that I have no idea how if at all to persuade
  1491. * them to do that.
  1492. */
  1493. if (combining) {
  1494. /*
  1495. * For a character with combining stuff, we just dump the
  1496. * whole lot in one go, and expect it to take up just one
  1497. * character cell.
  1498. */
  1499. clen = utflen;
  1500. n = 1;
  1501. } else {
  1502. /*
  1503. * Start by extracting a single UTF-8 character from the
  1504. * string.
  1505. */
  1506. clen = 1;
  1507. while (clen < utflen &&
  1508. (unsigned char)utfptr[clen] >= 0x80 &&
  1509. (unsigned char)utfptr[clen] < 0xC0)
  1510. clen++;
  1511. n = 1;
  1512. if (is_rtl(string[0]) ||
  1513. pangofont_char_width(layout, pfont, string[n-1],
  1514. utfptr, clen) != desired) {
  1515. /*
  1516. * If this character is a right-to-left one, or has an
  1517. * unusual width, then we must display it on its own.
  1518. */
  1519. } else {
  1520. /*
  1521. * Try to amalgamate a contiguous string of characters
  1522. * with the expected sensible width, for the common case
  1523. * in which we're using a monospaced font and everything
  1524. * works as expected.
  1525. */
  1526. while (clen < utflen) {
  1527. int oldclen = clen;
  1528. clen++; /* skip UTF-8 introducer byte */
  1529. while (clen < utflen &&
  1530. (unsigned char)utfptr[clen] >= 0x80 &&
  1531. (unsigned char)utfptr[clen] < 0xC0)
  1532. clen++;
  1533. n++;
  1534. if (is_rtl(string[n-1]) ||
  1535. pangofont_char_width(layout, pfont,
  1536. string[n-1], utfptr + oldclen,
  1537. clen - oldclen) != desired) {
  1538. clen = oldclen;
  1539. n--;
  1540. break;
  1541. }
  1542. }
  1543. }
  1544. }
  1545. pango_layout_set_text(layout, utfptr, clen);
  1546. pango_layout_get_pixel_extents(layout, NULL, &rect);
  1547. draw_layout(ctx,
  1548. x + (n*cellwidth - rect.width)/2,
  1549. y + (pfont->u.height - rect.height)/2, layout);
  1550. if (shadowbold)
  1551. draw_layout(ctx,
  1552. x + (n*cellwidth - rect.width)/2 + pfont->shadowoffset,
  1553. y + (pfont->u.height - rect.height)/2, layout);
  1554. utflen -= clen;
  1555. utfptr += clen;
  1556. string += n;
  1557. x += n * cellwidth;
  1558. }
  1559. sfree(utfstring);
  1560. g_object_unref(layout);
  1561. }
  1562. static void pangofont_draw_text(unifont_drawctx *ctx, unifont *font,
  1563. int x, int y, const wchar_t *string, int len,
  1564. bool wide, bool bold, int cellwidth)
  1565. {
  1566. pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
  1567. cellwidth, false);
  1568. }
  1569. static void pangofont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1570. int x, int y, const wchar_t *string,
  1571. int len, bool wide, bool bold,
  1572. int cellwidth)
  1573. {
  1574. wchar_t *tmpstring = NULL;
  1575. if (mk_wcwidth(string[0]) == 0) {
  1576. /*
  1577. * If we've been told to draw a sequence of _only_ combining
  1578. * characters, prefix a space so that they have something to
  1579. * combine with.
  1580. */
  1581. tmpstring = snewn(len+1, wchar_t);
  1582. memcpy(tmpstring+1, string, len * sizeof(wchar_t));
  1583. tmpstring[0] = L' ';
  1584. string = tmpstring;
  1585. len++;
  1586. }
  1587. pangofont_draw_internal(ctx, font, x, y, string, len, wide, bold,
  1588. cellwidth, true);
  1589. sfree(tmpstring);
  1590. }
  1591. /*
  1592. * Dummy size value to be used when converting a
  1593. * PangoFontDescription of a scalable font to a string for
  1594. * internal use.
  1595. */
  1596. #define PANGO_DUMMY_SIZE 12
  1597. static void pangofont_enum_fonts(GtkWidget *widget, fontsel_add_entry callback,
  1598. void *callback_ctx)
  1599. {
  1600. PangoContext *ctx;
  1601. #ifndef PANGO_PRE_1POINT6
  1602. PangoFontMap *map;
  1603. #endif
  1604. PangoFontFamily **families;
  1605. int i, nfamilies;
  1606. ctx = gtk_widget_get_pango_context(widget);
  1607. if (!ctx)
  1608. return;
  1609. /*
  1610. * Ask Pango for a list of font families, and iterate through
  1611. * them.
  1612. */
  1613. #ifndef PANGO_PRE_1POINT6
  1614. map = pango_context_get_font_map(ctx);
  1615. if (!map)
  1616. return;
  1617. pango_font_map_list_families(map, &families, &nfamilies);
  1618. #else
  1619. pango_context_list_families(ctx, &families, &nfamilies);
  1620. #endif
  1621. for (i = 0; i < nfamilies; i++) {
  1622. PangoFontFamily *family = families[i];
  1623. const char *familyname;
  1624. int flags;
  1625. PangoFontFace **faces;
  1626. int j, nfaces;
  1627. /*
  1628. * Set up our flags for this font family, and get the name
  1629. * string.
  1630. */
  1631. flags = FONTFLAG_CLIENTSIDE;
  1632. #ifndef PANGO_PRE_1POINT4
  1633. /*
  1634. * In very early versions of Pango, we can't tell
  1635. * monospaced fonts from non-monospaced.
  1636. */
  1637. if (!pango_font_family_is_monospace(family))
  1638. flags |= FONTFLAG_NONMONOSPACED;
  1639. #endif
  1640. familyname = pango_font_family_get_name(family);
  1641. /*
  1642. * Go through the available font faces in this family.
  1643. */
  1644. pango_font_family_list_faces(family, &faces, &nfaces);
  1645. for (j = 0; j < nfaces; j++) {
  1646. PangoFontFace *face = faces[j];
  1647. PangoFontDescription *desc;
  1648. const char *facename;
  1649. int *sizes;
  1650. int k, nsizes, dummysize;
  1651. /*
  1652. * Get the face name string.
  1653. */
  1654. facename = pango_font_face_get_face_name(face);
  1655. /*
  1656. * Set up a font description with what we've got so
  1657. * far. We'll fill in the size field manually and then
  1658. * call pango_font_description_to_string() to give the
  1659. * full real name of the specific font.
  1660. */
  1661. desc = pango_font_face_describe(face);
  1662. /*
  1663. * See if this font has a list of specific sizes.
  1664. */
  1665. #ifndef PANGO_PRE_1POINT4
  1666. pango_font_face_list_sizes(face, &sizes, &nsizes);
  1667. #else
  1668. /*
  1669. * In early versions of Pango, that call wasn't
  1670. * supported; we just have to assume everything is
  1671. * scalable.
  1672. */
  1673. sizes = NULL;
  1674. #endif
  1675. if (!sizes) {
  1676. /*
  1677. * Write a single entry with a dummy size.
  1678. */
  1679. dummysize = PANGO_DUMMY_SIZE * PANGO_SCALE;
  1680. sizes = &dummysize;
  1681. nsizes = 1;
  1682. }
  1683. /*
  1684. * If so, go through them one by one.
  1685. */
  1686. for (k = 0; k < nsizes; k++) {
  1687. char *fullname, *stylekey;
  1688. pango_font_description_set_size(desc, sizes[k]);
  1689. fullname = pango_font_description_to_string(desc);
  1690. /*
  1691. * Construct the sorting key for font styles.
  1692. */
  1693. {
  1694. strbuf *buf = strbuf_new();
  1695. int weight = pango_font_description_get_weight(desc);
  1696. /* Weight: normal, then lighter, then bolder */
  1697. if (weight <= PANGO_WEIGHT_NORMAL)
  1698. weight = PANGO_WEIGHT_NORMAL - weight;
  1699. put_fmt(buf, "%4d", weight);
  1700. put_fmt(buf, " %2d",
  1701. pango_font_description_get_style(desc));
  1702. int stretch = pango_font_description_get_stretch(desc);
  1703. /* Stretch: closer to normal sorts earlier */
  1704. stretch = 2 * abs(PANGO_STRETCH_NORMAL - stretch) +
  1705. (stretch < PANGO_STRETCH_NORMAL);
  1706. put_fmt(buf, " %2d", stretch);
  1707. put_fmt(buf, " %2d",
  1708. pango_font_description_get_variant(desc));
  1709. stylekey = strbuf_to_str(buf);
  1710. }
  1711. /*
  1712. * Got everything. Hand off to the callback.
  1713. * (The charset string is NULL, because only
  1714. * server-side X fonts use it.)
  1715. */
  1716. callback(callback_ctx, fullname, familyname, NULL, facename,
  1717. stylekey,
  1718. (sizes == &dummysize ? 0 : PANGO_PIXELS(sizes[k])),
  1719. flags, &pangofont_vtable);
  1720. sfree(stylekey);
  1721. g_free(fullname);
  1722. }
  1723. if (sizes != &dummysize)
  1724. g_free(sizes);
  1725. pango_font_description_free(desc);
  1726. }
  1727. g_free(faces);
  1728. }
  1729. g_free(families);
  1730. }
  1731. static char *pangofont_canonify_fontname(GtkWidget *widget, const char *name,
  1732. int *size, int *flags,
  1733. bool resolve_aliases)
  1734. {
  1735. /*
  1736. * When given a Pango font name to try to make sense of for a
  1737. * font selector, we must normalise it to PANGO_DUMMY_SIZE and
  1738. * extract its original size (in pixels) into the `size' field.
  1739. */
  1740. PangoContext *ctx;
  1741. #ifndef PANGO_PRE_1POINT6
  1742. PangoFontMap *map;
  1743. #endif
  1744. PangoFontDescription *desc;
  1745. PangoFontset *fset;
  1746. PangoFontMetrics *metrics;
  1747. char *newname, *retname;
  1748. desc = pango_font_description_from_string(name);
  1749. if (!desc)
  1750. return NULL;
  1751. ctx = gtk_widget_get_pango_context(widget);
  1752. if (!ctx) {
  1753. pango_font_description_free(desc);
  1754. return NULL;
  1755. }
  1756. if (!pangofont_check_desc_makes_sense(ctx, desc)) {
  1757. pango_font_description_free(desc);
  1758. return NULL;
  1759. }
  1760. #ifndef PANGO_PRE_1POINT6
  1761. map = pango_context_get_font_map(ctx);
  1762. if (!map) {
  1763. pango_font_description_free(desc);
  1764. return NULL;
  1765. }
  1766. fset = pango_font_map_load_fontset(map, ctx, desc,
  1767. pango_context_get_language(ctx));
  1768. #else
  1769. fset = pango_context_load_fontset(ctx, desc,
  1770. pango_context_get_language(ctx));
  1771. #endif
  1772. if (!fset) {
  1773. pango_font_description_free(desc);
  1774. return NULL;
  1775. }
  1776. metrics = pango_fontset_get_metrics(fset);
  1777. if (!metrics ||
  1778. pango_font_metrics_get_approximate_digit_width(metrics) == 0) {
  1779. pango_font_description_free(desc);
  1780. g_object_unref(fset);
  1781. return NULL;
  1782. }
  1783. *size = PANGO_PIXELS(pango_font_description_get_size(desc));
  1784. *flags = FONTFLAG_CLIENTSIDE;
  1785. pango_font_description_set_size(desc, PANGO_DUMMY_SIZE * PANGO_SCALE);
  1786. newname = pango_font_description_to_string(desc);
  1787. retname = dupstr(newname);
  1788. g_free(newname);
  1789. pango_font_metrics_unref(metrics);
  1790. pango_font_description_free(desc);
  1791. g_object_unref(fset);
  1792. return retname;
  1793. }
  1794. static char *pangofont_scale_fontname(GtkWidget *widget, const char *name,
  1795. int size)
  1796. {
  1797. PangoFontDescription *desc;
  1798. char *newname, *retname;
  1799. desc = pango_font_description_from_string(name);
  1800. if (!desc)
  1801. return NULL;
  1802. pango_font_description_set_size(desc, size * PANGO_SCALE);
  1803. newname = pango_font_description_to_string(desc);
  1804. retname = dupstr(newname);
  1805. g_free(newname);
  1806. pango_font_description_free(desc);
  1807. return retname;
  1808. }
  1809. static char *pangofont_size_increment(unifont *font, int increment)
  1810. {
  1811. struct pangofont *pfont = container_of(font, struct pangofont, u);
  1812. PangoFontDescription *desc;
  1813. int size;
  1814. char *newname, *retname;
  1815. desc = pango_font_description_copy_static(pfont->desc);
  1816. size = pango_font_description_get_size(desc);
  1817. size += PANGO_SCALE * increment;
  1818. if (size <= 0) {
  1819. retname = NULL;
  1820. } else {
  1821. pango_font_description_set_size(desc, size);
  1822. newname = pango_font_description_to_string(desc);
  1823. retname = dupcat(pfont->u.vt->prefix, ":", newname);
  1824. g_free(newname);
  1825. }
  1826. pango_font_description_free(desc);
  1827. return retname;
  1828. }
  1829. #endif /* GTK_CHECK_VERSION(2,0,0) */
  1830. /* ----------------------------------------------------------------------
  1831. * Outermost functions which do the vtable dispatch.
  1832. */
  1833. /*
  1834. * Complete list of font-type subclasses. Listed in preference
  1835. * order for unifont_create(). (That is, in the extremely unlikely
  1836. * event that the same font name is valid as both a Pango and an
  1837. * X11 font, it will be interpreted as the former in the absence
  1838. * of an explicit type-disambiguating prefix.)
  1839. *
  1840. * The 'multifont' subclass is omitted here, as discussed above.
  1841. */
  1842. static const struct UnifontVtable *unifont_types[] = {
  1843. #if GTK_CHECK_VERSION(2,0,0)
  1844. &pangofont_vtable,
  1845. #endif
  1846. #ifndef NOT_X_WINDOWS
  1847. &x11font_vtable,
  1848. #endif
  1849. };
  1850. /*
  1851. * Function which takes a font name and processes the optional
  1852. * scheme prefix. Returns the tail of the font name suitable for
  1853. * passing to individual font scheme functions, and also provides
  1854. * a subrange of the unifont_types[] array above.
  1855. *
  1856. * The return values `start' and `end' denote a half-open interval
  1857. * in unifont_types[]; that is, the correct way to iterate over
  1858. * them is
  1859. *
  1860. * for (i = start; i < end; i++) {...}
  1861. */
  1862. static const char *unifont_do_prefix(const char *name, int *start, int *end)
  1863. {
  1864. int colonpos = strcspn(name, ":");
  1865. int i;
  1866. if (name[colonpos]) {
  1867. /*
  1868. * There's a colon prefix on the font name. Use it to work
  1869. * out which subclass to use.
  1870. */
  1871. for (i = 0; i < lenof(unifont_types); i++) {
  1872. if (strlen(unifont_types[i]->prefix) == colonpos &&
  1873. !strncmp(unifont_types[i]->prefix, name, colonpos)) {
  1874. *start = i;
  1875. *end = i+1;
  1876. return name + colonpos + 1;
  1877. }
  1878. }
  1879. /*
  1880. * None matched, so return an empty scheme list to prevent
  1881. * any scheme from being called at all.
  1882. */
  1883. *start = *end = 0;
  1884. return name + colonpos + 1;
  1885. } else {
  1886. /*
  1887. * No colon prefix, so just use all the subclasses.
  1888. */
  1889. *start = 0;
  1890. *end = lenof(unifont_types);
  1891. return name;
  1892. }
  1893. }
  1894. unifont *unifont_create(GtkWidget *widget, const char *name, bool wide,
  1895. bool bold, int shadowoffset, bool shadowalways)
  1896. {
  1897. int i, start, end;
  1898. name = unifont_do_prefix(name, &start, &end);
  1899. for (i = start; i < end; i++) {
  1900. unifont *ret = unifont_types[i]->create(widget, name, wide, bold,
  1901. shadowoffset, shadowalways);
  1902. if (ret)
  1903. return ret;
  1904. }
  1905. return NULL; /* font not found in any scheme */
  1906. }
  1907. void unifont_destroy(unifont *font)
  1908. {
  1909. font->vt->destroy(font);
  1910. }
  1911. void unifont_draw_text(unifont_drawctx *ctx, unifont *font,
  1912. int x, int y, const wchar_t *string, int len,
  1913. bool wide, bool bold, int cellwidth)
  1914. {
  1915. font->vt->draw_text(ctx, font, x, y, string, len, wide, bold, cellwidth);
  1916. }
  1917. void unifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1918. int x, int y, const wchar_t *string, int len,
  1919. bool wide, bool bold, int cellwidth)
  1920. {
  1921. font->vt->draw_combining(ctx, font, x, y, string, len, wide, bold,
  1922. cellwidth);
  1923. }
  1924. char *unifont_size_increment(unifont *font, int increment)
  1925. {
  1926. return font->vt->size_increment(font, increment);
  1927. }
  1928. /* ----------------------------------------------------------------------
  1929. * Multiple-font wrapper. This is a type of unifont which encapsulates
  1930. * up to two other unifonts, permitting missing glyphs in the main
  1931. * font to be filled in by a fallback font.
  1932. *
  1933. * This is a type of unifont just like the previous two, but it has a
  1934. * separate constructor which is manually called by the client, so it
  1935. * doesn't appear in the list of available font types enumerated by
  1936. * unifont_create. This means it's not used by unifontsel either, so
  1937. * it doesn't need to support any methods except draw_text and
  1938. * destroy.
  1939. */
  1940. static void multifont_draw_text(unifont_drawctx *ctx, unifont *font,
  1941. int x, int y, const wchar_t *string, int len,
  1942. bool wide, bool bold, int cellwidth);
  1943. static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  1944. int x, int y, const wchar_t *string,
  1945. int len, bool wide, bool bold,
  1946. int cellwidth);
  1947. static void multifont_destroy(unifont *font);
  1948. static char *multifont_size_increment(unifont *font, int increment);
  1949. struct multifont {
  1950. unifont *main;
  1951. unifont *fallback;
  1952. struct unifont u;
  1953. };
  1954. static const UnifontVtable multifont_vtable = {
  1955. .create = NULL, /* creation is done specially */
  1956. .create_fallback = NULL,
  1957. .destroy = multifont_destroy,
  1958. .has_glyph = NULL,
  1959. .draw_text = multifont_draw_text,
  1960. .draw_combining = multifont_draw_combining,
  1961. .enum_fonts = NULL,
  1962. .canonify_fontname = NULL,
  1963. .scale_fontname = NULL,
  1964. .size_increment = multifont_size_increment,
  1965. .prefix = "client",
  1966. };
  1967. unifont *multifont_create(GtkWidget *widget, const char *name,
  1968. bool wide, bool bold,
  1969. int shadowoffset, bool shadowalways)
  1970. {
  1971. int i;
  1972. unifont *font, *fallback;
  1973. struct multifont *mfont;
  1974. font = unifont_create(widget, name, wide, bold,
  1975. shadowoffset, shadowalways);
  1976. if (!font)
  1977. return NULL;
  1978. fallback = NULL;
  1979. if (font->want_fallback) {
  1980. for (i = 0; i < lenof(unifont_types); i++) {
  1981. if (unifont_types[i]->create_fallback) {
  1982. fallback = unifont_types[i]->create_fallback(
  1983. widget, font->height, wide, bold,
  1984. shadowoffset, shadowalways);
  1985. if (fallback)
  1986. break;
  1987. }
  1988. }
  1989. }
  1990. /*
  1991. * Construct our multifont. Public members are all copied from the
  1992. * primary font we're wrapping.
  1993. */
  1994. mfont = snew(struct multifont);
  1995. mfont->u.vt = &multifont_vtable;
  1996. mfont->u.width = font->width;
  1997. mfont->u.ascent = font->ascent;
  1998. mfont->u.descent = font->descent;
  1999. mfont->u.height = font->height;
  2000. mfont->u.strikethrough_y = font->strikethrough_y;
  2001. mfont->u.public_charset = font->public_charset;
  2002. mfont->u.want_fallback = false; /* shouldn't be needed, but just in case */
  2003. mfont->u.preferred_drawtype = font->preferred_drawtype;
  2004. mfont->main = font;
  2005. mfont->fallback = fallback;
  2006. return &mfont->u;
  2007. }
  2008. static void multifont_destroy(unifont *font)
  2009. {
  2010. struct multifont *mfont = container_of(font, struct multifont, u);
  2011. unifont_destroy(mfont->main);
  2012. if (mfont->fallback)
  2013. unifont_destroy(mfont->fallback);
  2014. sfree(mfont);
  2015. }
  2016. typedef void (*unifont_draw_func_t)(unifont_drawctx *ctx, unifont *font,
  2017. int x, int y, const wchar_t *string,
  2018. int len, bool wide, bool bold,
  2019. int cellwidth);
  2020. static void multifont_draw_main(unifont_drawctx *ctx, unifont *font, int x,
  2021. int y, const wchar_t *string, int len,
  2022. bool wide, bool bold, int cellwidth,
  2023. int cellinc, unifont_draw_func_t draw)
  2024. {
  2025. struct multifont *mfont = container_of(font, struct multifont, u);
  2026. unifont *f;
  2027. bool ok;
  2028. int i;
  2029. while (len > 0) {
  2030. /*
  2031. * Find a maximal sequence of characters which are, or are
  2032. * not, supported by our main font.
  2033. */
  2034. ok = mfont->main->vt->has_glyph(mfont->main, string[0]);
  2035. for (i = 1;
  2036. i < len &&
  2037. !mfont->main->vt->has_glyph(mfont->main, string[i]) == !ok;
  2038. i++);
  2039. /*
  2040. * Now display it.
  2041. */
  2042. f = ok ? mfont->main : mfont->fallback;
  2043. if (f)
  2044. draw(ctx, f, x, y, string, i, wide, bold, cellwidth);
  2045. string += i;
  2046. len -= i;
  2047. x += i * cellinc;
  2048. }
  2049. }
  2050. static void multifont_draw_text(unifont_drawctx *ctx, unifont *font, int x,
  2051. int y, const wchar_t *string, int len,
  2052. bool wide, bool bold, int cellwidth)
  2053. {
  2054. multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
  2055. cellwidth, cellwidth, unifont_draw_text);
  2056. }
  2057. static void multifont_draw_combining(unifont_drawctx *ctx, unifont *font,
  2058. int x, int y, const wchar_t *string,
  2059. int len, bool wide, bool bold,
  2060. int cellwidth)
  2061. {
  2062. multifont_draw_main(ctx, font, x, y, string, len, wide, bold,
  2063. cellwidth, 0, unifont_draw_combining);
  2064. }
  2065. static char *multifont_size_increment(unifont *font, int increment)
  2066. {
  2067. struct multifont *mfont = container_of(font, struct multifont, u);
  2068. return unifont_size_increment(mfont->main, increment);
  2069. }
  2070. #if GTK_CHECK_VERSION(2,0,0)
  2071. /* ----------------------------------------------------------------------
  2072. * Implementation of a unified font selector. Used on GTK 2 only;
  2073. * for GTK 1 we still use the standard font selector.
  2074. */
  2075. typedef struct fontinfo fontinfo;
  2076. typedef struct unifontsel_internal {
  2077. GtkListStore *family_model, *style_model, *size_model;
  2078. GtkWidget *family_list, *style_list, *size_entry, *size_list;
  2079. GtkWidget *filter_buttons[4];
  2080. int n_filter_buttons;
  2081. GtkWidget *preview_area;
  2082. #ifndef NO_BACKING_PIXMAPS
  2083. GdkPixmap *preview_pixmap;
  2084. #endif
  2085. int preview_width, preview_height;
  2086. GdkColor preview_fg, preview_bg;
  2087. int filter_flags;
  2088. tree234 *fonts_by_realname, *fonts_by_selorder;
  2089. fontinfo *selected;
  2090. int selsize, intendedsize;
  2091. bool inhibit_response; /* inhibit callbacks when we change GUI controls */
  2092. unifontsel u;
  2093. } unifontsel_internal;
  2094. /*
  2095. * The structure held in the tree234s. All the string members are
  2096. * part of the same allocated area, so don't need freeing
  2097. * separately.
  2098. */
  2099. struct fontinfo {
  2100. char *realname;
  2101. char *family, *charset, *style, *stylekey;
  2102. int size, flags;
  2103. /*
  2104. * Fallback sorting key, to permit multiple identical entries
  2105. * to exist in the selorder tree.
  2106. */
  2107. int index;
  2108. /*
  2109. * Indices mapping fontinfo structures to indices in the list
  2110. * boxes. sizeindex is irrelevant if the font is scalable
  2111. * (size==0).
  2112. */
  2113. int familyindex, styleindex, sizeindex;
  2114. /*
  2115. * The class of font.
  2116. */
  2117. const struct UnifontVtable *fontclass;
  2118. };
  2119. struct fontinfo_realname_find {
  2120. const char *realname;
  2121. int flags;
  2122. };
  2123. static int strnullcasecmp(const char *a, const char *b)
  2124. {
  2125. int i;
  2126. /*
  2127. * If exactly one of the inputs is NULL, it compares before
  2128. * the other one.
  2129. */
  2130. if ((i = (!b) - (!a)) != 0)
  2131. return i;
  2132. /*
  2133. * NULL compares equal.
  2134. */
  2135. if (!a)
  2136. return 0;
  2137. /*
  2138. * Otherwise, ordinary strcasecmp.
  2139. */
  2140. return g_ascii_strcasecmp(a, b);
  2141. }
  2142. static int fontinfo_realname_compare(void *av, void *bv)
  2143. {
  2144. fontinfo *a = (fontinfo *)av;
  2145. fontinfo *b = (fontinfo *)bv;
  2146. int i;
  2147. if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
  2148. return i;
  2149. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  2150. return ((a->flags & FONTFLAG_SORT_MASK) <
  2151. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  2152. return 0;
  2153. }
  2154. static int fontinfo_realname_find(void *av, void *bv)
  2155. {
  2156. struct fontinfo_realname_find *a = (struct fontinfo_realname_find *)av;
  2157. fontinfo *b = (fontinfo *)bv;
  2158. int i;
  2159. if ((i = strnullcasecmp(a->realname, b->realname)) != 0)
  2160. return i;
  2161. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  2162. return ((a->flags & FONTFLAG_SORT_MASK) <
  2163. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  2164. return 0;
  2165. }
  2166. static int fontinfo_selorder_compare(void *av, void *bv)
  2167. {
  2168. fontinfo *a = (fontinfo *)av;
  2169. fontinfo *b = (fontinfo *)bv;
  2170. int i;
  2171. if ((i = strnullcasecmp(a->family, b->family)) != 0)
  2172. return i;
  2173. /*
  2174. * Font class comes immediately after family, so that fonts
  2175. * from different classes with the same family
  2176. */
  2177. if ((a->flags & FONTFLAG_SORT_MASK) != (b->flags & FONTFLAG_SORT_MASK))
  2178. return ((a->flags & FONTFLAG_SORT_MASK) <
  2179. (b->flags & FONTFLAG_SORT_MASK) ? -1 : +1);
  2180. if ((i = strnullcasecmp(a->charset, b->charset)) != 0)
  2181. return i;
  2182. if ((i = strnullcasecmp(a->stylekey, b->stylekey)) != 0)
  2183. return i;
  2184. if ((i = strnullcasecmp(a->style, b->style)) != 0)
  2185. return i;
  2186. if (a->size != b->size)
  2187. return (a->size < b->size ? -1 : +1);
  2188. if (a->index != b->index)
  2189. return (a->index < b->index ? -1 : +1);
  2190. return 0;
  2191. }
  2192. static void unifontsel_draw_preview_text(unifontsel_internal *fs);
  2193. static void unifontsel_deselect(unifontsel_internal *fs)
  2194. {
  2195. fs->selected = NULL;
  2196. gtk_list_store_clear(fs->style_model);
  2197. gtk_list_store_clear(fs->size_model);
  2198. gtk_widget_set_sensitive(fs->u.ok_button, false);
  2199. gtk_widget_set_sensitive(fs->size_entry, false);
  2200. unifontsel_draw_preview_text(fs);
  2201. }
  2202. static void unifontsel_setup_familylist(unifontsel_internal *fs)
  2203. {
  2204. GtkTreeIter iter;
  2205. int i, listindex, minpos = -1, maxpos = -1;
  2206. char *currfamily = NULL;
  2207. int currflags = -1;
  2208. fontinfo *info;
  2209. fs->inhibit_response = true;
  2210. gtk_list_store_clear(fs->family_model);
  2211. listindex = 0;
  2212. /*
  2213. * Search through the font tree for anything matching our
  2214. * current filter criteria. When we find one, add its font
  2215. * name to the list box.
  2216. */
  2217. for (i = 0 ;; i++) {
  2218. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2219. /*
  2220. * info may be NULL if we've just run off the end of the
  2221. * tree. We must still do a processing pass in that
  2222. * situation, in case we had an unfinished font record in
  2223. * progress.
  2224. */
  2225. if (info && (info->flags &~ fs->filter_flags)) {
  2226. info->familyindex = -1;
  2227. continue; /* we're filtering out this font */
  2228. }
  2229. if (!info || strnullcasecmp(currfamily, info->family) ||
  2230. currflags != (info->flags & FONTFLAG_SORT_MASK)) {
  2231. /*
  2232. * We've either finished a family, or started a new
  2233. * one, or both.
  2234. */
  2235. if (currfamily) {
  2236. gtk_list_store_append(fs->family_model, &iter);
  2237. gtk_list_store_set(fs->family_model, &iter,
  2238. 0, currfamily, 1, minpos, 2, maxpos+1, -1);
  2239. listindex++;
  2240. }
  2241. if (info) {
  2242. minpos = i;
  2243. currfamily = info->family;
  2244. currflags = info->flags & FONTFLAG_SORT_MASK;
  2245. }
  2246. }
  2247. if (!info)
  2248. break; /* now we're done */
  2249. info->familyindex = listindex;
  2250. maxpos = i;
  2251. }
  2252. /*
  2253. * If we've just filtered out the previously selected font,
  2254. * deselect it thoroughly.
  2255. */
  2256. if (fs->selected && fs->selected->familyindex < 0)
  2257. unifontsel_deselect(fs);
  2258. fs->inhibit_response = false;
  2259. }
  2260. static void unifontsel_setup_stylelist(unifontsel_internal *fs,
  2261. int start, int end)
  2262. {
  2263. GtkTreeIter iter;
  2264. int i, listindex, minpos = -1, maxpos = -1;
  2265. bool started = false;
  2266. char *currcs = NULL, *currstyle = NULL;
  2267. fontinfo *info;
  2268. gtk_list_store_clear(fs->style_model);
  2269. listindex = 0;
  2270. started = false;
  2271. /*
  2272. * Search through the font tree for anything matching our
  2273. * current filter criteria. When we find one, add its charset
  2274. * and/or style name to the list box.
  2275. */
  2276. for (i = start; i <= end; i++) {
  2277. if (i == end)
  2278. info = NULL;
  2279. else
  2280. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2281. /*
  2282. * info may be NULL if we've just run off the end of the
  2283. * relevant data. We must still do a processing pass in
  2284. * that situation, in case we had an unfinished font
  2285. * record in progress.
  2286. */
  2287. if (info && (info->flags &~ fs->filter_flags)) {
  2288. info->styleindex = -1;
  2289. continue; /* we're filtering out this font */
  2290. }
  2291. if (!info || !started || strnullcasecmp(currcs, info->charset) ||
  2292. strnullcasecmp(currstyle, info->style)) {
  2293. /*
  2294. * We've either finished a style/charset, or started a
  2295. * new one, or both.
  2296. */
  2297. started = true;
  2298. if (currstyle) {
  2299. gtk_list_store_append(fs->style_model, &iter);
  2300. gtk_list_store_set(fs->style_model, &iter,
  2301. 0, currstyle, 1, minpos, 2, maxpos+1,
  2302. 3, true, 4, PANGO_WEIGHT_NORMAL, -1);
  2303. listindex++;
  2304. }
  2305. if (info) {
  2306. minpos = i;
  2307. if (info->charset && strnullcasecmp(currcs, info->charset)) {
  2308. gtk_list_store_append(fs->style_model, &iter);
  2309. gtk_list_store_set(fs->style_model, &iter,
  2310. 0, info->charset, 1, -1, 2, -1,
  2311. 3, false, 4, PANGO_WEIGHT_BOLD, -1);
  2312. listindex++;
  2313. }
  2314. currcs = info->charset;
  2315. currstyle = info->style;
  2316. }
  2317. }
  2318. if (!info)
  2319. break; /* now we're done */
  2320. info->styleindex = listindex;
  2321. maxpos = i;
  2322. }
  2323. }
  2324. static const int unifontsel_default_sizes[] = { 10, 12, 14, 16, 20, 24, 32 };
  2325. static void unifontsel_setup_sizelist(unifontsel_internal *fs,
  2326. int start, int end)
  2327. {
  2328. GtkTreeIter iter;
  2329. int i, listindex;
  2330. char sizetext[40];
  2331. fontinfo *info;
  2332. gtk_list_store_clear(fs->size_model);
  2333. listindex = 0;
  2334. /*
  2335. * Search through the font tree for anything matching our
  2336. * current filter criteria. When we find one, add its font
  2337. * name to the list box.
  2338. */
  2339. for (i = start; i < end; i++) {
  2340. info = (fontinfo *)index234(fs->fonts_by_selorder, i);
  2341. if (!info) {
  2342. /* _shouldn't_ happen unless font list is completely funted */
  2343. break;
  2344. }
  2345. if (info->flags &~ fs->filter_flags) {
  2346. info->sizeindex = -1;
  2347. continue; /* we're filtering out this font */
  2348. }
  2349. if (info->size) {
  2350. sprintf(sizetext, "%d", info->size);
  2351. info->sizeindex = listindex;
  2352. gtk_list_store_append(fs->size_model, &iter);
  2353. gtk_list_store_set(fs->size_model, &iter,
  2354. 0, sizetext, 1, i, 2, info->size, -1);
  2355. listindex++;
  2356. } else {
  2357. int j;
  2358. assert(i == start);
  2359. assert(i+1 == end);
  2360. for (j = 0; j < lenof(unifontsel_default_sizes); j++) {
  2361. sprintf(sizetext, "%d", unifontsel_default_sizes[j]);
  2362. gtk_list_store_append(fs->size_model, &iter);
  2363. gtk_list_store_set(fs->size_model, &iter, 0, sizetext, 1, i,
  2364. 2, unifontsel_default_sizes[j], -1);
  2365. listindex++;
  2366. }
  2367. }
  2368. }
  2369. }
  2370. static void unifontsel_set_filter_buttons(unifontsel_internal *fs)
  2371. {
  2372. int i;
  2373. for (i = 0; i < fs->n_filter_buttons; i++) {
  2374. int flagbit = GPOINTER_TO_INT(g_object_get_data(
  2375. G_OBJECT(fs->filter_buttons[i]),
  2376. "user-data"));
  2377. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(fs->filter_buttons[i]),
  2378. !!(fs->filter_flags & flagbit));
  2379. }
  2380. }
  2381. static void unifontsel_draw_preview_text_inner(unifont_drawctx *dctx,
  2382. unifontsel_internal *fs)
  2383. {
  2384. unifont *font;
  2385. char *sizename = NULL;
  2386. fontinfo *info = fs->selected;
  2387. if (info) {
  2388. sizename = info->fontclass->scale_fontname(
  2389. GTK_WIDGET(fs->u.window), info->realname, fs->selsize);
  2390. font = info->fontclass->create(GTK_WIDGET(fs->u.window),
  2391. sizename ? sizename : info->realname,
  2392. false, false, 0, 0);
  2393. } else
  2394. font = NULL;
  2395. #ifdef DRAW_TEXT_GDK
  2396. if (dctx->type == DRAWTYPE_GDK) {
  2397. gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_bg);
  2398. gdk_draw_rectangle(dctx->u.gdk.target, dctx->u.gdk.gc, 1, 0, 0,
  2399. fs->preview_width, fs->preview_height);
  2400. gdk_gc_set_foreground(dctx->u.gdk.gc, &fs->preview_fg);
  2401. }
  2402. #endif
  2403. #ifdef DRAW_TEXT_CAIRO
  2404. if (dctx->type == DRAWTYPE_CAIRO) {
  2405. cairo_set_source_rgb(dctx->u.cairo.cr,
  2406. fs->preview_bg.red / 65535.0,
  2407. fs->preview_bg.green / 65535.0,
  2408. fs->preview_bg.blue / 65535.0);
  2409. cairo_paint(dctx->u.cairo.cr);
  2410. cairo_set_source_rgb(dctx->u.cairo.cr,
  2411. fs->preview_fg.red / 65535.0,
  2412. fs->preview_fg.green / 65535.0,
  2413. fs->preview_fg.blue / 65535.0);
  2414. }
  2415. #endif
  2416. if (font) {
  2417. /*
  2418. * The pangram used here is rather carefully
  2419. * constructed: it contains a sequence of very narrow
  2420. * letters (`jil') and a pair of adjacent very wide
  2421. * letters (`wm').
  2422. *
  2423. * If the user selects a proportional font, it will be
  2424. * coerced into fixed-width character cells when used
  2425. * in the actual terminal window. We therefore display
  2426. * it the same way in the preview pane, so as to show
  2427. * it the way it will actually be displayed - and we
  2428. * deliberately pick a pangram which will show the
  2429. * resulting miskerning at its worst.
  2430. *
  2431. * We aren't trying to sell people these fonts; we're
  2432. * trying to let them make an informed choice. Better
  2433. * that they find out the problems with using
  2434. * proportional fonts in terminal windows here than
  2435. * that they go to the effort of selecting their font
  2436. * and _then_ realise it was a mistake.
  2437. */
  2438. info->fontclass->draw_text(dctx, font,
  2439. 0, font->ascent,
  2440. L"bankrupt jilted showmen quiz convex fogey",
  2441. 41, false, false, font->width);
  2442. info->fontclass->draw_text(dctx, font,
  2443. 0, font->ascent + font->height,
  2444. L"BANKRUPT JILTED SHOWMEN QUIZ CONVEX FOGEY",
  2445. 41, false, false, font->width);
  2446. /*
  2447. * The ordering of punctuation here is also selected
  2448. * with some specific aims in mind. I put ` and '
  2449. * together because some software (and people) still
  2450. * use them as matched quotes no matter what Unicode
  2451. * might say on the matter, so people can quickly
  2452. * check whether they look silly in a candidate font.
  2453. * The sequence #_@ is there to let people judge the
  2454. * suitability of the underscore as an effectively
  2455. * alphabetic character (since that's how it's often
  2456. * used in practice, at least by programmers).
  2457. */
  2458. info->fontclass->draw_text(dctx, font,
  2459. 0, font->ascent + font->height * 2,
  2460. L"0123456789!?,.:;<>()[]{}\\/`'\"+*-=~#_@|%&^$",
  2461. 42, false, false, font->width);
  2462. info->fontclass->destroy(font);
  2463. }
  2464. sfree(sizename);
  2465. }
  2466. static void unifontsel_draw_preview_text(unifontsel_internal *fs)
  2467. {
  2468. unifont_drawctx dctx;
  2469. GdkWindow *target;
  2470. #ifndef NO_BACKING_PIXMAPS
  2471. target = fs->preview_pixmap;
  2472. #else
  2473. target = gtk_widget_get_window(fs->preview_area);
  2474. #endif
  2475. if (!target) /* we may be called when we haven't created everything yet */
  2476. return;
  2477. dctx.type = DRAWTYPE_DEFAULT;
  2478. #ifdef DRAW_TEXT_GDK
  2479. if (dctx.type == DRAWTYPE_GDK) {
  2480. dctx.u.gdk.target = target;
  2481. dctx.u.gdk.gc = gdk_gc_new(target);
  2482. }
  2483. #endif
  2484. #ifdef DRAW_TEXT_CAIRO
  2485. if (dctx.type == DRAWTYPE_CAIRO) {
  2486. #if GTK_CHECK_VERSION(3,22,0)
  2487. cairo_region_t *region;
  2488. #endif
  2489. dctx.u.cairo.widget = GTK_WIDGET(fs->preview_area);
  2490. #if GTK_CHECK_VERSION(3,22,0)
  2491. dctx.u.cairo.gdkwin = gtk_widget_get_window(dctx.u.cairo.widget);
  2492. region = gdk_window_get_clip_region(dctx.u.cairo.gdkwin);
  2493. dctx.u.cairo.drawctx = gdk_window_begin_draw_frame(
  2494. dctx.u.cairo.gdkwin, region);
  2495. dctx.u.cairo.cr = gdk_drawing_context_get_cairo_context(
  2496. dctx.u.cairo.drawctx);
  2497. cairo_region_destroy(region);
  2498. #else
  2499. dctx.u.cairo.cr = gdk_cairo_create(target);
  2500. #endif
  2501. }
  2502. #endif
  2503. unifontsel_draw_preview_text_inner(&dctx, fs);
  2504. #ifdef DRAW_TEXT_GDK
  2505. if (dctx.type == DRAWTYPE_GDK) {
  2506. gdk_gc_unref(dctx.u.gdk.gc);
  2507. }
  2508. #endif
  2509. #ifdef DRAW_TEXT_CAIRO
  2510. if (dctx.type == DRAWTYPE_CAIRO) {
  2511. #if GTK_CHECK_VERSION(3,22,0)
  2512. gdk_window_end_draw_frame(dctx.u.cairo.gdkwin, dctx.u.cairo.drawctx);
  2513. #else
  2514. cairo_destroy(dctx.u.cairo.cr);
  2515. #endif
  2516. }
  2517. #endif
  2518. gdk_window_invalidate_rect(gtk_widget_get_window(fs->preview_area),
  2519. NULL, false);
  2520. }
  2521. static void unifontsel_select_font(unifontsel_internal *fs,
  2522. fontinfo *info, int size, int leftlist,
  2523. bool size_is_explicit)
  2524. {
  2525. int index;
  2526. int minval, maxval;
  2527. gboolean success;
  2528. GtkTreePath *treepath;
  2529. GtkTreeIter iter;
  2530. fs->inhibit_response = true;
  2531. fs->selected = info;
  2532. fs->selsize = size;
  2533. if (size_is_explicit)
  2534. fs->intendedsize = size;
  2535. gtk_widget_set_sensitive(fs->u.ok_button, true);
  2536. /*
  2537. * Find the index of this fontinfo in the selorder list.
  2538. */
  2539. index = -1;
  2540. findpos234(fs->fonts_by_selorder, info, NULL, &index);
  2541. assert(index >= 0);
  2542. /*
  2543. * Adjust the font selector flags and redo the font family
  2544. * list box, if necessary.
  2545. */
  2546. if (leftlist <= 0 &&
  2547. (fs->filter_flags | info->flags) != fs->filter_flags) {
  2548. fs->filter_flags |= info->flags;
  2549. unifontsel_set_filter_buttons(fs);
  2550. unifontsel_setup_familylist(fs);
  2551. }
  2552. /*
  2553. * Find the appropriate family name and select it in the list.
  2554. */
  2555. assert(info->familyindex >= 0);
  2556. treepath = gtk_tree_path_new_from_indices(info->familyindex, -1);
  2557. gtk_tree_selection_select_path(
  2558. gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->family_list)),
  2559. treepath);
  2560. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->family_list),
  2561. treepath, NULL, false, 0.0, 0.0);
  2562. success = gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model),
  2563. &iter, treepath);
  2564. assert(success);
  2565. gtk_tree_path_free(treepath);
  2566. /*
  2567. * Now set up the font style list.
  2568. */
  2569. gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter,
  2570. 1, &minval, 2, &maxval, -1);
  2571. if (leftlist <= 1)
  2572. unifontsel_setup_stylelist(fs, minval, maxval);
  2573. /*
  2574. * Find the appropriate style name and select it in the list.
  2575. */
  2576. if (info->style) {
  2577. assert(info->styleindex >= 0);
  2578. treepath = gtk_tree_path_new_from_indices(info->styleindex, -1);
  2579. gtk_tree_selection_select_path(
  2580. gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->style_list)),
  2581. treepath);
  2582. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->style_list),
  2583. treepath, NULL, false, 0.0, 0.0);
  2584. gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->style_model),
  2585. &iter, treepath);
  2586. gtk_tree_path_free(treepath);
  2587. /*
  2588. * And set up the size list.
  2589. */
  2590. gtk_tree_model_get(GTK_TREE_MODEL(fs->style_model), &iter,
  2591. 1, &minval, 2, &maxval, -1);
  2592. if (leftlist <= 2)
  2593. unifontsel_setup_sizelist(fs, minval, maxval);
  2594. /*
  2595. * Find the appropriate size, and select it in the list.
  2596. */
  2597. if (info->size) {
  2598. assert(info->sizeindex >= 0);
  2599. treepath = gtk_tree_path_new_from_indices(info->sizeindex, -1);
  2600. gtk_tree_selection_select_path(
  2601. gtk_tree_view_get_selection(GTK_TREE_VIEW(fs->size_list)),
  2602. treepath);
  2603. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
  2604. treepath, NULL, false, 0.0, 0.0);
  2605. gtk_tree_path_free(treepath);
  2606. size = info->size;
  2607. } else {
  2608. int j;
  2609. for (j = 0; j < lenof(unifontsel_default_sizes); j++)
  2610. if (unifontsel_default_sizes[j] == size) {
  2611. treepath = gtk_tree_path_new_from_indices(j, -1);
  2612. gtk_tree_view_set_cursor(GTK_TREE_VIEW(fs->size_list),
  2613. treepath, NULL, false);
  2614. gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(fs->size_list),
  2615. treepath, NULL, false, 0.0,
  2616. 0.0);
  2617. gtk_tree_path_free(treepath);
  2618. }
  2619. }
  2620. /*
  2621. * And set up the font size text entry box.
  2622. */
  2623. {
  2624. char sizetext[40];
  2625. sprintf(sizetext, "%d", size);
  2626. gtk_entry_set_text(GTK_ENTRY(fs->size_entry), sizetext);
  2627. }
  2628. } else {
  2629. if (leftlist <= 2)
  2630. unifontsel_setup_sizelist(fs, 0, 0);
  2631. gtk_entry_set_text(GTK_ENTRY(fs->size_entry), "");
  2632. }
  2633. /*
  2634. * Grey out the font size edit box if we're not using a
  2635. * scalable font.
  2636. */
  2637. gtk_editable_set_editable(GTK_EDITABLE(fs->size_entry),
  2638. fs->selected->size == 0);
  2639. gtk_widget_set_sensitive(fs->size_entry, fs->selected->size == 0);
  2640. unifontsel_draw_preview_text(fs);
  2641. fs->inhibit_response = false;
  2642. }
  2643. static void unifontsel_button_toggled(GtkToggleButton *tb, gpointer data)
  2644. {
  2645. unifontsel_internal *fs = (unifontsel_internal *)data;
  2646. bool newstate = gtk_toggle_button_get_active(tb);
  2647. int newflags;
  2648. int flagbit = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(tb),
  2649. "user-data"));
  2650. if (newstate)
  2651. newflags = fs->filter_flags | flagbit;
  2652. else
  2653. newflags = fs->filter_flags & ~flagbit;
  2654. if (fs->filter_flags != newflags) {
  2655. fs->filter_flags = newflags;
  2656. unifontsel_setup_familylist(fs);
  2657. }
  2658. }
  2659. static void unifontsel_add_entry(void *ctx, const char *realfontname,
  2660. const char *family, const char *charset,
  2661. const char *style, const char *stylekey,
  2662. int size, int flags,
  2663. const struct UnifontVtable *fontclass)
  2664. {
  2665. unifontsel_internal *fs = (unifontsel_internal *)ctx;
  2666. fontinfo *info;
  2667. int totalsize;
  2668. char *p;
  2669. totalsize = sizeof(fontinfo) + strlen(realfontname) +
  2670. (family ? strlen(family) : 0) + (charset ? strlen(charset) : 0) +
  2671. (style ? strlen(style) : 0) + (stylekey ? strlen(stylekey) : 0) + 10;
  2672. info = (fontinfo *)smalloc(totalsize);
  2673. info->fontclass = fontclass;
  2674. p = (char *)info + sizeof(fontinfo);
  2675. info->realname = p;
  2676. strcpy(p, realfontname);
  2677. p += 1+strlen(p);
  2678. if (family) {
  2679. info->family = p;
  2680. strcpy(p, family);
  2681. p += 1+strlen(p);
  2682. } else
  2683. info->family = NULL;
  2684. if (charset) {
  2685. info->charset = p;
  2686. strcpy(p, charset);
  2687. p += 1+strlen(p);
  2688. } else
  2689. info->charset = NULL;
  2690. if (style) {
  2691. info->style = p;
  2692. strcpy(p, style);
  2693. p += 1+strlen(p);
  2694. } else
  2695. info->style = NULL;
  2696. if (stylekey) {
  2697. info->stylekey = p;
  2698. strcpy(p, stylekey);
  2699. p += 1+strlen(p);
  2700. } else
  2701. info->stylekey = NULL;
  2702. assert(p - (char *)info <= totalsize);
  2703. info->size = size;
  2704. info->flags = flags;
  2705. info->index = count234(fs->fonts_by_selorder);
  2706. /*
  2707. * It's just conceivable that a misbehaving font enumerator
  2708. * might tell us about the same font real name more than once,
  2709. * in which case we should silently drop the new one.
  2710. */
  2711. if (add234(fs->fonts_by_realname, info) != info) {
  2712. sfree(info);
  2713. return;
  2714. }
  2715. /*
  2716. * However, we should never get a duplicate key in the
  2717. * selorder tree, because the index field carefully
  2718. * disambiguates otherwise identical records.
  2719. */
  2720. add234(fs->fonts_by_selorder, info);
  2721. }
  2722. static fontinfo *update_for_intended_size(unifontsel_internal *fs,
  2723. fontinfo *info)
  2724. {
  2725. fontinfo info2, *below, *above;
  2726. int pos;
  2727. /*
  2728. * Copy the info structure. This doesn't copy its dynamic
  2729. * string fields, but that's unimportant because all we're
  2730. * going to do is to adjust the size field and use it in one
  2731. * tree search.
  2732. */
  2733. info2 = *info;
  2734. info2.size = fs->intendedsize;
  2735. /*
  2736. * Search in the tree to find the fontinfo structure which
  2737. * best approximates the size the user last requested.
  2738. */
  2739. below = findrelpos234(fs->fonts_by_selorder, &info2, NULL,
  2740. REL234_LE, &pos);
  2741. if (!below)
  2742. pos = -1;
  2743. above = index234(fs->fonts_by_selorder, pos+1);
  2744. /*
  2745. * See if we've found it exactly, which is an easy special
  2746. * case. If we have, it'll be in `below' and not `above',
  2747. * because we did a REL234_LE rather than REL234_LT search.
  2748. */
  2749. if (below && !fontinfo_selorder_compare(&info2, below))
  2750. return below;
  2751. /*
  2752. * Now we've either found two suitable fonts, one smaller and
  2753. * one larger, or we're at one or other extreme end of the
  2754. * scale. Find out which, by NULLing out either of below and
  2755. * above if it differs from this one in any respect but size
  2756. * (and the disambiguating index field). Bear in mind, also,
  2757. * that either one might _already_ be NULL if we're at the
  2758. * extreme ends of the font list.
  2759. */
  2760. if (below) {
  2761. info2.size = below->size;
  2762. info2.index = below->index;
  2763. if (fontinfo_selorder_compare(&info2, below))
  2764. below = NULL;
  2765. }
  2766. if (above) {
  2767. info2.size = above->size;
  2768. info2.index = above->index;
  2769. if (fontinfo_selorder_compare(&info2, above))
  2770. above = NULL;
  2771. }
  2772. /*
  2773. * Now return whichever of above and below is non-NULL, if
  2774. * that's unambiguous.
  2775. */
  2776. if (!above)
  2777. return below;
  2778. if (!below)
  2779. return above;
  2780. /*
  2781. * And now we really do have to make a choice about whether to
  2782. * round up or down. We'll do it by rounding to nearest,
  2783. * breaking ties by rounding up.
  2784. */
  2785. if (above->size - fs->intendedsize <= fs->intendedsize - below->size)
  2786. return above;
  2787. else
  2788. return below;
  2789. }
  2790. static void family_changed(GtkTreeSelection *treeselection, gpointer data)
  2791. {
  2792. unifontsel_internal *fs = (unifontsel_internal *)data;
  2793. GtkTreeModel *treemodel;
  2794. GtkTreeIter treeiter;
  2795. int minval;
  2796. fontinfo *info;
  2797. if (fs->inhibit_response) /* we made this change ourselves */
  2798. return;
  2799. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2800. return;
  2801. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
  2802. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2803. if (!info)
  2804. return; /* _shouldn't_ happen unless font list is completely funted */
  2805. info = update_for_intended_size(fs, info);
  2806. if (!info)
  2807. return; /* similarly shouldn't happen */
  2808. if (!info->size)
  2809. fs->selsize = fs->intendedsize; /* font is scalable */
  2810. unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
  2811. 1, false);
  2812. }
  2813. static void style_changed(GtkTreeSelection *treeselection, gpointer data)
  2814. {
  2815. unifontsel_internal *fs = (unifontsel_internal *)data;
  2816. GtkTreeModel *treemodel;
  2817. GtkTreeIter treeiter;
  2818. int minval;
  2819. fontinfo *info;
  2820. if (fs->inhibit_response) /* we made this change ourselves */
  2821. return;
  2822. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2823. return;
  2824. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, -1);
  2825. if (minval < 0)
  2826. return; /* somehow a charset heading got clicked */
  2827. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2828. if (!info)
  2829. return; /* _shouldn't_ happen unless font list is completely funted */
  2830. info = update_for_intended_size(fs, info);
  2831. if (!info)
  2832. return; /* similarly shouldn't happen */
  2833. if (!info->size)
  2834. fs->selsize = fs->intendedsize; /* font is scalable */
  2835. unifontsel_select_font(fs, info, info->size ? info->size : fs->selsize,
  2836. 2, false);
  2837. }
  2838. static void size_changed(GtkTreeSelection *treeselection, gpointer data)
  2839. {
  2840. unifontsel_internal *fs = (unifontsel_internal *)data;
  2841. GtkTreeModel *treemodel;
  2842. GtkTreeIter treeiter;
  2843. int minval, size;
  2844. fontinfo *info;
  2845. if (fs->inhibit_response) /* we made this change ourselves */
  2846. return;
  2847. if (!gtk_tree_selection_get_selected(treeselection, &treemodel, &treeiter))
  2848. return;
  2849. gtk_tree_model_get(treemodel, &treeiter, 1, &minval, 2, &size, -1);
  2850. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2851. if (!info)
  2852. return; /* _shouldn't_ happen unless font list is completely funted */
  2853. unifontsel_select_font(fs, info, info->size ? info->size : size, 3, true);
  2854. }
  2855. static void size_entry_changed(GtkEditable *ed, gpointer data)
  2856. {
  2857. unifontsel_internal *fs = (unifontsel_internal *)data;
  2858. const char *text;
  2859. int size;
  2860. if (fs->inhibit_response) /* we made this change ourselves */
  2861. return;
  2862. text = gtk_entry_get_text(GTK_ENTRY(ed));
  2863. size = atoi(text);
  2864. if (size > 0) {
  2865. assert(fs->selected->size == 0);
  2866. unifontsel_select_font(fs, fs->selected, size, 3, true);
  2867. }
  2868. }
  2869. static void alias_resolve(GtkTreeView *treeview, GtkTreePath *path,
  2870. GtkTreeViewColumn *column, gpointer data)
  2871. {
  2872. unifontsel_internal *fs = (unifontsel_internal *)data;
  2873. GtkTreeIter iter;
  2874. int minval, newsize;
  2875. fontinfo *info, *newinfo;
  2876. char *newname;
  2877. if (fs->inhibit_response) /* we made this change ourselves */
  2878. return;
  2879. gtk_tree_model_get_iter(GTK_TREE_MODEL(fs->family_model), &iter, path);
  2880. gtk_tree_model_get(GTK_TREE_MODEL(fs->family_model), &iter, 1,&minval, -1);
  2881. info = (fontinfo *)index234(fs->fonts_by_selorder, minval);
  2882. if (info) {
  2883. int flags;
  2884. struct fontinfo_realname_find f;
  2885. newname = info->fontclass->canonify_fontname(
  2886. GTK_WIDGET(fs->u.window), info->realname, &newsize, &flags, true);
  2887. f.realname = newname;
  2888. f.flags = flags;
  2889. newinfo = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  2890. sfree(newname);
  2891. if (!newinfo)
  2892. return; /* font name not in our index */
  2893. if (newinfo == info)
  2894. return; /* didn't change under canonification => not an alias */
  2895. unifontsel_select_font(fs, newinfo,
  2896. newinfo->size ? newinfo->size : newsize,
  2897. 1, true);
  2898. }
  2899. }
  2900. #if GTK_CHECK_VERSION(3,0,0)
  2901. static gint unifontsel_draw_area(GtkWidget *widget, cairo_t *cr, gpointer data)
  2902. {
  2903. unifontsel_internal *fs = (unifontsel_internal *)data;
  2904. unifont_drawctx dctx;
  2905. dctx.type = DRAWTYPE_CAIRO;
  2906. dctx.u.cairo.widget = widget;
  2907. dctx.u.cairo.cr = cr;
  2908. unifontsel_draw_preview_text_inner(&dctx, fs);
  2909. return true;
  2910. }
  2911. #else
  2912. static gint unifontsel_expose_area(GtkWidget *widget, GdkEventExpose *event,
  2913. gpointer data)
  2914. {
  2915. unifontsel_internal *fs = (unifontsel_internal *)data;
  2916. #ifndef NO_BACKING_PIXMAPS
  2917. if (fs->preview_pixmap) {
  2918. gdk_draw_pixmap(gtk_widget_get_window(widget),
  2919. (gtk_widget_get_style(widget)->fg_gc
  2920. [gtk_widget_get_state(widget)]),
  2921. fs->preview_pixmap,
  2922. event->area.x, event->area.y,
  2923. event->area.x, event->area.y,
  2924. event->area.width, event->area.height);
  2925. }
  2926. #else
  2927. unifontsel_draw_preview_text(fs);
  2928. #endif
  2929. return true;
  2930. }
  2931. #endif
  2932. static gint unifontsel_configure_area(GtkWidget *widget,
  2933. GdkEventConfigure *event, gpointer data)
  2934. {
  2935. #ifndef NO_BACKING_PIXMAPS
  2936. unifontsel_internal *fs = (unifontsel_internal *)data;
  2937. int ox, oy, nx, ny, x, y;
  2938. /*
  2939. * Enlarge the pixmap, but never shrink it.
  2940. */
  2941. ox = fs->preview_width;
  2942. oy = fs->preview_height;
  2943. x = event->width;
  2944. y = event->height;
  2945. if (x > ox || y > oy) {
  2946. if (fs->preview_pixmap)
  2947. gdk_pixmap_unref(fs->preview_pixmap);
  2948. nx = (x > ox ? x : ox);
  2949. ny = (y > oy ? y : oy);
  2950. fs->preview_pixmap = gdk_pixmap_new(gtk_widget_get_window(widget),
  2951. nx, ny, -1);
  2952. fs->preview_width = nx;
  2953. fs->preview_height = ny;
  2954. unifontsel_draw_preview_text(fs);
  2955. }
  2956. #endif
  2957. gdk_window_invalidate_rect(gtk_widget_get_window(widget), NULL, false);
  2958. return true;
  2959. }
  2960. unifontsel *unifontsel_new(const char *wintitle)
  2961. {
  2962. unifontsel_internal *fs = snew(unifontsel_internal);
  2963. GtkWidget *table, *label, *w, *ww, *scroll;
  2964. GtkListStore *model;
  2965. GtkTreeViewColumn *column;
  2966. int lists_height, preview_height, font_width, style_width, size_width;
  2967. int i;
  2968. fs->inhibit_response = false;
  2969. fs->selected = NULL;
  2970. {
  2971. int width, height;
  2972. /*
  2973. * Invent some magic size constants.
  2974. */
  2975. get_label_text_dimensions("Quite Long Font Name (Foundry)",
  2976. &width, &height);
  2977. font_width = width;
  2978. lists_height = 14 * height;
  2979. preview_height = 5 * height;
  2980. get_label_text_dimensions("Italic Extra Condensed", &width, &height);
  2981. style_width = width;
  2982. get_label_text_dimensions("48000", &width, &height);
  2983. size_width = width;
  2984. }
  2985. /*
  2986. * Create the dialog box and initialise the user-visible
  2987. * fields in the returned structure.
  2988. */
  2989. fs->u.user_data = NULL;
  2990. fs->u.window = GTK_WINDOW(gtk_dialog_new());
  2991. gtk_window_set_title(fs->u.window, wintitle);
  2992. fs->u.cancel_button = gtk_dialog_add_button(
  2993. GTK_DIALOG(fs->u.window), STANDARD_CANCEL_LABEL, GTK_RESPONSE_CANCEL);
  2994. fs->u.ok_button = gtk_dialog_add_button(
  2995. GTK_DIALOG(fs->u.window), STANDARD_OK_LABEL, GTK_RESPONSE_OK);
  2996. gtk_widget_grab_default(fs->u.ok_button);
  2997. /*
  2998. * Now set up the internal fields, including in particular all
  2999. * the controls that actually allow the user to select fonts.
  3000. */
  3001. #if GTK_CHECK_VERSION(3,0,0)
  3002. table = gtk_grid_new();
  3003. gtk_grid_set_column_spacing(GTK_GRID(table), 8);
  3004. #else
  3005. table = gtk_table_new(8, 3, false);
  3006. gtk_table_set_col_spacings(GTK_TABLE(table), 8);
  3007. #endif
  3008. gtk_widget_show(table);
  3009. #if GTK_CHECK_VERSION(3,0,0)
  3010. /* GtkAlignment has become deprecated and we use the "margin"
  3011. * property */
  3012. w = table;
  3013. g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
  3014. #elif GTK_CHECK_VERSION(2,4,0)
  3015. /* GtkAlignment seems to be the simplest way to put padding round things */
  3016. w = gtk_alignment_new(0, 0, 1, 1);
  3017. gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
  3018. gtk_container_add(GTK_CONTAINER(w), table);
  3019. gtk_widget_show(w);
  3020. #else
  3021. /* In GTK < 2.4, even that isn't available */
  3022. w = table;
  3023. #endif
  3024. gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(
  3025. GTK_DIALOG(fs->u.window))),
  3026. w, true, true, 0);
  3027. label = gtk_label_new_with_mnemonic("_Font:");
  3028. gtk_widget_show(label);
  3029. align_label_left(GTK_LABEL(label));
  3030. #if GTK_CHECK_VERSION(3,0,0)
  3031. gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
  3032. g_object_set(G_OBJECT(label), "hexpand", true, (const char *)NULL);
  3033. #else
  3034. gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1, GTK_FILL, 0, 0, 0);
  3035. #endif
  3036. /*
  3037. * The Font list box displays only a string, but additionally
  3038. * stores two integers which give the limits within the
  3039. * tree234 of the font entries covered by this list entry.
  3040. */
  3041. model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
  3042. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  3043. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), false);
  3044. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  3045. gtk_widget_show(w);
  3046. column = gtk_tree_view_column_new_with_attributes(
  3047. "Font", gtk_cell_renderer_text_new(),
  3048. "text", 0, (char *)NULL);
  3049. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  3050. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  3051. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  3052. "changed", G_CALLBACK(family_changed), fs);
  3053. g_signal_connect(G_OBJECT(w), "row-activated",
  3054. G_CALLBACK(alias_resolve), fs);
  3055. scroll = gtk_scrolled_window_new(NULL, NULL);
  3056. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  3057. GTK_SHADOW_IN);
  3058. gtk_container_add(GTK_CONTAINER(scroll), w);
  3059. gtk_widget_show(scroll);
  3060. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  3061. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  3062. gtk_widget_set_size_request(scroll, font_width, lists_height);
  3063. #if GTK_CHECK_VERSION(3,0,0)
  3064. gtk_grid_attach(GTK_GRID(table), scroll, 0, 1, 1, 2);
  3065. g_object_set(G_OBJECT(scroll), "expand", true, (const char *)NULL);
  3066. #else
  3067. gtk_table_attach(GTK_TABLE(table), scroll, 0, 1, 1, 3, GTK_FILL,
  3068. GTK_EXPAND | GTK_FILL, 0, 0);
  3069. #endif
  3070. fs->family_model = model;
  3071. fs->family_list = w;
  3072. label = gtk_label_new_with_mnemonic("_Style:");
  3073. gtk_widget_show(label);
  3074. align_label_left(GTK_LABEL(label));
  3075. #if GTK_CHECK_VERSION(3,0,0)
  3076. gtk_grid_attach(GTK_GRID(table), label, 1, 0, 1, 1);
  3077. g_object_set(G_OBJECT(label), "hexpand", true, (const char *)NULL);
  3078. #else
  3079. gtk_table_attach(GTK_TABLE(table), label, 1, 2, 0, 1, GTK_FILL, 0, 0, 0);
  3080. #endif
  3081. /*
  3082. * The Style list box can contain insensitive elements (character
  3083. * set headings for server-side fonts), so we add an extra column
  3084. * to the list store to hold that information. Also, since GTK3 at
  3085. * least doesn't seem to display insensitive elements differently
  3086. * by default, we add a further column to change their style.
  3087. */
  3088. model = gtk_list_store_new(5, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT,
  3089. G_TYPE_BOOLEAN, G_TYPE_INT);
  3090. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  3091. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), false);
  3092. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  3093. gtk_widget_show(w);
  3094. column = gtk_tree_view_column_new_with_attributes(
  3095. "Style", gtk_cell_renderer_text_new(),
  3096. "text", 0, "sensitive", 3, "weight", 4, (char *)NULL);
  3097. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  3098. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  3099. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  3100. "changed", G_CALLBACK(style_changed), fs);
  3101. scroll = gtk_scrolled_window_new(NULL, NULL);
  3102. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  3103. GTK_SHADOW_IN);
  3104. gtk_container_add(GTK_CONTAINER(scroll), w);
  3105. gtk_widget_show(scroll);
  3106. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  3107. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  3108. gtk_widget_set_size_request(scroll, style_width, lists_height);
  3109. #if GTK_CHECK_VERSION(3,0,0)
  3110. gtk_grid_attach(GTK_GRID(table), scroll, 1, 1, 1, 2);
  3111. g_object_set(G_OBJECT(scroll), "expand", true, (const char *)NULL);
  3112. #else
  3113. gtk_table_attach(GTK_TABLE(table), scroll, 1, 2, 1, 3, GTK_FILL,
  3114. GTK_EXPAND | GTK_FILL, 0, 0);
  3115. #endif
  3116. fs->style_model = model;
  3117. fs->style_list = w;
  3118. label = gtk_label_new_with_mnemonic("Si_ze:");
  3119. gtk_widget_show(label);
  3120. align_label_left(GTK_LABEL(label));
  3121. #if GTK_CHECK_VERSION(3,0,0)
  3122. gtk_grid_attach(GTK_GRID(table), label, 2, 0, 1, 1);
  3123. g_object_set(G_OBJECT(label), "hexpand", true, (const char *)NULL);
  3124. #else
  3125. gtk_table_attach(GTK_TABLE(table), label, 2, 3, 0, 1, GTK_FILL, 0, 0, 0);
  3126. #endif
  3127. /*
  3128. * The Size label attaches primarily to a text input box so
  3129. * that the user can select a size of their choice. The list
  3130. * of available sizes is secondary.
  3131. */
  3132. fs->size_entry = w = gtk_entry_new();
  3133. gtk_label_set_mnemonic_widget(GTK_LABEL(label), w);
  3134. gtk_widget_set_size_request(w, size_width, -1);
  3135. gtk_widget_show(w);
  3136. #if GTK_CHECK_VERSION(3,0,0)
  3137. gtk_grid_attach(GTK_GRID(table), w, 2, 1, 1, 1);
  3138. g_object_set(G_OBJECT(w), "hexpand", true, (const char *)NULL);
  3139. #else
  3140. gtk_table_attach(GTK_TABLE(table), w, 2, 3, 1, 2, GTK_FILL, 0, 0, 0);
  3141. #endif
  3142. g_signal_connect(G_OBJECT(w), "changed", G_CALLBACK(size_entry_changed),
  3143. fs);
  3144. model = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT);
  3145. w = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
  3146. gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(w), false);
  3147. gtk_widget_show(w);
  3148. column = gtk_tree_view_column_new_with_attributes(
  3149. "Size", gtk_cell_renderer_text_new(),
  3150. "text", 0, (char *)NULL);
  3151. gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
  3152. gtk_tree_view_append_column(GTK_TREE_VIEW(w), column);
  3153. g_signal_connect(G_OBJECT(gtk_tree_view_get_selection(GTK_TREE_VIEW(w))),
  3154. "changed", G_CALLBACK(size_changed), fs);
  3155. scroll = gtk_scrolled_window_new(NULL, NULL);
  3156. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll),
  3157. GTK_SHADOW_IN);
  3158. gtk_container_add(GTK_CONTAINER(scroll), w);
  3159. gtk_widget_show(scroll);
  3160. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
  3161. GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
  3162. #if GTK_CHECK_VERSION(3,0,0)
  3163. gtk_grid_attach(GTK_GRID(table), scroll, 2, 2, 1, 1);
  3164. g_object_set(G_OBJECT(scroll), "expand", true, (const char *)NULL);
  3165. #else
  3166. gtk_table_attach(GTK_TABLE(table), scroll, 2, 3, 2, 3, GTK_FILL,
  3167. GTK_EXPAND | GTK_FILL, 0, 0);
  3168. #endif
  3169. fs->size_model = model;
  3170. fs->size_list = w;
  3171. /*
  3172. * Preview widget.
  3173. */
  3174. fs->preview_area = gtk_drawing_area_new();
  3175. #ifndef NO_BACKING_PIXMAPS
  3176. fs->preview_pixmap = NULL;
  3177. #endif
  3178. fs->preview_width = 0;
  3179. fs->preview_height = 0;
  3180. fs->preview_fg.pixel = fs->preview_bg.pixel = 0;
  3181. fs->preview_fg.red = fs->preview_fg.green = fs->preview_fg.blue = 0x0000;
  3182. fs->preview_bg.red = fs->preview_bg.green = fs->preview_bg.blue = 0xFFFF;
  3183. #if !GTK_CHECK_VERSION(3,0,0)
  3184. gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_fg,
  3185. false, false);
  3186. gdk_colormap_alloc_color(gdk_colormap_get_system(), &fs->preview_bg,
  3187. false, false);
  3188. #endif
  3189. #if GTK_CHECK_VERSION(3,0,0)
  3190. g_signal_connect(G_OBJECT(fs->preview_area), "draw",
  3191. G_CALLBACK(unifontsel_draw_area), fs);
  3192. #else
  3193. g_signal_connect(G_OBJECT(fs->preview_area), "expose_event",
  3194. G_CALLBACK(unifontsel_expose_area), fs);
  3195. #endif
  3196. g_signal_connect(G_OBJECT(fs->preview_area), "configure_event",
  3197. G_CALLBACK(unifontsel_configure_area), fs);
  3198. gtk_widget_set_size_request(fs->preview_area, 1, preview_height);
  3199. gtk_widget_show(fs->preview_area);
  3200. ww = fs->preview_area;
  3201. w = gtk_frame_new(NULL);
  3202. gtk_container_add(GTK_CONTAINER(w), ww);
  3203. gtk_widget_show(w);
  3204. #if GTK_CHECK_VERSION(3,0,0)
  3205. /* GtkAlignment has become deprecated and we use the "margin"
  3206. * property */
  3207. g_object_set(G_OBJECT(w), "margin", 8, (const char *)NULL);
  3208. #elif GTK_CHECK_VERSION(2,4,0)
  3209. ww = w;
  3210. /* GtkAlignment seems to be the simplest way to put padding round things */
  3211. w = gtk_alignment_new(0, 0, 1, 1);
  3212. gtk_alignment_set_padding(GTK_ALIGNMENT(w), 8, 8, 8, 8);
  3213. gtk_container_add(GTK_CONTAINER(w), ww);
  3214. gtk_widget_show(w);
  3215. #endif
  3216. ww = w;
  3217. w = gtk_frame_new("Preview of font");
  3218. gtk_container_add(GTK_CONTAINER(w), ww);
  3219. gtk_widget_show(w);
  3220. #if GTK_CHECK_VERSION(3,0,0)
  3221. gtk_grid_attach(GTK_GRID(table), w, 0, 3, 3, 1);
  3222. g_object_set(G_OBJECT(w), "expand", true, (const char *)NULL);
  3223. #else
  3224. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 3, 4,
  3225. GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 8);
  3226. #endif
  3227. /*
  3228. * We only provide the checkboxes for client- and server-side
  3229. * fonts if we have the X11 back end available, because that's the
  3230. * only situation in which more than one class of font is
  3231. * available anyway.
  3232. */
  3233. fs->n_filter_buttons = 0;
  3234. #ifndef NOT_X_WINDOWS
  3235. w = gtk_check_button_new_with_label("Show client-side fonts");
  3236. g_object_set_data(G_OBJECT(w), "user-data",
  3237. GINT_TO_POINTER(FONTFLAG_CLIENTSIDE));
  3238. g_signal_connect(G_OBJECT(w), "toggled",
  3239. G_CALLBACK(unifontsel_button_toggled), fs);
  3240. gtk_widget_show(w);
  3241. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3242. #if GTK_CHECK_VERSION(3,0,0)
  3243. gtk_grid_attach(GTK_GRID(table), w, 0, 4, 3, 1);
  3244. g_object_set(G_OBJECT(w), "hexpand", true, (const char *)NULL);
  3245. #else
  3246. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 4, 5, GTK_FILL, 0, 0, 0);
  3247. #endif
  3248. w = gtk_check_button_new_with_label("Show server-side fonts");
  3249. g_object_set_data(G_OBJECT(w), "user-data",
  3250. GINT_TO_POINTER(FONTFLAG_SERVERSIDE));
  3251. g_signal_connect(G_OBJECT(w), "toggled",
  3252. G_CALLBACK(unifontsel_button_toggled), fs);
  3253. gtk_widget_show(w);
  3254. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3255. #if GTK_CHECK_VERSION(3,0,0)
  3256. gtk_grid_attach(GTK_GRID(table), w, 0, 5, 3, 1);
  3257. g_object_set(G_OBJECT(w), "hexpand", true, (const char *)NULL);
  3258. #else
  3259. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 5, 6, GTK_FILL, 0, 0, 0);
  3260. #endif
  3261. w = gtk_check_button_new_with_label("Show server-side font aliases");
  3262. g_object_set_data(G_OBJECT(w), "user-data",
  3263. GINT_TO_POINTER(FONTFLAG_SERVERALIAS));
  3264. g_signal_connect(G_OBJECT(w), "toggled",
  3265. G_CALLBACK(unifontsel_button_toggled), fs);
  3266. gtk_widget_show(w);
  3267. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3268. #if GTK_CHECK_VERSION(3,0,0)
  3269. gtk_grid_attach(GTK_GRID(table), w, 0, 6, 3, 1);
  3270. g_object_set(G_OBJECT(w), "hexpand", true, (const char *)NULL);
  3271. #else
  3272. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 6, 7, GTK_FILL, 0, 0, 0);
  3273. #endif
  3274. #endif /* NOT_X_WINDOWS */
  3275. w = gtk_check_button_new_with_label("Show non-monospaced fonts");
  3276. g_object_set_data(G_OBJECT(w), "user-data",
  3277. GINT_TO_POINTER(FONTFLAG_NONMONOSPACED));
  3278. g_signal_connect(G_OBJECT(w), "toggled",
  3279. G_CALLBACK(unifontsel_button_toggled), fs);
  3280. gtk_widget_show(w);
  3281. fs->filter_buttons[fs->n_filter_buttons++] = w;
  3282. #if GTK_CHECK_VERSION(3,0,0)
  3283. gtk_grid_attach(GTK_GRID(table), w, 0, 7, 3, 1);
  3284. g_object_set(G_OBJECT(w), "hexpand", true, (const char *)NULL);
  3285. #else
  3286. gtk_table_attach(GTK_TABLE(table), w, 0, 3, 7, 8, GTK_FILL, 0, 0, 0);
  3287. #endif
  3288. assert(fs->n_filter_buttons <= lenof(fs->filter_buttons));
  3289. fs->filter_flags = FONTFLAG_CLIENTSIDE | FONTFLAG_SERVERSIDE |
  3290. FONTFLAG_SERVERALIAS;
  3291. unifontsel_set_filter_buttons(fs);
  3292. /*
  3293. * Go and find all the font names, and set up our master font
  3294. * list.
  3295. */
  3296. fs->fonts_by_realname = newtree234(fontinfo_realname_compare);
  3297. fs->fonts_by_selorder = newtree234(fontinfo_selorder_compare);
  3298. for (i = 0; i < lenof(unifont_types); i++)
  3299. unifont_types[i]->enum_fonts(GTK_WIDGET(fs->u.window),
  3300. unifontsel_add_entry, fs);
  3301. /*
  3302. * And set up the initial font names list.
  3303. */
  3304. unifontsel_setup_familylist(fs);
  3305. fs->selsize = fs->intendedsize = 13; /* random default */
  3306. gtk_widget_set_sensitive(fs->u.ok_button, false);
  3307. return &fs->u;
  3308. }
  3309. void unifontsel_destroy(unifontsel *fontsel)
  3310. {
  3311. unifontsel_internal *fs = container_of(fontsel, unifontsel_internal, u);
  3312. fontinfo *info;
  3313. #ifndef NO_BACKING_PIXMAPS
  3314. if (fs->preview_pixmap)
  3315. gdk_pixmap_unref(fs->preview_pixmap);
  3316. #endif
  3317. freetree234(fs->fonts_by_selorder);
  3318. while ((info = delpos234(fs->fonts_by_realname, 0)) != NULL)
  3319. sfree(info);
  3320. freetree234(fs->fonts_by_realname);
  3321. gtk_widget_destroy(GTK_WIDGET(fs->u.window));
  3322. sfree(fs);
  3323. }
  3324. void unifontsel_set_name(unifontsel *fontsel, const char *fontname)
  3325. {
  3326. unifontsel_internal *fs = container_of(fontsel, unifontsel_internal, u);
  3327. int i, start, end, size, flags;
  3328. const char *fontname2 = NULL;
  3329. fontinfo *info;
  3330. /*
  3331. * Provide a default if given an empty or null font name.
  3332. */
  3333. if (!fontname || !*fontname)
  3334. fontname = DEFAULT_GTK_FONT;
  3335. /*
  3336. * Call the canonify_fontname function.
  3337. */
  3338. fontname = unifont_do_prefix(fontname, &start, &end);
  3339. for (i = start; i < end; i++) {
  3340. fontname2 = unifont_types[i]->canonify_fontname(
  3341. GTK_WIDGET(fs->u.window), fontname, &size, &flags, false);
  3342. if (fontname2)
  3343. break;
  3344. }
  3345. if (i == end)
  3346. return; /* font name not recognised */
  3347. /*
  3348. * Now look up the canonified font name in our index.
  3349. */
  3350. {
  3351. struct fontinfo_realname_find f;
  3352. f.realname = fontname2;
  3353. f.flags = flags;
  3354. info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  3355. }
  3356. /*
  3357. * If we've found the font, and its size field is either
  3358. * correct or zero (the latter indicating a scalable font),
  3359. * then we're done. Otherwise, try looking up the original
  3360. * font name instead.
  3361. */
  3362. if (!info || (info->size != size && info->size != 0)) {
  3363. struct fontinfo_realname_find f;
  3364. f.realname = fontname;
  3365. f.flags = flags;
  3366. info = find234(fs->fonts_by_realname, &f, fontinfo_realname_find);
  3367. if (!info || info->size != size)
  3368. return; /* font name not in our index */
  3369. }
  3370. /*
  3371. * Now we've got a fontinfo structure and a font size, so we
  3372. * know everything we need to fill in all the fields in the
  3373. * dialog.
  3374. */
  3375. unifontsel_select_font(fs, info, size, 0, true);
  3376. }
  3377. char *unifontsel_get_name(unifontsel *fontsel)
  3378. {
  3379. unifontsel_internal *fs = container_of(fontsel, unifontsel_internal, u);
  3380. char *name;
  3381. if (!fs->selected)
  3382. return NULL;
  3383. if (fs->selected->size == 0) {
  3384. name = fs->selected->fontclass->scale_fontname(
  3385. GTK_WIDGET(fs->u.window), fs->selected->realname, fs->selsize);
  3386. if (name) {
  3387. char *ret = dupcat(fs->selected->fontclass->prefix, ":", name);
  3388. sfree(name);
  3389. return ret;
  3390. }
  3391. }
  3392. return dupcat(fs->selected->fontclass->prefix, ":",
  3393. fs->selected->realname);
  3394. }
  3395. #endif /* GTK_CHECK_VERSION(2,0,0) */