青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

eryar

PipeCAD - Plant Piping Design Software.
RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
posts - 603, comments - 590, trackbacks - 0, articles - 0

FreeType in OpenCASCADE

Posted on 2017-10-22 21:18 eryar 閱讀(2359) 評論(0)  編輯 收藏 引用 所屬分類: 2.OpenCASCADE

FreeType in OpenCASCADE

eryar@163.com

Abstract. FreeType is required for text display in the 3D viewer. FreeType is a software font engine that is designed to be small, efficient, highly customizable, and portable while capable of producing high-quality output(glyph images). It can be used in graphics libraries, display servers, font conversion tools, text image generation tools, and many other products as well. The blog is focus on the FreeType usage in OpenCASCADE to convert text to BRep shape.

Key Words. FreeType, OpenCASCADE, Text, BRep

1.Introduction

FreeType 2被設計為一種占用空間小的、高效的、高度可定制的、并且可以產生可移植的高品質輸出(符號圖像)。可以被用在諸如圖像庫、展出服務器、字體轉換工具、圖像文字產生工具等多種其它產品上。

注意FreeType 2是一種字體服務而沒有提供為實現文字布局或圖形化處理這樣高階的功能使用的API(比如帶色文字渲染之類的)。然而,它提供一個簡單的、易用的并且統一的接口實現對多種字體文件的訪問,從而大大簡化了這些高級的任務。

FreeType 2的發行遵循兩個開源許可:我們自己的BSD樣式的FreeType License和GPL(通用公共許可證)。它可以被用在任何程序中,無論是專有與否。

在常見的圖形庫中,如OpenSceneGraph, OpenCASCADE, HOOPS, Qt,等涉及到文字處理的,都會用到FreeType. 在一些游戲開發中,也會用到FreeType.本文主要對FreeType的用法作簡單介紹,這樣對FreeType有個直觀認識。然后再介紹FreeType對文字輪廓的表示方法,及如何生成三維文字。

在OpenCASCADE中文字可以二維的方式顯示,也可以三維的方式顯示,三維方式可以有線框和渲染模式,如下圖所示:

wps_clip_image-5702

Figure 1. 2D Text in Length Dimension

wps_clip_image-18777

Figure 2. 3D Wireframe Text in Dimension

wps_clip_image-31985

Figure 3. 3D Shading Text in Dimension

2.FreeType Usage

在FreeType的官網上有詳細的教程說明FreeType的用法,網址為:https://www.freetype.org/freetype2/docs/tutorial/index.html

主要的步驟如下:

v 包含頭文件 Header Files;

v 庫的初始化 Library Initialization;

v 加載字體 Loading a Font Face;

v 訪問字體數據 Accessing the Face Data;

v 設置當前像素大小 Setting the Current Pixel Size;

v 加載文字 Loading a Glyph Image;

v 簡單的顯示 Simple Text Rendering; 

下面代碼是上述過程的一個實現,忽略了錯誤處理:

#include <ft2build.h>
#include FT_FREETYPE_H
#pragma comment(lib, "freetype.lib")
void test(void)
{
    FT_Face aFace = NULL;
    FT_Library aLibrary = NULL;
    // Library Initialization.
    FT_Init_FreeType(&aLibrary);
    // Loading a Font Face.
    FT_New_Face(aLibrary, "C:/Windows/Fonts/arial.ttf", 0, &aFace);
    // Setting the Current Pixel Size.
    FT_Set_Char_Size(
          aFace,    /* handle to face object           */
          0,       /* char_width in 1/64th of points  */
          16*64,   /* char_height in 1/64th of points */
          300,     /* horizontal device resolution    */
          300 );   /* vertical device resolution      */
    // Loading a Glyph Image
    // a. Converting a Character Code Into a Glyph Index
    FT_UInt aGlyphIndex = FT_Get_Char_Index( aFace, 'A' );
    // b. Loading a Glyph From the Face
    // Once you have a glyph index, you can load the corresponding glyph image.
    FT_Load_Glyph(
          aFace,            /* handle to face object */
          aGlyphIndex,      /* glyph index           */
          FT_LOAD_DEFAULT); /* load flags            */
    // Simple Text Rendering
    FT_GlyphSlot aGlyphSlot = aFace->glyph;
}
int main(int argc, char* argv[])
{
    test();
    return 0;
}

 

