Adjusted for the member name change
[m17n/m17n-lib.git] / example / mdump.c
index 996b5cc..272c816 100644 (file)
@@ -1,4 +1,4 @@
-/* mdump.c -- Dump text image
+/* mdump.c -- Dump text image                          -*- coding: euc-jp; -*-
    Copyright (C) 2003, 2004
      National Institute of Advanced Industrial Science and Technology (AIST)
      Registration Number H15PRO112
@@ -21,7 +21,7 @@
    02111-1307, USA.  */
 
 /***en
-    @page mdump dump text image
+    @enpage mdump dump text image
 
     @section mdump-synopsis SYNOPSIS
 
 
     @section mdump-description DESCRIPTION
 
-    Dump a text as a Netpbm image.
+    Dump a text as PNG image file.
 
-    The Netpbm image is written to a file created in the current
-    directory with the name "BASE.pbm" where BASE is the basename of
-    FILE.  If FILE is omitted, text is read from standard input, and
-    the image is dumped into the file "output.pbm".
+    The PNG file is written to a file created in the current directory
+    with the name "BASE.png" where BASE is the basename of FILE.  If
+    FILE is omitted, text is read from standard input, and the image
+    is dumped into the file "output.png".
 
     The following OPTIONs are available.
 
 
     <li> -p PAPER
 
-    PAPER is the paper size: a4, a4r, a5, a5r, b5, b5r, letter, or
-    WxH.  In the last case, W and H are the width and height in
-    millimeter.  If this option is specified, PAPER limits the image
-    size.  If FILE is too large for a single page, multiple files with
-    the names "BASE.01.pbm", "BASE.02.pbm", etc. are created.
+    PAPER is the paper size: a4, a4r, a5, a5r, b5, b5r, letter, WxH,
+    or W.  In the case of WxH, W and H are the width and height in
+    millimeter.  In the case of W, W is the width in millimeter.  If
+    this option is specified, PAPER limits the image size.  If FILE is
+    too large for a single page, multiple files with the names
+    "BASE.01.png", "BASE.02.png", etc. are created.
 
     <li> -m MARGIN
 
@@ -71,7 +72,7 @@
     <li> -x
 
     FILE is assumed to be an XML file generated by the serialize
-    facility of the m17n library, and FILE is deserialized before the
+    facility of the m17n library, and FILE is deserialized before an
     image is created.
 
     <li> -w
     <li> -f FILTER
 
     FILTER is a string containing a shell command line.  If this
-    option is specified, the Netpbm image is not written info a
+    option is specified, the PNG image is not written info a
     file but is given to FILTER as standard input.  If FILTER
     contains "%s", that part is replaced by a basename of FILE.
     So, the default behaviour is the same as specifying "cat >
-    %s.pbm" as FILTER.
+    %s.png" as FILTER.
+
+    <li> -a
+
+    Enable anti-alias drawing.
+
+    <li> --family FAMILY
+
+    Prefer a font whose family name is FAMILY.
+
+    <li> --language LANG
+
+    Prefer a font specified for the language LANG.  LANG must be a
+    2-letter code of ISO 630 (e.g. "en" for English).
 
     <li> -q
 
     </ul>
 */
 /***ja
-    @page mdump ¥Æ¥­¥¹¥È²èÁü¤Î¥À¥ó¥×
+    @japage mdump ¥Æ¥­¥¹¥È²èÁü¤Î¥À¥ó¥×
 
     @section mdump-synopsis SYNOPSIS
 
 
     @section mdump-description DESCRIPTION
 
-    ¥Æ¥­¥¹¥È¤ò Netpbm ²èÁü¤È¤·¤Æ¥À¥ó¥×¤¹¤ë¡£ 
+    ¥Æ¥­¥¹¥È¤ò PNG ²èÁü¤È¤·¤Æ¥À¥ó¥×¤¹¤ë¡£ 
 
-    Netpbm ²èÁü¤Ï¸½ºß¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ëºî¤é¤ì¤¿ "BASE.pbm" ¤È¤¤¤¦Ì¾Á°¤Î 
+    PNG ²èÁü¤Ï¸½ºß¤Î¥Ç¥£¥ì¥¯¥È¥ê¤Ëºî¤é¤ì¤¿ "BASE.png" ¤È¤¤¤¦Ì¾Á°¤Î 
     ¥Õ¥¡¥¤¥ë¤Ë½ñ¤­¹þ¤Þ¤ì¤ë¡£¤³¤³¤Ç BASE ¤ÏFILE ¤Î basename ¤Ç¤¢¤ë¡£ 
     FILE ¤¬¾Êά¤µ¤ì¤ì¤Ð¡¢¥Æ¥­¥¹¥È¤Ïɸ½àÆþÎϤ«¤éÆɤޤ졢²èÁü¤Ï 
-    "output.pbm" ¤Ë¥À¥ó¥×¤µ¤ì¤ë¡£
+    "output.png" ¤Ë¥À¥ó¥×¤µ¤ì¤ë¡£
 
     °Ê²¼¤Î¥ª¥×¥·¥ç¥ó¤¬ÍøÍѤǤ­¤ë¡£
 
 
     <li> -p PAPER
 
-    PAPER ¤Ï¤Ú¡¼¥Ñ¥µ¥¤¥º : a4, a4r, a5, a5r, b5, b5r, letter, ¤Þ¤¿¤Ï 
-    WxH. WxH ¤Î¾ì¹ç¡¢ W ¤È H ¤ÏÉý¤È¹â¤µ¤ò¥ß¥ê¥á¡¼¥¿Ã±°Ì¤Ç¼¨¤·¤¿¤â¤Î¡£ 
-    ¤³¤Î¥ª¥×¥·¥ç¥ó¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢ PAPER ¤¬²èÁü¥µ¥¤¥º¤òÀ©¸Â¤¹ 
-    ¤ë¡£FILE ¤¬ 1 ¥Ú¡¼¥¸¤ËǼ¤Þ¤é¤Ê¤¤¤Û¤ÉÂ礭¤¤¾ì¹ç¤Ï¡¢"BASE.01.pbm",
-    "BASE.02.pbm" Åù¤Î̾Á°¤Î¤Ä¤¤¤¿Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¤¬ºî¤é¤ì¤ë¡£
+    PAPER ¤Ï¤Ú¡¼¥Ñ¥µ¥¤¥º : a4, a4r, a5, a5r, b5, b5r, letter, WxH ¤Þ¤¿
+    ¤Ï W¡£ WxH ¤Î¾ì¹ç¡¢ W ¤È H ¤ÏÉý¤È¹â¤µ¤ò¥ß¥ê¥á¡¼¥¿Ã±°Ì¤Ç¼¨¤·¤¿¤â¤Î¡£
+    W ¤Î¾ì¹ç¡¢ W ¤ÏÉý¤ò¥ß¥ê¥á¡¼¥¿Ã±°Ì¤Ç¼¨¤·¤¿¤â¤Î¡£¤³¤Î¥ª¥×¥·¥ç¥ó¤¬»ØÄê
+    ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢ PAPER ¤¬²èÁü¥µ¥¤¥º¤òÀ©¸Â¤¹ ¤ë¡£FILE ¤¬ 1 ¥Ú¡¼¥¸
+    ¤ËǼ¤Þ¤é¤Ê¤¤¤Û¤ÉÂ礭¤¤¾ì¹ç¤Ï¡¢"BASE.01.png", "BASE.02.png" Åù¤Î̾Á°
+    ¤Î¤Ä¤¤¤¿Ê£¿ô¤Î¥Õ¥¡¥¤¥ë¤¬ºî¤é¤ì¤ë¡£
 
     <li> -m MARGIN
 
     <li> -f FILTER
 
     FILTER ¤Ï¥·¥§¥ë¥³¥Þ¥ó¥É¹Ô¤ò´Þ¤àʸ»úÎó¤Ç¤¢¤ë¡£¤³¤Î¥ª¥×¥·¥ç¥ó¤¬»ØÄê
-    ¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢Netpbm ²èÁü¤Ï¥Õ¥¡¥¤¥ë¤Ë½ñ¤«¤ì¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢FILTER ¤Ë 
-    É¸½àÆþÎϤȤ·¤ÆÅϤµ¤ì¤ë¡£ FILTER ¤¬ "s" ¤ò´Þ¤ó¤Ç¤¤¤ì¤Ð¡¢¤½¤ì¤Ï FILE 
+    ¤µ¤ì¤Æ¤¤¤ì¤Ð¡¢PNG ²èÁü¤Ï¥Õ¥¡¥¤¥ë¤Ë½ñ¤«¤ì¤ë¤Î¤Ç¤Ï¤Ê¤¯¡¢FILTER ¤Ë 
+    É¸½àÆþÎϤȤ·¤ÆÅϤµ¤ì¤ë¡£ FILTER ¤¬ "%s" ¤ò´Þ¤ó¤Ç¤¤¤ì¤Ð¡¢¤½¤ì¤Ï FILE 
     ¤Î¥Ù¡¼¥¹¥Í¡¼¥à¤ËÃÖ¤­´¹¤¨¤é¤ì¤ë¡£¤³¤Î¥×¥í¥°¥é¥à¤Î¥Ç¥Õ¥©¥ë¥È¤Î¿¶Éñ¤¤
-    ¤È¡¢FILTER ¤Ë "cat > s.pbm" ¤ò»ØÄꤷ¤¿¾ì¹ç¤Î¿¶Éñ¤ÏƱ°ì¤Ç¤¢¤ë¡£
+    ¤È¡¢FILTER ¤Ë "cat > %s.png" ¤ò»ØÄꤷ¤¿¾ì¹ç¤Î¿¶Éñ¤ÏƱ°ì¤Ç¤¢¤ë¡£
+
+    <li> -a
+
+    ¥¢¥ó¥Á¥¨¥¤¥ê¥¢¥¹½èÍý¤ò¹Ô¤¦¡£
+
+    <li> --family FAMILY
+
+    ¥Õ¥¡¥ß¥ê¥£Ì¾¤¬ FAMILY ¤Î¥Õ¥©¥ó¥È¤òÍ¥ÀèŪ¤Ë»È¤¦¡£
+
+    <li> --language LANG
+
+    ¸À¸ì LANG ÍѤ˻ØÄꤵ¤ì¤¿¥Õ¥©¥ó¥È¤òÍ¥ÀèŪ¤Ë»È¤¦¡£LANG ¤Ï ISO 630 ¤Î
+    £²Ê¸»ú¥³¡¼¥É¡ÊÎ㡧±Ñ¸ì¤Ï "en" ¡Ë¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
 
     <li> -q
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <libgen.h>
 
-#include <X11/Xlib.h>
+#include "config.h"
+
+#if defined (HAVE_FREETYPE) && defined (HAVE_GD)
+#include <gd.h>
 
 #include <m17n-gui.h>
 #include <m17n-misc.h>
 
-#define VERSION "1.0"
+#define PROGRAM_VERSION "1.1"
 
 /* Enumuration of the supported paper types.  */
 enum paper_type
