GLYPHオブジェクトの使用| C ++のAPIソリューション
フォントをユニットとコーディングする場合、グリフを使用することが重要です。グリフに関する基本情報が 記事 フォント段落の 記事で説明されている場合、ここでは、特にグリフとグリフオブジェクトを操作するためのAspose.Fontの機能を教えます。
グリフオブジェクト
任意のフォントのグリフは、 glyphAspose.Fontライブラリのタイプで表されます。 したがって、 glyphタイプは、フォント形式(TrueType、Compactフォント形式など)とは独立して、フォントグリフ用のユニバーサルオブジェクトです。
Glyphオブジェクトによって提供される機能
このオブジェクトはどのような機能を提供しますか?
この質問は、それが持っているプロパティによって答えられます。
- まず、プロパティ stateを見てみましょう。一部のフォントは破損している可能性があり、それらのグリフも破損する可能性があります。 プロパティ *状態 *グリフが破損しているかどうかを教えてください。値 parsedwitherrorsがある場合、そのグリフにエラーが解析され、すべてのGlyphデータが受信されたわけではありません。
- プロパティ widthvectorxおよび leftsidebearingxそのような glyph metricsは、それに応じて前進幅と左側のベアリングとして教えてください。
- プロパティ widthvectoryおよび leftsidebearingyは、widthvectorxおよびleftsidebearingxと同じ感覚を持っていますが、widthvectoryとleftsidebearingyは垂直y軸の座標に関連しています。
フォントサイズが10のときにピクセルでテキスト「Hello World」の幅を計算する方法を示す次のコードスニペットを備えた 例を持ちましょう。
ファイルの頭に次の名前空間を追加します。
1using namespace Aspose::Font::Glyphs;
2System::SharedPtr<Aspose::Font::Font> font;
次に、次のステップを踏む必要があります。
1 //Declare text and other constants
2 const System::String text = u"Hello world";
3 const int32_t fontSize = 10;
4
5 //Declare variable for string width
6 double width = 0;
7
8 //Get glyph for each letter in text and calculate width for whole text.
9 //The same result can be achieved using method font->get_Metrics()->MeasureString(text, fontSize).
10 for (char16_t symbol : text)
11 {
12 System::SharedPtr<GlyphId> gid = this->_font->get_Encoding()->DecodeToGid(symbol);
13 System::SharedPtr<Glyph> glyph = this->_font->GetGlyphById(gid);
14 width += (glyph->get_WidthVectorX() / this->_font->get_Metrics()->get_UnitsPerEM()) * fontSize;
15 }
グリフの境界ボックスを取得するには、 glyphbboxプロパティ glyphオブジェクトを使用します。
グリフの視覚的表現を取得するには、すべてのグリフのポイントの座標を知る必要があります。
GlyphオブジェクトからすべてのGlyphポイントの座標を取得する方法は?
次のプロパティ、 isemptyおよび pathは、このケースのために設計されました。
プロパティ isEmptyは補助です。グリフのパスが空であるかどうか、つまりグリフには描画指示がまったくないかどうかがわかります。値 *false *がある場合、非常に便利なプロパティ *パス *を使用してグリフ全体の図を構築する時が来ました。
Aspose.Fontライブラリの概念では、任意のグリフの表現は、セグメントと呼ばれる最も単純なグラフィックプリミティブに分割され、インターフェイス ipathsegmentで表されます。 Interface iPathsegmentは、ベースの抽象グラフィックプリミティブです。
コンクリートグラフィックプリミティブは、 moveto、 lineto、 curveto、 closepathなどのタイプで表されます。
タイプ closepathは、現在のグラフィック輪郭の終わりを示すために使用されます。
タイプ moveto、 lineto、および curvetoの定義により、同一のポストスクリプト演算子に対応しています。
また、定義により、 ムーブトおよび linetoの型は、 movetoex()
および lineto()
our from windows gdi libに対応しています。
glyphプロパティパスそのグリフのすべてのグラフィックプリミティブのコレクションを提供します。 プロパティパスには SegmentPathがあり、このタイプのすべてのオブジェクトには、 pathsegmentCollectionのプロパティ セグメントがあります。このプロパティ *セグメント は、Object SegmentPath *が含まれるすべてのグラフィックプリミティブを返します。言い換えれば、エントリglyph.path.segmentsを使用して、Glyphのすべてのグラフィックプリミティブを取得できます。
次の exampleは、Glyphが持っているすべてのポイントを計算し、 ポイントタイプのオブジェクトの配列を表す変数「ポイント」に保存します。
このサンプルで使用されるロジックは簡単で、グリフの輪郭を抽出しません。タイプ closepathを使用してこれらの輪郭を取得するには、セグメント処理に追加する必要があります。
ファイルの頭に次の名前空間を追加します。
1using System::Collections::Generic;
2using System::Drawing;
3using Aspose::Font::Glyphs;
4using Aspose::Font::RenderingPath;
次に、次のステップを踏む必要があります。
1 System::SharedPtr<Glyph> glyph;
2
3 //Declare resultant list with pints
4 System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points = System::MakeObject<System::Collections::Generic::List<System::Drawing::Point>>();
5
6 //Init service reference on IPathSegment
7 System::SharedPtr<IPathSegment> prevSegment;
8
9 //Iterate all glyph path segments and collect points
10 for (auto&& segment : glyph->get_Path()->get_Segments())
11 {
12 if ((System::ObjectExt::Is<LineTo>(segment)) || (System::ObjectExt::Is<CurveTo>(segment)))
13 {
14 if (System::ObjectExt::Is<MoveTo>(prevSegment))
15 {
16 System::SharedPtr<MoveTo> moveTo = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::MoveTo>(prevSegment);
17 AddPoint((int32_t)moveTo->get_X(), (int32_t)moveTo->get_Y(), points);
18 }
19 if (System::ObjectExt::Is<LineTo>(segment))
20 {
21 System::SharedPtr<LineTo> line = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::LineTo>(segment);
22 AddPoint((int32_t)line->get_X(), (int32_t)line->get_Y(), points);
23 }
24 else if (System::ObjectExt::Is<CurveTo>(segment))
25 {
26 System::SharedPtr<CurveTo> curve = System::DynamicCast_noexcept<Aspose::Font::RenderingPath::CurveTo>(segment);
27 AddPoint((int32_t)curve->get_X1(), (int32_t)curve->get_Y1(), points);
28 AddPoint((int32_t)curve->get_X2(), (int32_t)curve->get_Y2(), points);
29 AddPoint((int32_t)curve->get_X3(), (int32_t)curve->get_Y3(), points);
30 }
31 }
32 prevSegment = segment;
33 }
34
35void GlyphMetrics::AddPoint(int32_t x, int32_t y, System::SharedPtr<System::Collections::Generic::List<System::Drawing::Point>> points)
36{
37 System::Drawing::Point p;
38 p.set_X(x);
39 p.set_Y(y);
40 points->Add(p);
41}
Aspose.fontを使用するすべての例は、 aspose.cpp-examplesの aspose.font.examples.cpp.sln solutionに保存されます。
問題がある場合は、 aspose.font.product family free support forumのセクションに投稿することができ、数時間以内にサポートチームがすべてをクリアします。