1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #define LOG_TAG "Minikin" 18 #include <cutils/log.h> 19 #include <string> 20 21 #include "SkPathMeasure.h" 22 #include "Paint.h" 23 #include "TypefaceImpl.h" 24 25 #include "MinikinUtils.h" 26 27 namespace android { 28 29 // Do an sprintf starting at offset n, abort on overflow 30 static int snprintfcat(char* buf, int off, int size, const char* format, ...) 31 __attribute__((__format__(__printf__, 4, 5))); 32 static int snprintfcat(char* buf, int off, int size, const char* format, ...) { 33 va_list args; 34 va_start(args, format); 35 int n = vsnprintf(buf + off, size - off, format, args); 36 LOG_ALWAYS_FATAL_IF(n >= size - off, "String overflow in setting layout properties"); 37 va_end(args); 38 return off + n; 39 } 40 41 void MinikinUtils::doLayout(Layout* layout, const Paint* paint, int bidiFlags, TypefaceImpl* typeface, 42 const uint16_t* buf, size_t start, size_t count, size_t bufSize) { 43 TypefaceImpl* resolvedFace = TypefaceImpl_resolveDefault(typeface); 44 layout->setFontCollection(resolvedFace->fFontCollection); 45 FontStyle resolved = resolvedFace->fStyle; 46 47 /* Prepare minikin FontStyle */ 48 std::string lang = paint->getTextLocale(); 49 FontLanguage minikinLang(lang.c_str(), lang.size()); 50 FontVariant minikinVariant = (paint->getFontVariant() == VARIANT_ELEGANT) ? VARIANT_ELEGANT 51 : VARIANT_COMPACT; 52 FontStyle minikinStyle(minikinLang, minikinVariant, resolved.getWeight(), resolved.getItalic()); 53 54 /* Prepare minikin Paint */ 55 MinikinPaint minikinPaint; 56 minikinPaint.size = (int)/*WHY?!*/paint->getTextSize(); 57 minikinPaint.scaleX = paint->getTextScaleX(); 58 minikinPaint.skewX = paint->getTextSkewX(); 59 minikinPaint.letterSpacing = paint->getLetterSpacing(); 60 minikinPaint.paintFlags = MinikinFontSkia::packPaintFlags(paint); 61 minikinPaint.fontFeatureSettings = paint->getFontFeatureSettings(); 62 63 layout->doLayout(buf, start, count, bufSize, bidiFlags, minikinStyle, minikinPaint); 64 } 65 66 float MinikinUtils::xOffsetForTextAlign(Paint* paint, const Layout& layout) { 67 switch (paint->getTextAlign()) { 68 case Paint::kCenter_Align: 69 return layout.getAdvance() * -0.5f; 70 break; 71 case Paint::kRight_Align: 72 return -layout.getAdvance(); 73 break; 74 default: 75 break; 76 } 77 return 0; 78 } 79 80 float MinikinUtils::hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path) { 81 float align = 0; 82 switch (paint->getTextAlign()) { 83 case Paint::kCenter_Align: 84 align = -0.5f; 85 break; 86 case Paint::kRight_Align: 87 align = -1; 88 break; 89 default: 90 return 0; 91 } 92 SkPathMeasure measure(path, false); 93 return align * (layout.getAdvance() - measure.getLength()); 94 } 95 96 } 97