wafuの技術

技術習得の努力メモです。

C++でlibharu(2)UTF8に挑戦中

時代はUNICODEです。

libharuでも、対応していると信じて挑戦します。

 

libharuのページにも記載している。

HPDF_UseUTFEncodings

がないとか表示されます。

 

変にUTF-8を入れると、エラーに・・・orz

 

とりあえず、原因を探します。

 

HPDF_UseUTFEncodings関数は、ありました。

hpdf_encoder_utf.c

 

ここで、気づきます。

このソースがMakefileに記載されていません。

Makefile.msvc

に追加します。

 

とりあえず、makeはできました。

 

使用方法は、これからです。

C++でlibharu(1)文字(Shift-JIS)のPDF

今度は、C++とlibharuで、文字(Shift-JISを出力します。

 

ソース

    HPDF_Doc pdf;
    HPDF_Font font;
    HPDF_Page page;

    pdf = HPDF_New(NULL, NULL);

    HPDF_UseJPFonts (pdf);
    HPDF_UseJPEncodings (pdf);

    font = HPDF_GetFont (pdf,  "MS-Gothic", "90ms-RKSJ-H");

 

    page = HPDF_AddPage(pdf);

    HPDF_Page_SetHeight (page, 600);
    HPDF_Page_SetWidth (page, 400);

 

    HPDF_Page_BeginText(page);
    HPDF_Page_SetFontAndSize(page, font, 20.0);

 

    HPDF_Page_MoveTextPos(page, 10, 190);

    HPDF_Page_ShowText(page, "show text");

 

    HPDF_SaveToFile(pdf, "output_shift_jis.pdf");

    HPDF_Free (pdf);

 

C++でlibharu(1)ブランクPDF

VC++でも使えるhpdf

UTF-8に対応(バグなし)の可能性が出てきたので、使ってみます。

 

1 libharuを入手

  http://libharu.org/

  ここから、入手できます。

  zlibとか、libpngを用意して、モジュールの作成です。

 

 2 空白PDFの作成

    HPDF_Doc pdf;
    HPDF_Font font;
    HPDF_Page page;

    pdf = HPDF_New(NULL, NULL);

 HPDF_SaveToFile(pdf, "blank.pdf");

 HPDF_Free (pdf);

 

これで、空白のPDFが作成できます。

VC++のスケルトン

#ifndef UNICODE
#define UNICODE
#endif 

#include <windows.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
    // ウィンドウ クラスを登録する
    const wchar_t CLASS_NAME[]  = L"Sample Window Class";
    
    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;

    RegisterClass(&wc);

    // ウィンドウを作成する

    HWND hwnd = CreateWindowEx(
        0,                              // オプションのウィンドウ スタイル
        CLASS_NAME,                     // ウィンドウ クラス
        L"Learn to Program Windows",    // ウィンドウ テキスト
        WS_OVERLAPPEDWINDOW,            // ウィンドウ スタイル

        // サイズと位置
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,

        NULL,       // 親ウィンドウ    
        NULL,       // メニュー
        hInstance,  // インスタンス ハンドル
        NULL        // 追加のアプリケーション データ
        );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);

    // メッセージ ループを実行する

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hwnd, &ps);

            FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW+1));

            EndPaint(hwnd, &ps);
        }
        return 0;

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}