Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1.  
  2. /*=============================================================================
  3.    GNU UnRTF, a command-line program to convert RTF documents to other formats.
  4.    Copyright (C) 2000,2001 Zachary Thayer Smith
  5.  
  6.    This program is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2 of the License, or
  9.    (at your option) any later version.
  10.  
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  
  20.    The author is reachable by electronic mail at tuorfa@yahoo.com.
  21. =============================================================================*/
  22.  
  23.  
  24. /*----------------------------------------------------------------------
  25.  * Module name:    output
  26.  * Author name:    Zach Smith
  27.  * Create date:    18 Sep 01
  28.  * Purpose:        Generalized output module
  29.  *----------------------------------------------------------------------
  30.  * Changes:
  31.  * 22 Sep 01, tuorfa@yahoo.com: addition of functions to change font size
  32.  * 22 Sep 01, tuorfa@yahoo.com: added function-level comment blocks
  33.  *--------------------------------------------------------------------*/
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include "malloc.h"
  39. #include "defs.h"
  40. #include "error.h"
  41. #include "output.h"
  42. #include "main.h"
  43. #include "convert.h"
  44.  
  45.  
  46. /*========================================================================
  47.  * Name:        op_create
  48.  * Purpose:     Creates a blank output personality.
  49.  * Args:        None.
  50.  * Returns:     Output personality struct.
  51.  *=======================================================================*/
  52.  
  53. OutputPersonality*
  54. op_create ()
  55. {
  56.         OutputPersonality* new_op;
  57.  
  58.         new_op = (OutputPersonality*) my_malloc (sizeof(OutputPersonality));
  59.         if (!new_op)
  60.                 error_handler ("cannot allocate output personality");
  61.  
  62.         bzero ((void*) new_op, sizeof (OutputPersonality));
  63.         return new_op;
  64. }
  65.  
  66.  
  67.  
  68. /*========================================================================
  69.  * Name:        op_free
  70.  * Purpose:     Deallocates an output personality, but none of the strings
  71.  *              it points to since they are usually constants.
  72.  * Args:        OutputPersonality.
  73.  * Returns:     None.
  74.  *=======================================================================*/
  75.  
  76. void
  77. op_free (OutputPersonality *op)
  78. {
  79.         CHECK_PARAM_NOT_NULL(op);
  80.  
  81.         my_free ((void*) op);
  82. }
  83.  
  84.  
  85.  
  86.  
  87. /*========================================================================
  88.  * Name:        op_translate_char
  89.  * Purpose:     Performs a translation of a character in the context of
  90.  *              a given output personality.
  91.  * Args:        OutputPersonality, character set#, character.
  92.  * Returns:     String.
  93.  *=======================================================================*/
  94.  
  95. char *
  96. op_translate_char (OutputPersonality *op, int charset, int ch)
  97. {
  98.         short start;
  99.         char *result=NULL;
  100.  
  101.         CHECK_PARAM_NOT_NULL(op);
  102.  
  103.         if (ch >= 0x20 && ch < 0x80) {
  104.                 result = op->ascii_translation_table [ch - 0x20];
  105.         }
  106.         else
  107.         if (charset != CHARSET_ANSI &&
  108.             charset != CHARSET_MAC &&
  109.             charset != CHARSET_CP437 &&
  110.             charset != CHARSET_CP850)
  111.                 error_handler ("invalid character set value, cannot translate character");
  112.         else
  113.         switch (charset) {
  114.         case CHARSET_ANSI:
  115.                 start = op->ansi_first_char;
  116.                 if (ch >= start &&
  117.                     ch <= op->ansi_last_char)
  118.                         result = op->ansi_translation_table [ch-start];
  119.                 break;
  120.         case CHARSET_MAC:
  121.                 start = op->mac_first_char;
  122.                 if (ch >= start &&
  123.                     ch <= op->mac_last_char)
  124.                         result = op->mac_translation_table [ch-start];
  125.                 break;
  126.         case CHARSET_CP437:
  127.                 start = op->cp437_first_char;
  128.                 if (ch >= start &&
  129.                     ch <= op->cp437_last_char)
  130.                         result = op->cp437_translation_table [ch-start];
  131.                 break;
  132.         case CHARSET_CP850:
  133.                 start = op->cp850_first_char;
  134.                 if (ch >= start &&
  135.                     ch <= op->cp850_last_char)
  136.                         result = op->cp850_translation_table [ch-start];
  137.                 break;
  138.         }
  139.         return result;
  140. }
  141.  
  142.  
  143. /*========================================================================
  144.  * Name:        op_begin_std_fontsize
  145.  * Purpose:     Prints whatever is necessary to perform a change in the
  146.  *              current font size.
  147.  * Args:        OutputPersonality, desired size.
  148.  * Returns:     None.
  149.  *=======================================================================*/
  150.  
  151. void
  152. op_begin_std_fontsize (OutputPersonality *op, int size)
  153. {
  154.         int found_std_expr = FALSE;
  155.  
  156.         CHECK_PARAM_NOT_NULL(op);
  157.  
  158.         /* Look for an exact match with a standard point size.
  159.          */
  160.         switch (size) {
  161.         case 8:
  162.                 if (op->fontsize8_begin) {
  163.                         printf (op->fontsize8_begin);
  164.                         found_std_expr = TRUE;
  165.                 }
  166.                 break;
  167.         case 10:
  168.                 if (op->fontsize10_begin) {
  169.                         printf (op->fontsize10_begin);
  170.                         found_std_expr = TRUE;
  171.                 }
  172.                 break;
  173.         case 12:
  174.                 if (op->fontsize12_begin) {
  175.                         printf (op->fontsize12_begin);
  176.                         found_std_expr = TRUE;
  177.                 }
  178.                 break;
  179.         case 14:
  180.                 if (op->fontsize14_begin) {
  181.                         printf (op->fontsize14_begin);
  182.                         found_std_expr = TRUE;
  183.                 }
  184.                 break;
  185.         case 18:
  186.                 if (op->fontsize18_begin) {
  187.                         printf (op->fontsize18_begin);
  188.                         found_std_expr = TRUE;
  189.                 }
  190.                 break;
  191.         case 24:
  192.                 if (op->fontsize24_begin) {
  193.                         printf (op->fontsize24_begin);
  194.                         found_std_expr = TRUE;
  195.                 }
  196.                 break;
  197.         case 36:
  198.                 if (op->fontsize36_begin) {
  199.                         printf (op->fontsize36_begin);
  200.                         found_std_expr = TRUE;
  201.                 }
  202.                 break;
  203.         case 48:
  204.                 if (op->fontsize48_begin) {
  205.                         printf (op->fontsize48_begin);
  206.                         found_std_expr = TRUE;
  207.                 }
  208.                 break;
  209.         }
  210.  
  211.         /* If no exact match, try to write out a change to the
  212.          * exact point size.
  213.          */
  214.         if (!found_std_expr) {
  215.                 if (op->fontsize_begin) {
  216.                         char expr[16];
  217.                         sprintf (expr, "%d", size);
  218.                         printf (op->fontsize_begin, expr);
  219.                 } else {
  220.                         /* If we cannot write out a change for the exact
  221.                          * point size, we must approximate to a standard
  222.                          * size.
  223.                          */
  224.                         if (size<9 && op->fontsize8_begin) {
  225.                                 printf (op->fontsize8_begin);
  226.                         } else
  227.                         if (size<11 && op->fontsize10_begin) {
  228.                                 printf (op->fontsize10_begin);
  229.                         } else
  230.                         if (size<13 && op->fontsize12_begin) {
  231.                                 printf (op->fontsize12_begin);
  232.                         } else
  233.                         if (size<16 && op->fontsize14_begin) {
  234.                                 printf (op->fontsize14_begin);
  235.                         } else
  236.                         if (size<21 && op->fontsize18_begin) {
  237.                                 printf (op->fontsize18_begin);
  238.                         } else
  239.                         if (size<30 && op->fontsize24_begin) {
  240.                                 printf (op->fontsize24_begin);
  241.                         } else
  242.                         if (size<42 && op->fontsize36_begin) {
  243.                                 printf (op->fontsize36_begin);
  244.                         } else
  245.                         if (size>40 && op->fontsize48_begin) {
  246.                                 printf (op->fontsize48_begin);
  247.                         } else
  248.                         /* If we can't even produce a good approximation,
  249.                          * just try to get a font size near 12 point.
  250.                          */
  251.                         if (op->fontsize12_begin)
  252.                                 printf (op->fontsize12_begin);
  253.                         else
  254.                         if (op->fontsize14_begin)
  255.                                 printf (op->fontsize14_begin);
  256.                         else
  257.                         if (op->fontsize10_begin)
  258.                                 printf (op->fontsize10_begin);
  259.                         else
  260.                         if (op->fontsize18_begin)
  261.                                 printf (op->fontsize18_begin);
  262.                         else
  263.                         if (op->fontsize8_begin)
  264.                                 printf (op->fontsize8_begin);
  265.                         else
  266.                                 error_handler ("output personality lacks sufficient font size change capability");
  267.                 }
  268.         }
  269. }
  270.  
  271.  
  272. /*========================================================================
  273.  * Name:        op_end_std_fontsize
  274.  * Purpose:     Prints whatever is necessary to perform a change in the
  275.  *              current font size.
  276.  * Args:        OutputPersonality, desired size.
  277.  * Returns:     None.
  278.  *=======================================================================*/
  279.  
  280. void
  281. op_end_std_fontsize (OutputPersonality *op, int size)
  282. {
  283.         int found_std_expr = FALSE;
  284.  
  285.         CHECK_PARAM_NOT_NULL(op);
  286.  
  287.         /* Look for an exact match with a standard point size.
  288.          */
  289.         switch (size) {
  290.         case 8:
  291.                 if (op->fontsize8_end) {
  292.                         printf (op->fontsize8_end);
  293.                         found_std_expr = TRUE;
  294.                 }
  295.                 break;
  296.         case 10:
  297.                 if (op->fontsize10_end) {
  298.                         printf (op->fontsize10_end);
  299.                         found_std_expr = TRUE;
  300.                 }
  301.                 break;
  302.         case 12:
  303.                 if (op->fontsize12_end) {
  304.                         printf (op->fontsize12_end);
  305.                         found_std_expr = TRUE;
  306.                 }
  307.                 break;
  308.         case 14:
  309.                 if (op->fontsize14_end) {
  310.                         printf (op->fontsize14_end);
  311.                         found_std_expr = TRUE;
  312.                 }
  313.                 break;
  314.         case 18:
  315.                 if (op->fontsize18_end) {
  316.                         printf (op->fontsize18_end);
  317.                         found_std_expr = TRUE;
  318.                 }
  319.                 break;
  320.         case 24:
  321.                 if (op->fontsize24_end) {
  322.                         printf (op->fontsize24_end);
  323.                         found_std_expr = TRUE;
  324.                 }
  325.                 break;
  326.         case 36:
  327.                 if (op->fontsize36_end) {
  328.                         printf (op->fontsize36_end);
  329.                         found_std_expr = TRUE;
  330.                 }
  331.                 break;
  332.         case 48:
  333.                 if (op->fontsize48_end) {
  334.                         printf (op->fontsize48_end);
  335.                         found_std_expr = TRUE;
  336.                 }
  337.                 break;
  338.         }
  339.  
  340.         /* If no exact match, try to write out a change to the
  341.          * exact point size.
  342.          */
  343.         if (!found_std_expr) {
  344.                 if (op->fontsize_end) {
  345.                         char expr[16];
  346.                         sprintf (expr, "%d", size);
  347.                         printf (op->fontsize_end, expr);
  348.                 } else {
  349.                         /* If we cannot write out a change for the exact
  350.                          * point size, we must approximate to a standard
  351.                          * size.
  352.                          */
  353.                         if (size<9 && op->fontsize8_end) {
  354.                                 printf (op->fontsize8_end);
  355.                         } else
  356.                         if (size<11 && op->fontsize10_end) {
  357.                                 printf (op->fontsize10_end);
  358.                         } else
  359.                         if (size<13 && op->fontsize12_end) {
  360.                                 printf (op->fontsize12_end);
  361.                         } else
  362.                         if (size<16 && op->fontsize14_end) {
  363.                                 printf (op->fontsize14_end);
  364.                         } else
  365.                         if (size<21 && op->fontsize18_end) {
  366.                                 printf (op->fontsize18_end);
  367.                         } else
  368.                         if (size<30 && op->fontsize24_end) {
  369.                                 printf (op->fontsize24_end);
  370.                         } else
  371.                         if (size<42 && op->fontsize36_end) {
  372.                                 printf (op->fontsize36_end);
  373.                         } else
  374.                         if (size>40 && op->fontsize48_end) {
  375.                                 printf (op->fontsize48_end);
  376.                         } else
  377.                         /* If we can't even produce a good approximation,
  378.                          * just try to get a font size near 12 point.
  379.                          */
  380.                         if (op->fontsize12_end)
  381.                                 printf (op->fontsize12_end);
  382.                         else
  383.                         if (op->fontsize14_end)
  384.                                 printf (op->fontsize14_end);
  385.                         else
  386.                         if (op->fontsize10_end)
  387.                                 printf (op->fontsize10_end);
  388.                         else
  389.                         if (op->fontsize18_end)
  390.                                 printf (op->fontsize18_end);
  391.                         else
  392.                         if (op->fontsize8_end)
  393.                                 printf (op->fontsize8_end);
  394.                         else
  395.                                 error_handler ("output personality lacks sufficient font size change capability");
  396.                 }
  397.         }
  398. }
  399.  
  400.  
  401.