Skip to content

Instantly share code, notes, and snippets.

在 Facebook 上分享 Dcard 文章時,如果沒有指定圖片的話,可能會看到如下的文章摘要:

這張圖片是透過 Go library draw2d 產生的,它有類似 HTML5 canvas 的 API,所以畫一些簡單的圖形都還算小菜一碟,然而在處理文字上就沒這麼得心應手了。

初次嘗試

src, _ := loadBaseImageForPost(post)
func drawLine(gc *draw2dimg.GraphicContext, str string, maxX float64, maxY float64, x float64, y float64) (bool, float64) {
// Handle empty line
if len(strings.TrimSpace(str)) == 0 {
return false, y + gc.Current.FontSize*lineHeight
}
list := []rune(str)
length := len(list)
offsetX := x
offsetY := y
func drawLine(gc *draw2dimg.GraphicContext, str string, maxX float64, maxY float64, x float64, y float64) (bool, float64) {
// Handle empty line
if len(strings.TrimSpace(str)) == 0 {
return false, y + gc.Current.FontSize*lineHeight
}
list := []rune(str)
length := len(list)
offsetX := x
offsetY := y
func drawLine(gc *draw2dimg.GraphicContext, str string, x float64, y float64) {
gc.FillStringAt(str, x, y)
}
func drawText(gc *draw2dimg.GraphicContext, str string, width float64, height float64, x float64, y float64) {
str = strings.Trim(str, " \n\r\t")
lines := strings.Split(str, "\n")
fontSize := gc.Current.FontSize
offsetY := y
wrapLines := []string{}
draw2d.SetFontFolder("static/fonts")
draw2d.SetFontNamer(func(fontData draw2d.FontData) string {
return "WenQuanYiMicroHei.ttf"
})
2016/11/04 16:06:16 open ../resource/font/luxisr.ttf: no such file or directory
2016/11/04 16:06:16 open ../resource/font/luxisr.ttf: no such file or directory
2016/11/04 16:06:16 No font set, and no default font available.
src, _ := loadBaseImageForPost(post)
img := image.NewRGBA(src.Bounds())
gc := draw2dimg.NewGraphicContext(img)
gc.DrawImage(src)
// Draw title
gc.SetFillColor(color.RGBA{0xff, 0xff, 0xff, 0xff})
gc.SetFontSize(26)
gc.FillStringAt(post.Title, 140, 81.5)
@tommy351
tommy351 / benchmark.js
Last active August 29, 2015 14:15
hexo-util highlight benchmark
'use strict';
var highlight = require('highlight.js');
var Highlights = require('highlights');
var highlighter = new Highlights();
var util = require('../lib');
var fs = require('fs');
var pathFn = require('path');
var fixture = fs.readFileSync(pathFn.join(__dirname, 'fixtures', 'q3.js'), 'utf8');
@tommy351
tommy351 / gist:73f8c34d839c67a0732f
Last active August 29, 2015 14:10
The solution to the question 3. https://paiza.jp/poh/enkoi
var input = '';
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
input += chunk;
});
process.stdin.on('end', function() {
import exp.*;
import flowd.*;
class P99 extends FunNode {
P99(){
super("P99");
Var i = addVar("i");
Var j = addVar("j");
addBeginNode("L0");