GNU Unifont  15.1.01
Pan-Unicode font with complete Unicode Plane 0 coverage and partial coverage of higher planes
johab2syllables.c
Go to the documentation of this file.
1 /**
2  @file johab2syllables.c
3 
4  @brief Create the Unicode Hangul Syllables block from component letters.
5 
6  This program reads in a "hangul-base.hex" file containing Hangul
7  letters in Johab 6/3/1 format and outputs a Unifont .hex format
8  file covering the Unicode Hangul Syllables range of U+AC00..U+D7A3.
9 
10  @author Paul Hardy
11 
12  @copyright Copyright © 2023 Paul Hardy
13 */
14 /*
15  LICENSE:
16 
17  This program is free software: you can redistribute it and/or modify
18  it under the terms of the GNU General Public License as published by
19  the Free Software Foundation, either version 2 of the License, or
20  (at your option) any later version.
21 
22  This program is distributed in the hope that it will be useful,
23  but WITHOUT ANY WARRANTY; without even the implied warranty of
24  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  GNU General Public License for more details.
26 
27  You should have received a copy of the GNU General Public License
28  along with this program. If not, see <http://www.gnu.org/licenses/>.
29 */
30 
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 
35 #include "hangul.h"
36 
37 
38 /**
39  @brief The main function.
40 */
41 int
42 main (int argc, char *argv[]) {
43  int i; /* Loop variables */
44  int arg_count; /* index into *argv[] */
45  unsigned codept;
46  unsigned max_codept;
47  unsigned char hangul_base[MAX_GLYPHS][32];
48  int initial, medial, final; /* Base glyphs for a syllable. */
49  unsigned char syllable[32]; /* Syllable glyph built for output. */
50 
51  FILE *infp = stdin; /* Input Hangul Johab 6/3/1 file */
52  FILE *outfp = stdout; /* Output Hangul Syllables file */
53 
54  /* Print a help message */
55  void print_help ();
56 
57  /* Read the file containing Hangul base glyphs. */
58  unsigned hangul_read_base8 (FILE *infp, unsigned char hangul_base[][32]);
59 
60  /* Given a Hangul Syllables code point, determine component glyphs. */
61  void hangul_decompose (unsigned codept, int *, int *, int *);
62 
63  /* Given letters in a Hangul syllable, return a glyph. */
64  void hangul_syllable (int choseong, int jungseong, int jongseong,
65  unsigned char hangul_base[][32],
66  unsigned char *syllable);
67 
68 
69  /*
70  If there are command line arguments, parse them.
71  */
72  arg_count = 1;
73 
74  while (arg_count < argc) {
75  /* If input file is specified, open it for read access. */
76  if (strncmp (argv [arg_count], "-i", 2) == 0) {
77  arg_count++;
78  if (arg_count < argc) {
79  infp = fopen (argv [arg_count], "r");
80  if (infp == NULL) {
81  fprintf (stderr, "\n*** ERROR: Cannot open %s for input.\n\n",
82  argv [arg_count]);
83  exit (EXIT_FAILURE);
84  }
85  }
86  }
87  /* If output file is specified, open it for write access. */
88  else if (strncmp (argv [arg_count], "-o", 2) == 0) {
89  arg_count++;
90  if (arg_count < argc) {
91  outfp = fopen (argv [arg_count], "w");
92  if (outfp == NULL) {
93  fprintf (stderr, "\n*** ERROR: Cannot open %s for output.\n\n",
94  argv [arg_count]);
95  exit (EXIT_FAILURE);
96  }
97  }
98  }
99  /* If help is requested, print help message and exit. */
100  else if (strncmp (argv [arg_count], "-h", 2) == 0 ||
101  strncmp (argv [arg_count], "--help", 6) == 0) {
102  print_help ();
103  exit (EXIT_SUCCESS);
104  }
105 
106  arg_count++;
107  }
108 
109 
110  /*
111  Initialize entire glyph array to zeroes in case the input
112  file skips over some code points.
113  */
114  for (codept = 0; codept < MAX_GLYPHS; codept++) {
115  for (i = 0; i < 32; i++) hangul_base[codept][i] = 0;
116  }
117 
118  /*
119  Read the entire "hangul-base.hex" file into an array
120  organized as hangul_base [code_point][glyph_byte].
121  The Hangul glyphs are 16 columns wide, which is
122  two bytes, by 16 rows, for a total of 2 * 16 = 32 bytes.
123  */
124  max_codept = hangul_read_base8 (infp, hangul_base);
125  if (max_codept > 0x8FF) {
126  fprintf (stderr, "\nWARNING: Hangul glyph range exceeds PUA space.\n\n");
127  }
128 
129  /*
130  For each glyph in the Unicode Hangul Syllables block,
131  form a composite glyph of choseong + jungseong +
132  optional jongseong and output it in Unifont .hex format.
133  */
134  for (codept = 0xAC00; codept < 0xAC00 + 19 * 21 * 28; codept++) {
135  hangul_decompose (codept, &initial, &medial, &final);
136 
137  hangul_syllable (initial, medial, final, hangul_base, syllable);
138 
139  fprintf (outfp, "%04X:", codept);
140 
141  for (i = 0; i < 32; i++) {
142  fprintf (outfp, "%02X", syllable[i]);
143  }
144  fputc ('\n', outfp);
145  }
146 
147  exit (EXIT_SUCCESS);
148 }
149 
150 
151 /**
152  @brief Print a help message.
153 */
154 void
156 
157  printf ("\ngen-hangul [options]\n\n");
158  printf (" Generates Hangul syllables from an input Unifont .hex file encoded\n");
159  printf (" in Johab 6/3/1 format. The output is the Unicode Hangul Syllables\n");
160  printf (" range, U+AC00..U+D7A3.\n\n");
161  printf (" This program demonstrates forming Hangul syllables without shifting\n");
162  printf (" the final consonant (jongseong) when combined with a vowel having\n");
163  printf (" a long double vertical stroke. For a program that demonstrtes\n");
164  printf (" shifting jongseong in those cases, see unigen-hangul, which is what\n");
165  printf (" creates the Unifont Hangul Syllables block.\n\n");
166 
167  printf (" This program may be invoked with the following command line options:\n\n");
168 
169  printf (" Option Parameters Function\n");
170  printf (" ------ ---------- --------\n");
171  printf (" -h, --help Print this message and exit.\n\n");
172  printf (" -i input_file Unifont hangul-base.hex formatted input file.\n\n");
173  printf (" -o output_file Unifont .hex format output file.\n\n");
174  printf (" Example:\n\n");
175  printf (" johab2syllables -i hangul-base.hex -o hangul-syllables.hex\n\n");
176 
177  return;
178 }
179 
Define constants and function prototypes for using Hangul glyphs.
unsigned hangul_read_base8(FILE *infp, unsigned char base[][32])
Read hangul-base.hex file into a unsigned char array.
void hangul_decompose(unsigned codept, int *initial, int *medial, int *final)
Decompose a Hangul Syllables code point into three letters.
void hangul_syllable(int choseong, int jungseong, int jongseong, unsigned char hangul_base[][32], unsigned char *syllable)
Given letters in a Hangul syllable, return a glyph.
#define MAX_GLYPHS
An OpenType font has at most 65536 glyphs.
Definition: hex2otf.c:85
int main(int argc, char *argv[])
The main function.
void print_help()
Print a help message.