| 1 | /* Graphics_text.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2026 Paul Boersma, 2013 Tom Naughton, 2017 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "../kar/UnicodeData.h" |
| 20 | #include "GraphicsP.h" |
| 21 | #include "../kar/longchar.h" |
| 22 | #include "Printer.h" |
| 23 | |
| 24 | extern const char * ipaSerifRegularPS []; |
| 25 | |
| 26 | /* |
| 27 | When computing the width of a text by adding the widths of the separate characters, |
| 28 | we will make a correction for systems that make slanted characters overlap the character box to their right. |
| 29 | The effect is especially strong on Mac (older versions). |
| 30 | The slant correction is taken relative to the font size. |
| 31 | */ |
| 32 | #define POSTSCRIPT_SLANT_CORRECTION 0.1 |
| 33 | #define SCREEN_SLANT_CORRECTION 0.05 |
| 34 | |
| 35 | #define HAS_FI_AND_FL_LIGATURES ( my postScript == true ) |
| 36 | |
| 37 | #if cairo |
| 38 | PangoFontMap *thePangoFontMap; |
| 39 | PangoContext *thePangoContext; |
| 40 | static bool hasTimes, hasHelvetica, hasCourier, hasPalatino, hasDoulos, hasCharis, hasIpaSerif, hasCharis7; |
| 41 | #elif gdi |
| 42 | #define win_MAXIMUM_FONT_SIZE 500 |
| 43 | static HFONT fonts [1 + (int) kGraphics_resolution::MAX] [1 + kGraphics_font_JAPANESE] [1+win_MAXIMUM_FONT_SIZE] [1 + Graphics_BOLD_ITALIC]; |
| 44 | static int win_size2isize (int size) { return size > win_MAXIMUM_FONT_SIZE ? win_MAXIMUM_FONT_SIZE : size; } |
| 45 | static int win_isize2size (int isize) { return isize; } |
| 46 | static bool hasDoulos, hasCharis, hasCharis7; |
| 47 | #elif quartz |
| 48 | static bool hasTimes, hasHelvetica, hasCourier, hasPalatino, hasDoulos, hasCharis, hasIpaSerif, hasCharis7; |
| 49 | #define mac_MAXIMUM_FONT_SIZE 500 |
| 50 | static CTFontRef theScreenFonts [1 + kGraphics_font_DINGBATS] [1+mac_MAXIMUM_FONT_SIZE] [1 + Graphics_BOLD_ITALIC]; |
| 51 | #endif |
| 52 | |
| 53 | #if gdi |
| 54 | #ifdef __CYGWIN__ |
| 55 | #define FONT_TYPE_TYPE unsigned int |
| 56 | #else |
| 57 | #define FONT_TYPE_TYPE unsigned long int |
| 58 | #endif |
| 59 | static int CALLBACK fontFuncEx_charis (const LOGFONTW *oldLogFont, const TEXTMETRICW *oldTextMetric, FONT_TYPE_TYPE fontType, LPARAM lparam) { |
| 60 | const LPENUMLOGFONTW logFont = (LPENUMLOGFONTW) oldLogFont; (void) oldTextMetric; (void) fontType; (void) lparam; |
| 61 | hasCharis = true; |
| 62 | return 1; |
| 63 | } |
| 64 | static int CALLBACK fontFuncEx_doulos (const LOGFONTW *oldLogFont, const TEXTMETRICW *oldTextMetric, FONT_TYPE_TYPE fontType, LPARAM lparam) { |
| 65 | const LPENUMLOGFONTW logFont = (LPENUMLOGFONTW) oldLogFont; (void) oldTextMetric; (void) fontType; (void) lparam; |
| 66 | hasDoulos = true; |
| 67 | return 1; |
| 68 | } |
| 69 | static HFONT loadFont (GraphicsScreen me, int font, int size, int style) { |
| 70 | LOGFONTW spec; |
| 71 | static int ipaInited; |
| 72 | if (my printer || my metafile) { |
| 73 | spec. lfHeight = - win_isize2size (size) * my resolution / 72.0; |
| 74 | } else { |
| 75 | spec. lfHeight = - win_isize2size (size) * my resolution / 72.0; |
| 76 | } |
| 77 | spec. lfWidth = 0; |
| 78 | spec. lfEscapement = spec. lfOrientation = 0; |
| 79 | spec. lfWeight = style & Graphics_BOLD ? FW_BOLD : 0; |
| 80 | spec. lfItalic = style & Graphics_ITALIC ? 1 : 0; |
| 81 | spec. lfUnderline = spec. lfStrikeOut = 0; |
| 82 | spec. lfCharSet = |
| 83 | font == kGraphics_font_SYMBOL ? SYMBOL_CHARSET : |
| 84 | font == kGraphics_font_CHINESE ? DEFAULT_CHARSET : |
| 85 | font == kGraphics_font_JAPANESE ? DEFAULT_CHARSET : |
| 86 | font >= kGraphics_font_IPATIMES ? DEFAULT_CHARSET : |
| 87 | ANSI_CHARSET; |
| 88 | spec. lfOutPrecision = spec. lfClipPrecision = spec. lfQuality = 0; |
| 89 | spec. lfPitchAndFamily = |
| 90 | ( font == (int) kGraphics_font::COURIER ? FIXED_PITCH : font == kGraphics_font_IPATIMES ? DEFAULT_PITCH : VARIABLE_PITCH ) | |
| 91 | ( font == (int) kGraphics_font::HELVETICA ? FF_SWISS : font == (int) kGraphics_font::COURIER ? FF_MODERN : |
| 92 | font == kGraphics_font_CHINESE ? FF_DONTCARE : |
| 93 | font == kGraphics_font_JAPANESE ? FF_DONTCARE : |
| 94 | font >= kGraphics_font_IPATIMES ? FF_DONTCARE : FF_ROMAN ); |
| 95 | if (font == kGraphics_font_IPATIMES && ! ipaInited && ! (Melder_debug == 15)) { |
| 96 | LOGFONTW logFont; |
| 97 | logFont. lfCharSet = DEFAULT_CHARSET; |
| 98 | logFont. lfPitchAndFamily = 0; |
| 99 | wcscpy (logFont. lfFaceName, L"Charis"); // try Charis 7 |
| 100 | EnumFontFamiliesExW (my d_gdiGraphicsContext, & logFont, fontFuncEx_charis, 0, 0); |
| 101 | if (hasCharis) { |
| 102 | hasCharis7 = true; |
| 103 | } else { |
| 104 | wcscpy (logFont. lfFaceName, L"Charis SIL"); // try Charis 6 |
| 105 | EnumFontFamiliesExW (my d_gdiGraphicsContext, & logFont, fontFuncEx_charis, 0, 0); |
| 106 | } |
| 107 | wcscpy (logFont. lfFaceName, L"Doulos SIL"); |
| 108 | EnumFontFamiliesExW (my d_gdiGraphicsContext, & logFont, fontFuncEx_doulos, 0, 0); |
| 109 | ipaInited = true; |
| 110 | if (! hasCharis && ! hasDoulos) { |
| 111 | /* BUG: The next warning may cause reentry of drawing (on window exposure) and lead to crash. Some code must be non-reentrant !! */ |
| 112 | Melder_warning (U"The phonetic font is not available.\n" |
| 113 | "Several characters may not look correct.\n" |
| 114 | "You can download phonetics fonts via praat.org " |
| 115 | "(go to the download page for Windows)." |
| 116 | ); |
| 117 | } |
| 118 | } |
| 119 | wcscpy (spec. lfFaceName, |
| 120 | font == (int) kGraphics_font::HELVETICA ? L"Arial" : |
| 121 | font == (int) kGraphics_font::TIMES ? L"Times New Roman" : |
| 122 | font == (int) kGraphics_font::COURIER ? L"Courier New" : |
| 123 | font == (int) kGraphics_font::PALATINO ? L"Book Antiqua" : |
| 124 | font == kGraphics_font_SYMBOL ? L"Symbol" : |
| 125 | font == kGraphics_font_IPATIMES ? ( hasDoulos && style == 0 ? L"Doulos SIL" |
| 126 | : hasCharis ? ( hasCharis7 ? L"Charis" : L"Charis SIL" ) |
| 127 | : L"Times New Roman" |
| 128 | ) : |
| 129 | font == kGraphics_font_DINGBATS ? L"Wingdings" : |
| 130 | font == kGraphics_font_CHINESE ? L"SimSun" : |
| 131 | font == kGraphics_font_JAPANESE ? L"MS UI Gothic" : |
| 132 | L""); |
| 133 | return CreateFontIndirectW (& spec); |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | #if cairo |
| 138 | static PangoFontDescription *PangoFontDescription_create (int font, _Graphics_widechar *lc) { |
| 139 | static PangoFontDescription *fontDescriptions [1 + kGraphics_font_DINGBATS]; |
| 140 | Melder_assert (font >= 0 && font <= kGraphics_font_DINGBATS); |
| 141 | if (! fontDescriptions [font]) { |
| 142 | const char *fontFace = |
| 143 | font == (int) kGraphics_font::HELVETICA ? "Helvetica" : |
| 144 | font == (int) kGraphics_font::TIMES ? "Times" : |
| 145 | font == (int) kGraphics_font::COURIER ? "Courier New" : |
| 146 | font == (int) kGraphics_font::PALATINO ? "Palatino" : |
| 147 | font == kGraphics_font_IPATIMES ? "Doulos SIL" : |
| 148 | font == kGraphics_font_IPAPALATINO ? ( hasCharis7 ? "Charis" : "Charis SIL" ) : |
| 149 | font == kGraphics_font_DINGBATS ? "Dingbats" : "Serif"; |
| 150 | fontDescriptions [font] = pango_font_description_from_string (fontFace); |
| 151 | } |
| 152 | |
| 153 | PangoStyle slant = (lc -> style & Graphics_ITALIC ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL); |
| 154 | pango_font_description_set_style (fontDescriptions [font], slant); |
| 155 | |
| 156 | PangoWeight weight = (lc -> style & Graphics_BOLD ? PANGO_WEIGHT_BOLD : PANGO_WEIGHT_NORMAL); |
| 157 | pango_font_description_set_weight (fontDescriptions [font], weight); |
| 158 | pango_font_description_set_absolute_size (fontDescriptions [font], (int) (lc -> size * PANGO_SCALE)); |
| 159 | return fontDescriptions [font]; |
| 160 | } |
| 161 | #endif |
| 162 | |
| 163 | inline static bool isDiacritic (Longchar_Info info, int font) { |
| 164 | if (info -> isDiacritic == 0) |
| 165 | return false; |
| 166 | if (info -> isDiacritic == 1) |
| 167 | return true; |
| 168 | Melder_assert (info -> isDiacritic == 2); // corner |
| 169 | if (font == kGraphics_font_IPATIMES || font == kGraphics_font_IPAPALATINO) |
| 170 | return false; // Doulos or Charis |
| 171 | return true; // e.g. Times substitutes a zero-width corner |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | Most operating systems provide automatic font substitution nowadays, |
| 176 | so that e.g. phonetic characters will be drawn recognizably even if the |
| 177 | user-preferred font is e.g. Times, Helvetica, Courier or Palatino |
| 178 | and the phonetic characters are not available in that user font. |
| 179 | |
| 180 | However, on some systems the substituted font might really be a last resort |
| 181 | font that does not look at all similar to the user font. An example is |
| 182 | the use of the sans-serif font Lucida Grande for phonetic characters |
| 183 | within a stretch of a serif font such as Times or Palatino. |
| 184 | |
| 185 | This is not good enough for Praat. We need more control over the shape |
| 186 | of phonetic characters. We therefore advise the use of Doulos SIL, |
| 187 | which is Times-like, or Charis, which is Palatino-like. |
| 188 | For true continuity between non-phonetic and phonetic characters it is |
| 189 | mandatory that the exact same font is used for both types of characters, |
| 190 | so we use Doulos SIL to replace Times even for non-phonetic characters, |
| 191 | and Charis to replace Palatino even for non-phonetic characters. |
| 192 | A technical issue that makes this even more important is that diacritics |
| 193 | can look really weird if at the beginning of a Praat font stretch: |
| 194 | a "b" followed by a ring below will not be aligned correctly if they |
| 195 | are part of different Praat font stretches. |
| 196 | |
| 197 | Beside Praat-enforced visual font continuity, there are some more issues, |
| 198 | such as that the "/" (slash) character should extend below the baseline |
| 199 | whenever it is used in an equation or to demarcate a phonological |
| 200 | representation. |
| 201 | */ |
| 202 | #if cairo || quartz |
| 203 | inline static int chooseFont (Graphics me, _Graphics_widechar *lc) { |
| 204 | /* |
| 205 | When we arrive here, the character's font is the "user-preferred font", |
| 206 | which is Courier if the user asked for code style (e.g. between "$$" and "$"), |
| 207 | or otherwise Times, Helvetica, Courier or Palatino, as chosen from a font menu. |
| 208 | |
| 209 | Exception: if the character is a slash, its font has already been converted to Courier. |
| 210 | */ |
| 211 | int font = lc -> font.integer_; |
| 212 | Longchar_Info info = lc -> karInfo; |
| 213 | int alphabet = info -> alphabet; |
| 214 | |
| 215 | if (font == (int) kGraphics_font::COURIER) { |
| 216 | constexpr bool systemSubstitutesMonospacedSerifFontForIpaCourier = (quartz); |
| 217 | if (systemSubstitutesMonospacedSerifFontForIpaCourier) { |
| 218 | /* |
| 219 | No need to check whether the character is phonetic or not. |
| 220 | */ |
| 221 | return (int) kGraphics_font::COURIER; |
| 222 | } |
| 223 | if (alphabet == Longchar_SYMBOL || |
| 224 | alphabet == Longchar_PHONETIC || |
| 225 | lc [1]. kar > U'\t' && lc [1]. karInfo -> isDiacritic) // inspect next character to ensure diacritic continuity |
| 226 | { |
| 227 | /* |
| 228 | Serif is more important than monospaced, |
| 229 | and Charis looks slightly better within Courier than Doulos does. |
| 230 | */ |
| 231 | if (hasCharis) |
| 232 | return kGraphics_font_IPAPALATINO; |
| 233 | if (hasDoulos) |
| 234 | return kGraphics_font_IPATIMES; |
| 235 | } |
| 236 | return (int) kGraphics_font::COURIER; |
| 237 | } |
| 238 | font = |
| 239 | alphabet == Longchar_SYMBOL || // ? kGraphics_font_SYMBOL : |
| 240 | alphabet == Longchar_PHONETIC ? |
| 241 | ( my font == kGraphics_font::TIMES ? |
| 242 | ( hasDoulos ? |
| 243 | ( lc -> style == 0 ? |
| 244 | kGraphics_font_IPATIMES : |
| 245 | hasCharis ? |
| 246 | kGraphics_font_IPAPALATINO : // other styles in Charis, because Doulos has no bold or italic |
| 247 | (int) kGraphics_font::TIMES |
| 248 | ) : |
| 249 | hasCharis ? |
| 250 | kGraphics_font_IPAPALATINO : |
| 251 | (int) kGraphics_font::TIMES // on newer systems, Times and Times New Roman have a lot of phonetic characters |
| 252 | ) : |
| 253 | my font == kGraphics_font::HELVETICA ? |
| 254 | (int) kGraphics_font::HELVETICA : // sans serif, so fall back on Lucida Grande or so for phonetic characters |
| 255 | /* my font must be kGraphics_font_PALATINO */ |
| 256 | hasCharis && ! (Melder_debug == 900) ? |
| 257 | kGraphics_font_IPAPALATINO : |
| 258 | hasDoulos && ! (Melder_debug == 900) ? |
| 259 | ( lc -> style == 0 ? |
| 260 | kGraphics_font_IPATIMES : |
| 261 | (int) kGraphics_font::TIMES |
| 262 | ) : |
| 263 | (int) kGraphics_font::PALATINO |
| 264 | ) : |
| 265 | alphabet == Longchar_DINGBATS ? |
| 266 | kGraphics_font_DINGBATS : |
| 267 | lc -> kar >= 0x13A0 && lc -> kar <= 0x13FF ? |
| 268 | kGraphics_font_CHEROKEE : |
| 269 | my font == kGraphics_font::TIMES ? |
| 270 | ( hasDoulos ? |
| 271 | ( lc -> style == 0 ? |
| 272 | kGraphics_font_IPATIMES : |
| 273 | lc -> style == Graphics_ITALIC ? |
| 274 | ( lc [1]. kar > U'\t' && lc [1]. karInfo -> isDiacritic && hasCharis ? |
| 275 | kGraphics_font_IPAPALATINO : (int) kGraphics_font::TIMES ) : // correct placement of diacritics |
| 276 | hasCharis ? |
| 277 | kGraphics_font_IPAPALATINO : |
| 278 | (int) kGraphics_font::TIMES |
| 279 | ) : |
| 280 | (int) kGraphics_font::TIMES |
| 281 | ) : |
| 282 | my font == kGraphics_font::HELVETICA ? |
| 283 | (int) kGraphics_font::HELVETICA : |
| 284 | my font == kGraphics_font::PALATINO ? |
| 285 | ( hasCharis && ! (Melder_debug == 900) ? |
| 286 | kGraphics_font_IPAPALATINO : |
| 287 | (int) kGraphics_font::PALATINO |
| 288 | ) : |
| 289 | (int) my font; // why not lc -> font.integer_? |
| 290 | Melder_assert (font >= 0 && font <= kGraphics_font_DINGBATS); |
| 291 | return font; |
| 292 | } |
| 293 | #endif |
| 294 | |
| 295 | static void charSize (Graphics anyGraphics, _Graphics_widechar *lc) { |
| 296 | if (anyGraphics -> screen) { |
| 297 | #if cairo |
| 298 | GraphicsScreen me = static_cast <GraphicsScreen> (anyGraphics); |
| 299 | Melder_assert (my duringXor); |
| 300 | const int normalSize = my fontSize * my resolution / 72.0; |
| 301 | const int smallSize = (3 * normalSize + 2) / 4; |
| 302 | const int size = ( lc -> size < 100 ? smallSize : normalSize ); |
| 303 | lc -> width = 7; |
| 304 | lc -> baseline *= my fontSize * 0.01; |
| 305 | lc -> code = lc -> kar; |
| 306 | lc -> font.string = nullptr; |
| 307 | lc -> font.integer_ = 0; |
| 308 | lc -> size = size; |
| 309 | #elif gdi |
| 310 | GraphicsScreen me = static_cast <GraphicsScreen> (anyGraphics); |
| 311 | Longchar_Info info = lc -> karInfo; |
| 312 | const int normalSize = win_size2isize (my fontSize); |
| 313 | const int smallSize = (3 * normalSize + 2) / 4; |
| 314 | int font = info -> alphabet == Longchar_SYMBOL ? kGraphics_font_SYMBOL : |
| 315 | info -> alphabet == Longchar_PHONETIC ? kGraphics_font_IPATIMES : |
| 316 | info -> alphabet == Longchar_DINGBATS ? kGraphics_font_DINGBATS : lc -> font.integer_; |
| 317 | if ((unsigned int) lc -> kar >= 0x2E80 && (unsigned int) lc -> kar <= 0x9FFF) |
| 318 | font = ( theGraphicsCjkFontStyle == kGraphics_cjkFontStyle::CHINESE ? kGraphics_font_CHINESE : kGraphics_font_JAPANESE ); |
| 319 | const int size = ( lc -> size < 100 ? smallSize : normalSize ); |
| 320 | const int style = lc -> style & (Graphics_ITALIC | Graphics_BOLD); // take out Graphics_CODE |
| 321 | HFONT fontInfo = fonts [(int) my resolutionNumber] [font] [size] [style]; |
| 322 | if (! fontInfo) { |
| 323 | fontInfo = loadFont (me, font, size, style); |
| 324 | if (! fontInfo) |
| 325 | return; |
| 326 | fonts [(int) my resolutionNumber] [font] [size] [style] = fontInfo; |
| 327 | } |
| 328 | SIZE extent; |
| 329 | lc -> code = |
| 330 | font == kGraphics_font_IPATIMES || |
| 331 | font == (int) kGraphics_font::TIMES || |
| 332 | font == (int) kGraphics_font::HELVETICA || |
| 333 | font == kGraphics_font_CHINESE || |
| 334 | font == kGraphics_font_JAPANESE || |
| 335 | font == (int) kGraphics_font::COURIER ? lc -> kar : |
| 336 | info -> winEncoding; |
| 337 | if (lc -> code == 0) { |
| 338 | _Graphics_widechar *lc2; |
| 339 | if (lc -> kar == UNICODE_LATIN_SMALL_LETTER_SCHWA_WITH_HOOK) { |
| 340 | info = Longchar_getInfo (U's', U'w'); |
| 341 | lc -> kar = info -> unicode; |
| 342 | lc -> code = info -> winEncoding; |
| 343 | for (lc2 = lc + 1; lc2 -> kar != U'\0'; lc2 ++) { } |
| 344 | lc2 [1]. kar = U'\0'; |
| 345 | while (lc2 - lc > 0) { |
| 346 | lc2 [0] = lc2 [-1]; |
| 347 | lc2 --; |
| 348 | } |
| 349 | lc [1]. kar = UNICODE_MODIFIER_LETTER_RHOTIC_HOOK; |
| 350 | } else if (lc -> kar == UNICODE_LATIN_SMALL_LETTER_L_WITH_MIDDLE_TILDE) { |
| 351 | info = Longchar_getInfo (U'l', U' '); |
| 352 | lc -> kar = info -> unicode; |
| 353 | lc -> code = info -> winEncoding; |
| 354 | for (lc2 = lc + 1; lc2 -> kar != U'\0'; lc2 ++) { } |
| 355 | lc2 [1]. kar = U'\0'; |
| 356 | while (lc2 - lc > 0) { |
| 357 | lc2 [0] = lc2 [-1]; |
| 358 | lc2 --; |
| 359 | } |
| 360 | lc [1]. kar = UNICODE_COMBINING_TILDE_OVERLAY; |
| 361 | } |
| 362 | } |
| 363 | SelectFont (my d_gdiGraphicsContext, fontInfo); |
| 364 | if (lc -> code <= 0x00FFFF) { |
| 365 | char16 code = (char16) lc -> code; |
| 366 | GetTextExtentPoint32W (my d_gdiGraphicsContext, (WCHAR *) & code, 1, & extent); |
| 367 | } else { |
| 368 | char32 code [2] { lc -> code, U'\0' }; |
| 369 | GetTextExtentPoint32W (my d_gdiGraphicsContext, Melder_peek32toW (code), 2, & extent); |
| 370 | } |
| 371 | lc -> width = extent. cx; |
| 372 | lc -> baseline *= my fontSize * 0.01 * my resolution / 72.0; |
| 373 | lc -> font.string = nullptr; |
| 374 | lc -> font.integer_ = font; // kGraphics_font_HELVETICA .. kGraphics_font_DINGBATS |
| 375 | lc -> size = size; // 0..4 instead of 10..24 |
| 376 | lc -> style = style; // without Graphics_CODE |
| 377 | #elif quartz |
| 378 | #endif |
| 379 | } else if (anyGraphics -> postScript) { |
| 380 | GraphicsPostscript me = static_cast <GraphicsPostscript> (anyGraphics); |
| 381 | const int normalSize = (int) ((double) my fontSize * (double) my resolution / 72.0); |
| 382 | Longchar_Info info = lc -> karInfo; |
| 383 | const int font = |
| 384 | info -> alphabet == Longchar_SYMBOL ? kGraphics_font_SYMBOL : |
| 385 | info -> alphabet == Longchar_PHONETIC ? kGraphics_font_IPATIMES : |
| 386 | info -> alphabet == Longchar_DINGBATS ? kGraphics_font_DINGBATS : |
| 387 | lc -> font.integer_ |
| 388 | ; |
| 389 | const int style = |
| 390 | lc -> style == Graphics_ITALIC ? Graphics_ITALIC : |
| 391 | lc -> style == Graphics_BOLD || lc -> link ? Graphics_BOLD : |
| 392 | lc -> style == Graphics_BOLD_ITALIC ? Graphics_BOLD_ITALIC : |
| 393 | 0 |
| 394 | ; |
| 395 | if (! my fontInfos [font] [style]) { |
| 396 | const char *fontInfo, *secondaryFontInfo = nullptr, *tertiaryFontInfo = nullptr; |
| 397 | if (font == (int) kGraphics_font::COURIER) { |
| 398 | fontInfo = style == Graphics_BOLD ? "Courier-Bold" : |
| 399 | style == Graphics_ITALIC ? "Courier-Oblique" : |
| 400 | style == Graphics_BOLD_ITALIC ? "Courier-BoldOblique" : "Courier"; |
| 401 | secondaryFontInfo = style == Graphics_BOLD ? "CourierNewPS-BoldMT" : |
| 402 | style == Graphics_ITALIC ? "CourierNewPS-ItalicMT" : |
| 403 | style == Graphics_BOLD_ITALIC ? "CourierNewPS-BoldItalicMT" : "CourierNewPSMT"; |
| 404 | tertiaryFontInfo = style == Graphics_BOLD ? "CourierNew-Bold" : |
| 405 | style == Graphics_ITALIC ? "CourierNew-Italic" : |
| 406 | style == Graphics_BOLD_ITALIC ? "CourierNew-BoldItalic" : "CourierNew"; |
| 407 | } else if (font == (int) kGraphics_font::TIMES) { |
| 408 | fontInfo = style == Graphics_BOLD ? "Times-Bold" : |
| 409 | style == Graphics_ITALIC ? "Times-Italic" : |
| 410 | style == Graphics_BOLD_ITALIC ? "Times-BoldItalic" : "Times-Roman"; |
| 411 | secondaryFontInfo = style == Graphics_BOLD ? "TimesNewRomanPS-BoldMT" : |
| 412 | style == Graphics_ITALIC ? "TimesNewRomanPS-ItalicMT" : |
| 413 | style == Graphics_BOLD_ITALIC ? "TimesNewRomanPS-BoldItalicMT" : "TimesNewRomanPSMT"; |
| 414 | tertiaryFontInfo = style == Graphics_BOLD ? "TimesNewRoman-Bold" : |
| 415 | style == Graphics_ITALIC ? "TimesNewRoman-Italic" : |
| 416 | style == Graphics_BOLD_ITALIC ? "TimesNewRoman-BoldItalic" : "TimesNewRoman"; |
| 417 | } else if (font == (int) kGraphics_font::PALATINO) { |
| 418 | fontInfo = style == Graphics_BOLD ? "Palatino-Bold" : |
| 419 | style == Graphics_ITALIC ? "Palatino-Italic" : |
| 420 | style == Graphics_BOLD_ITALIC ? "Palatino-BoldItalic" : "Palatino-Roman"; |
| 421 | secondaryFontInfo = style == Graphics_BOLD ? "BookAntiquaPS-BoldMT" : |
| 422 | style == Graphics_ITALIC ? "BookAntiquaPS-ItalicMT" : |
| 423 | style == Graphics_BOLD_ITALIC ? "BookAntiquaPS-BoldItalicMT" : "BookAntiquaPSMT"; |
| 424 | tertiaryFontInfo = style == Graphics_BOLD ? "BookAntiqua-Bold" : |
| 425 | style == Graphics_ITALIC ? "BookAntiqua-Italic" : |
| 426 | style == Graphics_BOLD_ITALIC ? "BookAntiqua-BoldItalic" : "BookAntiqua"; |
| 427 | } else if (font == kGraphics_font_IPATIMES) { |
| 428 | if (my includeFonts && ! my loadedXipa) { |
| 429 | const char **p; |
| 430 | for (p = & ipaSerifRegularPS [0]; *p; p ++) |
| 431 | my d_printf (my d_file, "%s", *p); |
| 432 | my loadedXipa = true; |
| 433 | } |
| 434 | fontInfo = my useSilipaPS ? |
| 435 | (style == Graphics_BOLD || style == Graphics_BOLD_ITALIC ? "SILDoulosIPA93Bold" : "SILDoulosIPA93Regular") : |
| 436 | "TeX-xipa10-Praat-Regular"; |
| 437 | } else if (font == kGraphics_font_SYMBOL) { |
| 438 | fontInfo = "Symbol"; |
| 439 | } else if (font == kGraphics_font_DINGBATS) { |
| 440 | fontInfo = "ZapfDingbats"; |
| 441 | } else { |
| 442 | fontInfo = style == Graphics_BOLD ? "Helvetica-Bold" : |
| 443 | style == Graphics_ITALIC ? "Helvetica-Oblique" : |
| 444 | style == Graphics_BOLD_ITALIC ? "Helvetica-BoldOblique" : "Helvetica"; |
| 445 | secondaryFontInfo = style == Graphics_BOLD ? "Arial-BoldMT" : |
| 446 | style == Graphics_ITALIC ? "Arial-ItalicMT" : |
| 447 | style == Graphics_BOLD_ITALIC ? "Arial-BoldItalicMT" : "ArialMT"; |
| 448 | tertiaryFontInfo = style == Graphics_BOLD ? "Arial-Bold" : |
| 449 | style == Graphics_ITALIC ? "Arial-Italic" : |
| 450 | style == Graphics_BOLD_ITALIC ? "Arial-BoldItalic" : "Arial"; |
| 451 | } |
| 452 | my fontInfos [font] [style] = Melder_malloc_f (char, 200); |
| 453 | if (font == kGraphics_font_IPATIMES || font == kGraphics_font_SYMBOL || font == kGraphics_font_DINGBATS) { |
| 454 | strcpy (my fontInfos [font] [style], fontInfo); |
| 455 | } else { |
| 456 | snprintf (my fontInfos [font] [style],200, "%s-Praat", fontInfo); |
| 457 | if (thePrinter. fontChoiceStrategy == kGraphicsPostscript_fontChoiceStrategy::LINOTYPE) { |
| 458 | my d_printf (my d_file, "/%s /%s-Praat PraatEncode\n", fontInfo, fontInfo); |
| 459 | } else if (thePrinter. fontChoiceStrategy == kGraphicsPostscript_fontChoiceStrategy::MONOTYPE) { |
| 460 | my d_printf (my d_file, "/%s /%s-Praat PraatEncode\n", tertiaryFontInfo, fontInfo); |
| 461 | } else if (thePrinter. fontChoiceStrategy == kGraphicsPostscript_fontChoiceStrategy::PS_MONOTYPE) { |
| 462 | my d_printf (my d_file, "/%s /%s-Praat PraatEncode\n", secondaryFontInfo, fontInfo); |
| 463 | } else { |
| 464 | /* Automatic font choice strategy. */ |
| 465 | if (secondaryFontInfo) { |
| 466 | my d_printf (my d_file, |
| 467 | "/%s /Font resourcestatus\n" |
| 468 | "{ pop pop /%s /%s-Praat PraatEncode }\n" |
| 469 | "{ /%s /%s-Praat PraatEncode }\n" |
| 470 | "ifelse\n", |
| 471 | secondaryFontInfo, secondaryFontInfo, fontInfo, fontInfo, fontInfo); |
| 472 | } else { |
| 473 | my d_printf (my d_file, "/%s /%s-Praat PraatEncode\n", fontInfo, fontInfo); |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | lc -> font.integer_ = 0; |
| 479 | lc -> font.string = my fontInfos [font] [style]; |
| 480 | |
| 481 | /* |
| 482 | * Convert size and baseline information to device coordinates. |
| 483 | */ |
| 484 | lc -> size *= normalSize * 0.01; |
| 485 | lc -> baseline *= normalSize * 0.01; |
| 486 | |
| 487 | if (font == (int) kGraphics_font::COURIER) { |
| 488 | lc -> width = 600; // Courier |
| 489 | } else if (style == 0) { |
| 490 | if (font == (int) kGraphics_font::TIMES) |
| 491 | lc -> width = info -> ps.times; |
| 492 | else if (font == (int) kGraphics_font::HELVETICA) |
| 493 | lc -> width = info -> ps.helvetica; |
| 494 | else if (font == (int) kGraphics_font::PALATINO) |
| 495 | lc -> width = info -> ps.palatino; |
| 496 | else if (font == kGraphics_font_SYMBOL) |
| 497 | lc -> width = info -> ps.times; |
| 498 | else if (my useSilipaPS) |
| 499 | lc -> width = info -> ps.timesItalic; // ? |
| 500 | else |
| 501 | lc -> width = info -> ps.times; // XIPA |
| 502 | } else if (style == Graphics_BOLD) { |
| 503 | if (font == (int) kGraphics_font::TIMES) |
| 504 | lc -> width = info -> ps.timesBold; |
| 505 | else if (font == (int) kGraphics_font::HELVETICA) |
| 506 | lc -> width = info -> ps.helveticaBold; |
| 507 | else if (font == (int) kGraphics_font::PALATINO) |
| 508 | lc -> width = info -> ps.palatinoBold; |
| 509 | else if (font == kGraphics_font_SYMBOL) |
| 510 | lc -> width = info -> ps.times; |
| 511 | else if (my useSilipaPS) |
| 512 | lc -> width = info -> ps.timesBoldItalic; // ? |
| 513 | else |
| 514 | lc -> width = info -> ps.times; // Symbol, IPA |
| 515 | } else if (style == Graphics_ITALIC) { |
| 516 | if (font == (int) kGraphics_font::TIMES) |
| 517 | lc -> width = info -> ps.timesItalic; |
| 518 | else if (font == (int) kGraphics_font::HELVETICA) |
| 519 | lc -> width = info -> ps.helvetica; |
| 520 | else if (font == (int) kGraphics_font::PALATINO) |
| 521 | lc -> width = info -> ps.palatinoItalic; |
| 522 | else if (font == kGraphics_font_SYMBOL) |
| 523 | lc -> width = info -> ps.times; |
| 524 | else if (my useSilipaPS) |
| 525 | lc -> width = info -> ps.timesItalic; |
| 526 | else |
| 527 | lc -> width = info -> ps.times; // Symbol, IPA |
| 528 | } else if (style == Graphics_BOLD_ITALIC) { |
| 529 | if (font == (int) kGraphics_font::TIMES) |
| 530 | lc -> width = info -> ps.timesBoldItalic; |
| 531 | else if (font == (int) kGraphics_font::HELVETICA) |
| 532 | lc -> width = info -> ps.helveticaBold; |
| 533 | else if (font == (int) kGraphics_font::PALATINO) |
| 534 | lc -> width = info -> ps.palatinoBoldItalic; |
| 535 | else if (font == kGraphics_font_SYMBOL) |
| 536 | lc -> width = info -> ps.times; |
| 537 | else if (my useSilipaPS) |
| 538 | lc -> width = info -> ps.timesBoldItalic; |
| 539 | else |
| 540 | lc -> width = info -> ps.times; // Symbol, IPA |
| 541 | } |
| 542 | lc -> width *= lc -> size / 1000.0; |
| 543 | lc -> code = ( font == kGraphics_font_IPATIMES && my useSilipaPS ? info -> macEncoding : info -> psEncoding ); |
| 544 | if (lc -> code == 0) { |
| 545 | _Graphics_widechar *lc2; |
| 546 | if (lc -> kar == UNICODE_LATIN_SMALL_LETTER_SCHWA_WITH_HOOK) { |
| 547 | info = Longchar_getInfo (U's', U'w'); |
| 548 | lc -> kar = info -> unicode; |
| 549 | lc -> code = info -> macEncoding; |
| 550 | lc -> width = info -> ps.timesItalic * lc -> size / 1000.0; |
| 551 | for (lc2 = lc + 1; lc2 -> kar != U'\0'; lc2 ++) { } |
| 552 | lc2 [1]. kar = U'\0'; |
| 553 | while (lc2 - lc > 0) { |
| 554 | lc2 [0] = lc2 [-1]; |
| 555 | lc2 --; |
| 556 | } |
| 557 | lc [1]. kar = UNICODE_MODIFIER_LETTER_RHOTIC_HOOK; |
| 558 | } else if (lc -> kar == UNICODE_LATIN_SMALL_LETTER_L_WITH_MIDDLE_TILDE) { |
| 559 | info = Longchar_getInfo (U'l', U' '); |
| 560 | lc -> code = info -> macEncoding; |
| 561 | lc -> kar = info -> unicode; |
| 562 | lc -> width = info -> ps.timesItalic * lc -> size / 1000.0; |
| 563 | for (lc2 = lc + 1; lc2 -> kar != U'\0'; lc2 ++) { } |
| 564 | lc2 [1]. kar = U'\0'; |
| 565 | while (lc2 - lc > 0) { |
| 566 | lc2 [0] = lc2 [-1]; |
| 567 | lc2 --; |
| 568 | } |
| 569 | lc [1]. kar = UNICODE_COMBINING_TILDE_OVERLAY; |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | #if quartz |
| 576 | static conststring32 quartz_getFontName (int font, int style) { |
| 577 | if (Melder_systemVersion < 110000) |
| 578 | style = 0; // style extension in the name was not needed on macOS X |
| 579 | switch (font) { |
| 580 | case (int) kGraphics_font::TIMES: |
| 581 | return |
| 582 | style == 0 ? U"Times New Roman" |
| 583 | : style == Graphics_BOLD ? U"Times New Roman Bold" |
| 584 | : style == Graphics_ITALIC ? U"Times New Roman Italic" |
| 585 | : U"Times New Roman Bold Italic"; |
| 586 | case (int) kGraphics_font::HELVETICA: |
| 587 | return |
| 588 | style == 0 ? U"Arial" |
| 589 | : style == Graphics_BOLD ? U"Arial Bold" |
| 590 | : style == Graphics_ITALIC ? U"Arial Italic" |
| 591 | : U"Arial Bold Italic"; |
| 592 | case (int) kGraphics_font::COURIER: |
| 593 | return |
| 594 | style == 0 ? U"Courier New" |
| 595 | : style == Graphics_BOLD ? U"Courier New Bold" |
| 596 | : style == Graphics_ITALIC ? U"Courier New Italic" |
| 597 | : U"Courier New Bold Italic"; |
| 598 | case (int) kGraphics_font::PALATINO: |
| 599 | if (Melder_debug == 900) |
| 600 | return U"DG Meta Serif Science"; |
| 601 | else |
| 602 | return |
| 603 | style == 0 ? U"Palatino" |
| 604 | : style == Graphics_BOLD ? U"Palatino Bold" |
| 605 | : style == Graphics_ITALIC ? U"Palatino Italic" |
| 606 | : U"Palatino Bold Italic"; |
| 607 | case kGraphics_font_SYMBOL: |
| 608 | return U"Symbol"; |
| 609 | case kGraphics_font_IPATIMES: |
| 610 | return U"Doulos SIL"; |
| 611 | case kGraphics_font_IPAPALATINO: |
| 612 | if (hasCharis7) |
| 613 | return |
| 614 | style == 0 ? U"Charis" |
| 615 | : style == Graphics_BOLD ? U"Charis Bold" |
| 616 | : style == Graphics_ITALIC ? U"Charis Italic" |
| 617 | : U"Charis Bold Italic"; |
| 618 | else |
| 619 | return |
| 620 | style == 0 ? U"Charis SIL" |
| 621 | : style == Graphics_BOLD ? U"Charis SIL Bold" |
| 622 | : style == Graphics_ITALIC ? U"Charis SIL Italic" |
| 623 | : U"Charis SIL Bold Italic"; |
| 624 | case kGraphics_font_CHEROKEE: |
| 625 | return U"Plantagenet Cherokee"; |
| 626 | case kGraphics_font_DINGBATS: |
| 627 | return U"Zapf Dingbats"; |
| 628 | default: |
| 629 | return nullptr; |
| 630 | } |
| 631 | } |
| 632 | static CTFontRef quartz_getFontRef (int font, int size, int style) { |
| 633 | CTFontSymbolicTraits ctStyle = ( style & Graphics_BOLD ? kCTFontBoldTrait : 0 ) | ( style & Graphics_ITALIC ? kCTFontItalicTrait : 0 ); |
| 634 | #if 1 |
| 635 | CFStringRef key = kCTFontSymbolicTrait; |
| 636 | CFNumberRef value = CFNumberCreate (nullptr, kCFNumberIntType, & ctStyle); |
| 637 | CFIndex numberOfValues = 1; |
| 638 | CFDictionaryRef styleDict = CFDictionaryCreate (nullptr, (const void **) & key, (const void **) & value, numberOfValues, |
| 639 | & kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); |
| 640 | CFRelease (value); |
| 641 | CFStringRef keys [2]; |
| 642 | keys [0] = kCTFontTraitsAttribute; |
| 643 | keys [1] = kCTFontNameAttribute; |
| 644 | conststring32 fontName = quartz_getFontName (font, style); |
| 645 | CFStringRef cfFont = (CFStringRef) Melder_peek32toCfstring (fontName); |
| 646 | void *values [2] = { (void *) styleDict, (void *) cfFont }; |
| 647 | CFDictionaryRef attributes = CFDictionaryCreate (nullptr, (const void **) & keys, (const void **) & values, 2, |
| 648 | & kCFTypeDictionaryKeyCallBacks, & kCFTypeDictionaryValueCallBacks); |
| 649 | CFRelease (styleDict); |
| 650 | CTFontDescriptorRef ctFontDescriptor = CTFontDescriptorCreateWithAttributes (attributes); |
| 651 | CFRelease (attributes); |
| 652 | #else |
| 653 | NSMutableDictionary *styleDict = [[NSMutableDictionary alloc] initWithCapacity: 1]; |
| 654 | [styleDict setObject: [NSNumber numberWithUnsignedInt: ctStyle] forKey: (id) kCTFontSymbolicTrait]; |
| 655 | NSMutableDictionary *attributes = [[NSMutableDictionary alloc] initWithCapacity: 2]; |
| 656 | [attributes setObject: styleDict forKey: (id) kCTFontTraitsAttribute]; |
| 657 | switch (font) { |
| 658 | case (int) kGraphics_font::TIMES: { [attributes setObject: @"Times New Roman" forKey: (id) kCTFontNameAttribute]; } break; |
| 659 | case (int) kGraphics_font::HELVETICA: { [attributes setObject: @"Arial" forKey: (id) kCTFontNameAttribute]; } break; |
| 660 | case (int) kGraphics_font::COURIER: { [attributes setObject: @"Courier New" forKey: (id) kCTFontNameAttribute]; } break; |
| 661 | case (int) kGraphics_font::PALATINO: { if (Melder_debug == 900) |
| 662 | [attributes setObject: @"DG Meta Serif Science" forKey: (id) kCTFontNameAttribute]; |
| 663 | else |
| 664 | [attributes setObject: @"Palatino" forKey: (id) kCTFontNameAttribute]; |
| 665 | } break; |
| 666 | case kGraphics_font_SYMBOL: { [attributes setObject: @"Symbol" forKey: (id) kCTFontNameAttribute]; } break; |
| 667 | case kGraphics_font_IPATIMES: { [attributes setObject: @"Doulos SIL" forKey: (id) kCTFontNameAttribute]; } break; |
| 668 | case kGraphics_font_IPAPALATINO: { [attributes setObject: @"Charis SIL" forKey: (id) kCTFontNameAttribute]; } break; DONT USE |
| 669 | case kGraphics_font_CHEROKEE: { [attributes setObject: @"Plantagenet Cherokee" forKey: (id) kCTFontNameAttribute]; } break; |
| 670 | case kGraphics_font_DINGBATS: { [attributes setObject: @"Zapf Dingbats" forKey: (id) kCTFontNameAttribute]; } break; |
| 671 | } |
| 672 | CTFontDescriptorRef ctFontDescriptor = CTFontDescriptorCreateWithAttributes ((CFMutableDictionaryRef) attributes); |
| 673 | [styleDict release]; |
| 674 | [attributes release]; |
| 675 | #endif |
| 676 | CTFontRef ctFont = CTFontCreateWithFontDescriptor (ctFontDescriptor, size, nullptr); |
| 677 | CFRelease (ctFontDescriptor); |
| 678 | return ctFont; |
| 679 | } |
| 680 | #endif |
| 681 | |
| 682 | static void charDraw (Graphics anyGraphics, int xDC, int yDC, _Graphics_widechar *lc, |
| 683 | const char32 codes [], int nchars, int width) |
| 684 | { |
| 685 | trace (U"nchars ", nchars, U" first ", (int) lc->kar, U" ", (char32) lc -> kar, U" rightToLeft ", lc->rightToLeft); |
| 686 | if (anyGraphics -> postScript) { |
| 687 | GraphicsPostscript me = static_cast <GraphicsPostscript> (anyGraphics); |
| 688 | const bool onlyRegular = lc -> font.string [0] == 'S' || |
| 689 | (lc -> font.string [0] == 'T' && lc -> font.string [1] == 'e'); // Symbol & SILDoulos ! |
| 690 | const bool slant = (lc -> style & Graphics_ITALIC) && onlyRegular; |
| 691 | const bool thick = (lc -> style & Graphics_BOLD) && onlyRegular; |
| 692 | if (lc -> font.string != my lastFid || lc -> size != my lastSize) |
| 693 | my d_printf (my d_file, my languageLevel == 1 ? "/%s %d FONT\n" : "/%s %d selectfont\n", |
| 694 | my lastFid = lc -> font.string, my lastSize = lc -> size); |
| 695 | if (lc -> link) |
| 696 | my d_printf (my d_file, "0 0 1 setrgbcolor\n"); |
| 697 | for (int i = -3; i <= 3; i ++) { |
| 698 | if (i != 0 && ! thick) |
| 699 | continue; |
| 700 | my d_printf (my d_file, "%d %d M ", xDC + i, yDC); |
| 701 | if (my textRotation != 0.0 || slant) { |
| 702 | my d_printf (my d_file, "gsave currentpoint translate "); |
| 703 | if (my textRotation != 0.0) |
| 704 | my d_printf (my d_file, "%.6g rotate 0 0 M\n", (double) my textRotation); |
| 705 | if (slant) |
| 706 | my d_printf (my d_file, "[1 0 0.25 1 0 0] concat 0 0 M\n"); |
| 707 | } |
| 708 | my d_printf (my d_file, "("); |
| 709 | const char32 *p = & codes [0]; |
| 710 | while (*p) { |
| 711 | if (*p == U'(' || *p == U')' || *p == U'\\') { |
| 712 | my d_printf (my d_file, "\\%c", (unsigned char) *p); |
| 713 | } else if (*p >= 32 && *p <= 126) { |
| 714 | my d_printf (my d_file, "%c", (unsigned char) *p); |
| 715 | } else { |
| 716 | my d_printf (my d_file, "\\%d%d%d", (unsigned char) *p / 64, |
| 717 | ((unsigned char) *p % 64) / 8, (unsigned char) *p % 8); |
| 718 | } |
| 719 | p ++; |
| 720 | } |
| 721 | my d_printf (my d_file, ") show\n"); |
| 722 | if (my textRotation != 0.0 || slant) |
| 723 | my d_printf (my d_file, "grestore\n"); |
| 724 | } |
| 725 | if (lc -> link) |
| 726 | my d_printf (my d_file, "0 0 0 setrgbcolor\n"); |
| 727 | } else if (anyGraphics -> screen) { |
| 728 | GraphicsScreen me = static_cast <GraphicsScreen> (anyGraphics); |
| 729 | #if cairo |
| 730 | if (! my d_cairoGraphicsContext) |
| 731 | return; |
| 732 | // TODO! |
| 733 | if (lc -> link) |
| 734 | _Graphics_setColour (me, Melder_BLUE); |
| 735 | int font = lc -> font.integer_; |
| 736 | cairo_save (my d_cairoGraphicsContext); |
| 737 | cairo_translate (my d_cairoGraphicsContext, xDC, yDC); |
| 738 | //cairo_scale (my d_cairoGraphicsContext, 1, -1); |
| 739 | cairo_rotate (my d_cairoGraphicsContext, - my textRotation * NUMpi / 180.0); |
| 740 | const char *codes8 = Melder_peek32to8 (codes); |
| 741 | PangoFontDescription *font_description = PangoFontDescription_create (font, lc); |
| 742 | PangoLayout *layout = pango_cairo_create_layout (my d_cairoGraphicsContext); |
| 743 | pango_layout_set_font_description (layout, font_description); |
| 744 | pango_layout_set_text (layout, codes8, -1); |
| 745 | cairo_move_to (my d_cairoGraphicsContext, 0 /*xDC*/, 0 /*yDC*/); |
| 746 | // instead of pango_cairo_show_layout we use pango_cairo_show_layout_line to |
| 747 | // get the same text origin as cairo_show_text, i.e. baseline left, instead of Pango's top left! |
| 748 | pango_cairo_show_layout_line (my d_cairoGraphicsContext, pango_layout_get_line_readonly (layout, 0)); |
| 749 | g_object_unref (layout); |
| 750 | cairo_restore (my d_cairoGraphicsContext); |
| 751 | if (lc -> link) |
| 752 | _Graphics_setColour (me, my colour); |
| 753 | return; |
| 754 | #elif gdi |
| 755 | const int font = lc -> font.integer_; |
| 756 | conststringW codesW = Melder_peek32toW (codes); |
| 757 | if (my duringXor) { |
| 758 | /* |
| 759 | On GDI, SetROP2 does not influence text drawing, |
| 760 | so we have to create a bitmap in the background |
| 761 | and use BitBlt with SRCINVERT as its ROP. |
| 762 | */ |
| 763 | const int descent = (1.0/216) * my fontSize * my resolution; |
| 764 | const int ascent = (1.0/72) * my fontSize * my resolution; |
| 765 | const int maxWidth = 800, maxHeight = 200; |
| 766 | const int baseline = 100, top = baseline - ascent - 1, bottom = baseline + descent + 1; |
| 767 | static bool inited = false; |
| 768 | static HDC dc; |
| 769 | static HBITMAP bitmap; |
| 770 | if (! inited) { |
| 771 | dc = CreateCompatibleDC (my d_gdiGraphicsContext); |
| 772 | bitmap = CreateCompatibleBitmap (my d_gdiGraphicsContext, maxWidth, maxHeight); |
| 773 | SelectBitmap (dc, bitmap); |
| 774 | SetBkMode (dc, TRANSPARENT); // not the default! |
| 775 | SelectPen (dc, GetStockPen (BLACK_PEN)); |
| 776 | SelectBrush (dc, GetStockBrush (BLACK_BRUSH)); |
| 777 | SetTextAlign (dc, TA_LEFT | TA_BASELINE | TA_NOUPDATECP); // baseline is not the default! |
| 778 | inited = true; |
| 779 | } |
| 780 | width += 4; // for slant |
| 781 | Rectangle (dc, 0, top, width, bottom); |
| 782 | SelectFont (dc, fonts [(int) my resolutionNumber] [font] [lc -> size] [lc -> style]); |
| 783 | SetTextColor (dc, my d_winForegroundColour); |
| 784 | TextOutW (dc, 0, baseline, codesW, Melder16_length ((conststring16) codesW)); |
| 785 | BitBlt (my d_gdiGraphicsContext, xDC, yDC - ascent, width, bottom - top, dc, 0, top, SRCINVERT); |
| 786 | return; |
| 787 | } |
| 788 | SelectPen (my d_gdiGraphicsContext, my d_winPen); |
| 789 | SelectBrush (my d_gdiGraphicsContext, my d_winBrush); |
| 790 | if (lc -> link) |
| 791 | SetTextColor (my d_gdiGraphicsContext, RGB (0, 0, 255)); |
| 792 | else |
| 793 | SetTextColor (my d_gdiGraphicsContext, my d_winForegroundColour); |
| 794 | SelectFont (my d_gdiGraphicsContext, fonts [(int) my resolutionNumber] [font] [lc -> size] [lc -> style]); |
| 795 | if (my textRotation == 0.0) { |
| 796 | TextOutW (my d_gdiGraphicsContext, xDC, yDC, codesW, Melder16_length ((const char16 *) codesW)); |
| 797 | } else { |
| 798 | int restore = SaveDC (my d_gdiGraphicsContext); |
| 799 | SetGraphicsMode (my d_gdiGraphicsContext, GM_ADVANCED); |
| 800 | double a = my textRotation * NUMpi / 180.0, cosa = cos (a), sina = sin (a); |
| 801 | XFORM rotate = { (float) cosa, (float) - sina, (float) sina, (float) cosa, 0.0, 0.0 }; |
| 802 | ModifyWorldTransform (my d_gdiGraphicsContext, & rotate, MWT_RIGHTMULTIPLY); |
| 803 | XFORM translate = { 1.0, 0.0, 0.0, 1.0, (float) xDC, (float) yDC }; |
| 804 | ModifyWorldTransform (my d_gdiGraphicsContext, & translate, MWT_RIGHTMULTIPLY); |
| 805 | TextOutW (my d_gdiGraphicsContext, 0, 0, codesW, Melder16_length ((const char16 *) codesW)); |
| 806 | RestoreDC (my d_gdiGraphicsContext, restore); |
| 807 | } |
| 808 | if (lc -> link) |
| 809 | SetTextColor (my d_gdiGraphicsContext, my d_winForegroundColour); |
| 810 | SelectPen (my d_gdiGraphicsContext, GetStockPen (BLACK_PEN)), SelectBrush (my d_gdiGraphicsContext, GetStockBrush (NULL_BRUSH)); |
| 811 | return; |
| 812 | #elif quartz |
| 813 | /* |
| 814 | Determine the font family. |
| 815 | */ |
| 816 | const int font = lc -> font.integer_; // the font of the first character |
| 817 | |
| 818 | /* |
| 819 | Determine the style. |
| 820 | */ |
| 821 | const int style = lc -> style; // the style of the first character |
| 822 | |
| 823 | /* |
| 824 | Determine the font-style combination. |
| 825 | */ |
| 826 | CTFontRef ctFont = theScreenFonts [font] [lc -> size] [style]; |
| 827 | if (! ctFont) |
| 828 | theScreenFonts [font] [lc -> size] [style] = ctFont = |
| 829 | quartz_getFontRef (font, lc -> size, style); |
| 830 | |
| 831 | const char16 *codes16 = Melder_peek32to16 (codes); |
| 832 | #if 1 |
| 833 | CFStringRef s = CFStringCreateWithBytes (nullptr, |
| 834 | (const UInt8 *) codes16, Melder16_length (codes16) * 2, |
| 835 | kCFStringEncodingUTF16LE, false |
| 836 | ); |
| 837 | integer length = CFStringGetLength (s); |
| 838 | #else |
| 839 | NSString *s = [[NSString alloc] initWithBytes: codes16 length: Melder16_length (codes16) * 2 encoding: NSUTF16LittleEndianStringEncoding]; |
| 840 | integer length = [s length]; |
| 841 | #endif |
| 842 | |
| 843 | CGFloat descent = CTFontGetDescent (ctFont); |
| 844 | |
| 845 | CFMutableAttributedStringRef string = CFAttributedStringCreateMutable (kCFAllocatorDefault, length); |
| 846 | CFAttributedStringReplaceString (string, CFRangeMake (0, 0), (CFStringRef) s); |
| 847 | CFRange textRange = CFRangeMake (0, length); |
| 848 | CFAttributedStringSetAttribute (string, textRange, kCTFontAttributeName, ctFont); |
| 849 | |
| 850 | /* |
| 851 | We don't set kerning explicitly, so that Praat will use standard kerning. |
| 852 | */ |
| 853 | |
| 854 | static CTParagraphStyleRef paragraphStyle; |
| 855 | if (! paragraphStyle) { |
| 856 | CTTextAlignment textAlignment = kCTLeftTextAlignment; |
| 857 | CTParagraphStyleSetting paragraphSettings [1] = { { kCTParagraphStyleSpecifierAlignment, sizeof (CTTextAlignment), & textAlignment } }; |
| 858 | paragraphStyle = CTParagraphStyleCreate (paragraphSettings, 1); |
| 859 | Melder_assert (paragraphStyle != nullptr); |
| 860 | } |
| 861 | CFAttributedStringSetAttribute (string, textRange, kCTParagraphStyleAttributeName, paragraphStyle); |
| 862 | |
| 863 | MelderColour colour = lc -> link ? Melder_BLUE : my colour; |
| 864 | CGColorRef color = CGColorCreateGenericRGB (colour.red, colour.green, colour.blue, 1.0); |
| 865 | Melder_assert (color != nullptr); |
| 866 | CFAttributedStringSetAttribute (string, textRange, kCTForegroundColorAttributeName, color); |
| 867 | |
| 868 | /* |
| 869 | Draw. |
| 870 | */ |
| 871 | |
| 872 | CGContextSetTextMatrix (my d_macGraphicsContext, CGAffineTransformIdentity); // this could set the "current context" for CoreText |
| 873 | CFRelease (color); |
| 874 | |
| 875 | CGContextSaveGState (my d_macGraphicsContext); |
| 876 | CGContextTranslateCTM (my d_macGraphicsContext, xDC, yDC); |
| 877 | if (my yIsZeroAtTheTop) |
| 878 | CGContextScaleCTM (my d_macGraphicsContext, 1.0, -1.0); |
| 879 | CGContextRotateCTM (my d_macGraphicsContext, my textRotation * NUMpi / 180.0); |
| 880 | |
| 881 | CTLineRef line = CTLineCreateWithAttributedString (string); |
| 882 | CTLineDraw (line, my d_macGraphicsContext); |
| 883 | CFRelease (line); |
| 884 | CGContextRestoreGState (my d_macGraphicsContext); |
| 885 | |
| 886 | /* |
| 887 | Clean up. |
| 888 | */ |
| 889 | CFRelease (string); |
| 890 | CFRelease (s); |
| 891 | //CFRelease (ctFont); |
| 892 | return; |
| 893 | #endif |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | #define MAX_LINK_LENGTH 300 |
| 898 | |
| 899 | static integer bufferSize; |
| 900 | static _Graphics_widechar *theWidechar; |
| 901 | static char32 *charCodes; |
| 902 | static int initBuffer (conststring32 txt) { |
| 903 | try { |
| 904 | constexpr integer maximumNumberOfReplacementCharactersPerCharacter = 2; |
| 905 | integer sizeNeeded = maximumNumberOfReplacementCharactersPerCharacter * Melder_length (txt) + 1; |
| 906 | if (sizeNeeded > bufferSize) { |
| 907 | sizeNeeded += sizeNeeded / 2 + 100; |
| 908 | Melder_free (theWidechar); |
| 909 | Melder_free (charCodes); |
| 910 | theWidechar = Melder_calloc (_Graphics_widechar, sizeNeeded); |
| 911 | charCodes = Melder_calloc (char32, sizeNeeded); |
| 912 | bufferSize = sizeNeeded; |
| 913 | } |
| 914 | return 1; |
| 915 | } catch (MelderError) { |
| 916 | bufferSize = 0; |
| 917 | Melder_flushError (); |
| 918 | return 0; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | static int numberOfLinks = 0; |
| 923 | #define MAXIMUM_NUMBER_OF_LINKS_PER_STRING 1000 |
| 924 | static Graphics_Link links [MAXIMUM_NUMBER_OF_LINKS_PER_STRING]; |
| 925 | |
| 926 | static void charSizes (Graphics me, _Graphics_widechar string [], bool measureEachCharacterSeparately) { |
| 927 | /* |
| 928 | Ideally, this function should work even in cases where there is no screen. |
| 929 | Example: a Praat script wants to draw a text inside a rectangle |
| 930 | and determines the width of the rectangle by means of the "Text width (mm)" command. |
| 931 | Ideally, this script should run correctly from the command line. |
| 932 | On the Mac, `CTFramesetterSuggestFrameSizeWithConstraints` works correctly from the command line, |
| 933 | but on Linux, `pango_layout_get_extents` does not work if there is no d_cairoGraphicsContext. |
| 934 | (last checked 2020-07-17) |
| 935 | */ |
| 936 | if (my postScript || (cairo && ! my screen)) { // TODO: use Pango measurements even without Cairo context (if no screen) |
| 937 | for (_Graphics_widechar *character = string; character -> kar > U'\t'; character ++) |
| 938 | charSize (me, character); |
| 939 | } else { |
| 940 | /* |
| 941 | Measure the size of each character. |
| 942 | */ |
| 943 | _Graphics_widechar *character; |
| 944 | #if quartz || cairo |
| 945 | #if cairo |
| 946 | if (! ((GraphicsScreen) me) -> d_cairoGraphicsContext) |
| 947 | return; |
| 948 | #endif |
| 949 | int numberOfDiacritics = 0; |
| 950 | for (_Graphics_widechar *lc = string; lc -> kar > U'\t'; lc ++) { |
| 951 | /* |
| 952 | Determine the font family. |
| 953 | */ |
| 954 | Longchar_Info info = lc -> karInfo; |
| 955 | Melder_assert (info); |
| 956 | const int font = chooseFont (me, lc); |
| 957 | lc -> font.string = nullptr; // this erases font.integer_! |
| 958 | |
| 959 | /* |
| 960 | Determine the style. |
| 961 | */ |
| 962 | int style = lc -> style; |
| 963 | Melder_assert (style >= 0 && style <= Graphics_BOLD_ITALIC); |
| 964 | |
| 965 | const int normalSize = my fontSize * my resolution / 72.0; |
| 966 | const int smallSize = (3 * normalSize + 2) / 4; |
| 967 | const int size = ( lc -> size < 100 ? smallSize : normalSize ); |
| 968 | lc -> size = size; |
| 969 | lc -> baseline *= 0.01 * normalSize; |
| 970 | lc -> code = lc -> kar; |
| 971 | lc -> font.integer_ = font; |
| 972 | if (Longchar_Info_isDiacritic (info)) |
| 973 | numberOfDiacritics ++; |
| 974 | |
| 975 | #if quartz |
| 976 | /* |
| 977 | Determine and store the font-style combination. |
| 978 | */ |
| 979 | CTFontRef ctFont = theScreenFonts [font] [size] [style]; |
| 980 | if (! ctFont) |
| 981 | theScreenFonts [font] [size] [style] = ctFont = |
| 982 | quartz_getFontRef (font, size, style); |
| 983 | #endif |
| 984 | } |
| 985 | int nchars = 0; |
| 986 | for (_Graphics_widechar *lc = string; lc -> kar > U'\t'; lc ++) { |
| 987 | charCodes [nchars ++] = lc -> code; |
| 988 | _Graphics_widechar *next = lc + 1; |
| 989 | lc -> width = 0; |
| 990 | if (measureEachCharacterSeparately || |
| 991 | next->kar <= U' ' || next->style != lc->style || |
| 992 | next->baseline != lc->baseline || next->size != lc->size || next->link != lc->link || |
| 993 | next->font.integer_ != lc->font.integer_ || next->font.string != lc->font.string || |
| 994 | next->rightToLeft != lc->rightToLeft || |
| 995 | (my textRotation != 0.0 && my screen && my resolution > 150)) |
| 996 | { |
| 997 | charCodes [nchars] = U'\0'; |
| 998 | #if cairo |
| 999 | const char *codes8 = Melder_peek32to8 (charCodes); |
| 1000 | int length = strlen (codes8); // TODO: integer overflow |
| 1001 | PangoFontDescription *fontDescription = PangoFontDescription_create (lc -> font.integer_, lc); |
| 1002 | |
| 1003 | /* |
| 1004 | Measuring the width of a text with a homogeneous Praat font |
| 1005 | should still allow for Pango's font substitution. |
| 1006 | Low-level sequences such as `pango_itemize--pango_shape--pango_glyph_string_get_width` |
| 1007 | or `pango_itemize--pango_shape--pango_font_map_load_font--pango_glyph_string_extents` |
| 1008 | don't accomplish this: they seem to compute the width solely on the |
| 1009 | basis of the (perhaps substituted) font of the *first* glyph. By contrast, a PangoLayout |
| 1010 | performs font substitution when drawing with `pango_cairo_show_layout_line`, |
| 1011 | and also when measuring the width with `pango_layout_get_extents`. |
| 1012 | Fortunately, a PangoLayout is 1.5 to 2 times faster than the two low-level methods |
| 1013 | (measured 20170527). |
| 1014 | */ |
| 1015 | Melder_assert (my screen); |
| 1016 | PangoLayout *layout = pango_cairo_create_layout (((GraphicsScreen) me) -> d_cairoGraphicsContext); |
| 1017 | pango_layout_set_font_description (layout, fontDescription); |
| 1018 | pango_layout_set_text (layout, codes8, -1); |
| 1019 | PangoRectangle inkRect, logicalRect; |
| 1020 | pango_layout_get_extents (layout, & inkRect, & logicalRect); |
| 1021 | lc -> width = logicalRect. width / PANGO_SCALE; |
| 1022 | Melder_assert (logicalRect.x == 0); |
| 1023 | g_object_unref (layout); |
| 1024 | #elif quartz |
| 1025 | const conststring16 codes16 = Melder_peek32to16 (charCodes); |
| 1026 | const integer length = Melder16_length (codes16); |
| 1027 | |
| 1028 | NSString *s = [[NSString alloc] |
| 1029 | initWithBytes: codes16 |
| 1030 | length: (NSUInteger) (length * 2) |
| 1031 | encoding: NSUTF16LittleEndianStringEncoding // BUG: should be NSUTF16NativeStringEncoding, except that that doesn't exist |
| 1032 | ]; |
| 1033 | |
| 1034 | CFRange textRange = CFRangeMake (0, (CFIndex) [s length]); |
| 1035 | |
| 1036 | CFMutableAttributedStringRef cfstring = |
| 1037 | CFAttributedStringCreateMutable (kCFAllocatorDefault, (CFIndex) [s length]); |
| 1038 | CFAttributedStringReplaceString (cfstring, CFRangeMake (0, 0), (CFStringRef) s); |
| 1039 | CFAttributedStringSetAttribute (cfstring, textRange, kCTFontAttributeName, theScreenFonts [lc -> font.integer_] [lc -> size] [lc -> style]); |
| 1040 | |
| 1041 | /* |
| 1042 | Measure. |
| 1043 | */ |
| 1044 | |
| 1045 | // Create a path to render text in |
| 1046 | CGMutablePathRef path = CGPathCreateMutable (); |
| 1047 | NSRect measureRect = NSMakeRect (0, 0, CGFLOAT_MAX, CGFLOAT_MAX); |
| 1048 | CGPathAddRect (path, nullptr, (CGRect) measureRect); |
| 1049 | |
| 1050 | CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString ((CFAttributedStringRef) cfstring); |
| 1051 | CFRange fitRange; |
| 1052 | CGSize targetSize = CGSizeMake (lc -> width, CGFLOAT_MAX); |
| 1053 | CGSize frameSize = CTFramesetterSuggestFrameSizeWithConstraints (framesetter, textRange, nullptr, targetSize, & fitRange); |
| 1054 | CFRelease (framesetter); |
| 1055 | CFRelease (cfstring); |
| 1056 | [s release]; |
| 1057 | CFRelease (path); |
| 1058 | //Longchar_Info info = lc -> karInfo; |
| 1059 | //bool isDiacritic = ( info -> ps.times == 0 ); |
| 1060 | //lc -> width = ( isDiacritic ? 0.0 : frameSize.width * lc -> size / 100.0 ); |
| 1061 | lc -> width = frameSize.width /* * lc -> size / 100.0 */; |
| 1062 | if (Melder_systemVersion >= 101100) { |
| 1063 | /* |
| 1064 | If the text ends in a space, CTFramesetterSuggestFrameSizeWithConstraints() ignores the space. |
| 1065 | we correct for this. |
| 1066 | */ |
| 1067 | if (codes16 [length - 1] == u' ') |
| 1068 | lc -> width += ( lc->font.integer_ == (int) kGraphics_font::COURIER || lc->style == Graphics_CODE ? 60.0 : |
| 1069 | next->font.integer_ == (int) kGraphics_font::COURIER || next->style == Graphics_CODE ? 37.5 : 25.0 ) * lc -> size / 100.0; |
| 1070 | } |
| 1071 | #endif |
| 1072 | nchars = 0; |
| 1073 | } |
| 1074 | } |
| 1075 | #else |
| 1076 | for (character = string; character -> kar > U'\t'; character ++) |
| 1077 | charSize (me, character); |
| 1078 | #endif |
| 1079 | } |
| 1080 | /* |
| 1081 | Each character has been garnished with information about the character's width. |
| 1082 | Make a correction for systems that make slanted characters overlap the character box to their right. |
| 1083 | We must do this after the previous loop, because we query the size of the *next* character. |
| 1084 | |
| 1085 | Keep this in SYNC with psTextWidth. |
| 1086 | */ |
| 1087 | for (_Graphics_widechar *character = string; character -> kar > U'\t'; character ++) { |
| 1088 | if ((character -> style & Graphics_ITALIC) != 0) { |
| 1089 | _Graphics_widechar *nextCharacter = character + 1; |
| 1090 | if (nextCharacter -> kar <= U'\t') { |
| 1091 | character -> width += SCREEN_SLANT_CORRECTION / 72 * my fontSize * my resolution; |
| 1092 | } else if (((nextCharacter -> style & Graphics_ITALIC) == 0 && nextCharacter -> baseline >= character -> baseline) |
| 1093 | || (character -> baseline == 0 && nextCharacter -> baseline > 0)) |
| 1094 | { |
| 1095 | if (nextCharacter -> kar == U'.' || nextCharacter -> kar == U',') |
| 1096 | character -> width += SCREEN_SLANT_CORRECTION / 144 * my fontSize * my resolution; |
| 1097 | else |
| 1098 | character -> width += SCREEN_SLANT_CORRECTION / 72 * my fontSize * my resolution; |
| 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* |
| 1105 | The function textWidth() determines the fractional width of a text, in device coordinates. |
| 1106 | */ |
| 1107 | static double textWidth (_Graphics_widechar string []) { |
| 1108 | double width = 0.0; |
| 1109 | for (_Graphics_widechar *character = string; character -> kar > U'\t'; character ++) |
| 1110 | width += character -> width; |
| 1111 | return width; |
| 1112 | } |
| 1113 | |
| 1114 | static void drawOneCell (Graphics me, int xDC, int yDC, _Graphics_widechar lc []) { |
| 1115 | int nchars = 0; |
| 1116 | const double width = textWidth (lc); |
| 1117 | /* |
| 1118 | We must continue even if width is zero (for adjusting textY). |
| 1119 | */ |
| 1120 | _Graphics_widechar *plc, *lastlc; |
| 1121 | bool inLink = false; |
| 1122 | double dx, dy; |
| 1123 | switch (my horizontalTextAlignment) { |
| 1124 | case (int) Graphics_LEFT: dx = 1.0 + (0.1/72.0) * my fontSize * my resolution; break; |
| 1125 | case (int) Graphics_CENTRE: dx = - width / 2.0; break; |
| 1126 | case (int) Graphics_RIGHT: dx = ( width != 0.0 ? - width - (0.1/72.0) * my fontSize * my resolution : 0 ); break; // if width is zero, do not step left |
| 1127 | default: dx = 1 + (0.1/72.0) * my fontSize * my resolution; break; |
| 1128 | } |
| 1129 | switch (my verticalTextAlignment) { |
| 1130 | case Graphics_BOTTOM: dy = (0.4/72.0) * my fontSize * my resolution; break; |
| 1131 | case Graphics_HALF: dy = (-0.3/72.0) * my fontSize * my resolution; break; |
| 1132 | case Graphics_TOP: dy = (-1.0/72.0) * my fontSize * my resolution; break; |
| 1133 | case Graphics_BASELINE: dy = 0.0; break; |
| 1134 | default: dy = 0.0; break; |
| 1135 | } |
| 1136 | if (my textRotation != 0.0) { |
| 1137 | double xbegin = dx, x = xbegin, cosa, sina; |
| 1138 | if (my textRotation == 90.0f) { |
| 1139 | cosa = 0.0; |
| 1140 | sina = 1.0; |
| 1141 | } else if (my textRotation == 270.0f) { |
| 1142 | cosa = 0.0; |
| 1143 | sina = -1.0; |
| 1144 | } else { |
| 1145 | const double angle = my textRotation * NUMpi / 180.0; |
| 1146 | cosa = cos (angle); |
| 1147 | sina = sin (angle); |
| 1148 | } |
| 1149 | for (plc = lc; plc -> kar > U'\t'; plc ++) { |
| 1150 | _Graphics_widechar *next = plc + 1; |
| 1151 | charCodes [nchars ++] = plc -> code; // buffer... |
| 1152 | x += plc -> width; |
| 1153 | /* |
| 1154 | We can draw stretches of characters: |
| 1155 | they have different styles, baselines, sizes, or fonts, |
| 1156 | or if there is a break between them, |
| 1157 | or if we cannot rotate multiple characters, |
| 1158 | which is the case on bitmap printers. |
| 1159 | */ |
| 1160 | if (next->kar < U' ' || next->style != plc->style || |
| 1161 | next->baseline != plc->baseline || next->size != plc->size || |
| 1162 | next->font.integer_ != plc->font.integer_ || next->font.string != plc->font.string || |
| 1163 | next->rightToLeft != plc->rightToLeft || |
| 1164 | (my screen && my resolution > 150)) |
| 1165 | { |
| 1166 | const double dy2 = dy + plc -> baseline; |
| 1167 | const double xr = cosa * xbegin - sina * dy2; |
| 1168 | const double yr = sina * xbegin + cosa * dy2; |
| 1169 | charCodes [nchars] = U'\0'; // ...and flush |
| 1170 | charDraw (me, xDC + xr, my yIsZeroAtTheTop ? yDC - yr : yDC + yr, |
| 1171 | plc, charCodes, nchars, x - xbegin); |
| 1172 | nchars = 0; |
| 1173 | xbegin = x; |
| 1174 | } |
| 1175 | } |
| 1176 | } else { |
| 1177 | double xbegin = xDC + dx, x = xbegin, y = ( my yIsZeroAtTheTop ? yDC - dy : yDC + dy ); // all mutable |
| 1178 | lastlc = lc; |
| 1179 | if (my wrapWidth != 0.0) { |
| 1180 | /* |
| 1181 | Replace some spaces with new-line symbols. |
| 1182 | */ |
| 1183 | const int xmax = xDC + my wrapWidth * my scaleX; |
| 1184 | for (plc = lc; plc -> kar >= U' '; plc ++) { |
| 1185 | x += plc -> width; |
| 1186 | if (x > xmax) { // wrap (if wrapWidth is too small, each word will be on a separate line) |
| 1187 | while (plc >= lastlc) { |
| 1188 | if (plc -> kar == U' ' && ! plc -> link) // keep links contiguous |
| 1189 | break; |
| 1190 | plc --; |
| 1191 | } |
| 1192 | if (plc <= lastlc) |
| 1193 | break; // hopeless situation: no spaces; get over it |
| 1194 | lastlc = plc; |
| 1195 | plc -> kar = U'\n'; // replace space with newline |
| 1196 | #if quartz || cairo |
| 1197 | if (my screen) { |
| 1198 | /* |
| 1199 | This part is needed when using the non-`charSize()` variant of `charSizes()`, |
| 1200 | because otherwise you'll see extra spaces |
| 1201 | before the first font switch on each non-initial line. |
| 1202 | */ |
| 1203 | _Graphics_widechar *next = plc + 1; |
| 1204 | if (next->style != plc->style || |
| 1205 | next->baseline != plc->baseline || next->size != plc->size || next->link != plc->link || |
| 1206 | next->font.integer_ != plc->font.integer_ || next->font.string != plc->font.string || |
| 1207 | next->rightToLeft != plc->rightToLeft) |
| 1208 | { |
| 1209 | // nothing |
| 1210 | } else { |
| 1211 | next -> width -= 0.25 * my fontSize * my resolution / 72.0; // subtract the width of one space |
| 1212 | } |
| 1213 | } |
| 1214 | #endif |
| 1215 | x = xDC + dx + my secondIndent * my scaleX; |
| 1216 | } |
| 1217 | } |
| 1218 | xbegin = x = xDC + dx; // re-initialize for second pass |
| 1219 | } |
| 1220 | for (plc = lc; plc -> kar > U'\t'; plc ++) { |
| 1221 | _Graphics_widechar *next = plc + 1; |
| 1222 | if (plc -> link) { |
| 1223 | if (! inLink) { |
| 1224 | const double descent = ( my yIsZeroAtTheTop ? -(0.3/72) : (0.3/72) ) * my fontSize * my resolution; |
| 1225 | ++ numberOfLinks; |
| 1226 | Melder_assert (numberOfLinks < MAXIMUM_NUMBER_OF_LINKS_PER_STRING); |
| 1227 | links [numberOfLinks]. x1 = x; |
| 1228 | links [numberOfLinks]. y1 = y - descent; |
| 1229 | links [numberOfLinks]. y2 = y + 3 * descent; |
| 1230 | inLink = true; |
| 1231 | } |
| 1232 | } else if (inLink) { |
| 1233 | links [numberOfLinks]. x2 = x; |
| 1234 | inLink = false; |
| 1235 | } |
| 1236 | if (plc -> kar == U'\n') { |
| 1237 | xbegin = x = xDC + dx + my secondIndent * my scaleX; |
| 1238 | y = ( my yIsZeroAtTheTop ? y + (1.2/72) * my fontSize * my resolution : y - (1.2/72) * my fontSize * my resolution ); |
| 1239 | } else { |
| 1240 | charCodes [nchars ++] = plc -> code; // buffer... |
| 1241 | x += plc -> width; |
| 1242 | if (next->kar < U' ' || next->style != plc->style || |
| 1243 | next->baseline != plc->baseline || next->size != plc->size || next->link != plc->link || |
| 1244 | next->font.integer_ != plc->font.integer_ || next->font.string != plc->font.string || |
| 1245 | next->rightToLeft != plc->rightToLeft) |
| 1246 | { |
| 1247 | charCodes [nchars] = U'\0'; // ...and flush |
| 1248 | charDraw (me, xbegin, my yIsZeroAtTheTop ? y - plc -> baseline : y + plc -> baseline, |
| 1249 | plc, charCodes, nchars, x - xbegin); |
| 1250 | nchars = 0; |
| 1251 | xbegin = x; |
| 1252 | } |
| 1253 | } |
| 1254 | } |
| 1255 | if (inLink) { |
| 1256 | links [numberOfLinks]. x2 = x; |
| 1257 | inLink = false; |
| 1258 | } |
| 1259 | my textX = (x - my deltaX) / my scaleX; |
| 1260 | my textY = (( my yIsZeroAtTheTop ? y + dy : y - dy ) - my deltaY) / my scaleY; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /* |
| 1265 | The function `drawCells` handles table and layout. |
| 1266 | */ |
| 1267 | static void drawCells (Graphics me, double xWC, double yWC, _Graphics_widechar lc []) { |
| 1268 | _Graphics_widechar *plc; |
| 1269 | int tabWidth = 0; // only for first tab |
| 1270 | kGraphics_horizontalAlignment tabAlignment = Graphics_CENTRE; |
| 1271 | int saveTextAlignment = my horizontalTextAlignment; |
| 1272 | double saveWrapWidth = my wrapWidth; |
| 1273 | numberOfLinks = 0; |
| 1274 | for (plc = lc; /* No stop condition. */ ; plc ++) { |
| 1275 | charSizes (me, plc, false); |
| 1276 | drawOneCell (me, xWC * my scaleX + my deltaX, yWC * my scaleY + my deltaY, plc); |
| 1277 | while (plc -> kar != U'\0' && plc -> kar != U'\t') plc ++; // find end of cell |
| 1278 | if (plc -> kar == U'\0') // end of text? |
| 1279 | break; |
| 1280 | if (plc -> kar == U'\t') { // go to next cell |
| 1281 | xWC += ( tabAlignment == Graphics_LEFT ? tabWidth : |
| 1282 | tabAlignment == Graphics_CENTRE ? 0.5 * tabWidth : 0.0 ) * my fontSize / 12.0; |
| 1283 | tabWidth = 1; |
| 1284 | tabAlignment = Graphics_CENTRE; |
| 1285 | xWC += ( tabAlignment == Graphics_LEFT ? 0 : |
| 1286 | tabAlignment == Graphics_CENTRE ? 0.5 * tabWidth : tabWidth ) * my fontSize / 12.0; |
| 1287 | my horizontalTextAlignment = (int) tabAlignment; |
| 1288 | my wrapWidth = tabWidth * my fontSize / 12.0; |
| 1289 | } |
| 1290 | } |
| 1291 | my horizontalTextAlignment = saveTextAlignment; |
| 1292 | my wrapWidth = saveWrapWidth; |
| 1293 | } |
| 1294 | |
| 1295 | static void parseTextIntoCellsLinesRuns (Graphics me, conststring32 txt /* cattable */, _Graphics_widechar a_widechar []) { |
| 1296 | char32 kar; |
| 1297 | const char32 *in = & txt [0]; |
| 1298 | int nquote = 0; |
| 1299 | _Graphics_widechar *out = & a_widechar [0]; |
| 1300 | bool charSuperscript = false, charSubscript = false, charItalic = false, charBold = false; |
| 1301 | bool wordItalic = false, wordBold = false, wordCode = false, wordLink = false; |
| 1302 | bool globalSuperscript = false, globalSubscript = false, globalItalic = false, globalBold = false; |
| 1303 | bool globalCode = false, globalLink = false, verbatimLink = false; |
| 1304 | bool globalSmall = 0; |
| 1305 | numberOfLinks = 0; |
| 1306 | const bool weAreInManual = ( my dollarSignIsCode ); // TODO: this is temporary |
| 1307 | const bool weAreInNotebook = ( my backquoteIsVerbatim ); // TODO: this is temporary |
| 1308 | const bool topDownVerbatim = ( my font == kGraphics_font::COURIER && weAreInNotebook ); |
| 1309 | bool globalVerbatim = topDownVerbatim; |
| 1310 | bool thinLink = false; |
| 1311 | while ((kar = *in++) != U'\0') { |
| 1312 | //TRACE |
| 1313 | if (kar == U'^' && my circumflexIsSuperscript) { |
| 1314 | if (globalVerbatim || verbatimLink) { |
| 1315 | /* |
| 1316 | Output the caret verbatim, by falling through. |
| 1317 | */ |
| 1318 | } else if (globalSuperscript) { |
| 1319 | globalSuperscript = false; |
| 1320 | wordItalic = wordBold = wordCode = false; |
| 1321 | continue; |
| 1322 | } else if (in [0] == U'^') { |
| 1323 | static integer countGlobalSuperscript; |
| 1324 | trace (U"Global superscript: ", ++ countGlobalSuperscript, U" ", txt); |
| 1325 | globalSuperscript = true; |
| 1326 | in ++; |
| 1327 | wordItalic = wordBold = wordCode = false; |
| 1328 | continue; |
| 1329 | } else { |
| 1330 | static integer countCharacterSuperscript; |
| 1331 | trace (U"Character superscript: ", ++ countCharacterSuperscript, U" ", txt); |
| 1332 | charSuperscript = true; |
| 1333 | wordItalic = wordBold = wordCode = false; |
| 1334 | continue; |
| 1335 | } |
| 1336 | } else if (kar == U'_' && my underscoreIsSubscript) { |
| 1337 | if (globalVerbatim || verbatimLink) { |
| 1338 | /* |
| 1339 | Output the underscore verbatim, by falling through. |
| 1340 | */ |
| 1341 | } else if (globalSubscript) { |
| 1342 | globalSubscript = false; |
| 1343 | wordItalic = wordBold = wordCode = false; |
| 1344 | continue; |
| 1345 | } else if (in [0] == U'_') { |
| 1346 | static integer countGlobalSubscript; |
| 1347 | trace (U"Global subscript: ", ++ countGlobalSubscript, U" ", txt); |
| 1348 | globalSubscript = true; |
| 1349 | in ++; |
| 1350 | wordItalic = wordBold = wordCode = false; |
| 1351 | continue; |
| 1352 | } else if (! weAreInManual) { // TODO: this is temporary |
| 1353 | static integer countCharacterSubscript; |
| 1354 | trace (U"Character subscript: ", ++ countCharacterSubscript, U" ", txt); |
| 1355 | charSubscript = true; |
| 1356 | wordItalic = wordBold = wordCode = false; |
| 1357 | continue; |
| 1358 | } else if (weAreInNotebook) { // TODO: this is temporary |
| 1359 | charSubscript = true; |
| 1360 | wordItalic = wordBold = wordCode = false; |
| 1361 | continue; |
| 1362 | } else { |
| 1363 | // a normal underscore in manuals |
| 1364 | static integer countVerbatimSubscript; |
| 1365 | trace (U"Verbatim subscript: ", ++ countVerbatimSubscript, U" ", txt); |
| 1366 | } |
| 1367 | } else if (kar == U'%' && my percentSignIsItalic) { |
| 1368 | if (globalVerbatim || verbatimLink) { |
| 1369 | /* |
| 1370 | Output the percent sign verbatim, by falling through. |
| 1371 | */ |
| 1372 | } else if (globalItalic) { |
| 1373 | globalItalic = false; |
| 1374 | continue; |
| 1375 | } else if (in [0] == U'%') { |
| 1376 | static integer countGlobalItalic; |
| 1377 | trace (U"Global italic: ", ++ countGlobalItalic, U" ", txt); |
| 1378 | globalItalic = true; |
| 1379 | in ++; |
| 1380 | continue; |
| 1381 | } else if (weAreInManual) { |
| 1382 | static integer countWordItalic, countCharacterItalic; |
| 1383 | if (Melder_isWordCharacter (in [0]) && Melder_isWordCharacter (in [1]) && in [1] != U'_') |
| 1384 | trace (U"Word-italic: ", ++ countWordItalic, U" ", txt); |
| 1385 | else |
| 1386 | trace (U"Character-italic: ", ++ countCharacterItalic, U" ", txt); |
| 1387 | if (in [0] == U'`' && my backquoteIsVerbatim) |
| 1388 | globalItalic = true; |
| 1389 | else |
| 1390 | wordItalic = true; |
| 1391 | continue; |
| 1392 | } else if (weAreInNotebook) { // TODO: this is temporary |
| 1393 | charItalic = true; |
| 1394 | continue; |
| 1395 | } else { |
| 1396 | charItalic = true; |
| 1397 | continue; |
| 1398 | } |
| 1399 | } else if (kar == U'#' && my numberSignIsBold) { |
| 1400 | if (globalVerbatim || verbatimLink) { |
| 1401 | /* |
| 1402 | Output the hash sign verbatim, by falling through. |
| 1403 | */ |
| 1404 | } else if (globalBold) { |
| 1405 | globalBold = false; |
| 1406 | continue; |
| 1407 | } else if (in [0] == U'#') { |
| 1408 | static integer countGlobalBold; |
| 1409 | trace (U"Global bold: ", ++ countGlobalBold, U" ", txt); |
| 1410 | globalBold = true; |
| 1411 | in ++; |
| 1412 | continue; |
| 1413 | } else if (weAreInManual) { |
| 1414 | static integer countWordBold, countCharacterBold; |
| 1415 | if (Melder_isWordCharacter (in [0]) && Melder_isWordCharacter (in [1]) && in [1] != U'_') |
| 1416 | trace (U"Word-bold: ", ++ countWordBold, U" ", txt); |
| 1417 | else |
| 1418 | trace (U"Character-bold: ", ++ countCharacterBold, U" ", txt); |
| 1419 | if (in [0] == U'`' && my backquoteIsVerbatim) |
| 1420 | globalBold = true; |
| 1421 | else |
| 1422 | wordBold = true; |
| 1423 | continue; |
| 1424 | } else { |
| 1425 | charBold = true; |
| 1426 | continue; |
| 1427 | } |
| 1428 | } else if (kar == U'`' && my backquoteIsVerbatim && ! topDownVerbatim) { |
| 1429 | if (verbatimLink) { |
| 1430 | verbatimLink = false; |
| 1431 | continue; |
| 1432 | } else if (globalVerbatim) { |
| 1433 | if (in [0] == U'`') // a double backquote means a backquote |
| 1434 | in ++; |
| 1435 | else { |
| 1436 | globalVerbatim = false; |
| 1437 | globalItalic = false; |
| 1438 | globalBold = false; |
| 1439 | globalLink = false; |
| 1440 | continue; |
| 1441 | } |
| 1442 | } else { |
| 1443 | globalVerbatim = true; |
| 1444 | continue; |
| 1445 | } |
| 1446 | } else if (kar == U'$' && my dollarSignIsCode) { |
| 1447 | if (globalVerbatim || verbatimLink) { |
| 1448 | /* |
| 1449 | Output the dollar sign verbatim, by falling through. |
| 1450 | */ |
| 1451 | } else if (globalCode) { |
| 1452 | globalCode = false; |
| 1453 | continue; |
| 1454 | } else if (in [0] == U'$') { |
| 1455 | globalCode = true; |
| 1456 | in ++; |
| 1457 | continue; |
| 1458 | } else { |
| 1459 | wordCode = true; |
| 1460 | continue; |
| 1461 | } |
| 1462 | } else if (kar == U'@' && my atSignIsLink // recognize links |
| 1463 | && my textRotation == 0.0) // no links allowed in rotated text, because links are identified by 2-point rectangles |
| 1464 | { |
| 1465 | /* |
| 1466 | We will distinguish: |
| 1467 | 1. The link text: the text shown to the user, drawn in blue. |
| 1468 | 2. The link info: the information saved in the Graphics object when the user clicks the link; |
| 1469 | this may be a page title in a manual or any other information. |
| 1470 | The link info is equal to the link text in the following cases: |
| 1471 | 1. A single-word link: "this is a @Link that consists of one word". |
| 1472 | 2. Longer links without "|" in them: "@@Link with spaces@". |
| 1473 | The link info is unequal to the link text in the following case: |
| 1474 | 3. Longer links with "|" in them: "@@Page linked to|Text shown in blue@" |
| 1475 | */ |
| 1476 | if (globalVerbatim) { |
| 1477 | /* |
| 1478 | Output the at sign verbatim, by falling through. |
| 1479 | */ |
| 1480 | } else if (globalLink) { |
| 1481 | if (topDownVerbatim) { |
| 1482 | /* |
| 1483 | Output the at sign verbatim, by falling through. |
| 1484 | */ |
| 1485 | } else { |
| 1486 | /* |
| 1487 | Detected the third "@" in strings like "@@Link with spaces@". |
| 1488 | This closes the link text (which will be shown in blue). |
| 1489 | */ |
| 1490 | globalLink = false; // close the drawn link text (the normal colour will take over) |
| 1491 | continue; // the "@" must not be drawn |
| 1492 | } |
| 1493 | } else if (in [0] == U'@') { |
| 1494 | /* |
| 1495 | Detected the second "@" in strings like "@@Link with spaces@". |
| 1496 | A format like "@@Page linked to|Text shown in blue@" is permitted, |
| 1497 | as is @@PointProcess: ||Draw...@. |
| 1498 | First step: collect the page text (the link information); |
| 1499 | it is everything between "@@" and "|" or "@" or end of string. |
| 1500 | */ |
| 1501 | const char32 *from = in + 1; // start with first character after "@@" |
| 1502 | ++ numberOfLinks; |
| 1503 | Melder_assert (numberOfLinks < MAXIMUM_NUMBER_OF_LINKS_PER_STRING); |
| 1504 | if (! links [numberOfLinks]. name) // make room for saving link info |
| 1505 | links [numberOfLinks]. name = Melder_calloc_f (char32, MAX_LINK_LENGTH + 1); |
| 1506 | char32 *to = links [numberOfLinks]. name; |
| 1507 | char32 *max = to + MAX_LINK_LENGTH; |
| 1508 | while (*from && *from != U'@') { // until end-of-string or '@'... |
| 1509 | if (*from == U'|') { |
| 1510 | if (from [1] == U'|' && from [2] != U'\0' && from [2] != U'@') { |
| 1511 | /* |
| 1512 | Include the stuff after "||" into the link info. |
| 1513 | */ |
| 1514 | from += 2; |
| 1515 | in += to - links [numberOfLinks]. name + 2; // skip head of link info as well as "||" |
| 1516 | } else { |
| 1517 | /* |
| 1518 | Second step: collect the link text that is to be drawn. |
| 1519 | Its characters will be collected during the normal cycles of the loop. |
| 1520 | If the link info is equal to the link text, no action is needed. |
| 1521 | If, on the other hand, there is a separate link info, this will have to be skipped. |
| 1522 | */ |
| 1523 | in += to - links [numberOfLinks]. name + 1; // skip link info as well as "|" |
| 1524 | break; // ..or until single '|'... |
| 1525 | } |
| 1526 | } |
| 1527 | if (to < max) |
| 1528 | *to ++ = *from ++; // ... copy one character |
| 1529 | } |
| 1530 | *to = U'\0'; // close saved link info |
| 1531 | /* |
| 1532 | We are entering the link-text-collection mode. |
| 1533 | */ |
| 1534 | globalLink = true; |
| 1535 | /* |
| 1536 | Both "@" must be skipped and must not be drawn. |
| 1537 | */ |
| 1538 | in ++; // skip second '@' |
| 1539 | continue; // do not draw |
| 1540 | } else if (in [0] == U'`' && my backquoteIsVerbatim) { |
| 1541 | const char32 *from = in; // start with the opening backquote |
| 1542 | ++ numberOfLinks; |
| 1543 | Melder_assert (numberOfLinks < MAXIMUM_NUMBER_OF_LINKS_PER_STRING); |
| 1544 | if (! links [numberOfLinks]. name) // make room for saving link info |
| 1545 | links [numberOfLinks]. name = Melder_calloc_f (char32, MAX_LINK_LENGTH + 1); |
| 1546 | char32 *to = links [numberOfLinks]. name; |
| 1547 | char32 *max = to + MAX_LINK_LENGTH; |
| 1548 | *to ++ = *from++; // copy the backquote |
| 1549 | while (*from && *from != U'`' && to < max) // until end-of-verbatim... |
| 1550 | *to ++ = *from++; // ... copy one character |
| 1551 | *to ++ = U'`'; |
| 1552 | *to = U'\0'; // close saved link info |
| 1553 | trace (U"Verbatim link to: ", links [numberOfLinks]. name); |
| 1554 | /* |
| 1555 | Second step: collect the link text that is to be drawn. |
| 1556 | Its characters will be collected during the normal cycles of the loop. |
| 1557 | The link info is equal to the link text, so no skipping is needed. |
| 1558 | */ |
| 1559 | in ++; // skip the opening backquote |
| 1560 | verbatimLink = true; // enter the link-text-collection mode |
| 1561 | continue; |
| 1562 | } else { |
| 1563 | /* |
| 1564 | Detected a single-word link, like in "this is a @Link that consists of one word". |
| 1565 | First step: collect the page text: letters, digits, and underscores. |
| 1566 | */ |
| 1567 | const char32 *from = in; // start with first character after "@" |
| 1568 | ++ numberOfLinks; |
| 1569 | Melder_assert (numberOfLinks < MAXIMUM_NUMBER_OF_LINKS_PER_STRING); |
| 1570 | if (! links [numberOfLinks]. name) // make room for saving link info |
| 1571 | links [numberOfLinks]. name = Melder_calloc_f (char32, MAX_LINK_LENGTH + 1); |
| 1572 | char32 *to = links [numberOfLinks]. name; |
| 1573 | char32 *max = to + MAX_LINK_LENGTH; |
| 1574 | while (*from && (Melder_isWordCharacter (*from) || *from == U'_') && to < max) // until end-of-word... |
| 1575 | *to ++ = *from++; // ... copy one character |
| 1576 | *to = U'\0'; // close saved link info |
| 1577 | /* |
| 1578 | Second step: collect the link text that is to be drawn. |
| 1579 | Its characters will be collected during the normal cycles of the loop. |
| 1580 | The link info is equal to the link text, so no skipping is needed. |
| 1581 | */ |
| 1582 | wordLink = true; // enter the single-word link-text-collection mode |
| 1583 | continue; |
| 1584 | } |
| 1585 | } else if (kar == U'\\') { |
| 1586 | /* |
| 1587 | Detected backslash sequence: backslash + kar1 + kar2... |
| 1588 | */ |
| 1589 | char32 kar1, kar2; |
| 1590 | /* |
| 1591 | ... except if kar1 or kar2 is null: in that case, draw the backslash. |
| 1592 | */ |
| 1593 | if ((kar1 = in [0]) == U'\0' || (kar2 = in [1]) == U'\0') { |
| 1594 | ; // normal backslash symbol |
| 1595 | } else if (topDownVerbatim && |
| 1596 | (kar1 == U'@' && kar2 == U'{' || |
| 1597 | kar1 == U'`' && kar2 == U'{' || |
| 1598 | kar1 == U'#' && kar2 == U'@' && in [2] == U'{' || |
| 1599 | kar1 == U'#' && kar2 == U'`' && in [2] == U'{') |
| 1600 | ) { |
| 1601 | /* |
| 1602 | Detected "\@{" or "\`{" or "\#@{" or "\#`{" |
| 1603 | in strings like "\@{Link with spaces}" or "\`{writeInfoLine}". |
| 1604 | A format like "\@{Page linked to|Text shown in blue}" is permitted, |
| 1605 | as is \@{PointProcess: ||Draw}. |
| 1606 | */ |
| 1607 | const bool isVerbatimLink = ( kar1 == U'`' || kar2 == U'`' ); |
| 1608 | thinLink = ( kar1 != U'#' ); |
| 1609 | const char32 *from = in + 3 - thinLink; // start with first character after "\@{" |
| 1610 | ++ numberOfLinks; |
| 1611 | Melder_assert (numberOfLinks < MAXIMUM_NUMBER_OF_LINKS_PER_STRING); |
| 1612 | if (! links [numberOfLinks]. name) // make room for saving link info |
| 1613 | links [numberOfLinks]. name = Melder_calloc_f (char32, MAX_LINK_LENGTH + 1); |
| 1614 | char32 *to = links [numberOfLinks]. name; |
| 1615 | char32 *max = to + MAX_LINK_LENGTH; |
| 1616 | if (isVerbatimLink) |
| 1617 | *to ++ = U'`'; |
| 1618 | while (*from && *from != U'}') { // until end-of-string or '}' or... |
| 1619 | if (*from == U'|') { |
| 1620 | if (from [1] == U'|' && from [2] != U'\0' && from [2] != U'}') { |
| 1621 | /* |
| 1622 | Include the stuff after "||" into the link info. |
| 1623 | */ |
| 1624 | from += 2; |
| 1625 | in += to - links [numberOfLinks]. name + 2; // skip head of link info as well as "||" |
| 1626 | } else { |
| 1627 | /* |
| 1628 | Second step: collect the link text that is to be drawn. |
| 1629 | Its characters will be collected during the normal cycles of the loop. |
| 1630 | If the link info is equal to the link text, no action is needed. |
| 1631 | If, on the other hand, there is a separate link info, this will have to be skipped. |
| 1632 | */ |
| 1633 | in += to - links [numberOfLinks]. name + 1; // skip link info as well as "|" |
| 1634 | break; // ...or until single '|'... |
| 1635 | } |
| 1636 | } |
| 1637 | if (to < max) |
| 1638 | * to ++ = * from ++; // ... copy one character |
| 1639 | } |
| 1640 | *to = U'\0'; // close saved link info |
| 1641 | /* |
| 1642 | Replace final colon with three dots. |
| 1643 | This has to be done *after* the above increments of `in`. |
| 1644 | */ |
| 1645 | if (to - links [numberOfLinks]. name > 0 && to [-1] == U':') { |
| 1646 | to [-1] = U'.'; |
| 1647 | if (to < max) |
| 1648 | *to ++ = U'.'; |
| 1649 | if (to < max) |
| 1650 | *to ++ = U'.'; |
| 1651 | *to = U'\0'; // close saved link info again |
| 1652 | } |
| 1653 | if (isVerbatimLink && to < max) { |
| 1654 | *to ++ = U'`'; |
| 1655 | *to = U'\0'; // close saved link info again |
| 1656 | } |
| 1657 | /* |
| 1658 | We are entering the link-text-collection mode. |
| 1659 | */ |
| 1660 | globalLink = true; |
| 1661 | /* |
| 1662 | The closing "}" must be skipped and must not be drawn. |
| 1663 | */ |
| 1664 | in += 3 - thinLink; // skip '}' |
| 1665 | continue; // do not draw |
| 1666 | } else if (topDownVerbatim && kar1 == U'#' && kar2 == U'{') { |
| 1667 | globalBold = true; |
| 1668 | in += 2; |
| 1669 | continue; |
| 1670 | } else if (topDownVerbatim && kar1 == U'%' && kar2 == U'{') { |
| 1671 | globalItalic = true; |
| 1672 | in += 2; |
| 1673 | continue; |
| 1674 | } else if (globalVerbatim) { |
| 1675 | ; // normal backslash symbol |
| 1676 | } else if (kar1 == U's' && kar2 == U'{') { |
| 1677 | /* |
| 1678 | Catch "\s{", which means: small characters until corresponding '}'. |
| 1679 | */ |
| 1680 | globalSmall = true; |
| 1681 | in += 2; |
| 1682 | continue; |
| 1683 | } else { |
| 1684 | /* |
| 1685 | Default action: translate the backslash sequence into the long character 'kar1,kar2'. |
| 1686 | */ |
| 1687 | kar = Longchar_getInfo (kar1, kar2) -> unicode; |
| 1688 | in += 2; |
| 1689 | } |
| 1690 | } else if (kar == U'\"') { |
| 1691 | if (! (my font == kGraphics_font::COURIER || my fontStyle == Graphics_CODE || wordCode || globalCode)) |
| 1692 | kar = ++nquote & 1 ? UNICODE_LEFT_DOUBLE_QUOTATION_MARK : UNICODE_RIGHT_DOUBLE_QUOTATION_MARK; |
| 1693 | } else if (kar == U'\'' && ! my backquoteIsVerbatim) { |
| 1694 | kar = UNICODE_RIGHT_SINGLE_QUOTATION_MARK; |
| 1695 | } else if (kar == U'`' && ! my backquoteIsVerbatim) { |
| 1696 | kar = UNICODE_LEFT_SINGLE_QUOTATION_MARK; |
| 1697 | } else if (kar >= 32 && kar <= 126) { |
| 1698 | if (kar == U'f') { |
| 1699 | if (in [0] == U'i' && HAS_FI_AND_FL_LIGATURES && ! (my font == kGraphics_font::COURIER || my fontStyle == Graphics_CODE || wordCode || globalCode)) { |
| 1700 | kar = UNICODE_LATIN_SMALL_LIGATURE_FI; |
| 1701 | in ++; |
| 1702 | } else if (in [0] == U'l' && HAS_FI_AND_FL_LIGATURES && ! (my font == kGraphics_font::COURIER || my fontStyle == Graphics_CODE || wordCode || globalCode)) { |
| 1703 | kar = UNICODE_LATIN_SMALL_LIGATURE_FL; |
| 1704 | in ++; |
| 1705 | } |
| 1706 | } else if (kar == U'}') { |
| 1707 | if (globalSmall) { |
| 1708 | globalSmall = false; |
| 1709 | continue; |
| 1710 | } else if (globalBold && topDownVerbatim) { |
| 1711 | globalBold = false; |
| 1712 | continue; |
| 1713 | } else if (globalItalic && topDownVerbatim) { |
| 1714 | globalItalic = false; |
| 1715 | continue; |
| 1716 | } else if (globalLink && topDownVerbatim) { |
| 1717 | globalLink = false; |
| 1718 | continue; |
| 1719 | } |
| 1720 | } |
| 1721 | } else if (kar == U'\t') { |
| 1722 | out -> kar = U'\t'; |
| 1723 | out -> rightToLeft = false; |
| 1724 | wordItalic = wordBold = wordCode = wordLink = false; |
| 1725 | globalSubscript = globalSuperscript = globalItalic = globalBold = globalCode = globalLink = verbatimLink = globalSmall = false; |
| 1726 | charItalic = charBold = charSuperscript = charSubscript = false; |
| 1727 | out ++; |
| 1728 | continue; // do not draw |
| 1729 | } else if (kar == U'\n') { |
| 1730 | kar = U' '; |
| 1731 | } |
| 1732 | if (wordItalic || wordBold || wordCode || wordLink) { |
| 1733 | if (! Melder_isWordCharacter (kar) && kar != U'_') |
| 1734 | wordItalic = wordBold = wordCode = wordLink = false; |
| 1735 | } |
| 1736 | out -> style = |
| 1737 | (wordLink || globalLink || verbatimLink) && my fontStyle != Graphics_CODE ? (thinLink ? 0 : Graphics_BOLD) : |
| 1738 | ((my fontStyle & Graphics_ITALIC) || charItalic || wordItalic || globalItalic ? Graphics_ITALIC : 0) + |
| 1739 | ((my fontStyle & Graphics_BOLD) || charBold || wordBold || globalBold ? Graphics_BOLD : 0) |
| 1740 | ; |
| 1741 | out -> font.string = nullptr; |
| 1742 | out -> font.integer_ = my fontStyle == Graphics_CODE || wordCode || globalCode || globalVerbatim || verbatimLink || |
| 1743 | kar == U'/' && my font != kGraphics_font::PALATINO ? (int) kGraphics_font::COURIER : (int) my font; |
| 1744 | out -> link = wordLink || globalLink || verbatimLink; |
| 1745 | out -> baseline = charSuperscript || globalSuperscript ? 34 : charSubscript || globalSubscript ? -25 : 0; |
| 1746 | out -> size = globalSmall || out -> baseline != 0 ? 80 : 100; |
| 1747 | out -> code = U'?'; // does this have any meaning? |
| 1748 | Melder_assert (kar != U'\0'); |
| 1749 | if (my postScript) { |
| 1750 | if (kar == UNICODE_LATIN_SMALL_LETTER_TS_DIGRAPH) { |
| 1751 | kar = U't'; |
| 1752 | out -> kar = kar; |
| 1753 | out -> karInfo = Longchar_getInfoFromNative (kar); |
| 1754 | Melder_assert (out -> karInfo); |
| 1755 | out -> rightToLeft = false; |
| 1756 | kar = U's'; |
| 1757 | out ++; |
| 1758 | * out = out [-1]; |
| 1759 | } else if (kar == UNICODE_LATIN_SMALL_LETTER_TESH_DIGRAPH) { |
| 1760 | kar = U't'; |
| 1761 | out -> kar = kar; |
| 1762 | out -> karInfo = Longchar_getInfoFromNative (kar); |
| 1763 | Melder_assert (out -> karInfo); |
| 1764 | out -> rightToLeft = false; |
| 1765 | kar = UNICODE_LATIN_SMALL_LETTER_ESH; |
| 1766 | out ++; |
| 1767 | * out = out [-1]; |
| 1768 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_H) { |
| 1769 | kar = U'h'; |
| 1770 | out -> baseline = 34; |
| 1771 | out -> size = 80; |
| 1772 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_H_WITH_HOOK) { |
| 1773 | kar = UNICODE_LATIN_SMALL_LETTER_H_WITH_HOOK; |
| 1774 | out -> baseline = 34; |
| 1775 | out -> size = 80; |
| 1776 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_GAMMA) { |
| 1777 | kar = UNICODE_LATIN_SMALL_LETTER_GAMMA; |
| 1778 | out -> baseline = 34; |
| 1779 | out -> size = 80; |
| 1780 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_W) { |
| 1781 | kar = U'w'; |
| 1782 | out -> baseline = 34; |
| 1783 | out -> size = 80; |
| 1784 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_TURNED_H) { |
| 1785 | kar = UNICODE_LATIN_SMALL_LETTER_TURNED_H; |
| 1786 | out -> baseline = 34; |
| 1787 | out -> size = 80; |
| 1788 | } else if (kar == UNICODE_MODIFIER_LETTER_GLOTTAL_STOP) { |
| 1789 | kar = UNICODE_LATIN_LETTER_GLOTTAL_STOP; |
| 1790 | out -> baseline = 34; |
| 1791 | out -> size = 80; |
| 1792 | } else if (kar == UNICODE_MODIFIER_LETTER_REVERSED_GLOTTAL_STOP) { |
| 1793 | kar = UNICODE_LATIN_LETTER_PHARYNGEAL_VOICED_FRICATIVE; |
| 1794 | out -> baseline = 34; |
| 1795 | out -> size = 80; |
| 1796 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_L) { |
| 1797 | kar = U'l'; |
| 1798 | out -> baseline = 34; |
| 1799 | out -> size = 80; |
| 1800 | } else if (kar == UNICODE_SUPERSCRIPT_LATIN_SMALL_LETTER_N) { |
| 1801 | kar = U'n'; |
| 1802 | out -> baseline = 34; |
| 1803 | out -> size = 80; |
| 1804 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_M) { |
| 1805 | kar = U'm'; |
| 1806 | out -> baseline = 34; |
| 1807 | out -> size = 80; |
| 1808 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_ENG) { |
| 1809 | kar = UNICODE_LATIN_SMALL_LETTER_ENG; |
| 1810 | out -> baseline = 34; |
| 1811 | out -> size = 80; |
| 1812 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_S) { |
| 1813 | kar = U's'; |
| 1814 | out -> baseline = 34; |
| 1815 | out -> size = 80; |
| 1816 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_X) { |
| 1817 | kar = U'x'; |
| 1818 | out -> baseline = 34; |
| 1819 | out -> size = 80; |
| 1820 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_F) { |
| 1821 | kar = U'f'; |
| 1822 | out -> baseline = 34; |
| 1823 | out -> size = 80; |
| 1824 | } else if (kar == UNICODE_MODIFIER_LETTER_SMALL_Y) { |
| 1825 | kar = U'y'; |
| 1826 | out -> baseline = 34; |
| 1827 | out -> size = 80; |
| 1828 | } |
| 1829 | } |
| 1830 | out -> kar = kar; |
| 1831 | out -> karInfo = Longchar_getInfoFromNative (kar); |
| 1832 | Melder_assert (out -> karInfo); |
| 1833 | out -> rightToLeft = |
| 1834 | (kar >= 0x0590 && kar <= 0x06FF) || |
| 1835 | (kar >= 0xFE70 && kar <= 0xFEFF) || |
| 1836 | (kar >= 0xFB1E && kar <= 0xFDFF); |
| 1837 | charItalic = charBold = charSuperscript = charSubscript = false; |
| 1838 | out ++; |
| 1839 | } |
| 1840 | out -> kar = U'\0'; // end of text |
| 1841 | out -> karInfo = Longchar_getInfoFromNative (kar); |
| 1842 | Melder_assert (out -> karInfo); |
| 1843 | out -> rightToLeft = false; |
| 1844 | } |
| 1845 | |
| 1846 | double Graphics_textWidth (Graphics me, conststring32 txt) { |
| 1847 | if (! initBuffer (txt)) |
| 1848 | return 0.0; |
| 1849 | //Melder_casual (U"Graphics_textWidth: workstation viewport ", my d_x1DC, U" ", my d_x2DC, U" ", my d_y1DC, U" ", my d_y2DC); |
| 1850 | //Melder_casual (U"Graphics_textWidth: workstation window ", my d_x1wNDC, U" ", my d_x2wNDC, U" ", my d_y1wNDC, U" ", my d_y2wNDC); |
| 1851 | //Melder_casual (U"Graphics_textWidth: viewport ", my d_x1NDC, U" ", my d_x2NDC, U" ", my d_y1NDC, U" ", my d_y2NDC); |
| 1852 | //Melder_casual (U"Graphics_textWidth: window ", my d_x1WC, U" ", my d_x2WC, U" ", my d_y1WC, U" ", my d_y2WC); |
| 1853 | #if cairo |
| 1854 | Melder_assert (Thing_isa (me, classGraphicsScreen)); |
| 1855 | cairo_t *oldCairoGraphicsContext = ((GraphicsScreen) me) -> d_cairoGraphicsContext; |
| 1856 | cairo_surface_t *cairoSurface; |
| 1857 | if (! oldCairoGraphicsContext) |
| 1858 | #ifndef NO_GUI |
| 1859 | if (((GraphicsScreen) me) -> d_window) { |
| 1860 | //Melder_casual (U"Graphics_textWidth: creating a graphics context in an existing widget."); |
| 1861 | ((GraphicsScreen) me) -> d_cairoGraphicsContext = gdk_cairo_create (((GraphicsScreen) me) -> d_window); |
| 1862 | } else |
| 1863 | #endif |
| 1864 | { |
| 1865 | //Melder_casual (U"Graphics_textWidth: creating a graphics context outside any existing widget."); |
| 1866 | cairoSurface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, 1/*my d_x2DC - my d_x1DC*/, 1/*my d_y2DC - my d_y1DC*/); |
| 1867 | ((GraphicsScreen) me) -> d_cairoGraphicsContext = cairo_create (cairoSurface); |
| 1868 | } |
| 1869 | #endif |
| 1870 | parseTextIntoCellsLinesRuns (me, txt, theWidechar); |
| 1871 | charSizes (me, theWidechar, false); |
| 1872 | const double width = textWidth (theWidechar); |
| 1873 | //Melder_casual (U"Graphics_textWidth: width ", width, U", scale ", my scaleX); |
| 1874 | #if cairo |
| 1875 | if (! oldCairoGraphicsContext) { |
| 1876 | #ifndef NO_GUI |
| 1877 | if (((GraphicsScreen) me) -> d_window) { |
| 1878 | cairo_destroy (((GraphicsScreen) me) -> d_cairoGraphicsContext); |
| 1879 | } else |
| 1880 | #endif |
| 1881 | { |
| 1882 | cairo_destroy (((GraphicsScreen) me) -> d_cairoGraphicsContext); |
| 1883 | cairo_surface_destroy (cairoSurface); |
| 1884 | } |
| 1885 | ((GraphicsScreen) me) -> d_cairoGraphicsContext = nullptr; |
| 1886 | } |
| 1887 | #endif |
| 1888 | return width / my scaleX; |
| 1889 | } |
| 1890 | |
| 1891 | void Graphics_rectangleText_wrapAndTruncate (Graphics me, double x1, double x2, double y1, double y2, conststring32 text) { |
| 1892 | if (my recording) { |
| 1893 | const conststring8 text_utf8 = Melder_peek32to8 (text); |
| 1894 | const integer length = Melder8_length (text_utf8) / (integer) sizeof (double) + 1; |
| 1895 | op (RECTANGLE_TEXT_WRAP_AND_TRUNCATE, 5 + length); put (x1); put (x2); put (y1); put (y2); sput (text_utf8, length) |
| 1896 | } else { |
| 1897 | double width = 0.0, lineHeight = (1.1 / 72) * my fontSize * my resolution; |
| 1898 | const integer x1DC = x1 * my scaleX + my deltaX + 2, x2DC = x2 * my scaleX + my deltaX - 2; |
| 1899 | const integer y1DC = y1 * my scaleY + my deltaY, y2DC = y2 * my scaleY + my deltaY; |
| 1900 | const int availableHeight = ( my yIsZeroAtTheTop ? y1DC - y2DC : y2DC - y1DC ), availableWidth = x2DC - x1DC; |
| 1901 | const int linesAvailable = Melder_clippedLeft (1, int (availableHeight / lineHeight)); |
| 1902 | if (availableWidth <= 0) |
| 1903 | return; |
| 1904 | if (! initBuffer (text)) |
| 1905 | return; |
| 1906 | parseTextIntoCellsLinesRuns (me, text, theWidechar); |
| 1907 | charSizes (me, theWidechar, true); |
| 1908 | int linesNeeded = 1; |
| 1909 | for (_Graphics_widechar *plc = theWidechar; plc -> kar > U'\t'; plc ++) { |
| 1910 | width += plc -> width; |
| 1911 | if (width > availableWidth) { |
| 1912 | if (++ linesNeeded > linesAvailable) |
| 1913 | break; |
| 1914 | width = 0.0; |
| 1915 | } |
| 1916 | } |
| 1917 | const int lines = Melder_clippedRight (linesNeeded, linesAvailable); |
| 1918 | _Graphics_widechar *startOfLine = theWidechar; |
| 1919 | for (int iline = 1; iline <= lines; iline ++) { |
| 1920 | width = 0.0; |
| 1921 | for (_Graphics_widechar *plc = startOfLine; plc -> kar > U'\t'; plc ++) { |
| 1922 | bool flush = false; |
| 1923 | width += plc -> width; |
| 1924 | if (width > availableWidth) |
| 1925 | flush = true; |
| 1926 | /* |
| 1927 | Trick for incorporating end-of-text. |
| 1928 | */ |
| 1929 | if (! flush && plc [1]. kar <= U'\t') { |
| 1930 | Melder_assert (iline == lines); |
| 1931 | plc ++; // brr |
| 1932 | flush = true; |
| 1933 | } |
| 1934 | if (flush) { |
| 1935 | const char32 saveKar = plc -> kar; |
| 1936 | const int direction = ( my yIsZeroAtTheTop ? -1 : 1 ); |
| 1937 | const int x = ( |
| 1938 | my horizontalTextAlignment == (int) Graphics_LEFT ? |
| 1939 | x1DC |
| 1940 | : my horizontalTextAlignment == (int) Graphics_RIGHT ? |
| 1941 | x2DC |
| 1942 | : |
| 1943 | 0.5 * (x1 + x2) * my scaleX + my deltaX |
| 1944 | ); |
| 1945 | const int y = ( |
| 1946 | my verticalTextAlignment == Graphics_BOTTOM ? |
| 1947 | y1DC + direction * (lines - iline) * lineHeight |
| 1948 | : my verticalTextAlignment == Graphics_TOP ? |
| 1949 | y2DC - direction * (iline - 1) * lineHeight |
| 1950 | : |
| 1951 | 0.5 * (y1 + y2) * my scaleY + my deltaY + 0.5 * direction * (lines - iline*2 + 1) * lineHeight |
| 1952 | ); |
| 1953 | plc -> kar = U'\0'; |
| 1954 | drawOneCell (me, x, y, startOfLine); |
| 1955 | plc -> kar = saveKar; |
| 1956 | startOfLine = plc; |
| 1957 | break; |
| 1958 | }// endif flush |
| 1959 | }// next plc |
| 1960 | }// next iline |
| 1961 | }// endif my recording |
| 1962 | } |
| 1963 | |
| 1964 | static void nonrecorded_Graphics_text (Graphics me, double xWC, double yWC, conststring32 text) { |
| 1965 | if (my wrapWidth == 0.0 && str32chr (text, U'\n') && my textRotation == 0.0) { |
| 1966 | const double lineSpacingWC = (1.2/72.0) * my fontSize * my resolution / fabs (my scaleY); |
| 1967 | integer numberOfLines = 1; |
| 1968 | for (const char32 *p = & text [0]; *p != U'\0'; p ++) |
| 1969 | if (*p == U'\n') |
| 1970 | numberOfLines ++; |
| 1971 | yWC += ( |
| 1972 | my verticalTextAlignment == Graphics_TOP ? |
| 1973 | 0.0 |
| 1974 | : my verticalTextAlignment == Graphics_HALF ? |
| 1975 | 0.5 * (numberOfLines - 1) * lineSpacingWC |
| 1976 | : |
| 1977 | (numberOfLines - 1) * lineSpacingWC |
| 1978 | ); |
| 1979 | autostring32 linesToDraw = Melder_dup_f (text); |
| 1980 | const char32 *p = & linesToDraw [0]; |
| 1981 | for (;;) { |
| 1982 | char32 *const newline = str32chr (p, U'\n'); |
| 1983 | if (newline) |
| 1984 | *newline = U'\0'; |
| 1985 | nonrecorded_Graphics_text (me, xWC, yWC, p); // recurse |
| 1986 | yWC -= lineSpacingWC; |
| 1987 | if (newline) |
| 1988 | p = newline + 1; |
| 1989 | else |
| 1990 | break; |
| 1991 | } |
| 1992 | return; |
| 1993 | } |
| 1994 | if (! initBuffer (text)) |
| 1995 | return; |
| 1996 | parseTextIntoCellsLinesRuns (me, text, theWidechar); |
| 1997 | drawCells (me, xWC, yWC, theWidechar); |
| 1998 | } |
| 1999 | |
| 2000 | void Graphics_text (Graphics me, double xWC, double yWC, conststring32 text) { |
| 2001 | if (my recording) { |
| 2002 | const conststring8 text_utf8 = Melder_peek32to8 (text); |
| 2003 | const integer length = Melder8_length (text_utf8) / (integer) sizeof (double) + 1; |
| 2004 | op (TEXT, 3 + length); put (xWC); put (yWC); sput (text_utf8, length) |
| 2005 | } else { |
| 2006 | nonrecorded_Graphics_text (me, xWC, yWC, text); |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | void Graphics_rectangleText_maximalFit (Graphics me, |
| 2011 | const double x1, const double x2, const double minimumHorizontalMargin_in_textHeights, const double minimumHorizontalMargin_mm, |
| 2012 | const double y1, const double y2, const double minimumVerticalMargin_in_textHeights, const double minimumVerticalMargin_mm, |
| 2013 | conststring32 text |
| 2014 | ) { |
| 2015 | if (my recording) { |
| 2016 | const conststring8 text_utf8 = Melder_peek32to8 (text); |
| 2017 | const integer length = Melder8_length (text_utf8) / (integer) sizeof (double) + 1; |
| 2018 | op (RECTANGLE_TEXT_MAXIMAL_FIT, 9 + length); |
| 2019 | put (x1); put (x2); put (minimumHorizontalMargin_in_textHeights); put (minimumHorizontalMargin_mm); |
| 2020 | put (y1); put (y2); put (minimumVerticalMargin_in_textHeights); put (minimumVerticalMargin_mm); |
| 2021 | sput (text_utf8, length) |
| 2022 | } else { |
| 2023 | const double save_fontSize = my fontSize; |
| 2024 | my fontSize = 100; |
| 2025 | double textWidth_wc = Graphics_textWidth (me, text); |
| 2026 | double textHeight_mm = my fontSize * (25.4 / 72); |
| 2027 | double textHeight_wc = Graphics_dyMMtoWC (me, textHeight_mm); |
| 2028 | /* |
| 2029 | The number of 0.99 or 0.95 guards against rounding errors in font sizes (they may always be integers). |
| 2030 | */ |
| 2031 | #ifdef macintosh |
| 2032 | const double precision = 0.99; |
| 2033 | #else |
| 2034 | const double precision = 0.95; |
| 2035 | #endif |
| 2036 | double horizontalMargin_mm = |
| 2037 | std::max (minimumHorizontalMargin_in_textHeights * textHeight_mm, minimumHorizontalMargin_mm); |
| 2038 | double horizontalMargin_wc = Graphics_dxMMtoWC (me, horizontalMargin_mm); |
| 2039 | double maximumHorizontalScaling = std::max (0.001, precision * (x2 - x1 - 2.0 * horizontalMargin_wc) / textWidth_wc); |
| 2040 | double verticalMargin_mm = std::max (minimumVerticalMargin_in_textHeights * textHeight_mm, minimumVerticalMargin_mm); |
| 2041 | double verticalMargin_wc = Graphics_dyMMtoWC (me, verticalMargin_mm); |
| 2042 | double maximumVerticalScaling = std::max (0.001, (y2 - y1 - 2.0 * verticalMargin_wc) / textHeight_wc); |
| 2043 | double scaling = std::min (maximumHorizontalScaling, maximumVerticalScaling); |
| 2044 | my fontSize *= scaling; |
| 2045 | for (int improvementIteration = 1; improvementIteration <= 10; improvementIteration ++) { |
| 2046 | textWidth_wc *= scaling; |
| 2047 | textHeight_mm *= scaling; |
| 2048 | textHeight_wc *= scaling; |
| 2049 | horizontalMargin_mm = std::max (minimumHorizontalMargin_in_textHeights * textHeight_mm, minimumHorizontalMargin_mm); |
| 2050 | horizontalMargin_wc = Graphics_dxMMtoWC (me, horizontalMargin_mm); |
| 2051 | maximumHorizontalScaling = std::max (0.001, precision * (x2 - x1 - 2.0 * horizontalMargin_wc) / textWidth_wc); |
| 2052 | verticalMargin_mm = std::max (minimumVerticalMargin_in_textHeights * textHeight_mm, minimumVerticalMargin_mm); |
| 2053 | verticalMargin_wc = Graphics_dyMMtoWC (me, verticalMargin_mm); |
| 2054 | maximumVerticalScaling = std::max (0.001, (y2 - y1 - 2.0 * verticalMargin_wc) / textHeight_wc); |
| 2055 | //maximumVerticalScaling = 0.72 * (y2 - y1) / textHeight_wc; |
| 2056 | scaling = std::min (maximumHorizontalScaling, maximumVerticalScaling); |
| 2057 | if (scaling == 1.0) |
| 2058 | break; // we have converged |
| 2059 | my fontSize *= scaling; |
| 2060 | trace (improvementIteration, U" w wc ", textWidth_wc, U" h mm ", textHeight_mm, U" h wc ", textHeight_wc, |
| 2061 | U" m mm ", horizontalMargin_mm, U" scaling ", scaling, U" ", text); |
| 2062 | } |
| 2063 | if (scaling != 1.0) { |
| 2064 | //TRACE |
| 2065 | trace (U"0 w wc ", textWidth_wc, U" h mm ", textHeight_mm, U" h wc ", textHeight_wc, |
| 2066 | U" m mm ", horizontalMargin_mm, U" scaling ", scaling, U" ", text); |
| 2067 | } |
| 2068 | const double xWC = |
| 2069 | my horizontalTextAlignment == (int) Graphics_LEFT ? x1 + horizontalMargin_wc : |
| 2070 | my horizontalTextAlignment == (int) Graphics_RIGHT ? x2 - horizontalMargin_wc : |
| 2071 | 0.5 * (x1 + x2) |
| 2072 | ; |
| 2073 | const double yWC = |
| 2074 | my verticalTextAlignment == Graphics_BOTTOM ? y1 + verticalMargin_wc : |
| 2075 | my verticalTextAlignment == Graphics_TOP ? y2 - verticalMargin_wc : |
| 2076 | 0.5 * (y1 + y2) |
| 2077 | ; |
| 2078 | nonrecorded_Graphics_text (me, xWC, yWC, text); |
| 2079 | my fontSize = save_fontSize; |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | double Graphics_inqTextX (Graphics me) { return my textX; } |
| 2084 | double Graphics_inqTextY (Graphics me) { return my textY; } |
| 2085 | |
| 2086 | int Graphics_getLinks (Graphics_Link **plinks) { *plinks = & links [0]; return numberOfLinks; } |
| 2087 | |
| 2088 | static double psTextWidth (_Graphics_widechar string [], bool useSilipaPS) { |
| 2089 | /* |
| 2090 | The following has to be kept IN SYNC with GraphicsPostscript::charSize. |
| 2091 | */ |
| 2092 | double textWidth = 0.0; |
| 2093 | for (_Graphics_widechar *character = & string [0]; character -> kar > U'\t'; character ++) { |
| 2094 | Longchar_Info info = character -> karInfo; |
| 2095 | const int font = ( |
| 2096 | info -> alphabet == Longchar_SYMBOL ? |
| 2097 | kGraphics_font_SYMBOL |
| 2098 | : info -> alphabet == Longchar_PHONETIC ? |
| 2099 | kGraphics_font_IPATIMES |
| 2100 | : info -> alphabet == Longchar_DINGBATS ? |
| 2101 | kGraphics_font_DINGBATS |
| 2102 | : |
| 2103 | character -> font.integer_ |
| 2104 | ); |
| 2105 | const int style = ( |
| 2106 | character -> style == Graphics_ITALIC ? |
| 2107 | Graphics_ITALIC |
| 2108 | : character -> style == Graphics_BOLD || character -> link ? |
| 2109 | Graphics_BOLD |
| 2110 | : character -> style == Graphics_BOLD_ITALIC ? |
| 2111 | Graphics_BOLD_ITALIC |
| 2112 | : |
| 2113 | 0 |
| 2114 | ); |
| 2115 | const double size = character -> size * 0.01; |
| 2116 | double charWidth = 600; // Courier |
| 2117 | if (font == (int) kGraphics_font::COURIER) { |
| 2118 | charWidth = 600; |
| 2119 | } else if (style == 0) { |
| 2120 | if (font == (int) kGraphics_font::TIMES) |
| 2121 | charWidth = info -> ps.times; |
| 2122 | else if (font == (int) kGraphics_font::HELVETICA) |
| 2123 | charWidth = info -> ps.helvetica; |
| 2124 | else if (font == (int) kGraphics_font::PALATINO) |
| 2125 | charWidth = info -> ps.palatino; |
| 2126 | else if (font == kGraphics_font_IPATIMES && useSilipaPS) |
| 2127 | charWidth = info -> ps.timesItalic; |
| 2128 | else |
| 2129 | charWidth = info -> ps.times; // Symbol, IPA |
| 2130 | } else if (style == Graphics_BOLD) { |
| 2131 | if (font == (int) kGraphics_font::TIMES) |
| 2132 | charWidth = info -> ps.timesBold; |
| 2133 | else if (font == (int) kGraphics_font::HELVETICA) |
| 2134 | charWidth = info -> ps.helveticaBold; |
| 2135 | else if (font == (int) kGraphics_font::PALATINO) |
| 2136 | charWidth = info -> ps.palatinoBold; |
| 2137 | else if (font == kGraphics_font_IPATIMES && useSilipaPS) |
| 2138 | charWidth = info -> ps.timesBoldItalic; |
| 2139 | else |
| 2140 | charWidth = info -> ps.times; |
| 2141 | } else if (style == Graphics_ITALIC) { |
| 2142 | if (font == (int) kGraphics_font::TIMES) |
| 2143 | charWidth = info -> ps.timesItalic; |
| 2144 | else if (font == (int) kGraphics_font::HELVETICA) |
| 2145 | charWidth = info -> ps.helvetica; |
| 2146 | else if (font == (int) kGraphics_font::PALATINO) |
| 2147 | charWidth = info -> ps.palatinoItalic; |
| 2148 | else if (font == kGraphics_font_IPATIMES && useSilipaPS) |
| 2149 | charWidth = info -> ps.timesItalic; |
| 2150 | else |
| 2151 | charWidth = info -> ps.times; |
| 2152 | } else if (style == Graphics_BOLD_ITALIC) { |
| 2153 | if (font == (int) kGraphics_font::TIMES) |
| 2154 | charWidth = info -> ps.timesBoldItalic; |
| 2155 | else if (font == (int) kGraphics_font::HELVETICA) |
| 2156 | charWidth = info -> ps.helveticaBold; |
| 2157 | else if (font == (int) kGraphics_font::PALATINO) |
| 2158 | charWidth = info -> ps.palatinoBoldItalic; |
| 2159 | else if (font == kGraphics_font_IPATIMES && useSilipaPS) |
| 2160 | charWidth = info -> ps.timesBoldItalic; |
| 2161 | else |
| 2162 | charWidth = info -> ps.times; |
| 2163 | } |
| 2164 | charWidth *= size / 1000.0; |
| 2165 | textWidth += charWidth; |
| 2166 | } |
| 2167 | /* |
| 2168 | * The following has to be kept IN SYNC with charSizes (). |
| 2169 | */ |
| 2170 | for (_Graphics_widechar *character = & string [0]; character -> kar > U'\t'; character ++) { |
| 2171 | if ((character -> style & Graphics_ITALIC) != 0) { |
| 2172 | _Graphics_widechar *nextCharacter = character + 1; |
| 2173 | if (nextCharacter -> kar <= U'\t') { |
| 2174 | textWidth += POSTSCRIPT_SLANT_CORRECTION; |
| 2175 | } else if (((nextCharacter -> style & Graphics_ITALIC) == 0 && nextCharacter -> baseline >= character -> baseline) |
| 2176 | || (character -> baseline == 0 && nextCharacter -> baseline > 0)) |
| 2177 | { |
| 2178 | if (nextCharacter -> kar == U'.' || nextCharacter -> kar == U',') |
| 2179 | textWidth += 0.5 * POSTSCRIPT_SLANT_CORRECTION; |
| 2180 | else |
| 2181 | textWidth += POSTSCRIPT_SLANT_CORRECTION; |
| 2182 | } |
| 2183 | } |
| 2184 | } |
| 2185 | return textWidth; |
| 2186 | } |
| 2187 | |
| 2188 | double Graphics_textWidth_ps_mm (Graphics me, conststring32 txt, bool useSilipaPS) { |
| 2189 | if (! initBuffer (txt)) |
| 2190 | return 0.0; |
| 2191 | parseTextIntoCellsLinesRuns (me, txt, theWidechar); |
| 2192 | return psTextWidth (theWidechar, useSilipaPS) * (double) my fontSize * (25.4 / 72.0); |
| 2193 | } |
| 2194 | |
| 2195 | double Graphics_textWidth_ps (Graphics me, conststring32 txt, bool useSilipaPS) { |
| 2196 | return Graphics_dxMMtoWC (me, Graphics_textWidth_ps_mm (me, txt, useSilipaPS)); |
| 2197 | } |
| 2198 | |
| 2199 | #if quartz |
| 2200 | bool _GraphicsMac_tryToInitializeFonts () { |
| 2201 | static bool inited = false; |
| 2202 | if (inited) |
| 2203 | return true; |
| 2204 | NSArray *fontNames = [[NSFontManager sharedFontManager] availableFontFamilies]; |
| 2205 | hasTimes = [fontNames containsObject: @"Times"]; |
| 2206 | if (! hasTimes) |
| 2207 | hasTimes = [fontNames containsObject: @"Times New Roman"]; |
| 2208 | hasHelvetica = [fontNames containsObject: @"Helvetica"]; |
| 2209 | if (! hasHelvetica) |
| 2210 | hasHelvetica = [fontNames containsObject: @"Arial"]; |
| 2211 | hasCourier = [fontNames containsObject: @"Courier"]; |
| 2212 | if (! hasCourier) |
| 2213 | hasCourier = [fontNames containsObject: @"Courier New"]; |
| 2214 | hasPalatino = [fontNames containsObject: @"Palatino"]; |
| 2215 | if (! hasPalatino) |
| 2216 | hasPalatino = [fontNames containsObject: @"Book Antiqua"]; |
| 2217 | hasDoulos = [fontNames containsObject: @"Doulos SIL"]; |
| 2218 | hasCharis = [fontNames containsObject: @"Charis"]; // try Charis 7 |
| 2219 | if (hasCharis) |
| 2220 | hasCharis7 = true; |
| 2221 | else |
| 2222 | hasCharis = [fontNames containsObject: @"Charis SIL"]; // try Charis 6 |
| 2223 | hasIpaSerif = hasDoulos || hasCharis; |
| 2224 | inited = true; |
| 2225 | return true; |
| 2226 | } |
| 2227 | #endif |
| 2228 | |
| 2229 | #if cairo |
| 2230 | static const char *testFont (const char *fontName) { |
| 2231 | PangoFontDescription *pangoFontDescription = pango_font_description_from_string (fontName); |
| 2232 | PangoFont *pangoFont = pango_font_map_load_font (thePangoFontMap, thePangoContext, pangoFontDescription); |
| 2233 | PangoFontDescription *pangoFontDescription2 = pango_font_describe (pangoFont); |
| 2234 | return pango_font_description_get_family (pangoFontDescription2); |
| 2235 | } |
| 2236 | bool _GraphicsLin_tryToInitializeFonts () { |
| 2237 | static bool inited = false; |
| 2238 | if (inited) |
| 2239 | return true; |
| 2240 | thePangoFontMap = pango_cairo_font_map_get_default (); |
| 2241 | thePangoContext = pango_font_map_create_context (thePangoFontMap); |
| 2242 | #if 0 /* For debugging: list all fonts. */ |
| 2243 | PangoFontFamily **families; |
| 2244 | int numberOfFamilies; |
| 2245 | pango_font_map_list_families (thePangoFontMap, & families, & numberOfFamilies); |
| 2246 | for (int i = 0; i < numberOfFamilies; i ++) |
| 2247 | fprintf (Melder_stderr, "%d %s\n", i, pango_font_family_get_name (families [i])); |
| 2248 | g_free (families); |
| 2249 | #endif |
| 2250 | const char *trueName; |
| 2251 | |
| 2252 | trueName = testFont ("Times"); |
| 2253 | if (Melder_debug == 58) |
| 2254 | Melder_casual (U"True name of Times font: ", Melder_peek8to32_u (trueName)); |
| 2255 | hasTimes = !! strstr (trueName, "Times") || !! strstr (trueName, "Roman") || !! strstr (trueName, "Serif"); |
| 2256 | |
| 2257 | trueName = testFont ("Helvetica"); |
| 2258 | if (Melder_debug == 58) |
| 2259 | Melder_casual (U"True name of Helvetica font: ", Melder_peek8to32_u (trueName)); |
| 2260 | hasHelvetica = !! strstr (trueName, "Helvetica") || !! strstr (trueName, "Arial") || !! strstr (trueName, "Sans"); |
| 2261 | |
| 2262 | trueName = testFont ("Courier"); |
| 2263 | if (Melder_debug == 58) |
| 2264 | Melder_casual (U"True name of Courier font: ", Melder_peek8to32_u (trueName)); |
| 2265 | hasCourier = !! strstr (trueName, "Courier") || !! strstr (trueName, "Mono"); |
| 2266 | |
| 2267 | trueName = testFont ("Palatino"); |
| 2268 | if (Melder_debug == 58) |
| 2269 | Melder_casual (U"True name of Palatino font: ", Melder_peek8to32_u (trueName)); |
| 2270 | hasPalatino = !! strstr (trueName, "Palatino") || !! strstr (trueName, "Palladio") || !! strstr (trueName, "P052"); |
| 2271 | |
| 2272 | trueName = testFont ("Doulos SIL"); |
| 2273 | if (Melder_debug == 58) |
| 2274 | Melder_casual (U"True name of Doulos SIL font: ", Melder_peek8to32_u (trueName)); |
| 2275 | hasDoulos = !! strstr (trueName, "Doulos"); |
| 2276 | |
| 2277 | trueName = testFont ("Charis"); |
| 2278 | if (Melder_debug == 58) |
| 2279 | Melder_casual (U"True name of Charis font: ", Melder_peek8to32_u (trueName)); |
| 2280 | hasCharis = !! strstr (trueName, "Charis"); |
| 2281 | |
| 2282 | if (hasCharis) { |
| 2283 | hasCharis7 = true; |
| 2284 | } else { |
| 2285 | trueName = testFont ("Charis SIL"); |
| 2286 | if (Melder_debug == 58) |
| 2287 | Melder_casual (U"True name of Charis SIL font: ", Melder_peek8to32_u (trueName)); |
| 2288 | hasCharis = !! strstr (trueName, "Charis"); |
| 2289 | } |
| 2290 | |
| 2291 | hasIpaSerif = hasDoulos || hasCharis; |
| 2292 | testFont ("Symbol"); |
| 2293 | testFont ("Dingbats"); |
| 2294 | if (Melder_debug == 58) |
| 2295 | Melder_casual (U"times ", hasTimes, U" helvetica ", hasHelvetica, U" courier ", hasCourier, U" palatino ", hasPalatino, |
| 2296 | U" doulos ", hasDoulos, U" charis ", hasCharis, U" charis7 ", hasCharis7); |
| 2297 | inited = true; |
| 2298 | return true; |
| 2299 | } |
| 2300 | #endif |
| 2301 | |
| 2302 | void _GraphicsScreen_text_init (GraphicsScreen me) { // BUG: should be done as late as possible |
| 2303 | #if cairo |
| 2304 | (void) me; |
| 2305 | Melder_assert (_GraphicsLin_tryToInitializeFonts ()); |
| 2306 | #elif gdi |
| 2307 | int font, size, style; |
| 2308 | if (my printer || my metafile) |
| 2309 | for (font = (int) kGraphics_font::MIN; font <= kGraphics_font_DINGBATS; font ++) |
| 2310 | for (size = 0; size <= 4; size ++) |
| 2311 | for (style = 0; style <= Graphics_BOLD_ITALIC; style ++) |
| 2312 | if (fonts [(int) my resolutionNumber] [font] [size] [style]) { |
| 2313 | //DeleteObject (fonts [my resolutionNumber] [font] [size] [style]); |
| 2314 | //fonts [my resolutionNumber] [font] [size] [style] = 0; |
| 2315 | } |
| 2316 | #elif quartz |
| 2317 | (void) me; |
| 2318 | Melder_assert (_GraphicsMac_tryToInitializeFonts ()); // should have been handled when setting my useQuartz to true |
| 2319 | #endif |
| 2320 | } |
| 2321 | |
| 2322 | /* Output attributes. */ |
| 2323 | |
| 2324 | void Graphics_setTextAlignment (Graphics me, kGraphics_horizontalAlignment hor, int vert) { |
| 2325 | if ((int) hor != Graphics_NOCHANGE) |
| 2326 | my horizontalTextAlignment = (int) hor; |
| 2327 | if (vert != Graphics_NOCHANGE) |
| 2328 | my verticalTextAlignment = vert; |
| 2329 | if (my recording) { op (SET_TEXT_ALIGNMENT, 2); put (hor); put (vert); } |
| 2330 | } |
| 2331 | |
| 2332 | void Graphics_setFont (Graphics me, kGraphics_font font) { |
| 2333 | my font = font; |
| 2334 | if (my recording) { op (SET_FONT, 1); put (font); } |
| 2335 | } |
| 2336 | |
| 2337 | void Graphics_setFontSize (Graphics me, double size) { |
| 2338 | my fontSize = size; |
| 2339 | if (my recording) { op (SET_FONT_SIZE, 1); put (size); } |
| 2340 | } |
| 2341 | |
| 2342 | void Graphics_setFontStyle (Graphics me, int style) { |
| 2343 | my fontStyle = style; |
| 2344 | if (my recording) { op (SET_FONT_STYLE, 1); put (style); } |
| 2345 | } |
| 2346 | |
| 2347 | void Graphics_setItalic (Graphics me, bool onoff) { |
| 2348 | Graphics_setFontStyle (me, ( onoff ? my fontStyle | Graphics_ITALIC : my fontStyle & ~ Graphics_ITALIC )); |
| 2349 | } |
| 2350 | |
| 2351 | void Graphics_setBold (Graphics me, bool onoff) { |
| 2352 | Graphics_setFontStyle (me, ( onoff ? my fontStyle | Graphics_BOLD : my fontStyle & ~ Graphics_BOLD )); |
| 2353 | } |
| 2354 | |
| 2355 | void Graphics_setCode (Graphics me, bool onoff) { |
| 2356 | Graphics_setFontStyle (me, ( onoff ? my fontStyle | Graphics_CODE : my fontStyle & ~ Graphics_CODE )); |
| 2357 | } |
| 2358 | |
| 2359 | void Graphics_setTextRotation (Graphics me, double angle) { |
| 2360 | my textRotation = angle; |
| 2361 | if (my recording) { op (SET_TEXT_ROTATION, 1); put (angle); } |
| 2362 | } |
| 2363 | |
| 2364 | void Graphics_setWrapWidth (Graphics me, double wrapWidth) { |
| 2365 | my wrapWidth = wrapWidth; |
| 2366 | if (my recording) { op (SET_WRAP_WIDTH, 1); put (wrapWidth); } |
| 2367 | } |
| 2368 | |
| 2369 | void Graphics_setSecondIndent (Graphics me, double indent) { |
| 2370 | my secondIndent = indent; |
| 2371 | if (my recording) { op (SET_SECOND_INDENT, 1); put (indent); } |
| 2372 | } |
| 2373 | |
| 2374 | void Graphics_setPercentSignIsItalic (Graphics me, bool isItalic) { |
| 2375 | my percentSignIsItalic = isItalic; |
| 2376 | if (my recording) { op (SET_PERCENT_SIGN_IS_ITALIC, 1); put (isItalic); } |
| 2377 | } |
| 2378 | |
| 2379 | void Graphics_setNumberSignIsBold (Graphics me, bool isBold) { |
| 2380 | my numberSignIsBold = isBold; |
| 2381 | if (my recording) { op (SET_NUMBER_SIGN_IS_BOLD, 1); put (isBold); } |
| 2382 | } |
| 2383 | |
| 2384 | void Graphics_setCircumflexIsSuperscript (Graphics me, bool isSuperscript) { |
| 2385 | my circumflexIsSuperscript = isSuperscript; |
| 2386 | if (my recording) { op (SET_CIRCUMFLEX_IS_SUPERSCRIPT, 1); put (isSuperscript); } |
| 2387 | } |
| 2388 | |
| 2389 | void Graphics_setUnderscoreIsSubscript (Graphics me, bool isSubscript) { |
| 2390 | my underscoreIsSubscript = isSubscript; |
| 2391 | if (my recording) { op (SET_UNDERSCORE_IS_SUBSCRIPT, 1); put (isSubscript); } |
| 2392 | } |
| 2393 | |
| 2394 | void Graphics_setDollarSignIsCode (Graphics me, bool isCode) { |
| 2395 | my dollarSignIsCode = isCode; |
| 2396 | if (my recording) { op (SET_DOLLAR_SIGN_IS_CODE, 1); put (isCode); } |
| 2397 | } |
| 2398 | |
| 2399 | void Graphics_setBackquoteIsVerbatim (Graphics me, bool isVerbatim) { |
| 2400 | my backquoteIsVerbatim = isVerbatim; |
| 2401 | if (my recording) { op (SET_BACKQUOTE_IS_VERBATIM, 1); put (isVerbatim); } |
| 2402 | } |
| 2403 | |
| 2404 | void Graphics_setAtSignIsLink (Graphics me, bool isLink) { |
| 2405 | my atSignIsLink = isLink; |
| 2406 | if (my recording) { op (SET_AT_SIGN_IS_LINK, 1); put (isLink); } |
| 2407 | } |
| 2408 | |
| 2409 | /* Inquiries. */ |
| 2410 | |
| 2411 | enum kGraphics_font Graphics_inqFont (Graphics me) { return my font; } |
| 2412 | double Graphics_inqFontSize (Graphics me) { return my fontSize; } |
| 2413 | int Graphics_inqFontStyle (Graphics me) { return my fontStyle; } |
| 2414 | |
| 2415 | /* End of file Graphics_text.cpp */ |