PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/shinonome/tools/mkitalic.in

https://bitbucket.org/take_cheeze/easyrpg-player
Autoconf | 419 lines | 286 code | 47 blank | 86 comment | 71 complexity | 6759c294f17358369cd1716f31863060 MD5 | raw file
Possible License(s): GPL-3.0
  1. #!@GAWK@ -f
  2. #
  3. # -- makes BDF font italic
  4. # version 0.00.1
  5. # programmed by Yasuyuki Furukawa <furukawa@vinelinux.org>
  6. # * public domain *
  7. #
  8. BEGIN {
  9. dy = 3; # level of slant deep (2; Minimam and deepest)
  10. correct = 3; # level of pixel correction
  11. debug = 0;
  12. verbose = 0;
  13. verbose_min = 100;
  14. # The table for pixel correction
  15. # format:
  16. # before <- | -> after
  17. # "height offset pattern... | height offset pattern... "
  18. # if the offset is zero, the pattern starts from last
  19. # slant step immidiately.
  20. # pattern of 'left down to up right'
  21. i = 0;
  22. if (correct >= 3) {
  23. ptable[i++] = "5 -2 ..@ .@ .@ @.. @. 1 -1 .$"; # fixed
  24. ptable[i++] = "5 -4 ... @@@. ..@. .@. @. 2 -2 .$, $, ";
  25. # ptable[i++] = "5 -1 ...@. ...@. ..@. .@. @. 1 3 @$"; # XXX
  26. ptable[i++] = "5 -2 .....@ ....@ ...@. ..@. @@. 1 0 ...@$";
  27. ptable[i++] = "4 -3 ...@ ...@ ..@. @@.. 1 -1 .$@";
  28. ptable[i++] = "4 -3 ..@ ..@ .@. @.. 1 0 @$";
  29. ptable[i++] = "4 -1 .@ .@ @.. @. 2 -1 $, ,$";
  30. ptable[i++] = "4 -2 ..@ ..@@ @@.. .@. 2 -1 .$@ @@$";
  31. ptable[i++] = "4 -2 .@ .@. @.. @. 2 -1 $, ,$";
  32. }
  33. if (correct >= 2) {
  34. ptable[i++] = "3 -2 ....@. ..@@. @@... 1 0 @@$...";
  35. ptable[i++] = "3 -1 ..@ .@ .@. 1 0 .,$";
  36. ptable[i++] = "3 -2 .@ .@ @.@ 1 -1 $@";
  37. ptable[i++] = "3 -2 .@ .@. @.. 1 -1 $.";
  38. ptable[i++] = "3 -2 .@ .@@ @.. 1 -1 $@";
  39. ptable[i++] = "3 -1 ..@ .@. @@. 1 0 .,$";
  40. ptable[i++] = "3 -2 @@. .@. @.. 1 -1 $,";
  41. ptable[i++] = "3 -1 @.@ .@. ... 1 0 .@$";
  42. ptable[i++] = "3 -1 ..@ .@. .@@ 1 0 .,$";
  43. # ptable[i++] = "3 -2 ..@ .@@ @.@ 1 0 @$"; # XXX
  44. }
  45. # pattern of 'left up to down right'
  46. if (correct >= 3) {
  47. ptable[i++] = "6 -2 ..@. ...@. ...@. ...@. ..@. @@. 4 -1 ..$, ...@ ..$, .$,";
  48. ptable[i++] = "4 -1 @.. @.@ .@. .@. 1 1 $@";
  49. ptable[i++] = "4 -3 @. @@@ .@ .@ 1 -1 $.";
  50. ptable[i++] = "4 -3 .. @@. ..@. ..@. 2 -2 @, .$,";
  51. ptable[i++] = "3 -2 @.. .@ .@ 1 -1 $.";
  52. ptable[i++] = "4 -1 .@. .@. ..@. ..@. 1 0 .,$";
  53. ptable[i++] = "4 -1 .@. @@. ..@. ..@. 2 -1 @, @@$"; # XXX
  54. }
  55. if (correct >= 2) {
  56. ptable[i++] = "3 -1 @.. .@@ .. 1 0 .,@"; #
  57. ptable[i++] = "3 -2 @.. @@. .@ 1 -1 $.";
  58. ptable[i++] = "3 -2 @.@ .@ .@ 1 -1 $@";
  59. ptable[i++] = "3 -1 .@. .@. ..@. 1 0 ..$";
  60. # ptable[i++] = "4 -2 @. @. @. .@@ 1 0 $."; # fixed , XXX
  61. ptable[i++] = "4 -1 @. @. .@@ .. 2 0 ,$ .,$";
  62. ptable[i++] = "3 -1 @. @. .@@ 1 0 ,$";
  63. ptable[i++] = "3 -2 .. @@. ..@ 1 -1 @,.";
  64. }
  65. # least pattern
  66. ptable[i++] = "4 -2 ..@ .@ @@@ .. 2 -1 $@ @,@";
  67. ptable[i++] = "3 -1 .@.. @.@. .@@. 1 0 $" # fixed
  68. ptable[i++] = "2 -1 .@ @. 1 -1 $";
  69. init();
  70. }
  71. func init(i, tmp)
  72. {
  73. # local i, tmp;
  74. flag = 0; # reading status
  75. line[0]; # image buffer
  76. if (ARGV[1] == "-h") {
  77. usage();
  78. exit(0);
  79. }
  80. if (ARGV[1] == "-V") {
  81. verbose = 1;
  82. delete ARGV[1];
  83. }
  84. if (ARGV[1] == "-d") {
  85. debug = 1;
  86. delete ARGV[1];
  87. }
  88. if (ARGV[1] == "-D") {
  89. debug = 2;
  90. delete ARGV[1];
  91. }
  92. if (ARGV[1] == "-p") {
  93. print_ptable();
  94. exit(1);
  95. }
  96. vmeter[0] = "|";
  97. vmeter[1] = "\\";
  98. vmeter[2] = "-";
  99. vmeter[3] = "/";
  100. "stty size 2> /dev/null" |getline col;
  101. sub(/.* /, "", col);
  102. if (col !~ /^[1-9]/ || col < 30)
  103. col = 0;
  104. }
  105. func usage() {
  106. printf("usage: mkitalic [-V|-d|-p] {input BDF} > {output BDF}\n");
  107. printf(" -V verbose\n");
  108. printf(" -p output pettern table for pixel correction\n");
  109. printf(" -d debug level 1\n");
  110. printf(" -D debug level 2\n");
  111. }
  112. #
  113. # error(str)
  114. #
  115. # Print the error message and exit
  116. # with error state.
  117. #
  118. func error(str) {
  119. printf(str) > "/dev/stderr";
  120. exit(1)
  121. }
  122. #
  123. # rstr(s, n)
  124. #
  125. # Retern a string that repeats
  126. # pettern s(string).
  127. #
  128. func rstr(s, n, i, r) {
  129. # local i, r;
  130. r = "";
  131. for (i = 0; i < n; i++)
  132. r = s r;
  133. return r;
  134. }
  135. #
  136. # subindex(s, t, i)
  137. #
  138. # Serach t(string) in s(string).
  139. #
  140. func subindex(s, t, i, n, r) {
  141. # local n, r;
  142. if (n == 0)
  143. n = length(s);
  144. if (i == 0)
  145. return index(s, t);
  146. r = index(substr(s, i, n), t);
  147. if (r != 0)
  148. return r + i - 1;
  149. else
  150. return 0
  151. }
  152. #
  153. # replace_substr(s, t, i)
  154. #
  155. # Replace a part of s(string)
  156. # to t(string).
  157. #
  158. func replace_substr(s, t, i) {
  159. return substr(s, 1, i-1) t substr(s, i + length(t));
  160. }
  161. #
  162. # correct_pixel(line, width, height)
  163. #
  164. # Correct the pixels to make pettern
  165. # more clear after the slanting.
  166. # Reference the ptable at the BEGIN
  167. # routine.
  168. #
  169. func correct_pixel(line, width, height, x, y, xx, n, nn, d, dd, i, j) {
  170. # local x, y, xx, n, nn, d, dd, i, j;
  171. if (correct == 0)
  172. return;
  173. # add padding pixels from both side
  174. line[height] = rstr(".", width);
  175. for (y = 0; y < height + 1; y ++)
  176. sub(/.*/, ".&.", line[y]);
  177. # pattern matching with ptable
  178. for (y = dy; y < height ; y += dy) {
  179. for (i = 0; ptable[i]; i++) {
  180. split(ptable[i], p, " ");
  181. n = p[2]; d = p[1];
  182. if (y+n < 0 || y+n+d > height + 1)
  183. continue;
  184. for (x = 1; x < width && x > 0; x++) {
  185. x = subindex(line[y+n], p[3], x);
  186. if (x == 0) break;
  187. for (j = 1; j < d; j++) {
  188. xx = subindex(line[y+n+j], p[j+3], x);
  189. if (x != xx) break;
  190. }
  191. if (x == xx) { # matched !
  192. if (debug > 1)
  193. print "$$$$ MATCH with " i " $$$$ (" x ", " y ")";
  194. nn = p[d+4]; dd = p[d+3];
  195. for (j = 0; j < dd; j++)
  196. line[y+nn+j] = replace_substr(line[y+nn+j], p[j+d+5], x);
  197. break;
  198. }
  199. }
  200. }
  201. }
  202. # delete padding pixels from both side
  203. for (y = 0; y < height; y ++) {
  204. sub(/^\./, "", line[y]);
  205. sub(/\.$/, "", line[y]);
  206. }
  207. }
  208. #
  209. # make_slant(line, width, height)
  210. #
  211. # Just slant the pattern of font.
  212. #
  213. func make_slant(line, width, height, y, i, dcount, ncount, tp, ts, te) {
  214. # local y, i, dcount, ncuont, tp, ts, te;
  215. dcount = dx;
  216. ncount = 0;
  217. tp = rstr("#", pad);
  218. for (y = 0; y < height; y++) {
  219. if (y % dy == 0) {
  220. ts = rstr( "#", dcount-- - 1);
  221. te = rstr( "#", ncount++) tp;
  222. }
  223. line[y] = ts substr(line[y], 1, width) te;
  224. }
  225. }
  226. #
  227. # print_ptable()
  228. #
  229. # Visualize ptable.
  230. #
  231. func print_ptable(i, j, n, d, nn,dd, p, pp) {
  232. # local i, j, n, d, nn,dd, p, pp;
  233. printf("\t#### PATTERN TABLE ####\n");
  234. printf("\nFollowing patterns is for pixel correction in slant.\n");
  235. printf("Priority between patterns depends on pattern ID.\n");
  236. for (i = 0; ptable[i]; i++) {
  237. split(ptable[i], p, " ");
  238. n = p[2]; d = p[1];
  239. for (j = 0; j < d; j++)
  240. pp[j] = p[j+3];
  241. nn = p[d+4]; dd = p[d+3];
  242. for (j = 0; j < dd; j++)
  243. p[-n+nn+3+j] = replace_substr(p[-n+nn+3+j], p[j+d+5], 1);
  244. printf("\n\t--- pattern " i " ---\n\n");
  245. for (j = 0; j < d; j++) {
  246. printf(" " pp[j]);
  247. printf(rstr(" ", 7 - length(pp[j])));
  248. if (j == int(d/2))
  249. printf(" ==> ");
  250. else
  251. printf(" ");
  252. if (j +n < dy) printf(" ");
  253. if (j +n < 0) printf(" ");
  254. printf(pp[j]);
  255. printf(rstr(" ", 7 - length(pp[j])));
  256. if (j == int(d/2))
  257. printf(" ==> ");
  258. else
  259. printf(" ");
  260. printf(p[j+3] "\n");
  261. }
  262. }
  263. }
  264. END {
  265. if (verbose != 0 && max_chars > verbose_min)
  266. printf "\r" rstr(" ", col - 3) "\r" > "/dev/stderr";
  267. exit(0);
  268. }
  269. #
  270. # MAIN LOOP
  271. #
  272. # All of the follows is the main
  273. # loop routine.
  274. #
  275. # Change the font property
  276. /^FONT[ \t]/ {
  277. tmp = gsub(/-[R|r]-/, "-I-", $2);
  278. if (tmp == 0)
  279. error("error: the input font is already italic.\n");
  280. }
  281. # Change the font property
  282. /^SLANT[ \t]/ {
  283. sub( /\042[R|r]\042/, "\042I\042", $2);
  284. }
  285. # Get the metric information from the bounding box.
  286. /^(BBX|FONTBOUNDINGBOX)[ \t]/ {
  287. height = $3;
  288. width = $2;
  289. dx = int((height + dy - 1)/dy);
  290. if (height > 0 && width > 0) {
  291. $2 = $2 + dx - 1;
  292. $4 = $4 - int((dx -1)/2);
  293. pad = (8 - ($2 % 8)) % 8;
  294. } else
  295. pad = 0;
  296. }
  297. # Get the number of characters.
  298. /^CHARS[ \t]/ {
  299. max_chars = $2;
  300. }
  301. # Change inner state.
  302. /^BITMAP/ {
  303. flag = 1;
  304. count = 0;
  305. print $0;
  306. next;
  307. }
  308. # Modify the every font pattern.
  309. /^ENDCHAR/ {
  310. # correct pixel as pre-processing
  311. correct_pixel(line, width, height);
  312. # make simple slant font
  313. make_slant(line, width, height);
  314. # output font image
  315. for (y = 0; y < height; y ++) {
  316. if (debug == 0) {
  317. gsub(/#/, ".", line[y]);
  318. gsub(/,/, ".", line[y]);
  319. gsub(/\$/, "@", line[y]);
  320. gsub(/[.@][.@][.@][.@]/, "&_", line[y]);
  321. gsub(/\.\.\.\._/,"0", line[y]);
  322. gsub(/\.\.\.@_/, "1", line[y]);
  323. gsub(/\.\.@\._/, "2", line[y]);
  324. gsub(/\.\.@@_/, "3", line[y]);
  325. gsub(/\.@\.\._/, "4", line[y]);
  326. gsub(/\.@\.@_/, "5", line[y]);
  327. gsub(/\.@@\._/, "6", line[y]);
  328. gsub(/\.@@@_/, "7", line[y]);
  329. gsub(/@\.\.\._/, "8", line[y]);
  330. gsub(/@\.\.@_/, "9", line[y]);
  331. gsub(/@\.@\._/, "A", line[y]);
  332. gsub(/@\.@@_/, "B", line[y]);
  333. gsub(/@@\.\._/, "C", line[y]);
  334. gsub(/@@\.@_/, "D", line[y]);
  335. gsub(/@@@\._/, "E", line[y]);
  336. gsub(/@@@@_/, "F", line[y]);
  337. }
  338. print line[y];
  339. }
  340. # Display progress bar in verbose mode
  341. ch_count++
  342. if (verbose != 0 && max_chars > verbose_min) {
  343. n = int(ch_count * 100 / max_chars);
  344. m = int(n * (col - 21) / 100);
  345. l = col - 20 - m;
  346. printf "\rprogress|" rstr("=", m) rstr(" ", l) n "%%" vmeter[ch_count % 4] > "/dev/stderr";
  347. }
  348. flag = 0;
  349. }
  350. # Default
  351. {
  352. if (flag > 0) {
  353. gsub(/0/, "....");
  354. gsub(/1/, "...@");
  355. gsub(/2/, "..@.");
  356. gsub(/3/, "..@@");
  357. gsub(/4/, ".@..");
  358. gsub(/5/, ".@.@");
  359. gsub(/6/, ".@@.");
  360. gsub(/7/, ".@@@");
  361. gsub(/8/, "@...");
  362. gsub(/9/, "@..@");
  363. gsub(/A|a/, "@.@.");
  364. gsub(/B|b/, "@.@@");
  365. gsub(/C|c/, "@@..");
  366. gsub(/D|d/, "@@.@");
  367. gsub(/E|e/, "@@@.");
  368. gsub(/F|f/, "@@@@");
  369. line[count++] = $0;
  370. } else
  371. print $0;
  372. }