| 1 | /* |
| 2 | |
| 3 | This program, when compiled and run, will find glyphs and create cursored variants, and (not done) write an .fea lookup. Supply the glyph encoding range. |
| 4 | |
| 5 | */ |
| 6 | |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <cstdlib> |
| 10 | #include <iostream> |
| 11 | #include <fstream> |
| 12 | #include <ostream> |
| 13 | #include <math.h> |
| 14 | #include <string> |
| 15 | #include <cstring> |
| 16 | #include <sstream> |
| 17 | #include <sys/stat.h> |
| 18 | #include <vector> |
| 19 | #include <algorithm> |
| 20 | |
| 21 | using namespace std; |
| 22 | |
| 23 | // GLYPHS YOU WANT TO CONVERT |
| 24 | |
| 25 | string FileName = "FONTNAME"; |
| 26 | |
| 27 | string suffix = ".smcp"; |
| 28 | |
| 29 | string tagname = "sc"; |
| 30 | |
| 31 | string spacedtagname = "s c"; |
| 32 | |
| 33 | string featurename = "SmallCaps"; |
| 34 | |
| 35 | // |
| 36 | |
| 37 | string line; |
| 38 | string encoding; |
| 39 | vector<string> encodings; |
| 40 | |
| 41 | vector<int> enc1; |
| 42 | vector<int> enc2; |
| 43 | vector<int> enc3; |
| 44 | |
| 45 | vector<int> benc1; |
| 46 | vector<int> benc2; |
| 47 | vector<int> benc3; |
| 48 | |
| 49 | string BaseEncoding; |
| 50 | vector<string> BaseEncodings; |
| 51 | |
| 52 | string CursorEncoding; |
| 53 | vector<string> CursorEncodings; |
| 54 | |
| 55 | string Width; |
| 56 | string VWidth; |
| 57 | string Flags; |
| 58 | string ReferEncoding; |
| 59 | vector<string> ReferEncodings; |
| 60 | |
| 61 | vector<string> baseglyphs = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "space", "comma", "period", "colon", "semicolon", "hyphen", "parenleft", "parenright", "exclam", "question", "quotesingle", "quotedbl", "endash", "emdash", "quoteleft", "quoteright", "quotedblright", "quotedblleft", "quotedblbase", "ellipsis", "ampersand"}; |
| 62 | string glyphname; |
| 63 | string writeglyphname; |
| 64 | |
| 65 | int i = 0; |
| 66 | |
| 67 | |
| 68 | std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) { |
| 69 | std::stringstream ss(s); |
| 70 | std::string item; |
| 71 | while (std::getline(ss, item, delim)) { |
| 72 | elems.push_back(item); |
| 73 | } |
| 74 | return elems; |
| 75 | } |
| 76 | |
| 77 | string parameter ( string param ) { |
| 78 | string value; |
| 79 | if (line.substr(0, param.length()) == param) { value = line.substr(param.length(),9999); } |
| 80 | return value; |
| 81 | } |
| 82 | |
| 83 | bool hasEnding (std::string const &fullString, std::string const &ending) { |
| 84 | if (fullString.length() >= ending.length()) { |
| 85 | return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending)); |
| 86 | } else { |
| 87 | return false; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | int main () |
| 92 | { |
| 93 | |
| 94 | fstream fontfile; |
| 95 | fontfile.open (FileName); |
| 96 | |
| 97 | // FINDS OUT WHERE TO INSERT NEW GLYPHS |
| 98 | while ( getline (fontfile,line) ) |
| 99 | { |
| 100 | |
| 101 | if (line.substr(0,10) == "Encoding: " && ( line.substr(0,16) != "Encoding: Custom")) { |
| 102 | encoding = line; |
| 103 | &split(encoding, ' ', encodings); |
| 104 | |
| 105 | enc1.push_back(atoi(encodings.at(i + 1).c_str()) ); |
| 106 | enc2.push_back(atoi(encodings.at(i + 2).c_str()) ); |
| 107 | enc3.push_back(atoi(encodings.at(i + 3).c_str()) ); |
| 108 | |
| 109 | if (hasEnding(glyphname, suffix) ) { |
| 110 | baseglyphs.push_back(glyphname.substr(11,99)); |
| 111 | |
| 112 | |
| 113 | } |
| 114 | |
| 115 | i = i + 4; |
| 116 | } |
| 117 | glyphname = line; |
| 118 | |
| 119 | |
| 120 | } |
| 121 | |
| 122 | fontfile.close(); |
| 123 | fontfile.open (FileName); |
| 124 | |
| 125 | // FINDS CURSOR |
| 126 | |
| 127 | bool act = false; |
| 128 | |
| 129 | while ( getline (fontfile,line) ) |
| 130 | { |
| 131 | |
| 132 | |
| 133 | if (line.substr(0,11) == "StartChar: " && line.substr(11,99) == "start" + tagname) { |
| 134 | |
| 135 | act = true; |
| 136 | } |
| 137 | |
| 138 | if (act) { |
| 139 | if ( parameter("Encoding: ").length() > 0 ) { CursorEncoding = parameter("Encoding: "); &split(CursorEncoding, ' ', CursorEncodings); } |
| 140 | } |
| 141 | if (act && line == "EndChar") { |
| 142 | act = false; |
| 143 | } |
| 144 | |
| 145 | } |
| 146 | |
| 147 | |
| 148 | fontfile.close(); |
| 149 | fontfile.open (FileName); |
| 150 | |
| 151 | // WRITES DUPLICATE GLYPHS |
| 152 | |
| 153 | ofstream glyphs; |
| 154 | glyphs.open ("glyphs.txt"); |
| 155 | |
| 156 | act = false; |
| 157 | |
| 158 | int index = 1; |
| 159 | |
| 160 | while ( getline (fontfile,line) ) |
| 161 | { |
| 162 | |
| 163 | // FINDS BASE GLYPHS |
| 164 | |
| 165 | |
| 166 | if (line.substr(0,11) == "StartChar: " && find(baseglyphs.begin(), baseglyphs.end(), line.substr(11,99)) != baseglyphs.end() ) { |
| 167 | |
| 168 | act = true; |
| 169 | writeglyphname = line.substr(11,99); |
| 170 | } |
| 171 | |
| 172 | if (act) { |
| 173 | |
| 174 | if ( parameter("Width: ").length() > 0 ) { Width = parameter("Width: "); } |
| 175 | if ( parameter("VWidth: ").length() > 0 ) { VWidth = parameter("VWidth: "); } |
| 176 | if ( parameter("Flags: ").length() > 0 ) { Flags = parameter("Flags: "); } |
| 177 | if ( parameter("Encoding: ").length() > 0 ) { ReferEncoding = parameter("Encoding: "); &split(ReferEncoding, ' ', ReferEncodings); } |
| 178 | } |
| 179 | if (act && line == "EndChar") { |
| 180 | act = false; |
| 181 | |
| 182 | glyphs << "StartChar: " << writeglyphname.substr(0, writeglyphname.find(".")) << suffix << "flag" << "\n" ; |
| 183 | glyphs << "Encoding: " << *max_element(enc1.begin(), enc1.end()) + index << " -1 " << *max_element(enc3.begin(), enc3.end()) + index << "\n" ; |
| 184 | glyphs << "Width: " << Width << "\n" ; |
| 185 | glyphs << "VWidth: " << VWidth << "\n" ; |
| 186 | glyphs << "Flags: " << Flags << "\n" ; |
| 187 | glyphs << "LayerCount: 2\nBack\nFore" << "\n" ; |
| 188 | glyphs << "Refer: " << ReferEncodings.at(2) << " " << ReferEncodings.at(0) << " N 1 0 0 1 0 0 1" << "\n" ; |
| 189 | glyphs << "Refer: " << CursorEncodings.at(2) << " " << CursorEncodings.at(0) << " N 1 0 0 1 " << Width << " 0 2" << "\n" ; |
| 190 | glyphs << "Validated: 1" << "\n" ; |
| 191 | glyphs << "EndChar" << "\n\n" ; |
| 192 | ReferEncodings.clear(); |
| 193 | |
| 194 | index = index + 1; |
| 195 | |
| 196 | } |
| 197 | |
| 198 | } |
| 199 | |
| 200 | glyphs.close(); |
| 201 | |
| 202 | // START WRITING FEATURE RULES |
| 203 | |
| 204 | ofstream featurefile; |
| 205 | featurefile.open ("feature.fea"); |
| 206 | |
| 207 | // Write scripts |
| 208 | featurefile << "languagesystem DFLT dflt;\nlanguagesystem grek dflt;\nlanguagesystem latn dflt;\nlanguagesystem math dflt;\n\n" ; |
| 209 | |
| 210 | // Write letter class |
| 211 | featurefile << "@" << tagname << "characters = [" ; |
| 212 | |
| 213 | for (int n = 1; n <= baseglyphs.size(); n++) { |
| 214 | featurefile << "\\" << baseglyphs.at(n - 1).substr(0, baseglyphs.at(n - 1).find(".")) << " "; |
| 215 | } |
| 216 | |
| 217 | featurefile << "];\n\n" ; |
| 218 | |
| 219 | // Write base class |
| 220 | featurefile << "@" << tagname << "base = [" ; |
| 221 | |
| 222 | for (int n = 1; n <= baseglyphs.size(); n++) { |
| 223 | featurefile << "\\" << baseglyphs.at(n - 1) << " "; |
| 224 | } |
| 225 | |
| 226 | featurefile << "];\n\n" ; |
| 227 | |
| 228 | // Write flagged class |
| 229 | featurefile << "@" << tagname << "flags = [" ; |
| 230 | |
| 231 | for (int n = 1; n <= baseglyphs.size(); n++) { |
| 232 | featurefile << "\\" << baseglyphs.at(n - 1).substr(0, baseglyphs.at(n - 1).find(".")) << suffix << "flag" << " "; |
| 233 | } |
| 234 | |
| 235 | featurefile << "];\n\n" ; |
| 236 | |
| 237 | // Write initial lookup |
| 238 | |
| 239 | featurefile << "feature liga {\n" ; |
| 240 | |
| 241 | featurefile << "lookup " << featurename << "Open {\n" ; |
| 242 | featurefile << "sub " << "\\less " << spacedtagname << " \\greater" << " by " << "\\start" << tagname << ";\n" ; |
| 243 | |
| 244 | featurefile << "} " << featurename << "Open;\n" ; |
| 245 | |
| 246 | featurefile << "lookup " << featurename << "Initialize {\n" ; |
| 247 | for (int n = 1; n <= baseglyphs.size(); n++) { |
| 248 | featurefile << "sub " << "\\start" << tagname << " " << "\\" << baseglyphs.at(n - 1).substr(0, baseglyphs.at(n - 1).find(".")) << " by " << "\\" << baseglyphs.at(n - 1).substr(0, baseglyphs.at(n - 1).find(".")) << suffix << "flag" << ";\n" ; |
| 249 | } |
| 250 | |
| 251 | featurefile << "} " << featurename << "Initialize;\n" ; |
| 252 | featurefile << "} liga;\n\n" ; |
| 253 | |
| 254 | // Write chaining lookup |
| 255 | |
| 256 | featurefile << "feature calt {\n" ; |
| 257 | featurefile << "lookup " << featurename << "Chaining {\n" ; |
| 258 | featurefile << "sub " << "@" << tagname << "flags" << " " << "@" << tagname << "characters" << "'" << " by " << "@" << tagname << "flags" << ";\n" ; |
| 259 | |
| 260 | featurefile << "} " << featurename << "Chaining;\n" ; |
| 261 | |
| 262 | // Undo repeated flags |
| 263 | featurefile << "lookup " << featurename << "Rendering {\n" ; |
| 264 | featurefile << "sub " << "@" << tagname << "flags" << "'" << " " << "@" << tagname << "flags" << " by " << "@" << tagname << "base" << ";\n" ; |
| 265 | |
| 266 | featurefile << "} " << featurename << "Rendering;\n" ; |
| 267 | |
| 268 | featurefile << "} calt;\n\n" ; |
| 269 | |
| 270 | // Write closing tag lookup |
| 271 | |
| 272 | featurefile << "feature liga {\n" ; |
| 273 | featurefile << "lookup " << featurename << "Close {\n" ; |
| 274 | for (int n = 1; n <= baseglyphs.size(); n++) { |
| 275 | featurefile << "sub " << "\\" << baseglyphs.at(n - 1).substr(0, baseglyphs.at(n - 1).find(".")) << suffix << "flag" << " " << "\\less \\slash " << spacedtagname << " \\greater" << " by " << "\\" << baseglyphs.at(n - 1) << ";\n" ; |
| 276 | } |
| 277 | |
| 278 | featurefile << "} " << featurename << "Close;\n" ; |
| 279 | featurefile << "} liga;\n\n" ; |
| 280 | |
| 281 | featurefile.close(); |
| 282 | |
| 283 | return(0); |
| 284 | } |