Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: vector fonts? (freetype for pascal?)

  1. #11

    vector fonts? (freetype for pascal?)

    My PolygonFont class: (MPL license)

    Needs to be improved. Also only does a..z lowercase only.
    Also needs Freetype2 integration.

    Code:
    unit PolygonFont;
    
    interface
    
    uses Polygon;
    
    type
    TPolygonFont = class
         private
            FCharGlyph: array[0..255] of TPolygon;
            FCharWidth: array[0..255] of integer;
            FName: string;
            FPrecision: integer;
            FScale: single;
         public
            procedure Generate();
            procedure RenderChar(value: char);
            procedure RenderString(value: string);
            property Name: string read FName write FName;
            property Precision: integer read FPrecision write FPrecision;
            property Scale: single read FScale write FScale;
         end;
    
    implementation
    
    uses DGLOpenGL, VectorFont, Windows, Graphics;
    
    procedure TPolygonFont.Generate();
    var
      loop: integer;
      glyphs: TStrokeCollection;
      cbounds: TRect;
      x1, y1, x2, y2, xm, ym: integer;
      sx, sy: double; // Scaling factors
      i, j, k, sog: integer;
      stroke: TFontStroke;
      //scale: integer;
    
        TTF2Vector: TTTFToVectorConverter;
    
    begin
    
     //vectorfont test
      TTF2Vector := TTTFToVectorConverter.Create(nil);
      TTF2Vector.Font := TFont.Create();
      TTF2Vector.Font.Name := FName;
      // Setup spline precision (1 min, 100 max)
      TTF2Vector.Precision := FPrecision;
    
      for loop := 0 to 255 do
      begin
        FCharGlyph[loop] := TPolygon.Create();
        //FCharGlyph[loop].Outline := true;
        FCharGlyph[loop].SetColor(0.0,1.0,0.0,0.0);
    
        // Get glyphs' strokes
        if &#40;loop >= ord&#40;'a'&#41;&#41; and &#40;loop <= ord&#40;'z'&#41;&#41; then
        begin
        glyphs &#58;= TTF2Vector.GetCharacterGlyphs&#40; loop &#41;;
    
        // Get character bounds
        cbounds &#58;= glyphs.Bounds;
        xm &#58;= &#40;cbounds.Right-cbounds.Left+1&#41; div 2;
        ym &#58;= &#40;cbounds.Bottom-cbounds.Top+1&#41; div 2;
    
        // Compute the scaling factors
        sx &#58;= fscale;
        sy &#58;= fscale;
    
        for i &#58;= 0 to glyphs.Count-1 do
        begin
          // Get a stroke
          stroke &#58;= glyphs.Stroke&#91;i&#93;;
    
          x1 &#58;= 10 + round&#40; &#40;stroke.Pt1.X-cbounds.Left&#41; * sx &#41;;
          x2 &#58;= 10 + round&#40; &#40;stroke.Pt2.X-cbounds.Left&#41; * sx &#41;;
          y1 &#58;= 10 + round&#40; &#40;stroke.Pt1.Y-cbounds.Top&#41; * sy &#41;;
          y2 &#58;= 10 + round&#40; &#40;stroke.Pt2.Y-cbounds.Top&#41; * sy &#41;;
    
          FCharGlyph&#91;loop&#93;.Add&#40;x1/1000, &#40;y1-1&#41;/1000&#41;;
          FCharGlyph&#91;loop&#93;.Add&#40;x2/1000, &#40;y2-1&#41;/1000&#41;;
        end;
    
      // Free the glyphs
      glyphs.Free;
    
      FCharWidth&#91;loop&#93; &#58;= cbounds.Right;
      FCharGlyph&#91;loop&#93;.Tesselate&#40;&#41;;
      end;
    
      end;
    
      TTF2Vector.Free;
    end;
    
    procedure TPolygonFont.RenderChar&#40;value&#58; char&#41;;
    begin
      FCharGlyph&#91;ord&#40;value&#41;&#93;.Render&#40;&#41;;
      glTranslatef&#40;&#40;FCharWidth&#91;ord&#40;value&#41;&#93;*fscale&#41;/1000, 0, 0&#41;;
    end;
    
    procedure TPolygonFont.RenderString&#40;value&#58; string&#41;;
    var
      i&#58; integer;
    begin
      for i &#58;=1 to length&#40;value&#41; do
      begin
        RenderChar&#40;value&#91;i&#93;&#41;;
      end;
    end;
    
    end.
    http://3das.noeska.com - create adventure games without programming

  2. #12

    Re: vector fonts? (freetype for pascal?)

    Quote Originally Posted by Delfi
    Quote Originally Posted by noeska
    Is it possible to use vector fonts with opengl? I know of wgl, but that is windows only.
    Now there is also freetype2. It is supposed to be able to give access to the vector data of the glyph. That give me 3 questions:
    1. Is there a delphi/pascal unit for freetype2?
    2. How does rendering a glyph work? Bezier->Polygon->Triangulate?
    3. Has someone else already done the hard work?

    With knowing how point 2 works. The font can also be save in like a milkshape ascii format. So the original font is not needed.
    1. no
    Afaik Free Pascal has something freetype related in the fcl-image sub-package. Is a bit small, and might only contain necessary calls though.

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •