SOL9 Sample: DirectWriteTextFormat

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * DirectWriteTextFormat.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/directx/DirectXMainView.h>
#include <sol/directx/DirectXView.h>
#include <sol/directx/Direct2D1SolidColorBrush.h>
#include <sol/directx/DirectWriteTextFormat.h>

#include "resource.h"

namespace SOL {
  
class MainView :public DirectXMainView {

private:
  //Inner class starts.
  class SimpleView : public DirectXView {
  private:
    SmartPtr<Direct2D1HwndRenderTarget>  renderTarget;
    SmartPtr<Direct2D1SolidColorBrush>   solidColorBrush;
    SmartPtr<DirectWriteTextFormat>      textFormat;
    StringT<wchar_t>            textString;

  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();

         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);

         renderTarget->drawText(
            (const wchar_t*)textString,
            textString.getLength(),
            *textFormat,
            rectangle,
            *solidColorBrush
            );
          renderTarget->endDraw();
        }
      } catch (Exception& ex) {
        ex.display();
      }
    }
    
  public:
    SimpleView(MainView* parent, const TCHAR* name, Args& args)
    :DirectXView(parent, name, args)
    {
      textString = (const wchar_t*)args.get(XmNtextString);
      
      try {
        Direct2D1Factory* d2d1Factory = parent -> getD2D1Factory();
        renderTarget   = new Direct2D1HwndRenderTarget(*d2d1Factory, getWindow());

        DirectWriteFactory*  writeFactory = getWriteFactory();
        D2D1::ColorF color = (D2D1::ColorF)args.get(XmNforeground);
        
        solidColorBrush   = new Direct2D1SolidColorBrush(*renderTarget, 
                      D2D1::ColorF(color));

        textFormat         = new DirectWriteTextFormat(*writeFactory,
            (const wchar_t*)args.get(XmNfaceName),
            NULL,
            DWRITE_FONT_WEIGHT_NORMAL,
            DWRITE_FONT_STYLE_NORMAL,
            DWRITE_FONT_STRETCH_NORMAL,
            (float)args.get(XmNfontSize),  
            L"");      

        textFormat -> setTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
        textFormat -> setParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
      } catch (Exception& ex) {
        ex.display();
      }
    }

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

private:
  SmartPtr<SimpleView> view1;
  SmartPtr<SimpleView> view2;
  SmartPtr<SimpleView> view3;
    
public:
  /**
   * Constructor
   */
  MainView(Application& applet, const TCHAR* name, Args& args)
  :DirectXMainView(applet, name,
                 args.set(XmNstyle, (ulong)WS_CLIPSIBLINGS|WS_CLIPCHILDREN) )
  {
    try {
      Args ar;
      ar.set(XmNfaceName,   (long)L"Arial");
      ar.set(XmNfontSize,   (long)40.0f);
      ar.set(XmNtextString, (long)L"New Imperial Einstein. 新アインシュタイン帝国");
      ar.set(XmNforeground, (long)D2D1::ColorF::Red);

        view1 = new SimpleView(this, _T(""), ar);
      
      ar.reset();
      ar.set(XmNfaceName,   (long)L"Cambria");
      ar.set(XmNfontSize,   (long)60.0f);
      ar.set(XmNtextString, (long)L"You Raize Me Up.");
      ar.set(XmNforeground, (long)D2D1::ColorF::Green);
      view2 = new SimpleView(this, _T(""), ar);

      ar.reset();
      ar.set(XmNfaceName,   (long)L"Gabriola");
      ar.set(XmNfontSize,   (long)80.0f);
      ar.set(XmNtextString, (long)L"Time To Say Goodbye.");
      ar.set(XmNforeground, (long)D2D1::ColorF::Blue);
      view3 = 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 = 0;
    int h = 0;
    event.getSize(w, h);
    if (view1.notNull() && view2.notNull() && view3.notNull()) {
      view1 -> reshape(0, 0, w, h/3);
      view2 -> reshape(0, h/3, w, h/3);
      view3 -> reshape(0, h*2/3, w, h/3);
    }
      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.