/src/3rdPartyLibraries/libharu-2.1.0/demo/jpfont_demo.c

https://bitbucket.org/bathtub/rose · C · 197 lines · 137 code · 38 blank · 22 comment · 5 complexity · 6bb219f9cdc23b8fc8d56a6b250c2137 MD5 · raw file

  1. /*
  2. * << Haru Free PDF Library 2.0.0 >> -- jpfont_demo.c
  3. *
  4. * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
  5. *
  6. * Permission to use, copy, modify, distribute and sell this software
  7. * and its documentation for any purpose is hereby granted without fee,
  8. * provided that the above copyright notice appear in all copies and
  9. * that both that copyright notice and this permission notice appear
  10. * in supporting documentation.
  11. * It is provided "as is" without express or implied warranty.
  12. *
  13. */
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <setjmp.h>
  18. #include "hpdf.h"
  19. jmp_buf env;
  20. #ifdef HPDF_DLL
  21. void __stdcall
  22. #else
  23. void
  24. #endif
  25. error_handler (HPDF_STATUS error_no,
  26. HPDF_STATUS detail_no,
  27. void *user_data)
  28. {
  29. printf ("ERROR: error_no=%04X, detail_no=%u\n", (HPDF_UINT)error_no,
  30. (HPDF_UINT)detail_no);
  31. longjmp(env, 1);
  32. }
  33. int main (int argc, char **argv)
  34. {
  35. HPDF_Doc pdf;
  36. char fname[256];
  37. HPDF_Font title_font;
  38. HPDF_Font detail_font[16];
  39. char samp_text[2048];
  40. HPDF_Outline root;
  41. HPDF_UINT i;
  42. const HPDF_UINT PAGE_HEIGHT = 210;
  43. #ifdef __WIN32__
  44. FILE* f = fopen ("mbtext\\sjis.txt", "rb");
  45. #else
  46. FILE* f = fopen ("mbtext/sjis.txt", "rb");
  47. #endif
  48. if (!f) {
  49. printf ("error: cannot open 'mbtext/sjis.txt'\n");
  50. return 1;
  51. }
  52. fgets (samp_text, 2048, f);
  53. fclose (f);
  54. strcpy (fname, argv[0]);
  55. strcat (fname, ".pdf");
  56. pdf = HPDF_New (error_handler, NULL);
  57. if (!pdf) {
  58. printf ("error: cannot create PdfDoc object\n");
  59. return 1;
  60. }
  61. if (setjmp(env)) {
  62. HPDF_Free (pdf);
  63. return 1;
  64. }
  65. /* configure pdf-document to be compressed. */
  66. HPDF_SetCompressionMode (pdf, HPDF_COMP_ALL);
  67. /* declaration for using Japanese font, encoding. */
  68. HPDF_UseJPEncodings (pdf);
  69. HPDF_UseJPFonts (pdf);
  70. detail_font[0] = HPDF_GetFont (pdf, "MS-Mincyo", "90ms-RKSJ-H");
  71. detail_font[1] = HPDF_GetFont (pdf, "MS-Mincyo,Bold", "90ms-RKSJ-H");
  72. detail_font[2] = HPDF_GetFont (pdf, "MS-Mincyo,Italic", "90ms-RKSJ-H");
  73. detail_font[3] = HPDF_GetFont (pdf, "MS-Mincyo,BoldItalic", "90ms-RKSJ-H");
  74. detail_font[4] = HPDF_GetFont (pdf, "MS-PMincyo", "90msp-RKSJ-H");
  75. detail_font[5] = HPDF_GetFont (pdf, "MS-PMincyo,Bold", "90msp-RKSJ-H");
  76. detail_font[6] = HPDF_GetFont (pdf, "MS-PMincyo,Italic", "90msp-RKSJ-H");
  77. detail_font[7] = HPDF_GetFont (pdf, "MS-PMincyo,BoldItalic",
  78. "90msp-RKSJ-H");
  79. detail_font[8] = HPDF_GetFont (pdf, "MS-Gothic", "90ms-RKSJ-H");
  80. detail_font[9] = HPDF_GetFont (pdf, "MS-Gothic,Bold", "90ms-RKSJ-H");
  81. detail_font[10] = HPDF_GetFont (pdf, "MS-Gothic,Italic", "90ms-RKSJ-H");
  82. detail_font[11] = HPDF_GetFont (pdf, "MS-Gothic,BoldItalic", "90ms-RKSJ-H");
  83. detail_font[12] = HPDF_GetFont (pdf, "MS-PGothic", "90msp-RKSJ-H");
  84. detail_font[13] = HPDF_GetFont (pdf, "MS-PGothic,Bold", "90msp-RKSJ-H");
  85. detail_font[14] = HPDF_GetFont (pdf, "MS-PGothic,Italic", "90msp-RKSJ-H");
  86. detail_font[15] = HPDF_GetFont (pdf, "MS-PGothic,BoldItalic",
  87. "90msp-RKSJ-H");
  88. /* Set page mode to use outlines. */
  89. HPDF_SetPageMode(pdf, HPDF_PAGE_MODE_USE_OUTLINE);
  90. /* create outline root. */
  91. root = HPDF_CreateOutline (pdf, NULL, "JP font demo", NULL);
  92. HPDF_Outline_SetOpened (root, HPDF_TRUE);
  93. for (i = 0; i <= 15; i++) {
  94. HPDF_Page page;
  95. HPDF_Outline outline;
  96. HPDF_Destination dst;
  97. HPDF_REAL x_pos;
  98. HPDF_Point p;
  99. HPDF_UINT j;
  100. /* add a new page object. */
  101. page = HPDF_AddPage (pdf);
  102. /* create outline entry */
  103. outline = HPDF_CreateOutline (pdf, root,
  104. HPDF_Font_GetFontName (detail_font[i]), NULL);
  105. dst = HPDF_Page_CreateDestination (page);
  106. HPDF_Outline_SetDestination(outline, dst);
  107. title_font = HPDF_GetFont (pdf, "Helvetica", NULL);
  108. HPDF_Page_SetFontAndSize (page, title_font, 10);
  109. HPDF_Page_BeginText (page);
  110. /* move the position of the text to top of the page. */
  111. HPDF_Page_MoveTextPos(page, 10, 190);
  112. HPDF_Page_ShowText (page, HPDF_Font_GetFontName (detail_font[i]));
  113. HPDF_Page_SetFontAndSize (page, detail_font[i], 15);
  114. HPDF_Page_MoveTextPos (page, 10, -20);
  115. HPDF_Page_ShowText (page, "abcdefghijklmnopqrstuvwxyz");
  116. HPDF_Page_MoveTextPos (page, 0, -20);
  117. HPDF_Page_ShowText (page, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
  118. HPDF_Page_MoveTextPos (page, 0, -20);
  119. HPDF_Page_ShowText (page, "1234567890");
  120. HPDF_Page_MoveTextPos (page, 0, -20);
  121. HPDF_Page_SetFontAndSize (page, detail_font[i], 10);
  122. HPDF_Page_ShowText (page, samp_text);
  123. HPDF_Page_MoveTextPos (page, 0, -18);
  124. HPDF_Page_SetFontAndSize (page, detail_font[i], 16);
  125. HPDF_Page_ShowText (page, samp_text);
  126. HPDF_Page_MoveTextPos (page, 0, -27);
  127. HPDF_Page_SetFontAndSize (page, detail_font[i], 23);
  128. HPDF_Page_ShowText (page, samp_text);
  129. HPDF_Page_MoveTextPos (page, 0, -36);
  130. HPDF_Page_SetFontAndSize (page, detail_font[i], 30);
  131. HPDF_Page_ShowText (page, samp_text);
  132. p = HPDF_Page_GetCurrentTextPos (page);
  133. /* finish to print text. */
  134. HPDF_Page_EndText (page);
  135. HPDF_Page_SetLineWidth (page, 0.5);
  136. x_pos = 20;
  137. for (j = 0; j <= strlen (samp_text) / 2; j++) {
  138. HPDF_Page_MoveTo (page, x_pos, p.y - 10);
  139. HPDF_Page_LineTo (page, x_pos, p.y - 12);
  140. HPDF_Page_Stroke (page);
  141. x_pos = x_pos + 30;
  142. }
  143. HPDF_Page_SetWidth (page, p.x + 20);
  144. HPDF_Page_SetHeight (page, PAGE_HEIGHT);
  145. HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 25);
  146. HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 25);
  147. HPDF_Page_Stroke (page);
  148. HPDF_Page_MoveTo (page, 10, PAGE_HEIGHT - 85);
  149. HPDF_Page_LineTo (page, p.x + 10, PAGE_HEIGHT - 85);
  150. HPDF_Page_Stroke (page);
  151. HPDF_Page_MoveTo (page, 10, p.y - 12);
  152. HPDF_Page_LineTo (page, p.x + 10, p.y - 12);
  153. HPDF_Page_Stroke (page);
  154. }
  155. HPDF_SaveToFile (pdf, fname);
  156. /* clean up */
  157. HPDF_Free (pdf);
  158. return 0;
  159. }