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 0
  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. int state;
  36. } DrawInfo;
  37.  
  38. int scalex(DrawInfo *di, int x)
  39. {
  40. return di->ox + x / di->scale;
  41. }
  42.  
  43. int scaley(DrawInfo *di, int y)
  44. {
  45. return di->szy - (di->oy + y / di->scale);
  46. }
  47.  
  48. int b64decode(BYTE *dst, size_t *dst_size, BYTE *src, int src_size)
  49. {
  50. static BYTE b64o[80] = {
  51. 62, 0, 62, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  53. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  54. 0, 0, 0, 62, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
  55. 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
  56. int i, words = src_size/4, oversize = *dst_size+2, lasti = oversize/3-1;
  57. if(src_size % 4) return 1;
  58. if(*dst_size < words * 3) return 2;
  59. for(i = 0; i <= lasti; i++){
  60. int j;
  61. BYTE c[4];
  62. for(j = 0; j < sizeof(c); j++)
  63. c[j] = b64o[(src[i * sizeof(c) + j] - '+') % sizeof(b64o)];
  64. dst[i * 3 + 0] = ((c[0] << 2) & 0x00fc) | ((c[1] >> 4) & 0x03);
  65. if(i < lasti || oversize % 3)
  66. dst[i * 3 + 1] = ((c[1] << 4) & 0x00f0) | ((c[2] >> 2) & 0x0f);
  67. if(i < lasti || (oversize % 3) == 2)
  68. dst[i * 3 + 2] = ((c[2] << 6) & 0x00c0) | (c[3] & 0x3f);
  69. }
  70. if(src[src_size - 1] == '='){
  71. --(*dst_size);
  72. if(src[src_size - 2] == '=') --(*dst_size);
  73. }
  74. return 0;
  75. }
  76.  
  77. int initGlyph(BYTE *str)
  78. {
  79. int len = (strlen(str) >> 2) * 3;
  80. if(b64decode(str, &len, str, strlen(str))) return 0;
  81. return len;
  82. }
  83.  
  84. short b2h(BYTE *b)
  85. {
  86. return (short)((b[0] << 8) | b[1]);
  87. }
  88.  
  89. int getGlyphData(CurvePoint **cp, BYTE **epoc, int *numepoc, char ch)
  90. {
  91. int i, j, k, pos = 0, num = 0, code = 0, fpos = 0, epnum = 0, fnum = 0;
  92. *cp = NULL, *epoc = NULL, *numepoc = 0;
  93. while(pos < glyf_len){
  94. num = b2h(&glyf[pos]), code = b2h(&glyf[pos + 2]);
  95. epnum = gflg[fpos], fnum = (num + 7) / 8;
  96. if(code == ch){
  97. if(!(*cp = (CurvePoint *)malloc(num * sizeof(CurvePoint)))) break;
  98. if(!(*epoc = (BYTE *)malloc(*numepoc = epnum))) break;
  99. for(i = 0; i < epnum; ++i) (*epoc)[i] = gflg[fpos + 1 + i];
  100. for(i = 0, k = 0; i < fnum; ++i){
  101. BYTE b = gflg[fpos + 1 + epnum + i];
  102. BYTE m = 0x80;
  103. for(j = 0; j < 8 - (i == fnum - 1 ? fnum * 8 - num : 0); ++j, m >>= 1)
  104. (*cp)[k++].flg = b & m ? 1 : 0;
  105. }
  106. for(i = 0; i < num; ++i){
  107. (*cp)[i].x = (i ? (*cp)[i-1].x : 0) + b2h(&glyf[pos + (i+1)*4]);
  108. (*cp)[i].y = (i ? (*cp)[i-1].y : 0) + b2h(&glyf[pos + (i+1)*4 + 2]);
  109. }
  110. return num;
  111. }
  112. pos += (num + 1) * 4;
  113. fpos += epnum + fnum + 1;
  114. }
  115. return 0;
  116. }
  117.  
  118. int drawGlyph(DrawInfo *di, int *w, int *h, int ch)
  119. {
  120. CurvePoint *cp;
  121. BYTE *epoc;
  122. int numepoc, i, j;
  123. int numflags = getGlyphData(&cp, &epoc, &numepoc, ch);
  124. *w = 80, *h = 160;
  125. if(numflags){
  126. for(j = 0; j < numepoc; ++j){
  127. CurvePoint prv;
  128. int k = j ? epoc[j - 1] + 1 : 0;
  129. for(i = k; i <= epoc[j]; ++i){
  130. CurvePoint cur = cp[i];
  131. di->state = i - k;
  132. if(SHOW_MARK){
  133. int r = i == k ? 4 : 3;
  134. COLORREF col = cur.flg & 0x01 ? RGB(0, 0, 255) : RGB(255, 0, 0);
  135. HPEN hpen = CreatePen(PS_SOLID, 1, col);
  136. HPEN hopen = (HPEN)SelectObject(di->hdc, hpen);
  137. HBRUSH hbrush, hobrush;
  138. if(i == k) hbrush = CreateSolidBrush(RGB(0, 255, 0));
  139. else hbrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
  140. hobrush = (HBRUSH)SelectObject(di->hdc, hbrush);
  141. Ellipse(di->hdc, scalex(di, cur.x) - r, scaley(di, cur.y) + r,
  142. scalex(di, cur.x) + r, scaley(di, cur.y) - r);
  143. SelectObject(di->hdc, hobrush);
  144. if(i == k) DeleteObject(hbrush);
  145. SelectObject(di->hdc, hopen);
  146. DeleteObject(hpen);
  147. }
  148. if(i == k && !(cur.flg & 0x01))
  149. MessageBox(NULL, "off curve first", TITLE, MB_ICONEXCLAMATION|IDOK);
  150. if(i != k)
  151. if(curve(di, &prv, &cur, &cp[i == epoc[j] ? k : i + 1])) continue;
  152. prv = cur;
  153. }
  154. di->state = i - k;
  155. curve(di, &prv, &cp[k], &cp[k + 1]);
  156. }
  157. }
  158. if(cp) free(cp);
  159. if(epoc) free(epoc);
  160. return 0;
  161. }
  162.  
  163. int bezier(DrawInfo *di, int px, int py, int x, int y, int nx, int ny)
  164. {
  165. #if 1
  166. int t, m = ((nx - px) * (nx - px) + (ny - py) * (ny - py)) / 5000;
  167. if(di->state == 1)
  168. MoveToEx(di->hdc, scalex(di, px), scaley(di, py), NULL);
  169. for(t = 1; t < m; ++t){
  170. float f = t / (float)m;
  171. float a[] = {(1 - f) * (1 - f), 2 * f * (1 - f), f * f};
  172. int tx = a[0] * px + a[1] * x + a[2] * nx;
  173. int ty = a[0] * py + a[1] * y + a[2] * ny;
  174. LineTo(di->hdc, scalex(di, tx), scaley(di, ty));
  175. }
  176. LineTo(di->hdc, scalex(di, nx), scaley(di, ny));
  177. #else
  178. int i;
  179. POINT lppt[4];
  180. lppt[0].x = px, lppt[0].y = py;
  181. lppt[1].x = px + (x - px) * 2 / 3, lppt[1].y = py + (y - py) * 2 / 3;
  182. lppt[2].x = lppt[1].x + (nx - px) / 3, lppt[2].y = lppt[1].y + (ny - py) / 3;
  183. lppt[3].x = nx, lppt[3].y = ny;
  184. for(i = 0; i < sizeof(lppt) / sizeof(lppt[0]); ++i){
  185. lppt[i].x = scalex(di, lppt[i].x);
  186. lppt[i].y = scaley(di, lppt[i].y);
  187. }
  188. PolyBezierTo(di->hdc, lppt, sizeof(lppt) / sizeof(lppt[0]));
  189. #endif
  190. return 0;
  191. }
  192.  
  193. int curve(DrawInfo *di, CurvePoint *prv, CurvePoint *cur, CurvePoint *nxt)
  194. {
  195. if(cur->flg & 0x01){
  196. if(!(prv->flg & 0x01))
  197. MessageBox(NULL, "off curve bug", TITLE, MB_ICONEXCLAMATION|IDOK);
  198. stroke(di, prv->x, prv->y, cur->x, cur->y);
  199. return 0;
  200. }
  201. if(nxt->flg & 0x01){
  202. bezier(di, prv->x, prv->y, cur->x, cur->y, nxt->x, nxt->y);
  203. *prv = *nxt;
  204. return !0;
  205. }else{
  206. int mx = (cur->x + nxt->x) / 2, my = (cur->y + nxt->y) / 2;
  207. bezier(di, prv->x, prv->y, cur->x, cur->y, mx, my);
  208. prv->flg = 1, prv->x = mx, prv->y = my;
  209. return !0;
  210. }
  211. }
  212.  
  213. int stroke(DrawInfo *di, int xs, int ys, int xe, int ye)
  214. {
  215. if(di->state == 1){
  216. POINT p;
  217. MoveToEx(di->hdc, scalex(di, xs), scaley(di, ys), &p);
  218. }
  219. LineTo(di->hdc, scalex(di, xe), scaley(di, ye));
  220. return 0;
  221. }
  222.  
  223. int drawStrokes(DrawInfo *di, COLORREF foreground_color, char *str)
  224. {
  225. int ox = di->ox, oy = di->oy;
  226. HPEN hpen = CreatePen(PS_SOLID, 1, foreground_color);
  227. HPEN hopen = (HPEN)SelectObject(di->hdc, hpen);
  228. HBRUSH hbrush = CreateSolidBrush(foreground_color);
  229. HBRUSH hobrush = (HBRUSH)SelectObject(di->hdc, hbrush);
  230. char *p;
  231. for(p = str; *p; ++p){
  232. int w, h;
  233. BeginPath(di->hdc);
  234. SetBkMode(di->hdc, TRANSPARENT);
  235. drawGlyph(di, &w, &h, *p);
  236. EndPath(di->hdc);
  237. FillPath(di->hdc);
  238. if(*p == '\n') di->ox = ox, di->oy -= h + di->spy;
  239. else di->ox += w + di->spx;
  240. }
  241. DeleteObject(SelectObject(di->hdc, hobrush));
  242. DeleteObject(SelectObject(di->hdc, hopen));
  243. return 0;
  244. }
  245.  
  246. int drawCallback(HDC hdc, int szx, int szy, COLORREF transparent_color)
  247. {
  248. DrawInfo di = {hdc, szx, szy, 120, szy - 400, 40, 40, 10};
  249. HPEN hpen = CreatePen(PS_SOLID, 1, transparent_color);
  250. HPEN hopen = (HPEN)SelectObject(hdc, hpen);
  251. HBRUSH hbrush = CreateSolidBrush(transparent_color);
  252. HBRUSH hobrush = (HBRUSH)SelectObject(hdc, hbrush);
  253. Rectangle(hdc, 0, 0, szx, szy);
  254. drawStrokes(&di, RGB(0, 255, 0), "Hello,\nworld!");
  255. DeleteObject(SelectObject(hdc, hobrush));
  256. DeleteObject(SelectObject(hdc, hopen));
  257. return 0;
  258. }
  259.  
  260. int drawTransparent(HDC hdc, int x, int y, int w, int h,
  261. HDC hmdc, int szx, int szy,
  262. COLORREF transparent_color, int (*callback)(HDC, int, int, COLORREF))
  263. {
  264. HDC hndc = CreateCompatibleDC(hmdc);
  265. HBITMAP hnbmp = CreateBitmap(szx, szy, 1, 1, NULL);
  266. HBITMAP honbmp = (HBITMAP)SelectObject(hndc, hnbmp);
  267. HDC hgndc = CreateCompatibleDC(hmdc);
  268. HBITMAP hgnbmp = CreateCompatibleBitmap(hmdc, szx, szy);
  269. HBITMAP hognbmp = (HBITMAP)SelectObject(hgndc, hgnbmp);
  270. HDC hpdc = CreateCompatibleDC(hmdc);
  271. HBITMAP hpbmp = CreateBitmap(szx, szy, 1, 1, NULL);
  272. HBITMAP hopbmp = (HBITMAP)SelectObject(hpdc, hpbmp);
  273. COLORREF hocol = SetBkColor(hmdc, transparent_color);
  274. callback(hmdc, szx, szy, transparent_color);
  275. BitBlt(hndc, 0, 0, szx, szy, hmdc, 0, 0, SRCCOPY);
  276. BitBlt(hgndc, 0, 0, szx, szy, hndc, 0, 0, SRCCOPY);
  277. BitBlt(hpdc, 0, 0, szx, szy, hndc, 0, 0, NOTSRCCOPY);
  278. SetBkColor(hmdc, hocol);
  279. BitBlt(hmdc, 0, 0, szx, szy, hpdc, 0, 0, SRCAND);
  280. BitBlt(hdc, x, y, w, h, hgndc, 0, 0, SRCAND);
  281. BitBlt(hdc, x, y, w, h, hmdc, 0, 0, SRCPAINT);
  282. DeleteObject(SelectObject(hpdc, hopbmp));
  283. DeleteDC(hpdc);
  284. DeleteObject(SelectObject(hgndc, hognbmp));
  285. DeleteDC(hgndc);
  286. DeleteObject(SelectObject(hndc, honbmp));
  287. DeleteDC(hndc);
  288. return 0;
  289. }
  290.  
  291. BOOL versionCheck(DWORD major, DWORD minor, DWORD spmajor)
  292. {
  293. OSVERSIONINFO osvi;
  294. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  295. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  296. GetVersionEx(&osvi);
  297. if(osvi.dwMajorVersion > major) return TRUE;
  298. if(osvi.dwMajorVersion < major) return FALSE;
  299. if(osvi.dwMinorVersion > minor) return TRUE;
  300. if(osvi.dwMinorVersion < minor) return FALSE;
  301. if(spmajor && osvi.dwPlatformId == VER_PLATFORM_WIN32_NT){
  302. BOOL spCheck = FALSE;
  303. HKEY hkey;
  304. if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  305. "SYSTEM\\CurrentControlSet\\Control\\Windows",
  306. 0, KEY_QUERY_VALUE, &hkey) == ERROR_SUCCESS){
  307. DWORD CSDVersion, sz = sizeof(DWORD);
  308. if(RegQueryValueEx(hkey, "CSDVersion", NULL, NULL,
  309. (BYTE *)&CSDVersion, &sz) == ERROR_SUCCESS)
  310. spCheck = LOWORD(CSDVersion) >= spmajor;
  311. RegCloseKey(hkey);
  312. }
  313. return spCheck;
  314. }
  315. return TRUE;
  316. }
  317.  
  318. int main(int ac, char **av)
  319. {
  320. if(!versionCheck(4, 0, 3)) fprintf(stderr, "error 0");
  321. if(!initGlyph(gflg)) fprintf(stderr, "error 1");
  322. if(!(glyf_len = initGlyph(glyf))) fprintf(stderr, "error 2");
  323. {
  324. HWND hwnd = GetDesktopWindow();
  325. HDC hdc = GetDC(hwnd);
  326. RECT rc;
  327. GetClientRect(hwnd, &rc);
  328. {
  329. int szx = rc.right - rc.left, szy = rc.bottom - rc.top;
  330. HDC hmdc = CreateCompatibleDC(hdc);
  331. HBITMAP hbmp = CreateCompatibleBitmap(hdc, szx, szy);
  332. HBITMAP hobmp = (HBITMAP)SelectObject(hmdc, hbmp);
  333. BitBlt(hmdc, 0, 0, szx, szy, hdc, 0, 0, SRCCOPY); // for Aero
  334. drawTransparent(hdc, 0, 0, szx, szy, hmdc, szx, szy,
  335. RGB(255, 255, 0), drawCallback);
  336. DeleteObject(SelectObject(hmdc, hobmp));
  337. DeleteDC(hmdc);
  338. }
  339. ReleaseDC(hwnd, hdc);
  340. }
  341. return 0;
  342. }
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty