트루타입 글꼴 렌더링 C++

문자는 다른 단어의 의미를 설명하는 기호입니다. Aspose.Font for C++ API와 함께 C++를 사용하여 TrueType fonts로 다른 텍스트 문자를 렌더링할 수 있습니다. TrueType, Type1, OTF, EOT 및 기타 글꼴을 포함한 다양한 텍스트 렌더링 기능을 지원합니다. 몇 가지 간단한 API 호출로 텍스트 렌더링 기능을 사용하는 방법을 살펴보겠습니다. 다음 제목으로 주제를 다룹니다.

C++ 트루타입 글꼴 렌더링 API – 설치

C++ 플랫폼의 인기와 수요를 고려하여 Aspose.Font for C++ API에 매우 유용한 기능을 도입했습니다. 응용 프로그램에서 TrueType 글꼴을 렌더링하려면 New Releases에서 다운로드하여 API를 구성하거나 다음 명령을 사용하여 NuGet를 통해 설치할 수 있습니다.

Install-Package Aspose.Font.Cpp

C++를 사용하여 트루타입 글꼴을 렌더링하는 Glyph 그리기 인터페이스 구현

우선 Aspose.Font.Rendering 네임스페이스 아래에 IGlyphOutlinePainter을 구현해야 합니다. 메소드 및 클래스 구현을 위해 아래 단계를 따르십시오.

  1. GlyphOutlinePainter라는 클래스를 만듭니다.
  2. System.Drawing.Drawing2D.GraphicsPath를 사용하여 그래픽을 그립니다.

다음은 글리프를 그리기 위한 인터페이스 구현을 위한 C++ 코드 스니펫입니다.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
RenderingText::GlyphOutlinePainter::GlyphOutlinePainter(System::SharedPtr<System::Drawing::Drawing2D::GraphicsPath> path)
{
    _path = path;
}

void RenderingText::GlyphOutlinePainter::MoveTo(System::SharedPtr<Aspose::Font::RenderingPath::MoveTo> moveTo)
{
    _path->CloseFigure();
    _currentPoint.set_X((float)moveTo->get_X());
    _currentPoint.set_Y((float)moveTo->get_Y());
}

void RenderingText::GlyphOutlinePainter::LineTo(System::SharedPtr<Aspose::Font::RenderingPath::LineTo> lineTo)
{
    float x = (float)lineTo->get_X();
    float y = (float)lineTo->get_Y();
    _path->AddLine(_currentPoint.get_X(), _currentPoint.get_Y(), x, y);
    _currentPoint.set_X(x);
    _currentPoint.set_Y(y);
}

void RenderingText::GlyphOutlinePainter::CurveTo(System::SharedPtr<Aspose::Font::RenderingPath::CurveTo> curveTo)
{
    float x3 = (float)curveTo->get_X3();
    float y3 = (float)curveTo->get_Y3();
    
    _path->AddBezier(_currentPoint.get_X(), _currentPoint.get_Y(), (float)curveTo->get_X1(), (float)curveTo->get_Y1(), (float)curveTo->get_X2(), (float)curveTo->get_Y2(), x3, y3);
    
    _currentPoint.set_X(x3);
    _currentPoint.set_Y(y3);
}

void RenderingText::GlyphOutlinePainter::ClosePath()
{
    _path->CloseFigure();
}

System::Object::shared_members_type Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::GetSharedMembers()
{
    auto result = System::Object::GetSharedMembers();
    
    result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_path", this->_path);
    result.Add("Aspose::Font::Examples::WorkingWithTrueTypeAndOpenTypeFonts::RenderingText::GlyphOutlinePainter::_currentPoint", this->_currentPoint);
    
    return result;
}

둘째, 다음 단계에 따라 이름이 DrawText인 메서드를 생성하십시오.

  1. 텍스트 문자열의 기호 반복
  2. 각 기호에 대해 식별된 글리프로 GID 가져오기
  3. GlyphOutlinePainter를 생성하고 GlyphOutlineRenderer 객체에 전달
  4. Matrix 개체로 글리프 좌표 지정

또한 이 모든 단계를 수행한 후 다음 C++ 코드 스니펫에 설명된 대로 이미지의 글꼴 크기를 계산하는 유틸리티 메서드를 만듭니다.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
double RenderingText::FontWidthToImageWith(double width, int32_t fontSourceResulution, double fontSize, double dpi /* = 300*/)
{
    double resolutionCorrection = dpi / 72;
    // 72는 글꼴의 내부 dpi입니다.
    return (width / fontSourceResulution) * fontSize * resolutionCorrection;
}

C++를 사용하여 트루타입 글꼴로 텍스트 렌더링

TrueType 글꼴로 텍스트를 렌더링하는 데 필요한 기능을 구현했습니다. 이제 아래 코드 조각과 같이 C++ API용 Aspose.Font를 사용하여 메서드를 호출해 보겠습니다.

For complete examples and data files, please go to https://github.com/aspose-font/Aspose.Font-for-C
System::String dataDir = RunExamples::GetDataDir_Data();
    
System::String fileName1 = dataDir + u"Montserrat-Bold.ttf";
//전체 경로가 있는 글꼴 파일 이름
System::SharedPtr<FontDefinition> fd1 = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName1)));
System::SharedPtr<TtfFont> ttfFont1 = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd1));
    
System::String fileName2 = dataDir + u"Lora-Bold.ttf";
//전체 경로가 있는 글꼴 파일 이름
System::SharedPtr<FontDefinition> fd2 = System::MakeObject<FontDefinition>(Aspose::Font::FontType::TTF, System::MakeObject<FontFileDefinition>(u"ttf", System::MakeObject<FileSystemStreamSource>(fileName2)));
System::SharedPtr<TtfFont> ttfFont2 = System::DynamicCast_noexcept<Aspose::Font::Ttf::TtfFont>(Aspose::Font::Font::Open(fd2));
    
DrawText(u"Hello world", ttfFont1, 14, System::Drawing::Brushes::get_White(), System::Drawing::Brushes::get_Black(), dataDir + u"hello1_montserrat_out.jpg");
DrawText(u"Hello world", ttfFont2, 14, System::Drawing::Brushes::get_Yellow(), System::Drawing::Brushes::get_Red(), dataDir + u"hello2_lora_out.jpg");

결론

이 기사에서는 C++에서 TrueType 글꼴을 사용하여 텍스트를 렌더링하는 방법을 배웠습니다. Aspose.Font for C++ API에서 제공하는 기능에 대해 자세히 알고 싶으십니까? API 참조 또는 제품 문서를 탐색하여 자세한 내용을 알아볼 수 있습니다. 그러나 모호한 부분이 있거나 도움이 필요한 경우 무료 지원 포럼을 통해 언제든지 문의해 주십시오. 여러분과 함께 할 수 있기를 기대합니다!

또한보십시오