home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / Epoc / Palmtime / files / FrotzS5_src.ZIP / S5text.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-11  |  7.3 KB  |  352 lines

  1. /*
  2.  * file "S5text.c"
  3.  *
  4.  *
  5.  */
  6.  
  7. #include "frotz.h"
  8. #include "S5frotz.h"
  9. #include "S5api.h"
  10.  
  11.  
  12. /*
  13.  * os_font_data
  14.  *
  15.  * Return true if the given font is available. The font can be
  16.  *
  17.  *    TEXT_FONT
  18.  *    PICTURE_FONT
  19.  *    GRAPHICS_FONT
  20.  *    FIXED_WIDTH_FONT
  21.  *
  22.  * The font size should be stored in "height" and "width". If the given
  23.  * font is unavailable then these values must _not_ be changed.
  24.  *
  25.  */
  26.  
  27. short os_font_data (struct sg *g, short font, short *height, short *width)
  28. {
  29.  
  30.     /* All fonts of this interface have the same size */
  31.  
  32.     *height = g->h_font_height;
  33.     *width = g->h_font_width;
  34.  
  35.     /* Not every font is available in every mode */
  36.  
  37.     if (font == TEXT_FONT)
  38.     return TRUE;
  39.     if (font == FIXED_WIDTH_FONT)
  40.     return TRUE;
  41.  
  42.     /* Unavailable font */
  43.  
  44.     return FALSE;
  45.  
  46. }/* os_font_data */
  47.  
  48. /*
  49.  * switch_scrn_attr
  50.  *
  51.  * Parts of the screen are usually erased to background colour.  However,
  52.  * for deleting text in the input routine it can be useful to erase to
  53.  * the current text background colour.  The two colours can be different,
  54.  * for example when the reverse text style is used.  This helper function
  55.  * toggles between the two possible behaviours.
  56.  *
  57.  */
  58.  
  59. void switch_scrn_attr (struct sg *g, short flag)
  60. {
  61.     byte scrn_bg;
  62.     byte scrn_fg;
  63.  
  64.     if (flag) {
  65.     scrn_bg = g->text_bg;
  66.     scrn_fg = g->text_fg;
  67.     } else {
  68.     scrn_bg = (unsigned char)g->bg;
  69.     scrn_fg = (unsigned char)g->fg;
  70.     }
  71.     
  72.     g->scrn_attr = scrn_bg;
  73. }/* switch_scrn_attr */
  74.  
  75. /*
  76.  * adjust_style
  77.  *
  78.  * Set the current colours. This combines the current colour selection
  79.  * and the current text style.
  80.  *
  81.  */
  82.  
  83. void adjust_style (struct sg *g)
  84. {
  85.     short attr;
  86.  
  87.     attr =  (g->current_style & REVERSE_STYLE ? 1 : 0);
  88.     attr |= (g->current_style & EMPHASIS_STYLE ? 2 : 0);
  89.     attr |= (g->current_style & BOLDFACE_STYLE ? 4 : 0);
  90.     SrvTextAttr(g,attr);
  91.     
  92.     /* Set the screen attribute for scrolling and erasing */
  93.  
  94.     switch_scrn_attr (g,FALSE);
  95.  
  96. }/* adjust_style */
  97.  
  98.  
  99. /*
  100.  * os_set_text_style
  101.  *
  102.  * Set the current text style. Following flags can be set:
  103.  *
  104.  *     REVERSE_STYLE
  105.  *     BOLDFACE_STYLE
  106.  *     EMPHASIS_STYLE (aka underline aka italics)
  107.  *     FIXED_WIDTH_STYLE
  108.  *
  109.  */
  110.  
  111. void os_set_text_style (struct sg *g, short new_style)
  112. {
  113.  
  114.     g->current_style = new_style;
  115.  
  116.     /* Apply changes */
  117.  
  118.     adjust_style (g);
  119.  
  120. }/* os_set_text_style */
  121.  
  122. /*
  123.  * os_set_font
  124.  *
  125.  * Set the font for text output. The interpreter takes care not to
  126.  * choose fonts which aren't supported by the interface.
  127.  *
  128.  */
  129.  
  130. void os_set_font (struct sg *g, short new_font)
  131. {
  132.  
  133.     g->current_font = new_font;
  134.  
  135. }/* os_set_font */
  136.  
  137. /*
  138.  * os_display_char
  139.  *
  140.  * Display a character of the current font using the current colours and
  141.  * text style. The cursor moves to the next position. Printable codes are
  142.  * all ASCII values from 32 to 126, ISO Latin-1 characters from 160 to
  143.  * 255, ZC_GAP (gap between two sentences) and ZC_INDENT (paragraph
  144.  * indentation). The screen should not be scrolled after printing to the
  145.  * bottom right corner.
  146.  *
  147.  */
  148.  
  149. void os_display_char (struct sg *g, zchar c)
  150. {
  151.     /* Handle special indentations */
  152.  
  153.     if (c == ZC_INDENT)
  154.     { 
  155.         SrvPutChar(g,g->cursor_x++, g->cursor_y, ' ');
  156.         SrvPutChar(g,g->cursor_x++, g->cursor_y, ' ');
  157.         SrvPutChar(g,g->cursor_x++, g->cursor_y, ' ');
  158.         return;
  159.     }
  160.     if (c == ZC_GAP)
  161.     { 
  162.         SrvPutChar(g,g->cursor_x++, g->cursor_y, ' ');
  163.         SrvPutChar(g,g->cursor_x++, g->cursor_y, ' ');
  164.         return;
  165.     }
  166.  
  167.     // Translate FROTZ bordering fonts from funny EPOC equivalents
  168.     // to nearest looking.
  169.     if (c > 170) {
  170.         if (c == 196) c=151;
  171.         else if (c == 179) c=124;
  172.         else if (c == 191 || c == 192 || c == 217 || c == 218) c=32;
  173.     }
  174.  
  175.     /* Display character */
  176.     SrvPutChar(g,g->cursor_x++, g->cursor_y, c);
  177.     /* Move cursor to next position */
  178.  
  179. }/* os_display_char */
  180.  
  181. /*
  182.  * os_display_string
  183.  *
  184.  * Pass a string of characters to os_display_char.
  185.  *
  186.  */
  187.  
  188. void os_display_string (struct sg *g, const zchar *s)
  189. {
  190.  
  191.     zchar c;
  192.     const zchar *s2 = s;
  193.     short i, contains_code = 0;
  194.  
  195.     i = 0;
  196.     while (*s2 != 0) 
  197.     {
  198.         if (*s2 == ZC_NEW_FONT || *s2 == ZC_NEW_STYLE || *s2 == ZC_INDENT || *s2 == ZC_GAP )
  199.         {
  200.             contains_code = 1;
  201.             break;
  202.         }
  203.         i++;
  204.         s2++;
  205.     }
  206.  
  207.     // If string contains any formatting codes or is only 1 char wide
  208.     // then we use display each char code
  209.     if (contains_code == 1 || i <= 1)
  210.     {
  211.         while ((c = *s++) != 0)
  212.         {
  213.             if (c == ZC_NEW_FONT || c == ZC_NEW_STYLE)
  214.             {
  215.  
  216.                 short arg = *s++;
  217.  
  218.                 if (c == ZC_NEW_FONT)
  219.                     os_set_font (g,arg);
  220.                 if (c == ZC_NEW_STYLE)
  221.                     os_set_text_style (g,arg);
  222.  
  223.             }
  224.             else os_display_char (g,c);
  225.         }
  226.     }
  227.     else 
  228.     {
  229.         // Translate FROTZ bordering fonts from funny EPOC equivalents
  230.         // to nearest looking.
  231.         i = 0;
  232.         while (*s != 0)
  233.         {
  234.             g->tempstr[i] = *s++;
  235.             if (g->tempstr[i] > 170) {
  236.                 if (g->tempstr[i] == 196) g->tempstr[i] = 151;
  237.                 else if (g->tempstr[i] == 179) g->tempstr[i] = 124;
  238.                 else if (g->tempstr[i] == 191 || g->tempstr[i] == 192 || g->tempstr[i] == 217 || g->tempstr[i] == 218) g->tempstr[i] = 32;
  239.             }
  240.             i++;
  241.         }
  242.         g->tempstr[i] = 0;
  243.  
  244.         // Prints whole word to screen
  245.         SrvPutString(g,g->cursor_x, g->cursor_y, g->tempstr);
  246.         g->cursor_x += os_string_width(g,g->tempstr);
  247.     }
  248.  
  249.  
  250. }/* os_display_string */
  251.  
  252. /*
  253.  * os_string_width
  254.  *
  255.  * Calculate the length of a word in screen units. Apart from letters,
  256.  * the word may contain special codes:
  257.  *
  258.  *    ZC_NEW_STYLE - next character is a new text style
  259.  *    ZC_NEW_FONT  - next character is a new font
  260.  *
  261.  */
  262.  
  263. short os_string_width (struct sg *g, const zchar *s)
  264. {
  265.     short width = 0;
  266.  
  267.     short saved_font = g->current_font;
  268.     short saved_style = g->current_style;
  269.  
  270.     zchar c;
  271.  
  272.     while ((c = *s++) != 0)
  273.     if (c == ZC_NEW_STYLE || c == ZC_NEW_FONT) {
  274.  
  275.         short arg = *s++;
  276.  
  277.         if (c == ZC_NEW_FONT)
  278.         g->current_font = arg;
  279.         if (c == ZC_NEW_STYLE)
  280.         g->current_style = arg;
  281.  
  282.     } else width += 1;
  283.  
  284.     g->current_font = saved_font;
  285.     g->current_style = saved_style;
  286.  
  287.     return width;
  288.  
  289. }/* os_string_width */
  290.  
  291. /*
  292.  * os_set_cursor
  293.  *
  294.  * Place the text cursor at the given coordinates. Top left is (1,1).
  295.  *
  296.  */
  297.  
  298. void os_set_cursor (struct sg *g, short y, short x)
  299. {
  300.     g->cursor_y = y - 1;
  301.     g->cursor_x = x - 1;
  302.     SrvSetCursor(g,g->cursor_x, g->cursor_y);
  303. }/* os_set_cursor */
  304.  
  305. /*
  306.  * os_more_prompt
  307.  *
  308.  * Display a MORE prompt, wait for a keypress and remove the MORE
  309.  * prompt from the screen.
  310.  *
  311.  */
  312.  
  313. void os_more_prompt (struct sg *g)
  314. {
  315.     short saved_x;
  316.  
  317.     /* Save text font and style */
  318.  
  319.     short saved_font = g->current_font;
  320.     short saved_style = g->current_style;
  321.  
  322.     /* Choose plain text style */
  323.  
  324.     g->current_font = TEXT_FONT;
  325.     g->current_style = 0;
  326.  
  327.     adjust_style (g);
  328.  
  329.     /* Wait until the user presses a key */
  330.  
  331.     saved_x = g->cursor_x;
  332.  
  333.     os_display_string (g,(zchar *) "[MORE]");
  334.     os_read_key (g,0, TRUE);
  335.  
  336.     os_erase_area (g,
  337.         g->cursor_y + 1,
  338.         saved_x + 1,
  339.         g->cursor_y + g->h_font_height,
  340.         g->cursor_x + 1);
  341.  
  342.     g->cursor_x = saved_x;
  343.  
  344.     /* Restore text font and style */
  345.  
  346.     g->current_font = saved_font;
  347.     g->current_style = saved_style;
  348.  
  349.     adjust_style (g);
  350.  
  351. }/* os_more_prompt */
  352.