6 # SpeakGoodChinese: sgc2.praat is the master GUI of SpeakGoodChinese
7 # It is written in Praat script for the Demo window
9 # Copyright (C) 2007-2010 R.J.J.H. van Son
10 # The SpeakGoodChinese team are:
11 # Guangqin Chen, Zhonyan Chen, Stefan de Koning, Eveline van Hagen,
12 # Rob van Son, Dennis Vierkant, David Weenink
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
29 # Define variable that will be reset in Initialise*.praat
30 # These are simply "useful" defaults
31 localTableDir$ = "Data"
32 buttonsTableName$ = "Buttons"
33 configTableName$ = "Config"
37 # Parameters for isolating recorded speech from noise
38 # Should be mostly left alone unless you are using ultra clean
39 # or very noisy recordings
48 defaultFont$ = "Helvetica"
54 # Load supporting scripts
55 # Set up system and load preferences
56 include InitialiseSGC2.praat
57 # Include the main page buttons and procedures
59 # Include the configuration page buttons and procedures
61 # Load tables in script format
62 include CreateTables.praat
63 include CreateWordlists.praat
65 # Start instruction loop
66 while demoWaitForInput()
74 call buttonClicked 'buttons$' '.clickX' '.clickY'
75 .label$ = buttonClicked.label$
76 elsif demoKeyPressed()
77 .pressed$ = demoKey$()
78 call keyPressed 'buttons$' '.pressed$'
79 .label$ = keyPressed.label$
85 call Draw_button 'buttons$' '.label$' 1
86 call process_label '.label$' '.clickX' '.clickY' '.pressed$'
88 call Draw_button 'buttons$' '.label$' 0
95 ########################################################
97 # Definitions of procedures
99 ########################################################
102 procedure process_label .label$ .clickX .clickY .pressed$
103 if .label$ <> "" and not startsWith(.label$,"!")
104 .label$ = replace$(.label$, "_", " ", 0)
105 call process'buttons$''.label$' '.clickX' '.clickY' '.pressed$'
110 procedure init_buttons
111 call Draw_all_buttons 'buttons$'
115 procedure Draw_all_buttons .table$
116 select Table '.table$'
117 .numRows = Get number of rows
120 .label$ = Get value... '.row' Label
121 if not startsWith(.label$, "!")
122 call Draw_button '.table$' '.label$' 0
127 # Draw a button from a predefined button table
128 procedure Draw_button .table$ .label$ .push
129 # Scale rounding of rounded rectangles
131 .mm = demo Horizontal wc to mm... '.wc'
132 # Allow to overide ! skip directive
134 if startsWith(.label$, "+")
135 .label$ = right$(.label$, length(.label$)-1)
139 select Table '.table$'
140 .row = Search column... Label '.label$'
142 exit Button Table '.table$' does not have a row with label '.label$'
145 # Perspective shift sizes
149 # Set drawing parameters
150 .topBackGroundColorUp$ = "{0.93,0.93,0.93}"
151 .topLineColorUp$ = "Black"
152 .topLineWidthUp = 1.5
153 .topBackGroundColorDown$ = "{0.89,0.89,0.89}"
154 .topLineColorDown$ = "Grey"
155 .topLineWidthDown = 1.5
156 .flankBackGroundColorUp$ = "{0.6,0.6,0.6}"
157 .flankLineColorUp$ = "{0.2,0.2,0.2}"
158 .flankLineWidthUp = 1.5
159 .flankBackGroundColorDown$ = "{0.75,0.75,0.75}"
160 .flankLineColorDown$ = .flankLineColorUp$
161 .flankLineWidthDown = 1.5
162 .buttonFontSize = defaultFontSize
165 .leftX = Get value... '.row' LeftX
166 .rightX = Get value... '.row' RightX
167 .lowY = Get value... '.row' LowY
168 .highY = Get value... '.row' HighY
169 .buttonText$ = Get value... '.row' Text
170 .buttonColor$ = Get value... '.row' Color
171 .buttonDraw$ = Get value... '.row' Draw
172 .buttonKey$ = Get value... '.row' Key
174 goto NOBUTTON startsWith(.label$, "!") and not .forceDraw
176 # Replace button text with ALERT
178 .buttonText$ = alertText$
181 # Adapt size of button to length of text
182 .maxWidth = (.rightX - .leftX) - 2
183 call adjustFontSizeOnWidth '.buttonFontSize' '.maxWidth' '.buttonText$'
184 .buttonFontSize = adjustFontSizeOnWidth.newFontSize
185 if adjustFontSizeOnWidth.diff > 0
186 .rightX += adjustFontSizeOnWidth.diff/2
187 .leftX -= adjustFontSizeOnWidth.diff/2
189 call set_font_size '.buttonFontSize'
191 # Adapt size of button to length of text
192 .maxHeight = (.highY - .lowY) - 1
193 call adjustFontSizeOnHeight '.buttonFontSize' '.maxHeight'
194 if adjustFontSizeOnHeight.diff > 0
195 .lowY -= adjustFontSizeOnHeight.diff/2
196 .highY += adjustFontSizeOnHeight.diff/2
198 .buttonFontSize = adjustFontSizeOnHeight.newFontSize
200 # Reset and erase button area
202 demo Line width... 'defaultLineWidth'
203 .shiftLeftX = .leftX - .shiftX
204 .shiftRightX = .rightX
205 .shiftLowY = .lowY - .shiftY
207 demo Paint rectangle... White .shiftLeftX .shiftRightX .shiftLowY .shiftHighY
209 # Give some depth to button: Draw flank outline
211 demo Paint rounded rectangle... '.flankBackGroundColorUp$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
212 demo Colour... '.flankLineColorUp$'
213 demo Line width... '.flankLineWidthUp'
215 demo Paint rounded rectangle... '.flankBackGroundColorDown$' .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
216 demo Colour... '.flankLineColorDown$'
217 demo Line width... '.flankLineWidthDown'
219 demo Draw rounded rectangle... .shiftLeftX .shiftRightX .shiftLowY .shiftHighY '.mm'
221 # Button Down will shift the top perspective
223 # Draw the button top
225 demo Paint rounded rectangle... '.topBackGroundColorUp$' '.leftX' '.rightX' '.lowY' '.highY' '.mm'
226 demo Colour... '.topLineColorUp$'
227 demo Line width... '.topLineWidthUp'
231 .rightX -= .shiftDown
235 demo Paint rounded rectangle... '.topBackGroundColorDown$' .leftX .rightX .lowY .highY '.mm'
236 demo Colour... '.topLineColorDown$'
237 demo Line width... '.topLineWidthDown'
239 demo Draw rounded rectangle... '.leftX' '.rightX' '.lowY' '.highY' '.mm'
241 # The button text and symbol
242 .centerX = (.leftX + .rightX)/2
243 .centerY = .lowY + 0.6*(.highY-.lowY)
244 .radius = (.highY - .lowY )/4
245 .newText$ = replace_regex$(.buttonText$, "['.buttonKey$']", "#%&", 1)
247 .newText$ = .buttonText$
251 if .buttonColor$ = "Red"
252 .buttonColor$ = "Pink"
253 elsif .buttonColor$ = "Blue"
254 .buttonColor$ = "{0.5,0.5,1}"
256 .buttonColor$ = "Grey"
259 .buttonColor$ = "Maroon"
264 call '.buttonDraw$' '.buttonColor$' '.centerX' '.centerY' '.radius'
265 call set_font_size '.buttonFontSize'
266 demo Colour... '.buttonColor$'
267 demo Text... '.centerX' Centre '.lowY' Bottom '.newText$'
271 call set_font_size 'defaultFontSize'
273 demo Line width... 'defaultLineWidth'
278 procedure set_window_title .table$ .addedText$
279 select Table '.table$'
280 .row = Search column... Label !WindowTitle
282 exit Button Table '.table$' does not have a row with label !WindowTitle
284 .windowText$ = Get value... '.row' Text
286 demoWindowTitle(.windowText$+ .addedText$)
289 # Handle language setting
290 procedure processLanguageCodes .table$ .label$
292 call Draw_button 'config$' Lang_'language$' 0
293 call Draw_button 'config$' '.label$' 2
294 # Someone might have to use more than 2 chars for the language code
295 .numChars = length(.label$) - length("Lang_")
296 .lang$ = right$(.label$, .numChars)
298 call set_language '.lang$'
302 procedure set_language .lang$
306 select Table 'buttons$'
311 select Table 'config$'
319 # Load buttons tables
320 call loadTable 'buttonsTableName$'
321 buttons$ = selected$("Table")
322 Append column... Text
324 Append column... Helptext
325 .numLabels = Get number of rows
326 call loadTable 'buttonsTableName$'_'language$'
327 .buttonsLang$ = selected$("Table")
328 for .row to .numLabels
329 select Table 'buttons$'
330 .label$ = Get value... '.row' Label
331 call findLabel '.buttonsLang$' '.label$'
333 select Table '.buttonsLang$'
334 .valueText$ = Get value... 'findLabel.row' Text
335 .valueKey$ = Get value... 'findLabel.row' Key
336 .valueHelp$ = Get value... 'findLabel.row' Helptext
337 select Table 'buttons$'
338 Set string value... '.row' Text '.valueText$'
339 Set string value... '.row' Key '.valueKey$'
340 Set string value... '.row' Helptext '.valueHelp$'
341 elsif index(.label$, "_")
342 # Load alternative language table
343 .startChar = rindex(.label$, "_")
344 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
345 call loadTable 'buttonsTableName$'_'.otherLanguage$'
346 .otherbuttonsLang$ = selected$("Table")
347 call findLabel '.otherbuttonsLang$' '.label$'
349 select Table '.buttonsLang$'
350 .valueText$ = Get value... 'findLabel.row' Text
351 .valueKey$ = Get value... 'findLabel.row' Key
352 .valueHelp$ = Get value... 'findLabel.row' Helptext
353 select Table 'buttons$'
354 Set string value... '.row' Text '.valueText$'
355 Set string value... '.row' Key '.valueKey$'
356 Set string value... '.row' Helptext '.valueHelp$'
358 exit Cannot find Label: '.otherbuttonsLang$' '.label$'
360 select Table '.otherbuttonsLang$'
363 exit Cannot find Label: '.buttonsLang$' '.label$'
366 select Table '.buttonsLang$'
369 # Load configuration table
370 call loadTable 'configTableName$'
371 config$ = selected$("Table")
372 Append column... Text
374 Append column... Helptext
375 .numLabels = Get number of rows
376 call loadTable 'configTableName$'_'language$'
377 .configLang$ = selected$("Table")
378 for .row to .numLabels
379 select Table 'config$'
380 .label$ = Get value... '.row' Label
381 call findLabel '.configLang$' '.label$'
383 select Table '.configLang$'
384 .valueText$ = Get value... 'findLabel.row' Text
385 .valueKey$ = Get value... 'findLabel.row' Key
386 .valueHelp$ = Get value... 'findLabel.row' Helptext
387 select Table 'config$'
388 Set string value... '.row' Text '.valueText$'
389 Set string value... '.row' Key '.valueKey$'
390 Set string value... '.row' Helptext '.valueHelp$'
391 elsif index(.label$, "_")
392 .startChar = rindex(.label$, "_")
393 .otherLanguage$ = right$(.label$, length(.label$) - .startChar)
394 call loadTable 'configTableName$'_'.otherLanguage$'
395 .otherconfigLang$ = selected$("Table")
396 call findLabel '.otherconfigLang$' '.label$'
398 select Table '.otherconfigLang$'
399 .valueText$ = Get value... 'findLabel.row' Text
400 .valueKey$ = Get value... 'findLabel.row' Key
401 .valueHelp$ = Get value... 'findLabel.row' Helptext
402 select Table 'config$'
403 Set string value... '.row' Text '.valueText$'
404 Set string value... '.row' Key '.valueKey$'
405 Set string value... '.row' Helptext '.valueHelp$'
407 exit Cannot find Label: '.otherconfigLang$' '.label$'
409 select Table '.otherconfigLang$'
412 exit Cannot find Label: '.configLang$' '.label$'
415 select Table '.configLang$'
418 # Make language change visible
420 call Draw_config_page
425 ###############################################################
427 # Button Drawing Routines
429 ###############################################################
431 # A stub for buttons that do not have a drawing routine (yet)
432 procedure DrawNull .color$ .x .y .size
435 procedure DrawHelp .color$ .x .y .size
436 .currentFontSize = 24
439 call adjustFontSizeOnHeight '.currentFontSize' '.maxHeight'
440 .currentFontSize = adjustFontSizeOnHeight.currentFontSize
441 call set_font_size '.currentFontSize'
442 demo Colour... '.color$'
443 demo Text... '.x' Centre '.y' Bottom ?
444 call set_font_size 'defaultFontSize'
447 ###############################################################
449 # Button Processing Routines
451 ###############################################################
453 # Search row in table on label
454 procedure findKey .table$ .label$
456 select Table '.table$'
457 .to$ = selected$("Table")
459 .numRows = Get number of rows
461 .currentKey$ = '.to$'$[.i, "Key"]
462 if .label$ = .currentKey$
468 if .row <= 0 and index(.label$, "_") <= 0
469 printline "'.label$'" is not a key in '.table$'
473 procedure findLabel .table$ .label$
475 select Table '.table$'
476 .to$ = selected$("Table")
478 .numRows = Get number of rows
480 .currentKey$ = '.to$'$[.i, "Label"]
481 if .label$ = .currentKey$
487 if .row <= 0 and index(.label$, "_") <= 0
488 exit "'.label$'" is not a key in '.table$'
493 procedure buttonClicked table$ .x .y
495 select Table 'table$'
496 .bo$ = selected$("Table")
498 .numRows = Get number of rows
501 .leftX = '.bo$'[.i, "LeftX"]
502 .rightX = '.bo$'[.i, "RightX"]
503 .lowY = '.bo$'[.i, "LowY"]
504 .highY = '.bo$'[.i, "HighY"]
505 if .x > .leftX and .x < .rightX and .y > .lowY and .y < .highY
506 .label$ = '.bo$'$[.i, "Label"]
512 procedure keyPressed table$ .pressed$
518 .lowerPressed$ = replace_regex$(.pressed$, ".", "\L&", 0)
519 .upperPressed$ = replace_regex$(.pressed$, ".", "\U&", 0)
520 select Table 'table$'
521 .bo$ = selected$("Table")
523 .numRows = Get number of rows
526 .key$ = '.bo$'$[.i, "Key"]
527 if index(.key$, .lowerPressed$) or index(.key$, .upperPressed$)
528 .label$ = '.bo$'$[.i, "Label"]
535 procedure play_sound .sound$
537 select Sound '.sound$'
542 procedure record_sound
543 if recordedSound$ != ""
544 select Sound 'recordedSound$'
549 demo Paint circle... Red 5 95 2
551 nowarn Record Sound (fixed time)... 'soundInput$' 0.99 1 44100 4
552 demo Paint circle... White 5 95 2.5
553 call wipeArea 'wipeFeedbackArea$'
554 # Feedback on recording level
555 .extremum = Get absolute extremum... 0 0 None
556 .radius = 2 * .extremum
562 elsif .extremum >= 0.49
565 .green = .extremum / 0.5
567 .color$ = "{'.red','.green','.blue'}"
568 demo Colour... '.color$'
570 demo Draw circle... 5 95 '.radius'
574 demo Line width... 'defaultLineWidth'
578 Rename... Pronunciation
579 recordedSound$ = selected$("Sound")
582 select Sound 'recordedSound$'
584 # Cut out real sound from silences/noise
585 call sound_detection 'recordedSound$' 'soundMargin'
586 select Sound 'recordedSound$'
590 # Select real sound from recording
591 # Uses some global variable
592 procedure sound_detection .sound$ .margin
593 select Sound '.sound$'
594 .soundlength = Get total duration
595 .internalSilence = 2*.margin
597 # Silence and remove noise, DANGEROUS
598 To TextGrid (silences)... 'minimumPitch' 0 'noiseThresshold' '.internalSilence' 0.1 silent sounding
599 Rename... Input'.sound$'
601 select TextGrid Input'.sound$'
602 .numberofIntervals = Get number of intervals... 1
604 # Remove buzzing and other obnoxious sounds (if switched on)
605 for .i from 1 to .numberofIntervals
606 select TextGrid Input'.sound$'
607 .value$ = Get label of interval... 1 '.i'
608 .begintime = Get starting point... 1 '.i'
609 .endtime = Get end point... 1 '.i'
612 if .value$ = "silent"
613 select Sound '.sound$'
614 Set part to zero... '.begintime' '.endtime' at nearest zero crossing
618 # Select target sound
619 .maximumIntensity = -1
621 for i from 1 to .numberofIntervals
622 select TextGrid Input'.sound$'
624 .value$ = Get label of interval... 1 'i'
625 .begintime = Get starting point... 1 'i'
626 .endtime = Get end point... 1 'i'
628 if .value$ != "silent"
629 if .begintime > .margin
630 .begintime -= .margin
634 if .endtime + .margin < .soundlength
637 .endtime = .soundlength
640 select Sound '.sound$'
641 Extract part... '.begintime' '.endtime' Rectangular 1.0 no
642 Rename... Tmp'.sound$'
644 .newIntensity = Get intensity (dB)
645 if .newIntensity > .maximumIntensity
646 if .maximumIntensity > 0
647 select Sound New'.sound$'
650 select Sound Tmp'.sound$'
651 Rename... New'.sound$'
652 .maximumIntensity = .newIntensity
654 select Sound Tmp'.sound$'
660 if .maximumIntensity > minimumIntensity
661 select Sound '.sound$'
663 select Sound New'.sound$'
665 elsif .maximumIntensity > -1
666 select Sound New'.sound$'
669 select TextGrid Input'.sound$'
673 procedure end_program
674 call write_preferences ""
681 ######################################################
685 ######################################################
686 procedure config_page
688 demoWindowTitle("Speak Good Chinese: Change settings")
690 call Draw_config_page
695 while (.label$ <> "Return") and demoWaitForInput()
700 call buttonClicked 'config$' '.clickX' '.clickY'
701 .label$ = buttonClicked.label$
702 elsif demoKeyPressed()
703 .pressed$ = demoKey$()
704 call keyPressed 'config$' '.pressed$'
705 .label$ = keyPressed.label$
710 # Handle push button in process_config
711 call process_config '.label$' '.clickX' '.clickY' '.pressed$'
714 if .label$ = "Return"
724 procedure Draw_config_page
726 call Draw_all_buttons 'config$'
727 call set_window_title 'config$'
728 # Set correct buttons (alert)
729 call setConfigButtons
733 procedure process_config .label$ .clickX .clickY .pressed$
734 if .label$ = "!Logging"
735 logPerformance = not logPerformance
736 .displayButton = logPerformance
737 call Draw_button 'config$' +'.label$' '.displayButton'
741 elsif .label$ <> "" and not startsWith(.label$,"!")
742 .label$ = replace$(.label$, "_", " ", 0)
743 call process'config$''.label$' '.clickX' '.clickY' '.pressed$'
747 ###############################################################
749 # Presenting help texts
751 ###############################################################
754 procedure help_loop .table$ .redrawProc$
756 call write_help_title '.table$'
759 call Draw_button '.table$' Help 2
761 while (.label$ <> "Help") and demoWaitForInput()
766 call buttonClicked '.table$' '.clickX' '.clickY'
767 .label$ = buttonClicked.label$
768 elsif demoKeyPressed()
769 .pressed$ = demoKey$()
770 call keyPressed '.table$' '.pressed$'
771 .label$ = keyPressed.label$
774 if .label$ != "" and .label$ <> "Help"
775 # Handle push button in process_config
776 call write_help_text '.table$' '.label$' '.redrawProc$'
782 call Draw_button '.table$' Help 0
788 procedure write_help_text .table$ .label$ .redrawProc$
789 call findLabel '.table$' '.label$'
791 select Table '.table$'
794 call findLabel '.table$' Help
796 select Table '.table$'
798 .helpText$ = Get value... '.row' Helptext
799 .helpKey$ = Get value... '.row' Key
800 .helpKey$ = replace$(.helpKey$, "\", "", 0)
801 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
802 .helpText$ = .helpText$+" ("+.helpKey$+")"
804 .leftX = Get value... '.row' LeftX
805 .rightX = Get value... '.row' RightX
806 .lowY = Get value... '.row' LowY
807 .highY = Get value... '.row' HighY
810 .currentHelpFontSize = defaultFontSize
811 call set_font_size '.currentHelpFontSize'
812 .helpTextSize = demo Text width (wc)... '.helpText$'
816 .htXright = .htXleft + .helpTextSize + 5
820 .htXleft = .htXright - .helpTextSize - 5
825 .htYhigh = .htYlow + 7
830 .htYlow = .htYhigh - 7
835 # Adapt font size to horizontal dimensions
837 call adjustFontSizeOnWidth '.currentHelpFontSize' '.maxWidth' '.helpText$'
838 .currentHelpFontSize = adjustFontSizeOnWidth.newFontSize
839 if .htXleft < 0 or .htXright > 100
841 .htXright = .htXleft + adjustFontSizeOnWidth.textWidth + 5
843 call set_font_size '.currentHelpFontSize'
845 # Adapt vertical dimensions to font height
846 call points_to_wc '.currentHelpFontSize'
847 .lineHeight = points_to_wc.wc
848 if .lineHeight > .htYhigh - .htYlow - 4
849 .htYhigh = .htYlow + .lineHeight + 4
852 # Determine arrow endpoints
854 if abs(.htXleft - .xstart) > abs(.htXright - .xstart)
857 if abs((.htXleft+.htXright)/2 - .xstart) < min(abs(.htXright - .xstart),abs(.htXleft - .xstart))
858 .xend = (.htXleft+.htXright)/2
861 .xtext = .htXleft + 2
865 .mm2wc = demo Horizontal mm to wc... 1
866 .lineWidth = 2/.mm2wc
867 demo Line width... '.lineWidth'
868 demo Arrow size... '.lineWidth'
869 demo Colour... {0.5,0.5,1}
870 demo Paint rectangle... {0.9,0.9,1} '.htXleft' '.htXright' '.htYlow' '.htYhigh'
871 demo Draw rectangle... '.htXleft' '.htXright' '.htYlow' '.htYhigh'
872 demo Draw arrow... '.xstart' '.ystart' '.xend' '.yend'
873 demo Line width... 'defaultLineWidth'
876 demo Text... '.xtext' Left '.ytext' Bottom '.helpText$'
878 call set_font_size 'defaultFontSize'
880 # Now wait for some input to continue
886 call Draw_button '.table$' Help 2
887 call write_help_title '.table$'
890 procedure write_help_title .table$
891 # Set help text title
893 call findLabel '.table$' Help
895 select Table '.table$'
896 .helpTitle$ = Get value... '.row' Helptext
897 .helpKey$ = Get value... '.row' Key
898 .helpKey$ = replace$(.helpKey$, "\", "", 0)
899 .helpKey$ = replace$(.helpKey$, "_", "\_ ", 0)
900 .helpTitle$ = .helpTitle$+" ("+.helpKey$+")"
903 .helpTitleFontSize = 14
904 # Adapt size of button to length of text
906 call adjustFontSizeOnWidth '.helpTitleFontSize' '.maxWidth' '.helpTitle$'
907 .helpTitleFontSize = adjustFontSizeOnWidth.newFontSize
908 call set_font_size '.helpTitleFontSize'
911 demo Select inner viewport... 0 100 0 100
912 demo Axes... 0 100 0 100
913 demo Text... 50 Centre '.helpTop' Top '.helpTitle$'
914 call set_font_size 'defaultFontSize'
918 ###############################################################
920 # Miscelaneous procedures
922 ###############################################################
923 procedure points_to_wc .points
924 .mm = .points * 0.3527777778
925 .wc = demo Vertical mm to wc... '.mm'
928 procedure reset_viewport
929 .low = viewportMargin
930 .high = 100 - viewportMargin
931 demo Select inner viewport... '.low' '.high' '.low' '.high'
932 demo Axes... 0 100 0 100
935 procedure set_font_size .fontSize
937 demo Font size... '.fontSize'
941 procedure wipeArea .areaCommand$
946 procedure adjustFontSizeOnWidth .currentFontSize .maxWidth .text$
948 call set_font_size '.currentFontSize'
949 .textWidth = demo Text width (wc)... '.text$'
950 while .textWidth > .maxWidth and .currentFontSize > 4
951 .currentFontSize -= 0.5
952 call set_font_size '.currentFontSize'
953 .textWidth = demo Text width (wc)... '.text$'
955 .diff = .textWidth - .maxWidth
956 .newFontSize = .currentFontSize
959 procedure adjustFontSizeOnHeight .currentFontSize .maxHeight
960 call points_to_wc '.currentFontSize'
961 .lineHeight = points_to_wc.wc
962 while .lineHeight > .maxHeight and .currentFontSize > 4
963 .currentFontSize -= 0.5
964 call points_to_wc '.currentFontSize'
965 .lineHeight = points_to_wc.wc
967 .diff = .lineHeight - .maxHeight
968 .newFontSize = .currentFontSize
971 # Load a table with button info etc.
972 # Load local tables if present. Else load
973 # build-in scripted tables
974 procedure loadTable .tableName$
975 # Search for the table in local, preference, and global directories
976 if fileReadable("'localTableDir$'/'.tableName$'.Table")
977 Read from file... 'localTableDir$'/'.tableName$'.Table
978 elsif fileReadable("'preferencesTableDir$'/'.tableName$'.Table")
979 Read from file... 'preferencesTableDir$'/'.tableName$'.Table
980 elsif fileReadable("'globaltablelists$'/'.tableName$'.Table")
981 Read from file... 'globaltablelists$'/'.tableName$'.Table
982 # Load them from script
984 call Create'.tableName$'
988 # Create a pop-up window with text from a Text Table
989 procedure write_text_table .table$
996 # Get table with text and longest line
997 call loadTable '.table$'
998 .instructionText = selected()
999 .numLines = Get number of rows
1000 .instructionFontSize = 14
1001 .referenceText$ = ""
1007 select '.instructionText'
1008 .currentText$ = Get value... '.l' text
1009 .font$ = Get value... '.l' font
1010 .fontSize = Get value... '.l' size
1011 call set_font_size '.fontSize'
1012 .textWidth = demo Text width (wc)... '.currentText$'
1013 if .fontSize > .maxFontSize
1014 .maxFontSize = .fontSize
1016 if .textWidth > .maxWidth
1017 .maxWidth = .textWidth
1018 .instructionFontSize = .fontSize
1022 select '.instructionText'
1023 .referenceText$ = Get value... '.maxLine' text
1024 .maxLineFont$ = Get value... '.maxLine' font
1025 .instructionFontSize = Get value... '.maxLine' size
1026 call set_font_size '.maxFontSize'
1028 # Adapt size of button to length of text
1029 .maxWidth = (.xright - .xleft) - 4
1030 .origFontSize = .instructionFontSize
1031 call adjustFontSizeOnWidth '.instructionFontSize' '.maxWidth' '.referenceText$'
1032 call adjustFontSizeOnHeight '.maxFontSize' '.lineHeight'
1033 .instructionFontSize = min(adjustFontSizeOnWidth.newFontSize, adjustFontSizeOnHeight.newFontSize)
1034 if adjustFontSizeOnWidth.diff > 0
1035 .xright += adjustFontSizeOnWidth.diff/4
1036 .xleft -= 3*adjustFontSizeOnWidth.diff/4
1038 call set_font_size '.instructionFontSize'
1039 .fontSizeFactor = .instructionFontSize / .origFontSize
1041 .numRows = Get number of rows
1042 # Calculate length from number of lines.
1044 .midY = .yhigh - (.yhigh - .ylow)/2
1045 .yhigh = .midY + (.numRows+1) * .dy / 2
1046 .ylow = .yhigh - (.numRows+1) * .dy
1047 .textleft = .xleft + 2
1049 demo Line width... 8
1050 demo Colour... {0.5,0.5,1}
1051 demo Paint rectangle... {0.9,0.9,1} '.xleft' '.xright' '.ylow' '.yhigh'
1052 demo Draw rectangle... '.xleft' '.xright' '.ylow' '.yhigh'
1053 demo Line width... 'defaultLineWidth'
1055 .ytext = .yhigh - 2 - .dy
1057 select '.instructionText'
1058 .font$ = Get value... '.i' font
1059 .fontSize = Get value... '.i' size
1060 .font$ = extractWord$(.font$, "")
1062 .fontSize = floor(.fontSize*.fontSizeFactor)
1066 .line$ = Get value... '.i' text
1068 demo Text special... '.textleft' Left '.ytext' Bottom '.font$' '.fontSize' 0 '.line$'
1072 call set_font_size 'defaultFontSize'
1074 select '.instructionText'