Home | History | Annotate | Download | only in graphics
      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 /**
     18  * Utilities for making Minikin work, especially from existing objects like
     19  * Paint and so on.
     20  **/
     21 
     22  // TODO: does this really need to be separate from MinikinSkia?
     23 
     24 #ifndef ANDROID_MINIKIN_UTILS_H
     25 #define ANDROID_MINIKIN_UTILS_H
     26 
     27 #include <minikin/Layout.h>
     28 #include "Paint.h"
     29 #include "MinikinSkia.h"
     30 #include "TypefaceImpl.h"
     31 
     32 namespace android {
     33 
     34 // TODO: these should be defined in Minikin's Layout.h
     35 enum {
     36     kBidi_LTR = 0,
     37     kBidi_RTL = 1,
     38     kBidi_Default_LTR = 2,
     39     kBidi_Default_RTL = 3,
     40     kBidi_Force_LTR = 4,
     41     kBidi_Force_RTL = 5,
     42 
     43     kBidi_Mask = 0x7
     44 };
     45 
     46 class MinikinUtils {
     47 public:
     48     static void doLayout(Layout* layout, const Paint* paint, int bidiFlags, TypefaceImpl* typeface,
     49             const uint16_t* buf, size_t start, size_t count, size_t bufSize);
     50 
     51     static float xOffsetForTextAlign(Paint* paint, const Layout& layout);
     52 
     53     static float hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path);
     54     // f is a functor of type void f(size_t start, size_t end);
     55     template <typename F>
     56     static void forFontRun(const Layout& layout, Paint* paint, F& f) {
     57         float saveSkewX = paint->getTextSkewX();
     58         bool savefakeBold = paint->isFakeBoldText();
     59         MinikinFont* curFont = NULL;
     60         size_t start = 0;
     61         size_t nGlyphs = layout.nGlyphs();
     62         for (size_t i = 0; i < nGlyphs; i++) {
     63             MinikinFont* nextFont = layout.getFont(i);
     64             if (i > 0 && nextFont != curFont) {
     65                 MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
     66                 f(start, i);
     67                 paint->setTextSkewX(saveSkewX);
     68                 paint->setFakeBoldText(savefakeBold);
     69                 start = i;
     70             }
     71             curFont = nextFont;
     72         }
     73         if (nGlyphs > start) {
     74             MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
     75             f(start, nGlyphs);
     76             paint->setTextSkewX(saveSkewX);
     77             paint->setFakeBoldText(savefakeBold);
     78         }
     79     }
     80 };
     81 
     82 }  // namespace android
     83 
     84 #endif  // ANDROID_MINIKIN_UTILS_H
     85