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_GRAPHICS_MINIKIN_UTILS_H_
     25 #define _ANDROID_GRAPHICS_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 class MinikinUtils {
     35 public:
     36     static FontStyle prepareMinikinPaint(MinikinPaint* minikinPaint, FontCollection** pFont,
     37             const Paint* paint, TypefaceImpl* typeface);
     38 
     39     static void doLayout(Layout* layout, const Paint* paint, int bidiFlags,
     40             TypefaceImpl* typeface, const uint16_t* buf, size_t start, size_t count,
     41             size_t bufSize);
     42 
     43     static float xOffsetForTextAlign(Paint* paint, const Layout& layout);
     44 
     45     static float hOffsetForTextAlign(Paint* paint, const Layout& layout, const SkPath& path);
     46     // f is a functor of type void f(size_t start, size_t end);
     47     template <typename F>
     48     static void forFontRun(const Layout& layout, Paint* paint, F& f) {
     49         float saveSkewX = paint->getTextSkewX();
     50         bool savefakeBold = paint->isFakeBoldText();
     51         MinikinFont* curFont = NULL;
     52         size_t start = 0;
     53         size_t nGlyphs = layout.nGlyphs();
     54         for (size_t i = 0; i < nGlyphs; i++) {
     55             MinikinFont* nextFont = layout.getFont(i);
     56             if (i > 0 && nextFont != curFont) {
     57                 MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
     58                 f(start, i);
     59                 paint->setTextSkewX(saveSkewX);
     60                 paint->setFakeBoldText(savefakeBold);
     61                 start = i;
     62             }
     63             curFont = nextFont;
     64         }
     65         if (nGlyphs > start) {
     66             MinikinFontSkia::populateSkPaint(paint, curFont, layout.getFakery(start));
     67             f(start, nGlyphs);
     68             paint->setTextSkewX(saveSkewX);
     69             paint->setFakeBoldText(savefakeBold);
     70         }
     71     }
     72 };
     73 
     74 }  // namespace android
     75 
     76 #endif  // _ANDROID_GRAPHICS_MINIKIN_UTILS_H_
     77