/android/LGame-Android-0.3.1/src/org/loon/framework/android/game/core/graphics/LFont.java

http://loon-simple.googlecode.com/ · Java · 307 lines · 225 code · 61 blank · 21 comment · 30 complexity · 6101f7c62cc6f042bfdd1fb31f1b503c MD5 · raw file

  1. package org.loon.framework.android.game.core.graphics;
  2. import java.util.HashMap;
  3. import org.loon.framework.android.game.core.LSystem;
  4. import android.graphics.Paint;
  5. import android.graphics.Rect;
  6. import android.graphics.Typeface;
  7. import android.graphics.Paint.FontMetrics;
  8. /**
  9. *
  10. * Copyright 2008 - 2009
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  13. * use this file except in compliance with the License. You may obtain a copy of
  14. * the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  21. * License for the specific language governing permissions and limitations under
  22. * the License.
  23. *
  24. * @project loonframework
  25. * @author chenpeng
  26. * @email ceponline@yahoo.com.cn
  27. * @version 0.1.0
  28. */
  29. public class LFont {
  30. public static final int LEFT = 1;
  31. public static final int RIGHT = 2;
  32. public static final int CENTER = 3;
  33. public static final int JUSTIFY = 4;
  34. final static public int FACE_SYSTEM = 0;
  35. final static public int FACE_MONOSPACE = 32;
  36. final static public int FACE_PROPORTIONAL = 64;
  37. final static public int FONT_STATIC_TEXT = 0;
  38. final static public int FONT_INPUT_TEXT = 1;
  39. final static public int SIZE_SMALL = 8;
  40. final static public int SIZE_LARGE = 16;
  41. final static public int SIZE_MEDIUM = 0;
  42. final static public int STYLE_PLAIN = 0;
  43. final static public int STYLE_BOLD = 1;
  44. final static public int STYLE_ITALIC = 2;
  45. final static public int STYLE_UNDERLINED = 4;
  46. final private static String tmp = "H";
  47. final private static HashMap<String, Object> fonts = new HashMap<String, Object>(
  48. 100);
  49. final private Rect rect = new Rect();
  50. private Paint typefacePaint;
  51. private int fontSize;
  52. public static LFont getDefaultFont() {
  53. return LFont.getFont(LSystem.FONT_NAME, 0, 20);
  54. }
  55. public static LFont getFont(int size) {
  56. return LFont.getFont(LSystem.FONT_NAME, 0, size);
  57. }
  58. public static LFont getFont(String familyName, int size) {
  59. return getFont(familyName, 0, size);
  60. }
  61. public static LFont getFont(String familyName, int style, int size) {
  62. String name = (familyName + style + size).toLowerCase();
  63. Object o = fonts.get(name);
  64. if (o == null) {
  65. if (familyName != null) {
  66. if (familyName.equalsIgnoreCase("Serif")
  67. || familyName.equalsIgnoreCase("TimesRoman")) {
  68. familyName = "serif";
  69. } else if (familyName.equalsIgnoreCase("SansSerif")
  70. || familyName.equalsIgnoreCase("Helvetica")) {
  71. familyName = "sans-serif";
  72. } else if (familyName.equalsIgnoreCase("Monospaced")
  73. || familyName.equalsIgnoreCase("Courier")
  74. || familyName.equalsIgnoreCase("Dialog")) {
  75. familyName = "monospace";
  76. }
  77. }
  78. Typeface face = Typeface.create(familyName, style);
  79. Paint paint = new Paint();
  80. paint.setTypeface(face);
  81. paint.setTextSize(size);
  82. paint.setAntiAlias(true);
  83. fonts.put(name, o = new LFont(paint, size));
  84. }
  85. return (LFont) o;
  86. }
  87. public static LFont getFromAssetFont(String path, int style, int fontSize) {
  88. return new LFont(Typeface.DEFAULT, path, fontSize);
  89. }
  90. public static LFont getFont(int face, int style, int fontSize) {
  91. LFont font = new LFont(fontSize);
  92. return getFont(font, face, style, fontSize);
  93. }
  94. public static LFont getFont(LFont font, int face, int style, int fontSize) {
  95. int paintFlags = 0;
  96. int typefaceStyle = Typeface.NORMAL;
  97. Typeface base;
  98. switch (face) {
  99. case FACE_MONOSPACE:
  100. base = Typeface.MONOSPACE;
  101. break;
  102. case FACE_SYSTEM:
  103. base = Typeface.DEFAULT;
  104. break;
  105. case FACE_PROPORTIONAL:
  106. base = Typeface.SANS_SERIF;
  107. break;
  108. default:
  109. throw new IllegalArgumentException("unknown font " + face);
  110. }
  111. if ((style & STYLE_BOLD) != 0) {
  112. typefaceStyle |= Typeface.BOLD;
  113. }
  114. if ((style & STYLE_ITALIC) != 0) {
  115. typefaceStyle |= Typeface.ITALIC;
  116. }
  117. if ((style & STYLE_UNDERLINED) != 0) {
  118. paintFlags |= Paint.UNDERLINE_TEXT_FLAG;
  119. }
  120. Typeface typeface = Typeface.create(base, typefaceStyle);
  121. Paint paint = new Paint(paintFlags);
  122. paint.setTypeface(typeface);
  123. font.setTypefacePaint(paint);
  124. return font;
  125. }
  126. private static Paint createPaint(Typeface typeface) {
  127. Paint paint = new Paint();
  128. paint.setTypeface(typeface);
  129. return paint;
  130. }
  131. private LFont(int fontSize) {
  132. this.fontSize = fontSize;
  133. }
  134. private LFont(Typeface typeface, int fontSize) {
  135. this(createPaint(typeface), fontSize);
  136. }
  137. private LFont(Paint typefacePaint, int fontSize) {
  138. this.fontSize = fontSize;
  139. this.setTypefacePaint(typefacePaint);
  140. }
  141. private LFont(Typeface typeface, String path, int fontSize) {
  142. this(createPaint(typeface), path, fontSize);
  143. }
  144. private LFont(Paint typefacePaint, String path, int fontSize) {
  145. Typeface face = Typeface.createFromAsset(LSystem.screenActivity
  146. .getAssets(), path);
  147. this.fontSize = fontSize;
  148. this.typefacePaint.setTypeface(face);
  149. this.setTypefacePaint(typefacePaint);
  150. }
  151. public float getScale() {
  152. int fontSize = this.getSize();
  153. float scale;
  154. if (fontSize == LFont.SIZE_LARGE) {
  155. scale = 1.5F;
  156. } else if (fontSize == LFont.SIZE_SMALL) {
  157. scale = 0.8F;
  158. } else {
  159. scale = 1;
  160. }
  161. return scale;
  162. }
  163. public Paint getTypefacePaint() {
  164. return this.typefacePaint;
  165. }
  166. public float getAscent() {
  167. return typefacePaint.ascent();
  168. }
  169. public float getDescent() {
  170. return typefacePaint.descent();
  171. }
  172. public float getLeading() {
  173. return (typefacePaint.getFontMetrics().leading + 2) * 2;
  174. }
  175. public void setTypefacePaint(Paint typefacePaint) {
  176. this.typefacePaint = typefacePaint;
  177. this.typefacePaint.setTextSize(getSize());
  178. }
  179. public int getBaselinePosition() {
  180. return Math.round(-this.typefacePaint.ascent() * getSize());
  181. }
  182. public int getFace() {
  183. return FACE_SYSTEM;
  184. }
  185. public FontMetrics getFontMetrics() {
  186. return typefacePaint.getFontMetrics();
  187. }
  188. public int getLineHeight() {
  189. return ((int) Math.ceil(Math.abs(typefacePaint.getFontMetrics().ascent)
  190. + Math.abs(typefacePaint.getFontMetrics().descent))) - 2;
  191. }
  192. public int getStyle() {
  193. int style = STYLE_PLAIN;
  194. Typeface typeface = this.typefacePaint.getTypeface();
  195. if (typeface.isBold()) {
  196. style |= STYLE_BOLD;
  197. }
  198. if (typeface.isItalic()) {
  199. style |= STYLE_ITALIC;
  200. }
  201. if (this.typefacePaint.isUnderlineText()) {
  202. style |= STYLE_UNDERLINED;
  203. }
  204. return style;
  205. }
  206. public boolean isBold() {
  207. return this.typefacePaint.getTypeface().isBold();
  208. }
  209. public boolean isItalic() {
  210. return this.typefacePaint.getTypeface().isItalic();
  211. }
  212. public boolean isPlain() {
  213. return this.getStyle() == STYLE_PLAIN;
  214. }
  215. public String getFontName() {
  216. return LSystem.FONT_NAME;
  217. }
  218. public int getSize() {
  219. return this.fontSize;
  220. }
  221. public boolean isUnderlined() {
  222. return this.typefacePaint.isUnderlineText();
  223. }
  224. public int charWidth(char ch) {
  225. char[] chars = Character.toChars(ch);
  226. int w = (int) typefacePaint.measureText(chars, 0, 1);
  227. return w;
  228. }
  229. public int stringWidth(String str) {
  230. return (int) typefacePaint.measureText(str);
  231. }
  232. public int subStringWidth(String str, int offset, int len) {
  233. return stringWidth(str.substring(offset, len));
  234. }
  235. public int getHeight() {
  236. return typefacePaint.getFontMetricsInt(typefacePaint
  237. .getFontMetricsInt());
  238. }
  239. public int getTextHeight() {
  240. return (int) (getTextBounds(tmp).height() * 2);
  241. }
  242. public Rect getTextBounds(String text) {
  243. typefacePaint.getTextBounds(text, 0, text.length(), rect);
  244. return rect;
  245. }
  246. }