wps_clip_image-11299

調試程序可以看出Face中已經有了一些數據。

3.FreeType Outlines

FreeType中的一個文字輪廓Outline是由二維空間閉合的線圍成。每一個輪廓線是由一系列的直線段和Bezier曲線組成。根據字體文件格式的不同,他們可能是二階或三階多項式。二階的通常稱為quadratic或conic弧,他們用于TrueType格式。三階的稱為cubic弧,通常用于PostScript Type1, CFF, 和CFF2格式中。詳細描述見:

https://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html

Bezier曲線是B樣條曲線的一個特例,他的特點就是曲線的階數與控制點的個數相關,即給定控制頂點就可以確定Bezier曲線。所以OpenCASCADE中對于Bezier曲線有這樣的構造函數:

wps_clip_image-28489

每一段曲線弧都由起點start,終點end和控制頂點control points來描述。描述輪廓線的每個點都有一個特定的標記Tag來區別是線段還是Bezier曲線。

wps_clip_image-26515

wps_clip_image-23987

兩個連續的on點確定了線段的兩個端點;

在兩個on點之間的一個conic off點組成了一個conic Bezier曲線;

在兩個on點之間的兩個cubic off點組成了一個cubic Bezier曲線;

理解了上述內容就可以得到文字的輪廓線了。OpenCASCADE中對輪廓線的處理代碼如下所示:

