본문 바로가기

DirectX/Direct2D,DirectWrite

Tutorial: Getting Started with DirectWrite

https://docs.microsoft.com/ko-kr/windows/win32/directwrite/getting-started-with-directwrite

 

DirectWrite 시작 자습서 - Win32 apps

이 문서에서는 DirectWrite 및 Direct2D를 사용 하 여 단일 형식을 포함 하는 간단한 텍스트와 여러 형식이 포함 된 텍스트를 만드는 방법을 보여 줍니다.

docs.microsoft.com

 

Drawing Simple Text



화면에 간단한 텍스트를 그리기위한 네 가지 구성 요소

  - 렌더링 할 문자열

  - IDWriteTextFormat의 인스턴스

  - 텍스트를 포함 할 영역의 크기

  - 텍스트를 렌더링 할 수있는 렌더 타깃

 

IDWriteTextFormat 인스턴스를 생성하려면 IDWriteFactory 인스턴스가 필요

 --> DWriteCreateFactory 함수를 통해 생성할 수 있다.



Part 1: Declare DirectWrite and Direct2D Resources.

 - DirectWrite are declared in the dwrite.h header file

 - Direct2D are declared in the d2d1.h

 

  1.1 

   - 클래스 헤더 파일 (SimpleText.h)에서 IDWriteFactory 및 IDWriteTextFormat 인터페이스에 대한 포인터를 전용 멤버로 선언

  1.2

    - 렌더링 할 텍스트 문자열과 문자열 길이를 보유 할 멤버를 선언

  1.3

    - Direct2D로 텍스트를 렌더링 하기 위해 ID2D1Factory , ID2D1HwndRenderTarget 및 ID2D1SolidColorBrush 인터페이스에 대한 포인터를 선언



Part 2: Create Device Independent Resources.

 - DirectWrite 리소스는 장치 독립적 (렌더링 장치와 연결 상관없이 어플리케이션 실행 동안 지속)

 - 장치 독립적 리소스는 인터페이스의 Release 메서드를 호출하여 해제해야한다.

 

  1.1

    - 클래스를 초기화 할 때 호출되는 CreateDeviceIndependentResources 메서드 내에서 D2D1CreateFactory 함수를 호출하여 ID2D1Factory(모든 Direct2D 개체의 루트 팩토리 인터페이스) 인터페이스를 생성, 리소스를 인스턴스화

  1.2

    - DWriteCreateFactory 함수를 호출하여  IDWriteFactory 인터페이스(모든 DirectWrite 개체 의 루트 팩토리 인터페이스) 생성, 리소스를 인스턴스화

  1.3
    - 텍스트 초기화, 길이 저장

  1.4

    - IDWriteFactory :: CreateTextFormat 메서드(pDWriteFactory_->CreateTextFormat 이런 식으로 호출)를 사용하여 IDWriteTextFormat 인터페이스 개체 생성

    - IDWriteTextFormat는 텍스트 문자열을 렌더링 하는 데 사용되는 font, weight, stretch, style 및 locale을 지정

  1.5

    - IDWriteTextFormat :: SetTextAlignment 및 IDWriteTextFormat :: SetParagraphAlignment 메서드를 호출하여 텍스트를 가로 및 세로 중앙에 배치

    - 호출 방식 : pTextFormat_->SetTextAlignment, pTextFormat_->SetParagraphAlignment 



Part 3: Create Device-Dependent Resources.

 - ID2D1HwndRenderTarget 과ID2D1SolidColorBrush를 생성

 - render target : Direct2D object, ID2D1HwndRenderTarget (윈도우 핸들에 렌더링 하는 렌더 타깃)

 - ID2D1SolidColorBrush : 렌더 타깃이 만들 수 있는 단색 리소스

 

  1.1

    - 이미 생성됐는지 확인하기 위해 render target pointer가 NULL인지 확인, 아니면 다음 과정 진행

    - ID2D1HwndRenderTarget 생성 (윈도우 핸들을 통해 size를 파악해서)

    - 생성된 render target으로 ID2D1SolidColorBrush 생성

  1.2

    - DiscardDeviceResources 메서드에서 브러시와 렌더링 대상을 모두 해제




Part 4: Draw Text By Using the Direct2D DrawText Method.

 

  1.1

    - 텍스트 레이아웃 영역을 정하기 위해 Direct2D 사각형(D2D1_RECT_F)을 생성

  1.2

    - ID2D1RenderTarget :: DrawText 메서드 사용

    - 화면에 그리기 위해 IDWriteTextFormat 객체 사용

    - ID2D1RenderTarget::DrawText 매개 변수

        -> A string to render.

        -> A string's length

        -> A pointer to an IDWriteTextFormat interface.

        -> A Direct2D layout rectangle.

        -> A pointer to an interface that exposes ID2D1Brush.



