forked from primejyothi/glyphRen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
glyphRen.cc
1372 lines (1213 loc) · 35.2 KB
/
glyphRen.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* \mainpage glyphRen
* \brief Rename the glyphs in a SFD file based on names from a reference file.
* \author Prime Jyothi (primejyothi [at] gmail [dot] com)
* \date 2014-01-01
* \copyright This project is released under the GNU Public License.
*
* For the Malayalam Unicode fonts, there is no standard conventions for
* naming a glyphs across different fonts. Since it is difficult for the
* developers to associate code point values and the characters, meaningful
* and human readable names are given to glyphs. However, these names are
* not standard across different fonts. If the names are same, it would be
* easier to apply different rules from one font to another. This utility
* is an attempt to rename the Malayalam glyphs in SFD files based on a
* standard naming convention.
*
* \par Acknowledgment
* Thanks to Rajeesh K Nambiar, who taught me the first glyphs of Malayalam fonts.
*
*/
using namespace std;
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cstdlib>
#include <getopt.h>
#include <limits.h>
#include <string.h>
#include "fontClass.hpp"
#include "jlog.hpp"
//! \file glyphRen.cc Rename glyphs in SFD file
//! \brief Rename the glyphs in a SFD file based on a standard file.
//!
//! Usage : glyphRen -r referenceFile -i inputSFDName -o outputSFDName
//! -l : Log level (DBG or TRACE)
//! -h : Display the help screen
//!
//! 1. Read the code points and the standard values from the Reference file.
//! 2. Read all Unicode characters and the names into the list
//! 3. Traverse through the list of characters and set the new names
//! for the characters.
//! 4. Write the new SFD file with renamed glyphs
//!
// Performance considerations are thrown out of the window.
int loadReferenceData (char *refFile, map<int, CharRefData>& ref);
int hexStrtoInt (string hexVal);
int analyzeSFDFile (char *inSfdFile, vector<FontChar>& vFontChar);
int getTok (string inStr, string& out, char delim, int pos);
int storeLigature (string sfdData, Ligature& sfdLigature);
int renameGlyphs (map<int, CharRefData> vRefData, vector <FontChar>& vFontChar, map<string, string>& nameMap, int& renCount);
void showMap (map<string, string> nameMap);
int buildName (map<string, string> nameMap, vector<string> comps, string& out);
int writeNewSFD (char *inSFDName, char *outFile, vector <FontChar>& vFontChar, map<string, string> nameMap);
int replaceFCName (map <string, string> nameMap, string& sfdData);
int replaceGlyphNames (map<string, string> nameMap, string& sfdData);
void help (char *progName);
int processArgs (int argc, char **argv, char *inFile, char *outFile, char *refFile, string& lvl);
int checkDups (vector<FontChar>& vFontChar, unsigned int idx, string newName);
int processHalfForms (string curName, string newName, string& hName);
//! Glyph name from the input SFD corresponding to Conjunct.
string Conjunct;
//! Glyph name from the input SFD corresponding to ZWJ.
string Zwj;
//! \fn int main (int argc, char **argv)
//! \brief Starting point of glyphRen.
int main (int argc, char **argv)
{
char inFile[PATH_MAX];
char outFile[PATH_MAX];
char refFile[PATH_MAX];
string logLvl;
memset (inFile, '\0', PATH_MAX);
memset (outFile, '\0', PATH_MAX);
memset (refFile, '\0', PATH_MAX);
// Process the command line arguments.
processArgs (argc, argv, inFile, outFile, refFile, logLvl);
if (logLvl == "DBG")
{
SETMSGLVL (DBG);
}
else if (logLvl == "TRACE")
{
SETMSGLVL (TRACE);
}
else
{
SETMSGLVL (LOG);
}
SETFWDT (13);
jTRACE ("inFile = " << inFile);
//! Map that hold the ref data from the file.
map<int, CharRefData> vRefData;
//! Vector that hold the glyph data from the SFD file.
vector<FontChar> vFontChar;
int retVal;
//! Load the reference data
retVal = loadReferenceData (refFile, vRefData);
if (SUCCESS != retVal)
{
jERR ("Error : loadReferenceData failed");
return (2);
}
// Print the data from the reference list
jTRACE ("Data from the reference list");
jTRACE ("vRefData.size () " << vRefData.size ());
for (map <int, CharRefData>::iterator i = vRefData.begin ();
i != vRefData.end(); ++i)
{
jTRACE ("vRefData[" << (*i).first << "] = ["
<< (*i).second.getCharName() << "]");
}
//! Analyze the input SFD file and load the data into FontChar class.
retVal = analyzeSFDFile (inFile, vFontChar);
if (SUCCESS != retVal)
{
jERR ("Error : analyzeSFDFile failed");
return (2);
}
map<string, string> nameMap;
int renCount = 0;
//! Load all glyphs into a map for convenience. This map will contain
//! old name and new name. The "old name" will be the key.
for (unsigned int i = 0; i < vFontChar.size(); i++)
{
vFontChar[i].loadMap (nameMap);
}
showMap (nameMap);
int pass = 1;
while (1)
{
jLOG ("renameGlyphs() : pass - " << pass);
//! Traverse the glyph info and rename the glyphs
retVal = renameGlyphs (vRefData, vFontChar, nameMap, renCount);
if (SUCCESS != retVal)
{
jERR ("Error : renameGlyphs failed");
return (2);
}
jLOG ("Number of glyphs renamed : " << renCount);
if (renCount == 0)
{
break;
}
pass++;
}
jDBG ("Starting writeNewSFD ========================================");
//! Read the SFD file again and write a new file with new glyph names.
retVal = writeNewSFD (inFile, outFile, vFontChar, nameMap);
if (SUCCESS != retVal)
{
jERR ("Error : renameGlyphs failed");
return (2);
}
showMap (nameMap);
}
//! \fn int loadReferenceData (char *refFile, map<int, CharRefData>& ref)
//! \brief Load the reference data from the reference file
//! \param [in] refFile Name of the file containing reference data.
//! \param [out] ref The CharRefData map that will hold the ref data.
//! \returns SUCCESS if operation is successful.
//! \returns FAIL if operation is not successful.
int loadReferenceData (char *refFile, map<int, CharRefData>& ref)
{
//! Read the data from the reference file
ifstream stdFile (refFile);
if (!stdFile.is_open())
{
jERR ("Unable to read reference file " << refFile);
return (FAIL);
}
jLOG ("Loading Reference data");
string readLine;
while (getline (stdFile, readLine))
{
CharRefData t;
int codeValue;
string tmpStr;
// Extract the codepoint value which is the first token.
if (getTok (readLine, tmpStr, ' ', 1) == SUCCESS)
{
// Convert hex to int and store.
codeValue = hexStrtoInt (tmpStr);
t.setCodeptVal (codeValue);
}
else
{
return (FAIL);
}
// Extract the glyph name which is the second token.
if (getTok (readLine, tmpStr, ' ', 2) == SUCCESS)
{
// Set the char name
t.setCharName (tmpStr);
}
else
{
return (FAIL);
}
ref[codeValue] = t;
ref[codeValue].displayData ();
}
stdFile.close ();
jLOG ("Finished Loading Reference data");
return (SUCCESS);
}
//! \fn int hexStrtoInt (string)
//! \brief Convert a hex string to int
//! \param [in] hexVal string containing hex value
//! \returns int value of hex string
int hexStrtoInt (string hexVal)
{
// Convert the hex string to int.
int h;
stringstream x;
x << hex << hexVal;
x >> h;
return h;
}
//! \fn int analyzeSFDFile (char *inSfdName, vector<FontChar>& vFontChar)
//! \brief Analyze the input SFD file and load the data into FontChar vector.
//! \param [in] inSfdName Name of the input SFD file.
//! \param [out] vFontChar vector holding glyph data.
//! \returns SUCCESS if operation is successful.
//! \returns FAIL if operation is not successful.
//
//! The glyphs are enclosed within [StartChar:] and [EndChar] sections.
//! Read the SFD file and load the following into FontChar vector:
//! -# Name of the glyph as mentioned in the SFD file.
//! -# Start position(?) of the glyph
//! -# End position(?) of the glyph
//! -# Code point value of the glyph
//! -# Skip the glyph if it is not a Malayalam glyph
//
int analyzeSFDFile (char *inSfdName, vector<FontChar>& vFontChar)
{
ifstream inSfdFile (inSfdName);
if (! inSfdFile.is_open ())
{
jERR ("ERROR : Unable to open SFD file " << inSfdName);
return FAIL;
}
string glyphName; // Name of the glyph from SFD file
int dataFlag; // Indicate if the StartChar pattern is found
int startPos;
int codeValue;
string sfdData;
int retVal;
FontChar sfdFC;
jLOG ("Analyzing the SFD file");
Ligature sfdLigature; // Ligatures read from input SFD file.
vector<Ligature> vLigature;
//! Read the data from the input SFD file.
dataFlag = 0;
int lineNo = 0;
while (getline (inSfdFile, sfdData))
{
lineNo++;
//! Look for [StartChar:]
string srch (START_CHAR_TEXT);
size_t found;
found = sfdData.find (srch);
if (found != string::npos)
{
jTRACE ("Rec# " << lineNo << " Processing ["
<< sfdData << "]");
dataFlag = 1;
//! [StartChar:] found, extract the glyph name which is
//! the 2nd token.
if (getTok (sfdData, glyphName, ' ', 2) != SUCCESS)
{
return FAIL;
}
}
//! Look for [Encoding:]
srch = ENCODING_TEXT;
found = sfdData.find (srch);
if (found != string::npos)
{
jTRACE (setw(5) << "Rec# " <<lineNo << " Processing ["
<< sfdData << "]");
//! Check if StartChar is already found, if not skip.
if (0 == dataFlag)
{
jTRACE (setw (5) << "Rec# " << lineNo << " Skipping ["
<< sfdData << "]");
continue;
}
//! [Encoding:] found, extract start, code point value, and end.
string tmpStr;
if (getTok (sfdData, tmpStr, ' ', 2) != SUCCESS)
{
return FAIL;
}
else
{
startPos = atoi (tmpStr.c_str ());
}
// Code point value at position 3
if (getTok (sfdData, tmpStr, ' ', 3) != SUCCESS)
{
return FAIL;
}
else
{
if (tmpStr == "-1")
{
codeValue = -1;
}
else
{
codeValue = atoi (tmpStr.c_str());
}
}
}
//! Look for Ligature
srch = LIGATURE_TEXT;
found = sfdData.find (srch);
if (found != string::npos)
{
jTRACE ("Rec# " << lineNo << " Processing ["
<< sfdData << "]");
//! Split the data and store in Ligature class
retVal = storeLigature (sfdData, sfdLigature);
if (retVal != SUCCESS)
{
jERR ("Error : storeLigature [" << sfdData << "]");
continue;
}
jTRACE ("Rec# " << lineNo << " Storing Ligatures");
// sfdLigature.displayData ();
//! Add the ligature to the temp list.
vLigature.push_back (sfdLigature);
// Clear the sfdLigature data.
sfdLigature.clearGlypName ();
sfdLigature.setForm ("");
}
//! Look for EndChar
srch = END_CHAR_TEXT;
found = sfdData.find (srch);
if (found != string::npos)
{
jTRACE (setw(5) << "Rec# " << lineNo << " Processing ["
<< sfdData << "]");
//! Save the glyph name into FontChar vector.
sfdFC.setCurName (glyphName);
//! Save the start pos into FontChar vector
sfdFC.setStartPos (startPos);
//! Save the code value into the FontChar vector
sfdFC.setUnicodeVal (codeValue);
//! Save the ligatures to FontChar vector from the
//! temporary Ligature list.
for (unsigned int i = 0; i < vLigature.size (); i++)
{
sfdFC.addLigature (vLigature[i]);
}
vFontChar.push_back (sfdFC);
jTRACE (setw(5) << "Rec# " << lineNo << " Added glyph info for " <<
glyphName << "]");
sfdFC.displayData ();
vLigature.clear();
sfdFC.clearData ();
}
}
jLOG ("Finished analyzing the SFD file");
inSfdFile.close ();
return SUCCESS;
}
//! \fn int getTok (string inStr, string& out, char delim, int pos)
//! \brief Copy the token at the pos from the string inStr delimited by
//! delim to the string out.
int getTok (string inStr, string& out, char delim, int pos)
{
string tok;
stringstream s (inStr);
for (int i = 0; i < pos; i++)
{
if (!getline (s, tok, delim))
{
return FAIL;
}
}
if (inStr == tok)
{
// The delimiter was not present in the input string.
return FAIL;
}
else
{
out = tok;
}
return (SUCCESS);
}
//! \fn int storeLigature (string sfdData, Ligature& sfdLigature)
//! \brief Populate the Ligature class from the Ligature Line read from
//! the input SFD file
//! \param [in] sfdData Ligature line from the input SFD file.
//! \param [in] sfdLigature Ligature class to which the data will be loaded.
//! \returns SUCCESS if data is stored successfully.
//! \returns FAIL if operation fails.
int storeLigature (string sfdData, Ligature& sfdLigature)
{
//! Extract the form from the SFD line. Form will be enclosed in single
//! quotes. The form will be second token when the delimiter is
//! single quote.
jTRACE ("Store Ligature");
string tmpStr;
int retVal;
retVal = getTok (sfdData, tmpStr, '\'', 2);
if (retVal != SUCCESS)
{
return FAIL;
}
else
{
sfdLigature.setForm (tmpStr);
}
//! Extract the names of the glyphs from the end. The glyphs will be
//! the third token if the delimiter is set to double quotes.
retVal = getTok (sfdData, tmpStr, '"', 3);
if (retVal != SUCCESS)
{
return FAIL;
}
int i;
i = 1;
string glyphName;
while (1)
{
retVal = getTok (tmpStr, glyphName, ' ', i);
i++;
if (retVal != SUCCESS)
{
//! getTok will return FAIL when it encounter the end of the
//! string. At this moment, it cannot be determined whether
//! getTok encountered an error or end of the string.
return SUCCESS;
}
else
{
if (glyphName.length () == 0)
{
// Ignore the spaces.
continue;
}
sfdLigature.addGlypToList (glyphName);
}
}
return SUCCESS;
}
//! \fn int renameGlyphs (map<int, CharRefData> vRefData, vector <FontChar>& vFontChar, map<string, string>& nameMap, int& renCount)
//! \brief Traverse through the glyph info and identify the glyphs
//! that need to be renamed.
//! \param [in] vRefData Map containing reference data
//! \param [in] vFontChar Vector holding SFD glyph data
//! \param [in] nameMap map holding key value pair of old and new glyph names.
//! \param [out] renCount Number of renames performed
//! \returns SUCCESS if operation is successful
//! \returns FAIL if operation is not successful
//!
//! Rules of the game:
//!
//! -# Glyphs will be renamed as specified in the reference file.
//! -# Composite glyphs will be renamed based on the constituent ligatures.
//! The name of the constituent glyphs will be combined to form the new name
//! of the composite glyph.
//! -# If there are multiple ligatures for a composite glyph, the one
//! with akhn will be used.
//! -# In case of a tie, the ligature with maximum glyphs will be used
//! for the creation of the new name
//! -# When two or more glyphs are joined to form new glyph name, the Conjunct
//! symbols are ignored to keep the name short and readable.
//! -# If the derived new name is already used in the SFD file, an underscore
//! followed by a sequence number will be appended to the new name to
//! avoid conflicts.
//! -# Certain glyphs need special processing and they are renamed to
//! pre defined names. Refer processHalfForms () for details on such glyphs.
int renameGlyphs (map<int, CharRefData> vRefData,
vector <FontChar>& vFontChar, map<string, string>& nameMap, int& renCount)
{
unsigned int i;
string fcName;
string refName;
//! Rename the characters. Since the Map and reference data are
//! not directly connected, have to use the data loaded from
//! the SFD file.
// renCount might contain value from previous run, set it to 0,
renCount = 0;
jLOG ("renameGlyphs() : Renaming the Glyphs");
jLOG ("renameGlyphs() : Processing base characters");
for (i = 0; i < vFontChar.size (); i++)
{
int fcUniVal;
fcUniVal = vFontChar[i].getUnicodeVal();
fcName = vFontChar[i].getCurName();
if (fcUniVal == -1)
{
//! Ignore composite characters while renaming base characters.
continue;
}
refName = vRefData[fcUniVal].getCharName ();
// Name of character from SFD file and corresponding name from
// ref file.
nameMap[fcName] = refName;
// Set the new name in the FontChar.
vFontChar[i].setNewName (refName);
jLOG ("Base char " << "old [" << fcName <<
"] new [" << refName << "]");
}
for (map <string, string>::iterator i = nameMap.begin ();
i != nameMap.end(); ++i)
{
if ((*i).second == CONJUNCT)
{
Conjunct = (*i).first;
jTRACE ("Conjunct [" << Conjunct << "]");
}
if ((*i).second == ZWJ)
{
Zwj = (*i).first;
jTRACE ("Zwj [" << Zwj << "]");
}
}
jLOG ("renameGlyphs() : Finished processing base characters");
showMap (nameMap);
//! Traverse through the glyphs in the FontChar vector and see if
//! any glyphs can be renamed with the available data in Rename map.
jLOG ("renameGlyphs() : Processing the glyphs");
for (unsigned int i = 0; i < vFontChar.size (); i++)
{
string tCurName = vFontChar[i].getCurName ();
string tNewName = nameMap[tCurName];
if (tNewName.length() != 0)
{
jTRACE ("New name found for " << tCurName << ": "
<< tNewName);
// Set the new name in the FontChar.
vFontChar[i].setNewName (tNewName);
}
}
jLOG ("renameGlyphs() : Finished processing the glyphs");
jLOG ("renameGlyphs() : Processing the Ligatures");
//! Traverse through the glyphs of the Ligatures in the FontChar vector
//! and see if any of FontChar glyphs can be renamed.
for (unsigned int i = 0; i < vFontChar.size (); i++)
{
string curName;
string newName;
vector<string> nameComps;
vector<string> akhnComps;
vector<string> maxComps;
vector<string> finalComps;
int maxCount; // Maximum glyphs in a ligature
int copyToMax;
int akhnFlag; // Indicate if akhn form is found
int renFlag; // Indicate if the glyph can be renamed.
curName = vFontChar[i].getCurName ();
int LigatureCount = vFontChar[i].getLigatureCount ();
maxCount = 0;
copyToMax = 0;
akhnFlag = 0;
renFlag = 0;
// Clear the vectors holding the glyph names.
nameComps.clear ();
akhnComps.clear ();
maxComps.clear ();
newName.clear ();
jTRACE ("\n");
jTRACE ("renameGlyphs() : Processing Ligature : " << curName);
if (LigatureCount == 0)
{
// No ligatures, skip.
continue;
}
// Check in the nameMap to see if it is already renamed.
newName = nameMap[curName];
if (newName.length() > 0)
{
// New name available, skip this.
jTRACE ("[" << curName << "] already renamed to [" << newName << "]");
continue;
}
for (int l = 0; l < LigatureCount; l++)
{
Ligature& tLig = vFontChar[i].getLigature (l);
// If the index out of range, getLigature will return a object
// with form set to InvalidObject. If that is the case free the
// object and return FAIL.
if (curName == "InvalidObject")
{
jERR ("getLigature at " << l << " failed.");
delete &tLig;
return (FAIL);
}
string tForm;
tForm = tLig.getForm ();
jTRACE ("Processing form [" << tForm << "]");
// Check if all the glyphs are renamed.
unsigned int glyphCount = tLig.getGlypListSize ();
int newCount;
newCount = 0;
if ((int) glyphCount > maxCount)
{
maxCount = glyphCount;
// This Ligature has got max glyphs so far, set copyToMax flag
// so that the glyphs will be copied to maxComps.
copyToMax = 1;
// maxComps may contain data from the previous Ligature,
// clear it.
maxComps.clear ();
}
else
{
copyToMax = 0;
}
if ("akhn" == tForm)
{
jTRACE ("Setting akhn flag ");
akhnFlag = 1;
akhnComps.clear ();
}
else
{
akhnFlag = 0;
}
// Clear the data from the previous ligature.
nameComps.clear ();
for (unsigned int k = 0; k < glyphCount; k++)
{
string tGlyphName;
string tNewName;
tLig.getNthglyphName (k, tGlyphName);
jDBG (k << " : " << tGlyphName);
nameComps.push_back (tGlyphName);
if (copyToMax)
{
// Max # of glyphs, save it for later use.
maxComps.push_back (tGlyphName);
}
jTRACE ("tForm: " << tForm);
if (akhnFlag)
{
jTRACE ("Adding [" << tGlyphName << "] to akhnComps");
akhnComps.push_back (tGlyphName);
}
// Check if the glyph name has got new name in the map.
tNewName = nameMap[tGlyphName];
if (tNewName.length () != 0)
{
newCount++;
}
jTRACE ("[" << tGlyphName << "] [" << tNewName << "]");
}
if (newCount == (int) glyphCount)
{
// All the constituent glyphs have new names, can be renamed.
jDBG (curName << " Can be renamed");
renFlag++;
}
else
{
jDBG (curName << " Cannot be renamed");
continue;
}
}
int dupRet;
if ((LigatureCount == 1) && (renFlag == LigatureCount))
{
// Only one form, straight away rename.
jDBG ("Straight rename");
finalComps = nameComps;
}
else
{
if (renFlag != LigatureCount)
{
// At least one of the ligature had a glyph without new name.
// Skip the renaming
jDBG ("RenFlag check failed for " << curName);
continue;
}
jDBG ("Multiple ligatures, further processing required");
if (akhnFlag)
{
jDBG ("Multiple ligatures, akhn form being added");
finalComps = akhnComps;
}else
{
jDBG ("Multiple ligatures, max being added");
finalComps = maxComps;
}
}
string suffix;
int seq;
string base;
seq = 0;
buildName (nameMap, finalComps, newName);
base = newName; // Base name, required in case of duplicates.
base.append ("_");
do
{
jTRACE ("Calling checkDups");
jTRACE ("Current glyph is [" << curName << "]");
dupRet = checkDups (vFontChar, i, newName);
if (dupRet == FAIL)
{
// Special processing required for some half forms.
int retVal;
string hName;
retVal = processHalfForms (curName, newName, hName);
if (retVal == SUCCESS)
{
// It was one of those cases that required special processing.
newName = hName;
dupRet = SUCCESS; // Break out of the checkDups loop.
}
else
{
jLOG ("[" << newName
<< "] already taken, appending seq #");
seq ++;
stringstream ss;
ss << seq;
suffix = ss.str();
jTRACE ("Suffix [" << suffix << "]");
newName = base;
newName.append (suffix);
}
}
} while (dupRet == FAIL);
jDBG ("Adding [" << curName << "] and [" << newName <<
"]to the map");
nameMap[curName] = newName;
// Set the new name.
vFontChar[i].setNewName (newName);
renCount++;
}
jLOG ("renameGlyphs() : Finished processing the Ligatures");
showMap (nameMap);
/*
for (i = 0; i < vFontChar.size (); i++)
{
vFontChar[i].displayData ();
vFontChar[i].displayGlyphs ();
}
*/
return SUCCESS;
}
//! \fn void showMap (map<string, string> nameMap)
//! \brief Display the contents of the Rename map
void showMap (map<string, string> nameMap)
{
jTRACE ("showMap ()");
for (map <string, string>::iterator i = nameMap.begin ();
i != nameMap.end(); ++i)
{
jTRACE ((*i).first << ":" << (*i).second);
}
}
//! \fn int buildName (map<string, string> nameMap, vector<string> comps, string& out)
//! \brief Build the new name for a glyph.
//! The new names of the strings are looked up against the Rename map and
//! creates new name.
//! \param [in] nameMap The rename map from which the new names will be
//! looked up
//! \param [in] comps Vector containing the glyphs to be renamed.
//! \param [out] out The string that will hold the new name.
//!
//! -# If the strings are glyph + xx + zwj, it is considered as a chillu
//! and new new name will be glyph + "cil"
int buildName (map<string, string> nameMap, vector<string> comps, string& out)
{
unsigned int i;
int zFlag;
int cFlag;
string newName;
string mappedName; // Currently mapped name for the glyph
cFlag = 0;
zFlag = 0;
newName = "";
for (i = 0; i < comps.size (); i++)
{
jTRACE ("i = " << i << " " << comps[i]);
}
for (i = 0; i < comps.size(); i++)
{
jDBG ("Finding new name for " << comps[i]);
// Check for Chillu & ZWJ
// if (comps[i] == ZWJ)
if ((comps[i] == ZWJ) || (comps[i] == Zwj))
{
zFlag++;
jTRACE ("Found ZWJ case");
//! Check if this is a chillu - glyph + xx + zwj. If true, rename
//! glyph to glyph + chil
if ( (i == 2) && (cFlag == 1) && (comps.size() == 3))
{
jDBG ("Found chillu comibination for " << comps[0]);
// out = comps[0];
out.append (CHILLU_NANE);
}
continue;
}
/*
jTRACE ("Conjunct [" << Conjunct << "]");
if ((comps[i] == "CONJUNCT") || (comps[i] == Conjunct))
{
//! If there are only two glyphs and the 2nd one is xx, retain it.
jTRACE ("Found Conjunct case");
cFlag++;
jTRACE ("out, before appening virama [" << out << "]");
if ( (i == 1) && (comps.size() == 2))
{
// out = comps[0];
out.append (CONJUNCT);
}
jTRACE ("Appended Conjunct, now the name is [" << out);
continue;
}
*/
if ((comps[i] == "CONJUNCT") || (comps[i] == Conjunct))
{
// Skip Conjunct.
cFlag++;
jTRACE ("Skipping Conjunct");
continue;
}
mappedName = nameMap[(comps[i])];
if (mappedName.length() != 0)
{
if (mappedName == CONJUNCT)
{
// Conjunct, skip it.
continue;
}
jDBG ("Named map [" << comps[i]
<< "] [" << mappedName << "]");
newName.append (mappedName);
jDBG ("New Name : [" << newName << "]");
out = newName;
}
else
{
// Not found in the remap, use the old one itself.
newName.append (comps[i]);
jDBG ("New Name : [" << newName << "]");
out = newName;
}
}
return (SUCCESS);
}
//! \fn int writeNewSFD (char *inSfdName, char *outFname, vector <FontChar>& vFontChar, map<string, string> nameMap)
//! \brief Read the input SFD file and create new SFD file with new glyph names.
//!
//! Read the input SFD file and and rename the glyphs using the look up table.
//! \param [in] inSfdName Name of the input SFD file.
//! \param [in] outFname Name of the output SFD file.
//! \param [in] vFontChar FontChar vector
//! \param [in] nameMap The lookup table for new glyph names.
int writeNewSFD (char *inSfdName, char *outFname, vector <FontChar>& vFontChar, map<string, string> nameMap)
{
string sfdData; // Data read from the input SFD file.
string srchPattern; // Search pattern
ifstream inSfdFile (inSfdName);
size_t strPos;
jLOG ("Writing new SFD file");
if (! inSfdFile.is_open ())
{
jERR ("Error : Unable to open SFD file " << inSfdName);
return FAIL;
}
ofstream outFile (outFname);
if (! outFile.is_open ())
{
jERR ("Uanble to open output file " << outFname);
}
while (getline (inSfdFile, sfdData))
{
srchPattern = START_CHAR_TEXT;