// =======================================================================
// function : renderGlyph
// purpose  :
// =======================================================================
Standard_Boolean Font_BRepFont::renderGlyph (const Standard_Utf32Char theChar,
                                             TopoDS_Shape&            theShape)
{
  theShape.Nullify();
  if (!loadGlyph (theChar)
   || myFTFace->glyph->format != FT_GLYPH_FORMAT_OUTLINE)
  {
    return Standard_False;
  }
  else if (myCache.Find (theChar, theShape))
  {
    return !theShape.IsNull();
  }
  FT_Outline& anOutline = myFTFace->glyph->outline;
  if (!anOutline.n_contours)
    return Standard_False;
  TopLoc_Location aLoc;
  TopoDS_Face aFaceDraft;
  myBuilder.MakeFace (aFaceDraft, mySurface, myPrecision);
  // Get orientation is useless since it doesn't retrieve any in-font information and just computes orientation.
  // Because it fails in some cases - leave this to ShapeFix.
  //const FT_Orientation anOrient = FT_Outline_Get_Orientation (&anOutline);
  for (short aContour = 0, aStartIndex = 0; aContour < anOutline.n_contours; ++aContour)
  {
    const FT_Vector* aPntList = &anOutline.points[aStartIndex];
    const char* aTags      = &anOutline.tags[aStartIndex];
    const short anEndIndex = anOutline.contours[aContour];
    const short aPntsNb    = (anEndIndex - aStartIndex) + 1;
    aStartIndex = anEndIndex + 1;
    if (aPntsNb < 3)
    {
      // closed contour can not be constructed from < 3 points
      continue;
    }
    BRepBuilderAPI_MakeWire aWireMaker;
    gp_XY aPntPrev;
    gp_XY aPntCurr = readFTVec (aPntList[aPntsNb - 1]);
    gp_XY aPntNext = readFTVec (aPntList[0]);
    Standard_Integer aLinePnts = (FT_CURVE_TAG(aTags[aPntsNb - 1]) == FT_Curve_Tag_On) ? 1 : 0;
    gp_XY aPntLine1 = aPntCurr;
    // see http://freetype.sourceforge.net/freetype2/docs/glyphs/glyphs-6.html
    // for a full description of FreeType tags.
    for (short aPntId = 0; aPntId < aPntsNb; ++aPntId)
    {
      aPntPrev = aPntCurr;
      aPntCurr = aPntNext;
      aPntNext = readFTVec (aPntList[(aPntId + 1) % aPntsNb]);
      // process tags
      if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_On)
      {
        if (aLinePnts < 1)
        {
          aPntLine1 = aPntCurr;
          aLinePnts = 1;
          continue;
        }
        const gp_XY         aDirVec  = aPntCurr - aPntLine1;
        const Standard_Real aLen     = aDirVec.Modulus();
        if (aLen <= myPrecision)
        {
          aPntLine1 = aPntCurr;
          aLinePnts = 1;
          continue;
        }
        if (myIsCompositeCurve)
        {
          Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (gp_Pnt2d (aPntLine1), gp_Pnt2d (aPntCurr));
          myConcatMaker.Add (aLine, myPrecision);
        }
        else
        {
          Handle(Geom_Curve)  aCurve3d;
          Handle(Geom2d_Line) aCurve2d = new Geom2d_Line (gp_Pnt2d (aPntLine1), gp_Dir2d (aDirVec));
          if (to3d (aCurve2d, GeomAbs_C1, aCurve3d))
          {
            TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d, 0.0, aLen);
            myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
            aWireMaker.Add (anEdge);
          }
        }
        aPntLine1 = aPntCurr;
      }
      else if (FT_CURVE_TAG(aTags[aPntId]) == FT_Curve_Tag_Conic)
      {
        aLinePnts = 0;
        gp_XY aPntPrev2 = aPntPrev;
        gp_XY aPntNext2 = aPntNext;
        // previous point is either the real previous point (an "on" point),
        // or the midpoint between the current one and the previous "conic off" point
        if (FT_CURVE_TAG(aTags[(aPntId - 1 + aPntsNb) % aPntsNb]) == FT_Curve_Tag_Conic)
        {
          aPntPrev2 = (aPntCurr + aPntPrev) * 0.5;
        }
        // next point is either the real next point or the midpoint
        if (FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Conic)
        {
          aPntNext2 = (aPntCurr + aPntNext) * 0.5;
        }
        my3Poles.SetValue (1, aPntPrev2);
        my3Poles.SetValue (2, aPntCurr);
        my3Poles.SetValue (3, aPntNext2);
        Handle(Geom2d_BezierCurve) aBezierArc = new Geom2d_BezierCurve (my3Poles);
        if (myIsCompositeCurve)
        {
          myConcatMaker.Add (aBezierArc, myPrecision);
        }
        else
        {
          Handle(Geom_Curve) aCurve3d;
          if (to3d (aBezierArc, GeomAbs_C1, aCurve3d))
          {
            TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
            myBuilder.UpdateEdge (anEdge, aBezierArc, mySurface, aLoc, myPrecision);
            aWireMaker.Add (anEdge);
          }
        }
      }
      else if (FT_CURVE_TAG(aTags[aPntId])                 == FT_Curve_Tag_Cubic
            && FT_CURVE_TAG(aTags[(aPntId + 1) % aPntsNb]) == FT_Curve_Tag_Cubic)
      {
        aLinePnts = 0;
        my4Poles.SetValue (1, aPntPrev);
        my4Poles.SetValue (2, aPntCurr);
        my4Poles.SetValue (3, aPntNext);
        my4Poles.SetValue (4, gp_Pnt2d(readFTVec (aPntList[(aPntId + 2) % aPntsNb])));
        Handle(Geom2d_BezierCurve) aBezier = new Geom2d_BezierCurve (my4Poles);
        if (myIsCompositeCurve)
        {
          myConcatMaker.Add (aBezier, myPrecision);
        }
        else
        {
          Handle(Geom_Curve) aCurve3d;
          if (to3d (aBezier, GeomAbs_C1, aCurve3d))
          {
            TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
            myBuilder.UpdateEdge (anEdge, aBezier, mySurface, aLoc, myPrecision);
            aWireMaker.Add (anEdge);
          }
        }
      }
    }
    if (myIsCompositeCurve)
    {
      Handle(Geom2d_BSplineCurve) aDraft2d = myConcatMaker.BSplineCurve();
      if (aDraft2d.IsNull())
      {
        continue;
      }
      const gp_Pnt2d aFirstPnt = aDraft2d->StartPoint();
      const gp_Pnt2d aLastPnt  = aDraft2d->EndPoint();
      if (!aFirstPnt.IsEqual (aLastPnt, myPrecision))
      {
        Handle(Geom2d_TrimmedCurve) aLine = GCE2d_MakeSegment (aLastPnt, aFirstPnt);
        myConcatMaker.Add (aLine, myPrecision);
      }
      Handle(Geom2d_BSplineCurve) aCurve2d = myConcatMaker.BSplineCurve();
      Handle(Geom_Curve)          aCurve3d;
      if (to3d (aCurve2d, GeomAbs_C0, aCurve3d))
      {
        TopoDS_Edge anEdge = BRepLib_MakeEdge (aCurve3d);
        myBuilder.UpdateEdge (anEdge, aCurve2d, mySurface, aLoc, myPrecision);
        aWireMaker.Add (anEdge);
      }
      myConcatMaker.Clear();
    }
    else
    {
      if (!aWireMaker.IsDone())
      {
        continue;
      }
      TopoDS_Vertex aFirstV, aLastV;
      TopExp::Vertices (aWireMaker.Wire(), aFirstV, aLastV);
      gp_Pnt aFirstPoint = BRep_Tool::Pnt (aFirstV);
      gp_Pnt aLastPoint  = BRep_Tool::Pnt (aLastV);
      if (!aFirstPoint.IsEqual (aLastPoint, myPrecision))
      {
        aWireMaker.Add (BRepLib_MakeEdge (aFirstV, aLastV));
      }
    }
    if (!aWireMaker.IsDone())
    {
      continue;
    }
    TopoDS_Wire aWireDraft = aWireMaker.Wire();
    //if (anOrient == FT_ORIENTATION_FILL_LEFT)
    //{
    // According to the TrueType specification, clockwise contours must be filled
    aWireDraft.Reverse();
    //}
    myBuilder.Add (aFaceDraft, aWireDraft);
  }
  myFixer.Init (aFaceDraft);
  myFixer.Perform();
  theShape = myFixer.Result();
  if (!theShape.IsNull()
  &&  theShape.ShapeType() != TopAbs_FACE)
  {
    // shape fix can not fix orientation within the single call
    TopoDS_Compound aComp;
    myBuilder.MakeCompound (aComp);
    for (TopExp_Explorer aFaceIter (theShape, TopAbs_FACE); aFaceIter.More(); aFaceIter.Next())
    {
      TopoDS_Face aFace = TopoDS::Face (aFaceIter.Current());
      myFixer.Init (aFace);
      myFixer.Perform();
      myBuilder.Add (aComp, myFixer.Result());
    }
    theShape = aComp;
  }
  myCache.Bind (theChar, theShape);
  return !theShape.IsNull();
}

 

