Skip to content

Instantly share code, notes, and snippets.

@qq157755587
Last active April 20, 2020 05:48
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save qq157755587/32e927e77e259cd84631 to your computer and use it in GitHub Desktop.
Save qq157755587/32e927e77e259cd84631 to your computer and use it in GitHub Desktop.
If you are going to generate PDF file, some printer dont' support embeded font. Your only choice is to convert text to curves.
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import com.google.common.primitives.Floats;
import java.util.ArrayList;
import java.util.List;
public void drawText(String text, float textSize, Canvas canvas) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setTextSize(textSize);
List<Path> paths = new ArrayList<>();//Here can be other Path class of PDF
List<PathPoints> pathPointsList = new ArrayList<>();
Path fontPath = new Path();
paint.getTextPath(text, 0, text.length(), 0f, paint.getFontSpacing(), fontPath);
PathMeasure pathMeasure = new PathMeasure(fontPath, false);
float[] pos = new float[2];
do {
float distance = 0f;
Path textPath = new Path();
List<Float> pxList = new ArrayList<>();
List<Float> pyList = new ArrayList<>();
while (distance < pathMeasure.getLength()) {
pathMeasure.getPosTan(distance, pos, null);
if (Float.compare(distance, 0f) == 0) {
textPath.moveTo(pos[0], pos[1]);
} else {
textPath.lineTo(pos[0], pos[1]);
}
pxList.add(pos[0]);
pyList.add(pos[1]);
distance += 0.1f;
}
textPath.close();
paths.add(textPath);
PathPoints pathPoints = new PathPoints();
pathPoints.px = Floats.toArray(pxList);
pathPoints.py = Floats.toArray(pyList);
pathPointsList.add(pathPoints);
} while (pathMeasure.nextContour());
paint.setStyle(Paint.Style.FILL);
for (int i = 0; i < paths.size(); i++) {
Path path = paths.get(i);
PathPoints pathPoints = pathPointsList.get(i);
int nestingNum = surroundedNumber(pathPointsList, pathPoints);
if (nestingNum % 2 == 0) {
paint.setColor(Color.BLACK);
} else {
paint.setColor(Color.WHITE);
}
canvas.drawPath(path, paint);
}
}
/**
* How many paths are surround the target
*/
private int surroundedNumber(List<PathPoints> pathPointsList, PathPoints target) {
int number = 0;
for (PathPoints pathPoints : pathPointsList) {
if (pathPoints.equals(target)) {
continue;
}
boolean contains = true;
for (int i = 0; i < target.px.length; i++) {
float x = target.px[i];
float y = target.py[i];
Polygon polygon = new Polygon(pathPoints.px, pathPoints.py, pathPoints.px.length);
if (!polygon.contains(x, y)) {
contains = false;
break;
}
}
if (contains) {
number++;
}
}
return number;
}
static class PathPoints {
public float[] px;
public float[] py;
@Override
public boolean equals(Object o) {
if (!(o instanceof PathPoints)) {
return false;
}
PathPoints other = (PathPoints) o;
if (px.length != other.px.length) {
return false;
}
for (int i = 0; i < px.length; i++) {
float x = px[i];
float ox = other.px[i];
if (Float.compare(x, ox) != 0) {
return false;
}
}
for (int i = 0; i < py.length; i++) {
float y = py[i];
float oy = other.py[i];
if (Float.compare(y, oy) != 0) {
return false;
}
}
return true;
}
}
static class Polygon {
// Polygon coodinates.
private float[] polyY, polyX;
// Number of sides in the polygon.
private int polySides;
/**
* Default constructor.
* @param px Polygon y coods.
* @param py Polygon x coods.
* @param ps Polygon sides count.
*/
public Polygon( float[] px, float[] py, int ps ) {
polyX = px;
polyY = py;
polySides = ps;
}
/**
* Checks if the Polygon contains a point.
* @see "http://alienryderflex.com/polygon/"
* @param x Point horizontal pos.
* @param y Point vertical pos.
* @return Point is in Poly flag.
*/
public boolean contains( float x, float y ) {
boolean oddTransitions = false;
for( int i = 0, j = polySides -1; i < polySides; j = i++ ) {
if((Float.compare(polyY[i], y) < 0 && Float.compare(polyY[j], y) >= 0 ) || ( Float.compare(polyY[j], y) < 0 && Float.compare(polyY[i], y) >= 0)){
if(Float.compare(polyX[i] + ( y - polyY[i] ) / ( polyY[j] - polyY[i] ) * ( polyX[j] - polyX[i] ), x) < 0 ) {
oddTransitions = !oddTransitions;
}
}
}
return oddTransitions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment