*** empty log message ***
[m17n/m17n-lib.git] / example / mdump.c
index af72d91..2159f90 100644 (file)
@@ -1,5 +1,5 @@
 /* mdump.c -- Dump text image                          -*- coding: euc-jp; -*-
-   Copyright (C) 2003, 2004
+   Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
 
@@ -17,7 +17,7 @@
 
    You should have received a copy of the GNU Lesser General Public
    License along with the m17n library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    02111-1307, USA.  */
 
 /***en
@@ -88,6 +88,8 @@
     So, the default behaviour is the same as specifying "cat >
     %s.png" as FILTER.
 
+    If FILTER is just "-", the PNG image is written to stdout.
+
     <li> -a
 
     Enable anti-alias drawing.
     Prefer a font specified for the language LANG.  LANG must be a
     2-letter code of ISO 630 (e.g. "en" for English).
 
+    <li> -fg FOREGROUND
+
+    Specify the text color.  The supported color names are those of
+    HTML 4.0 and "#RRGGBB" notation.
+
+    <li> -bg BACKGROUND
+
+    Specify the background color.  The supported color names are the
+    same as FOREGROUND, except that if "transparent" is specified,
+    make the background transparent.
+
     <li> -r
 
     Specify that the orientation of the text is right-to-left.
     ¤Î¥Ù¡¼¥¹¥Í¡¼¥à¤ËÃÖ¤­´¹¤¨¤é¤ì¤ë¡£¤³¤Î¥×¥í¥°¥é¥à¤Î¥Ç¥Õ¥©¥ë¥È¤Î¿¶Éñ¤¤
     ¤È¡¢FILTER ¤Ë "cat > %s.png" ¤ò»ØÄꤷ¤¿¾ì¹ç¤Î¿¶Éñ¤ÏƱ°ì¤Ç¤¢¤ë¡£
 
+    ¤â¤· FILTER ¤¬Ã±¤Ë "-" ¤Ç¤¢¤ì¤Ð¡¢ PNG ²èÁü¤Ï stdout ¤Ë½ÐÎϤµ¤ì¤ë¡£
+
     <li> -a
 
     ¥¢¥ó¥Á¥¨¥¤¥ê¥¢¥¹½èÍý¤ò¹Ô¤¦¡£
     ¸À¸ì LANG ÍѤ˻ØÄꤵ¤ì¤¿¥Õ¥©¥ó¥È¤òÍ¥ÀèŪ¤Ë»È¤¦¡£LANG ¤Ï ISO 630 ¤Î
     £²Ê¸»ú¥³¡¼¥É¡ÊÎ㡧±Ñ¸ì¤Ï "en" ¡Ë¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
 
+    <li> -fg FOREGROUND
+
+    ¥Æ¥­¥¹¥È¤Î¿§¤ò»ØÄꤹ¤ë¡£HTML 4.0 ¤Î¿§¤Î̾Á°¤ª¤è¤Ó "#RRGGBB" µ­Ë¡¤ò
+    ¥µ¥Ý¡¼¥È¡£
+
+    <li> -bg BACKGROUND
+
+    Çطʤ理ò»ØÄꤹ¤ë¡£¥µ¥Ý¡¼¥È¤µ¤ì¤Æ¤¤¤ë¿§¤Î̾Á°¤Ï FOREGROUND Æ±¤¸¡£
+    ¤¿¤À¤·¡¢¤â¤· "transparent" ¤¬»ØÄꤵ¤ì¤¿¤éÇطʤòÆ©ÌÀ¤Ë¤¹¤ë¡£
+
+    <li> -r
+
+    Specify that the orientation of the text is right-to-left.
+
     <li> -q
 
     °ìÀڤΥá¥Ã¥»¡¼¥¸¤òɽ¼¨¤·¤Ê¤¤¡£ 
 #include <ctype.h>
 #include <libgen.h>
 
-#include "config.h"
-
 #if defined (HAVE_FREETYPE) && defined (HAVE_GD)
 #include <gd.h>
 
 #include <m17n-gui.h>
 #include <m17n-misc.h>
 
-#define PROGRAM_VERSION "1.1"
-
 /* Enumuration of the supported paper types.  */
 enum paper_type
   {
@@ -300,6 +325,10 @@ help_exit (char *prog, int exit_code)
          "Prefer a font specified for the langauge LANG.\n");
   printf ("  %-13s %s", "-r", 
          "Specify that the orientation of the text is right-to-left.\n");
+  printf ("  %-13s %s", "-fg FOREGROUND",
+         "Specify the text color (HTML 4.0 color names or \"#RRGGBB\").\n");
+  printf ("  %-13s %s", "-bg BACKGROUND",
+         "Specify the background color (\"transparent\" for transparent)\n");
   printf ("  %-13s %s", "-q", "Quiet mode.  Don't print any messages.\n");
   printf ("  %-13s %s", "--version", "Print the version number.\n");
   printf ("  %-13s %s", "-h, --help", "Print this message.\n");