4.Text 3D

在一些圖形庫中都可以生成三維文字,如下圖所示:

wps_clip_image-12745

理解了FreeType的用法后,實現上述功能也是很簡單的。這里簡要說明實現步驟:

l 使用FreeType得到文字的輪廓線;

l 將閉合的輪廓線生成Wire->Face;

l 將輪廓線生成的Face進行拉伸得到Solid體。

在OpenCASCADE中得到文字輪廓線生成的Face的類是:Font_BRepFont。下面給出示例得到指定文字的面。

#include <BRepTools.hxx>
#include <Font_BRepFont.hxx>
#include <Font_BRepTextBuilder.hxx>
#pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib")
#pragma comment(lib, "TKG2d.lib")
#pragma comment(lib, "TKG3d.lib")
#pragma comment(lib, "TKGeomBase.lib")
#pragma comment(lib, "TKGeomAlgo.lib")
#pragma comment(lib, "TKBRep.lib")
#pragma comment(lib, "TKTopAlgo.lib")
#pragma comment(lib, "TKService.lib")
void text2brep()
{
    Font_BRepFont aBrepFont("C:/Windows/Fonts/arial.ttf", 3.5);
    Font_BRepTextBuilder aTextBuilder;
    TopoDS_Shape aTextShape = aTextBuilder.Perform(aBrepFont, NCollection_String("eryar@163.com"));
    BRepTools::Dump(aTextShape, std::cout);
    BRepTools::Write(aTextShape, "d:/text.brep");
}
int main(int argc, char* argv[])
{
    text2brep();
    return 0;
}

