티스토리 뷰


VTK 라이브러리 에서 문자열의 넓이 및 높이 값을 얻어오는 방법 이다. 참고로 VTK는 6.2.0 기준이다.

VTK 내부에서 텍스트를 렌더링 할때 사용하는 방법으로 텍스트를 이미지화 하여 길이를 구할수 있다.


#include <vtkSmartPointer.h>

#include <vtkImageData.h>

#include <vtkTextRendererStringToImage.h>

#include <vtkTextProperty.h>


std::string strSampleText = "문자열 길이 구하기";


// 텍스트의 정보 객체

vtkTextProperty*                          titleProperties  = vtkTextProperty::New();

// 이미지화 객체

vtkTextRendererStringToImage*    textRenderer   = vtkTextRendererStringToImage::New();

// 이미지 데이터 객체

vtkSmartPointer<vtkImageData>  imageData      = vtkSmartPointer<vtkImageData>::New();


// 텍스트 정보 객체에 텍스트에 대한 정보를 담는다. 아래는 폰트명만 넣었지만 여기에 폰트 크기등을 설정 할 수 있다.

titleProperties->SetFontFile("폰트파일");


// 텍스트 이미지를 통해 textDims로 원하는 텍스트의 width 값이나 height값을 구할 수 있다.

int textDims[2];

int nTextWidth, nTextHeight;

if (textRenderer ->RenderString(titlePropertiesstrSampleText.c_str() imageData , textDims))

{

nTextWidth = textDims[0];    // 스트링의 넓이

nTextHeight = textDims[1];   // 스트링의 높이

}


이 방법으로 하면 문자열의 넓이 및 높이를 구할 수 있다.

여러 구글링을 통해서 다른 방법이 있는지 알아 보았지만 찾을 수 없었다.

이 코드는 VTK 내부에서 텍스트를 랜더링 할 때, 내부에서 사용되는 코드를 따온 것이다.

혹시 더 좋은 방법이 있다면 꼭 공유 해주기를...


아래는 VTK 소스의 DrawString 내부 코드 이다.


void vtkOpenGLContextDevice2D::DrawString(float *point, const vtkUnicodeString &string)

{

  vtkOpenGLClearErrorMacro();


  GLfloat mv[16];

  glGetFloatv(GL_MODELVIEW_MATRIX, mv);

  float xScale = mv[0];

  float yScale = mv[5];


  float p[] = { std::floor(point[0] * xScale) / xScale, std::floor(point[1] * yScale) / yScale };


  // Cache rendered text strings

  vtkTextureImageCache<UTF16TextPropertyKey>::CacheData &cache = this->Storage->TextTextureCache.GetCacheData(

        UTF16TextPropertyKey(this->TextProp, string));

  vtkImageData* image = cache.ImageData;

  if (image->GetNumberOfPoints() == 0 && image->GetNumberOfCells() == 0)

  {

    int textDims[2];

    if (!this->TextRenderer->RenderString(this->TextProp, string, image,

                                          textDims))

    {

    return;

    }

    cache.TextWidth = textDims[0];

    cache.TextHeight = textDims[1];

  }

  vtkTexture* texture = cache.Texture;

  glEnable(GL_TEXTURE_2D);

  texture->Render(this->Renderer);


  int imgDims[3];

  image->GetDimensions(imgDims);


  float width = cache.TextWidth / xScale;

  float height = cache.TextHeight / yScale;


  float xw = cache.TextWidth / static_cast<float>(imgDims[0]);

  float xh = cache.TextHeight / static_cast<float>(imgDims[1]);


  this->AlignText(this->TextProp->GetOrientation(), width, height, p);


  float points[] = { p[0], p[1], p[0] + width, p[1], p[0] + width, p[1] + height, p[0], p[1] + height };


  float texCoord[] = { 0.0f, 0.0f, xw, 0.0f, xw, xh, 0.0f, xh };


  glColor4ub(255, 255, 255, 255);

  glEnableClientState(GL_VERTEX_ARRAY);

  glEnableClientState(GL_TEXTURE_COORD_ARRAY);

  glVertexPointer(2, GL_FLOAT, 0, points);

  glTexCoordPointer(2, GL_FLOAT, 0, texCoord);

  glDrawArrays(GL_QUADS, 0, 4);

  glDisableClientState(GL_TEXTURE_COORD_ARRAY);

  glDisableClientState(GL_VERTEX_ARRAY);


  texture->PostRender(this->Renderer);

  glDisable(GL_TEXTURE_2D);


  vtkOpenGLCheckErrorMacro("failed after DrawString");

}


'Programming > Library' 카테고리의 다른 글

[Poco] 파일 로그 쓰기  (0) 2016.05.04
[Poco] 디렉토리 생성 하기  (0) 2016.05.04
[VTK] VTK Chart point size 변경  (0) 2016.04.15
[VTK] Sample 빌드해보기 (VS2010 기준)  (0) 2016.03.30
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함