00001 #include "mex.h"
00002 #include <math.h>
00003 #include <unistd.h>
00004 #include "mexhead.h"
00005 #include "Doc/text_on_image_font_helper_docstring.h"
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #define NARGIN_MIN 2
00066 #define NARGIN_MAX 4
00067 #define NARGOUT_MIN 0
00068 #define NARGOUT_MAX 2
00069
00070 #define ARG_font 0
00071 #define ARG_dict 1
00072 #define ARG_height 2
00073 #define ARG_width 3
00074
00075 #define ARG_bm 0
00076 #define ARG_cpos 1
00077
00078 static const char *progname = "text_on_image_font_helper";
00079 #define PROGNAME text_on_image_font_helper
00080 static const char *in_specs[NARGIN_MAX] = {
00081 "SV",
00082 "SV",
00083 "IM(0,0)|IV(1)|IV(2)|IV(3)|IV(4)",
00084 "IM(0,0)|IV(1)|IV(2)|IV(3)",
00085 };
00086 static const char *in_names[NARGIN_MAX] = {
00087 "font",
00088 "dict",
00089 "height",
00090 "width",
00091 };
00092 static const char *out_names[NARGOUT_MAX] = {
00093 "bm", "cw" };
00094
00095
00096
00097
00098
00099
00100
00101 #define STB_TRUETYPE_IMPLEMENTATION
00102 #include "stb_truetype.h"
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 static
00169 char *
00170 open_font(char *fontfile,
00171 stbtt_fontinfo **fontP,
00172 unsigned char **ttf_bufP
00173 )
00174 {
00175 FILE *fp;
00176 const size_t BlockSize = 1<<20;
00177 int blocks;
00178 int offset;
00179
00180 unsigned char *ttf_buf;
00181 stbtt_fontinfo *font;
00182
00183
00184 *fontP = NULL;
00185 *ttf_bufP = NULL;
00186
00187
00188 if ((fp = fopen(fontfile, "rb")) == NULL) {
00189 return "Could not open font file";
00190 }
00191 if ((ttf_buf = malloc(1 * BlockSize)) == NULL)
00192 return "Failed to allocate buffer for file";
00193 for (blocks = 0; !feof(fp); blocks++)
00194 fread(ttf_buf, BlockSize, 1, fp);
00195 if (blocks > 1) {
00196
00197 rewind(fp);
00198 free(ttf_buf);
00199 if ((ttf_buf = malloc(blocks*BlockSize)) == NULL)
00200 return "Failed to reallocate buffer for file";
00201 fread(ttf_buf, BlockSize, (size_t) blocks, fp);
00202 }
00203 fclose(fp);
00204
00205
00206 if ((offset = stbtt_GetFontOffsetForIndex(ttf_buf, 0)) < 0) {
00207 free(ttf_buf);
00208 return "Truetype problem: FontOffset (weird ttc?)";
00209 }
00210
00211 if ((font = malloc(sizeof(stbtt_fontinfo))) == NULL) {
00212 free(ttf_buf);
00213 return "Failed to allocate font object";
00214 }
00215 if (stbtt_InitFont(font, ttf_buf, offset) == 0) {
00216 free(font);
00217 free(ttf_buf);
00218 return "Truetype problem: InitFont";
00219 }
00220
00221
00222 *fontP = font;
00223 *ttf_bufP = ttf_buf;
00224 return NULL;
00225 }
00226
00227
00228
00229
00230
00231 static char *
00232 find_scale(stbtt_fontinfo *font,
00233
00234 int *height_dimP,
00235 int *width_dimP,
00236 int *baselineP,
00237 float *scaleP,
00238
00239 int height_char,
00240 int add_descenders,
00241 int height_pad_top,
00242 int height_pad_bot,
00243
00244 int width_reference,
00245 int width_pad_left,
00246 int width_pad_right)
00247 {
00248
00249 int height_dim, width_dim, baseline;
00250 float scale;
00251
00252 int ascent, descent;
00253 int advanceWidth, leftSideBearing;
00254 int width_char;
00255
00256
00257
00258
00259 stbtt_GetFontVMetrics(font, &ascent, &descent, NULL);
00260 if (add_descenders) {
00261 scale = (float) height_char / (ascent - descent);
00262 baseline = height_pad_top + ceil(scale * ascent);
00263 } else {
00264 scale = (float) height_char / ascent;
00265 baseline = height_pad_top + height_char;
00266 }
00267
00268 height_dim = height_char + height_pad_top + height_pad_bot;
00269
00270 if (height_dim <= 0 || baseline < 0)
00271 return "Error finding height dimensions";
00272
00273
00274 stbtt_GetCodepointHMetrics(font, width_reference, &advanceWidth, &leftSideBearing);
00275 if (0)
00276 printf("advanceWidth = %d, lsb = %d, scale = %f\n",
00277 advanceWidth, leftSideBearing, scale);
00278 width_char = (int) ceil(advanceWidth * scale);
00279
00280 width_dim = width_char + width_pad_left + width_pad_right;
00281 if (0)
00282 printf("Width computed from `%c': char %d, total dim %d\n",
00283 width_reference, width_char, width_dim);
00284
00285 if (width_dim <= 0)
00286 return "Error finding width dimensions";
00287
00288 if (0)
00289 printf("Desired character height = %d pixels -> scalefactor = %.4f\n",
00290 height_char, scale);
00291
00292
00293 *height_dimP = height_dim;
00294 *width_dimP = width_dim;
00295 *baselineP = baseline;
00296 *scaleP = scale;
00297
00298 return NULL;
00299 }
00300
00301
00302
00303
00304
00305
00306 static
00307 char *
00308 render(stbtt_fontinfo *font,
00309 mxChar *dict,
00310 int Ndict,
00311 double *cpos,
00312 unsigned char *bms,
00313 int Hdim,
00314 int Wdim,
00315 int baseline,
00316 int width_pad_left,
00317 int width_pad_right,
00318 float scale)
00319 {
00320 unsigned char *bms1;
00321 int x0, y0, x1, y1;
00322 int xOrig, yOrig;
00323 int nchar;
00324 int c;
00325 float scalefac;
00326
00327
00328 for (nchar = 0, bms1 = bms; nchar < Ndict; nchar++, bms1 += Wdim*Hdim) {
00329
00330 c = (int) dict[nchar];
00331
00332
00333
00334
00335
00336
00337 stbtt_GetCodepointBitmapBox(font, c, scale, scale,
00338 &x0, &y0, &x1, &y1);
00339
00340 xOrig = x0 + width_pad_left;
00341 if (xOrig < 0) xOrig = 0;
00342 if (xOrig > Wdim) continue;
00343
00344 yOrig = baseline + y0;
00345 if (yOrig < 0) yOrig = 0;
00346 if (yOrig > Hdim) continue;
00347
00348 stbtt_MakeCodepointBitmap(font,
00349 bms1 + xOrig + yOrig*Wdim,
00350 Wdim-xOrig,
00351 Hdim-yOrig,
00352 Wdim,
00353 scale, scale, c);
00354
00355 if (cpos) {
00356
00357 cpos[nchar*2+0] = x0 + width_pad_left + 1;
00358 cpos[nchar*2+1] = x1 + width_pad_left + 1;
00359
00360 if (cpos[nchar*2+0] < 1 ) cpos[nchar*2+0] = 1;
00361 if (cpos[nchar*2+0] > Wdim) cpos[nchar*2+0] = Wdim;
00362 }
00363 if (0)
00364 printf("*** Copied char #%d = <%04x> = <%c>\n"
00365 "\tX: % 3d .. % 3d\n\tY: % 3d .. % 3d\n",
00366 nchar+1, c, c, x0, x1, y0, y1);
00367 }
00368 return NULL;
00369 }
00370
00371
00372
00373
00374
00375
00376 #define MAX_PIX_SIZE 1024
00377
00378 #ifdef StaticP
00379 StaticP
00380 #endif
00381 void
00382 mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
00383 {
00384 char errstr[256];
00385 char *msg;
00386
00387 stbtt_fontinfo *font;
00388 unsigned char *fontbuf;
00389
00390 char *fontfile;
00391
00392 mxChar *dict;
00393 int Ndict;
00394
00395 double *heights;
00396 int Nheight;
00397 int height_char, add_descenders, height_pad_top, height_pad_bot;
00398
00399 double *widths;
00400 int Nwidth;
00401 int width_reference, width_pad_left, width_pad_right;
00402
00403 int height_dim, width_dim;
00404 int baseline;
00405 float scale;
00406
00407 mwSize dims[3];
00408
00409 double *cpos_arg;
00410
00411
00412 if (nrhs < 0) {
00413 plhs[0] = mxt_PackSignature((mxt_Signature) (-nrhs),
00414 NARGIN_MIN, NARGIN_MAX,
00415 NARGOUT_MIN, NARGOUT_MAX,
00416 in_names, in_specs, out_names, docstring);
00417 return;
00418 }
00419
00420 if ((nrhs < NARGIN_MIN) || (nrhs > NARGIN_MAX))
00421 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00422 "%s: Expect %d <= input args <= %d",
00423 progname, NARGIN_MIN, NARGIN_MAX), errstr));
00424 if ((nlhs < NARGOUT_MIN) || (nlhs > NARGOUT_MAX))
00425 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00426 "%s: Expect %d <= output args <= %d",
00427 progname, NARGOUT_MIN, NARGOUT_MAX), errstr));
00428 mexargparse(nrhs, prhs, in_names, in_specs, NULL, progname);
00429
00430
00431
00432
00433
00434
00435
00436
00437 if (nrhs > ARG_height) {
00438 heights = mxGetPr(prhs[ARG_height]);
00439 Nheight = mxGetNumberOfElements(prhs[ARG_height]);
00440 } else {
00441 heights = NULL;
00442 Nheight = 0;
00443 }
00444 if (Nheight > 0)
00445 height_char = heights[0];
00446 else
00447 height_char = 32;
00448 if (Nheight > 1)
00449 add_descenders = (heights[1] > 0);
00450 else
00451 add_descenders = 1;
00452 if (Nheight > 2)
00453 height_pad_top = heights[2];
00454 else
00455 height_pad_top = 0;
00456 if (Nheight > 3)
00457 height_pad_bot = heights[3];
00458 else
00459 height_pad_bot = height_pad_top;
00460 if (height_char <= 0 || height_char > MAX_PIX_SIZE)
00461 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00462 "%s: character height is a positive integer, not too big, got %d",
00463 progname, height_char), errstr));
00464 if (height_pad_top < -MAX_PIX_SIZE || height_pad_top > MAX_PIX_SIZE)
00465 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00466 "%s: height pad top is too big, got %d",
00467 progname, height_pad_top), errstr));
00468 if (height_pad_bot < -MAX_PIX_SIZE || height_pad_bot > MAX_PIX_SIZE)
00469 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00470 "%s: height pad bot is too big, got %d",
00471 progname, height_pad_bot), errstr));
00472
00473
00474 if (nrhs > ARG_width) {
00475 widths = mxGetPr(prhs[ARG_width]);
00476 Nwidth = mxGetNumberOfElements(prhs[ARG_width]);
00477 } else {
00478 widths = NULL;
00479 Nwidth = 0;
00480 }
00481 if (Nwidth > 0)
00482 width_reference = (int) widths[0];
00483 else
00484 width_reference = (int) '0';
00485 if (Nwidth > 1)
00486 width_pad_left = widths[1];
00487 else
00488 width_pad_left = 0;
00489 if (Nwidth > 2)
00490 width_pad_right = widths[2];
00491 else
00492 width_pad_right = width_pad_left;
00493 if (width_pad_left < -MAX_PIX_SIZE || width_pad_left > MAX_PIX_SIZE)
00494 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00495 "%s: width pad left is too big, got %d",
00496 progname, width_pad_left), errstr));
00497 if (width_pad_right < -MAX_PIX_SIZE || width_pad_right > MAX_PIX_SIZE)
00498 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00499 "%s: width pad right is too big, got %d",
00500 progname, width_pad_right), errstr));
00501
00502
00503 Ndict = mxGetNumberOfElements(prhs[ARG_dict]);
00504 dict = mxGetChars(prhs[ARG_dict]);
00505
00506
00507 if ((fontfile = mxArrayToString(prhs[ARG_font])) == NULL)
00508 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00509 "%s: could not get the font name",
00510 progname), errstr));
00511 if (access(fontfile, R_OK) < 0)
00512 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00513 "%s: could not access the font file `%s'",
00514 progname, fontfile), errstr));
00515
00516
00517
00518
00519
00520
00521
00522 msg = open_font(fontfile, &font, &fontbuf);
00523 if (msg) {
00524 mxFree(fontfile);
00525 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00526 "%s: problem opening font file: %s",
00527 progname, msg), errstr));
00528 }
00529 mxFree(fontfile);
00530
00531
00532 msg = find_scale(font,
00533
00534 &height_dim, &width_dim, &baseline, &scale,
00535
00536 height_char, add_descenders, height_pad_top, height_pad_bot,
00537
00538 width_reference, width_pad_left, width_pad_right);
00539 if (msg) {
00540 free(font);
00541 free(fontbuf);
00542 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00543 "%s: problem finding font information: %s",
00544 progname, msg), errstr));
00545 }
00546
00547
00548 dims[0] = width_dim;
00549 dims[1] = height_dim;
00550 dims[2] = Ndict;
00551 if ((plhs[ARG_bm] = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL)) == NULL) {
00552 free(font);
00553 free(fontbuf);
00554 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00555 "%s: could not allocate memory for %d bitmaps of size %d x %d",
00556 progname, Ndict, width_dim, height_dim), errstr));
00557 }
00558 if (nlhs > ARG_cpos) {
00559 plhs[ARG_cpos] = mxCreateDoubleMatrix(2, Ndict, mxREAL);
00560 cpos_arg = mxGetPr(plhs[ARG_cpos]);
00561 } else {
00562 cpos_arg = NULL;
00563 }
00564
00565
00566 msg = render(font,
00567 dict, Ndict,
00568 cpos_arg,
00569 mxGetData(plhs[ARG_bm]),
00570 height_dim, width_dim,
00571 baseline, width_pad_left, width_pad_right,
00572 scale);
00573 if (msg) {
00574 free(font);
00575 free(fontbuf);
00576 mxDestroyArray(plhs[ARG_bm]);
00577 if (cpos_arg) mxDestroyArray(plhs[ARG_cpos]);
00578 mexErrMsgTxt((snprintf(errstr, sizeof(errstr),
00579 "%s: problem rendering: %s",
00580 progname, msg), errstr));
00581 }
00582
00583
00584 free(font);
00585 free(fontbuf);
00586 }
00587
00588
00589 #ifdef MEX2C_TAIL_HOOK
00590 #include "mex2c_tail.h"
00591 #endif
00592