Part 5: Render the Window Contents Using Direct2D

 - 렌더링 과정

 

  1.1

    - CreateDeviceResources 메서드를 호출하여 장치 종속 리소스를 만든다.

  1.2

    - 렌더 타깃의 ID2D1HwndRenderTarget :: BeginDraw 메서드를 호출

  1.3

    - ID2D1HwndRenderTarget :: Clear 메서드를 호출하여 렌더링 대상을 지운다.

  1.4

    - DrawText 메서드 호출

  1.5

    - 렌더 타깃의 ID2D1HwndRenderTarget :: EndDraw 메서드를 호출

  1.6

    - 창을 다시 그릴 때 다시 만들 수 있도록 장치 종속 리소스 삭제




Drawing Text with Multiple Formats.

 

다중 서식 텍스트를 만들려면 IDWriteTextFormat 인터페이스 외에 IDWriteTextLayout 인터페이스 를 사용

 

IDWriteTextLayout의 인터페이스는 텍스트 블록의 포맷 및 레이아웃을 설정

 

IDWriteTextFormat으로 디폴트 포맷을 설정하고 특정 영역의 포맷을 IDWriteTextLayout을 통해 설정할 수 있다.




Part 1: Create an IDWriteTextLayout Interface.

  

  1.1

    - IDWriteTextLayout 인터페이스에 대한 포인터 멤버변수로 선언

  1.2

    - CreateDeviceIndependentResources 메서드의 끝에 CreateTextLayout 메서드를 호출(pDWriteFactory_->CreateTextLayout)하여 IDWriteTextLayout 인터페이스 개체 생성

    - IDWriteTextLayout의 인터페이스는 텍스트의 선택된 부분에 다른 포맷을 적용 할 수있는 추가 포맷팅 기능을 제공



Part 2: Applying Formatting with IDWriteTextLayout.

 

  1.1

    - DWRITE_TEXT_RANGE 를 선언 하고 IDWriteTextLayout :: SetFontSize 메서드를 호출(pTextLayout_->SetFontSize)하여 선언한 범위에 대한 글꼴 크기 설정

  1.2

    - IDWriteTextLayout :: SetUnderline 메서드를 호출(pTextLayout_->SetFontWeight)하여 "DirectWrite"하위 문자열에 밑줄 생성 .

  1.3

    - IDWriteTextLayout :: SetFontWeight 메서드를 호출(pTextLayout_->SetFontWeight)하여 하위 문자열 "DirectWrite"의 글꼴 두께를 굵게 설정




Part 3: Adding Typographic Features with IDWriteTypography.

 

  1.1

    - IDWriteTypography 선언

    - IDWriteFactory :: CreateTypography 메서드를 통해 선언한 IDWriteTypography 인터페이스 객체 호출 (pDWriteFactory_->CreateTypography(&pTypography);)

 

  1.2

    - 스타일 집합 7이 지정된 DWRITE_FONT_FEATURE 개체를 선언 (DWRITE_FONT_FEATURE fontFeature = {DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7,1};)

    -  IDWriteTypography :: AddFontFeature 메서드를 호출하여 글꼴 기능을 추가(pTypography->AddFontFeature(fontFeature))

 

  1.3

    - DWRITE_TEXT_RANGE 변수 선언 

(DWRITE_TEXT_RANGE textRange = {0, cTextLength_};)

 

    - DWriteTextLayout :: SetTypography 메서드를 호출 (pTextLayout_->SetTypography(pTypography, textRange)) 하고 텍스트 범위를 전달하여 텍스트 레이아웃을 설정)

 

  1.4

    - 텍스트 레이아웃 개체의 새 너비와 높이를 설정

    - pTextLayout_->SetMaxWidth

    - pTextLayout_->SetMaxHeight



Part 4: Draw Text Using the Direct2D DrawTextLayout Method.

 - IDWriteTextLayout 객체에서 지정한 텍스트 레이아웃 설정으로 텍스트를 그리려면  IDWriteTextLayout :: DrawTextLayout 을 사용

 

  1.1

    - D2D1_POINT_2F 변수 선언

  1.2

    - Direct2D 렌더링 대상 의 ID2D1RenderTarget :: DrawTextLayout 메서드를 호출 하고 IDWriteTextLayout 포인터를 전달하여 화면에 텍스트를 그린다.

(pRT_->DrawTextLayout(

    origin, //D2D1_POINT_2F 변수 (텍스트를 그릴 위치)

    pTextLayout_, //IDWriteTextLayout 포인터

    pBlackBrush_ //ID2D1SolidColorBrush 포인터

    );

 

'DirectX > Direct2D,DirectWrite' 카테고리의 다른 글

Direct2D Resources Overview  (0) 2021.04.30
Text Rendering with Direct2D and DirectWrite  (0) 2021.04.30
Direct2D Hello World Sample  (0) 2021.04.22
Creating a Simple Direct2D Application  (0) 2021.04.22
Direct2D 학습 목표  (0) 2021.04.22