Skip to content

Commit

Permalink
feat(clanf-format): use formatter #4
Browse files Browse the repository at this point in the history
  • Loading branch information
pine committed Mar 21, 2018
1 parent 79ca368 commit 5991817
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 41 deletions.
115 changes: 115 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
RawStringFormats:
- Delimiter: pb
Language: TextProto
BasedOnStyle: google
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,29 @@ int main(void) {
params.fWidth = 256;
params.fHeight = 256;

EgGenerateResult result;
EgGenerateResult result;
if (emoji_generate(&params, &result) != EG_OK) {
emoji_free(&result);
return -1;
}

FILE *fp = fopen("./emoji.png", "w");
fwrite(result.fData, result.fSize, 1, fp);
fclose(fp);

emoji_free(&result);
return 0;
}
```
See also `example` directory.
## Development
### Run formatter
```
$ clang-format -i -style=file src/*.cpp src/*.h
```
## License
MIT &copy; Emoji Generator
24 changes: 11 additions & 13 deletions src/EgGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@
#include "EgGenerator.h"
#include "EgLine.h"

EgGenerator::EgGenerator() {
}
EgGenerator::EgGenerator() {}

void EgGenerator::setTypefaceFromName(const char* familyName) {
void EgGenerator::setTypefaceFromName(const char *familyName) {
fTypeface = SkTypeface::MakeFromName(familyName, SkFontStyle());
}

void EgGenerator::setTypefaceFromFile(const char* path) {
void EgGenerator::setTypefaceFromFile(const char *path) {
fTypeface = SkTypeface::MakeFromFile(path);
}


void EgGenerator::setText(const char* text) {
void EgGenerator::setText(const char *text) {
std::istringstream stream(text);
std::string line;
std::vector<std::string> texts;
Expand All @@ -39,15 +37,15 @@ void EgGenerator::setText(const char* text) {

sk_sp<SkData> EgGenerator::generate() {
sk_sp<SkSurface> surface = SkSurface::MakeRasterN32Premul(fWidth, fHeight);
SkCanvas* canvas = surface->getCanvas();
SkCanvas *canvas = surface->getCanvas();
canvas->clear(fBackgroundColor);

std::vector<EgLine::MeasureSpec> specs;
SkScalar lineHeight = fHeight / SkIntToScalar(fTexts.size());

// 行ボックスを作成
std::vector<EgLine> lines;
for (auto& text : fTexts) {
for (auto &text : fTexts) {
EgLine line;
line.setWidth(fWidth);
line.setLineHeight(lineHeight);
Expand All @@ -60,16 +58,17 @@ sk_sp<SkData> EgGenerator::generate() {
}

// 高さ・幅を計算
for (auto& line : lines) {
for (auto &line : lines) {
specs.push_back(line.measure());
}

// サイズ固定モード: 最小テキストサイズで再計算
if (fTextSizeFixed) {
auto foundSpecItr = std::min_element(
specs.begin(), specs.end(),
[](EgLine::MeasureSpec lhs, EgLine::MeasureSpec rhs) { return lhs.fTextSize < rhs.fTextSize; }
);
[](EgLine::MeasureSpec lhs, EgLine::MeasureSpec rhs) {
return lhs.fTextSize < rhs.fTextSize;
});

if (foundSpecItr != specs.end()) {
SkScalar minTextSize = foundSpecItr->fTextSize;
Expand All @@ -81,7 +80,7 @@ sk_sp<SkData> EgGenerator::generate() {

// テキストを描画
for (std::size_t i = 0; i < lines.size(); ++i) {
lines[i].draw(canvas, lineHeight * i, specs[i]);
lines[i].draw(canvas, lineHeight * i, specs[i]);
}

// エンコード
Expand All @@ -90,4 +89,3 @@ sk_sp<SkData> EgGenerator::generate() {

return data;
}

16 changes: 10 additions & 6 deletions src/EgGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@

#include "SkCanvas.h"
#include "SkColor.h"
#include "SkTypeface.h"
#include "SkScalar.h"
#include "SkTypeface.h"

class EgGenerator final {
public:
EgGenerator();

sk_sp<SkData> generate();

void setText(const char* text);
void setText(const char *text);
void setWidth(uint32_t width) { fWidth = SkIntToScalar(width); }
void setHeight(uint32_t height) { fHeight = SkIntToScalar(height); }
void setTextAlign(SkPaint::Align align) { fTextAlign = align; }
void setTextSizeFixed(bool textSizeFixed) { fTextSizeFixed = textSizeFixed; }
void setDisableStretch(bool disableStretch) { fDisableStretch = disableStretch; }
void setTypefaceFromName(const char* familyName);
void setTypefaceFromFile(const char* path);
void setTextSizeFixed(bool textSizeFixed) {
fTextSizeFixed = textSizeFixed;
}
void setDisableStretch(bool disableStretch) {
fDisableStretch = disableStretch;
}
void setTypefaceFromName(const char *familyName);
void setTypefaceFromFile(const char *path);
void setColor(SkColor color) { fColor = color; }
void setBackgroundColor(SkColor color) { fBackgroundColor = color; }
void setFormat(SkEncodedImageFormat format) { fFormat = format; }
Expand Down
25 changes: 15 additions & 10 deletions src/EgLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

#include "EgLine.h"

EgLine::EgLine() {
}
EgLine::EgLine() {}

EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {
SkPaint paint;
Expand All @@ -23,7 +22,8 @@ EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {

measureSpec.fTextSize = textSize;
measureSpec.fBounds = bounds;
measureSpec.fTextScaleX = bounds.width() > fWidth ? fWidth / bounds.width() : 1;
measureSpec.fTextScaleX =
bounds.width() > fWidth ? fWidth / bounds.width() : 1;
return measureSpec;
}

Expand All @@ -36,7 +36,8 @@ EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {
if (!fText.empty()) {
// 非伸縮モード: 初期フォントサイズを調整
if (fDisableStretch) {
for (SkScalar i = minTextSize; i > SkIntToScalar(0); i -= SkDoubleToScalar(0.5)) {
for (SkScalar i = minTextSize; i > SkIntToScalar(0);
i -= SkDoubleToScalar(0.5)) {
paint.setTextSize(i);
paint.measureText(fText.c_str(), fText.length(), &bounds);
if (bounds.width() < fWidth) {
Expand All @@ -46,7 +47,8 @@ EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {
}
}

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

Expand All @@ -60,13 +62,14 @@ EgLine::MeasureSpec EgLine::measure(SkScalar textSize) {

measureSpec.fTextSize = prevTextSize;
measureSpec.fBounds = prevBounds;
measureSpec.fTextScaleX =
prevBounds.width() > fWidth ? fWidth / prevBounds.width() : SkIntToScalar(1);
measureSpec.fTextScaleX = prevBounds.width() > fWidth
? fWidth / prevBounds.width()
: SkIntToScalar(1);

return measureSpec;
}

void EgLine::draw(SkCanvas* canvas, std::size_t y, const MeasureSpec& spec) {
void EgLine::draw(SkCanvas *canvas, std::size_t y, const MeasureSpec &spec) {
SkPaint paint;
paint.setColor(fColor);
paint.setAntiAlias(true);
Expand All @@ -84,7 +87,8 @@ void EgLine::draw(SkCanvas* canvas, std::size_t y, const MeasureSpec& spec) {
if (spec.fTextScaleX < SkIntToScalar(1)) {
x = -spec.fBounds.fLeft * spec.fTextScaleX;
} else {
x = (fWidth - spec.fBounds.width()) / SkIntToScalar(2) - spec.fBounds.fLeft;
x = (fWidth - spec.fBounds.width()) / SkIntToScalar(2) -
spec.fBounds.fLeft;
}
break;
case SkPaint::kRight_Align:
Expand All @@ -100,5 +104,6 @@ void EgLine::draw(SkCanvas* canvas, std::size_t y, const MeasureSpec& spec) {
SkScalar offsetY = (fLineHeight - spec.fBounds.height()) / SkIntToScalar(2);

paint.setTextScaleX(spec.fTextScaleX);
canvas->drawString(fText.c_str(), x, y - spec.fBounds.fTop + offsetY, paint);
canvas->drawString(fText.c_str(), x, y - spec.fBounds.fTop + offsetY,
paint);
}
8 changes: 5 additions & 3 deletions src/EgLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ class EgLine final {

EgLine();

void setText(const std::string& text) { fText = text; }
void setText(const std::string &text) { fText = text; }
void setWidth(SkScalar width) { fWidth = width; }
void setLineHeight(SkScalar lineHeight) { fLineHeight = lineHeight; }
void setTypeface(sk_sp<SkTypeface> typeface) { fTypeface = typeface; }
void setTextAlign(SkPaint::Align textAlign) { fTextAlign = textAlign; }
void setColor(SkColor color) { fColor = color; }
void setDisableStretch(bool disableStretch) { fDisableStretch = disableStretch; }
void setDisableStretch(bool disableStretch) {
fDisableStretch = disableStretch;
}

MeasureSpec measure(SkScalar textSize = SK_ScalarNaN);
void draw(SkCanvas* canvas, std::size_t y, const MeasureSpec& spec);
void draw(SkCanvas *canvas, std::size_t y, const MeasureSpec &spec);

private:
std::string fText = "";
Expand Down
Loading

0 comments on commit 5991817

Please sign in to comment.