在Draw Test Harness中顯示出文字的輪廓text.brep如下圖所示:

wps_clip_image-22486

如果要顯示出文字的填充效果,則需要有三角化工具將文字輪廓網格化。OpenCASCADE中將輪廓生成Wire->Face,即可以生成顯示數據了:

wps_clip_image-28082

5.Conclusion

FreeType的文字處理功能很強大,幾乎所有的三維造型內核中文字的處理都是使用的FreeType。

FreeType的文字輪廓使用了線段和Bezier曲線來表達,Bezier曲線是B樣條曲線的特例。理解Bezier曲線就可以自己繪制文字輪廓了。

FreeType使用簡單,可以方便得到文字的輪廓數據。將輪廓數據生成Face即可以拉伸出三維文字效果。

6.References

1. https://www.freetype.org/freetype2/docs/tutorial/index.html

2. http://www.shnenglu.com/eryar/archive/2014/08/17/OpenCascade_Text_Rendering.html

3. https://www.freetype.org/freetype2/docs/glyphs/glyphs-6.html

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲在线不卡| 亚洲午夜在线| 欧美电影免费观看高清| 久久亚洲影音av资源网| 在线精品一区二区| 亚洲第一网站免费视频| 欧美r片在线| 一区二区三区高清不卡| 亚洲一区www| 国产一区二区三区黄| 美日韩在线观看| 欧美精品色综合| 亚洲欧美日韩国产综合精品二区 | 欧美一区二区三区电影在线观看| 亚洲欧美bt| 亚洲高清视频在线| 99re热这里只有精品视频 | 欧美成人四级电影| 亚洲午夜精品| 久久九九免费视频| 一本久道综合久久精品| 香蕉av777xxx色综合一区| 在线欧美日韩国产| 亚洲性感美女99在线| 在线观看视频日韩| 亚洲一区二区三区高清不卡| 在线观看av不卡| 亚洲视频999| 亚洲福利电影| 亚洲欧美日韩精品| 99re热精品| 久久激情网站| 亚洲一区二区三区在线| 久久字幕精品一区| 亚洲欧美在线x视频| 免费精品99久久国产综合精品| 亚洲一区二区精品在线| 奶水喷射视频一区| 久久精品一区二区三区中文字幕| 欧美激情精品久久久六区热门| 久久久久青草大香线综合精品| 欧美日韩亚洲一区二区三区在线| 麻豆精品在线视频| 久久久精品国产一区二区三区| 欧美黄色日本| 免费日韩av片| 韩国福利一区| 欧美一区二区三区在线看 | 亚洲视频碰碰| 免费不卡中文字幕视频| 久久在线播放| 国产伊人精品| 香蕉久久一区二区不卡无毒影院 | 久久综合色8888| 国产一区二区| 午夜久久福利| 欧美一级专区| 国产欧美日韩| 欧美一区二区久久久| 欧美与黑人午夜性猛交久久久| 国产精品久久久久久久第一福利| 亚洲精品一级| 亚洲香蕉伊综合在人在线视看| 欧美日韩成人综合| 亚洲日本理论电影| 一区二区三区免费网站| 欧美日韩国产影片| 日韩亚洲成人av在线| 亚洲小视频在线观看| 国产精品久久久久久久电影| 亚洲午夜精品一区二区| 欧美成人在线免费视频| 亚洲国产高清自拍| 夜夜爽av福利精品导航| 国产精品国产a级| 午夜精品久久久久久久| 欧美在线高清视频| 黄色成人在线| 欧美国产视频在线观看| 亚洲免费大片| 欧美一区二区三区在线免费观看| 国产欧美一区二区精品仙草咪| 久久疯狂做爰流白浆xx| 欧美激情精品久久久久久| 亚洲美女毛片| 国产精品日韩在线观看| 久久精品首页| 亚洲精品小视频在线观看| 午夜精品网站| 亚洲盗摄视频| 欧美少妇一区| 久久精品视频在线免费观看| 亚洲国产成人av在线| 亚洲性视频网址| 国内外成人免费视频| 蜜乳av另类精品一区二区| 日韩手机在线导航| 久久久噜噜噜久久中文字幕色伊伊| 激情成人中文字幕| 欧美日韩综合久久| 久久天堂av综合合色| 99精品国产在热久久| 另类天堂av| 午夜日韩视频| 亚洲国产欧美在线人成| 国产欧美日韩视频在线观看 | 亚洲男人影院| 欧美大成色www永久网站婷| 亚洲天堂第二页| 亚洲国产mv| 亚洲高清激情| 久久免费99精品久久久久久| 一区二区三区三区在线| 在线观看91精品国产麻豆| 国产精品日韩电影| 欧美了一区在线观看| 久久精品国产一区二区三区免费看 | 久久精品123| 亚洲在线电影| 亚洲精品美女在线观看| 国产亚洲一区二区在线观看| 国产精品va在线播放我和闺蜜| 欧美1区2区3区| 久久免费国产精品| 欧美一级视频精品观看| 国产精品99久久99久久久二8| 亚洲国产欧美一区二区三区丁香婷| 久久久精品久久久久| 欧美一区二区在线看| 亚洲网站啪啪| 久久一区二区视频| 国产伦精品一区二区三区四区免费 | 国产精品福利在线观看网址| 欧美激情视频在线播放| 欧美日韩亚洲免费| 国产亚洲成精品久久| 在线精品观看| 亚洲欧美区自拍先锋| 麻豆精品一区二区av白丝在线| 亚洲国产美国国产综合一区二区| 一本到12不卡视频在线dvd| 欧美在线视频免费| 欧美日韩成人一区二区| 国产一在线精品一区在线观看| 日韩午夜三级在线| 久久国产综合精品| 亚洲精品在线看| 久久本道综合色狠狠五月| 欧美母乳在线| 亚洲福利国产| 久久福利电影| 亚洲精品一区二区三区av| 欧美一级淫片aaaaaaa视频| 欧美日韩精品三区| 亚洲国产成人一区| 欧美在线啊v| 亚洲美女av黄| 欧美99在线视频观看| 国产在线不卡| 欧美一级片一区| 亚洲黄色成人网| 久久理论片午夜琪琪电影网| 国产精品九九久久久久久久| 91久久综合亚洲鲁鲁五月天| 久久国产视频网站| 亚洲一二三四久久| 欧美日韩国产丝袜另类| 亚洲欧洲在线一区| 蜜桃av一区二区三区| 午夜日韩电影| 国产女人aaa级久久久级| 亚洲伊人伊色伊影伊综合网| 91久久在线播放| 免费精品视频| 91久久精品国产91性色| 美女999久久久精品视频| 欧美一区二区高清在线观看| 国产精品推荐精品| 亚洲一二三区在线| 亚洲免费观看在线视频| 欧美精品精品一区| 亚洲免费成人av| 亚洲激情在线激情| 欧美a级大片| 亚洲日本乱码在线观看| 欧美国产激情二区三区| 麻豆精品在线视频| 亚洲婷婷在线| 国产精品99免视看9| 亚洲性xxxx| 亚洲桃色在线一区| 国产精品每日更新| 性视频1819p久久| 午夜免费在线观看精品视频| 国产在线观看一区| 蜜臀久久久99精品久久久久久| 老**午夜毛片一区二区三区| 亚洲国产三级| 日韩视频欧美视频| 国产精品女同互慰在线看|