MCPcopy Create free account
hub / github.com/FloatTech/ZeroBot-Plugin-Playground / createPic2

Method createPic2

plugin/draw/draw.go:325–381  ·  view source on GitHub ↗

创建图片

(lt *location, titlec *titleColor, info []string)

Source from the content-addressed store, hash-verified

323
324// 创建图片
325func (mp *mpic) createPic2(lt *location, titlec *titleColor, info []string) (image.Image, error) {
326 fontSize := mp.fontSize
327 one := gg.NewContext(1280, 256+len(mp.info)*15)
328 if err := one.LoadFontFace(mp.font1, fontSize); err != nil { // 加载字体
329 return nil, err
330 }
331 lineTexts := make([]string, 0, 32)
332 rlx := lt.rlineX
333 rly := lt.rlineY
334 lh := lt.lastH
335 for i := 0; i < len(info); i++ { // 遍历文字切片
336 lineText, textW, textH, tmpw := "", 0.0, 0.0, 0.0
337 if mp.isDouble {
338 if strings.Contains(info[i], ": ● ") || strings.Contains(info[i], ": ○ ") {
339 titlec.randfill() // 随机一次颜色
340 }
341 }
342 for len(info[i]) > 0 {
343 lineText, tmpw = truncate(one, info[i], lt.maxTwidth)
344 lineTexts = append(lineTexts, lineText)
345 if tmpw > textW {
346 textW = tmpw
347 }
348 if len(lineText) >= len(info[i]) {
349 break // 如果写入的文本大于等于本次写入的文本 则跳出
350 }
351 textH += fontSize * 1.3 // 截断一次加一行高度
352 info[i] = info[i][len(lineText):] // 丢弃已经写入的文字并重新赋值
353 }
354 threeW, threeH := textW+fontSize, (textH + (fontSize * 1.2)) // 圆角矩形宽度和高度
355 if int(rlx+textW)+int(fontSize*2) > one.W() { // 越界
356 rly += float64(lh) + fontSize/4 // 加一次高度
357 rlx = 5 // 重置宽度位置
358 if threeH+rly+fontSize >= float64(one.H()) { // 超出最大高度则进行加高
359 imgtmp := gg.NewContext(one.W(), int(rly+threeH*mp.multiple)) // 高度
360 imgtmp.DrawImage(one.Image(), 0, 0)
361 one = gg.NewContextForImage(imgtmp.Image())
362 if err := one.LoadFontFace(mp.font1, mp.fontSize); err != nil { // 加载字体
363 return nil, err
364 }
365 }
366 dx := rlx + 13 // 圆角矩形位置宽度
367 one.DrawRoundedRectangle(dx, rly, threeW, threeH, 20.0) // 创建圆角矩形
368 titlec.drawsc(one, fontSize, dx, rly, lineTexts)
369 rlx += threeW + fontSize/2 // 添加后加一次宽度
370 lh = int(threeH)
371 } else {
372 dx := rlx + 13 // 圆角矩形位置宽度
373 one.DrawRoundedRectangle(dx, rly, threeW, threeH, 20.0) // 创建圆角矩形
374 titlec.drawsc(one, fontSize, dx, rly, lineTexts)
375 rlx += threeW + fontSize/2 // 添加后加一次宽度
376 lh = int(threeH)
377 }
378 lineTexts = lineTexts[:0]
379 }
380 return one.Image(), nil
381}
382

Callers 1

createPicMethod · 0.95

Calls 3

truncateFunction · 0.85
randfillMethod · 0.80
drawscMethod · 0.80

Tested by

no test coverage detected