1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 package org.tensorflow.demo.env; 17 18 import android.graphics.Canvas; 19 import android.graphics.Color; 20 import android.graphics.Paint; 21 import android.graphics.Paint.Align; 22 import android.graphics.Paint.Style; 23 import android.graphics.Rect; 24 import android.graphics.Typeface; 25 import java.util.Vector; 26 27 /** 28 * A class that encapsulates the tedious bits of rendering legible, bordered text onto a canvas. 29 */ 30 public class BorderedText { 31 private final Paint interiorPaint; 32 private final Paint exteriorPaint; 33 34 private final float textSize; 35 36 /** 37 * Creates a left-aligned bordered text object with a white interior, and a black exterior with 38 * the specified text size. 39 * 40 * @param textSize text size in pixels 41 */ BorderedText(final float textSize)42 public BorderedText(final float textSize) { 43 this(Color.WHITE, Color.BLACK, textSize); 44 } 45 46 /** 47 * Create a bordered text object with the specified interior and exterior colors, text size and 48 * alignment. 49 * 50 * @param interiorColor the interior text color 51 * @param exteriorColor the exterior text color 52 * @param textSize text size in pixels 53 */ BorderedText(final int interiorColor, final int exteriorColor, final float textSize)54 public BorderedText(final int interiorColor, final int exteriorColor, final float textSize) { 55 interiorPaint = new Paint(); 56 interiorPaint.setTextSize(textSize); 57 interiorPaint.setColor(interiorColor); 58 interiorPaint.setStyle(Style.FILL); 59 interiorPaint.setAntiAlias(false); 60 interiorPaint.setAlpha(255); 61 62 exteriorPaint = new Paint(); 63 exteriorPaint.setTextSize(textSize); 64 exteriorPaint.setColor(exteriorColor); 65 exteriorPaint.setStyle(Style.FILL_AND_STROKE); 66 exteriorPaint.setStrokeWidth(textSize / 8); 67 exteriorPaint.setAntiAlias(false); 68 exteriorPaint.setAlpha(255); 69 70 this.textSize = textSize; 71 } 72 setTypeface(Typeface typeface)73 public void setTypeface(Typeface typeface) { 74 interiorPaint.setTypeface(typeface); 75 exteriorPaint.setTypeface(typeface); 76 } 77 drawText(final Canvas canvas, final float posX, final float posY, final String text)78 public void drawText(final Canvas canvas, final float posX, final float posY, final String text) { 79 canvas.drawText(text, posX, posY, exteriorPaint); 80 canvas.drawText(text, posX, posY, interiorPaint); 81 } 82 drawLines(Canvas canvas, final float posX, final float posY, Vector<String> lines)83 public void drawLines(Canvas canvas, final float posX, final float posY, Vector<String> lines) { 84 int lineNum = 0; 85 for (final String line : lines) { 86 drawText(canvas, posX, posY - getTextSize() * (lines.size() - lineNum - 1), line); 87 ++lineNum; 88 } 89 } 90 setInteriorColor(final int color)91 public void setInteriorColor(final int color) { 92 interiorPaint.setColor(color); 93 } 94 setExteriorColor(final int color)95 public void setExteriorColor(final int color) { 96 exteriorPaint.setColor(color); 97 } 98 getTextSize()99 public float getTextSize() { 100 return textSize; 101 } 102 setAlpha(final int alpha)103 public void setAlpha(final int alpha) { 104 interiorPaint.setAlpha(alpha); 105 exteriorPaint.setAlpha(alpha); 106 } 107 getTextBounds( final String line, final int index, final int count, final Rect lineBounds)108 public void getTextBounds( 109 final String line, final int index, final int count, final Rect lineBounds) { 110 interiorPaint.getTextBounds(line, index, count, lineBounds); 111 } 112 setTextAlign(final Align align)113 public void setTextAlign(final Align align) { 114 interiorPaint.setTextAlign(align); 115 exteriorPaint.setTextAlign(align); 116 } 117 } 118