; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Fonts managing library.
;
;  Target OS: Win32
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________


iglobal
  if used FontAttr.font_weights

;fwLight     = 0
;fwDemilight = 1
;fwNormal    = 2
;fwMedium    = 3
;fwDemibold  = 4
;fwBold      = 5
;fwExtraBold = 6
;fwBlack     = 7

    FontAttr:
      .font_weights dw FW_THIN, FW_EXTRALIGHT, FW_NORMAL, FW_MEDIUM, FW_SEMIBOLD, FW_BOLD, FW_EXTRABOLD, FW_HEAVY
  end if
endg



body FontCreate
.pwc dd ?
begin
        push    ebx ecx edx esi edi

        mov     eax, [.weight]
        and     eax, 7
        movzx   eax, [FontAttr.font_weights+2*eax]

        mov     [.weight], eax

        neg     [.size]

        stdcall StrLen, [.font_name]
        mov     ebx, eax

        stdcall StrPtr, [.font_name]
        mov     [.font_name], eax

        lea     ecx, [8*ebx]
        stdcall GetMem, ecx
        mov     edi, eax

        invoke  MultiByteToWideChar, CP_UTF8, 0, [.font_name], ebx, edi, ecx

        mov     eax, [.flags]
        mov     ebx, eax
        mov     ecx, eax
        mov     edx, eax
        and     ebx, ffItalic
        and     ecx, ffUnderline
        and     edx, ffStrikeOut
        and     eax, ffMonospaced
        jz      @f
        mov     eax, FIXED_PITCH
@@:
        invoke  CreateFontW, [.size], 0, 0, 0, [.weight], ebx, ecx, edx,         \
                                    DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,        \
                                    CLIP_DEFAULT_PRECIS, PROOF_QUALITY, eax,  \
                                    edi

      if defined options.DebugMode & options.DebugMode
        pushad
        mov     esi, eax

        invoke        CreateCompatibleDC, 0
        mov           ebx, eax

        invoke  SelectObject, ebx, esi
        push    eax

        invoke  GetTextFaceA, ebx, 1024, fontName

        OutputLn fontName

        invoke  SelectObject, ebx ; from the stack
        invoke  DeleteObject, ebx

        popad
      end if

        stdcall FreeMem, edi
        pop     edi esi edx ecx ebx
        return
endp

uglobal
  if defined options.DebugMode & options.DebugMode
    fontName rb 1024
  end if
endg

body FontDestroy
begin
        push    eax ecx edx
        invoke  DeleteObject, [.pFont]
        pop     edx ecx eax
        return
endp


; returns some font metrics
;
; Arguments:
;    .font - the font
; Returns:
;    EAX - the line height of the font.
;    EBX - the ascender of the font.
;    EDX - the descender of the font.

body GetFontMetrics
.tm TEXTMETRIC
begin
        invoke  CreateCompatibleDC, 0
        mov     ebx, eax

        invoke  SelectObject, ebx, [.font]
        push    eax

        lea     eax, [.tm]
        invoke  GetTextMetricsW, ebx, eax

        invoke  SelectObject, ebx ; from the stack

        invoke  DeleteDC, ebx

        mov     eax, [.tm.tmHeight]
        mov     ebx, [.tm.tmAscent]
        mov     edx, [.tm.tmDescent]

        return
endp