@@ -390,14 +419,19 @@ dump_image (gdImagePtr image, char *filename, char *filter,
 
   if (filter)
     {
-      char *command = alloca (strlen (filename) + strlen (filter) + 1);
+      if (filter[0] == '-' && filter[1] == '\0')
+       fp = stdout;
+      else
+       {
+         char *command = alloca (strlen (filename) + strlen (filter) + 1);
 
-      sprintf (command, filter, filename);
-      fp = popen (command, "w");
-      if (! fp)
-       FATAL_ERROR ("Can't run the command \"%s\"\n", command);
-      if (! quiet_mode)
-       printf ("Running \"%s\" ... ", command);
+         sprintf (command, filter, filename);
+         fp = popen (command, "w");
+         if (! fp)
+           FATAL_ERROR ("Can't run the command \"%s\"\n", command);
+         if (! quiet_mode)
+           printf ("Running \"%s\" ... ", command);
+       }
     }
   else
     {
@@ -413,7 +447,8 @@ dump_image (gdImagePtr image, char *filename, char *filter,
 
   /* Generate PNG.  */
   gdImagePng (image, fp);
-  fclose (fp);
+  if (fp != stderr)
+    fclose (fp);
   if (! quiet_mode)
     printf (" done (%dx%d)\n", image->sx, image->sy);
 }
@@ -437,11 +472,13 @@ main (int argc, char **argv)
   int anti_alias = 0;
   char *family_name = NULL;
   char *lang_name = NULL;
+  char *fg_color = NULL, *bg_color = NULL;
+  int transparent = 0;
   int r2l = 0;
   int i;
   int page_index;
   gdImagePtr image;
-  int white;
+  int bg_rgb;
 
   MFrame *frame;
   MText *mt;
@@ -449,6 +486,8 @@ main (int argc, char **argv)
   MDrawMetric rect;
   char *filename = "output";
   int len, from;
+  char *fontset_name = "generic";
+
 
   /* Parse the command line arguments.  */
   for (i = 1; i < argc; i++)
@@ -459,8 +498,8 @@ main (int argc, char **argv)
        help_exit (argv[0], 0);
       else if (! strcmp (argv[i], "--version"))
        {
-         printf ("m17n-dump (m17n library) %s\n", PROGRAM_VERSION);
-         printf ("Copyright (C) 2003, 2004 AIST, JAPAN\n");
+         printf ("m17n-dump (m17n library) %s\n", M17NLIB_VERSION_NAME);
+         printf ("Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 AIST, JAPAN\n");
          exit (0);
        }
       else if (! strcmp (argv[i], "-s") && i + 1< argc)
@@ -562,6 +601,32 @@ main (int argc, char **argv)
            FATAL_ERROR ("Fail to open the file %s!\n", argv[i]);
          filename = basename (argv[i]);
        }
+      else if (! strcmp (argv[i], "--fontset"))
+       {
+         fontset_name = argv[++i];
+       }
+      else if (! strcmp (argv[i], "-fg"))
+       {
+         if (i + 1 == argc)
+           {
+             fprintf (stderr, "Foreground color not specified\n");
+             help_exit (argv[0], 1);
+           }
+         fg_color = argv[++i];
+       }
+      else if (! strcmp (argv[i], "-bg"))
+       {
+         if (i + 1 == argc)
+           {
+             fprintf (stderr, "Background color not specified\n");
+             help_exit (argv[0], 1);
+           }
+         i++;
+         if (! strcmp (argv[i], "transparent"))
+           transparent = 1;
+         else
+           bg_color = argv[i];
+       }
       else
        {
          fprintf (stderr, "Unknown or invalid option: %s\n", argv[i]);
@@ -596,7 +661,7 @@ main (int argc, char **argv)
 
   {
     MPlist *plist = mplist (), *p;
-    MFontset *fontset = mfontset ("truetype");
+    MFontset *fontset = mfontset (fontset_name);
     MFace *face = mface ();
 
     mface_put_prop (face, Mfontset, fontset);
@@ -609,7 +674,10 @@ main (int argc, char **argv)
          if (isupper (*p)) *p = tolower (*p);
        mface_put_prop (face, Mfamily, msymbol (family_name));
       }
-
+    if (fg_color)
+      mface_put_prop (face, Mforeground, msymbol (fg_color));
+    if (bg_color)
+      mface_put_prop (face, Mbackground, msymbol (bg_color));
     p = mplist_add (plist, Mdevice, msymbol ("gd"));
     p = mplist_add (p, Mface, face);
     m17n_object_unref (face);
@@ -654,10 +722,46 @@ main (int argc, char **argv)
        }
     }
 
+#if HAVE_GD > 1
+  image = gdImageCreateTrueColor (paper_width, paper_height);
+#else
   image = gdImageCreate (paper_width, paper_height);
+#endif
   from = 0;
   page_index = 1;
-  white = gdImageColorAllocate (image, 255, 255, 255);
+
+  if (transparent)
+    {
+      MFace *face = mframe_get_prop (frame, Mface);
+      MSymbol fg = mface_get_prop (face, Mforeground);
+      int rgb_value = 0;
+
+      if (fg)
+       rgb_value = (int) msymbol_get (fg, msymbol ("  rgb"));
+      if (rgb_value == 0xFFFFFF)
+       bg_rgb = gdImageColorAllocate (image, 0, 0, 0);
+      else
+       bg_rgb = gdImageColorAllocate (image, 255, 255, 255);
+      gdImageColorTransparent (image, bg_rgb);
+#if HAVE_GD > 1
+      gdImageAlphaBlending (image, 0);
+#endif
+    }
+  else
+    {
+      MFace *face = mframe_get_prop (frame, Mface);
+      MSymbol bg = mface_get_prop (face, Mbackground);
+      if (bg)
+       {
+         int rgb_value = (int) msymbol_get (bg, msymbol ("  rgb"));
+         bg_rgb = gdImageColorAllocate (image, rgb_value >> 16, 
+                                        (rgb_value >> 8) & 255, 
+                                        rgb_value & 255);
+       }
+      else
+       bg_rgb = gdImageColorAllocate (image, 255, 255, 255);
+    }
+
   while (from < len)
     {
       int to;
@@ -669,7 +773,7 @@ main (int argc, char **argv)
                            &control, &rect);
 
       gdImageFilledRectangle (image, 0, 0, paper_width - 1, paper_height - 1,
-                             white);
+                             bg_rgb);
       if (! r2l)
        mdraw_text_with_control (frame, image,
                                 margin, margin - rect.y,