Get software font fonts

How can I get a font template (ttf / otf) as a curve / series of points on Android? For example, if I wanted to convert a word with a special font to a vector format?

+5
source share
1 answer

Since I was never developed for an Android device, I will give you a way to do it, but in Java .

These are some good libraries, but I don't know if it can be used ( C/C++ ). Therefore, I will explain to you how to do it yourself.

First, you must convert your word in the shape using TextLayout (the unchanging graphic representation of stylized characters you can draw) into a FontRenderContext .

According to John J. Smith, answer here: fooobar.com/questions/659047 / .... On Android, it should be possible to use something similar to TextLayout . But there is no parity FontRenderContext . As I said, I never developed an Android device, but there is probably (hopefully) a workaround for converting characters to form.

In Java, Something like this should work (to convert text to form):

 public Shape getShape(String text, Font font, Point from) { FontRenderContext context = new FontRenderContext(null, false, false); GeneralPath shape = new GeneralPath(); TextLayout layout = new TextLayout(text, font, context); Shape outline = layout.getOutline(null); shape.append(outline, true); return shape; } 

Then you should find the border of the form. It is not very complicated here because your form can give you a direct path iterator using shape.getPathIterator(null)

At each iteration, you can get the current segment, its type and coordinates .:

  • SEG_QUADTO: quadratic parametric curve;
  • SEG_CUBICTO: cubic parametric curve;
  • SEG_LINETO: indicates the endpoint of the line;
  • SEG_MOVETO: point indicating the starting location for the new subpath.

At this point you should read about the Bezier curve here and.

You will learn that:

Any quadratic spline can be expressed as a cube (where the cubic term is zero). The end points of the cubes will be the same as the quadratic x.

CP0 = QP0 CP3 = QP2

Two control points for cubes:

CP1 = QP0 + 2/3 * (QP1-QP0) CP2 = CP1 + 1/3 * (QP2-QP0)

So, converting from TrueType to PostScript is trivial.

In Java, Something like this should work:

 public List<Point> getPoints(Shape shape) { List<Point> out = new ArrayList<Point>(); PathIterator iterator = shape.getPathIterator(null); double[] coordinates = new double[6]; double x = 0, y = 0; while (!iterator.isDone()) { double x1 = coordinates[0]; double y1 = coordinates[1]; double x2 = coordinates[2]; double y2 = coordinates[3]; double x3 = coordinates[4]; double y3 = coordinates[5]; switch (iterator.currentSegment(coordinates)) { case PathIterator.SEG_QUADTO: x3 = x2; y3 = y2; x2 = x1 + 1 / 3f * (x2 - x1); y2 = y1 + 1 / 3f * (y2 - y1); x1 = x + 2 / 3f * (x1 - x); y1 = y + 2 / 3f * (y1 - y); out.add(new Point(x3, y3)); x = x3; y = y3; break; case PathIterator.SEG_CUBICTO: out.add(new Point(x3, y3)); x = x3; y = y3; break; case PathIterator.SEG_LINETO: out.add(new Point(x1, y1)); x = x1; y = y1; break; case PathIterator.SEG_MOVETO: out.add(new Point(x1, y1)); x = x1; y = y1; break; } iterator.next(); } return out; } 

I created a demo project on Bitbucket, maybe it can help you. https://bitbucket.org/pieralexandre/fontshape

The source text of the form (after transforming the outline):

enter image description here

Shape points:

enter image description here

Only points:

enter image description here

And the conclusion of all points:

 (0.0,0.0) (9.326171875,200.0) (9.326171875,127.734375) (0.0,0.0) (50.7080078125,130.126953125) (62.158203125,138.232421875) (69.82421875,162.158203125) (60.302734375,190.087890625) (50.78125,200.0) //... 

I know that you cannot use Graphics2D (I used it for the UI) on Android, but you should be able to use my solution for the Android Project (hopefully).

After that, you could (using the Bezier curve) recreate your curves.

In addition, there are some more good tools. take a look at this:

http://nodebox.imtqy.com/opentype.js/

This is in Javascript, but maybe this can help you even more.

+5
source

Source: https://habr.com/ru/post/1205498/


All Articles