Skip to content

Commit

Permalink
Merge pull request #11 from emoji-gen/feature/update-skia
Browse files Browse the repository at this point in the history
Update Skia
  • Loading branch information
pine authored Jul 28, 2019
2 parents 2f76aa7 + ce14de9 commit 999f9bb
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 33 deletions.
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ Makefile
lib
tmp
*.o
*.png
*.webp

# ninja
*.ninja
.ninja_deps
.ninja_log

# Example
# project
example/*.png
example/example
bin/
*.png
*.webp
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(EXECUTABLE_OUTPUT_PATH "example")
set(EXECUTABLE_OUTPUT_PATH "bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "tmp")
include_directories(
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/src
${CMAKE_SOURCE_DIR}/externals/skia
${CMAKE_SOURCE_DIR}/externals/skia/include/core
${CMAKE_SOURCE_DIR}/externals/skia/include/config
${CMAKE_SOURCE_DIR}/externals/skia/include/utils
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ See also `example` directory.
```
$ clang-format --version
clang-format version 7.0.0 (tags/google/stable/2018-04-24)
clang-format version 8.0.0 (tags/google/stable/2019-01-18)

$ clang-format -i -style=file src/*.cpp src/*.h
```
Expand Down
4 changes: 2 additions & 2 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ int main() {
EgGenerateParams params;
memset(&params, 0, sizeof(params));

params.fText = "絵文\nあいうえおかきくけこ";
params.fText = "絵文\n字。";
params.fWidth = 256;
params.fHeight = 256;
params.fColor = 0xFF000000;
params.fBackgroundColor = 0x00FFFFFF;
params.fTextAlign = kCenter_Align;
params.fTextAlign = kLeft_Align;
params.fTextSizeFixed = false;
params.fDisableStretch = false;
params.fTypefaceFile = "./example/NotoSansMonoCJKjp-Bold.otf";
Expand Down
2 changes: 1 addition & 1 deletion externals/depot_tools
Submodule depot_tools updated from db0055 to c42022
2 changes: 1 addition & 1 deletion externals/skia
Submodule skia updated from 3605fd to c76bf2
64 changes: 42 additions & 22 deletions src/EgLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {
*/
EgLine::MeasureSpec EgLine::measureAdjusted() {
SkPaint paint = preparePaintForMeasure();
SkFont font = prepareFontForMeasure();
SkRect bounds;
MeasureSpec spec;

Expand All @@ -29,8 +30,9 @@ EgLine::MeasureSpec EgLine::measureAdjusted() {
if (fDisableStretch) {
for (SkScalar i = minTextSize; i > SkIntToScalar(0);
i -= SkDoubleToScalar(0.5)) {
paint.setTextSize(i);
paint.measureText(fText.c_str(), fText.length(), &bounds);
font.setSize(i);
font.measureText(fText.c_str(), fText.length(),
SkTextEncoding::kUTF8, &bounds, &paint);
if (bounds.width() < fWidth) {
minTextSize = i;
break;
Expand All @@ -40,8 +42,9 @@ EgLine::MeasureSpec EgLine::measureAdjusted() {

for (SkScalar i = minTextSize; i < maxTextSize;
i += SkDoubleToScalar(0.5)) {
paint.setTextSize(i);
paint.measureText(fText.c_str(), fText.length(), &bounds);
font.setSize(i);
font.measureText(fText.c_str(), fText.length(),
SkTextEncoding::kUTF8, &bounds, &paint);

if (bounds.height() > fLineHeight) break;
if (fDisableStretch && bounds.width() > fWidth) break;
Expand All @@ -57,12 +60,13 @@ EgLine::MeasureSpec EgLine::measureAdjusted() {

// 横方向圧縮が必要な場合: 圧縮率の調整
if (prevBounds.width() > fWidth) {
paint.setTextSize(prevTextSize);
font.setSize(prevTextSize);

for (SkScalar i = fWidth / prevBounds.width(); i > SkDoubleToScalar(0);
i -= SkDoubleToScalar(0.0001)) {
paint.setTextScaleX(i);
paint.measureText(fText.c_str(), fText.length(), &bounds);
font.setScaleX(i);
font.measureText(fText.c_str(), fText.length(),
SkTextEncoding::kUTF8, &bounds, &paint);
if (bounds.width() <= fWidth) {
spec.fBounds = bounds;
spec.fTextScaleX = i;
Expand All @@ -81,11 +85,13 @@ EgLine::MeasureSpec EgLine::measureAdjusted() {
*/
EgLine::MeasureSpec EgLine::measureSizeFixed(SkScalar textSize) {
SkPaint paint = preparePaintForMeasure();
SkFont font = prepareFontForMeasure();
SkRect bounds;
MeasureSpec spec;

paint.setTextSize(textSize);
paint.measureText(fText.c_str(), fText.length(), &bounds);
font.setSize(textSize);
font.measureText(fText.c_str(), fText.length(), SkTextEncoding::kUTF8,
&bounds);

spec.fTextSize = textSize;
spec.fBounds = bounds;
Expand All @@ -95,8 +101,9 @@ EgLine::MeasureSpec EgLine::measureSizeFixed(SkScalar textSize) {
if (bounds.width() > fWidth) {
for (SkScalar i = fWidth / bounds.width(); i > SkDoubleToScalar(0);
i -= SkDoubleToScalar(0.0001)) {
paint.setTextScaleX(i);
paint.measureText(fText.c_str(), fText.length(), &bounds);
font.setScaleX(i);
font.measureText(fText.c_str(), fText.length(),
SkTextEncoding::kUTF8, &bounds);
if (bounds.width() <= fWidth) {
spec.fBounds = bounds;
spec.fTextScaleX = i;
Expand All @@ -109,7 +116,8 @@ EgLine::MeasureSpec EgLine::measureSizeFixed(SkScalar textSize) {
}

void EgLine::draw(SkCanvas *canvas, SkScalar y, const MeasureSpec &spec) {
SkPaint paint = preparePaintForDraw(spec.fTextSize);
SkPaint paint = preparePaintForDraw();
SkFont font = prepareFontForDraw(spec);

// for X-axis
SkScalar x;
Expand Down Expand Up @@ -137,32 +145,44 @@ void EgLine::draw(SkCanvas *canvas, SkScalar y, const MeasureSpec &spec) {
// for Y-axis
SkScalar offsetY = (fLineHeight - spec.fBounds.height()) / SkIntToScalar(2);

paint.setTextScaleX(spec.fTextScaleX);
SkTextUtils::DrawString(canvas, fText.c_str(), x,
y - spec.fBounds.fTop + offsetY, paint);
y - spec.fBounds.fTop + offsetY, font, paint);
}

SkPaint EgLine::preparePaintForMeasure() {
SkPaint paint;
paint.setColor(SK_ColorBLACK);
paint.setAntiAlias(true);

if (fTypeface != nullptr) {
paint.setTypeface(fTypeface);
}

return paint;
}

SkPaint EgLine::preparePaintForDraw(SkScalar textSize) {
SkPaint EgLine::preparePaintForDraw() {
SkPaint paint;
paint.setColor(fColor);
paint.setAntiAlias(true);
paint.setTextSize(textSize);

return paint;
}

SkFont EgLine::prepareFontForMeasure() {
SkFont font;

if (fTypeface != nullptr) {
paint.setTypeface(fTypeface);
font.setTypeface(fTypeface);
}

return paint;
return font;
}

SkFont EgLine::prepareFontForDraw(const MeasureSpec &spec) {
SkFont font;
font.setSize(spec.fTextSize);
font.setScaleX(spec.fTextScaleX);

if (fTypeface != nullptr) {
font.setTypeface(fTypeface);
}

return font;
}
5 changes: 4 additions & 1 deletion src/EgLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "SkCanvas.h"
#include "SkColor.h"
#include "SkFont.h"
#include "SkPaint.h"
#include "SkRect.h"
#include "SkScalar.h"
Expand Down Expand Up @@ -51,7 +52,9 @@ class EgLine final {
bool fDisableStretch;

SkPaint preparePaintForMeasure();
SkPaint preparePaintForDraw(SkScalar textSize);
SkPaint preparePaintForDraw();
SkFont prepareFontForMeasure();
SkFont prepareFontForDraw(const MeasureSpec &measureSpec);
MeasureSpec measureAdjusted();
MeasureSpec measureSizeFixed(SkScalar textSize);
};
Expand Down

0 comments on commit 999f9bb

Please sign in to comment.