| 1 |
// SPDX-License-Identifier: MIT OR MPL-2.0 OR LGPL-2.1-or-later OR GPL-2.0-or-later |
| 2 |
// Copyright (C) 2005 www.thanlwinsoft.org, SIL International |
| 3 |
/* |
| 4 |
Responsibility: Keith Stribley, Martin Hosken |
| 5 |
Description: |
| 6 |
A simple console app that creates a segment using FileFont and dumps a |
| 7 |
diagnostic table of the resulting glyph vector to the console. |
| 8 |
If graphite has been built with -DTRACING then it will also produce a |
| 9 |
diagnostic log of the segment creation in grSegmentLog.txt |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <cstdio> |
| 13 |
#include <cstdlib> |
| 14 |
#include <cassert> |
| 15 |
#include <climits> |
| 16 |
#include <iomanip> |
| 17 |
#include <cstring> |
| 18 |
|
| 19 |
#include "UtfCodec.h" |
| 20 |
|
| 21 |
#include "graphite2/Types.h" |
| 22 |
#include "graphite2/Segment.h" |
| 23 |
#include "graphite2/Log.h" |
| 24 |
|
| 25 |
class Gr2TextSrc |
| 26 |
{ |
| 27 |
|
| 28 |
public: |
| 29 |
Gr2TextSrc(const gr_uint32* base, size_t len) : m_buff(base), m_len(len) { } |
| 30 |
gr_encform utfEncodingForm() const { return gr_utf32; } |
| 31 |
size_t getLength() const { return m_len; } |
| 32 |
const void* get_utf_buffer_begin() const { return m_buff; } |
| 33 |
|
| 34 |
private: |
| 35 |
const gr_uint32* m_buff; |
| 36 |
size_t m_len; |
| 37 |
}; |
| 38 |
|
| 39 |
#ifndef HAVE_STRTOF |
| 40 |
float strtof(char * text, char ** /*ignore*/) |
| 41 |
{ |
| 42 |
return static_cast<float>(atof(text)); |
| 43 |
} |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifndef HAVE_STRTOL |
| 47 |
long strtol(char * text, char ** /*ignore*/) |
| 48 |
{ |
| 49 |
return atol(text); |
| 50 |
} |
| 51 |
#endif |
| 52 |
|
| 53 |
class Parameters |
| 54 |
{ |
| 55 |
public: |
| 56 |
Parameters(); |
| 57 |
~Parameters(); |
| 58 |
void clear(); |
| 59 |
void closeLog(); |
| 60 |
bool loadFromArgs(int argc, char *argv[]); |
| 61 |
int testFileFont() const; |
| 62 |
gr_feature_val* parseFeatures(const gr_face * face) const; |
| 63 |
void printFeatures(const gr_face * face) const; |
| 64 |
public: |
| 65 |
const char * fileName; |
| 66 |
const char * features; |
| 67 |
float pointSize; |
| 68 |
int dpi; |
| 69 |
bool lineStart; |
| 70 |
bool lineEnd; |
| 71 |
bool ws; |
| 72 |
bool rtl; |
| 73 |
bool useLineFill; |
| 74 |
bool noprint; |
| 75 |
int useCodes; |
| 76 |
bool autoCodes; |
| 77 |
int justification; |
| 78 |
float width; |
| 79 |
int textArgIndex; |
| 80 |
unsigned int * pText32; |
| 81 |
size_t charLength; |
| 82 |
size_t offset; |
| 83 |
FILE * log; |
| 84 |
char * trace; |
| 85 |
char * alltrace; |
| 86 |
int codesize; |
| 87 |
gr_face_options opts; |
| 88 |
|
| 89 |
private : //defensive since log should not be copied |
| 90 |
Parameters(const Parameters&); |
| 91 |
Parameters& operator=(const Parameters&); |
| 92 |
}; |
| 93 |
|
| 94 |
Parameters::Parameters() |
| 95 |
{ |
| 96 |
log = stdout ; |
| 97 |
clear(); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
Parameters::~Parameters() |
| 102 |
{ |
| 103 |
free(pText32); |
| 104 |
pText32 = NULL; |
| 105 |
closeLog(); |
| 106 |
} |
| 107 |
|
| 108 |
void Parameters::clear() |
| 109 |
{ |
| 110 |
closeLog() ; |
| 111 |
fileName = ""; |
| 112 |
features = nullptr; |
| 113 |
pointSize = 12.0f; |
| 114 |
dpi = 72; |
| 115 |
lineStart = false; |
| 116 |
lineEnd = false; |
| 117 |
rtl = false; |
| 118 |
ws = false; |
| 119 |
useLineFill = false; |
| 120 |
useCodes = 0; |
| 121 |
autoCodes = false; |
| 122 |
noprint = false; |
| 123 |
justification = 0; |
| 124 |
width = 100.0f; |
| 125 |
pText32 = NULL; |
| 126 |
textArgIndex = 0; |
| 127 |
charLength = 0; |
| 128 |
codesize = 4; |
| 129 |
offset = 0; |
| 130 |
log = stdout; |
| 131 |
trace = NULL; |
| 132 |
alltrace = NULL; |
| 133 |
opts = gr_face_preloadAll; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void Parameters::closeLog() |
| 138 |
{ |
| 139 |
if (log==stdout) |
| 140 |
return ; |
| 141 |
|
| 142 |
fclose(log); |
| 143 |
log = stdout; |
| 144 |
} |
| 145 |
|
| 146 |
int lookup(size_t *map, size_t val); |
| 147 |
|
| 148 |
namespace gr2 = graphite2; |
| 149 |
|
| 150 |
template <typename utf> |
| 151 |
size_t convertUtf(const void * src, unsigned int * & dest) |
| 152 |
{ |
| 153 |
dest = static_cast<unsigned int *>(malloc(sizeof(unsigned int)*(strlen(reinterpret_cast<const char *>(src))+1))); |
| 154 |
if (!dest) |
| 155 |
return 0; |
| 156 |
|
| 157 |
typename utf::const_iterator ui = src; |
| 158 |
unsigned int * out = dest; |
| 159 |
while ((*out = *ui) != 0 && !ui.error()) |
| 160 |
{ |
| 161 |
++ui; |
| 162 |
++out; |
| 163 |
} |
| 164 |
|
| 165 |
if (ui.error()) |
| 166 |
{ |
| 167 |
free(dest); |
| 168 |
dest = 0; |
| 169 |
return size_t(-1); |
| 170 |
} |
| 171 |
|
| 172 |
return (out-dest); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
bool Parameters::loadFromArgs(int argc, char *argv[]) |
| 177 |
{ |
| 178 |
int mainArgOffset = 0; |
| 179 |
pText32 = NULL; |
| 180 |
features = NULL; |
| 181 |
log = stdout; |
| 182 |
codesize = 4; |
| 183 |
bool argError = false; |
| 184 |
char* pText = NULL; |
| 185 |
typedef enum |
| 186 |
{ |
| 187 |
NONE, |
| 188 |
POINT_SIZE, |
| 189 |
DPI, |
| 190 |
JUSTIFY, |
| 191 |
LINE_START, |
| 192 |
LINE_END, |
| 193 |
LINE_FILL, |
| 194 |
CODES, |
| 195 |
AUTOCODES, |
| 196 |
FEAT, |
| 197 |
LOG, |
| 198 |
TRACE, |
| 199 |
ALLTRACE, |
| 200 |
SIZE |
| 201 |
} TestOptions; |
| 202 |
TestOptions option = NONE; |
| 203 |
char * pIntEnd = NULL; |
| 204 |
char * pFloatEnd = NULL; |
| 205 |
long lTestSize = 0; |
| 206 |
float fTestSize = 0.0f; |
| 207 |
for (int a = 1; a < argc; a++) |
| 208 |
{ |
| 209 |
switch (option) |
| 210 |
{ |
| 211 |
case DPI: |
| 212 |
pIntEnd = NULL; |
| 213 |
lTestSize = strtol(argv[a],&pIntEnd, 10); |
| 214 |
if (lTestSize > 0 && lTestSize < INT_MAX && lTestSize != LONG_MAX) |
| 215 |
{ |
| 216 |
dpi = lTestSize; |
| 217 |
} |
| 218 |
else |
| 219 |
{ |
| 220 |
fprintf(stderr,"Invalid dpi %s\n", argv[a]); |
| 221 |
} |
| 222 |
option = NONE; |
| 223 |
break; |
| 224 |
case POINT_SIZE: |
| 225 |
pFloatEnd = NULL; |
| 226 |
fTestSize = strtof(argv[a],&pFloatEnd); |
| 227 |
// what is a reasonable maximum here |
| 228 |
if (fTestSize > 0 && fTestSize < 5000.0f) |
| 229 |
{ |
| 230 |
pointSize = fTestSize; |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
fprintf(stderr,"Invalid point size %s\n", argv[a]); |
| 235 |
argError = true; |
| 236 |
} |
| 237 |
option = NONE; |
| 238 |
break; |
| 239 |
case JUSTIFY: |
| 240 |
pIntEnd = NULL; |
| 241 |
justification = strtol(argv[a], &pIntEnd, 10); |
| 242 |
if (justification <= 0) |
| 243 |
{ |
| 244 |
fprintf(stderr, "Justification value must be > 0 but was %s\n", argv[a]); |
| 245 |
justification = 0; |
| 246 |
} |
| 247 |
option = NONE; |
| 248 |
break; |
| 249 |
case LINE_FILL: |
| 250 |
pFloatEnd = NULL; |
| 251 |
fTestSize = strtof(argv[a],&pFloatEnd); |
| 252 |
// what is a good max width? |
| 253 |
if (fTestSize > 0 && fTestSize < 10000) |
| 254 |
{ |
| 255 |
width = fTestSize; |
| 256 |
} |
| 257 |
else |
| 258 |
{ |
| 259 |
fprintf(stderr,"Invalid line width %s\n", argv[a]); |
| 260 |
argError = true; |
| 261 |
} |
| 262 |
option = NONE; |
| 263 |
break; |
| 264 |
case FEAT: |
| 265 |
features = argv[a]; |
| 266 |
option = NONE; |
| 267 |
break; |
| 268 |
case LOG: |
| 269 |
closeLog(); |
| 270 |
log = fopen(argv[a], "w"); |
| 271 |
if (log == NULL) |
| 272 |
{ |
| 273 |
fprintf(stderr,"Failed to open %s\n", argv[a]); |
| 274 |
log = stdout; |
| 275 |
} |
| 276 |
option = NONE; |
| 277 |
break; |
| 278 |
case TRACE: |
| 279 |
trace = argv[a]; |
| 280 |
option = NONE; |
| 281 |
break; |
| 282 |
case ALLTRACE: |
| 283 |
alltrace = argv[a]; |
| 284 |
option = NONE; |
| 285 |
break; |
| 286 |
case SIZE : |
| 287 |
pIntEnd = NULL; |
| 288 |
codesize = strtol(argv[a],&pIntEnd, 10); |
| 289 |
option = NONE; |
| 290 |
break; |
| 291 |
default: |
| 292 |
option = NONE; |
| 293 |
if (argv[a][0] == '-') |
| 294 |
{ |
| 295 |
if (strcmp(argv[a], "-pt") == 0) |
| 296 |
{ |
| 297 |
option = POINT_SIZE; |
| 298 |
} |
| 299 |
else if (strcmp(argv[a], "-dpi") == 0) |
| 300 |
{ |
| 301 |
option = DPI; |
| 302 |
} |
| 303 |
else if (strcmp(argv[a], "-ls") == 0) |
| 304 |
{ |
| 305 |
option = NONE; |
| 306 |
lineStart = true; |
| 307 |
} |
| 308 |
else if (strcmp(argv[a], "-le") == 0) |
| 309 |
{ |
| 310 |
option = NONE; |
| 311 |
lineEnd = true; |
| 312 |
} |
| 313 |
else if (strcmp(argv[a], "-le") == 0) |
| 314 |
{ |
| 315 |
option = NONE; |
| 316 |
lineEnd = true; |
| 317 |
} |
| 318 |
else if (strcmp(argv[a], "-rtl") == 0) |
| 319 |
{ |
| 320 |
option = NONE; |
| 321 |
rtl = true; |
| 322 |
} |
| 323 |
else if (strcmp(argv[a], "-ws") == 0) |
| 324 |
{ |
| 325 |
option = NONE; |
| 326 |
ws = true; |
| 327 |
} |
| 328 |
else if (strcmp(argv[a], "-feat") == 0) |
| 329 |
{ |
| 330 |
option = FEAT; |
| 331 |
} |
| 332 |
else if (strcmp(argv[a], "-bytes") == 0) |
| 333 |
{ |
| 334 |
option = SIZE; |
| 335 |
} |
| 336 |
else if (strcmp(argv[a], "-noprint") == 0) |
| 337 |
{ |
| 338 |
noprint = true; |
| 339 |
} |
| 340 |
else if (strcmp(argv[a], "-codes") == 0) |
| 341 |
{ |
| 342 |
option = NONE; |
| 343 |
useCodes = 4; |
| 344 |
// must be less than argc |
| 345 |
//pText32 = new unsigned int[argc]; |
| 346 |
pText32 = (unsigned int *)malloc(sizeof(unsigned int) * argc); |
| 347 |
fprintf(log, "Text codes\n"); |
| 348 |
} |
| 349 |
else if (strcmp(argv[a], "-auto") == 0) |
| 350 |
{ |
| 351 |
const unsigned kCodeLimit = 0x1000; |
| 352 |
charLength = kCodeLimit - 1; |
| 353 |
pText32 = (unsigned int *)malloc(sizeof(unsigned int) * kCodeLimit); |
| 354 |
unsigned int i; |
| 355 |
for (i = 1; i < kCodeLimit; ++i) |
| 356 |
pText32[i - 1] = i; |
| 357 |
pText32[charLength] = 0; |
| 358 |
autoCodes = true; |
| 359 |
option = NONE; |
| 360 |
} |
| 361 |
else if (strcmp(argv[a], "-linefill") == 0) |
| 362 |
{ |
| 363 |
option = LINE_FILL; |
| 364 |
useLineFill = true; |
| 365 |
} |
| 366 |
else if (strcmp(argv[a], "-j") == 0) |
| 367 |
{ |
| 368 |
option = JUSTIFY; |
| 369 |
} |
| 370 |
else if (strcmp(argv[a], "-log") == 0) |
| 371 |
{ |
| 372 |
option = LOG; |
| 373 |
} |
| 374 |
else if (strcmp(argv[a], "-trace") == 0) |
| 375 |
{ |
| 376 |
option = TRACE; |
| 377 |
} |
| 378 |
else if (strcmp(argv[a], "-alltrace") == 0) |
| 379 |
{ |
| 380 |
option = ALLTRACE; |
| 381 |
} |
| 382 |
else if (strcmp(argv[a], "-demand") == 0) |
| 383 |
{ |
| 384 |
option = NONE; |
| 385 |
opts = gr_face_default; |
| 386 |
} |
| 387 |
else |
| 388 |
{ |
| 389 |
argError = true; |
| 390 |
fprintf(stderr,"Unknown option %s\n",argv[a]); |
| 391 |
} |
| 392 |
} |
| 393 |
else if (mainArgOffset == 0) |
| 394 |
{ |
| 395 |
fileName = argv[a]; |
| 396 |
mainArgOffset++; |
| 397 |
} |
| 398 |
else if (useCodes) |
| 399 |
{ |
| 400 |
pIntEnd = NULL; |
| 401 |
mainArgOffset++; |
| 402 |
unsigned int code = strtol(argv[a],&pIntEnd, 16); |
| 403 |
if (code > 0) |
| 404 |
{ |
| 405 |
// convert text to utfOut using iconv because its easier to debug string placements |
| 406 |
assert(pText32); |
| 407 |
pText32[charLength++] = code; |
| 408 |
if (charLength % 10 == 0) |
| 409 |
fprintf(log, "%4x\n",code); |
| 410 |
else |
| 411 |
fprintf(log, "%4x\t",code); |
| 412 |
} |
| 413 |
else |
| 414 |
{ |
| 415 |
fprintf(stderr,"Invalid dpi %s\n", argv[a]); |
| 416 |
} |
| 417 |
} |
| 418 |
else if (mainArgOffset == 1) |
| 419 |
{ |
| 420 |
mainArgOffset++; |
| 421 |
pText = argv[a]; |
| 422 |
textArgIndex = a; |
| 423 |
} |
| 424 |
else |
| 425 |
{ |
| 426 |
argError = true; |
| 427 |
fprintf(stderr,"too many arguments %s\n",argv[a]); |
| 428 |
} |
| 429 |
break; |
| 430 |
} |
| 431 |
} |
| 432 |
if (mainArgOffset < 1) argError = true; |
| 433 |
else if (mainArgOffset > 1) |
| 434 |
{ |
| 435 |
if (!autoCodes && !useCodes && pText != NULL) |
| 436 |
{ |
| 437 |
charLength = convertUtf<gr2::utf8>(pText, pText32); |
| 438 |
if (!pText32) |
| 439 |
{ |
| 440 |
if (charLength == ~0) |
| 441 |
perror("decoding utf-8 data failed"); |
| 442 |
else |
| 443 |
perror("insufficent memory for text buffer"); |
| 444 |
} |
| 445 |
fprintf(log, "String has %d characters\n", (int)charLength); |
| 446 |
size_t ci; |
| 447 |
for (ci = 0; ci < 10 && ci < charLength; ci++) |
| 448 |
{ |
| 449 |
fprintf(log, "%d\t", (int)ci); |
| 450 |
} |
| 451 |
fprintf(log, "\n"); |
| 452 |
for (ci = 0; ci < charLength; ci++) |
| 453 |
{ |
| 454 |
fprintf(log, "%04x\t", (int)ci); |
| 455 |
if (((ci + 1) % 10) == 0) |
| 456 |
fprintf(log, "\n"); |
| 457 |
} |
| 458 |
fprintf(log, "\n"); |
| 459 |
} |
| 460 |
else |
| 461 |
{ |
| 462 |
assert(pText32); |
| 463 |
pText32[charLength] = 0; |
| 464 |
fprintf(log, "\n"); |
| 465 |
} |
| 466 |
} |
| 467 |
return (argError) ? false : true; |
| 468 |
} |
| 469 |
|
| 470 |
typedef gr_uint32 tag_t; |
| 471 |
|
| 472 |
void Parameters::printFeatures(const gr_face * face) const |
| 473 |
{ |
| 474 |
gr_uint16 numFeatures = gr_face_n_fref(face); |
| 475 |
fprintf(log, "%d features\n", numFeatures); |
| 476 |
gr_uint16 langId = 0x0409; |
| 477 |
for (gr_uint16 i = 0; i < numFeatures; i++) |
| 478 |
{ |
| 479 |
const gr_feature_ref * f = gr_face_fref(face, i); |
| 480 |
gr_uint32 length = 0; |
| 481 |
char * label = reinterpret_cast<char *>(gr_fref_label(f, &langId, gr_utf8, &length)); |
| 482 |
const tag_t featId = gr_fref_id(f); |
| 483 |
if (label) |
| 484 |
if ((char(featId >> 24) >= 0x20 && char(featId >> 24) < 0x7F) && |
| 485 |
(char(featId >> 16) >= 0x20 && char(featId >> 16) < 0x7F) && |
| 486 |
(char(featId >> 8) >= 0x20 && char(featId >> 8) < 0x7F) && |
| 487 |
(char(featId) >= 0x20 && char(featId) < 0x7F)) |
| 488 |
{ |
| 489 |
fprintf(log, "%d %c%c%c%c %s\n", featId, featId >> 24, featId >> 16, featId >> 8, featId, label); |
| 490 |
} |
| 491 |
else |
| 492 |
{ |
| 493 |
fprintf(log, "%d %s\n", featId, label); |
| 494 |
} |
| 495 |
else |
| 496 |
fprintf(log, "%d\n", featId); |
| 497 |
gr_label_destroy(reinterpret_cast<void*>(label)); |
| 498 |
gr_uint16 numSettings = gr_fref_n_values(f); |
| 499 |
for (gr_uint16 j = 0; j < numSettings; j++) |
| 500 |
{ |
| 501 |
gr_int16 value = gr_fref_value(f, j); |
| 502 |
label = reinterpret_cast<char *>(gr_fref_value_label |
| 503 |
(f, j, &langId, gr_utf8, &length)); |
| 504 |
fprintf(log, "\t%d\t%s\n", value, label); |
| 505 |
gr_label_destroy(reinterpret_cast<void*>(label)); |
| 506 |
} |
| 507 |
} |
| 508 |
gr_uint16 numLangs = gr_face_n_languages(face); |
| 509 |
fprintf(log, "Feature Languages:"); |
| 510 |
for (gr_uint16 i = 0; i < numLangs; i++) |
| 511 |
{ |
| 512 |
const tag_t lang_id = gr_face_lang_by_index(face, i); |
| 513 |
fprintf(log, "\t"); |
| 514 |
for (size_t j = 4; j; --j) |
| 515 |
{ |
| 516 |
const unsigned char c = lang_id >> (j*8-8); |
| 517 |
if ((c >= 0x20) && (c < 0x80)) |
| 518 |
fprintf(log, "%c", c); |
| 519 |
} |
| 520 |
} |
| 521 |
fprintf(log, "\n"); |
| 522 |
} |
| 523 |
|
| 524 |
gr_feature_val * Parameters::parseFeatures(const gr_face * face) const |
| 525 |
{ |
| 526 |
gr_feature_val * featureList = NULL; |
| 527 |
const char * pLang = NULL; |
| 528 |
tag_t lang_id = 0; |
| 529 |
if (features && (pLang = strstr(features, "lang="))) |
| 530 |
{ |
| 531 |
pLang += 5; |
| 532 |
for (int i = 4; i; --i) |
| 533 |
{ |
| 534 |
lang_id <<= 8; |
| 535 |
if (!*pLang || *pLang == '0' || *pLang == '&') continue; |
| 536 |
lang_id |= *pLang++; |
| 537 |
} |
| 538 |
} |
| 539 |
featureList = gr_face_featureval_for_lang(face, lang_id); |
| 540 |
if (!features || strlen(features) == 0) |
| 541 |
return featureList; |
| 542 |
size_t featureLength = strlen(features); |
| 543 |
const char * name = features; |
| 544 |
const char * valueText = NULL; |
| 545 |
size_t nameLength = 0; |
| 546 |
gr_int32 value = 0; |
| 547 |
const gr_feature_ref* ref = NULL; |
| 548 |
tag_t feat_id = 0; |
| 549 |
for (size_t i = 0; i < featureLength; i++) |
| 550 |
{ |
| 551 |
switch (features[i]) |
| 552 |
{ |
| 553 |
case ',': |
| 554 |
case '&': |
| 555 |
assert(valueText); |
| 556 |
value = atoi(valueText); |
| 557 |
if (ref) |
| 558 |
{ |
| 559 |
gr_fref_set_feature_value(ref, value, featureList); |
| 560 |
ref = NULL; |
| 561 |
} |
| 562 |
valueText = NULL; |
| 563 |
name = features + i + 1; |
| 564 |
nameLength = 0; |
| 565 |
feat_id = 0; |
| 566 |
break; |
| 567 |
case '=': |
| 568 |
if (nameLength <= 4) |
| 569 |
{ |
| 570 |
ref = gr_face_find_fref(face, feat_id); |
| 571 |
} |
| 572 |
if (!ref) |
| 573 |
{ |
| 574 |
assert(name); |
| 575 |
feat_id = atoi(name); |
| 576 |
ref = gr_face_find_fref(face, feat_id); |
| 577 |
} |
| 578 |
valueText = features + i + 1; |
| 579 |
name = NULL; |
| 580 |
break; |
| 581 |
default: |
| 582 |
if (valueText == NULL) |
| 583 |
{ |
| 584 |
if (nameLength < 4) |
| 585 |
feat_id = feat_id << 8 | features[i]; |
| 586 |
} |
| 587 |
break; |
| 588 |
} |
| 589 |
if (ref) |
| 590 |
{ |
| 591 |
assert(valueText); |
| 592 |
value = atoi(valueText); |
| 593 |
gr_fref_set_feature_value(ref, value, featureList); |
| 594 |
if (feat_id > 0x20000000) |
| 595 |
{ |
| 596 |
fprintf(log, "%c%c%c%c=%d\n", feat_id >> 24, feat_id >> 16, feat_id >> 8, feat_id, value); |
| 597 |
} |
| 598 |
else |
| 599 |
fprintf(log, "%u=%d\n", feat_id, value); |
| 600 |
ref = NULL; |
| 601 |
} |
| 602 |
} |
| 603 |
return featureList; |
| 604 |
} |
| 605 |
|
| 606 |
int Parameters::testFileFont() const |
| 607 |
{ |
| 608 |
int returnCode = 0; |
| 609 |
// try |
| 610 |
{ |
| 611 |
gr_face *face = NULL; |
| 612 |
FILE * testfile = fopen(fileName, "rb"); |
| 613 |
if (!testfile) |
| 614 |
{ |
| 615 |
fprintf(stderr, "Unable to open font file\n"); |
| 616 |
return 4; |
| 617 |
} |
| 618 |
else |
| 619 |
fclose(testfile); |
| 620 |
|
| 621 |
if (alltrace) gr_start_logging(NULL, alltrace); |
| 622 |
face = gr_make_file_face(fileName, opts); |
| 623 |
|
| 624 |
// use the -trace option to specify a file |
| 625 |
if (trace) gr_start_logging(face, trace); |
| 626 |
|
| 627 |
if (!face) |
| 628 |
{ |
| 629 |
fprintf(stderr, "Invalid font, failed to read or parse tables\n"); |
| 630 |
return 3; |
| 631 |
} |
| 632 |
if (charLength == 0) |
| 633 |
{ |
| 634 |
printFeatures(face); |
| 635 |
gr_stop_logging(face); |
| 636 |
gr_face_destroy(face); |
| 637 |
return 0; |
| 638 |
} |
| 639 |
|
| 640 |
gr_font *sizedFont = gr_make_font(pointSize * dpi / 72, face); |
| 641 |
gr_feature_val * featureList = NULL; |
| 642 |
Gr2TextSrc textSrc(pText32, charLength); |
| 643 |
gr_segment* pSeg = NULL; |
| 644 |
if (features) |
| 645 |
featureList = parseFeatures(face); |
| 646 |
if (codesize == 2) |
| 647 |
{ |
| 648 |
unsigned short *pText16 = (unsigned short *)malloc((textSrc.getLength() * 2 + 1) * sizeof(unsigned short)); |
| 649 |
gr2::utf16::iterator ui = pText16; |
| 650 |
unsigned int *p = pText32; |
| 651 |
for (unsigned int i = 0; i < textSrc.getLength(); ++i) |
| 652 |
{ |
| 653 |
*ui = *p++; |
| 654 |
ui++; |
| 655 |
} |
| 656 |
*ui = 0; |
| 657 |
pSeg = gr_make_seg(sizedFont, face, 0, features ? featureList : NULL, (gr_encform)codesize, pText16, textSrc.getLength(), rtl ? 1 : 0); |
| 658 |
} |
| 659 |
else if (codesize == 1) |
| 660 |
{ |
| 661 |
unsigned char *pText8 = (unsigned char *)malloc((textSrc.getLength() + 1) * 4); |
| 662 |
gr2::utf8::iterator ui = pText8; |
| 663 |
unsigned int *p = pText32; |
| 664 |
for (unsigned int i = 0; i < textSrc.getLength(); ++i) |
| 665 |
{ |
| 666 |
*ui = *p++; |
| 667 |
ui++; |
| 668 |
} |
| 669 |
*ui = 0; |
| 670 |
pSeg = gr_make_seg(sizedFont, face, 0, features ? featureList : NULL, (gr_encform)codesize, pText8, textSrc.getLength(), rtl ? 1 : 0); |
| 671 |
free(pText8); |
| 672 |
} |
| 673 |
else |
| 674 |
pSeg = gr_make_seg(sizedFont, face, 0, features ? featureList : NULL, textSrc.utfEncodingForm(), |
| 675 |
textSrc.get_utf_buffer_begin(), textSrc.getLength(), rtl ? 1 : 0); |
| 676 |
|
| 677 |
if (pSeg && !noprint) |
| 678 |
{ |
| 679 |
int i = 0; |
| 680 |
float advanceWidth; |
| 681 |
#ifndef NDEBUG |
| 682 |
int numSlots = gr_seg_n_slots(pSeg); |
| 683 |
#endif |
| 684 |
// size_t *map = new size_t [seg.length() + 1]; |
| 685 |
if (justification > 0) |
| 686 |
advanceWidth = gr_seg_justify(pSeg, gr_seg_first_slot(pSeg), sizedFont, gr_seg_advance_X(pSeg) * justification / 100., gr_justCompleteLine, NULL, NULL); |
| 687 |
else |
| 688 |
advanceWidth = gr_seg_advance_X(pSeg); |
| 689 |
size_t *map = (size_t*)malloc((gr_seg_n_slots(pSeg) + 1) * sizeof(size_t)); |
| 690 |
for (const gr_slot* slot = gr_seg_first_slot(pSeg); slot; slot = gr_slot_next_in_segment(slot), ++i) |
| 691 |
{ map[i] = (size_t)slot; } |
| 692 |
map[i] = 0; |
| 693 |
fprintf(log, "Segment length: %d\n", gr_seg_n_slots(pSeg)); |
| 694 |
fprintf(log, "pos gid attach\t x\t y\tins bw\t chars\t\tUnicode\t"); |
| 695 |
fprintf(log, "\n"); |
| 696 |
i = 0; |
| 697 |
for (const gr_slot* slot = gr_seg_first_slot(pSeg); slot; slot = gr_slot_next_in_segment(slot), ++i) |
| 698 |
{ |
| 699 |
// consistency check for last slot |
| 700 |
assert((i + 1 < numSlots) || (slot == gr_seg_last_slot(pSeg))); |
| 701 |
float orgX = gr_slot_origin_X(slot); |
| 702 |
float orgY = gr_slot_origin_Y(slot); |
| 703 |
const gr_char_info *cinfo = gr_seg_cinfo(pSeg, gr_slot_original(slot)); |
| 704 |
fprintf(log, "%02d %4d %3d@%d,%d\t%6.1f\t%6.1f\t%2d%4d\t%3d %3d\t", |
| 705 |
i, gr_slot_gid(slot), lookup(map, (size_t)gr_slot_attached_to(slot)), |
| 706 |
gr_slot_attr(slot, pSeg, gr_slatAttX, 0), |
| 707 |
gr_slot_attr(slot, pSeg, gr_slatAttY, 0), orgX, orgY, gr_slot_can_insert_before(slot) ? 1 : 0, |
| 708 |
cinfo ? gr_cinfo_break_weight(cinfo) : 0, gr_slot_before(slot), gr_slot_after(slot)); |
| 709 |
|
| 710 |
if (pText32 != NULL && gr_slot_before(slot) + offset < charLength |
| 711 |
&& gr_slot_after(slot) + offset < charLength) |
| 712 |
{ |
| 713 |
fprintf(log, "%7x\t%7x", |
| 714 |
pText32[gr_slot_before(slot) + offset], |
| 715 |
pText32[gr_slot_after(slot) + offset]); |
| 716 |
} |
| 717 |
fprintf(log, "\n"); |
| 718 |
} |
| 719 |
assert(i == numSlots); |
| 720 |
// assign last point to specify advance of the whole array |
| 721 |
// position arrays must be one bigger than what countGlyphs() returned |
| 722 |
fprintf(log, "Advance width = %6.1f\n", advanceWidth); |
| 723 |
unsigned int numchar = gr_seg_n_cinfo(pSeg); |
| 724 |
fprintf(log, "\nChar\tUnicode\tBefore\tAfter\tBase\n"); |
| 725 |
for (unsigned int j = 0; j < numchar; j++) |
| 726 |
{ |
| 727 |
const gr_char_info *c = gr_seg_cinfo(pSeg, j); |
| 728 |
fprintf(log, "%d\t%04X\t%d\t%d\t%d\n", j, gr_cinfo_unicode_char(c), gr_cinfo_before(c), gr_cinfo_after(c), int(gr_cinfo_base(c))); |
| 729 |
} |
| 730 |
free(map); |
| 731 |
} |
| 732 |
if (pSeg) |
| 733 |
gr_seg_destroy(pSeg); |
| 734 |
if (featureList) gr_featureval_destroy(featureList); |
| 735 |
gr_font_destroy(sizedFont); |
| 736 |
if (trace) gr_stop_logging(face); |
| 737 |
gr_face_destroy(face); |
| 738 |
if (alltrace) gr_stop_logging(NULL); |
| 739 |
} |
| 740 |
return returnCode; |
| 741 |
} |
| 742 |
|
| 743 |
int lookup(size_t *map, size_t val) |
| 744 |
{ |
| 745 |
int i = 0; |
| 746 |
for ( ; map[i] != val && map[i]; i++) {} |
| 747 |
return map[i] ? i : -1; |
| 748 |
} |
| 749 |
|
| 750 |
int main(int argc, char *argv[]) |
| 751 |
{ |
| 752 |
|
| 753 |
Parameters parameters; |
| 754 |
|
| 755 |
if (!parameters.loadFromArgs(argc, argv)) |
| 756 |
{ |
| 757 |
fprintf(stderr,"Usage: %s [options] fontfile utf8text \n",argv[0]); |
| 758 |
fprintf(stderr,"Options: (default in brackets)\n"); |
| 759 |
fprintf(stderr,"-dpi d\tDots per Inch (72)\n"); |
| 760 |
fprintf(stderr,"-pt d\tPoint size (12)\n"); |
| 761 |
fprintf(stderr,"-codes\tEnter text as hex code points instead of utf8 (false)\n"); |
| 762 |
fprintf(stderr,"\te.g. %s font.ttf -codes 1000 102f\n",argv[0]); |
| 763 |
fprintf(stderr,"-auto\tAutomatically generate a test string of all codes 1-0xFFF\n"); |
| 764 |
fprintf(stderr,"-noprint\tDon't print results\n"); |
| 765 |
//fprintf(stderr,"-ls\tStart of line = true (false)\n"); |
| 766 |
//fprintf(stderr,"-le\tEnd of line = true (false)\n"); |
| 767 |
fprintf(stderr,"-rtl\tRight to left = true (false)\n"); |
| 768 |
fprintf(stderr,"-j percentage\tJustify to percentage of string width\n"); |
| 769 |
//fprintf(stderr,"-ws\tAllow trailing whitespace = true (false)\n"); |
| 770 |
//fprintf(stderr,"-linefill w\tuse a LineFillSegment of width w (RangeSegment)\n"); |
| 771 |
fprintf(stderr,"\nIf a font, but no text is specified, then a list of features will be shown.\n"); |
| 772 |
fprintf(stderr,"-feat f=g\tSet feature f to value g. Separate multiple features with ,\n"); |
| 773 |
fprintf(stderr,"-log out.log\tSet log file to use rather than stdout\n"); |
| 774 |
fprintf(stderr,"-trace trace.json\tDefine a file for the JSON trace log\n"); |
| 775 |
fprintf(stderr,"-demand\tDemand load glyphs and cmap cache\n"); |
| 776 |
fprintf(stderr,"-bytes\tword size for character transfer [1,2,4] defaults to 4\n"); |
| 777 |
return 1; |
| 778 |
} |
| 779 |
return parameters.testFileFont(); |
| 780 |
} |