fork download
  1. // copyright ◆en3PGsfOXo http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1363042502/
  2.  
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define SHOW_MARK 1
  8.  
  9. char *TITLE = "glyf";
  10. BYTE *gflg = "AQv/8AIYH8k5JMkBA/ACCxeSSSQBA/ABDP/4ARLJ+SACEyDkknySAAIDB/8=";
  11. BYTE *glyf = "AAwASAU7AAD/OgAAAAAC2f0ZAAAAAP0n/zoAAAAABdEAxgAAAAD9uALnAAAAAAJI"
  12. "AMYAAAAgAGUEYAIb/MkAAAAA/5kAPv9nADb/zwA0/9AAj//QAFYAAAByAAAA5wBb"
  13. "ADEALAAKAAAAAP8z/6H/2P86/8r/kwAA/uoAAP7IAS0AAAEVAAABEgErAUIA9AAA"
  14. "AOIAAAD1/vgAAP8N/0kAKv//AJT/bQCi/2oAAP9pAAD/Tf9O//T/fAAEAGwBdwAA"
  15. "/0QAAAAABhQAvAAAABgAbwRxAi4AAP7v/uj+xP8VAAD/EwAA/ukBPAAAAREAAAER"
  16. "ARcBPQDtAAAA6wAAARj+w/8+/u8AAADZ/1YA0/9pAAD/ZwAA/1f/LQAA/ycAAP8u"
  17. "AKr/JwCYAAAAlgAAAKsA1wAEACwCPwEd/ub9cf9uAAAArgKPAA0AdwY2BF3+3fuj"
  18. "/1IAAP7hA13+4/yj/1MAAP7aBF0AxAAAAM38nwEXA2EAmwAAAR78nwDCA2EAEwBy"
  19. "A3IDkP/2AAD/1gAK/7EACf/KAAD/qQAA/17/s/+1/8MAAPzn/0QAAAAABF0AvAAA"
  20. "AAD/WwBwAFoAqwBLAFkAAAAxAAAALP/7ACz/+QAhAGQEQwAA/0QAAAAAAHX/r/+6"
  21. "/1D/sv+ZAAD/OAAA/xUBNAAAAREAAACOAFEA3gBFAE4ARABMALUAUABhAAAAWAAA"
  22. "AIj/2wBL/9kAAAHkALwAAP9E+v8AAAKB/7QAIv+IABr/uQAA/2IAAP9Q/yQAAP82"
  23. "AAD/OQCI/zEAlgAAAFAAAACkAEcACAAhAgMF0f/m+8n/VgAA/+QENwDW+i//NAAA"
  24. "AAAA0wDMAAA=";
  25. int glyf_len = 0;
  26.  
  27. typedef struct _CurvePoint {
  28. BYTE flg;
  29. int x, y;
  30. } CurvePoint;
  31.  
  32. typedef struct _DrawInfo {
  33. HDC hdc;
  34. int szx, szy, ox, oy, spx, spy, scale;
  35. } DrawInfo;
  36.  
  37. int scalex(DrawInfo *di, int x)
  38. {
  39. return di->ox + x / di->scale;
  40. }
  41.  
  42. int scaley(DrawInfo *di, int y)
  43. {
  44. return di->szy - (di->oy + y / di->scale);
  45. }
  46.  
  47. int b64decode(BYTE *dst, size_t *dst_size, BYTE *src, int src_size)
  48. {
  49. static BYTE b64o[80] = {
  50. 62, 0, 62, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  52. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  53. 0, 0, 0, 62, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  54. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
  55. int i, words = src_size/4, oversize = *dst_size+2, lasti = oversize/3-1;
  56. if(src_size % 4) return 1;
  57. if(*dst_size < words * 3) return 2;
  58. for(i = 0; i <= lasti; i++){
  59. int j;
  60. BYTE c[4];
  61. for(j = 0; j < sizeof(c); j++)
  62. c[j] = b64o[(src[i * sizeof(c) + j] - '+') % sizeof(b64o)];
  63. dst[i * 3 + 0] = ((c[0] << 2) & 0x00fc) | ((c[1] >> 4) & 0x03);
  64. if(i < lasti || oversize % 3)
  65. dst[i * 3 + 1] = ((c[1] << 4) & 0x00f0) | ((c[2] >> 2) & 0x0f);
  66. if(i < lasti || (oversize % 3) == 2)
  67. dst[i * 3 + 2] = ((c[2] << 6) & 0x00c0) | (c[3] & 0x3f);
  68. }
  69. if(src[src_size - 1] == '='){
  70. --(*dst_size);
  71. if(src[src_size - 2] == '=') --(*dst_size);
  72. }
  73. return 0;
  74. }
  75.  
  76. int initGlyph(BYTE *str)
  77. {
  78. int len = (strlen(str) >> 2) * 3;
  79. if(b64decode(str, &len, str, strlen(str))) return 0;
  80. return len;
  81. }
  82.  
  83. short b2h(BYTE *b)
  84. {
  85. return (short)((b[0] << 8) | b[1]);
  86. }
  87.  
  88. int getGlyphData(CurvePoint **cp, BYTE **epoc, int *numepoc, char ch)
  89. {
  90. int i, j, k, pos = 0, num = 0, code = 0, fpos = 0, epnum = 0, fnum = 0;
  91. *cp = NULL, *epoc = NULL, *numepoc = 0;
  92. while(pos < glyf_len){
  93. num = b2h(&glyf[pos]), code = b2h(&glyf[pos + 2]);
  94. epnum = gflg[fpos], fnum = (num + 7) / 8;
  95. if(code == ch){
  96. if(!(*cp = (CurvePoint *)malloc(num * sizeof(CurvePoint)))) break;
  97. if(!(*epoc = (BYTE *)malloc(*numepoc = epnum))) break;
  98. for(i = 0; i < epnum; ++i) (*epoc)[i] = gflg[fpos + 1 + i];
  99. for(i = 0, k = 0; i < fnum; ++i){
  100. BYTE b = gflg[fpos + 1 + epnum + i];
  101. BYTE m = 0x80;
  102. for(j = 0; j < 8 - (i == fnum - 1 ? fnum * 8 - num : 0); ++j, m >>= 1)
  103. (*cp)[k++].flg = b & m ? 1 : 0;
  104. }
  105. for(i = 0; i < num; ++i){
  106. (*cp)[i].x = (i ? (*cp)[i-1].x : 0) + b2h(&glyf[pos + (i+1)*4]);
  107. (*cp)[i].y = (i ? (*cp)[i-1].y : 0) + b2h(&glyf[pos + (i+1)*4 + 2]);
  108. }
  109. return num;
  110. }
  111. pos += (num + 1) * 4;
  112. fpos += epnum + fnum + 1;
  113. }
  114. return 0;
  115. }
  116.  
  117. int drawGlyph(DrawInfo *di, int *w, int *h, int ch)
  118. {
  119. CurvePoint *cp;
  120. BYTE *epoc;
  121. int numepoc, i, j;
  122. int numflags = getGlyphData(&cp, &epoc, &numepoc, ch);
  123. *w = 80, *h = 160;
  124. if(numflags){
  125. for(j = 0; j < numepoc; ++j){
  126. CurvePoint prv;
  127. int k = j ? epoc[j - 1] + 1 : 0;
  128. for(i = k; i <= epoc[j]; ++i){
  129. CurvePoint cur = cp[i];
  130. if(SHOW_MARK){
  131. int r = i == k ? 4 : 3;
  132. COLORREF col = cur.flg & 0x01 ? RGB(0, 0, 255) : RGB(255, 0, 0);
  133. HPEN hpen = CreatePen(PS_SOLID, 1, col);
  134. HPEN hopen = (HPEN)SelectObject(di->hdc, hpen);
  135. HBRUSH hbrush, hobrush;
  136. if(i == k) hbrush = CreateSolidBrush(RGB(0, 255, 0));
  137. else hbrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
  138. hobrush = (HBRUSH)SelectObject(di->hdc, hbrush);
  139. Ellipse(di->hdc, scalex(di, cur.x) - r, scaley(di, cur.y) + r,
  140. scalex(di, cur.x) + r, scaley(di, cur.y) - r);
  141. SelectObject(di->hdc, hobrush);
  142. if(i == k) DeleteObject(hbrush);
  143. SelectObject(di->hdc, hopen);
  144. DeleteObject(hpen);
  145. }
  146. if(i == k && !(cur.flg & 0x01))
  147. MessageBox(NULL, "off curve first", TITLE, MB_ICONEXCLAMATION|IDOK);
  148. if(i != k)
  149. if(curve(di, &prv, &cur, &cp[i == epoc[j] ? k : i + 1])) continue;
  150. prv = cur;
  151. }
  152. curve(di, &prv, &cp[k], &cp[k + 1]);
  153. }
  154. }
  155. if(cp) free(cp);
  156. if(epoc) free(epoc);
  157. return 0;
  158. }
  159.  
  160. int bezier(DrawInfo *di, int px, int py, int x, int y, int nx, int ny)
  161. {
  162. #if 0
  163. int t, m = ((nx - px) * (nx - px) + (ny - py) * (ny - py)) / 5000;
  164. MoveToEx(di->hdc, scalex(di, px), scaley(di, py), NULL);
  165. for(t = 1; t < m; ++t){
  166. float f = t / (float)m;
  167. float a[] = {(1 - f) * (1 - f), 2 * f * (1 - f), f * f};
  168. int tx = a[0] * px + a[1] * x + a[2] * nx;
  169. int ty = a[0] * py + a[1] * y + a[2] * ny;
  170. LineTo(di->hdc, scalex(di, tx), scaley(di, ty));
  171. }
  172. LineTo(di->hdc, scalex(di, nx), scaley(di, ny));
  173. #else
  174. int i;
  175. POINT lppt[4];
  176. lppt[0].x = px, lppt[0].y = py;
  177. lppt[1].x = px + (x - px) * 2 / 3, lppt[1].y = py + (y - py) * 2 / 3;
  178. lppt[2].x = lppt[1].x + (nx - px) / 3, lppt[2].y = lppt[1].y + (ny - py) / 3;
  179. lppt[3].x = nx, lppt[3].y = ny;
  180. for(i = 0; i < sizeof(lppt) / sizeof(lppt[0]); ++i){
  181. lppt[i].x = scalex(di, lppt[i].x);
  182. lppt[i].y = scaley(di, lppt[i].y);
  183. }
  184. PolyBezier(di->hdc, lppt, sizeof(lppt) / sizeof(lppt[0]));
  185. #endif
  186. return 0;
  187. }
  188.  
  189. int curve(DrawInfo *di, CurvePoint *prv, CurvePoint *cur, CurvePoint *nxt)
  190. {
  191. if(cur->flg & 0x01){
  192. if(!(prv->flg & 0x01))
  193. MessageBox(NULL, "off curve bug", TITLE, MB_ICONEXCLAMATION|IDOK);
  194. stroke(di, prv->x, prv->y, cur->x, cur->y);
  195. return 0;
  196. }
  197. if(nxt->flg & 0x01){
  198. bezier(di, prv->x, prv->y, cur->x, cur->y, nxt->x, nxt->y);
  199. *prv = *nxt;
  200. return !0;
  201. }else{
  202. int mx = (cur->x + nxt->x) / 2, my = (cur->y + nxt->y) / 2;
  203. bezier(di, prv->x, prv->y, cur->x, cur->y, mx, my);
  204. prv->flg = 1, prv->x = mx, prv->y = my;
  205. return !0;
  206. }
  207. }
  208.  
  209. int stroke(DrawInfo *di, int xs, int ys, int xe, int ye)
  210. {
  211. POINT p;
  212. MoveToEx(di->hdc, scalex(di, xs), scaley(di, ys), &p);
  213. LineTo(di->hdc, scalex(di, xe), scaley(di, ye));
  214. return 0;
  215. }
  216.  
  217. int drawStrokes(DrawInfo *di, char *str)
  218. {
  219. int ox = di->ox, oy = di->oy;
  220. COLORREF foreground_color = RGB(0, 255, 0);
  221. HPEN hpen = CreatePen(PS_SOLID, 1, foreground_color);
  222. HPEN hopen = (HPEN)SelectObject(di->hdc, hpen);
  223. HBRUSH hbrush = CreateSolidBrush(foreground_color);
  224. HBRUSH hobrush = (HBRUSH)SelectObject(di->hdc, hbrush);
  225. char *p;
  226. for(p = str; *p; ++p){
  227. int w, h;
  228. drawGlyph(di, &w, &h, *p);
  229. if(*p == '\n') di->ox = ox, di->oy -= h + di->spy;
  230. else di->ox += w + di->spx;
  231. }
  232. DeleteObject(SelectObject(di->hdc, hobrush));
  233. DeleteObject(SelectObject(di->hdc, hopen));
  234. return 0;
  235. }
  236.  
  237. int drawCallback(HDC hdc, int szx, int szy, COLORREF transparent_color)
  238. {
  239. DrawInfo di = {hdc, szx, szy, 120, szy - 400, 40, 40, 10};
  240. HPEN hpen = CreatePen(PS_SOLID, 1, transparent_color);
  241. HPEN hopen = (HPEN)SelectObject(hdc, hpen);
  242. HBRUSH hbrush = CreateSolidBrush(transparent_color);
  243. HBRUSH hobrush = (HBRUSH)SelectObject(hdc, hbrush);
  244. Rectangle(hdc, 0, 0, szx, szy);
  245. drawStrokes(&di, "Hello,\nworld!");
  246. DeleteObject(SelectObject(hdc, hobrush));
  247. DeleteObject(SelectObject(hdc, hopen));
  248. return 0;
  249. }
  250.  
  251. int drawTransparent(HDC hdc, int x, int y, int w, int h,
  252. HDC hmdc, int szx, int szy,
  253. COLORREF transparent_color, int (*callback)(HDC, int, int, COLORREF))
  254. {
  255. HDC hndc = CreateCompatibleDC(hmdc);
  256. HBITMAP hnbmp = CreateBitmap(szx, szy, 1, 1, NULL);
  257. HBITMAP honbmp = (HBITMAP)SelectObject(hndc, hnbmp);
  258. HDC hgndc = CreateCompatibleDC(hmdc);
  259. HBITMAP hgnbmp = CreateCompatibleBitmap(hmdc, szx, szy);
  260. HBITMAP hognbmp = (HBITMAP)SelectObject(hgndc, hgnbmp);
  261. HDC hpdc = CreateCompatibleDC(hmdc);
  262. HBITMAP hpbmp = CreateBitmap(szx, szy, 1, 1, NULL);
  263. HBITMAP hopbmp = (HBITMAP)SelectObject(hpdc, hpbmp);
  264. COLORREF hocol = SetBkColor(hmdc, transparent_color);
  265. callback(hmdc, szx, szy, transparent_color);
  266. BitBlt(hndc, 0, 0, szx, szy, hmdc, 0, 0, SRCCOPY);
  267. BitBlt(hgndc, 0, 0, szx, szy, hndc, 0, 0, SRCCOPY);
  268. BitBlt(hpdc, 0, 0, szx, szy, hndc, 0, 0, NOTSRCCOPY);
  269. SetBkColor(hmdc, hocol);
  270. BitBlt(hmdc, 0, 0, szx, szy, hpdc, 0, 0, SRCAND);
  271. BitBlt(hdc, x, y, w, h, hgndc, 0, 0, SRCAND);
  272. BitBlt(hdc, x, y, w, h, hmdc, 0, 0, SRCPAINT);
  273. DeleteObject(SelectObject(hpdc, hopbmp));
  274. DeleteDC(hpdc);
  275. DeleteObject(SelectObject(hgndc, hognbmp));
  276. DeleteDC(hgndc);
  277. DeleteObject(SelectObject(hndc, honbmp));
  278. DeleteDC(hndc);
  279. return 0;
  280. }
  281.  
  282. int main(int ac, char **av)
  283. {
  284. if(!initGlyph(gflg)) fprintf(stderr, "error 1");
  285. if(!(glyf_len = initGlyph(glyf))) fprintf(stderr, "error 2");
  286. {
  287. HWND hwnd = GetDesktopWindow();
  288. HDC hdc = GetDC(hwnd);
  289. RECT rc;
  290. GetClientRect(hwnd, &rc);
  291. {
  292. int szx = rc.right - rc.left, szy = rc.bottom - rc.top;
  293. HDC hmdc = CreateCompatibleDC(hdc);
  294. HBITMAP hbmp = CreateCompatibleBitmap(hdc, szx, szy);
  295. HBITMAP hobmp = (HBITMAP)SelectObject(hmdc, hbmp);
  296. BitBlt(hmdc, 0, 0, szx, szy, hdc, 0, 0, SRCCOPY); // for Aero
  297. drawTransparent(hdc, 0, 0, szx, szy, hmdc, szx, szy,
  298. RGB(255, 255, 0), drawCallback);
  299. DeleteObject(SelectObject(hmdc, hobmp));
  300. DeleteDC(hmdc);
  301. }
  302. ReleaseDC(hwnd, hdc);
  303. }
  304. return 0;
  305. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty