From 0d034b3169408e2e550f81afaa2b0ae080bcef55 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 29 Nov 2012 14:08:47 +0900 Subject: [PATCH 1/7] Consolidate data and algorithm of calculating rounded line (Step 1) Change-Id: Ic6d72b5a34ddfbc55317fa651f78af6f50c9a284 --- .../internal/GesturePreviewTrail.java | 120 +++++++++--------- 1 file changed, 60 insertions(+), 60 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java index 699aaeaef..42d327820 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java @@ -144,68 +144,68 @@ final class GesturePreviewTrail { // Sweep angle of the trail arc at P2. public float a2; public RectF arc2 = new RectF(); - } - private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); + private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); + private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - private static boolean calculatePathPoints(final WorkingSet w) { - final float dx = w.p2x - w.p1x; - final float dy = w.p2y - w.p1y; - // Distance of the points. - final double l = Math.hypot(dx, dy); - if (Double.compare(0.0d, l) == 0) { - return false; + public static boolean calculatePathPoints(final WorkingSet w) { + final float dx = w.p2x - w.p1x; + final float dy = w.p2y - w.p1y; + // Distance of the points. + final double l = Math.hypot(dx, dy); + if (Double.compare(0.0d, l) == 0) { + return false; + } + // Angle of the line p1-p2 + final float a = (float)Math.atan2(dy, dx); + // Difference of trail cap radius. + final float dr = w.r2 - w.r1; + // Variation of angle at trail cap. + final float ar = (float)Math.asin(dr / l); + // The start angle of trail cap arc at P1. + final float aa = a - (RIGHT_ANGLE + ar); + // The end angle of trail cap arc at P2. + final float ab = a + (RIGHT_ANGLE + ar); + final float cosa = (float)Math.cos(aa); + final float sina = (float)Math.sin(aa); + final float cosb = (float)Math.cos(ab); + final float sinb = (float)Math.sin(ab); + w.p1ax = w.p1x + w.r1 * cosa; + w.p1ay = w.p1y + w.r1 * sina; + w.p1bx = w.p1x + w.r1 * cosb; + w.p1by = w.p1y + w.r1 * sinb; + w.p2ax = w.p2x + w.r2 * cosa; + w.p2ay = w.p2y + w.r2 * sina; + w.p2bx = w.p2x + w.r2 * cosb; + w.p2by = w.p2y + w.r2 * sinb; + w.aa = aa * RADIAN_TO_DEGREE; + final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; + w.a1 = -180.0f + ar2degree; + w.a2 = 180.0f + ar2degree; + w.arc1.set(w.p1x, w.p1y, w.p1x, w.p1y); + w.arc1.inset(-w.r1, -w.r1); + w.arc2.set(w.p2x, w.p2y, w.p2x, w.p2y); + w.arc2.inset(-w.r2, -w.r2); + return true; } - // Angle of the line p1-p2 - final float a = (float)Math.atan2(dy, dx); - // Difference of trail cap radius. - final float dr = w.r2 - w.r1; - // Variation of angle at trail cap. - final float ar = (float)Math.asin(dr / l); - // The start angle of trail cap arc at P1. - final float aa = a - (RIGHT_ANGLE + ar); - // The end angle of trail cap arc at P2. - final float ab = a + (RIGHT_ANGLE + ar); - final float cosa = (float)Math.cos(aa); - final float sina = (float)Math.sin(aa); - final float cosb = (float)Math.cos(ab); - final float sinb = (float)Math.sin(ab); - w.p1ax = w.p1x + w.r1 * cosa; - w.p1ay = w.p1y + w.r1 * sina; - w.p1bx = w.p1x + w.r1 * cosb; - w.p1by = w.p1y + w.r1 * sinb; - w.p2ax = w.p2x + w.r2 * cosa; - w.p2ay = w.p2y + w.r2 * sina; - w.p2bx = w.p2x + w.r2 * cosb; - w.p2by = w.p2y + w.r2 * sinb; - w.aa = aa * RADIAN_TO_DEGREE; - final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; - w.a1 = -180.0f + ar2degree; - w.a2 = 180.0f + ar2degree; - w.arc1.set(w.p1x, w.p1y, w.p1x, w.p1y); - w.arc1.inset(-w.r1, -w.r1); - w.arc2.set(w.p2x, w.p2y, w.p2x, w.p2y); - w.arc2.inset(-w.r2, -w.r2); - return true; - } - private static void createPath(final Path path, final WorkingSet w) { - path.rewind(); - // Trail cap at P1. - path.moveTo(w.p1x, w.p1y); - path.arcTo(w.arc1, w.aa, w.a1); - // Trail cap at P2. - path.moveTo(w.p2x, w.p2y); - path.arcTo(w.arc2, w.aa, w.a2); - // Two trapezoids connecting P1 and P2. - path.moveTo(w.p1ax, w.p1ay); - path.lineTo(w.p1x, w.p1y); - path.lineTo(w.p1bx, w.p1by); - path.lineTo(w.p2bx, w.p2by); - path.lineTo(w.p2x, w.p2y); - path.lineTo(w.p2ax, w.p2ay); - path.close(); + public static void createPath(final Path path, final WorkingSet w) { + path.rewind(); + // Trail cap at P1. + path.moveTo(w.p1x, w.p1y); + path.arcTo(w.arc1, w.aa, w.a1); + // Trail cap at P2. + path.moveTo(w.p2x, w.p2y); + path.arcTo(w.arc2, w.aa, w.a2); + // Two trapezoids connecting P1 and P2. + path.moveTo(w.p1ax, w.p1ay); + path.lineTo(w.p1x, w.p1y); + path.lineTo(w.p1bx, w.p1by); + path.lineTo(w.p2bx, w.p2by); + path.lineTo(w.p2x, w.p2y); + path.lineTo(w.p2ax, w.p2ay); + path.close(); + } } private final WorkingSet mWorkingSet = new WorkingSet(); @@ -262,8 +262,8 @@ final class GesturePreviewTrail { paint.setAlpha(alpha); final float width = getWidth(elapsedTime, params); w.r2 = width / 2.0f; - if (calculatePathPoints(w)) { - createPath(path, w); + if (WorkingSet.calculatePathPoints(w)) { + WorkingSet.createPath(path, w); canvas.drawPath(path, paint); outBoundsRect.union((int)w.p2x, (int)w.p2y); } From 8cfd64eed9b22e1dcce592de1a1780dd5b512183 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 29 Nov 2012 14:22:18 +0900 Subject: [PATCH 2/7] Make rounde line algorithm as instance method (Step 2) Change-Id: Ia6f41dc0ce7d3f3a1e4a8bbe838835cbdd94e6e9 --- .../internal/GesturePreviewTrail.java | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java index 42d327820..a13ecd64d 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java @@ -127,30 +127,29 @@ final class GesturePreviewTrail { public float p2x, p2y; public float r2; - // Output // Closing point of arc at P1. - public float p1ax, p1ay; + private float p1ax, p1ay; // Opening point of arc at P1. - public float p1bx, p1by; + private float p1bx, p1by; // Opening point of arc at P2. - public float p2ax, p2ay; + private float p2ax, p2ay; // Closing point of arc at P2. - public float p2bx, p2by; + private float p2bx, p2by; // Start angle of the trail arcs. - public float aa; + private float angle; // Sweep angle of the trail arc at P1. - public float a1; - public RectF arc1 = new RectF(); + private float a1; + private final RectF arc1 = new RectF(); // Sweep angle of the trail arc at P2. - public float a2; - public RectF arc2 = new RectF(); + private float a2; + private final RectF arc2 = new RectF(); private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - public static boolean calculatePathPoints(final WorkingSet w) { - final float dx = w.p2x - w.p1x; - final float dy = w.p2y - w.p1y; + public boolean calculatePathPoints() { + final float dx = p2x - p1x; + final float dy = p2y - p1y; // Distance of the points. final double l = Math.hypot(dx, dy); if (Double.compare(0.0d, l) == 0) { @@ -159,7 +158,7 @@ final class GesturePreviewTrail { // Angle of the line p1-p2 final float a = (float)Math.atan2(dy, dx); // Difference of trail cap radius. - final float dr = w.r2 - w.r1; + final float dr = r2 - r1; // Variation of angle at trail cap. final float ar = (float)Math.asin(dr / l); // The start angle of trail cap arc at P1. @@ -170,40 +169,40 @@ final class GesturePreviewTrail { final float sina = (float)Math.sin(aa); final float cosb = (float)Math.cos(ab); final float sinb = (float)Math.sin(ab); - w.p1ax = w.p1x + w.r1 * cosa; - w.p1ay = w.p1y + w.r1 * sina; - w.p1bx = w.p1x + w.r1 * cosb; - w.p1by = w.p1y + w.r1 * sinb; - w.p2ax = w.p2x + w.r2 * cosa; - w.p2ay = w.p2y + w.r2 * sina; - w.p2bx = w.p2x + w.r2 * cosb; - w.p2by = w.p2y + w.r2 * sinb; - w.aa = aa * RADIAN_TO_DEGREE; + p1ax = p1x + r1 * cosa; + p1ay = p1y + r1 * sina; + p1bx = p1x + r1 * cosb; + p1by = p1y + r1 * sinb; + p2ax = p2x + r2 * cosa; + p2ay = p2y + r2 * sina; + p2bx = p2x + r2 * cosb; + p2by = p2y + r2 * sinb; + angle = aa * RADIAN_TO_DEGREE; final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; - w.a1 = -180.0f + ar2degree; - w.a2 = 180.0f + ar2degree; - w.arc1.set(w.p1x, w.p1y, w.p1x, w.p1y); - w.arc1.inset(-w.r1, -w.r1); - w.arc2.set(w.p2x, w.p2y, w.p2x, w.p2y); - w.arc2.inset(-w.r2, -w.r2); + a1 = -180.0f + ar2degree; + a2 = 180.0f + ar2degree; + arc1.set(p1x, p1y, p1x, p1y); + arc1.inset(-r1, -r1); + arc2.set(p2x, p2y, p2x, p2y); + arc2.inset(-r2, -r2); return true; } - public static void createPath(final Path path, final WorkingSet w) { + public void createPath(final Path path) { path.rewind(); // Trail cap at P1. - path.moveTo(w.p1x, w.p1y); - path.arcTo(w.arc1, w.aa, w.a1); + path.moveTo(p1x, p1y); + path.arcTo(arc1, angle, a1); // Trail cap at P2. - path.moveTo(w.p2x, w.p2y); - path.arcTo(w.arc2, w.aa, w.a2); + path.moveTo(p2x, p2y); + path.arcTo(arc2, angle, a2); // Two trapezoids connecting P1 and P2. - path.moveTo(w.p1ax, w.p1ay); - path.lineTo(w.p1x, w.p1y); - path.lineTo(w.p1bx, w.p1by); - path.lineTo(w.p2bx, w.p2by); - path.lineTo(w.p2x, w.p2y); - path.lineTo(w.p2ax, w.p2ay); + path.moveTo(p1ax, p1ay); + path.lineTo(p1x, p1y); + path.lineTo(p1bx, p1by); + path.lineTo(p2bx, p2by); + path.lineTo(p2x, p2y); + path.lineTo(p2ax, p2ay); path.close(); } } @@ -262,8 +261,8 @@ final class GesturePreviewTrail { paint.setAlpha(alpha); final float width = getWidth(elapsedTime, params); w.r2 = width / 2.0f; - if (WorkingSet.calculatePathPoints(w)) { - WorkingSet.createPath(path, w); + if (w.calculatePathPoints()) { + w.createPath(path); canvas.drawPath(path, paint); outBoundsRect.union((int)w.p2x, (int)w.p2y); } From 1a6d56e31da2c98e7ab4e60e6bba71b7ea46c4c3 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 29 Nov 2012 14:28:59 +0900 Subject: [PATCH 3/7] Consolidate point calculating and path creating methods into one (Step 3) Change-Id: Id49eee7c38595ea312dc9a63341a376825030035 --- .../internal/GesturePreviewTrail.java | 43 +++++++++---------- 1 file changed, 20 insertions(+), 23 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java index a13ecd64d..900349cea 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java @@ -118,7 +118,7 @@ final class GesturePreviewTrail { / params.mTrailLingerDuration, 0.0f); } - static final class WorkingSet { + static final class RoundedLine { // Input // Previous point (P1) coordinates and trail radius. public float p1x, p1y; @@ -143,17 +143,18 @@ final class GesturePreviewTrail { // Sweep angle of the trail arc at P2. private float a2; private final RectF arc2 = new RectF(); + private final Path path = new Path(); private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - public boolean calculatePathPoints() { + public Path makePath() { final float dx = p2x - p1x; final float dy = p2y - p1y; // Distance of the points. final double l = Math.hypot(dx, dy); if (Double.compare(0.0d, l) == 0) { - return false; + return null; } // Angle of the line p1-p2 final float a = (float)Math.atan2(dy, dx); @@ -185,10 +186,7 @@ final class GesturePreviewTrail { arc1.inset(-r1, -r1); arc2.set(p2x, p2y, p2x, p2y); arc2.inset(-r2, -r2); - return true; - } - public void createPath(final Path path) { path.rewind(); // Trail cap at P1. path.moveTo(p1x, p1y); @@ -204,11 +202,11 @@ final class GesturePreviewTrail { path.lineTo(p2x, p2y); path.lineTo(p2ax, p2ay); path.close(); + return path; } } - private final WorkingSet mWorkingSet = new WorkingSet(); - private final Path mPath = new Path(); + private final RoundedLine mRoundedLine = new RoundedLine(); /** * Draw gesture preview trail @@ -242,36 +240,35 @@ final class GesturePreviewTrail { if (startIndex < trailSize) { paint.setColor(params.mTrailColor); paint.setStyle(Paint.Style.FILL); - final Path path = mPath; - final WorkingSet w = mWorkingSet; - w.p1x = getXCoordValue(xCoords[startIndex]); - w.p1y = yCoords[startIndex]; + final RoundedLine line = mRoundedLine; + line.p1x = getXCoordValue(xCoords[startIndex]); + line.p1y = yCoords[startIndex]; int lastTime = sinceDown - eventTimes[startIndex]; float maxWidth = getWidth(lastTime, params); - w.r1 = maxWidth / 2.0f; + line.r1 = maxWidth / 2.0f; // Initialize bounds rectangle. - outBoundsRect.set((int)w.p1x, (int)w.p1y, (int)w.p1x, (int)w.p1y); + outBoundsRect.set((int)line.p1x, (int)line.p1y, (int)line.p1x, (int)line.p1y); for (int i = startIndex + 1; i < trailSize - 1; i++) { final int elapsedTime = sinceDown - eventTimes[i]; - w.p2x = getXCoordValue(xCoords[i]); - w.p2y = yCoords[i]; + line.p2x = getXCoordValue(xCoords[i]); + line.p2y = yCoords[i]; // Draw trail line only when the current point isn't a down point. if (!isDownEventXCoord(xCoords[i])) { final int alpha = getAlpha(elapsedTime, params); paint.setAlpha(alpha); final float width = getWidth(elapsedTime, params); - w.r2 = width / 2.0f; - if (w.calculatePathPoints()) { - w.createPath(path); + line.r2 = width / 2.0f; + final Path path = line.makePath(); + if (path != null) { canvas.drawPath(path, paint); - outBoundsRect.union((int)w.p2x, (int)w.p2y); + outBoundsRect.union((int)line.p2x, (int)line.p2y); } // Take union for the bounds. maxWidth = Math.max(maxWidth, width); } - w.p1x = w.p2x; - w.p1y = w.p2y; - w.r1 = w.r2; + line.p1x = line.p2x; + line.p1y = line.p2y; + line.r1 = line.r2; lastTime = elapsedTime; } // Take care of trail line width. From f90475b5d4c2f94188ace4b7dd45160a02d1d204 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 29 Nov 2012 14:41:08 +0900 Subject: [PATCH 4/7] Make RoundedLine as top level class (Step 4) Change-Id: Id52baefb3d61eb46b3679e1ba3aa7ef7cd020efc --- .../internal/GesturePreviewTrail.java | 88 --------------- .../keyboard/internal/RoundedLine.java | 105 ++++++++++++++++++ 2 files changed, 105 insertions(+), 88 deletions(-) create mode 100644 java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java index 900349cea..a11f8ef5d 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java @@ -118,94 +118,6 @@ final class GesturePreviewTrail { / params.mTrailLingerDuration, 0.0f); } - static final class RoundedLine { - // Input - // Previous point (P1) coordinates and trail radius. - public float p1x, p1y; - public float r1; - // Current point (P2) coordinates and trail radius. - public float p2x, p2y; - public float r2; - - // Closing point of arc at P1. - private float p1ax, p1ay; - // Opening point of arc at P1. - private float p1bx, p1by; - // Opening point of arc at P2. - private float p2ax, p2ay; - // Closing point of arc at P2. - private float p2bx, p2by; - // Start angle of the trail arcs. - private float angle; - // Sweep angle of the trail arc at P1. - private float a1; - private final RectF arc1 = new RectF(); - // Sweep angle of the trail arc at P2. - private float a2; - private final RectF arc2 = new RectF(); - private final Path path = new Path(); - - private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); - private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - - public Path makePath() { - final float dx = p2x - p1x; - final float dy = p2y - p1y; - // Distance of the points. - final double l = Math.hypot(dx, dy); - if (Double.compare(0.0d, l) == 0) { - return null; - } - // Angle of the line p1-p2 - final float a = (float)Math.atan2(dy, dx); - // Difference of trail cap radius. - final float dr = r2 - r1; - // Variation of angle at trail cap. - final float ar = (float)Math.asin(dr / l); - // The start angle of trail cap arc at P1. - final float aa = a - (RIGHT_ANGLE + ar); - // The end angle of trail cap arc at P2. - final float ab = a + (RIGHT_ANGLE + ar); - final float cosa = (float)Math.cos(aa); - final float sina = (float)Math.sin(aa); - final float cosb = (float)Math.cos(ab); - final float sinb = (float)Math.sin(ab); - p1ax = p1x + r1 * cosa; - p1ay = p1y + r1 * sina; - p1bx = p1x + r1 * cosb; - p1by = p1y + r1 * sinb; - p2ax = p2x + r2 * cosa; - p2ay = p2y + r2 * sina; - p2bx = p2x + r2 * cosb; - p2by = p2y + r2 * sinb; - angle = aa * RADIAN_TO_DEGREE; - final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; - a1 = -180.0f + ar2degree; - a2 = 180.0f + ar2degree; - arc1.set(p1x, p1y, p1x, p1y); - arc1.inset(-r1, -r1); - arc2.set(p2x, p2y, p2x, p2y); - arc2.inset(-r2, -r2); - - path.rewind(); - // Trail cap at P1. - path.moveTo(p1x, p1y); - path.arcTo(arc1, angle, a1); - // Trail cap at P2. - path.moveTo(p2x, p2y); - path.arcTo(arc2, angle, a2); - // Two trapezoids connecting P1 and P2. - path.moveTo(p1ax, p1ay); - path.lineTo(p1x, p1y); - path.lineTo(p1bx, p1by); - path.lineTo(p2bx, p2by); - path.lineTo(p2x, p2y); - path.lineTo(p2ax, p2ay); - path.close(); - return path; - } - } - private final RoundedLine mRoundedLine = new RoundedLine(); /** diff --git a/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java b/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java new file mode 100644 index 000000000..2a5d444cc --- /dev/null +++ b/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.inputmethod.keyboard.internal; + +import android.graphics.Path; +import android.graphics.RectF; + +public final class RoundedLine { + // Start point (P1) coordinates and trail radius. + public float p1x, p1y; + public float r1; + // End point (P2) coordinates and trail radius. + public float p2x, p2y; + public float r2; + + // Closing point of arc at P1. + private float p1ax, p1ay; + // Opening point of arc at P1. + private float p1bx, p1by; + // Opening point of arc at P2. + private float p2ax, p2ay; + // Closing point of arc at P2. + private float p2bx, p2by; + // Start angle of the trail arcs. + private float angle; + // Sweep angle of the trail arc at P1. + private float a1; + private final RectF arc1 = new RectF(); + // Sweep angle of the trail arc at P2. + private float a2; + private final RectF arc2 = new RectF(); + private final Path path = new Path(); + + private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); + private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); + + public Path makePath() { + final float dx = p2x - p1x; + final float dy = p2y - p1y; + // Distance of the points. + final double l = Math.hypot(dx, dy); + if (Double.compare(0.0d, l) == 0) { + return null; + } + // Angle of the line p1-p2 + final float a = (float)Math.atan2(dy, dx); + // Difference of trail cap radius. + final float dr = r2 - r1; + // Variation of angle at trail cap. + final float ar = (float)Math.asin(dr / l); + // The start angle of trail cap arc at P1. + final float aa = a - (RIGHT_ANGLE + ar); + // The end angle of trail cap arc at P2. + final float ab = a + (RIGHT_ANGLE + ar); + final float cosa = (float)Math.cos(aa); + final float sina = (float)Math.sin(aa); + final float cosb = (float)Math.cos(ab); + final float sinb = (float)Math.sin(ab); + p1ax = p1x + r1 * cosa; + p1ay = p1y + r1 * sina; + p1bx = p1x + r1 * cosb; + p1by = p1y + r1 * sinb; + p2ax = p2x + r2 * cosa; + p2ay = p2y + r2 * sina; + p2bx = p2x + r2 * cosb; + p2by = p2y + r2 * sinb; + angle = aa * RADIAN_TO_DEGREE; + final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; + a1 = -180.0f + ar2degree; + a2 = 180.0f + ar2degree; + arc1.set(p1x, p1y, p1x, p1y); + arc1.inset(-r1, -r1); + arc2.set(p2x, p2y, p2x, p2y); + arc2.inset(-r2, -r2); + + path.rewind(); + // Trail cap at P1. + path.moveTo(p1x, p1y); + path.arcTo(arc1, angle, a1); + // Trail cap at P2. + path.moveTo(p2x, p2y); + path.arcTo(arc2, angle, a2); + // Two trapezoids connecting P1 and P2. + path.moveTo(p1ax, p1ay); + path.lineTo(p1x, p1y); + path.lineTo(p1bx, p1by); + path.lineTo(p2bx, p2by); + path.lineTo(p2x, p2y); + path.lineTo(p2ax, p2ay); + path.close(); + return path; + } +} \ No newline at end of file From e14df775d454cb96b751cf7668df71ce5873930d Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" Date: Thu, 29 Nov 2012 15:14:44 +0900 Subject: [PATCH 5/7] Make RoundedLine member variables as method parameters (Step 5) Change-Id: Ib2ade2bf51c293c65fc9206a9a16694e6d18da50 --- .../internal/GesturePreviewTrail.java | 27 ++-- .../keyboard/internal/RoundedLine.java | 126 +++++++++--------- 2 files changed, 75 insertions(+), 78 deletions(-) diff --git a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java index a11f8ef5d..80562cb2c 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java +++ b/java/src/com/android/inputmethod/keyboard/internal/GesturePreviewTrail.java @@ -19,7 +19,6 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; -import android.graphics.RectF; import android.os.SystemClock; import com.android.inputmethod.latin.Constants; @@ -153,34 +152,34 @@ final class GesturePreviewTrail { paint.setColor(params.mTrailColor); paint.setStyle(Paint.Style.FILL); final RoundedLine line = mRoundedLine; - line.p1x = getXCoordValue(xCoords[startIndex]); - line.p1y = yCoords[startIndex]; + int p1x = getXCoordValue(xCoords[startIndex]); + int p1y = yCoords[startIndex]; int lastTime = sinceDown - eventTimes[startIndex]; float maxWidth = getWidth(lastTime, params); - line.r1 = maxWidth / 2.0f; + float r1 = maxWidth / 2.0f; // Initialize bounds rectangle. - outBoundsRect.set((int)line.p1x, (int)line.p1y, (int)line.p1x, (int)line.p1y); + outBoundsRect.set(p1x, p1y, p1x, p1y); for (int i = startIndex + 1; i < trailSize - 1; i++) { final int elapsedTime = sinceDown - eventTimes[i]; - line.p2x = getXCoordValue(xCoords[i]); - line.p2y = yCoords[i]; + final int p2x = getXCoordValue(xCoords[i]); + final int p2y = yCoords[i]; + final float width = getWidth(elapsedTime, params); + final float r2 = width / 2.0f; // Draw trail line only when the current point isn't a down point. if (!isDownEventXCoord(xCoords[i])) { final int alpha = getAlpha(elapsedTime, params); paint.setAlpha(alpha); - final float width = getWidth(elapsedTime, params); - line.r2 = width / 2.0f; - final Path path = line.makePath(); + final Path path = line.makePath(p1x, p1y, r1, p2x, p2y, r2); if (path != null) { canvas.drawPath(path, paint); - outBoundsRect.union((int)line.p2x, (int)line.p2y); + outBoundsRect.union(p2x, p2y); } // Take union for the bounds. maxWidth = Math.max(maxWidth, width); } - line.p1x = line.p2x; - line.p1y = line.p2y; - line.r1 = line.r2; + p1x = p2x; + p1y = p2y; + r1 = r2; lastTime = elapsedTime; } // Take care of trail line width. diff --git a/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java b/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java index 2a5d444cc..1f5252077 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java +++ b/java/src/com/android/inputmethod/keyboard/internal/RoundedLine.java @@ -18,88 +18,86 @@ import android.graphics.Path; import android.graphics.RectF; public final class RoundedLine { - // Start point (P1) coordinates and trail radius. - public float p1x, p1y; - public float r1; - // End point (P2) coordinates and trail radius. - public float p2x, p2y; - public float r2; + private final RectF mArc1 = new RectF(); + private final RectF mArc2 = new RectF(); + private final Path mPath = new Path(); - // Closing point of arc at P1. - private float p1ax, p1ay; - // Opening point of arc at P1. - private float p1bx, p1by; - // Opening point of arc at P2. - private float p2ax, p2ay; - // Closing point of arc at P2. - private float p2bx, p2by; - // Start angle of the trail arcs. - private float angle; - // Sweep angle of the trail arc at P1. - private float a1; - private final RectF arc1 = new RectF(); - // Sweep angle of the trail arc at P2. - private float a2; - private final RectF arc2 = new RectF(); - private final Path path = new Path(); + private static final double RADIAN_TO_DEGREE = 180.0d / Math.PI; + private static final double RIGHT_ANGLE = Math.PI / 2.0d; - private static final float RADIAN_TO_DEGREE = (float)(180.0d / Math.PI); - private static final float RIGHT_ANGLE = (float)(Math.PI / 2.0d); - - public Path makePath() { - final float dx = p2x - p1x; - final float dy = p2y - p1y; + /** + * Make a rounded line path + * + * @param p1x the x-coordinate of the start point. + * @param p1y the y-coordinate of the start point. + * @param r1 the radius at the start point + * @param p2x the x-coordinate of the end point. + * @param p2y the y-coordinate of the end point. + * @param r2 the radius at the end point + * @return the path of rounded line + */ + public Path makePath(final float p1x, final float p1y, final float r1, + final float p2x, final float p2y, final float r2) { + final double dx = p2x - p1x; + final double dy = p2y - p1y; // Distance of the points. final double l = Math.hypot(dx, dy); if (Double.compare(0.0d, l) == 0) { return null; } // Angle of the line p1-p2 - final float a = (float)Math.atan2(dy, dx); + final double a = Math.atan2(dy, dx); // Difference of trail cap radius. - final float dr = r2 - r1; + final double dr = r2 - r1; // Variation of angle at trail cap. - final float ar = (float)Math.asin(dr / l); + final double ar = Math.asin(dr / l); // The start angle of trail cap arc at P1. - final float aa = a - (RIGHT_ANGLE + ar); + final double aa = a - (RIGHT_ANGLE + ar); // The end angle of trail cap arc at P2. - final float ab = a + (RIGHT_ANGLE + ar); + final double ab = a + (RIGHT_ANGLE + ar); final float cosa = (float)Math.cos(aa); final float sina = (float)Math.sin(aa); final float cosb = (float)Math.cos(ab); final float sinb = (float)Math.sin(ab); - p1ax = p1x + r1 * cosa; - p1ay = p1y + r1 * sina; - p1bx = p1x + r1 * cosb; - p1by = p1y + r1 * sinb; - p2ax = p2x + r2 * cosa; - p2ay = p2y + r2 * sina; - p2bx = p2x + r2 * cosb; - p2by = p2y + r2 * sinb; - angle = aa * RADIAN_TO_DEGREE; - final float ar2degree = ar * 2.0f * RADIAN_TO_DEGREE; - a1 = -180.0f + ar2degree; - a2 = 180.0f + ar2degree; - arc1.set(p1x, p1y, p1x, p1y); - arc1.inset(-r1, -r1); - arc2.set(p2x, p2y, p2x, p2y); - arc2.inset(-r2, -r2); + // Closing point of arc at P1. + final float p1ax = p1x + r1 * cosa; + final float p1ay = p1y + r1 * sina; + // Opening point of arc at P1. + final float p1bx = p1x + r1 * cosb; + final float p1by = p1y + r1 * sinb; + // Opening point of arc at P2. + final float p2ax = p2x + r2 * cosa; + final float p2ay = p2y + r2 * sina; + // Closing point of arc at P2. + final float p2bx = p2x + r2 * cosb; + final float p2by = p2y + r2 * sinb; + // Start angle of the trail arcs. + final float angle = (float)(aa * RADIAN_TO_DEGREE); + final float ar2degree = (float)(ar * 2.0d * RADIAN_TO_DEGREE); + // Sweep angle of the trail arc at P1. + final float a1 = -180.0f + ar2degree; + // Sweep angle of the trail arc at P2. + final float a2 = 180.0f + ar2degree; + mArc1.set(p1x, p1y, p1x, p1y); + mArc1.inset(-r1, -r1); + mArc2.set(p2x, p2y, p2x, p2y); + mArc2.inset(-r2, -r2); - path.rewind(); + mPath.rewind(); // Trail cap at P1. - path.moveTo(p1x, p1y); - path.arcTo(arc1, angle, a1); + mPath.moveTo(p1x, p1y); + mPath.arcTo(mArc1, angle, a1); // Trail cap at P2. - path.moveTo(p2x, p2y); - path.arcTo(arc2, angle, a2); + mPath.moveTo(p2x, p2y); + mPath.arcTo(mArc2, angle, a2); // Two trapezoids connecting P1 and P2. - path.moveTo(p1ax, p1ay); - path.lineTo(p1x, p1y); - path.lineTo(p1bx, p1by); - path.lineTo(p2bx, p2by); - path.lineTo(p2x, p2y); - path.lineTo(p2ax, p2ay); - path.close(); - return path; + mPath.moveTo(p1ax, p1ay); + mPath.lineTo(p1x, p1y); + mPath.lineTo(p1bx, p1by); + mPath.lineTo(p2bx, p2by); + mPath.lineTo(p2x, p2y); + mPath.lineTo(p2ax, p2ay); + mPath.close(); + return mPath; } -} \ No newline at end of file +} From 02996dc56a6088d2ef594d36a769d1d3d97e2778 Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Thu, 29 Nov 2012 19:09:01 +0900 Subject: [PATCH 6/7] Clean up dependency for dicttool bug: 7635215 Change-Id: Ie50297ab592261390885c593341b6c46161b2e63 --- tools/dicttool/Android.mk | 14 +++++++++----- tools/dicttool/tests/etc/test-dicttool.sh | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/tools/dicttool/Android.mk b/tools/dicttool/Android.mk index 159c1c160..666887a2e 100644 --- a/tools/dicttool/Android.mk +++ b/tools/dicttool/Android.mk @@ -21,18 +21,22 @@ LATINIME_CORE_SOURCE_DIRECTORY := $(LATINIME_BASE_SOURCE_DIRECTORY)/latin LATINIME_ANNOTATIONS_SOURCE_DIRECTORY := $(LATINIME_BASE_SOURCE_DIRECTORY)/annotations MAKEDICT_CORE_SOURCE_DIRECTORY := $(LATINIME_CORE_SOURCE_DIRECTORY)/makedict -LOCAL_MAIN_SRC_FILES := $(call all-java-files-under,$(MAKEDICT_CORE_SOURCE_DIRECTORY)) -LOCAL_TOOL_SRC_FILES := $(call all-java-files-under,src) -LOCAL_ANNOTATIONS_SRC_FILES := $(call all-java-files-under,$(LATINIME_ANNOTATIONS_SOURCE_DIRECTORY)) +LOCAL_MAIN_SRC_FILES := $(call all-java-files-under, $(MAKEDICT_CORE_SOURCE_DIRECTORY)) +LOCAL_TOOL_SRC_FILES := $(call all-java-files-under, src) +LOCAL_ANNOTATIONS_SRC_FILES := \ + $(call all-java-files-under, $(LATINIME_ANNOTATIONS_SOURCE_DIRECTORY)) LOCAL_SRC_FILES := $(LOCAL_TOOL_SRC_FILES) \ $(filter-out $(addprefix %/, $(notdir $(LOCAL_TOOL_SRC_FILES))), $(LOCAL_MAIN_SRC_FILES)) \ - $(call all-java-files-under,tests) \ $(LOCAL_ANNOTATIONS_SRC_FILES) \ $(LATINIME_CORE_SOURCE_DIRECTORY)/Constants.java +ifeq ($(DICTTOOL_UNITTEST), true) + LOCAL_SRC_FILES += $(call all-java-files-under, tests) + LOCAL_JAVA_LIBRARIES := junit +endif + LOCAL_JAR_MANIFEST := etc/manifest.txt LOCAL_MODULE := dicttool_aosp -LOCAL_JAVA_LIBRARIES := junit include $(BUILD_HOST_JAVA_LIBRARY) include $(LOCAL_PATH)/etc/Android.mk diff --git a/tools/dicttool/tests/etc/test-dicttool.sh b/tools/dicttool/tests/etc/test-dicttool.sh index 0f3ed6d62..092120769 100755 --- a/tools/dicttool/tests/etc/test-dicttool.sh +++ b/tools/dicttool/tests/etc/test-dicttool.sh @@ -13,5 +13,16 @@ # See the License for the specific language governing permissions and # limitations under the License. +if [[ $(type -t mmm) != function ]]; then +echo "Usage:" 1>&2 +echo " source $0" 1>&2 +echo " or" 1>&2 +echo " . $0" 1>&2 +exit 1 +fi + +find out -name "dicttool_aosp*" -exec rm -rf {} \; > /dev/null 2>&1 +mmm -j8 external/junit +DICTTOOL_UNITTEST=true mmm -j8 packages/inputmethods/LatinIME/tools/dicttool java -classpath ${ANDROID_HOST_OUT}/framework/junit.jar:${ANDROID_HOST_OUT}/framework/dicttool_aosp.jar junit.textui.TestRunner com.android.inputmethod.latin.makedict.BinaryDictInputOutputTest java -classpath ${ANDROID_HOST_OUT}/framework/junit.jar:${ANDROID_HOST_OUT}/framework/dicttool_aosp.jar junit.textui.TestRunner com.android.inputmethod.latin.dicttool.BinaryDictOffdeviceUtilsTests From 22025c6a371edf7f19ad2ff37e5418b85e023c0a Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Thu, 29 Nov 2012 17:33:53 +0900 Subject: [PATCH 7/7] Add utilities to read header values. Bug: 7540132 Change-Id: I19d85481135e79d8782f711da5cbb3a5a7bc06f8 --- native/jni/src/binary_format.h | 68 ++++++++++++++++++++++++++++++++++ native/jni/src/defines.h | 6 +++ 2 files changed, 74 insertions(+) diff --git a/native/jni/src/binary_format.h b/native/jni/src/binary_format.h index 9a8c315f7..1c211e412 100644 --- a/native/jni/src/binary_format.h +++ b/native/jni/src/binary_format.h @@ -17,6 +17,7 @@ #ifndef LATINIME_BINARY_FORMAT_H #define LATINIME_BINARY_FORMAT_H +#include #include #include #include "bloom_filter.h" @@ -64,6 +65,9 @@ class BinaryFormat { static int detectFormat(const uint8_t *const dict); static unsigned int getHeaderSize(const uint8_t *const dict); static unsigned int getFlags(const uint8_t *const dict); + static void readHeaderValue(const uint8_t *const dict, const char *const key, + int *outValue, const int outValueSize); + static int readHeaderValueInt(const uint8_t *const dict, const char *const key); static int getGroupCountAndForwardPointer(const uint8_t *const dict, int *pos); static uint8_t getFlagsAndForwardPointer(const uint8_t *const dict, int *pos); static int getCodePointAndForwardPointer(const uint8_t *const dict, int *pos); @@ -167,6 +171,70 @@ inline unsigned int BinaryFormat::getHeaderSize(const uint8_t *const dict) { } } +inline void BinaryFormat::readHeaderValue(const uint8_t *const dict, const char *const key, + int *outValue, const int outValueSize) { + int outValueIndex = 0; + // Only format 2 and above have header attributes as {key,value} string pairs. For prior + // formats, we just return an empty string, as if the key wasn't found. + if (2 <= detectFormat(dict)) { + const int headerOptionsOffset = 4 /* magic number */ + + 2 /* dictionary version */ + 2 /* flags */; + const int headerSize = + (dict[headerOptionsOffset] << 24) + (dict[headerOptionsOffset + 1] << 16) + + (dict[headerOptionsOffset + 2] << 8) + dict[headerOptionsOffset + 3]; + const int headerEnd = headerOptionsOffset + 4 + headerSize; + int index = headerOptionsOffset + 4; + while (index < headerEnd) { + int keyIndex = 0; + int codePoint = getCodePointAndForwardPointer(dict, &index); + while (codePoint != NOT_A_CODE_POINT) { + if (codePoint != key[keyIndex++]) { + break; + } + codePoint = getCodePointAndForwardPointer(dict, &index); + } + if (codePoint == NOT_A_CODE_POINT && key[keyIndex] == 0) { + // We found the key! Copy and return the value. + codePoint = getCodePointAndForwardPointer(dict, &index); + while (codePoint != NOT_A_CODE_POINT + && outValueIndex < outValueSize) { + outValue[outValueIndex++] = codePoint; + codePoint = getCodePointAndForwardPointer(dict, &index); + } + if (outValueIndex < outValueIndex) outValue[outValueIndex] = 0; + // Finished copying. Break to go to the termination code. + break; + } + // We didn't find the key, skip the remainder of it and its value + while (codePoint != NOT_A_CODE_POINT) { + codePoint = getCodePointAndForwardPointer(dict, &index); + } + codePoint = getCodePointAndForwardPointer(dict, &index); + while (codePoint != NOT_A_CODE_POINT) { + codePoint = getCodePointAndForwardPointer(dict, &index); + } + } + // We couldn't find it - fall through and return an empty value. + } + // Put a terminator 0 if possible at all (always unless outValueSize is <= 0) + if (outValueIndex >= outValueSize) outValueIndex = outValueSize - 1; + if (outValueIndex >= 0) outValue[outValueIndex] = 0; + return; +} + +inline int BinaryFormat::readHeaderValueInt(const uint8_t *const dict, const char *const key) { + const int bufferSize = LARGEST_INT_DIGIT_COUNT; + int intBuffer[bufferSize]; + char charBuffer[bufferSize]; + BinaryFormat::readHeaderValue(dict, key, intBuffer, bufferSize); + for (int i = 0; i < bufferSize; ++i) { + charBuffer[i] = intBuffer[i]; + } + // If not a number, return S_INT_MIN + if (!isdigit(charBuffer[0])) return S_INT_MIN; + return atoi(charBuffer); +} + AK_FORCE_INLINE int BinaryFormat::getGroupCountAndForwardPointer(const uint8_t *const dict, int *pos) { const int msb = dict[(*pos)++]; diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h index 894e5f1c9..ebdff7f09 100644 --- a/native/jni/src/defines.h +++ b/native/jni/src/defines.h @@ -251,6 +251,12 @@ static inline void prof_out(void) { // GCC warns about this. #define S_INT_MIN (-2147483647 - 1) // -(1 << 31) #endif +// Number of base-10 digits in the largest integer + 1 to leave room for a zero terminator. +// As such, this is the maximum number of characters will be needed to represent an int as a +// string, including the terminator; this is used as the size of a string buffer large enough to +// hold any value that is intended to fit in an integer, e.g. in the code that reads the header +// of the binary dictionary where a {key,value} string pair scheme is used. +#define LARGEST_INT_DIGIT_COUNT 11 // Define this to use mmap() for dictionary loading. Undefine to use malloc() instead of mmap(). // We measured and compared performance of both, and found mmap() is fairly good in terms of