SOL9 Sample: DirectWriteTextLayout

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * DirectWriteTextLayout.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// 2015/10/01

#define COMMONCONTROLS_V6

//#include <sol/ApplicationView.h>
#include <sol/COMInitializer.h>
#include <sol/PaintDC.h>
#include <sol/StringT.h>

#include <sol/directx/DirectXMainView.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/Direct2D1SolidColorBrush.h>
#include <sol/directx/DirectWriteTextFormat.h>
#include <sol/directx/DirectWriteTextLayout.h>

#include "resource.h"

namespace SOL {

class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget>  renderTarget;
    SmartPtr<Direct2D1SolidColorBrush>   blackColorBrush;
    SmartPtr<Direct2D1SolidColorBrush>   navyColorBrush;
    SmartPtr<DirectWriteTextFormat>      textFormat;
    SmartPtr<DirectWriteTextLayout>      textLayout;
    StringT<wchar_t>            text;

  private:
   void resize(int w, int h)
   {
     try {
       auto size = D2D1::SizeU(w, h);
       renderTarget ->resize(size);
     } catch (Exception& ex) {
       ex.display();
     }
   }
  
   void display()
   {
     try {
       if (!(renderTarget -> checkWindowState() & D2D1_WINDOW_STATE_OCCLUDED)){

         renderTarget -> beginDraw();
         RECT rc;
         getClientRect(&rc);
         renderTarget -> setTransform(D2D1::Matrix3x2F::Identity());
         renderTarget -> clear(D2D1::ColorF(D2D1::ColorF::White));

         auto size = renderTarget->getSize();
         auto rectangle = D2D1::RectF(0.0f, 0.0f, size.width, size.height);

         D2D1_POINT_2F origin = D2D1::Point2F(10, 0);

         renderTarget->drawTextLayout(
            origin,
            *textLayout,
            *navyColorBrush
            );
         renderTarget->endDraw();
        }
      } catch (Exception& ex) {
        ex.display();
      }
    }
    
  public:
    SimpleView(MainView* parent, const TCHAR* name, Args& args)
    :DirectXView(parent, name, args)
    {
      try {
        Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
        renderTarget   = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());

        DirectWriteFactory*  writeFactory = getWriteFactory();
        blackColorBrush   = new Direct2D1SolidColorBrush(*renderTarget, D2D1::ColorF(D2D1::ColorF::Black));
        navyColorBrush   = new Direct2D1SolidColorBrush(*renderTarget, D2D1::ColorF(D2D1::ColorF::Navy));
    
        text = L"The dragon wing of night o'erspreads the earth.";

        textFormat         = new DirectWriteTextFormat(*writeFactory,
            L"Gabriola",
            NULL,
            DWRITE_FONT_WEIGHT_NORMAL,
            DWRITE_FONT_STYLE_NORMAL,
            DWRITE_FONT_STRETCH_NORMAL,
            50.0f,
            L"en-us");      

        textFormat -> setTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
        textFormat -> setParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);

        textLayout = new DirectWriteTextLayout(*writeFactory,
          (const wchar_t*)text,
          text.getLength(),
          *textFormat,
          900.0f,
          180.0f);
        const wchar_t* dragon = L"dragon";
        int pos = text.findPosition(dragon);

        DWRITE_TEXT_RANGE textRange = {pos, wcslen(dragon) }; //"dragon" range in text
        textLayout -> setFontSize(100.0f, textRange);
        textLayout -> setUnderline(TRUE, textRange);

      } catch (Exception& ex) {
        ex.display();
      }
    }

    ~SimpleView()
    {
    }
  };
  // Inner class ends.

private:
  SmartPtr<SimpleView> view;

public:
  /**
   * Constructor
   */
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectXMainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPCHILDREN) )
  {
    try {
      Args ar;
      ar.set(XmNstyle, WS_BORDER|WS_CHILD|WS_VISIBLE);
      view = new SimpleView(this, _T(""), ar);

      addEventHandler(WM_SIZE, this, 
        (Handler)&MainView::size, NULL);

    } catch (Exception& ex) {
      ex.display();
    }
    restorePlacement();
  }

public:
  ~MainView()
  {
  }

private:
  long size(Event& event)
  {  
    int w, h;
    event.getSize(w, h);
    if (view.notNull()) {
      view -> reshape(10, 10, w-20, h-20);
    }
    return 0;
  }
};

}


//////////////////////////////////////////////
//
void  Main(int argc, TCHAR** argv)
{
  const TCHAR* appClass = appName(argv[0]); 
  try {
    HeapSetInformation(nullptr, HeapEnableTerminationOnCorruption, nullptr, 0);

    COMInitializer initializer( COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
    
    Application applet(appClass, argc, argv);

    Args args; 
    MainView imageViewer(applet, appClass, args);
    imageViewer.realize();

    applet.run();
  } catch (Exception& ex) {
    ex.display();
  }
}


Last modified: 8 Dec 2016

Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.