@@ -236,18 +268,18 @@ help_exit (char *prog, int exit_code)
       prog = p;
 
   printf ("Usage: %s [ OPTION ...] [ FILE ]\n", prog);
-  printf ("Dump a text as a Netpbm image into a PBM file.\n");
-  printf ("  The PBM file is created in the current directory\n");
-  printf ("    with the name \"BASE.pbm\" where BASE is the basename of FILE.\n");
+  printf ("Dump a text as a PNG image into a file.\n");
+  printf ("  The PNG file is created in the current directory\n");
+  printf ("    with the name \"BASE.png\" where BASE is the basename of FILE.\n");
   printf ("  If FILE is omitted, text is read from standard input, and\n");
-  printf ("    dumped into the file \"output.pbm\".\n");
+  printf ("    dumped into the file \"output.png\".\n");
   printf ("The following OPTIONs are available.\n");
   printf ("  %-13s %s", "-s SIZE",
-         "Font size in point (default 12).\n");
+         "Font size in 1/10 point (default 120).\n");
   printf ("  %-13s %s", "-d DPI",
          "Resolution in dots per inch (defualt 300).\n");
   printf ("  %-13s %s", "-p PAPER",
-         "Paper size; a4, a4r, a5, a5r, b5, b5r, letter, or WxH.\n");
+         "Paper size; a4, a4r, a5, a5r, b5, b5r, letter, W, or WxH.\n");
   printf ("  %-13s %s", "-m MARGIN",
          "Marginal space in millimeter (default 20).\n");
   printf ("  %-13s %s", "-c POS",
@@ -257,6 +289,11 @@ help_exit (char *prog, int exit_code)
   printf ("  %-13s %s", "-f FILTER",
          "String containing a shell command line to be used as a filter.\n");
   printf ("  %-13s %s", "-w", "Each line is broken at word boundary.\n");
+  printf ("  %-13s %s", "-a", "Enable anti-alias drawing.\n");
+  printf ("  %-13s %s", "--family FAMILY", 
+         "Prefer a font whose family is FAMILY.\n");
+  printf ("  %-13s %s", "--language LANG", 
+         "Prefer a font specified for the langauge LANG.\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");
@@ -313,9 +350,9 @@ find_page_end (MFrame *frame, int height, MText *mt, int pos,
          while (to < next)
            {
              mdraw_glyph_info (frame, mt, to, to, control, &info);
-             if (y + info.this.height > height)
+             if (y + info.metrics.height > height)
                break;
-             y += info.this.height;
+             y += info.metrics.height;
              to = info.line_to;
            }
          break;
@@ -351,13 +388,10 @@ init_reverse_bit_order ()
    FILENAME and PAGE_INDEX (if it is not zero).  */
 
 void
-dump_image (XImage *image, char *filename, char *filter,
-           int white_is_zero, int page_index, int quiet_mode)
+dump_image (gdImagePtr image, char *filename, char *filter,
+           int page_index, int quiet_mode)
 {
   FILE *fp;
-  int pbm_bytes_per_line;
-  char *data = image->data;
-  int x, y;
 
   if (page_index)
     {
@@ -382,7 +416,7 @@ dump_image (XImage *image, char *filename, char *filter,
     {
       char *fullname = alloca (strlen (filename) + 5);
 
-      sprintf (fullname, "%s.pbm", filename);
+      sprintf (fullname, "%s.png", filename);
       fp = fopen (fullname, "w");
       if (! fp)
        FATAL_ERROR ("Can't write to \"%s\"\n", fullname);
@@ -390,53 +424,11 @@ dump_image (XImage *image, char *filename, char *filter,
        printf ("Writing %s ... ", fullname);
     }
 
-  if (image->bitmap_bit_order != MSBFirst
-      || (image->byte_order != MSBFirst
-         && image->bitmap_unit != 8))
-    {
-      /* We must adjust image->data for PBM.  */
-      int bytes_per_unit = image->bitmap_unit / 8;
-
-      for (y = 0; y < image->height; y++, data += image->bytes_per_line)
-       {
-         char b;
-
-         if (image->byte_order != MSBFirst)
-           {
-             if (reverse_bit_order[0] == 0)
-               init_reverse_bit_order ();
-             if (bytes_per_unit == 2)
-               for (x = 0; x < image->bytes_per_line; x += 2)
-                 b = data[x], data[x] = data[x + 1], data[x + 1] = b;
-             else if (bytes_per_unit == 6)
-               for (x = 0; x < image->bytes_per_line; x += 3)
-                 {
-                   b = data[x], data[x] = data[x + 3], data[x + 3] = b;
-                   x++;
-                   b = data[x], data[x] = data[x + 1], data[x + 1] = b;
-                 }
-           }
-
-         if (image->bitmap_bit_order != MSBFirst)
-           for (x = 0; x < image->bytes_per_line; x++)
-             data[x] = reverse_bit_order[(unsigned char) data[x]];
-         if (! white_is_zero)
-           for (x = 0; x < image->bytes_per_line; x++)
-             data[x] = ~data[x];
-       }
-      /* Reset DATA.  */
-      data = image->data;
-    }
-
-  /* Generate PBM (Portable Bitmap File Format) of P4 format.  */
-  fprintf (fp, "P4\n%d %d\n", image->width, image->height);
-  pbm_bytes_per_line = (image->width + 7) / 8;
-  for (y = 0; y < image->height; y++, data += image->bytes_per_line)
-    fwrite (data, 1, pbm_bytes_per_line, fp);
-
+  /* Generate PNG.  */
+  gdImagePng (image, fp);
   fclose (fp);
   if (! quiet_mode)
-    printf (" done (%dx%d)\n", image->width, image->height);
+    printf (" done (%dx%d)\n", image->sx, image->sy);
 }
 
 extern int line_break (MText *mt, int pos, int from, int to, int line, int y);
@@ -444,11 +436,6 @@ extern int line_break (MText *mt, int pos, int from, int to, int line, int y);
 int
 main (int argc, char **argv)
 {
-  Display *display;
-  int screen;
-  GC gc;
-  Pixmap pixmap;
-  XImage *image;
   int fontsize = 120;
   int paper = PAPER_NOLIMIT;
   int dpi = 300;
@@ -459,9 +446,14 @@ main (int argc, char **argv)
   int quiet_mode = 0;
   int break_by_word = 0;
   char *filter = NULL;
-  int i;
   int paper_width, paper_height;
+  int anti_alias = 0;
+  char *family_name = NULL;
+  char *lang_name = NULL;
+  int i;
   int page_index;
+  gdImagePtr image;
+  int white;
 
   MFrame *frame;
   MText *mt;
@@ -479,7 +471,7 @@ main (int argc, char **argv)
        help_exit (argv[0], 0);
       else if (! strcmp (argv[i], "--version"))
        {
-         printf ("mdump (m17n library) %s\n", VERSION);
+         printf ("mdump (m17n library) %s\n", PROGRAM_VERSION);
          printf ("Copyright (C) 2003, 2004 AIST, JAPAN\n");
          exit (0);
        }
@@ -515,6 +507,13 @@ main (int argc, char **argv)
              paper_size[paper].width = w;
              paper_size[paper].height = h;
            }
+         else if (sscanf (argv[i], "%d", &w) == 1
+                  && w > 0)
+           {
+             paper = PAPER_USER;
+             paper_size[paper].width = w;
+             paper_size[paper].height = 0;
+           }
          else
            FATAL_ERROR ("Invalid paper type: %s\n", argv[i]);
        }
@@ -552,6 +551,18 @@ main (int argc, char **argv)
        {
          quiet_mode = 1;
        }
+      else if (! strcmp (argv[i], "-a"))
+       {
+         anti_alias = 1;
+       }
+      else if (! strcmp (argv[i], "--family"))
+       {
+         family_name = argv[++i];
+       }
+      else if (! strcmp (argv[i], "--language"))
+       {
+         lang_name = argv[++i];
+       }
       else if (argv[i][0] != '-')
        {
          fp = fopen (argv[i], "r");
@@ -579,6 +590,8 @@ main (int argc, char **argv)
     FATAL_ERROR ("%s\n", "Fail to decode the input file or stream!");
 
   len = mtext_len (mt);
+  if (lang_name)
+    mtext_put_prop (mt, 0, len, Mlanguage, msymbol (lang_name));
 
   if (paper == PAPER_NOLIMIT)
     paper_width = paper_height = margin = 0;
@@ -589,9 +602,6 @@ main (int argc, char **argv)
       margin = margin * dpi / 25.4;
     }
 
-  display = XOpenDisplay (NULL);
-  screen = DefaultScreen (display);
-
   {
     MPlist *plist = mplist (), *p;
     MFontset *fontset = mfontset ("truetype");
@@ -599,20 +609,28 @@ main (int argc, char **argv)
 
     mface_put_prop (face, Mfontset, fontset);
     mface_put_prop (face, Msize, (void *) (fontsize * dpi / 100));
-    p = mplist_add (plist, msymbol ("display"), display);
-    p = mplist_add (p, msymbol ("depth"), (void *) 1);
+    if (family_name)
+      {
+       char *p;
+
+       for (p = family_name; *p; p++)
+         if (isupper (*p)) *p = tolower (*p);
+       mface_put_prop (face, Mfamily, msymbol (family_name));
+      }
+
+    p = mplist_add (plist, Mdevice, msymbol ("gd"));
     p = mplist_add (p, Mface, face);
     m17n_object_unref (face);
     frame = mframe (plist);
     m17n_object_unref (plist);
     if (! frame)
-      FATAL_ERROR ("%s\n", "Can't open a frame (perhaps no font avairable)!");
+      FATAL_ERROR ("%s\n", "Can't open a frame (perhaps no font available)!");
   }
 
   memset (&control, 0, sizeof control);
-  control.as_image = 1;
   control.two_dimensional = 1;
   control.enable_bidi = 1;
+  control.anti_alias = anti_alias;
   if (cursor_pos >= 0)
     {
       control.with_cursor = 1;
@@ -634,39 +652,35 @@ main (int argc, char **argv)
       paper_height = rect.height;
     }
   else
-    control.max_line_width = paper_width - margin * 2;
-
-  pixmap = XCreatePixmap (display, RootWindow (display, screen),
-                         paper_width, paper_height, 1);
-  gc = XCreateGC (display, pixmap, 0L, NULL);
+    {
+      control.max_line_width = paper_width - margin * 2;
+      if (paper_height == 0)
+       {
+         mdraw_text_extents (frame, mt, 0, len, &control, NULL, NULL, &rect);
+         paper_height = rect.height + margin * 2;
+       }
+    }
 
+  image = gdImageCreate (paper_width, paper_height);
   from = 0;
   page_index = 1;
+  white = gdImageColorAllocate (image, 255, 255, 255);
   while (from < len)
     {
       int to;
 
-      if (paper == PAPER_NOLIMIT)
+      if (paper == PAPER_NOLIMIT || paper_size[paper].height == 0)
        to = len;
       else
        to = find_page_end (frame, paper_height - margin * 2, mt, from,
                            &control, &rect);
 
-      XSetForeground (display, gc, WhitePixel (display, screen));
-      XFillRectangle (display, pixmap, gc, 0, 0, paper_width, paper_height);
-      mdraw_text_with_control (frame, (MDrawWindow) pixmap,
+      gdImageFilledRectangle (image, 0, 0, paper_width - 1, paper_height - 1,
+                             white);
+      mdraw_text_with_control (frame, image,
                               margin, margin - rect.y, mt, from, to,
                               &control);
-      XSetForeground (display, gc, BlackPixel (display, screen));
-#if 0
-      XDrawRectangle (display, pixmap, gc, margin, margin,
-                     paper_width - margin * 2 - 1,
-                     paper_height - margin * 2 - 1);
-#endif
-      image = XGetImage (display, pixmap, 0, 0, paper_width, paper_height,
-                        AllPlanes, XYPixmap);
-      XInitImage (image);
-      dump_image (image, filename, filter, !WhitePixel (display, screen),
+      dump_image (image, filename, filter,
                  ((from > 0 || to < len) ? page_index : 0),
                  quiet_mode);
 
@@ -677,7 +691,18 @@ main (int argc, char **argv)
   m17n_object_unref (frame);
   m17n_object_unref (mt);
   M17N_FINI ();
-  XCloseDisplay (display);
+  gdImageDestroy (image);
   exit (0);
 }
+
+#else  /* not HAVE_FREETYPE nor HAVE_GD */
+
+int
+main (int argc, char **argv)
+{
+  fprintf (stderr, "Can't run without Freetype and GD library!\n");
+  exit (1);
+}
+
+#endif  /* not HAVE_FREETYPE nor HAVE_GD */
 #endif /* not FOR_DOXYGEN */